Skip to content

Commit

Permalink
Merge pull request feast-dev#2 from farfetch-test/enpolat/67-configur…
Browse files Browse the repository at this point in the history
…e-github

Script to configure GitHub
  • Loading branch information
polatengin authored May 13, 2020
2 parents e9be37d + e477a81 commit 6ad3ecd
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions act/docker-build-push-action@v1
Submodule docker-build-push-action@v1 added at e5e36c
110 changes: 110 additions & 0 deletions infra/scripts/configure-github.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

# This script configures given GitHub Repo and Branch to be protected
# After configuration `branch` closed for commits
# Only way to update the `branch` is merging another branch via a PR
# Script also configures that the PR reviewed by at least 2 reviewers

# Stop on error
set -e

HELP="
$(basename "$0") [--token] [--repo repo_name] [--owner repo_owner] [--branch branch_name]
commands:
-h [--help] show this help text
-t [--token] Personal Access Token (PAT), you can generate a PAT on https://github.com/settings/tokens page
-r [--repo] name of the repo, format is (owner/repo) or just (repo)
-o [--owner] owner of the repo, if you set owner in [--repo] parameter, no need to set it here
-b [--branch] name of the branch to protect
"

while [[ "$#" -gt 0 ]]
do
case $1 in
-h | --help)
echo "$HELP"
exit 0
;;
-t | --token)
TOKEN=$2
;;
-r | --repo)
IFS='/'
read -ra TEMP <<< "$2"
IFS=' '

REPO_NAME=${TEMP[0]}
OWNER_NAME=${TEMP[1]}
;;
-o | --owner)
OWNER_NAME=$2
;;
-b | --branch)
BRANCH_NAME=$2
;;
esac
shift
done

if [ -z "$TOKEN" ]
then
echo "Get Personal Access Token (PAT) from your GitHub Account and paste it below"
echo "You can click https://github.com/settings/tokens and create a token"
read -p "PAT : " TOKEN
fi

if [ -z "$REPO_NAME" ]
then
echo "Format : {owner}/{repo} or {repo}"
read -p "Name of the repo : " TEMP

IFS='/'
read -ra TEMP <<< "$TEMP"
IFS=' '

REPO_NAME=${TEMP[0]}
OWNER_NAME=${TEMP[1]}
fi

if [ -z "$OWNER_NAME" ]
then
read -p "Name of the owner : " OWNER_NAME
fi

if [ -z "$BRANCH_NAME" ]
then
read -p "Name of the branch : " BRANCH_NAME
fi

QUERY_PAYLOAD="{ \"query\": \"query getNodeIdOfRepo { repository(name: \\\"$REPO_NAME\\\", owner: \\\"$OWNER_NAME\\\") { id branchProtectionRules(first: 100) { edges { node { id databaseId pattern } } } } }\" }"

RESPONSE=$(curl -f -v --request POST --header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json" --data-raw "$QUERY_PAYLOAD" https://api.github.com/graphql)

HAS_DATA=$(jq -e 'has("data")' <<< "$RESPONSE")
HAS_MESSAGE=$(jq -e 'has("message")' <<< "$RESPONSE")

if $HAS_MESSAGE;
then
echo "Something went wrong, we couldn't get repo_id from '$OWNER_NAME/$REPO_NAME' please check console log..."

exit 1;
fi

REPO_ID=$(jq -r '.data.repository.id' <<< "$RESPONSE")

MUTATION_PAYLOAD="{ \"query\": \"mutation setBranchProtectionRule { createBranchProtectionRule(input: {repositoryId: \\\"$REPO_ID\\\", pattern: \\\"$BRANCH_NAME\\\", requiredApprovingReviewCount: 2, requiresApprovingReviews: true}) { branchProtectionRule { id } } }\" }"

RESPONSE=$(curl -f -v --request POST --header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json" --data-raw "$MUTATION_PAYLOAD" https://api.github.com/graphql)

HAS_DATA=$(jq -e 'has("data")' <<< "$RESPONSE")
HAS_MESSAGE=$(jq -e 'has("message")' <<< "$RESPONSE")

if $HAS_MESSAGE;
then
echo "Something went wrong, branch policy couldn't be set on '$OWNER_NAME/$REPO_NAME' please check console log..."

exit 1;
fi

echo "Completed successfully."

0 comments on commit 6ad3ecd

Please sign in to comment.