diff --git a/buildbot_gitea/test/test_webhook.py b/buildbot_gitea/test/test_webhook.py index 5ee94fb..5ef70d5 100644 --- a/buildbot_gitea/test/test_webhook.py +++ b/buildbot_gitea/test/test_webhook.py @@ -1,11 +1,8 @@ - +import calendar import buildbot.www.change_hook as change_hook from buildbot.test.fake.web import FakeRequest from buildbot.test.fake.web import fakeMasterForHooks -import mock - -import calendar from twisted.internet import defer from twisted.trial import unittest @@ -355,6 +352,356 @@ giteaJsonPullRequestPayload = rb""" } """ +giteaJsonPullRequestPayloadNotMergeable = rb""" +{ + "secret": "test", + "action": "opened", + "number": 1, + "pull_request": { + "id": 8, + "url": "", + "number": 1, + "user": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "title": "TestPR", + "body": "", + "labels": [], + "milestone": null, + "assignee": null, + "assignees": null, + "state": "open", + "comments": 0, + "html_url": "https://git.example.com/max/webhook_test/pulls/1", + "diff_url": "https://git.example.com/max/webhook_test/pulls/1.diff", + "patch_url": "https://git.example.com/max/webhook_test/pulls/1.patch", + "mergeable": false, + "merged": false, + "merged_at": null, + "merge_commit_sha": null, + "merged_by": null, + "base": { + "label": "master", + "ref": "master", + "sha": "7c5de0796c409e7802abe759113d7fc37e0d6578", + "repo_id": 20, + "repo": { + "id": 20, + "owner": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "name": "webhook_test", + "full_name": "max/webhook_test", + "description": "", + "empty": false, + "private": true, + "fork": false, + "parent": null, + "mirror": false, + "size": 48, + "html_url": "https://git.example.com/max/webhook_test", + "ssh_url": "ssh://git@git.example.com/max/webhook_test.git", + "clone_url": "https://git.example.com/max/webhook_test.git", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "default_branch": "master", + "created_at": "2018-09-04T10:45:23Z", + "updated_at": "2018-09-04T12:10:14Z", + "permissions": { + "admin": false, + "push": false, + "pull": false + } + } + }, + "head": { + "label": "feature-branch", + "ref": "feature-branch", + "sha": "9d7157cc4a137b3e1dfe92750ccfb1bbad239f99", + "repo_id": 20, + "repo": { + "id": 20, + "owner": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "name": "webhook_test", + "full_name": "max/webhook_test", + "description": "", + "empty": false, + "private": true, + "fork": false, + "parent": null, + "mirror": false, + "size": 48, + "html_url": "https://git.example.com/max/webhook_test", + "ssh_url": "ssh://git@git.example.com/max/webhook_test.git", + "clone_url": "https://git.example.com/max/webhook_test.git", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "default_branch": "master", + "created_at": "2018-09-04T10:45:23Z", + "updated_at": "2018-09-04T12:10:14Z", + "permissions": { + "admin": false, + "push": false, + "pull": false + } + } + }, + "merge_base": "7c5de0796c409e7802abe759113d7fc37e0d6578", + "due_date": null, + "created_at": "2018-09-04T12:14:49Z", + "updated_at": "2018-09-04T12:14:49Z", + "closed_at": null + }, + "repository": { + "id": 20, + "owner": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "name": "webhook_test", + "full_name": "max/webhook_test", + "description": "", + "empty": false, + "private": true, + "fork": false, + "parent": null, + "mirror": false, + "size": 48, + "html_url": "https://git.example.com/max/webhook_test", + "ssh_url": "ssh://git@git.example.com/max/webhook_test.git", + "clone_url": "https://git.example.com/max/webhook_test.git", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "default_branch": "master", + "created_at": "2018-09-04T10:45:23Z", + "updated_at": "2018-09-04T12:10:14Z", + "permissions": { + "admin": true, + "push": true, + "pull": true + } + }, + "sender": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + } +} +""" + +giteaJsonPullRequestPayloadMerged = rb""" +{ + "secret": "test", + "action": "opened", + "number": 1, + "pull_request": { + "id": 8, + "url": "", + "number": 1, + "user": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "title": "TestPR", + "body": "", + "labels": [], + "milestone": null, + "assignee": null, + "assignees": null, + "state": "open", + "comments": 0, + "html_url": "https://git.example.com/max/webhook_test/pulls/1", + "diff_url": "https://git.example.com/max/webhook_test/pulls/1.diff", + "patch_url": "https://git.example.com/max/webhook_test/pulls/1.patch", + "mergeable": true, + "merged": true, + "merged_at": null, + "merge_commit_sha": null, + "merged_by": null, + "base": { + "label": "master", + "ref": "master", + "sha": "7c5de0796c409e7802abe759113d7fc37e0d6578", + "repo_id": 20, + "repo": { + "id": 20, + "owner": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "name": "webhook_test", + "full_name": "max/webhook_test", + "description": "", + "empty": false, + "private": true, + "fork": false, + "parent": null, + "mirror": false, + "size": 48, + "html_url": "https://git.example.com/max/webhook_test", + "ssh_url": "ssh://git@git.example.com/max/webhook_test.git", + "clone_url": "https://git.example.com/max/webhook_test.git", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "default_branch": "master", + "created_at": "2018-09-04T10:45:23Z", + "updated_at": "2018-09-04T12:10:14Z", + "permissions": { + "admin": false, + "push": false, + "pull": false + } + } + }, + "head": { + "label": "feature-branch", + "ref": "feature-branch", + "sha": "9d7157cc4a137b3e1dfe92750ccfb1bbad239f99", + "repo_id": 20, + "repo": { + "id": 20, + "owner": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "name": "webhook_test", + "full_name": "max/webhook_test", + "description": "", + "empty": false, + "private": true, + "fork": false, + "parent": null, + "mirror": false, + "size": 48, + "html_url": "https://git.example.com/max/webhook_test", + "ssh_url": "ssh://git@git.example.com/max/webhook_test.git", + "clone_url": "https://git.example.com/max/webhook_test.git", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "default_branch": "master", + "created_at": "2018-09-04T10:45:23Z", + "updated_at": "2018-09-04T12:10:14Z", + "permissions": { + "admin": false, + "push": false, + "pull": false + } + } + }, + "merge_base": "7c5de0796c409e7802abe759113d7fc37e0d6578", + "due_date": null, + "created_at": "2018-09-04T12:14:49Z", + "updated_at": "2018-09-04T12:14:49Z", + "closed_at": null + }, + "repository": { + "id": 20, + "owner": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + }, + "name": "webhook_test", + "full_name": "max/webhook_test", + "description": "", + "empty": false, + "private": true, + "fork": false, + "parent": null, + "mirror": false, + "size": 48, + "html_url": "https://git.example.com/max/webhook_test", + "ssh_url": "ssh://git@git.example.com/max/webhook_test.git", + "clone_url": "https://git.example.com/max/webhook_test.git", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "default_branch": "master", + "created_at": "2018-09-04T10:45:23Z", + "updated_at": "2018-09-04T12:10:14Z", + "permissions": { + "admin": true, + "push": true, + "pull": true + } + }, + "sender": { + "id": 1, + "login": "max", + "full_name": "Max Mustermann", + "email": "max@example.com", + "avatar_url": "https://secure.gravatar.com/avatar/c9a5fca94b7fd6f8d4dbab8d1575e4fc?d=identicon", + "language": "en-US", + "username": "max" + } +} +""" + class TestChangeHookGiteaPush(unittest.TestCase): def setUp(self): @@ -378,7 +725,8 @@ class TestChangeHookGiteaPush(unittest.TestCase): change["comments"], "TestBranch\n") self.assertEqual(change["branch"], "feature-branch") self.assertEqual(change[ - "revlink"], "https://git.example.com/max/webhook_test/commit/9d7157cc4a137b3e1dfe92750ccfb1bbad239f99") + "revlink"], + "https://git.example.com/max/webhook_test/commit/9d7157cc4a137b3e1dfe92750ccfb1bbad239f99") def checkChangesFromPullRequest(self, codebase=None): self.assertEqual(len(self.changeHook.master.addedChanges), 1) @@ -429,6 +777,24 @@ class TestChangeHookGiteaPush(unittest.TestCase): res = yield self.request.test_render(self.changeHook) self.checkChangesFromPullRequest(res) + @defer.inlineCallbacks + def testPullRequestNotMergeableEvent(self): + self.request = FakeRequest(content=giteaJsonPullRequestPayloadNotMergeable) + self.request.uri = b'/change_hook/gitea' + self.request.method = b'POST' + self.request.received_headers[_HEADER_EVENT_TYPE] = b"pull_request" + yield self.request.test_render(self.changeHook) + self.assertEqual(len(self.changeHook.master.addedChanges), 0) + + @defer.inlineCallbacks + def testPullRequestMergedEvent(self): + self.request = FakeRequest(content=giteaJsonPullRequestPayloadMerged) + self.request.uri = b'/change_hook/gitea' + self.request.method = b'POST' + self.request.received_headers[_HEADER_EVENT_TYPE] = b"pull_request" + yield self.request.test_render(self.changeHook) + self.assertEqual(len(self.changeHook.master.addedChanges), 0) + class TestChangeHookGiteaSecretPhrase(unittest.TestCase): def setUp(self): diff --git a/buildbot_gitea/webhook.py b/buildbot_gitea/webhook.py index 093c979..0795981 100644 --- a/buildbot_gitea/webhook.py +++ b/buildbot_gitea/webhook.py @@ -59,6 +59,12 @@ class GiteaHandler(BaseHookHandler): log.msg("Gitea Pull Request event '{}' ignored".format(action)) return [] pull_request = payload['pull_request'] + if not pull_request['mergeable']: + log.msg("Gitea Pull Request ignored because it is not mergeable.") + return [] + if pull_request['merged']: + log.msg("Gitea Pull Request ignored because it is already merged.") + return [] timestamp = dateparse(pull_request['updated_at']) base = pull_request['base'] head = pull_request['head'] @@ -104,8 +110,8 @@ class GiteaHandler(BaseHookHandler): try: content = request.content.read() payload = json.loads(bytes2unicode(content)) - except Exception as e: - raise ValueError('Error loading JSON: ' + str(e)) + except Exception as exception: + raise ValueError('Error loading JSON: ' + str(exception)) if secret is not None and secret != payload['secret']: raise ValueError('Invalid secret') event_type = bytes2unicode(request.getHeader(_HEADER_EVENT_TYPE))