mcpField
G
gitlab-protected-branch

gitlab-protected-branch

github.com/grandcamelセキュリティ488 回インストール40 週間前に更新
リポジトリ

gitlab-protected-branch について

`glab api` の直接呼び出しで GitLab の保護ブランチを管理するスキルです。メンテナーがプッシュ、マージ、強制プッシュ、コードオーナー承認のルールを確認・設定でき、変更操作のリスクも示します。

主な機能

  1. 01保護ブランチの一覧表示
  2. 02ブランチルールの確認
  3. 03アクセスレベルの設定と更新
  4. 04強制プッシュとコードオーナールールの設定
  5. 05ブランチ保護の解除

活用シーン

  1. 01プロジェクトのブランチ保護を監査
  2. 02main へのプッシュやマージを制限
  3. 03リリースブランチにワイルドカードルールを適用
  4. 04保護ブランチのアクセス権を一時変更

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/grandcamel/gitlab-assistant-skills --skill gitlab-protected-branch

インストール後、Claude Code で/gitlab-protected-branchを入力して読み込まれたか確認します。

詳細

保護ブランチスキル

glab api の生のエンドポイント呼び出しを使用した GitLab のブランチ保護管理です。

クイックリファレンス

操作コマンドパターンリスク
保護ブランチ一覧glab api projects/:id/protected_branches-
保護設定の取得glab api projects/:id/protected_branches/:name-
ブランチの保護glab api projects/:id/protected_branches -X POST -f ...⚠️
保護設定の更新glab api projects/:id/protected_branches/:name -X PATCH -f ...⚠️
ブランチの保護解除glab api projects/:id/protected_branches/:name -X DELETE⚠️⚠️

リスクの凡例:- 安全 | ⚠️ 注意 | ⚠️⚠️ 警告 | ⚠️⚠️⚠️ 危険

このスキルを使用する場合

必ず使用する場合:

  • ユーザーが「ブランチを保護」「ブランチ保護」「保護ブランチ」に言及した場合
  • ユーザーがブランチへのプッシュ/マージを許可するユーザーを制限したい場合
  • ユーザーが「フォースプッシュ」「コードオーナー」「マージアクセス」に言及した場合
  • ユーザーが main/release ブランチのセキュリティを設定したい場合

使用しない場合:

  • ユーザーがブランチを作成/削除したい場合(git または gitlab-repo を使用)
  • ユーザーがマージリクエストの承認を管理したい場合(別の API を使用)
  • ユーザーがブランチの CI/CD を設定したい場合(gitlab-ci を使用)

API の前提条件

必須のトークンスコープ: api

権限:

  • 保護ブランチの表示:Developer 以上
  • 保護ブランチの管理:Maintainer 以上

Premium の機能:

  • コードオーナーの承認:GitLab Premium
  • 複数のアクセスレベル:GitLab Premium

アクセスレベル

レベル値説明
アクセスなし0誰も操作を実行できません
Developer30Developer 以上
Maintainer40Maintainer 以上
Admin60インスタンス管理者のみ

使用可能なコマンド

保護ブランチの一覧表示
# List all protected branches
glab api projects/123/protected_branches --method GET

# With pagination
glab api projects/123/protected_branches --paginate

# Using project path
glab api "projects/$(echo 'mygroup/myproject' | jq -Rr @uri)/protected_branches"
保護設定の詳細を取得
# Get protection for specific branch
glab api projects/123/protected_branches/main --method GET

# Branch with special characters (URL-encode)
glab api "projects/123/protected_branches/$(echo 'release/1.0' | jq -Rr @uri)"

# Branch with wildcard pattern
glab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)"
ブランチを保護
# Basic protection (maintainers push, developers merge)
glab api projects/123/protected_branches --method POST \
  -f name="main" \
  -f push_access_level=40 \
  -f merge_access_level=30

# Strict protection (only maintainers)
glab api projects/123/protected_branches --method POST \
  -f name="main" \
  -f push_access_level=40 \
  -f merge_access_level=40 \
  -f allow_force_push=false

# With code owner approval (Premium)
glab api projects/123/protected_branches --method POST \
  -f name="main" \
  -f push_access_level=40 \
  -f merge_access_level=30 \
  -f code_owner_approval_required=true

# Protect wildcard pattern
glab api projects/123/protected_branches --method POST \
  -f name="release/*" \
  -f push_access_level=40 \
  -f merge_access_level=40

# Allow developers to push, anyone to merge
glab api projects/123/protected_branches --method POST \
  -f name="develop" \
  -f push_access_level=30 \
  -f merge_access_level=30 \
  -f allow_force_push=false

# No direct push (only through MR)
glab api projects/123/protected_branches --method POST \
  -f name="main" \
  -f push_access_level=0 \
  -f merge_access_level=30
保護設定を更新
# Change merge access level
glab api projects/123/protected_branches/main --method PATCH \
  -f merge_access_level=40

# Enable code owner approval (Premium)
glab api projects/123/protected_branches/main --method PATCH \
  -f code_owner_approval_required=true

# Allow force push (not recommended for main)
glab api projects/123/protected_branches/feature%2F* --method PATCH \
  -f allow_force_push=true
ブランチの保護を解除

警告: この操作により、ブランチの保護がすべて解除されます!

# Unprotect branch
glab api projects/123/protected_branches/main --method DELETE

# Unprotect wildcard pattern (URL-encode)
glab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)" --method DELETE

保護オプション

オプション型説明
namestringブランチ名またはワイルドカードパターン
push_access_levelintegerプッシュできるユーザー(0、30、40、60)
merge_access_levelintegerMR をマージできるユーザー(0、30、40、60)
unprotect_access_levelinteger保護を解除できるユーザー(40、60)
allow_force_pushbooleanブランチへのフォースプッシュを許可
code_owner_approval_requiredbooleanコードオーナーの承認を必須にする(Premium)

ワイルドカードパターン

パターン一致するブランチ
*すべてのブランチ
feature/*feature/login、feature/signup など
release/*release/1.0、release/2.0 など
hotfix/*hotfix/bug-123 など
*-stable1.0-stable、2.0-stable など

よく使うワークフロー

ワークフロー 1:標準的なブランチ保護
# Protect main branch
glab api projects/123/protected_branches --method POST \
  -f name="main" \
  -f push_access_level=40 \
  -f merge_access_level=30 \
  -f allow_force_push=false

# Protect develop branch
glab api projects/123/protected_branches --method POST \
  -f name="develop" \
  -f push_access_level=30 \
  -f merge_access_level=30 \
  -f allow_force_push=false

# Protect release branches
glab api projects/123/protected_branches --method POST \
  -f name="release/*" \
  -f push_access_level=40 \
  -f merge_access_level=40
ワークフロー 2:現在の保護設定を監査
# List all protections with details
glab api projects/123/protected_branches --paginate | \
  jq -r '.[] | "Branch: \(.name)\n  Push: \(.push_access_levels[0].access_level_description // "none")\n  Merge: \(.merge_access_levels[0].access_level_description // "none")\n  Force Push: \(.allow_force_push)\n"'
ワークフロー 3:本番ブランチをロックダウン
# Strict protection: only maintainers, no force push, require code owners
glab api projects/123/protected_branches --method POST \
  -f name="production" \
  -f push_access_level=40 \
  -f merge_access_level=40 \
  -f allow_force_push=false \
  -f code_owner_approval_required=true
ワークフロー 4:保護ブランチへのプッシュを一時的に許可
# 1. Check current protection
glab api projects/123/protected_branches/main

# 2. Update to allow developer push
glab api projects/123/protected_branches/main --method PATCH \
  -f push_access_level=30

# 3. Do the work...

# 4. Restore protection
glab api projects/123/protected_branches/main --method PATCH \
  -f push_access_level=40
ワークフロー 5:GitFlow の保護を設定
project_id=123

# Main - production (strict)
glab api projects/$project_id/protected_branches --method POST \
  -f name="main" \
  -f push_access_level=0 \
  -f merge_access_level=40 \
  -f allow_force_push=false

# Develop - integration
glab api projects/$project_id/protected_branches --method POST \
  -f name="develop" \
  -f push_access_level=30 \
  -f merge_access_level=30

# Feature branches - allow developers
glab api projects/$project_id/protected_branches --method POST \
  -f name="feature/*" \
  -f push_access_level=30 \
  -f merge_access_level=30

# Release branches - maintainers only
glab api projects/$project_id/protected_branches --method POST \
  -f name="release/*" \
  -f push_access_level=40 \
  -f merge_access_level=40

# Hotfix branches - maintainers only
glab api projects/$project_id/protected_branches --method POST \
  -f name="hotfix/*" \
  -f push_access_level=40 \
  -f merge_access_level=40

トラブルシューティング

問題原因解決策
403 ForbiddenMaintainer ではないMaintainer 以上のロールが必要です
404 Not Foundブランチが存在しない、または保護されていないブランチ名を確認してください
400 Bad Request無効なアクセスレベル0、30、40、60 のいずれかを使用してください
ブランチがまだ保護されているパターンに一致しているワイルドカードパターンを確認してください
保護ブランチにプッシュできないアクセスレベルが低すぎる保護設定を更新するか、より高いロールを取得してください

ベストプラクティス

  1. 必ず main を保護する:少なくともデフォルトブランチを保護してください
  2. ワイルドカードを適切に使う:個々のリリースではなく release/* を保護してください
  3. main へのフォースプッシュを避ける:allow_force_push=false を設定してください
  4. 保護設定を文書化する:ブランチ保護戦略を記録しておいてください
  5. 定期的に見直す:保護設定を定期的に監査してください

関連ドキュメント

コメント