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

Adding codeowners file creation script #32

Merged
merged 8 commits into from
Mar 4, 2024
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
1 change: 1 addition & 0 deletions github-management-script/branch-protection-ruleset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set_branch_protection() {
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1
"require_code_owner_reviews": true
},
"restrictions": null
}'
Expand Down
28 changes: 28 additions & 0 deletions github-management-script/codeowners-file-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# CODEOWNERS Automation Script

This script automates the creation of CODEOWNERS files within repositories of
the CFIA organization and applies tag rules based on repository names.

## Functionality

* **Creates CODEOWNERS Files:** The script generates CODEOWNERS files in target
repositories, defining code ownership rules to streamline the review process.
* **Customizable Team Tagging:** It tags relevant teams (`backend`, `frontend`,
`data`, `devops`) based on the repository name.
* **DevOps Ownership:** The script assigns specific ownership to the DevOps team
for files within the `.github` directory, Dockerfile, and docker-compose
configurations.

## Requirements

* **GitHub Personal Access Token (PAT):** A PAT with the `repo` scope.

## Usage

1. **Set Environment Variables:**
- `GITHUB_TOKEN`: Store your GitHub PAT in this environment variable.
- `ORG_NAME`: Set this to the name of your target GitHub organization.
2. **Execute the Script:** Run the script. It will:
- Prompt for your GitHub token (if not set).
- Retrieve a list of repositories within the organization.
- Process each repository, generating and adding the CODEOWNERS file.
54 changes: 54 additions & 0 deletions github-management-script/codeowners-file-creation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
generate_codeowners() {
repo_name=$1

echo "# This CODEOWNERS file is auto-generated. See the script for modification details." > .github/CODEOWNERS

# 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
fi

# Specific rules for DevOps ownership
echo "/.github/ @ai-cfia/devops" >> .github/CODEOWNERS
echo "Dockerfile @ai-cfia/devops" >> .github/CODEOWNERS
echo "docker-compose.yml @ai-cfia/devops" >> .github/CODEOWNERS
echo "docker-compose.*.yml @ai-cfia/devops" >> .github/CODEOWNERS
}

create_codeowners() {
org_name=$1
repo_name=$2
codeowners_content=$(generate_codeowners $repo_name)

encoded_content=$(echo "$codeowners_content" | base64 -w 0)

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 "Authorization: Bearer ${GITHUB_TOKEN}" \
-d "{\"message\": \"Add CODEOWNERS file\", \"content\": \"${encoded_content}\"}" \
"${API_URL}"
}

echo "Please enter your GitHub token:"
read GITHUB_TOKEN

ORG_NAME="ai-cfia"

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

for REPO in ${REPOS}; do
echo "Processing repository: ${REPO}"

create_codeowners $(dirname $REPO) $(basename $REPO)

done
rngadam marked this conversation as resolved.
Show resolved Hide resolved