- 有効なアクセスレベル
- 保護されたブランチのリスト
- 単一のプロテクトブランチまたはワイルドカードプロテクトブランチを取得します。
- リポジトリブランチの保護
- リポジトリブランチの保護解除
- 保護されたブランチの更新
- 関連するトピック
保護ブランチAPI
有効なアクセスレベル
アクセス・レベルはProtectedRefAccess.allowed_access_levels メソッドで定義されます。現在、これらのレベルが認識されています:
0 => No access
30 => Developer access
40 => Maintainer access
60 => Admin access
保護されたブランチのリスト
GitLab 16.0で導入されたキー情報をデプロイします。
プロジェクトから、UI で定義されているprotected ブランチのリストを取得します。ワイルドカードが設定されている場合、そのワイルドカードにマッチするブランチの正確な名前の代わりにワイルドカードが返されます。
GET /projects/:id/protected_branches
| 属性 | 種類 | 必須 | 説明 |
|---|---|---|---|
id | 整数/文字列 | yes | 認証ユーザーが所有するプロジェクトのIDまたはURLエンコードされたパス |
search | 文字列です。 | いいえ | 検索する保護ブランチの名前または名前の一部 |
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches"
応答例
[
{
"id": 1,
"name": "master",
"push_access_levels": [
{
"id": 1,
"access_level": 40,
"access_level_description": "Maintainers"
},
{
"id": 2,
"access_level": 40,
"access_level_description": "Deploy key",
"deploy_key_id": 1
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 40,
"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,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
},
...
]
GitLab Premium または Ultimate のユーザーもuser_id,group_id とinherited パラメータを inherited見ることができます。inherited パラメータが inherited存在する場合、その設定はプロジェクトのグループから継承されたことを意味します。
応答例
[
{
"id": 1,
"name": "master",
"push_access_levels": [
{
"id": 1,
"access_level": 40,
"user_id": null,
"group_id": null,
"access_level_description": "Maintainers"
},
{
"id": 2,
"access_level": 40,
"access_level_description": "Deploy key",
"deploy_key_id": 1,
"user_id": null,
"group_id": null
}
],
"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,
"inherited": true
},
...
]
単一のプロテクトブランチまたはワイルドカードプロテクトブランチを取得します。
単一のプロテクトブランチまたはワイルドカードプロテクトブランチを取得します。
GET /projects/:id/protected_branches/:name
| 属性 | 種類 | 必須 | 説明 |
|---|---|---|---|
id | 整数/文字列 | yes | 認証ユーザーが所有するプロジェクトのIDまたはURLエンコードされたパス |
name | 文字列です。 | yes | ブランチの名前またはワイルドカード |
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/master"
応答例
{
"id": 1,
"name": "master",
"push_access_levels": [
{
"id": 1,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}
GitLab Premium または Ultimate のユーザーには、user_id とgroup_id パラメータも表示されます:
応答例
{
"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 /projects/:id/protected_branches
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "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 | 整数/文字列 | 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} の形式のハッシュで記述されます。 アクセス・レベルNo access は、このフィールドでは使用できません。 |
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,
"access_level_description": "Developers + Maintainers"
}
],
"merge_access_levels": [
{
"id": 1,
"access_level": 30,
"access_level_description": "Developers + Maintainers"
}
],
"unprotect_access_levels": [
{
"id": 1,
"access_level": 40,
"access_level_description": "Maintainers"
}
],
"allow_force_push":false,
"code_owner_approval_required": false
}
GitLab Premium または Ultimate のユーザーには、user_id とgroup_id パラメータも表示されます:
応答例
{
"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/projects/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
}
プッシュ許可とマージ許可の例
13.9でGitLab Premiumに移行しました。
リクエストの例
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/projects/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 /projects/:id/protected_branches/:name
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/*-stable"
| 属性 | 種類 | 必須 | 説明 |
|---|---|---|---|
id | 整数/文字列 | yes | 認証ユーザーが所有するプロジェクトのIDまたはURLエンコードされたパス |
name | 文字列です。 | yes | ブランチの名前 |
保護されたブランチの更新
GitLab 15.6で導入されました。
プロテクトブランチを更新します。
PATCH /projects/:id/protected_branches/:name
curl --request PATCH --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/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_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}, または{id: integer, _destroy: true} の形式のハッシュで記述され、既存のアクセ ス・レベルを破棄します。アクセス・レベルNo access はこのフィールドでは使用できません。 |
code_owner_approval_required
| boolean | いいえ |
CODEOWNERS ファイル の項目にマッチした場合に、このブランチへのプッシュを防ぎます。 |
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/projects/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/projects/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/projects/22034114/protected_branches/master"
応答例
{
"name": "master",
"push_access_levels": []
}