import gitlab
# GitLab server URL and personal access token
gitlab_url = 'https://gitlab.example.com'
private_token = 'your_personal_access_token'
# ID of the project and merge request
project_id = 1234
merge_request_iid = 5678
# Create GitLab client
gl = gitlab.Gitlab(gitlab_url, private_token)
# Get the project
project = gl.projects.get(project_id)
# Get the merge request
mr = project.mergerequests.get(merge_request_iid)
# Get the approval status of the merge request
all_approved = mr.approval_state.get()
for rule in all_approved.rules:
if rule.get('approved')==False:
print("Not all approvals received.")
break
else:
print("All approvals received.")
mr.approval_state.get()返回的格式:
<class 'gitlab.v4.objects.merge_request_approvals.ProjectMergeRequestApprovalState'> => {
'approval_rules_overwritten': True,
'rules': [
{
'id': 12345,
'name': 'All Members',
'rule_type': 'any_approver',
'eligible_approvers': [],
'approvals_required': 1,
'users': [],
'groups': [],
'contains_hidden_groups': False,
'section': None,
'source_rule': {
'approvals_required': 1
},
'overridden': False,
'code_owner': False,
'approved_by': [
{
'id': 121,
'username': 'xxx',
......
},
{
'id': 122,
'username': 'xxx',
......
}
],
'approved': True
},
所有rule的approved为True则表明全部approved。