Skip to content
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

PR Builder GitLab Integration #6066

Merged
merged 10 commits into from Aug 19, 2019

API tests added

  • Loading branch information...
saadmk11 committed Aug 12, 2019
commit 73c55f3595ee573d8acb85eae9a086c912170daa
@@ -23,6 +23,12 @@
GITHUB_PULL_REQUEST_REOPENED,
GITHUB_PULL_REQUEST_CLOSED,
GITHUB_PULL_REQUEST_SYNC,
GITLAB_MERGE_REQUEST,
GITLAB_MERGE_REQUEST_CLOSE,
GITLAB_MERGE_REQUEST_MERGE,
GITLAB_MERGE_REQUEST_OPEN,
GITLAB_MERGE_REQUEST_REOPEN,
GITLAB_MERGE_REQUEST_UPDATE,
GITLAB_NULL_HASH,
GITLAB_PUSH,
GITLAB_TAG_PUSH,
@@ -794,6 +800,16 @@ def setUp(self):
}
}
}
self.gitlab_merge_request_payload = {
"object_kind": GITLAB_MERGE_REQUEST,
"object_attributes": {
"iid": '2',
"last_commit": {
"id": self.commit
},
"action": "open"
},
}
self.gitlab_payload = {
'object_kind': GITLAB_PUSH,
'ref': 'master',
@@ -1536,6 +1552,291 @@ def test_gitlab_skip_token_validation(self, trigger_build):
)
self.assertEqual(resp.status_code, 200)

@mock.patch('readthedocs.core.utils.trigger_build')
def test_gitlab_merge_request_open_event(self, trigger_build, core_trigger_build):
client = APIClient()

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
self.gitlab_merge_request_payload,
format='json',
)
# get the created external version
external_version = self.project.versions(
manager=EXTERNAL
).get(verbose_name='2')

self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertTrue(resp.data['build_triggered'])
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [external_version.verbose_name])
core_trigger_build.assert_called_once_with(
force=True, project=self.project,
version=external_version, commit=self.commit
)
self.assertTrue(external_version)

@mock.patch('readthedocs.core.utils.trigger_build')
def test_gitlab_merge_request_reopen_event(self, trigger_build, core_trigger_build):
client = APIClient()

# Update the payload for `reopen` webhook event
merge_request_number = '5'
payload = self.gitlab_merge_request_payload
payload["object_attributes"]["action"] = GITLAB_MERGE_REQUEST_REOPEN
payload["object_attributes"]["iid"] = merge_request_number

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)
# get the created external version
external_version = self.project.versions(
manager=EXTERNAL
).get(verbose_name=merge_request_number)

self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertTrue(resp.data['build_triggered'])
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [external_version.verbose_name])
core_trigger_build.assert_called_once_with(
force=True, project=self.project,
version=external_version, commit=self.commit
)
self.assertTrue(external_version)

@mock.patch('readthedocs.core.utils.trigger_build')
def test_gitlab_merge_request_update_event(self, trigger_build, core_trigger_build):
client = APIClient()

merge_request_number = '6'
prev_identifier = '95790bf891e76fee5e1747ab589903a6a1f80f23'
# create an existing external version for merge request
version = get(
Version,
project=self.project,
type=EXTERNAL,
built=True,
uploaded=True,
active=True,
verbose_name=merge_request_number,
identifier=prev_identifier
)

# Update the payload for merge request `update` webhook event
payload = self.gitlab_merge_request_payload
payload["object_attributes"]["action"] = GITLAB_MERGE_REQUEST_UPDATE
payload["object_attributes"]["iid"] = merge_request_number

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)
# get updated external version
external_version = self.project.versions(
manager=EXTERNAL
).get(verbose_name=merge_request_number)

self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertTrue(resp.data['build_triggered'])
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [external_version.verbose_name])
core_trigger_build.assert_called_once_with(
force=True, project=self.project,
version=external_version, commit=self.commit
)
# `update` webhook event updated the identifier (commit hash)
self.assertNotEqual(prev_identifier, external_version.identifier)

@mock.patch('readthedocs.core.utils.trigger_build')
def test_gitlab_merge_request_close_event(self, trigger_build, core_trigger_build):
client = APIClient()

merge_request_number = '7'
identifier = '95790bf891e76fee5e1747ab589903a6a1f80f23'
# create an existing external version for merge request
version = get(
Version,
project=self.project,
type=EXTERNAL,
built=True,
uploaded=True,
active=True,
verbose_name=merge_request_number,
identifier=identifier
)

# Update the payload for `closed` webhook event
payload = self.gitlab_merge_request_payload
payload["object_attributes"]["action"] = GITLAB_MERGE_REQUEST_CLOSE
payload["object_attributes"]["iid"] = merge_request_number
payload["object_attributes"]["last_commit"]["id"] = identifier

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)
external_version = self.project.versions(
manager=EXTERNAL
).filter(verbose_name=merge_request_number)

# external version should be deleted
self.assertFalse(external_version.exists())
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertTrue(resp.data['version_deleted'])
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [version.verbose_name])
core_trigger_build.assert_not_called()

@mock.patch('readthedocs.core.utils.trigger_build')
def test_gitlab_merge_request_merge_event(self, trigger_build, core_trigger_build):
client = APIClient()

merge_request_number = '8'
identifier = '95790bf891e76fee5e1747ab589903a6a1f80f23'
# create an existing external version for merge request
version = get(
Version,
project=self.project,
type=EXTERNAL,
built=True,
uploaded=True,
active=True,
verbose_name=merge_request_number,
identifier=identifier
)

# Update the payload for `merge` webhook event
payload = self.gitlab_merge_request_payload
payload["object_attributes"]["action"] = GITLAB_MERGE_REQUEST_MERGE
payload["object_attributes"]["iid"] = merge_request_number
payload["object_attributes"]["last_commit"]["id"] = identifier

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)
external_version = self.project.versions(
manager=EXTERNAL
).filter(verbose_name=merge_request_number)

# external version should be deleted
self.assertFalse(external_version.exists())
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertTrue(resp.data['version_deleted'])
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [version.verbose_name])
core_trigger_build.assert_not_called()

def test_gitlab_merge_request_no_action(self, trigger_build):
client = APIClient()

payload = {
"object_kind": GITLAB_MERGE_REQUEST,
"object_attributes": {
"iid": 2,
"last_commit": {
"id": self.commit
},
},
}

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['detail'], 'Unhandled webhook event')

def test_gitlab_merge_request_open_event_invalid_payload(self, trigger_build):
client = APIClient()

payload = {
"object_kind": GITLAB_MERGE_REQUEST,
"object_attributes": {
"action": GITLAB_MERGE_REQUEST_CLOSE
},
}
resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)

self.assertEqual(resp.status_code, 400)

def test_gitlab_merge_request_close_event_invalid_payload(self, trigger_build):
client = APIClient()

payload = {
"object_kind": GITLAB_MERGE_REQUEST,
"object_attributes": {
"action": GITLAB_MERGE_REQUEST_CLOSE
},
}

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
payload,
format='json',
)

self.assertEqual(resp.status_code, 400)

@mock.patch('readthedocs.core.utils.trigger_build')
def test_gitlab_merge_request_event_no_feature_flag(self, trigger_build, core_trigger_build):
# delete feature flag
self.feature_flag.delete()

client = APIClient()

resp = client.post(
reverse(
'api_webhook_gitlab',
kwargs={'project_slug': self.project.slug}
),
self.gitlab_merge_request_payload,
format='json',
)
# get external version
external_version = self.project.versions(
manager=EXTERNAL
).filter(verbose_name='2').first()

self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['detail'], 'Unhandled webhook event')
core_trigger_build.assert_not_called()
self.assertFalse(external_version)

def test_bitbucket_webhook(self, trigger_build):
"""Bitbucket webhook API."""
client = APIClient()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.