Skip to content

Instantly share code, notes, and snippets.

@tpryan
Created April 6, 2020 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpryan/e7ece39cf237bf92442ba303d2b5ccd6 to your computer and use it in GitHub Desktop.
Save tpryan/e7ece39cf237bf92442ba303d2b5ccd6 to your computer and use it in GitHub Desktop.
Batch setting protection on a branch of a github repo.
#!/bin/bash
# Protect branches in a repository.
# (c) 2020 tpryan
# Author: github.com/tpryan
# MIT License, see below
# Adapted from https://gist.github.com/miraculixx/b4c30db06bb1f3b56057
function help {
echo "Add collaborators to one or more repositories on github"
echo ""
echo "Syntax: $0 -u user -p password [-b] [-o] -r repo1,repo2 "
echo ""
echo " -u User to access github"
echo " -p Password (optional, will be promoted otherwise)"
echo " -r repositories, list as repo,repo"
echo " -b branch to match for protection (optional, defaults to master)"
echo " -o owner of the repo (optional, defaults to username)"
}
while getopts "h?u:p:o:r:b:?" opt; do
case $opt in
h|\?)
help
exit 0
;;
u)
GH_USER=$OPTARG
;;
p)
PASSWORD=$OPTARG
;;
o)
OWNER=$OPTARG
;;
r)
REPOS=$OPTARG
;;
b)
BRANCH=$OPTARG
;;
esac
done
shift $((OPTIND-1))
COL_USER=$1
if [[ -z "$GH_USER" ]]; then
echo Enter your github user
read GH_USER
fi
if [[ -z "$PASSWORD" ]]; then
echo Enter Password
read -s PASSWORD
fi
if [[ -z "$OWNER" ]]; then
OWNER=$GH_USER
fi
if [[ -z "$BRANCH" ]]; then
BRANCH="master"
fi
if [[ -z "$REPOS" ]]; then
echo Enter the repositories. Multiple repos comma separated.
read REPOS
fi
METHOD=PUT
array=(${REPOS//,/ })
json="{\"required_pull_request_reviews\": {
\"dismiss_stale_reviews\": false,
\"require_code_owner_reviews\": false,
\"required_approving_review_count\": 1
},
\"enforce_admins\":null,
\"restrictions\":null,
\"required_status_checks\":null
}"
if [[ ! -z "$GH_USER" ]]; then
for repo in "${array[@]}"; do
echo repo: $repo
echo "[INFO] $OWNER/$repo $BRANCH protected"
curl -i -u "$GH_USER:$PASSWORD" -H 'Accept: application/vnd.github.luke-cage-preview+json' -X PUT https://api.github.com/repos/$OWNER/$repo/branches/$BRANCH/protection -d "$json" 2>&1 | grep message || echo "OK, done."
done
fi
exit 0
: <<< 'EOF'
The MIT License (MIT)
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment