Skip to content

Commit

Permalink
Merge pull request #34 from ai-cfia/33-as-a-devops-i-would-like-to-be…
Browse files Browse the repository at this point in the history
…-able-to-use-the-script-to-automate-branch-protection-ruleset

Issue #33: Hotfix for branch protection ruleset script
  • Loading branch information
SonOfLope authored Mar 6, 2024
2 parents e832e4f + f9646d4 commit fe0b55d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 45 deletions.
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
92 changes: 61 additions & 31 deletions github-management-script/codeowners-file-creation.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,55 +1,85 @@
#!/bin/bash
generate_codeowners() {
repo_name=$1
local repo_name=$1
local content=""

echo "# This CODEOWNERS file is auto-generated. See the script for modification details." > .github/CODEOWNERS
content+="# This CODEOWNERS file is auto-generated. See the script at <https://github.com/ai-cfia/devops/blob/main/github-management-script/codeowners-file-creation.sh> for modification details.\n\n"

# Default rules for AI-CFIA ownership for repositories which name ends with "backend", "frontend" or "db"
if [[ ${repo_name} == *"backend" ]]; then
echo "* @ai-cfia/backend" >> .github/CODEOWNERS
elif [[ ${repo_name} == *"frontend" ]]; then
echo "* @ai-cfia/frontend" >> .github/CODEOWNERS
elif [[ ${repo_name} == *"db" ]]; then
echo "* @ai-cfia/data" >> .github/CODEOWNERS
if [[ ${repo_name} == *"backend"* ]]; then
content+="* @ai-cfia/backend\n"
elif [[ ${repo_name} == *"frontend"* ]]; then
content+="* @ai-cfia/frontend\n"
elif [[ ${repo_name} == *"db"* ]]; then
content+="* @ai-cfia/data\n"
fi

{
echo "/.github/ @ai-cfia/devops"
echo "Dockerfile @ai-cfia/devops"
echo "docker-compose.yml @ai-cfia/devops"
echo "docker-compose.*.yml @ai-cfia/devops"
} >> .github/CODEOWNERS
content+="/.github/ @ai-cfia/devops\n"
content+="Dockerfile @ai-cfia/devops\n"
content+="docker-compose.yml @ai-cfia/devops\n"
content+="docker-compose.*.yml @ai-cfia/devops\n"

printf "%b" "${content}"
}

create_codeowners() {
org_name=$1
repo_name=$2
codeowners_content=$(generate_codeowners "${repo_name}")
local org_name=$1
local repo_name=$2
local codeowners_content
codeowners_content="$(generate_codeowners "${repo_name}")"
codeowners_content+=$'\n'

local API_URL="https://api.github.com/repos/${org_name}/${repo_name}/contents/.github/CODEOWNERS"

# Extract the SHA from the response, if the file exists. Common requirement
# when updating an existing file in a repository.
local response
response=$(curl -s -H "Authorization: Bearer ${GITHUB_TOKEN}" "${API_URL}")
local sha
sha=$(echo "${response}" | jq -r '.sha // empty')

encoded_content=$(echo "${codeowners_content}" | base64 -w 0)
local encoded_content
encoded_content=$(printf "%b" "${codeowners_content}" | base64 -w 0)

API_URL="https://api.github.com/repos/${org_name}/${repo_name}/contents/.github/CODEOWNERS"
local json_data
if [[ -n "${sha}" ]]; then
# If the file exists, include the SHA in the request to update it
json_data="{\"message\": \"Update CODEOWNERS file with EOF line\", \"content\": \"${encoded_content}\", \"sha\": \"${sha}\"}"
else
# If the file doesn't exist, the SHA is not required
json_data="{\"message\": \"Add CODEOWNERS file\", \"content\": \"${encoded_content}\"}"
fi

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}"
-d "${json_data}" \
"${API_URL}"
}

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}"

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

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

0 comments on commit fe0b55d

Please sign in to comment.