Skip to content

Commit aea6c12

Browse files
committed
🚧 Added early support for gitlab merge requests
* cf issue #10 Signed-off-by: Guyzmo <guyzmo+github@m0g.net>
1 parent 6ee2525 commit aea6c12

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

git_repo/services/ext/gitlab.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,64 @@ def gist_delete(self, gist_id):
241241

242242
return snippet.delete()
243243

244+
def request_create(self, user, repo, local_branch, remote_branch, title, description=None):
245+
repository = self.gl.projects.list(search=repo)[0]
246+
if not repository:
247+
raise ResourceNotFoundError('Could not find repository `{}/{}`!'.format(user, repo))
248+
if not local_branch:
249+
remote_branch = self.repository.active_branch.name or self.repository.active_branch.name
250+
if not remote_branch:
251+
local_branch = repository.master_branch or 'master'
252+
try:
253+
request = self.gl.project_mergerequests.create(
254+
project_id=repository.id,
255+
data= {
256+
'source_branch':local_branch,
257+
'target_branch':remote_branch,
258+
'title':title,
259+
'description':description
260+
}
261+
)
262+
except Exception as err:
263+
raise ResourceError("Unhandled error: {}".format(err)) from err
264+
265+
return {'local': local_branch,
266+
'remote': remote_branch,
267+
'ref': request.iid}
268+
269+
def request_list(self, user, repo):
270+
project = self.gl.projects.list(search=repo)[0]
271+
for mr in self.gl.project_mergerequests.list(project_id=project.id):
272+
yield ( str(mr.iid),
273+
mr.title,
274+
'https://{}/{}/{}/merge_requests/{}'.format(
275+
self.fqdn,
276+
project.namespace.name,
277+
project.name,
278+
mr.iid
279+
)
280+
)
281+
282+
def request_fetch(self, user, repo, request, pull=False):
283+
if pull:
284+
raise NotImplementedError('Pull operation on requests for merge are not yet supported')
285+
try:
286+
for remote in self.repository.remotes:
287+
if remote.name == self.name:
288+
local_branch_name = 'request/{}'.format(request)
289+
self.fetch(
290+
remote,
291+
'merge-requests/{}/head'.format(request),
292+
local_branch_name
293+
)
294+
return local_branch_name
295+
else:
296+
raise ResourceNotFoundError('Could not find remote {}'.format(self.name))
297+
except GitCommandError as err:
298+
if 'Error when fetching: fatal: Couldn\'t find remote ref' in err.command[0]:
299+
raise ResourceNotFoundError('Could not find opened request #{}'.format(request)) from err
300+
raise err
301+
244302
@property
245303
def user(self):
246304
return self.gl.user.username

0 commit comments

Comments
 (0)