【问题标题】:Gitlab-PythonApi Merge requestGitlab-PythonApi 合并请求
【发布时间】:2021-06-04 20:03:46
【问题描述】:

如何使用python API调用检查GitLab中的合并请求状态,我想在合并前检查批准状态,根据批准状态必须做一些检查,一旦检查被批准,就会发生合并

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)

输出:

{
   "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
}

【问题讨论】:

    标签: python api gitlab merge-request


    【解决方案1】:

    我不熟悉 python Gitlab API 库,但可以从单独的 API 检索合并请求批准。它们不在 Merge Requests API 中,而是在 Merge Request Approvals API 中,这是一项付费功能。您必须至少是高级客户才能使用 API。

    使用 Merge Request Approvals API,您可以批准、取消批准或获取 MR 的批准状态。要获取状态,请使用以下端点:

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

    结果将如下所示:

    {
      "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
        }
      ]
    }
    

    这将包括 MR 批准的适用规则,如果可用,谁已经批准了 MR,以及指示 MR 是否被批准的简单布尔值。

    您可以在the docs 中阅读有关 Merge Request Approvals API 及其其他操作的更多信息。

    【讨论】:

      【解决方案2】:

      创建后可以获取合并请求的参数

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

      获取网址

      print(getattr(mr,'web_url'))
      

      或

      print(getattr(mr,'merge_error'))
      

      或将其设置为下一个代码的变量(在此示例中为“has_conflicts” - True 或 False):

      merge_has_no_conflicts = getattr(mr,'has_conflicts')
      if merge_has_no_conflicts:
         do_smthg
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-05
        • 2019-03-13
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-18
        • 1970-01-01
        相关资源
        最近更新 更多