|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script configures given GitHub Repo and Branch to be protected |
| 4 | +# After configuration `branch` closed for commits |
| 5 | +# Only way to update the `branch` is merging another branch via a PR |
| 6 | +# Script also configures that the PR reviewed by at least 2 reviewers |
| 7 | + |
| 8 | +# Stop on error |
| 9 | +set -e |
| 10 | + |
| 11 | +HELP=" |
| 12 | +$(basename "$0") [--token] [--repo repo_name] [--owner repo_owner] [--branch branch_name] |
| 13 | +
|
| 14 | +commands: |
| 15 | + -h [--help] show this help text |
| 16 | + -t [--token] Personal Access Token (PAT), you can generate a PAT on https://github.com/settings/tokens page |
| 17 | + -r [--repo] name of the repo, format is (owner/repo) or just (repo) |
| 18 | + -o [--owner] owner of the repo, if you set owner in [--repo] parameter, no need to set it here |
| 19 | + -b [--branch] name of the branch to protect |
| 20 | +" |
| 21 | + |
| 22 | +while [[ "$#" -gt 0 ]] |
| 23 | +do |
| 24 | + case $1 in |
| 25 | + -h | --help) |
| 26 | + echo "$HELP" |
| 27 | + exit 0 |
| 28 | + ;; |
| 29 | + -t | --token) |
| 30 | + TOKEN=$2 |
| 31 | + ;; |
| 32 | + -r | --repo) |
| 33 | + IFS='/' |
| 34 | + read -ra TEMP <<< "$2" |
| 35 | + IFS=' ' |
| 36 | + |
| 37 | + REPO_NAME=${TEMP[0]} |
| 38 | + OWNER_NAME=${TEMP[1]} |
| 39 | + ;; |
| 40 | + -o | --owner) |
| 41 | + OWNER_NAME=$2 |
| 42 | + ;; |
| 43 | + -b | --branch) |
| 44 | + BRANCH_NAME=$2 |
| 45 | + ;; |
| 46 | + esac |
| 47 | + shift |
| 48 | +done |
| 49 | + |
| 50 | +if [ -z "$TOKEN" ] |
| 51 | +then |
| 52 | + echo "Get Personal Access Token (PAT) from your GitHub Account and paste it below" |
| 53 | + echo "You can click https://github.com/settings/tokens and create a token" |
| 54 | + read -p "PAT : " TOKEN |
| 55 | +fi |
| 56 | + |
| 57 | +if [ -z "$REPO_NAME" ] |
| 58 | +then |
| 59 | + echo "Format : {owner}/{repo} or {repo}" |
| 60 | + read -p "Name of the repo : " TEMP |
| 61 | + |
| 62 | + IFS='/' |
| 63 | + read -ra TEMP <<< "$TEMP" |
| 64 | + IFS=' ' |
| 65 | + |
| 66 | + REPO_NAME=${TEMP[0]} |
| 67 | + OWNER_NAME=${TEMP[1]} |
| 68 | +fi |
| 69 | + |
| 70 | +if [ -z "$OWNER_NAME" ] |
| 71 | +then |
| 72 | + read -p "Name of the owner : " OWNER_NAME |
| 73 | +fi |
| 74 | + |
| 75 | +if [ -z "$BRANCH_NAME" ] |
| 76 | +then |
| 77 | + read -p "Name of the branch : " BRANCH_NAME |
| 78 | +fi |
| 79 | + |
| 80 | +QUERY_PAYLOAD="{ \"query\": \"query getNodeIdOfRepo { repository(name: \\\"$REPO_NAME\\\", owner: \\\"$OWNER_NAME\\\") { id branchProtectionRules(first: 100) { edges { node { id databaseId pattern } } } } }\" }" |
| 81 | + |
| 82 | +RESPONSE=$(curl -f -v --request POST --header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json" --data-raw "$QUERY_PAYLOAD" https://api.github.com/graphql) |
| 83 | + |
| 84 | +HAS_DATA=$(jq -e 'has("data")' <<< "$RESPONSE") |
| 85 | +HAS_MESSAGE=$(jq -e 'has("message")' <<< "$RESPONSE") |
| 86 | + |
| 87 | +if $HAS_MESSAGE; |
| 88 | +then |
| 89 | + echo "Something went wrong, we couldn't get repo_id from '$OWNER_NAME/$REPO_NAME' please check console log..." |
| 90 | + |
| 91 | + exit 1; |
| 92 | +fi |
| 93 | + |
| 94 | +REPO_ID=$(jq -r '.data.repository.id' <<< "$RESPONSE") |
| 95 | + |
| 96 | +MUTATION_PAYLOAD="{ \"query\": \"mutation setBranchProtectionRule { createBranchProtectionRule(input: {repositoryId: \\\"$REPO_ID\\\", pattern: \\\"$BRANCH_NAME\\\", requiredApprovingReviewCount: 2, requiresApprovingReviews: true}) { branchProtectionRule { id } } }\" }" |
| 97 | + |
| 98 | +RESPONSE=$(curl -f -v --request POST --header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json" --data-raw "$MUTATION_PAYLOAD" https://api.github.com/graphql) |
| 99 | + |
| 100 | +HAS_DATA=$(jq -e 'has("data")' <<< "$RESPONSE") |
| 101 | +HAS_MESSAGE=$(jq -e 'has("message")' <<< "$RESPONSE") |
| 102 | + |
| 103 | +if $HAS_MESSAGE; |
| 104 | +then |
| 105 | + echo "Something went wrong, branch policy couldn't be set on '$OWNER_NAME/$REPO_NAME' please check console log..." |
| 106 | + |
| 107 | + exit 1; |
| 108 | +fi |
| 109 | + |
| 110 | +echo "Completed successfully." |
0 commit comments