Skip to content
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

Issue #33: Hotfix for branch protection ruleset script #34

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions github-management-script/branch-protection-ruleset.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#!/bin/bash

# prompt for GitHub token
echo "Please enter your GitHub token:"
read -r GITHUB_TOKEN

ORG_NAME="ai-cfia"
PAGE=1
PER_PAGE=100
REPOS=""

API_URL="https://api.github.com/orgs/${ORG_NAME}/repos?type=public"
while :; do
API_URL="https://api.github.com/orgs/${ORG_NAME}/repos?type=public&per_page=${PER_PAGE}&page=${PAGE}"

# get list of all public repos
RESPONSE=$(curl -s -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${API_URL}")
RESPONSE=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${API_URL}")

REPOS=$(echo "${RESPONSE}" | jq -r '.[].full_name')
CURRENT_PAGE_REPOS=$(echo "${RESPONSE}" | jq -r '.[].full_name')

if [[ -z "${CURRENT_PAGE_REPOS}" ]]; then
break
else
REPOS="${REPOS} ${CURRENT_PAGE_REPOS}"
((PAGE++))
fi
done

# Trim leading whitespace
REPOS=$(echo "${REPOS}" | xargs)

set_branch_protection() {
REPO_NAME=$1
Expand All @@ -22,21 +35,18 @@ set_branch_protection() {
API_URL="https://api.github.com/repos/${REPO_NAME}/branches/${BRANCH_NAME}/protection"

DATA='{
"required_status_checks": {
"strict": true,
"checks": ["lint-test / lint-test"]
},
"required_status_checks": null,
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1
"required_approving_review_count": 1,
"require_code_owner_reviews": true
},
"restrictions": null
}'

curl -L \
-X PUT \
-H "Accept: application/vnd.github.v3+json" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${API_URL}" \
-d "${DATA}"
Expand All @@ -45,7 +55,7 @@ set_branch_protection() {
# for each repo, check if .github/workflows exists
for REPO in ${REPOS}; do
WORKFLOWS_URL="https://api.github.com/repos/${REPO}/contents/.github/workflows"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Accept: application/vnd.github.v3+json" \
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${WORKFLOWS_URL}")

Expand Down
29 changes: 20 additions & 9 deletions github-management-script/codeowners-file-creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ create_codeowners() {
API_URL="https://api.github.com/repos/${org_name}/${repo_name}/contents/.github/CODEOWNERS"

curl -s -X PUT \
-H "Accept: application/vnd.github.v3+json" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-d "{\"message\": \"Add CODEOWNERS file\", \"content\": \"${encoded_content}\"}" \
"${API_URL}"
Expand All @@ -41,15 +41,26 @@ echo "Please enter your GitHub token:"
read -r GITHUB_TOKEN

ORG_NAME="ai-cfia"
API_URL="https://api.github.com/orgs/${ORG_NAME}/repos?type=public"
RESPONSE=$(curl -s -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${API_URL}")
REPOS=$(echo "${RESPONSE}" | jq -r '.[].full_name')
PAGE=1
PER_PAGE=100

for REPO in ${REPOS}; do
echo "Processing repository: ${REPO}"
while :; do
API_URL="https://api.github.com/orgs/${ORG_NAME}/repos?type=public&per_page=${PER_PAGE}&page=${PAGE}"

RESPONSE=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${API_URL}")

create_codeowners "$(dirname "${REPO}") $(basename "${REPO}")"
REPOS=$(echo "${RESPONSE}" | jq -r '.[].full_name')

if [[ -z "${REPOS}" ]]; then
break
fi

for REPO in ${REPOS}; do
echo "Processing repository: ${REPO}"
create_codeowners "${ORG_NAME}" "$(basename "${REPO}")"
done

((PAGE++))
done
Loading