{{< details >}}

  • Tier: 基础版, 专业版, 旗舰版
  • Offering: JihuLab.com, 私有化部署

{{< /details >}}

有效的访问级别

访问级别在 ProtectedRefAccess.allowed_access_levels 方法中定义。以下级别被认可:

0  => No access
30 => Developer access
40 => Maintainer access
60 => Admin access

列出受保护的分支

{{< history >}}

  • Deploy key 信息在极狐GitLab 16.0 中引入。

{{< /history >}}

从项目中获取受保护的分支列表,如在 UI 中定义的那样。如果设置了通配符,则返回匹配该通配符的分支的确切名称。

GET /projects/:id/protected_branches
属性 类型 必需 描述
id integer/string yes 项目的 ID 或 URL 编码路径
search string no 要搜索的受保护分支的名称或部分名称

在以下示例中,项目 ID 为 5

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches"

以下示例响应包括:

  • 两个受保护的分支,ID 分别为 100101
  • push_access_levels 的 ID 为 100110021003
  • merge_access_levels 的 ID 为 20012002
[
  {
    "id": 100,
    "name": "main",
    "push_access_levels": [
      {
        "id":  1001,
        "access_level": 40,
        "access_level_description": "Maintainers"
      },
      {
        "id": 1002,
        "access_level": 40,
        "access_level_description": "Deploy key",
        "deploy_key_id": 1
      }
    ],
    "merge_access_levels": [
      {
        "id":  2001,
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  {
    "id": 101,
    "name": "release/*",
    "push_access_levels": [
      {
        "id":  1003,
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "merge_access_levels": [
      {
        "id":  2002,
        "access_level": 40,
        "access_level_description": "Maintainers"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  ...
]

使用极狐GitLab 专业版或旗舰版的用户还会看到 user_idgroup_idinherited 参数。如果存在 inherited 参数,表示设置是从项目的群组继承的。

以下示例响应包括:

  • 一个受保护的分支,ID 为 100
  • push_access_levels 的 ID 为 10011002
  • merge_access_levels 的 ID 为 2001
[
  {
    "id": 101,
    "name": "main",
    "push_access_levels": [
      {
        "id":  1001,
        "access_level": 40,
        "user_id": null,
        "group_id": null,
        "access_level_description": "Maintainers"
      },
      {
        "id": 1002,
        "access_level": 40,
        "access_level_description": "Deploy key",
        "deploy_key_id": 1,
        "user_id": null,
        "group_id": null
      }
    ],
    "merge_access_levels": [
      {
        "id":  2001,
        "access_level": null,
        "user_id": null,
        "group_id": 1234,
        "access_level_description": "Example Merge Group"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false,
    "inherited": true
  },
  ...
]

获取单个受保护的分支或通配符受保护的分支

获取单个受保护的分支或通配符受保护的分支。

GET /projects/:id/protected_branches/:name
属性 类型 必需 描述
id integer/string yes 项目的 ID 或 URL 编码路径
name string yes 分支或通配符的名称

在以下示例中,项目 ID 为 5,分支名称为 main

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches/main"

示例响应:

{
  "id": 101,
  "name": "main",
  "push_access_levels": [
    {
      "id":  1001,
      "access_level": 40,
      "access_level_description": "Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "id":  2001,
      "access_level": 40,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

使用极狐GitLab 专业版或旗舰版的用户还会看到 user_idgroup_id 参数:

示例响应:

{
  "id": 101,
  "name": "main",
  "push_access_levels": [
    {
      "id":  1001,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "id":  2001,
      "access_level": null,
      "user_id": null,
      "group_id": 1234,
      "access_level_description": "Example Merge Group"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

保护仓库分支

{{< history >}}

  • deploy_key_id 配置在极狐GitLab 17.5 中引入。

{{< /history >}}

保护单个仓库分支或使用通配符受保护分支保护多个项目仓库分支。

POST /projects/:id/protected_branches
属性 类型 必需 描述
id integer/string yes 项目的 ID 或 URL 编码路径
name string yes 分支或通配符的名称。
allow_force_push boolean no 启用后,可以推送到此分支的成员也可以强制推送。(默认:false
allowed_to_merge array no 合并访问级别数组,每个级别由 {user_id: integer}{group_id: integer}{access_level: integer} 的形式描述。仅适用于专业版和旗舰版。
allowed_to_push array no 推送访问级别数组,每个级别由 {user_id: integer}{group_id: integer}{deploy_key_id: integer}{access_level: integer} 的形式描述。仅适用于专业版和旗舰版。
allowed_to_unprotect array no 取消保护访问级别数组,每个级别由 {user_id: integer}{group_id: integer}{access_level: integer} 的形式描述。此字段不提供 No access 访问级别。仅适用于专业版和旗舰版。
code_owner_approval_required boolean no 如果此分支与 CODEOWNERS 文件中的项目匹配,则阻止推送。(默认:false)仅适用于专业版和旗舰版。
merge_access_level integer no 允许合并的访问级别。(默认:40,维护者角色)。
push_access_level integer no 允许推送的访问级别。(默认:40,维护者角色)
unprotect_access_level integer no 允许取消保护的访问级别。(默认:40,维护者角色)

在以下示例中,项目 ID 为 5,分支名称为 *-stable

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&push_access_level=30&merge_access_level=30&unprotect_access_level=40"

示例响应包括:

  • 一个受保护的分支,ID 为 101
  • push_access_levels 的 ID 为 1001
  • merge_access_levels 的 ID 为 2001
  • unprotect_access_levels 的 ID 为 3001
{
  "id": 101,
  "name": "*-stable",
  "push_access_levels": [
    {
      "id":  1001,
      "access_level": 30,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "id":  2001,
      "access_level": 30,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "id":  3001,
      "access_level": 40,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

使用极狐GitLab 专业版或旗舰版的用户还会看到 user_idgroup_id 参数:

以下示例响应包括:

  • 一个受保护的分支,ID 为 101
  • push_access_levels 的 ID 为 1001
  • merge_access_levels 的 ID 为 2001
  • unprotect_access_levels 的 ID 为 3001
{
  "id": 1,
  "name": "*-stable",
  "push_access_levels": [
    {
      "id":  1001,
      "access_level": 30,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "id":  2001,
      "access_level": 30,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "id":  3001,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

带有用户推送访问和群组合并访问的示例

{{< details >}}

  • Tier: Premium, Ultimate
  • Offering: JihuLab.com, 极狐GitLab 私有化部署, 极狐GitLab Dedicated

{{< /details >}}

allowed_to_push / allowed_to_merge / allowed_to_unprotect 数组中的元素应采用 {user_id: integer}{group_id: integer}{access_level: integer} 的形式。每个用户必须有项目访问权限,每个群组必须共享此项目。这些访问级别允许对受保护分支访问进行更细粒度的控制。有关更多信息,请参阅将群组添加到受保护的分支

以下示例请求创建一个受保护的分支,具有用户推送访问和群组合并访问权限。user_id2group_id3

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&allowed_to_push%5B%5D%5Buser_id%5D=2&allowed_to_merge%5B%5D%5Bgroup_id%5D=3"

以下示例响应包括:

  • 一个受保护的分支,ID 为 101
  • push_access_levels 的 ID 为 1001
  • merge_access_levels 的 ID 为 2001
  • unprotect_access_levels 的 ID 为 3001
{
  "id": 101,
  "name": "*-stable",
  "push_access_levels": [
    {
      "id":  1001,
      "access_level": null,
      "user_id": 2,
      "group_id": null,
      "access_level_description": "Administrator"
    }
  ],
  "merge_access_levels": [
    {
      "id":  2001,
      "access_level": null
      "user_id": null,
      "group_id": 3,
      "access_level_description": "Example Merge Group"
    }
  ],
  "unprotect_access_levels": [
    {
      "id":  3001,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

带有部署密钥访问的示例

{{< details >}}

  • Tier: 专业版, 旗舰版
  • Offering: JihuLab.com, 私有化部署

{{< /details >}}

{{< history >}}

  • 在极狐GitLab 17.5 中引入。

{{< /history >}}

allowed_to_push 数组中的元素应采用 {user_id: integer}{group_id: integer}{deploy_key_id: integer}{access_level: integer} 的形式。部署密钥必须为您的项目启用,并且必须对您的项目仓库具有写入权限。有关其他要求,请参阅允许部署密钥推送到受保护的分支

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&allowed_to_push[][deploy_key_id]=1"

以下示例响应包括:

  • 一个受保护的分支,ID 为 101
  • push_access_levels 的 ID 为 1001
  • merge_access_levels 的 ID 为 2001
  • unprotect_access_levels 的 ID 为 3001
{
  "id": 101,
  "name": "*-stable",
  "push_access_levels": [
    {
      "id":  1001,
      "access_level": null,
      "user_id": null,
      "group_id": null,
      "deploy_key_id": 1,
      "access_level_description": "Deploy"
    }
  ],
  "merge_access_levels": [
    {
      "id":  2001,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "id":  3001,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}

允许推送和合并访问的示例

{{< details >}}

  • Tier: 专业版, 旗舰版
  • Offering: JihuLab.com, 私有化部署

{{< /details >}}

{{< history >}}

  • 在 13.9 中转移到极狐GitLab 专业版。

{{< /history >}}

示例请求:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --header "Content-Type: application/json" \
  --data '{
    "name": "main",
    "allowed_to_push": [
      {"access_level": 30}
    ],
    "allowed_to_merge": [
      {"access_level": 30},
      {"access_level": 40}
    ]
  }'
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches"

以下示例响应包括:

  • 一个受保护的分支,ID 为 105
  • push_access_levels 的 ID 为 1001
  • merge_access_levels 的 ID 为 20012002
  • unprotect_access_levels 的 ID 为 3001
{
    "id": 105,
    "name": "main",
    "push_access_levels": [
        {
            "id": 1001,
            "access_level": 30,
            "access_level_description": "Developers + Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "merge_access_levels": [
        {
            "id": 2001,
            "access_level": 30,
            "access_level_description": "Developers + Maintainers",
            "user_id": null,
            "group_id": null
        },
        {
            "id": 2002,
            "access_level": 40,
            "access_level_description": "Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "unprotect_access_levels": [
        {
            "id": 3001,
            "access_level": 40,
            "access_level_description": "Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
}

取消保护仓库分支

取消保护给定的受保护分支或通配符受保护分支。

DELETE /projects/:id/protected_branches/:name
属性 类型 必需 描述
id integer/string yes 项目的 ID 或 URL 编码路径
name string yes 分支的名称

在以下示例中,项目 ID 为 5,分支名称为 *-stable

curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches/*-stable"

更新受保护的分支

{{< history >}}

  • 在极狐GitLab 15.6 中引入。
  • deploy_key_id 配置在极狐GitLab 17.5 中引入。

{{< /history >}}

更新受保护的分支。

PATCH /projects/:id/protected_branches/:name
属性 类型 必需 描述
id integer/string yes 项目的 ID 或 URL 编码路径
name string yes 分支或通配符的名称。
allow_force_push boolean no 启用后,可以推送到此分支的成员也可以强制推送。
allowed_to_merge array no 合并访问级别数组,每个级别由 {user_id: integer}{group_id: integer}{access_level: integer} 的形式描述。仅适用于专业版和旗舰版。
allowed_to_push array no 推送访问级别数组,每个级别由 {user_id: integer}{group_id: integer}{deploy_key_id: integer}{access_level: integer} 的形式描述。仅适用于专业版和旗舰版。
allowed_to_unprotect array no 取消保护访问级别数组,每个级别由 {user_id: integer}{group_id: integer}{access_level: integer}{id: integer, _destroy: true} 的形式描述,以销毁现有的访问级别。此字段不提供 No access 访问级别。仅适用于专业版和旗舰版。
code_owner_approval_required boolean no 如果此分支与 CODEOWNERS 文件中的项目匹配,则阻止推送。仅适用于专业版和旗舰版。

在以下示例中,项目 ID 为 5,分支名称为 feature-branch

curl --request PATCH \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/5/protected_branches/feature-branch?allow_force_push=true&code_owner_approval_required=true"

allowed_to_pushallowed_to_mergeallowed_to_unprotect 数组中的元素应该是 user_idgroup_idaccess_level,并采用 {user_id: integer}{group_id: integer}{access_level: integer} 的形式。

allowed_to_push 包含一个额外的元素 deploy_key_id,其形式为 {deploy_key_id: integer}

要更新:

  • user_id: 确保更新的用户具有项目访问权限。您还必须在相应的哈希中传递 access_levelid
  • group_id: 确保更新的群组共享此项目。您还必须在相应的哈希中传递 access_levelid
  • deploy_key_id: 确保部署密钥已为您的项目启用,并且必须对您的项目仓库具有写入权限。

要删除:

  • 必须传递 _destroy 设置为 true。请参阅以下示例。

示例:创建 push_access_level 记录

curl --header 'Content-Type: application/json' --request PATCH \
  --data '{"allowed_to_push": [{"access_level": 40}]}' \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/22034114/protected_branches/main"

示例响应:

{
   "name": "main",
   "push_access_levels": [
      {
         "id": 12,
         "access_level": 40,
         "access_level_description": "Maintainers",
         "user_id": null,
         "group_id": null
      }
   ]
}

示例:更新 push_access_level 记录

curl --header 'Content-Type: application/json' --request PATCH \
  --data '{"allowed_to_push": [{"id": 12, "access_level": 0}]}' \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/22034114/protected_branches/main"

示例响应:

{
   "name": "main",
   "push_access_levels": [
      {
         "id": 12,
         "access_level": 0,
         "access_level_description": "No One",
         "user_id": null,
         "group_id": null
      }
   ]
}

示例:删除 push_access_level 记录

curl --header 'Content-Type: application/json' --request PATCH \
  --data '{"allowed_to_push": [{"id": 12, "_destroy": true}]}' \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/22034114/protected_branches/main"

示例响应:

{
   "name": "main",
   "push_access_levels": []
}

相关主题