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。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐