0
0

ブランチプロテクションの設定をcurlで変更する方法

Last updated at Posted at 2024-09-05

目的

テストブランチをブランチプロテクションで保護設定をして削除できないようにしていますが、少しの間だけブランチ削除を許容する必要がありました。そのためcurlでブランチプロテクションの強制削除を有効化にするための方法をメモしておきます

エンドポイント

ブランチプロテクションのREST APIエンドポイントが用意されているので、そちらを叩きます。
ブランチプロテクションのエンドポイントを叩くにはpersonal_access_tokenが必須です。

curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer ${PERSONAL_ACCESS_TOKEN}" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/${owner}/{repository}/branches/${branch}/protection

ブランチプロテクションの設定を変更

設定を変更するときはPUTで実行します。
注意事項としてリクエストボディは変更したい設定以外は、既存のブランチプロテクションの設定と同一になるように設定をいけません。仮にすべてtrueにしてしまうと、すべてのブランチプロテクションの設定が有効化になってしまうので慎重に設定を見比べながら実行してください

❯ curl -X PUT -L -H "Authorization: token $PERSONAL_ACCESS_TOKEN" -H "Accept: application/vnd.github+json" "https://api.github.com/repos/st-tech/${repository}/branches/develop-test/protection" -d '{
  "required_status_checks": {
    "strict": true,
    "contexts": []
  },
  "enforce_admins": false,
  "required_pull_request_reviews": {
    "dismiss_stale_reviews": false,
    "require_code_owner_reviews": false,
    "required_approving_review_count": 1
  },
  "restrictions": null,
  "required_linear_history": false,
  "allow_force_pushes": false,
  "allow_deletions": true,
  "block_creations": false,
  "required_conversation_resolution": false,
  "lock_branch": false,
  "allow_fork_syncing": false
}'

今回はallow_deletionsをtrueにしたいので、"allow_deletions": trueに設定します。

実行後にブランチプロテクションの設定を見に行くとAllow deletionsが有効化になっていました。
image.png

逆に無効化したい場合は、"allow_deletions": falseに設定して再度実行します。

curl -X PUT -L -H "Authorization: token $PERSONAL_ACCESS_TOKEN" -H "Accept: application/vnd.github+json" "https://api.github.com/repos/st-tech/${repository}/branches/develop-test/protection" -d '{
  "required_status_checks": {
    "strict": true,
    "contexts": []
  },
  "enforce_admins": false,
  "required_pull_request_reviews": {
    "dismiss_stale_reviews": false,
    "require_code_owner_reviews": false,
    "required_approving_review_count": 1
  },
  "restrictions": null,
  "required_linear_history": false,
  "allow_force_pushes": false,
  "allow_deletions": false,
  "block_creations": false,
  "required_conversation_resolution": false,
  "lock_branch": false,
  "allow_fork_syncing": false
}'

無効化になったことを確認しました。
image.png

終わりに

あまりブランチプロテクションを弄りたくはないですが、もし必要になった場合は参考にしていただけると幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0