forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request feast-dev#2 from farfetch-test/enpolat/67-configur…
…e-github Script to configure GitHub
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
Submodule docker-build-push-action@v1
added at
e5e36c
This file contains 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,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." |