Skip to content

Commit 334ae82

Browse files
committed
Add gerrit patchset fetch support
1 parent 00d42eb commit 334ae82

8 files changed

+735
-8
lines changed

git_repo/services/ext/gerrit.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def connect(self):
2525
self.create_connection()
2626
self.server_client = client.get_client('server', connection=self.connection)
2727
self.project_client = client.get_client('project', connection=self.connection)
28+
self.change_client = client.get_client('change', connection=self.connection)
2829

2930
try:
3031
self.server_client.get_version()
@@ -128,3 +129,26 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch=None, ti
128129
yield '{} -> {}: {}'
129130
for element in info:
130131
yield [element.local_ref, element.remote_ref_string, element.summary]
132+
133+
def request_fetch(self, user, repo, request, pull=False, force=False):
134+
if 'refs/changes/' not in request:
135+
if '/' in request:
136+
change_id, patch_set = request.split('/')
137+
else:
138+
change_id = request
139+
change = self.change_client.get_all(['change: {}'.format(change_id)], ['CURRENT_REVISION'])[0]
140+
current_patchset = change['revisions'][change['current_revision']]
141+
patch_set = current_patchset['_number']
142+
143+
if change_id[0] == 'I':
144+
change_id = str(self.change_client.get_by_id(request)['_number'])
145+
146+
request = 'refs/changes/{}/{}/{}'.format(change_id[-2:], change_id, patch_set)
147+
else:
148+
change_id = request.split('/')[3]
149+
150+
remote = self.repository.remote(self.name)
151+
local_branch_name = 'requests/{}/{}'.format(self.name, change_id)
152+
self.fetch(remote, request, local_branch_name, force=force)
153+
154+
return local_branch_name

git_repo/services/service.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,18 @@ def push(self, remote, branch=None):
337337
print()
338338
return result, pb.other_lines
339339

340-
def fetch(self, remote, remote_branch, local_branch, force=False):
340+
def fetch(self, remote, branch, local_branch = None, force=False):
341341
'''Pull a repository
342342
:param remote: git-remote instance
343343
:param branch: name of the branch to pull
344344
'''
345345
pb = ProgressBar()
346346
pb.setup(self.name)
347-
remote.fetch(':'.join([remote_branch, local_branch]),
348-
update_head_ok=True, force=force, progress=pb)
347+
348+
if local_branch:
349+
branch = ':'.join([branch, local_branch])
350+
351+
remote.fetch(branch, update_head_ok=True, force=force, progress=pb)
349352
print()
350353

351354
def clone(self, user, repo, branch=None, rw=True):

tests/helpers.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,14 @@ def action_request_list(self, namespace, repository, rq_list_data=[]):
641641
for i, rq in enumerate(rq_list_data):
642642
assert requests[i] == rq
643643

644-
def action_request_fetch(self, namespace, repository, request, pull=False, fail=False, remote_branch='pull', local_branch='requests'):
644+
def action_request_fetch(self, namespace, repository, request, pull=False, fail=False, remote_branch='pull', local_branch='requests', remote_ref=None, local_ref=None):
645+
if remote_ref is None:
646+
remote_ref = '{}/{}/head'.format(remote_branch, request)
647+
if local_ref is None:
648+
local_ref = '{}/{}'.format(local_branch, request)
649+
additional_flags = ''
650+
if not remote_ref.endswith('/head'):
651+
additional_flags += '--update-head-ok '
645652
local_slug = self.service.format_path(namespace=namespace, repository=repository, rw=False)
646653
with self.recorder.use_cassette(self._make_cassette_name()):
647654
with self.mockup_git(namespace, repository):
@@ -663,11 +670,11 @@ def action_request_fetch(self, namespace, repository, request, pull=False, fail=
663670
' * [new branch] master -> {1}/{0}'.format(request, local_branch)]).encode('utf-8'),
664671
0),
665672
('git version', b'git version 2.8.0', b'', 0),
666-
('git fetch --progress -v {0} {2}/{1}/head:{3}/{1}'.format(
673+
('git fetch --progress {0}-v {1} {2}:{3}'.format(
674+
additional_flags,
667675
self.service.name,
668-
request,
669-
remote_branch,
670-
local_branch), b'', '\n'.join([
676+
remote_ref,
677+
local_ref), b'', '\n'.join([
671678
'POST git-upload-pack (140 bytes)',
672679
'remote: Counting objects: 8318, done.',
673680
'remote: Compressing objects: 100% (3/3), done.',
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"recorded_at": "2017-12-20T12:16:57",
5+
"request": {
6+
"body": {
7+
"encoding": "utf-8",
8+
"string": ""
9+
},
10+
"headers": {
11+
"Accept": "application/json",
12+
"Accept-Encoding": "identity",
13+
"Authorization": "Basic UGhhbnRvbS00MjovRk45NTI2TE84MkxYTXJucWJUNWE3Y25wSkY4b2hVTVFRQ1l6TkgzTVE=",
14+
"Connection": "keep-alive",
15+
"Content-Type": "application/json",
16+
"User-Agent": "python-requests/2.10.0"
17+
},
18+
"method": "GET",
19+
"uri": "https://review.gerrithub.io/a/config/server/version"
20+
},
21+
"response": {
22+
"body": {
23+
"encoding": "utf-8",
24+
"string": ")]}'\n\"2.13.6-3044-g7e9c06d\"\n"
25+
},
26+
"headers": {
27+
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
28+
"Connection": "close",
29+
"Content-Disposition": "attachment",
30+
"Content-Length": "28",
31+
"Content-Type": "application/json;charset=utf-8",
32+
"Date": "Wed, 20 Dec 2017 12:16:57 GMT",
33+
"Expires": "Mon, 01 Jan 1990 00:00:00 GMT",
34+
"Pragma": "no-cache",
35+
"Server": "Apache/2.4.6 (CentOS)",
36+
"Set-Cookie": "JSESSIONID=jku5o0xf87zfuoa39kvtkwr;Path=/;Secure",
37+
"X-Content-Type-Options": "nosniff"
38+
},
39+
"status": {
40+
"code": 200,
41+
"message": "OK"
42+
},
43+
"url": "https://review.gerrithub.io/a/config/server/version"
44+
}
45+
},
46+
{
47+
"recorded_at": "2017-12-20T12:16:58",
48+
"request": {
49+
"body": {
50+
"encoding": "utf-8",
51+
"string": ""
52+
},
53+
"headers": {
54+
"Accept": "application/json",
55+
"Accept-Encoding": "identity",
56+
"Authorization": "Basic UGhhbnRvbS00MjovRk45NTI2TE84MkxYTXJucWJUNWE3Y25wSkY4b2hVTVFRQ1l6TkgzTVE=",
57+
"Connection": "keep-alive",
58+
"Content-Type": "application/json",
59+
"Cookie": "JSESSIONID=jku5o0xf87zfuoa39kvtkwr",
60+
"User-Agent": "python-requests/2.10.0"
61+
},
62+
"method": "GET",
63+
"uri": "https://review.gerrithub.io/a/projects/TestUser%2FDemoRepository"
64+
},
65+
"response": {
66+
"body": {
67+
"encoding": "utf-8",
68+
"string": ")]}'\n{\"id\":\"TestUser%2FDemoRepository\",\"name\":\"TestUser/DemoRepository\",\"parent\":\"All-Projects\",\"description\":\"Just a demo repository for some testing\",\"state\":\"ACTIVE\",\"web_links\":[{\"name\":\"GitHub\",\"url\":\"https://github.com/TestUser/DemoRepository\"}]}\n"
69+
},
70+
"headers": {
71+
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
72+
"Connection": "close",
73+
"Content-Disposition": "attachment",
74+
"Content-Length": "259",
75+
"Content-Type": "application/json;charset=utf-8",
76+
"Date": "Wed, 20 Dec 2017 12:16:58 GMT",
77+
"Expires": "Mon, 01 Jan 1990 00:00:00 GMT",
78+
"Pragma": "no-cache",
79+
"Server": "Apache/2.4.6 (CentOS)",
80+
"X-Content-Type-Options": "nosniff"
81+
},
82+
"status": {
83+
"code": 200,
84+
"message": "OK"
85+
},
86+
"url": "https://review.gerrithub.io/a/projects/TestUser%2FDemoRepository"
87+
}
88+
},
89+
{
90+
"recorded_at": "2017-12-20T12:16:58",
91+
"request": {
92+
"body": {
93+
"encoding": "utf-8",
94+
"string": ""
95+
},
96+
"headers": {
97+
"Accept": "application/json",
98+
"Accept-Encoding": "identity",
99+
"Authorization": "Basic UGhhbnRvbS00MjovRk45NTI2TE84MkxYTXJucWJUNWE3Y25wSkY4b2hVTVFRQ1l6TkgzTVE=",
100+
"Connection": "keep-alive",
101+
"Content-Type": "application/json",
102+
"Cookie": "JSESSIONID=jku5o0xf87zfuoa39kvtkwr",
103+
"User-Agent": "python-requests/2.10.0"
104+
},
105+
"method": "GET",
106+
"uri": "https://review.gerrithub.io/a/projects/TestUser%2FDemoRepository/branches"
107+
},
108+
"response": {
109+
"body": {
110+
"encoding": "utf-8",
111+
"string": ")]}'\n[{\"ref\":\"HEAD\",\"revision\":\"master\"},{\"web_links\":[{\"name\":\"GitHub\",\"url\":\"https://github.com/TestUser/DemoRepository/tree/refs/meta/config\"}],\"ref\":\"refs/meta/config\",\"revision\":\"3d3d58c5516834d9fcd92d4d10efe5d3b878bdb4\"},{\"web_links\":[{\"name\":\"GitHub\",\"url\":\"https://github.com/TestUser/DemoRepository/tree/refs/heads/master\"}],\"ref\":\"refs/heads/master\",\"revision\":\"d8ffe1ba74eee8dbbf1c305d0828594b65064d24\"},{\"web_links\":[{\"name\":\"GitHub\",\"url\":\"https://github.com/TestUser/DemoRepository/tree/refs/heads/second_branch\"}],\"ref\":\"refs/heads/second_branch\",\"revision\":\"734a6e38bf82c9f1bd5e0d66d3fe06b9010a9b37\",\"can_delete\":true}]\n"
112+
},
113+
"headers": {
114+
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
115+
"Connection": "close",
116+
"Content-Disposition": "attachment",
117+
"Content-Length": "642",
118+
"Content-Type": "application/json;charset=utf-8",
119+
"Date": "Wed, 20 Dec 2017 12:16:58 GMT",
120+
"Expires": "Mon, 01 Jan 1990 00:00:00 GMT",
121+
"Pragma": "no-cache",
122+
"Server": "Apache/2.4.6 (CentOS)",
123+
"X-Content-Type-Options": "nosniff"
124+
},
125+
"status": {
126+
"code": 200,
127+
"message": "OK"
128+
},
129+
"url": "https://review.gerrithub.io/a/projects/TestUser%2FDemoRepository/branches"
130+
}
131+
},
132+
{
133+
"recorded_at": "2017-12-20T12:16:59",
134+
"request": {
135+
"body": {
136+
"encoding": "utf-8",
137+
"string": ""
138+
},
139+
"headers": {
140+
"Accept": "application/json",
141+
"Accept-Encoding": "identity",
142+
"Authorization": "Basic UGhhbnRvbS00MjovRk45NTI2TE84MkxYTXJucWJUNWE3Y25wSkY4b2hVTVFRQ1l6TkgzTVE=",
143+
"Connection": "keep-alive",
144+
"Content-Type": "application/json",
145+
"Cookie": "JSESSIONID=jku5o0xf87zfuoa39kvtkwr",
146+
"User-Agent": "python-requests/2.10.0"
147+
},
148+
"method": "GET",
149+
"uri": "https://review.gerrithub.io/a/changes/?q=change:%20392089&o=CURRENT_REVISION"
150+
},
151+
"response": {
152+
"body": {
153+
"encoding": "utf-8",
154+
"string": ")]}'\n[{\"id\":\"TestUser%2FDemoRepository~master~I873f1207d660256dd00851852754b8bc3cd7a321\",\"project\":\"TestUser/DemoRepository\",\"branch\":\"master\",\"hashtags\":[],\"change_id\":\"I873f1207d660256dd00851852754b8bc3cd7a321\",\"subject\":\"Test2\",\"status\":\"NEW\",\"created\":\"2017-12-17 08:22:09.808000000\",\"updated\":\"2017-12-17 08:23:37.652000000\",\"submit_type\":\"MERGE_IF_NECESSARY\",\"mergeable\":true,\"insertions\":2,\"deletions\":0,\"unresolved_comment_count\":0,\"_number\":392089,\"owner\":{\"_account_id\":1013076},\"current_revision\":\"010ea1b90c48f414f868b854e7cd946802bbd58d\",\"revisions\":{\"010ea1b90c48f414f868b854e7cd946802bbd58d\":{\"kind\":\"REWORK\",\"_number\":2,\"created\":\"2017-12-17 08:23:37.652000000\",\"uploader\":{\"_account_id\":1013076},\"ref\":\"refs/changes/89/392089/2\",\"fetch\":{\"ssh\":{\"url\":\"ssh://[email protected]:29418/TestUser/DemoRepository\",\"ref\":\"refs/changes/89/392089/2\"},\"anonymous http\":{\"url\":\"https://review.gerrithub.io/TestUser/DemoRepository\",\"ref\":\"refs/changes/89/392089/2\"},\"http\":{\"url\":\"https://[email protected]/a/TestUser/DemoRepository\",\"ref\":\"refs/changes/89/392089/2\"}}}}}]\n"
155+
},
156+
"headers": {
157+
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
158+
"Connection": "close",
159+
"Content-Disposition": "attachment",
160+
"Content-Length": "1113",
161+
"Content-Type": "application/json;charset=utf-8",
162+
"Date": "Wed, 20 Dec 2017 12:16:59 GMT",
163+
"Expires": "Mon, 01 Jan 1990 00:00:00 GMT",
164+
"Pragma": "no-cache",
165+
"Server": "Apache/2.4.6 (CentOS)",
166+
"X-Content-Type-Options": "nosniff"
167+
},
168+
"status": {
169+
"code": 200,
170+
"message": "OK"
171+
},
172+
"url": "https://review.gerrithub.io/a/changes/?q=change:%20392089&o=CURRENT_REVISION"
173+
}
174+
}
175+
],
176+
"recorded_with": "betamax/0.5.1"
177+
}

0 commit comments

Comments
 (0)