Help Center/ CodeArts Repo/ API Reference/ APIs/ Repository/ Creating a Protected Branch
Updated on 2025-03-28 GMT+08:00

Creating a Protected Branch

Function

This API is used to create a protected branch.

URI

PUT /v2/repositories/{repository_id}/branch/{branch_name}/protect

Table 1 Path parameters

Parameter

Mandatory

Type

Description

repository_id

Yes

Integer

Repository primary key ID.

branch_name

Yes

String

Branch name.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token.

It can be obtained by calling the IAM API used to obtain a user token. The value of X-Subject-Token in the response header is the user token.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

access_level

Yes

AddProtectAccessLevel object

Permission to create a protected branch.

Table 4 AddProtectAccessLevel

Parameter

Mandatory

Type

Description

push_access_level

Yes

Integer

Commit permission. 0: No one is allowed to commit. 30: Developers and administrators can commit. 40: Administrators can commit.

Enumerated values:

  • 0

  • 30

  • 40

merge_access_level

Yes

Integer

Merge permission. 0: No one can merge; 30: developers and administrators can merge; 40: administrators can merge. The merge permission must be greater than or equal to the commit permission.

Enumerated values:

  • 0

  • 30

  • 40

Response Parameters

Status code: 200

Table 5 Response body parameters

Parameter

Type

Description

error

Error object

Response error.

result

AddProtectResponse object

Response result.

status

String

Response status.

Table 6 Error

Parameter

Type

Description

code

String

Error code.

message

String

Error message.

Table 7 AddProtectResponse

Parameter

Type

Description

name

String

Branch name.

commit

CommitRepoV2 object

Commit message.

protected

Boolean

Whether to protect it.

developers_can_push

Boolean

Whether a developer can commit.

developers_can_merge

Boolean

Whether a developer can merge.

master_can_push

Boolean

Whether an administrator can commit.

master_can_merge

Boolean

Whether an administrator can merge.

no_one_can_push

Boolean

No one can commit.

no_one_can_merge

Boolean

No one can merge.

in_an_opened_merge_request

Boolean

Whether it is in an open MR.

Table 8 CommitRepoV2

Parameter

Type

Description

id

String

SHA ID of a commit.

short_id

String

Short SHA ID of a commit.

created_at

String

Creation time.

title

String

Commit title.

parent_ids

Array of strings

Parent commit ID.

message

String

Commit message.

author_name

String

Author.

committer_name

String

Committer.

committed_date

String

Commit time.

Example Requests

PUT https://{endpoint}/v2/repositories/{repository_id}/branch/{branch_name}/protect

{
  "access_level" : {
    "push_access_level" : 40,
    "merge_access_level" : 40
  }
}

Example Responses

Status code: 200

OK

{
  "result" : {
    "name" : "master",
    "commit" : {
      "id" : "3b68902cf0eb7de3d5c8757e4a990bc3fe0af349",
      "short_id" : "3b68902c",
      "title" : "Create a File",
      "created_at" : "2022-03-08T03:14:39.000Z",
      "parent_ids" : [ "08618c900a4048aae7e4cd88913d3d521eae02eb" ],
      "message": " Create a file",
      "author_name" : "repo",
      "committer_name" : "repo",
      "committed_date" : "2022-03-08T03:14:39.000Z"
    },
    "protected" : true,
    "developers_can_push" : false,
    "developers_can_merge" : false,
    "master_can_push" : false,
    "master_can_merge" : false,
    "no_one_can_push" : true,
    "no_one_can_merge" : true,
    "in_an_opened_merge_request" : false
  },
  "status" : "success"
}

Status Codes

Status Code

Description

200

OK

Error Codes

See Error Codes.