diff --git a/github-management-script/branch-protection-ruleset.sh b/github-management-script/branch-protection-ruleset.sh old mode 100644 new mode 100755 index 4a9d635..98a238f --- a/github-management-script/branch-protection-ruleset.sh +++ b/github-management-script/branch-protection-ruleset.sh @@ -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 @@ -22,13 +35,10 @@ 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 @@ -36,7 +46,7 @@ set_branch_protection() { 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}" @@ -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}") diff --git a/github-management-script/codeowners-file-creation.sh b/github-management-script/codeowners-file-creation.sh old mode 100644 new mode 100755 index 233d787..d084c0b --- a/github-management-script/codeowners-file-creation.sh +++ b/github-management-script/codeowners-file-creation.sh @@ -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 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