- 有効なアクセスレベル
 - 保護されたブランチのリスト
 - 単一のプロテクトブランチまたはワイルドカードプロテクトブランチを取得します。
 - リポジトリブランチの保護
 - リポジトリブランチの保護解除
 - 保護されたブランチの更新
 
グループレベルの保護ブランチAPI
GitLab 15.9で
group_protected_branchesというフラグで導入されました。デフォルトでは無効です。
フラグ: セルフマネジメントのGitLabでは、デフォルトではこの機能は利用できません。利用可能にするには、管理者がgroup_protected_branchesという機能フラグを有効にします。GitLab.comでは、この機能は利用できません。
有効なアクセスレベル
アクセスレベルはProtectedRefAccess.allowed_access_levels メソッドで定義されています。これらのレベルは認識されます:
0  => No access
30 => Developer access
40 => Maintainer access
60 => Admin access
保護されたブランチのリスト
グループから保護されたブランチのリストを取得します。ワイルドカードが設定されている場合は、そのワイルドカードにマッチするブランチの正確な名前の代わりにワイルドカードが返されます。
GET /groups/:id/protected_branches
| 属性 | 種類 | 必須 | 説明 | 
|---|---|---|---|
id | 整数または文字列。 | yes | 認証されたユーザーが所有するグループのID またはURL エンコードされたパス。 | 
search | 文字列です。 | いいえ | 検索する保護ブランチの名前または名前の一部。 | 
curl --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/5/protected_branches"
応答例
[
  {
    "id": 1,
    "name": "master",
    "push_access_levels": [
      {
        "id":  1,
        "access_level": 40,
        "user_id": null,
        "group_id": 1234,
        "access_level_description": "Maintainers"
      }
    ],
    "merge_access_levels": [
      {
        "id":  1,
        "access_level": 40,
        "user_id": null,
        "group_id": 1234,
        "access_level_description": "Maintainers"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  {
    "id": 1,
    "name": "release/*",
    "push_access_levels": [
      {
        "id":  1,
        "access_level": 40,
        "user_id": null,
        "group_id": null,
        "access_level_description": "Maintainers"
      }
    ],
    "merge_access_levels": [
      {
        "id":  1,
        "access_level": 40,
        "user_id": null,
        "group_id": 1234,
        "access_level_description": "Maintainers"
      }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
  },
  ...
]
単一のプロテクトブランチまたはワイルドカードプロテクトブランチを取得します。
単一のプロテクトブランチまたはワイルドカードプロテクトブランチを取得します。
GET /groups/:id/protected_branches/:name
| 属性 | 種類 | 必須 | 説明 | 
|---|---|---|---|
id | 整数または文字列。 | yes | 認証されたユーザーが所有するグループのID またはURL エンコードされたパス。 | 
name | 文字列です。 | yes | ブランチの名前またはワイルドカード。 | 
curl --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/5/protected_branches/master"
応答例
{
  "id": 1,
  "name": "master",
  "push_access_levels": [
    {
      "id":  1,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "id":  1,
      "access_level": null,
      "user_id": null,
      "group_id": 1234,
      "access_level_description": "Example Merge Group"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}
リポジトリブランチの保護
ワイルドカード保護ブランチを使用して、単一のリポジトリブランチを保護します。
POST /groups/:id/protected_branches
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/5/protected_branches?name=*-stable&push_access_level=30&merge_access_level=30&unprotect_access_level=40"
| 属性 | 種類 | 必須 | 説明 | 
|---|---|---|---|
id | 整数または文字列。 | yes | 認証されたユーザーが所有するグループのID またはURL エンコードされたパス。 | 
name | 文字列です。 | yes | ブランチの名前またはワイルドカード。 | 
allow_force_push | boolean | いいえ | プッシュアクセス権を持つすべてのユーザーに強制プッシュを許可します。デフォルト:false. | 
allowed_to_merge | アレイ | いいえ | 
{user_id: integer},{group_id: integer}, または{access_level: integer} 形式のハッシュで記述された、 マージが許可されるアクセスレベルの配列。 | 
allowed_to_push | アレイ | いいえ | プッシュが許可されるアクセスレベルの配列。各レベルは{user_id: integer},{group_id: integer}, または{access_level: integer} の形のハッシュで記述されます。 | 
allowed_to_unprotect | アレイ | いいえ | 保護解除が許可されるアクセスレベルの配列。各レベルは{user_id: integer},{group_id: integer}, または{access_level: integer} の形式のハッシュで表されます。 | 
code_owner_approval_required | boolean | いいえ | このブランチがCODEOWNERS ファイルの項目にマッチした場合に、そのブランチへのプッシュを禁止します。デフォルト:false. | 
merge_access_level | 整数。 | いいえ | マージが許可されるアクセス・レベル。デフォルト:40メンテナーのロール。 | 
push_access_level | 整数。 | いいえ | プッシュを許可するアクセス・レベル。デフォルト:40メンテナーのロール。 | 
unprotect_access_level | 整数。 | いいえ | 保護解除を許可するアクセス・レベル。デフォルト:40メンテナーのロール。 | 
応答例
{
  "id": 1,
  "name": "*-stable",
  "push_access_levels": [
    {
      "id":  1,
      "access_level": 30,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "merge_access_levels": [
    {
      "id":  1,
      "access_level": 30,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Developers + Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "id":  1,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}
ユーザー/グループレベルのアクセスの例
allowed_to_push /allowed_to_merge /allowed_to_unprotect 配列の要素は、{user_id: integer},{group_id: integer}, または{access_level: integer}の形式をとる必要があります。 各ユーザーはこのプロジェクトにアクセスでき、各グループはこのプロジェクトを共有する必要があります。これらのアクセスレベルで、保護されたブランチへのアクセスをより細かく制御することができます。
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/5/protected_branches?name=*-stable&allowed_to_push%5B%5D%5Buser_id%5D=1"
応答例
{
  "id": 1,
  "name": "*-stable",
  "push_access_levels": [
    {
      "id":  1,
      "access_level": null,
      "user_id": 1,
      "group_id": null,
      "access_level_description": "Administrator"
    }
  ],
  "merge_access_levels": [
    {
      "id":  1,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "unprotect_access_levels": [
    {
      "id":  1,
      "access_level": 40,
      "user_id": null,
      "group_id": null,
      "access_level_description": "Maintainers"
    }
  ],
  "allow_force_push":false,
  "code_owner_approval_required": false
}
プッシュ許可とマージ許可の例
リクエストの例
curl --request POST \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     --header "Content-Type: application/json" \
     --data '{
      "name": "master",
      "allowed_to_push": [{"access_level": 30}],
      "allowed_to_merge": [{
          "access_level": 30
        },{
          "access_level": 40
        }
      ]}'
     "https://gitlab.example.com/api/v4/groups/5/protected_branches"
応答例
{
    "id": 5,
    "name": "master",
    "push_access_levels": [
        {
            "id": 1,
            "access_level": 30,
            "access_level_description": "Developers + Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "merge_access_levels": [
        {
            "id": 1,
            "access_level": 30,
            "access_level_description": "Developers + Maintainers",
            "user_id": null,
            "group_id": null
        },
        {
            "id": 2,
            "access_level": 40,
            "access_level_description": "Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "unprotect_access_levels": [
        {
            "id": 1,
            "access_level": 40,
            "access_level_description": "Maintainers",
            "user_id": null,
            "group_id": null
        }
    ],
    "allow_force_push":false,
    "code_owner_approval_required": false
}
リポジトリブランチの保護解除
指定した保護ブランチまたはワイルドカード保護ブランチの保護を解除します。
DELETE /groups/:id/protected_branches/:name
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/5/protected_branches/*-stable"
| 属性 | 種類 | 必須 | 説明 | 
|---|---|---|---|
id | 整数または文字列。 | yes | 認証されたユーザーが所有するグループのID またはURL エンコードされたパス。 | 
name | 文字列です。 | yes | ブランチの名前。 | 
応答例
{
   "name": "master",
   "push_access_levels": [
      {
         "id": 12,
         "access_level": 40,
         "access_level_description": "Maintainers",
         "user_id": null,
         "group_id": null
      }
   ]
}
保護されたブランチの更新
プロテクトブランチを更新します。
PATCH /groups/:id/protected_branches/:name
curl --request PATCH --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/5/protected_branches/feature-branch?allow_force_push=true&code_owner_approval_required=true"
| 属性 | 種類 | 必須 | 説明 | 
|---|---|---|---|
id | 整数または文字列。 | yes | 認証されたユーザーが所有するグループのID またはURL エンコードされたパス。 | 
name | 文字列です。 | yes | ブランチの名前。 | 
allow_force_push | boolean | いいえ | 有効にすると、このブランチにプッシュできるメンバーは強制プッシュもできるようになります。 | 
allowed_to_push | アレイ | いいえ | プッシュアクセスレベルの配列。 | 
allowed_to_merge | アレイ | いいえ | マージアクセスレベルの配列。 | 
allowed_to_unprotect | アレイ | いいえ | ハッシュで記述された保護解除アクセスレベルの配列。 | 
code_owner_approval_required | boolean | いいえ | このブランチがCODEOWNERS ファイルの項目にマッチした場合に、そのブランチへのプッシュを禁止します。デフォルト:false. | 
allowed_to_push 、allowed_to_merge 、allowed_to_unprotect 配列の要素は、以下のようにします:
- 
user_id,group_id,access_levelのいずれかであること。 - 
{user_id: integer},{group_id: integer}, または{access_level: integer}のいずれかの形式をとります。 
更新するには
- 
user_id:更新したユーザーがプロジェクトにアクセスできることを確認します。また、それぞれのハッシュにaccess_levelのidを渡す必要があります。 - 
group_id:更新されたグループがこのプロジェクトを共有していることを確認します。それぞれのハッシュのaccess_levelのidも渡す必要があります。 
削除するには
- 
_destroytrue次の例を参照してください。 
例:push_access_level レコードの作成
curl --header 'Content-Type: application/json' --request PATCH \
     --data '{"allowed_to_push": [{access_level: 40}]}' \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     "https://gitlab.example.com/api/v4/groups/22034114/protected_branches/master"
応答例
{
   "name": "master",
   "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>" "https://gitlab.example.com/api/v4/groups/22034114/protected_branches/master"
応答例
{
   "name": "master",
   "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>" "https://gitlab.example.com/api/v4/groups/22034114/protected_branches/master"
応答例
{
   "name": "master",
   "push_access_levels": []
}