更新时间:2025-09-28 GMT+08:00
分享

更新合并请求

功能介绍

更新合并请求

调试

您可以在API Explorer中调试该接口,支持自动认证鉴权。API Explorer可以自动生成SDK代码示例,并提供SDK代码示例调试功能。

URI

PUT /v4/repositories/{repository_id}/merge-requests/{merge_request_iid}

表1 路径参数

参数

是否必选

参数类型

描述

repository_id

Integer

参数解释:

仓库的ID,通过查询用户所有仓库接口查询项目列表获取。

约束限制:

不涉及。

取值范围:

1-2147483647

merge_request_iid

Integer

参数解释:

合并请求 iid。

取值范围:

1-2147483647

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数解释:

用户Token。 通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。

约束限制:

必传。

取值范围:

字符串长度不少于1,不超过10万。

默认取值:

不涉及。

表3 请求Body参数

参数

是否必选

参数类型

描述

title

String

合并请求标题

state_event

String

合并请求状态

枚举值:

  • reopen

  • close

assignee_ids

String

合并人id列表

reviewer_ids

String

评审人id列表

description

String

合并请求描述

milestone_id

Integer

里程碑id

labels

Object

标签

force_remove_source_branch

Boolean

合入后删除源分支

squash

Boolean

squash合入

squash_commit_message

String

squash提交信息

work_item_ids

Array of strings

e2e单号

响应参数

状态码:200

表4 响应Body参数

参数

参数类型

描述

id

Integer

合并请求id

iid

Integer

合并请求iid

repository_id

Integer

目标仓库id

title

String

合并请求标题

description

String

合并请求描述

state

String

合并请求状态

created_at

String

创建时间

updated_at

String

更新时间

source_branch

String

源分支

target_branch

String

目标分支

is_source_branch_protected

Boolean

源分支是否为保护分支

devcloud_source_branch

String

源分支

author

UserBasicExternalDto object

作者

source_repository_id

Integer

源仓库id

target_repository_id

Integer

目标仓库id

source_project_id

String

源项目id

target_project_id

String

目标项目id

labels

Array of objects

标签

work_in_progress

Boolean

WIP状态

milestone

MilestoneBasicDto object

里程碑

merge_when_build_succeeds

Boolean

流水线成功后自动合入

merge_status

String

可合并状态

sha

String

当前合并请求最新commit

merge_commit_sha

String

合入commit节点

subscribed

Boolean

订阅

merged_by

UserBasicExternalDto object

合并人

merged_at

String

合并时间

closed_by

UserBasicExternalDto object

关闭人

closed_at

String

关闭时间

user_notes_count

Integer

检视意见数量

force_remove_source_branch

Boolean

合入后删除源分支

web_url

String

主页url

merge_request_diff

MergeRequestDiffExternalDto object

合并请求diff

merge_request_reviewers_count

Integer

评审人数量

merge_request_review_count

Integer

打分

merge_request_reviewer_list

Array of MergeRequestReviewerExternalDto objects

评审人列表

merge_request_assignee_list

Array of UserBasicExternalDto objects

合并人列表

notes

Integer

检视意见

codecheck_state

Integer

代码检查状态

codecheck_defect_count

Integer

代码检查问题数

merge_request_related_work_items

Array of MergeRequestRelatedWorkItemDto objects

合并请求关联单数量

diverged_commits_count

Integer

源分支落后commit数

moderation_result

Boolean

送审结果

moderation_time

Long

送审时间时间戳

取值范围:

1-9223372036854775807

moderation_status

Integer

送审状态码

取值范围:

1-2147483647

is_use_temp_branch

Boolean

是否使用临时分支

approval_merge_request_approvers

Array of ApprovalUserDto objects

审核人

review_mode

String

合并请求模式

枚举值:

  • vote

  • approval

squash

Boolean

squash合入

squash_commit_message

String

squash提交信息

rebase_in_progress

Boolean

是否正在rebase

source_repository

ProjectSimpleDto object

源仓库

target_repository

ProjectSimpleDto object

目标仓库

is_source_branch_exist

Boolean

源分支是否存在

merge_request_type

String

合并请求类型

表5 MilestoneBasicDto

参数

参数类型

描述

id

Integer

里程碑id

取值范围:

1-2147483647

iid

Integer

里程碑iid

取值范围:

1-2147483647

repository_id

Integer

仓库id

取值范围:

1-2147483647

title

String

里程碑标题

description

String

里程碑描述

state

String

状态

created_at

String

创建时间

updated_at

String

更新时间

due_date

String

到期时间

start_date

String

开始时间

repository_path

String

仓库路径

web_url

String

主页url

表6 UserBasicExternalDto

参数

参数类型

描述

id

Integer

用户id

取值范围:

1-2147483647

name

String

名称

username

String

用户名称

state

String

状态

avatar_url

String

头像url

web_url

String

主页url

nick_name

String

用户昵称

tenant_name

String

租户名称

表7 MergeRequestDiffExternalDto

参数

参数类型

描述

id

Integer

合并请求diff id

取值范围:

1-2147483647

state

String

状态

merge_request_id

Integer

合并请求id

取值范围:

1-2147483647

created_at

String

创建时间

updated_at

String

更新时间

base_commit_sha

String

base commit节点

real_size

String

diff文件数量

head_commit_sha

String

head commit节点

start_commit_sha

String

start commit节点

commits_count

Integer

commit数量

取值范围:

1-2147483647

external_diff

String

外部diff文件

external_diff_store

Integer

外部差异存储

取值范围:

1-2147483647

stored_externally

Boolean

是否外部存储

added_lines

Integer

新增行数

取值范围:

0-2147483647

removed_lines

Integer

删除行数

取值范围:

0-2147483647

表8 MergeRequestReviewerExternalDto

参数

参数类型

描述

id

Integer

评审人id

取值范围:

1-2147483647

username

String

评审人名称

name

String

评审人名称

nick_name

String

评审人昵称

state

Boolean

评审人状态

score

Integer

打分

取值范围:

-2-2

tenant_name

String

租户名称

表9 MergeRequestRelatedWorkItemDto

参数

参数类型

描述

id

Integer

关联id

取值范围:

1-2147483647

user_id

String

用户id

repo_id

Integer

仓库id

取值范围:

1-2147483647

merge_request_id

Integer

合并请求id

取值范围:

1-2147483647

target_branch

String

目标分支

source_branch

String

源分支

merge_request_title

String

合并请求标题

merge_request_url

String

合并请求url

merge_request_state

String

合并请求状态

related_id

String

关联单id

related_title

String

关联单标题

related_url

String

关联单url

result

Integer

关联结果

message

String

关联结果信息

create_at

String

创建时间

update_at

String

更新时间

subject

String

话题

tracker

Object

跟单人

status

Object

状态

表10 ApprovalUserDto

参数

参数类型

描述

id

Integer

用户id

取值范围:

1-2147483647

username

String

用户名称

name

String

用户名称

nick_name

String

用户昵称

name_cn

String

中文名称

email

String

电子邮箱

state

String

状态

updated_at

String

更新时间

avatar_url

String

头像url

tenant_name

String

租户名称

approver_comment

String

审核意见

表11 ProjectSimpleDto

参数

参数类型

描述

id

Integer

仓库id

description

String

仓库描述信息

name

String

仓库名称

name_with_namespace

String

组织名/组织名.../仓库名

path

String

仓库路径

path_with_namespace

String

仓库完整路径

created_at

String

创建时间

updated_at

String

更新时间

archived

Boolean

是否归档

is_kia

Boolean

是否机密

ssh_url_to_repo

String

仓库ssh地址

http_url_to_repo

String

仓库http地址

web_url

String

仓库页面链接

readme_url

String

仓库readme文件链接

project_id

String

项目id

project_name

String

项目名称

develop_mode

String

仓库开发模式:normal、CR

moderation_result

Boolean

审核状态

状态码:401

表12 响应Body参数

参数

参数类型

描述

error_code

String

参数解释:

错误码。

error_msg

String

参数解释:

错误信息。

请求示例

/v4/repositories/123/merge-requests/123

{
  "title" : "test-title",
  "state_event" : "reopen",
  "assignee_ids" : "111,222",
  "reviewer_ids" : "111,222",
  "description" : "test-desc",
  "milestone_id" : 111,
  "labels" : "222,333",
  "force_remove_source_branch" : true,
  "squash" : "squash提交信息",
  "work_item_ids" : [ "US123456", "US456789" ]
}

响应示例

状态码:200

OK

{
  "id" : 1242586,
  "iid" : 1,
  "title" : "更新 README.md",
  "description" : "merge \\asfd\\ into \\master\\\\n更新 README.md",
  "state" : "opened",
  "created_at" : "2024-08-14T14:17:39.000+08:00",
  "updated_at" : "2025-01-06T15:38:31.000+08:00",
  "source_branch" : "asfd",
  "target_branch" : "master",
  "is_source_branch_protected" : false,
  "devcloud_source_branch" : "asfd",
  "author" : {
    "id" : 1130646,
    "name" : "example_name",
    "username" : "7806db92698840d794a70e53e1295cb7",
    "state" : "active",
    "avatar_url" : null,
    "web_url" : "https://example.com/7806db92698840d794a70e53e1295cb7",
    "nick_name" : "example_name",
    "tenant_name" : "example_name"
  },
  "source_repository_id" : 2663641,
  "target_repository_id" : 2663641,
  "source_project_id" : "07c8683c4bb0445393c5eab0ea354003",
  "target_project_id" : "07c8683c4bb0445393c5eab0ea354003",
  "labels" : [ ],
  "work_in_progress" : false,
  "merge_when_build_succeeds" : false,
  "merge_status" : "can_be_merged",
  "sha" : "2032553aab2bbe1855687885da9165412b3fa3a4",
  "subscribed" : true,
  "user_notes_count" : 0,
  "force_remove_source_branch" : false,
  "web_url" : "https://example.com/********/2663641/1/mergedetail?source\\u003dasfd\\u0026target\\u003dmaster",
  "merge_request_diff" : {
    "id" : 1286204,
    "state" : "collected",
    "merge_request_id" : 1242586,
    "created_at" : "2024-08-14T14:17:39.000+08:00",
    "updated_at" : "2024-08-14T14:17:39.000+08:00",
    "base_commit_sha" : "cdb34fb2431d57aaf6701b4026425acde1d8a84c",
    "real_size" : 1,
    "head_commit_sha" : "2032553aab2bbe1855687885da9165412b3fa3a4",
    "start_commit_sha" : "cdb34fb2431d57aaf6701b4026425acde1d8a84c",
    "commits_count" : 1,
    "external_diff" : "diff-1286204",
    "external_diff_store" : 1,
    "stored_externally" : true,
    "added_lines" : 1,
    "removed_lines" : 1
  },
  "merge_request_reviewers_count" : 0,
  "merge_request_review_count" : 0,
  "merge_request_reviewer_list" : [ ],
  "merge_request_assignee_list" : [ ],
  "notes" : 0,
  "codecheck_state" : 7,
  "codecheck_defect_count" : 0,
  "merge_request_related_work_items" : [ ],
  "diverged_commits_count" : 0,
  "moderation_result" : true,
  "moderation_time" : 0,
  "moderation_status" : 0,
  "is_use_temp_branch" : false,
  "approval_merge_request_approvers" : [ ],
  "review_mode" : "approval",
  "squash" : false,
  "source_repository" : {
    "id" : 2663641,
    "description" : null,
    "name" : "mr_test",
    "name_with_namespace" : "07c8683c4bb0445393c5eab0ea354003 / mr_test",
    "path" : "mr_test",
    "path_with_namespace" : "07c8683c4bb0445393c5eab0ea354003/mr_test",
    "created_at" : "2024-08-05T11:32:27.000+08:00",
    "updated_at" : "2024-08-05T11:32:27.000+08:00",
    "archived" : false,
    "is_kia" : false,
    "ssh_url_to_repo" : "git@example.com:07c8683c4bb0445393c5eab0ea354003/mr_test.git",
    "http_url_to_repo" : "https://example.com/07c8683c4bb0445393c5eab0ea354003/mr_test.git",
    "web_url" : "https://example.com/07c8683c4bb0445393c5eab0ea354003/mr_test",
    "project_id" : "07c8683c4bb0445393c5eab0ea354003",
    "project_name" : "sll_test",
    "develop_mode" : "normal",
    "moderation_result" : true
  },
  "target_repository" : {
    "id" : 2663641,
    "description" : null,
    "name" : "mr_test",
    "name_with_namespace" : "07c8683c4bb0445393c5eab0ea354003 / mr_test",
    "path" : "mr_test",
    "path_with_namespace" : "07c8683c4bb0445393c5eab0ea354003/mr_test",
    "created_at" : "2024-08-05T11:32:27.000+08:00",
    "updated_at" : "2024-08-05T11:32:27.000+08:00",
    "archived" : false,
    "is_kia" : false,
    "ssh_url_to_repo" : "git@example.com:07c8683c4bb0445393c5eab0ea354003/mr_test.git",
    "http_url_to_repo" : "https://example.com/07c8683c4bb0445393c5eab0ea354003/mr_test.git",
    "web_url" : "https://example.com/07c8683c4bb0445393c5eab0ea354003/mr_test",
    "project_id" : "07c8683c4bb0445393c5eab0ea354003",
    "project_name" : "sll_test",
    "develop_mode" : "normal",
    "moderation_result" : true
  },
  "is_source_branch_exist" : true,
  "merge_request_type" : "MergeRequest"
}

状态码:401

Unauthorized

{
  "error_code" : "DEV.00000003",
  "error_msg" : "认证信息过期"
}

SDK代码示例

SDK代码示例如下。

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.codehub.v4.region.CodeHubRegion;
import com.huaweicloud.sdk.codehub.v4.*;
import com.huaweicloud.sdk.codehub.v4.model.*;

import java.util.List;
import java.util.ArrayList;

public class UpdateMergeRequestSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        CodeHubClient client = CodeHubClient.newBuilder()
                .withCredential(auth)
                .withRegion(CodeHubRegion.valueOf("<YOUR REGION>"))
                .build();
        UpdateMergeRequestRequest request = new UpdateMergeRequestRequest();
        request.withRepositoryId({repository_id});
        request.withMergeRequestIid({merge_request_iid});
        PutMergeRequestParamsDto body = new PutMergeRequestParamsDto();
        List<String> listbodyWorkItemIds = new ArrayList<>();
        listbodyWorkItemIds.add("US123456");
        listbodyWorkItemIds.add("US456789");
        body.withWorkItemIds(listbodyWorkItemIds);
        body.withSquash(squash提交信息);
        body.withForceRemoveSourceBranch(true);
        body.withLabels("222,333");
        body.withMilestoneId(111);
        body.withDescription("test-desc");
        body.withReviewerIds("111,222");
        body.withAssigneeIds("111,222");
        body.withStateEvent(PutMergeRequestParamsDto.StateEventEnum.fromValue("reopen"));
        body.withTitle("test-title");
        request.withBody(body);
        try {
            UpdateMergeRequestResponse response = client.updateMergeRequest(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcodehub.v4.region.codehub_region import CodeHubRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcodehub.v4 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = BasicCredentials(ak, sk)

    client = CodeHubClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CodeHubRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = UpdateMergeRequestRequest()
        request.repository_id = {repository_id}
        request.merge_request_iid = {merge_request_iid}
        listWorkItemIdsbody = [
            "US123456",
            "US456789"
        ]
        request.body = PutMergeRequestParamsDto(
            work_item_ids=listWorkItemIdsbody,
            squash=Squash提交信息,
            force_remove_source_branch=True,
            labels="222,333",
            milestone_id=111,
            description="test-desc",
            reviewer_ids="111,222",
            assignee_ids="111,222",
            state_event="reopen",
            title="test-title"
        )
        response = client.update_merge_request(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    codehub "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codehub/v4"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codehub/v4/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codehub/v4/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := codehub.NewCodeHubClient(
        codehub.CodeHubClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.UpdateMergeRequestRequest{}
	request.RepositoryId = int32({repository_id})
	request.MergeRequestIid = int32({merge_request_iid})
	var listWorkItemIdsbody = []string{
        "US123456",
	    "US456789",
    }
	squashPutMergeRequestParamsDto:= squash提交信息
	forceRemoveSourceBranchPutMergeRequestParamsDto:= true
	var labelsPutMergeRequestParamsDto interface{} = "222,333"
	milestoneIdPutMergeRequestParamsDto:= int32(111)
	descriptionPutMergeRequestParamsDto:= "test-desc"
	reviewerIdsPutMergeRequestParamsDto:= "111,222"
	assigneeIdsPutMergeRequestParamsDto:= "111,222"
	stateEventPutMergeRequestParamsDto:= model.GetPutMergeRequestParamsDtoStateEventEnum().REOPEN
	titlePutMergeRequestParamsDto:= "test-title"
	request.Body = &model.PutMergeRequestParamsDto{
		WorkItemIds: &listWorkItemIdsbody,
		Squash: &squashPutMergeRequestParamsDto,
		ForceRemoveSourceBranch: &forceRemoveSourceBranchPutMergeRequestParamsDto,
		Labels: &labelsPutMergeRequestParamsDto,
		MilestoneId: &milestoneIdPutMergeRequestParamsDto,
		Description: &descriptionPutMergeRequestParamsDto,
		ReviewerIds: &reviewerIdsPutMergeRequestParamsDto,
		AssigneeIds: &assigneeIdsPutMergeRequestParamsDto,
		StateEvent: &stateEventPutMergeRequestParamsDto,
		Title: &titlePutMergeRequestParamsDto,
	}
	response, err := client.UpdateMergeRequest(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK

401

Unauthorized

错误码

请参见错误码

相关文档