-1

How to check the merge request status in GitLab using a python API call, I want to check the status of approval before merging, based on the status of approval have to do some checks, once checks are approved then merge will takes place

import gitlab
import os
import json
Gitlab_repo = "https://gitlab.devtools.xxxx.com/"
gl = gitlab.Gitlab(Gitlab_repo, private_token='xxxx')
project = gl.projects.get(projectid)
merge_request = project.mergerequests.get(merge_request number)
print(merge_request)

Output:

{
   "source_branch":"",
   "id":,
   "references":{
      "full":"",
      "relative":"",
      "short":""
   },
   "project_id":,
   "time_stats":{
      "time_estimate":0,
      "human_total_time_spent":"None",
      "total_time_spent":0,
      "human_time_estimate":"None"
   },
   "description":"",
   "merge_when_pipeline_succeeds":,
   "work_in_progress":,
   "changes_count":"",
   "reference":"",
   "first_deployed_to_production_at":"None",
   "source_project_id":,
   "should_remove_source_branch":"None",
   "milestone":"None",
   "updated_at":"",
   "user_notes_count":0,
   "latest_build_finished_at":"None",
   "head_pipeline":"None",
   "subscribed":true,
   "web_url":"",
   "state":"opened",
   "created_at":"",
   "approvals_before_merge":"None",
   "user":{
      "can_merge":true
   },
   "iid":,
   "assignee":{
      "avatar_url":"None",
      "web_url":"",
      "id":,
      "username":"",
      "state":"",
      "name":""
   },
   "author":{
      "avatar_url":"None",
      "web_url":"",
      "id":,
      "username":"",
      "state":"active",
      "name":""
   },
   "target_project_id":,
   "merge_error":"None",
   "task_completion_status":{
      "count":0,
      "completed_count":0
   },
   "target_branch":"master",
   "squash":false,
   "assignees":[
      {
         "avatar_url":"None",
         "web_url":"",
         "id":,
         "username":"",
         "state":"active",
         "name":""
      }
   ],
   "merged_at":"None",
   "has_conflicts":false,
   "force_remove_source_branch":true,
   "merge_status":"can_be_merged",
   "labels":[
      
   ],
   "pipeline":"None",
   "title":"",
   "merged_by":"None",
   "closed_by":"None",
   "sha":"",
   "closed_at":"None",
   "diff_refs":{
      "start_sha":"",
      "head_sha":"",
      "base_sha":""
   },
   "squash_commit_sha":"None",
   "latest_build_started_at":"None",
   "downvotes":0,
   "upvotes":0,
   "discussion_locked":"None",
   "merge_commit_sha":"None",
   "blocking_discussions_resolved":true
}
Cena
  • 3,316
  • 2
  • 17
  • 34
shahbaz
  • 1
  • 1
  • 1

2 Answers2

2

After creation you can get parameters of merge request:

# create MR
mr = project.mergerequests.create({'source_branch': 'cool_feature',
                                   'target_branch': 'master',
                                   'title': 'merge cool feature',
                                   'labels': ['label1', 'label2']})

get URL

print(getattr(mr,'web_url'))

or

print(getattr(mr,'merge_error'))

or set it as variable to next code('has_conflicts' - True or False, in this example):

merge_has_no_conflicts = getattr(mr,'has_conflicts')
if merge_has_no_conflicts:
   do_smthg
vvvvv
  • 25,404
  • 19
  • 49
  • 81
krasnosvar
  • 101
  • 4
0

I'm not familiar with the python Gitlab API library, but Merge Request Approvals can be retrieved from a separate API. They're not in the Merge Requests API but rather the Merge Request Approvals API, which is a paid feature. You must be at least a Premium customer to use the API.

With the Merge Request Approvals API you can approve a MR, unapprove, or get the approval status of a MR. To get the status, use the following endpoint:

GET /projects/:id/merge_requests/:merge_request_iid/approval_state

The results will look like:

{
  "approval_rules_overwritten": true,
  "rules": [
    {
      "id": 1,
      "name": "Ruby",
      "rule_type": "regular",
      "eligible_approvers": [
        {
          "id": 4,
          "name": "John Doe",
          "username": "jdoe",
          "state": "active",
          "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
          "web_url": "http://localhost/jdoe"
        }
      ],
      "approvals_required": 2,
      "users": [
        {
          "id": 4,
          "name": "John Doe",
          "username": "jdoe",
          "state": "active",
          "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
          "web_url": "http://localhost/jdoe"
        }
      ],
      "groups": [],
      "contains_hidden_groups": false,
      "approved_by": [
        {
          "id": 4,
          "name": "John Doe",
          "username": "jdoe",
          "state": "active",
          "avatar_url": "https://www.gravatar.com/avatar/0?s=80&d=identicon",
          "web_url": "http://localhost/jdoe"
        }
      ],
      "source_rule": null,
      "approved": true,
      "overridden": false
    }
  ]
}

This will include the applicable rules for MR approval, who has already approved the MR if available, and a simple boolean indicating if the MR is approved or not.

You can read more about the Merge Request Approvals API and its other operations in the docs.

Adam Marshall
  • 6,369
  • 1
  • 29
  • 45