Skip to content

Instantly share code, notes, and snippets.

@devilelephant
Last active July 5, 2023 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilelephant/e94f9246899423e35bc98bcb959ef73f to your computer and use it in GitHub Desktop.
Save devilelephant/e94f9246899423e35bc98bcb959ef73f to your computer and use it in GitHub Desktop.
Protect Branches With GH

Script Branch Protection for Github

Display current protection rules for "main"

gh api repos/:owner/:repo/branches/main/protection

Add branch protection

Dump branch protection configuration to temp file for next step

See https://docs.github.com/en/rest/reference/repos#update-branch-protection

echo '{
  "required_status_checks": {
    "strict": true,
    "contexts": ["contexts"]
  },
  "enforce_admins": true,
  "required_pull_request_reviews": {
    "dismissal_restrictions": {
      "users": ["users"],
      "teams": ["teams"]
    },
    "dismiss_stale_reviews": false,
    "require_code_owner_reviews": false,
    "required_approving_review_count": 1
  },
  "restrictions": {
    "users": ["users"],
    "teams": ["teams"],
    "apps": ["apps"]
  }
}' >/tmp/config-branch-rules

Applies the branch protection specified above to main branch

gh api --method PUT repos/:owner/:repo/branches/main/protection --input /tmp/config-branch-rules >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment