-
Notifications
You must be signed in to change notification settings - Fork 5
Add script to require signed commits on protected branches #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
razo7
wants to merge
5
commits into
medik8s:main
Choose a base branch
from
razo7:require-signed-commits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a27902
Add script to manage signed commit requirements via GitHub API
razo7 d989e71
Improve error handling in require-signed-commits.sh
razo7 b714ccc
Fix set -e interaction with exit code capture
razo7 3d7e2ac
Harden script against injection, accidental disable, and silent failures
razo7 390abe4
Replace static YAML config with dynamic repo/branch discovery
razo7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| ORG="medik8s" | ||
| DRY_RUN=false | ||
| ACTION="enable" | ||
| CONFIRM=false | ||
| REPOS=() | ||
| BRANCHES=() | ||
| DEFAULT_BRANCH_PATTERN='main|release-.+' | ||
|
|
||
| log() { echo "==> $*"; } | ||
| info() { echo " $(date -u +%H:%M:%SZ) $*"; } | ||
| warn() { echo "WARNING: $(date -u +%H:%M:%SZ) $*" >&2; } | ||
| die() { echo "ERROR: $*" >&2; exit 1; } | ||
|
|
||
| valid_name() { [[ "$1" =~ ^[a-zA-Z0-9._-]+$ ]]; } | ||
|
|
||
| while [[ $# -gt 0 ]]; do | ||
| case "$1" in | ||
| --dry-run) DRY_RUN=true; shift ;; | ||
| --disable) ACTION="disable"; shift ;; | ||
| --confirm) CONFIRM=true; shift ;; | ||
| --org) | ||
| [[ -n "${2:-}" ]] || die "--org requires a value" | ||
| ORG="$2"; shift 2 ;; | ||
| --repo) | ||
| [[ -n "${2:-}" ]] || die "--repo requires a value" | ||
| REPOS+=("$2"); shift 2 ;; | ||
| --branch) | ||
| [[ -n "${2:-}" ]] || die "--branch requires a value" | ||
| BRANCHES+=("$2"); shift 2 ;; | ||
| --help|-h) | ||
| cat <<'EOF' | ||
| Usage: require-signed-commits.sh [OPTIONS] | ||
|
|
||
| Enable or disable required signed commits on protected branches. | ||
|
|
||
| By default, discovers all non-archived, non-fork repos in the org and | ||
| targets branches matching: main, release-* | ||
|
|
||
| Options: | ||
| --dry-run Show what would be done without making changes | ||
| --disable Remove the requirement instead of adding it | ||
| --confirm Required with --disable to prevent accidental removal | ||
| --org ORG GitHub organization (default: medik8s) | ||
| --repo REPO Target specific repo(s) — repeatable | ||
| --branch PATTERN Override branch filter — repeatable, regex | ||
| (default: main and release-.+) | ||
|
|
||
| Examples: | ||
| require-signed-commits.sh --dry-run | ||
| require-signed-commits.sh --repo self-node-remediation --dry-run | ||
| require-signed-commits.sh --repo nhc --repo snr --branch main --dry-run | ||
| require-signed-commits.sh --disable --confirm | ||
| EOF | ||
| exit 0 | ||
| ;; | ||
| *) die "Unknown option: $1" ;; | ||
| esac | ||
| done | ||
|
|
||
| if [[ "$ACTION" == "disable" && "$DRY_RUN" != "true" && "$CONFIRM" != "true" ]]; then | ||
| die "--disable requires --confirm (or use --dry-run to preview)" | ||
| fi | ||
|
|
||
| gh auth status &>/dev/null || die "Not authenticated with gh CLI. Run 'gh auth login' first." | ||
| valid_name "$ORG" || die "Invalid org name: $ORG" | ||
|
|
||
| # Build branch pattern from --branch flags or default | ||
| if [[ ${#BRANCHES[@]} -gt 0 ]]; then | ||
| branch_pattern=$(IFS='|'; echo "${BRANCHES[*]}") | ||
| else | ||
| branch_pattern="$DEFAULT_BRANCH_PATTERN" | ||
| fi | ||
|
|
||
| # Validate regex before use (grep exit 1 = no match, exit 2 = bad regex) | ||
| grep_exit=0 | ||
| grep -E "^(${branch_pattern})$" /dev/null >/dev/null 2>&1 || grep_exit=$? | ||
| [[ $grep_exit -le 1 ]] || die "Invalid branch regex: ${branch_pattern}" | ||
|
|
||
| # Discover repos: --repo flags or all non-archived, non-fork repos in the org | ||
| if [[ ${#REPOS[@]} -eq 0 ]]; then | ||
| log "Discovering repos in ${ORG} (non-archived, non-fork)..." | ||
| while IFS= read -r r; do | ||
| [[ -n "$r" ]] && REPOS+=("$r") | ||
| done < <(gh api "orgs/${ORG}/repos" --paginate \ | ||
| --jq '.[] | select(.archived == false and .fork == false) | .name') | ||
| [[ ${#REPOS[@]} -gt 0 ]] || die "No repos found in org ${ORG}" | ||
| log "Found ${#REPOS[@]} repos" | ||
| fi | ||
|
|
||
| changed=0 | ||
| skipped=0 | ||
| failed=0 | ||
|
|
||
| for repo in "${REPOS[@]}"; do | ||
| valid_name "$repo" || { warn "Skipping invalid repo name: $repo"; ((failed++)) || true; continue; } | ||
| log "$repo" | ||
|
|
||
| branches_exit=0 | ||
| branches_response=$(gh api "repos/${ORG}/${repo}/branches" --paginate --jq '.[].name' 2>&1) || branches_exit=$? | ||
| if [[ $branches_exit -ne 0 ]]; then | ||
| warn "Failed to list branches for ${ORG}/${repo}: ${branches_response}" | ||
| ((failed++)) || true | ||
| continue | ||
| fi | ||
|
|
||
| matched_branches=() | ||
| while IFS= read -r b; do | ||
| [[ -n "$b" ]] && matched_branches+=("$b") | ||
| done < <(printf '%s\n' "$branches_response" \ | ||
| | grep -E "^(${branch_pattern})$" || true) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| if [[ ${#matched_branches[@]} -eq 0 ]]; then | ||
| info "(no branches matching: ${branch_pattern})" | ||
| continue | ||
| fi | ||
|
|
||
| for branch in "${matched_branches[@]}"; do | ||
| valid_name "$branch" || { warn "Skipping invalid branch name: $branch"; ((failed++)) || true; continue; } | ||
|
|
||
| api_exit=0 | ||
| api_response=$(gh api "repos/${ORG}/${repo}/branches/${branch}/protection/required_signatures" 2>&1) || api_exit=$? | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if [[ $api_exit -eq 0 ]]; then | ||
| current=$(echo "$api_response" | grep -o '"enabled":\s*[a-z]*' | grep -o 'true\|false' || echo "unknown") | ||
| elif echo "$api_response" | grep -q "Branch not protected\|Not Found"; then | ||
| current="no-protection" | ||
| else | ||
| warn "$branch — API error: $api_response" | ||
| ((failed++)) || true | ||
| continue | ||
| fi | ||
|
|
||
| if [[ "$ACTION" == "disable" ]]; then | ||
| if [[ "$current" == "false" || "$current" == "no-protection" ]]; then | ||
| info "$branch — already disabled" | ||
| ((skipped++)) || true | ||
| continue | ||
| fi | ||
| if [[ "$DRY_RUN" == "true" ]]; then | ||
| info "[dry-run] $branch — would DISABLE signed commits" | ||
| continue | ||
| fi | ||
| if api_err=$(gh api "repos/${ORG}/${repo}/branches/${branch}/protection/required_signatures" --method DELETE 2>&1); then | ||
| info "$branch — disabled" | ||
| ((changed++)) || true | ||
| else | ||
| warn "$repo/$branch — failed to disable: $api_err" | ||
| ((failed++)) || true | ||
| fi | ||
| else | ||
| if [[ "$current" == "true" ]]; then | ||
| info "$branch — already enabled" | ||
| ((skipped++)) || true | ||
| continue | ||
| fi | ||
| if [[ "$current" == "no-protection" ]]; then | ||
| warn "$branch — no branch protection rule yet (waiting for branchprotector?)" | ||
| ((skipped++)) || true | ||
| continue | ||
| fi | ||
| if [[ "$DRY_RUN" == "true" ]]; then | ||
| info "[dry-run] $branch — would ENABLE signed commits" | ||
| continue | ||
| fi | ||
| if api_err=$(gh api "repos/${ORG}/${repo}/branches/${branch}/protection/required_signatures" --method POST 2>&1); then | ||
| info "$branch — enabled" | ||
| ((changed++)) || true | ||
| else | ||
| warn "$repo/$branch — failed to enable: $api_err" | ||
| ((failed++)) || true | ||
| fi | ||
| fi | ||
| done | ||
| done | ||
|
|
||
| log "Done: changed=${changed} skipped=${skipped} failed=${failed}" | ||
| [[ "$failed" -eq 0 ]] || exit 1 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.