Bitbucket Cloud のブランチ制限
ブランチ制限は、リポジトリの Settings → Branch restrictions(Workflow の下)にあります。1つずつがブランチ名か release/* のようなパターンに付き、誰がそこへ書き込めるか、誰がプルリクエストでマージできるか、履歴の書き換えやブランチの削除を許すか、そのプルリクエストにどのマージ チェックが要るかを決めます(Use branch permissions)。以下は、Atlassian のナレッジ ベースがブランチ制限について答えている問題です。
グループや人が一覧に出てこない
- グループ:足せるのはリポジトリへのアクセスを持つグループだけで、少なくとも Write が要ります。リポジトリの Settings → Repository permissions を開き、Add users or groups で足します(A user group is not appearing in branch restriction search results)。新しいリポジトリに最初からそのグループを入れておくには、グループを作るときにワークスペースのレベルでアクセスレベルを与えるか、プロジェクトに足します(User group not available on Branch Restrictions)。
- 名前にアクセント記号やウムラウトのある人:その文字のまま入力します。「Zoe」で探しても「Zoë」は見つかりません(Adding User Names with Acute Accents or Umlauts to Branch Restrictions)。
マージの前にプルリクエストごとのマージ チェックを見るなら:JetBrains IDE のプラグイン Pull Request Inbox for Bitbucket は、あなたのレビュー待ちのプルリクエストを並べ、どのチェックが通っていて、どれがマージを止めているかを示します。
保存すると「Internal server error」
ブランチが 500 本以上あるようなリポジトリでは、ブランチ制限の保存が時間切れになり、500 のエラーを返すことがあります。ナレッジ ベースの直し方はブランチを減らすことで、リポジトリを 200 本ほどまでに保つよう勧めています(Issue while updating branch restrictions)。消しやすいのはマージ済みのブランチです。
新しいブランチにはパターンで
新しいブランチは、作ったもとのブランチの制限を引き継ぎません。付くのは、名前がパターンに合う制限です。release/* に制限を付けておけば、リリースのブランチはできた時からすべてそれを受けます。そうでなければ、ブランチができてから API で制限を作ります。たとえば、どのブランチでも動くパイプラインのステップからです(Inherit branch restrictions for newly created branch from existing branch)。
パターンの * は0文字以上に合い、ほかの文字はその文字そのものに合います。foo* は foo と foobar に合い、barfoo には合いません。* はすべてのブランチに合います。制限はパターンの代わりに、リポジトリのブランチ モデルの種類(production・development・bugfix・release・feature・hotfix)で当てることもできます(REST API のリファレンスの Branch restrictions)。
一部のブランチ名だけを許す
決まった名前のブランチだけを作れるようにするには、* で書き込みとプルリクエストでのマージを取り上げ、許す名前でそれを戻します。ナレッジ ベースの例は、main と test で始まるブランチを許しています(Restrict Creation of Branches to Certain Patterns):
| パターン | 書き込みとプルリクエストでのマージ |
|---|---|
* | 誰もできない |
main | そこへ push してよい人やグループ |
test* | test のブランチを作ってよい人やグループ |
名前と * の両方に合うブランチには、両方の制限が付きます。同じ記事が、その結果を示しています:
* の側 | main の側 | main にアクセスできる人 |
|---|---|---|
| ユーザーもグループも無し | ユーザー A | ユーザー A だけ |
| 全員 | ユーザー A | ユーザー A だけ |
| ユーザー A | ユーザー B | ユーザー A とユーザー B |
| ユーザー A | ユーザーもグループも無し | ユーザー A だけ |
| ユーザー A | 全員 | ユーザー A だけ |
API で
POST /2.0/repositories/{workspace}/{repository}/branch-restrictions で作り、API トークンには admin:repository:bitbucket が要ります。1つずつに kind と pattern があります。users(ワークスペースのメンバーの一覧にある UUID)と groups を取るのは push と restrict_merges だけで、そこに入れた人とグループが通され、空の一覧なら全員が止まります。あとから人を足すには、その ID に PUT します(REST API のリファレンス・How to create/edit branch restrictions in Bitbucket Cloud repositories via API)。たとえば、リリースのブランチへの直接の push を止める:
curl --request POST --user 'you@example.com:{api_token}' \
--header 'Content-Type: application/json' \
--data '{"pattern": "release/*", "kind": "push", "users": [], "groups": []}' \
https://api.bitbucket.org/2.0/repositories/{workspace}/{repository}/branch-restrictions
| 設定の画面 | kind |
|---|---|
| Write access | push |
| Merge via pull requests | restrict_merges |
| Minimum number of approvals | require_approvals_to_merge(value つき) |
| Minimum number of approvals from default reviewers | require_default_reviewer_approvals_to_merge(value つき) |
| Minimum number of successful builds for the last commit with no failed builds and no in progress builds | require_passing_builds_to_merge(value つき) |
| Maximum number of commits behind destination branch | require_commits_behind(value つき) |
| No unresolved pull request tasks | require_tasks_to_be_completed |
| No changes are requested | require_no_changes_requested |
| Reset approvals when the source branch is modified | reset_pullrequest_approvals_on_change |
| Keep approvals if there is no change to the diff in the pull request | smart_reset_pullrequest_approvals |
| Reset requested changes when the source branch is modified | reset_pullrequest_changes_requested_on_change |
| Prevent a merge with unresolved merge checks | enforce_merge_checks |
| Allow automatic merge when builds pass | allow_auto_merge_when_builds_pass |
ほかに force と delete があり、Allow rewriting branch history と Allow deleting this branch に当たります。
IntelliJ IDEA で Bitbucket のプルリクエスト
Pull Request Inbox for Bitbucket は、JetBrains IDE 2024.1 以降で動く Lesstep のプラグインです。あなたのレビュー待ちのプルリクエストを差分と議論つきで IDE のツール ウィンドウに並べ、それぞれの横に、どのマージ チェックが通っていてどれがマージを止めているかを示します。レビューの受信箱・差分・議論・マージ チェックは無料、承認・コメント・マージは Pro(30日の無料試用つき)。Bitbucket Data Center(ベータ)でも動きます。
Bitbucket と Atlassian は Atlassian の商標、IntelliJ IDEA は JetBrains s.r.o. の商標です。このページはそのいずれとも提携しておらず、承認も受けていません。
ほかに:Bitbucket のマージチェック · 「pre-receive hook declined」 · 消したブランチを戻す · ブランチから変更が消えた · English