Skip to content

Commit 619b359

Browse files
Michael Russellguyzmo
Michael Russell
authored andcommitted
Fix gitlab merge request by supporting the edit option. Fixes #131
1 parent c475487 commit 619b359

File tree

4 files changed

+146
-5
lines changed

4 files changed

+146
-5
lines changed

git_repo/services/ext/gitlab.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,19 @@ def gist_delete(self, snippet):
257257

258258
return snippet.delete()
259259

260-
def request_create(self, user, repo, local_branch, remote_branch, title, description=None, auto_slug=False):
260+
def request_create(self, user, repo, local_branch, remote_branch, title, description=None, auto_slug=False, edit=None):
261261
try:
262262
repository = self.gl.projects.get('/'.join([user, repo]))
263263
if not repository:
264264
raise ResourceNotFoundError('Could not find repository `{}/{}`!'.format(user, repo))
265265
if not title and not description and edit:
266-
title, description = edit(repository, from_branch)
266+
title, description = edit(repository, local_branch)
267267
if not title and not description:
268268
raise ArgumentError('Missing message for request creation')
269269
if not local_branch:
270-
remote_branch = self.repository.active_branch.name or self.repository.active_branch.name
270+
local_branch = self.repository.active_branch.name
271271
if not remote_branch:
272-
local_branch = repository.master_branch or 'master'
272+
remote_branch = repository.default_branch or 'master'
273273
request = self.gl.project_mergerequests.create(
274274
project_id=repository.id,
275275
data= {

tests/helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@ def prepare_project_for_test():
750750
self.service.delete(create_repository)
751751

752752
#self.service.repository = self.repository
753+
754+
def edit_stub(repository, branch):
755+
return 'title', 'description'
756+
753757
with prepare_project_for_test():
754758
with self.recorder.use_cassette(cassette_name):
755759
self.service.connect()
@@ -759,7 +763,8 @@ def prepare_project_for_test():
759763
source_branch,
760764
target_branch,
761765
title,
762-
description
766+
description,
767+
edit=edit_stub
763768
)
764769
return request
765770

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"recorded_at": "2017-03-04T23:36:28",
5+
"request": {
6+
"body": {
7+
"encoding": "utf-8",
8+
"string": ""
9+
},
10+
"headers": {
11+
"Accept": "*/*",
12+
"Accept-Encoding": "identity",
13+
"Connection": "keep-alive",
14+
"PRIVATE-TOKEN": "<PRIVATE_KEY_GITLAB>",
15+
"User-Agent": "python-requests/2.13.0"
16+
},
17+
"method": "GET",
18+
"uri": "https://gitlab.com/api/v3/user"
19+
},
20+
"response": {
21+
"body": {
22+
"encoding": null,
23+
"string": "{\"name\":\"Michael Russell\",\"username\":\"<BITBUCKET_NAMESPACE>\",\"id\":922189,\"state\":\"active\",\"avatar_url\":\"https://secure.gravatar.com/avatar/fbff0e16b2d2c737d36575686375c203?s=80&d=identicon\",\"web_url\":\"https://gitlab.com/<BITBUCKET_NAMESPACE>\",\"created_at\":\"2017-01-02T21:31:34.426Z\",\"is_admin\":false,\"bio\":null,\"location\":null,\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"website_url\":\"\",\"organization\":null,\"last_sign_in_at\":\"2017-02-28T18:43:00.076Z\",\"confirmed_at\":\"2017-01-02T21:31:34.289Z\",\"email\":\"<BITBUCKET_NAMESPACE>[email protected]\",\"theme_id\":2,\"color_scheme_id\":4,\"projects_limit\":100000,\"current_sign_in_at\":\"2017-03-04T16:30:18.407Z\",\"identities\":[{\"provider\":\"google_oauth2\",\"extern_uid\":\"118342393548148958779\"}],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":true,\"external\":false}"
24+
},
25+
"headers": {
26+
"Cache-Control": "max-age=0, private, must-revalidate",
27+
"Content-Length": "773",
28+
"Content-Type": "application/json",
29+
"Date": "Sat, 04 Mar 2017 23:36:28 GMT",
30+
"Etag": "W/\"8e35e69f3641436a588171181b0de9c3\"",
31+
"Server": "nginx",
32+
"Vary": "Origin",
33+
"X-Request-Id": "2c8f4f01-777d-48d9-b3da-a00c5e0a9e26",
34+
"X-Runtime": "0.067689"
35+
},
36+
"status": {
37+
"code": 200,
38+
"message": "OK"
39+
},
40+
"url": "https://gitlab.com/api/v3/user"
41+
}
42+
},
43+
{
44+
"recorded_at": "2017-03-04T23:36:29",
45+
"request": {
46+
"body": {
47+
"encoding": "utf-8",
48+
"string": ""
49+
},
50+
"headers": {
51+
"Accept": "*/*",
52+
"Accept-Encoding": "identity",
53+
"Connection": "keep-alive",
54+
"PRIVATE-TOKEN": "<PRIVATE_KEY_GITLAB>",
55+
"User-Agent": "python-requests/2.13.0"
56+
},
57+
"method": "GET",
58+
"uri": "https://gitlab.com/api/v3/projects/<BITBUCKET_NAMESPACE>%2Ftest_create_requests"
59+
},
60+
"response": {
61+
"body": {
62+
"encoding": null,
63+
"string": "{\"id\":2830817,\"description\":null,\"default_branch\":\"master\",\"tag_list\":[],\"public\":false,\"archived\":false,\"visibility_level\":0,\"ssh_url_to_repo\":\"[email protected]:<BITBUCKET_NAMESPACE>/test_create_requests.git\",\"http_url_to_repo\":\"https://gitlab.com/<BITBUCKET_NAMESPACE>/test_create_requests.git\",\"web_url\":\"https://gitlab.com/<BITBUCKET_NAMESPACE>/test_create_requests\",\"owner\":{\"name\":\"Michael Russell\",\"username\":\"<BITBUCKET_NAMESPACE>\",\"id\":922189,\"state\":\"active\",\"avatar_url\":\"https://secure.gravatar.com/avatar/fbff0e16b2d2c737d36575686375c203?s=80&d=identicon\",\"web_url\":\"https://gitlab.com/<BITBUCKET_NAMESPACE>\"},\"name\":\"test_create_requests\",\"name_with_namespace\":\"Michael Russell / test_create_requests\",\"path\":\"test_create_requests\",\"path_with_namespace\":\"<BITBUCKET_NAMESPACE>/test_create_requests\",\"container_registry_enabled\":true,\"issues_enabled\":true,\"merge_requests_enabled\":true,\"wiki_enabled\":true,\"builds_enabled\":true,\"snippets_enabled\":false,\"created_at\":\"2017-03-04T23:36:16.987Z\",\"last_activity_at\":\"2017-03-04T23:36:16.987Z\",\"shared_runners_enabled\":true,\"lfs_enabled\":true,\"creator_id\":922189,\"namespace\":{\"id\":1107131,\"name\":\"<BITBUCKET_NAMESPACE>\",\"path\":\"<BITBUCKET_NAMESPACE>\",\"kind\":\"user\"},\"avatar_url\":null,\"star_count\":0,\"forks_count\":0,\"open_issues_count\":0,\"runners_token\":\"mRkZBd-_9Pohkns84y_K\",\"public_builds\":true,\"shared_with_groups\":[],\"only_allow_merge_if_build_succeeds\":false,\"request_access_enabled\":false,\"only_allow_merge_if_all_discussions_are_resolved\":false,\"approvals_before_merge\":0,\"permissions\":{\"project_access\":{\"access_level\":40,\"notification_level\":3},\"group_access\":null}}"
64+
},
65+
"headers": {
66+
"Cache-Control": "max-age=0, private, must-revalidate",
67+
"Content-Length": "1527",
68+
"Content-Type": "application/json",
69+
"Date": "Sat, 04 Mar 2017 23:36:28 GMT",
70+
"Etag": "W/\"e03c6b6653e6a6a4c1f252f937f8592b\"",
71+
"Server": "nginx",
72+
"Vary": "Origin",
73+
"X-Request-Id": "9a1c6b67-6ac8-42c2-afa5-fdbbaa66636a",
74+
"X-Runtime": "0.249491"
75+
},
76+
"status": {
77+
"code": 200,
78+
"message": "OK"
79+
},
80+
"url": "https://gitlab.com/api/v3/projects/<BITBUCKET_NAMESPACE>%2Ftest_create_requests"
81+
}
82+
},
83+
{
84+
"recorded_at": "2017-03-04T23:36:31",
85+
"request": {
86+
"body": {
87+
"encoding": "utf-8",
88+
"string": "{\"source_branch\": \"pr-test\", \"target_branch\": \"master\", \"title\": \"title\", \"description\": \"description\"}"
89+
},
90+
"headers": {
91+
"Accept": "*/*",
92+
"Accept-Encoding": "identity",
93+
"Connection": "keep-alive",
94+
"Content-Length": "103",
95+
"Content-type": "application/json",
96+
"PRIVATE-TOKEN": "<PRIVATE_KEY_GITLAB>",
97+
"User-Agent": "python-requests/2.13.0"
98+
},
99+
"method": "POST",
100+
"uri": "https://gitlab.com/api/v3/projects/2830817/merge_requests"
101+
},
102+
"response": {
103+
"body": {
104+
"encoding": null,
105+
"string": "{\"id\":2605518,\"iid\":1,\"project_id\":2830817,\"title\":\"title\",\"description\":\"description\",\"state\":\"opened\",\"created_at\":\"2017-03-04T23:36:28.616Z\",\"updated_at\":\"2017-03-04T23:36:28.616Z\",\"target_branch\":\"master\",\"source_branch\":\"pr-test\",\"upvotes\":0,\"downvotes\":0,\"author\":{\"name\":\"Michael Russell\",\"username\":\"<BITBUCKET_NAMESPACE>\",\"id\":922189,\"state\":\"active\",\"avatar_url\":\"https://secure.gravatar.com/avatar/fbff0e16b2d2c737d36575686375c203?s=80&d=identicon\",\"web_url\":\"https://gitlab.com/<BITBUCKET_NAMESPACE>\"},\"assignee\":null,\"source_project_id\":2830817,\"target_project_id\":2830817,\"labels\":[],\"work_in_progress\":false,\"milestone\":null,\"merge_when_build_succeeds\":false,\"merge_status\":\"unchecked\",\"sha\":\"f16c0d4a371caa606e951b157dc5709a87996cb4\",\"merge_commit_sha\":null,\"subscribed\":true,\"user_notes_count\":0,\"approvals_before_merge\":null,\"should_remove_source_branch\":null,\"force_remove_source_branch\":null,\"squash\":false,\"web_url\":\"https://gitlab.com/<BITBUCKET_NAMESPACE>/test_create_requests/merge_requests/1\"}"
106+
},
107+
"headers": {
108+
"Cache-Control": "max-age=0, private, must-revalidate",
109+
"Content-Length": "979",
110+
"Content-Type": "application/json",
111+
"Date": "Sat, 04 Mar 2017 23:36:30 GMT",
112+
"Etag": "W/\"da575f32647af69c59092ddc47bd44cf\"",
113+
"Server": "nginx",
114+
"Vary": "Origin",
115+
"X-Request-Id": "ddd03590-53f9-458f-9bc7-23b301cc9be3",
116+
"X-Runtime": "1.789859"
117+
},
118+
"status": {
119+
"code": 201,
120+
"message": "Created"
121+
},
122+
"url": "https://gitlab.com/api/v3/projects/2830817/merge_requests"
123+
}
124+
}
125+
],
126+
"recorded_with": "betamax/0.5.1"
127+
}

tests/integration/test_gitlab.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ def test_20_request_create(self):
262262
description='PR description')
263263
assert r == {'local': 'pr-test', 'ref': 1, 'remote': 'master'}
264264

265+
def test_20_request_create_with_edit_and_no_title(self):
266+
r = self.action_request_create(namespace='crazybus',
267+
repository='test_create_requests',
268+
branch='pr-test',
269+
service='gitlab',
270+
title=None,
271+
description=None)
272+
assert r == {'local': 'pr-test', 'ref': 1, 'remote': 'master'}
273+
265274
# TODO lookup why this is not raising the expected error !
266275
# def test_20_request_create__bad_branch(self):
267276
# with pytest.raises(ResourceError):

0 commit comments

Comments
 (0)