-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(webhook): Add GitLab webhook end-point. #1448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,101 @@ | |
log = logging.getLogger(__name__) | ||
|
||
|
||
class GitLabWebHookTest(TestCase): | ||
fixtures = ["eric", "test_data"] | ||
|
||
def tearDown(self): | ||
tasks.update_docs = self.old_bd | ||
|
||
def setUp(self): | ||
self.old_bd = tasks.update_docs | ||
|
||
def mock(*args, **kwargs): | ||
log.info("Mocking for great profit and speed.") | ||
tasks.update_docs = mock | ||
tasks.update_docs.delay = mock | ||
|
||
self.client.login(username='eric', password='test') | ||
self.payload = { | ||
"object_kind": "push", | ||
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22", | ||
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", | ||
"ref": "refs/heads/awesome", | ||
"user_id": 4, | ||
"user_name": "John Smith", | ||
"user_email": "[email protected]", | ||
"project_id": 15, | ||
"repository": { | ||
"name": "Diaspora", | ||
"url": "[email protected]:rtfd/readthedocs.org.git", | ||
"description": "", | ||
"homepage": "http://github.com/rtfd/readthedocs.org", | ||
"git_http_url": "http://github.com/rtfd/readthedocs.org.git", | ||
"git_ssh_url": "[email protected]:rtfd/readthedocs.org.git", | ||
"visibility_level":0 | ||
}, | ||
"commits": [ | ||
{ | ||
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", | ||
"message": "Update Catalan translation to e38cb41.", | ||
"timestamp": "2011-12-12T14:27:31+02:00", | ||
"url": "http://github.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", | ||
"author": { | ||
"name": "Jordi Mallach", | ||
"email": "[email protected]" | ||
} | ||
}, | ||
{ | ||
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", | ||
"message": "fixed readme", | ||
"timestamp": "2012-01-03T23:36:29+02:00", | ||
"url": "http://github.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", | ||
"author": { | ||
"name": "GitLab dev user", | ||
"email": "gitlabdev@dv6700.(none)" | ||
} | ||
} | ||
], | ||
"total_commits_count": 4 | ||
} | ||
|
||
def test_gitlab_post_commit_hook_builds_branch_docs_if_it_should(self): | ||
""" | ||
Test the github post commit hook to see if it will only build | ||
versions that are set to be built if the branch they refer to | ||
is updated. Otherwise it is no op. | ||
""" | ||
r = self.client.post('/gitlab/', {'payload': json.dumps(self.payload)}) | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.content, '(URL Build) Build Started: github.com/rtfd/readthedocs.org [awesome]') | ||
self.payload['ref'] = 'refs/heads/not_ok' | ||
r = self.client.post('/gitlab/', {'payload': json.dumps(self.payload)}) | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.content, '(URL Build) Not Building: github.com/rtfd/readthedocs.org [not_ok]') | ||
self.payload['ref'] = 'refs/heads/unknown' | ||
r = self.client.post('/gitlab/', {'payload': json.dumps(self.payload)}) | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.content, '(URL Build) Not Building: github.com/rtfd/readthedocs.org []') | ||
|
||
def test_gitlab_post_commit_knows_default_branches(self): | ||
""" | ||
Test the gitlab post commit hook so that the default branch | ||
will be respected and built as the latest version. | ||
""" | ||
rtd = Project.objects.get(slug='read-the-docs') | ||
old_default = rtd.default_branch | ||
rtd.default_branch = 'master' | ||
rtd.save() | ||
self.payload['ref'] = 'refs/heads/master' | ||
|
||
r = self.client.post('/gitlab/', {'payload': json.dumps(self.payload)}) | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.content, '(URL Build) Build Started: github.com/rtfd/readthedocs.org [latest]') | ||
|
||
rtd.default_branch = old_default | ||
rtd.save() | ||
|
||
|
||
class PostCommitTest(TestCase): | ||
fixtures = ["eric", "test_data"] | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a real POST request from a gitlab commit hook? I'm asking because it includes github.com URLs which seem a bit odd. It won't make any difference in the logic but it will be confusing for future developers who look at the tests and see github URLs in the tests for gitlab.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gregmuellegger, this payload data is based on the POST example provided by GitLab.
The modifications introducing
github
were necessary to pass the tests, as the project lookup matches the git url againstjiasu.xzqcsaa.nyc.mn/rtfd/readthedocs.org
.