Skip to content

Commit

Permalink
Add gitlab / github webhooks unit tests and newsframents
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirtodorov committed Mar 14, 2017
1 parent 8e60798 commit 461724b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 5 deletions.
1 change: 1 addition & 0 deletions master/buildbot/newsfragments/www_hooks_github.feature
@@ -0,0 +1 @@
:py:class: `~buildbot.www.hooks.github.GitHubEventHandler` now process git tag push events
1 change: 1 addition & 0 deletions master/buildbot/newsfragments/www_hooks_gitlab.feature
@@ -0,0 +1 @@
:py:class: `~buildbot.www.hooks.gitlab` now process git tag push events
59 changes: 59 additions & 0 deletions master/buildbot/test/unit/test_www_hooks_github.py
Expand Up @@ -83,6 +83,54 @@
}
"""

gitJsonPayloadTag = """
{
"before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
"repository": {
"url": "http://github.com/defunkt/github",
"html_url": "http://github.com/defunkt/github",
"name": "github",
"full_name": "defunkt/github",
"description": "You're lookin' at it.",
"watchers": 5,
"forks": 2,
"private": 1,
"owner": {
"email": "fred@flinstone.org",
"name": "defunkt"
}
},
"commits": [
{
"id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
"distinct": true,
"url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
"author": {
"email": "fred@flinstone.org",
"name": "Fred Flinstone"
},
"message": "okay i give in",
"timestamp": "2008-02-15T14:57:17-08:00",
"added": ["filepath.rb"]
},
{
"id": "de8251ff97ee194a289832576287d6f8ad74e3d0",
"url": "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
"author": {
"email": "fred@flinstone.org",
"name": "Fred Flinstone"
},
"message": "update pricing a tad",
"timestamp": "2008-02-15T14:36:34-08:00",
"modified": ["modfile"],
"removed": ["removedFile"]
}
],
"after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
"ref": "refs/tags/v1.0.0"
}
"""

gitJsonPayloadNonBranch = """
{
"before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
Expand Down Expand Up @@ -348,6 +396,17 @@ def test_ping_encoded(self):
def test_ping_json(self):
self._check_ping('{}')

@defer.inlineCallbacks
def test_git_with_push_tag(self):
self.request = _prepare_request('push', gitJsonPayloadTag)
yield self.request.test_render(self.changeHook)

self.assertEqual(len(self.changeHook.master.addedChanges), 2)
change = self.changeHook.master.addedChanges[0]
self.assertEqual(change["author"],
"Fred Flinstone <fred@flinstone.org>")
self.assertEqual(change["branch"], "v1.0.0")

# Test 'base' hook with attributes. We should get a json string
# representing a Change object as a dictionary. All values show be set.
@defer.inlineCallbacks
Expand Down
73 changes: 68 additions & 5 deletions master/buildbot/test/unit/test_www_hooks_gitlab.py
Expand Up @@ -68,15 +68,68 @@
"total_commits_count": 2
}
"""

gitJsonPayloadTag = """
{
"object_kind": "tag_push",
"before": "0000000000000000000000000000000000000000",
"after": "82b3d5ae55f7080f1e6022629cdb57bfae7cccc7",
"ref": "refs/tags/v1.0.0",
"checkout_sha": "82b3d5ae55f7080f1e6022629cdb57bfae7cccc7",
"user_id": 1,
"user_name": "John Smith",
"repository":{
"name": "Example",
"url": "git@localhost:diaspora.git",
"description": "",
"homepage": "http://example.com/jsmith/example",
"git_http_url":"http://example.com/jsmith/example.git",
"git_ssh_url":"git@example.com:jsmith/example.git",
"visibility_level":0
},
"commits": [
{
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"message": "Update Catalan translation to e38cb41.",
"timestamp": "2011-12-12T14:27:31+02:00",
"url": "http://localhost/diaspora/commits/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"author": {
"name": "Jordi Mallach",
"email": "jordi@softcatala.org"
}
},
{
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29+02:00",
"url": "http://localhost/diaspora/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)"
}
}
],
"total_commits_count": 2
}
"""

class TestChangeHookConfiguredWithGitChange(unittest.TestCase):

def setUp(self):
self.changeHook = change_hook.ChangeHookResource(
dialects={'gitlab': True}, master=fakeMasterForHooks())

def check_changes(self, r, project='', codebase=None):
def check_changes_tag_event(self, r, project='', codebase=None):
self.assertEqual(len(self.changeHook.master.addedChanges), 2)
change = self.changeHook.master.addedChanges[0]

self.assertEqual(change["repository"], "git@localhost:diaspora.git")
self.assertEqual(
calendar.timegm(change["when_timestamp"].utctimetuple()),
1323692851
)
self.assertEqual(change["branch"], "v1.0.0")

def check_changes_push_event(self, r, project='', codebase=None):
self.assertEqual(len(self.changeHook.master.addedChanges), 2)
change = self.changeHook.master.addedChanges[0]

Expand Down Expand Up @@ -114,6 +167,7 @@ def check_changes(self, r, project='', codebase=None):
self.assertEqual(change.get("project"), project)
self.assertEqual(change.get("codebase"), codebase)


# Test 'base' hook with attributes. We should get a json string representing
# a Change object as a dictionary. All values show be set.
@defer.inlineCallbacks
Expand All @@ -122,7 +176,7 @@ def testGitWithChange(self):
self.request.uri = "/change_hook/gitlab"
self.request.method = "POST"
res = yield self.request.test_render(self.changeHook)
self.check_changes(res)
self.check_changes_push_event(res)

@defer.inlineCallbacks
def testGitWithChange_WithProjectToo(self):
Expand All @@ -131,7 +185,7 @@ def testGitWithChange_WithProjectToo(self):
self.request.args = {'project': ['MyProject']}
self.request.method = "POST"
res = yield self.request.test_render(self.changeHook)
self.check_changes(res, project="MyProject")
self.check_changes_push_event(res, project="MyProject")

@defer.inlineCallbacks
def testGitWithChange_WithCodebaseToo(self):
Expand All @@ -140,7 +194,16 @@ def testGitWithChange_WithCodebaseToo(self):
self.request.args = {'codebase': ['MyCodebase']}
self.request.method = "POST"
res = yield self.request.test_render(self.changeHook)
self.check_changes(res, codebase="MyCodebase")
self.check_changes_push_event(res, codebase="MyCodebase")

@defer.inlineCallbacks
def testGitWithChange_WithPushTag(self):
self.request = FakeRequest(content=gitJsonPayloadTag)
self.request.uri = "/change_hook/gitlab"
self.request.args = {'codebase': ['MyCodebase']}
self.request.method = "POST"
res = yield self.request.test_render(self.changeHook)
self.check_changes_tag_event(res, codebase="MyCodebase")

def testGitWithNoJson(self):
self.request = FakeRequest()
Expand Down

0 comments on commit 461724b

Please sign in to comment.