diff --git a/buildbot_gitea/reporter.py b/buildbot_gitea/reporter.py index f30b75b..05ff15d 100644 --- a/buildbot_gitea/reporter.py +++ b/buildbot_gitea/reporter.py @@ -158,10 +158,15 @@ class GiteaStatusPush(http.ReporterBase): for sourcestamp in sourcestamps: sha = sourcestamp['revision'] + repository_owner = None if sha is None: # No special revision for this, so ignore it continue - if 'repository_name' in props: + # If this is a pull request, send the status to the head repository + if 'pr_id' in props: + repository_name = props['head_reponame'] + repository_owner = props['head_owner'] + elif 'repository_name' in props: repository_name = props['repository_name'] else: match = re.match(self.ssh_url_match, sourcestamp['repository']) @@ -172,17 +177,18 @@ class GiteaStatusPush(http.ReporterBase): "Could not send status, " "build has no repository_name property for Gitea.") continue - if 'owner' in props: - repository_owner = props['owner'] - else: - match = re.match(self.ssh_url_match, sourcestamp['repository']) - if match is not None: - repository_owner = match.group("owner") + if repository_owner is None: + if 'owner' in props: + repository_owner = props['owner'] else: - log.msg( - "Could not send status, " - "build has no owner property for Gitea.") - continue + match = re.match(self.ssh_url_match, sourcestamp['repository']) + if match is not None: + repository_owner = match.group("owner") + else: + log.msg( + "Could not send status, " + "build has no owner property for Gitea.") + continue try: target_url = build['url'] res = yield self.createStatus( diff --git a/buildbot_gitea/test/test_reporter.py b/buildbot_gitea/test/test_reporter.py index f4bc410..5c7362f 100644 --- a/buildbot_gitea/test/test_reporter.py +++ b/buildbot_gitea/test/test_reporter.py @@ -94,6 +94,24 @@ class TestGiteaStatusPush( build['results'] = FAILURE self.sp._got_event(('builds', 20, 'finished'), build) + @defer.inlineCallbacks + def test_pullrequest(self): + self.setupProps() + self.reporter_test_props["pr_id"] = 42 + self.reporter_test_props["head_owner"] = 'foo' + self.reporter_test_props["head_reponame"] = 'bar' + build = yield self.setupBuildResults(SUCCESS) + # we make sure proper calls to txrequests have been made + self._http.expect( + 'post', + '/api/v1/repos/foo/bar/statuses/d34db33fd43db33f', + json={'state': 'success', + 'target_url': 'http://localhost:8080/#builders/79/builds/0', + 'description': 'Build done.', 'context': 'buildbot/pull_request/Builder0'}) + + build['complete'] = True + self.sp._got_event(('builds', 20, 'finished'), build) + @defer.inlineCallbacks def test_sshurl(self): self.setupProps() diff --git a/buildbot_gitea/webhook.py b/buildbot_gitea/webhook.py index 2659b2f..49b74d1 100644 --- a/buildbot_gitea/webhook.py +++ b/buildbot_gitea/webhook.py @@ -111,6 +111,8 @@ class GiteaHandler(BaseHookHandler): 'head_repo_id': head['repo_id'], 'head_repository': head['repo']['clone_url'], 'head_git_ssh_url': head['repo']['ssh_url'], + 'head_owner': head['repo']['owner']['username'], + 'head_reponame': head['repo']['name'], 'pr_id': pull_request['id'], 'pr_number': pull_request['number'], 'repository_name': repository['name'],