I’m able to write a python script that creates a new Merge Request through API on GitLab with labels using Python GitLab library, python-gitlab 3.15.0
and refer this documentation link:- Merge requests - python-gitlab v3.15.0
I can create the MR as well through my python script as shown in this screenshot. However, I’m not able to understand on how to add the reviewers automatically at the time of MR creation itself hence it is coming as empty (as highlighted in the screenshot). As shown in the screenshot, the merge request approvals have already been configured at the project repo level and my requirement is to add these reviewers automatically (as defined in the approval rules) whenever the MR is created through the API. For example, for Merge Request to Master/Main, my code should retrieve the approver details from this configuration and use them in the MR creation.
Here is my Python code snippet:-
def check_open_mr(project, source_branch, target_branch):
global existing_mr_id
mrs = project.mergerequests.list(state='opened', source_branch=source_branch, target_branch=target_branch)
if mrs is None:
return False
else:
print(mrs)
existing_mr_id=mrs
def open_mr(gl_project_id, gl_token, source_branch, target_branch, mr_data):
gl = gitlab.Gitlab('https://gitlab.com', private_token=gl_token)
project = gl.projects.get(gl_project_id)
check_mr_labels(project)
isMrExists = check_open_mr(project, source_branch, target_branch)
if isMrExists == True:
mr = project.mergerequests.create({'source_branch': source_branch,
'target_branch': target_branch,
'title': mr_data['title']})
mr.description = mr_data['description']
mr.labels = ['test-label1', 'test-label2']
mr.notes.create({
"body": 'test body'
})
mr.save()
print("Merge request opened")
else:
print('Merge Request already exists with id as ', existing_mr_id)