Skip to content

Commit

Permalink
Merge pull request #37 from ai-cfia/112-automatically-assign-product-…
Browse files Browse the repository at this point in the history
…teams-for-pull-request-review

Issue #112: Update codeowner script with product teams
  • Loading branch information
SonOfLope authored May 17, 2024
2 parents 746eff2 + 0430ef5 commit 1ad2c93
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
8 changes: 4 additions & 4 deletions github-management-script/add-team-to-repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ that an administrative team is added to each repository.
- **Automated Team Assignment:** Automatically adds a specified team to all
repositories within a given GitHub organization.
- **Dynamic Team Assignment:** The script assigns teams (`backend`, `frontend`,
`db`) dynamically based on the naming convention of the repositories.
`data`, `finesse`, `nachet`, `harvester`) dynamically based on the naming
convention of the repositories.
- **Admin Team Enforcement:** Ensures that an administrative team is added to
every repository, regardless of its naming convention.

Expand All @@ -27,9 +28,8 @@ that an administrative team is added to each repository.

2. **Configuration:**
- Open the script in your text editor.
- Fill in the `GITHUB_ORG`, `TEAM_PERMISSION` and
`ADMIN_TEAM_SLUG` variables with the appropriate values for your GitHub
organization and teams.
- Fill in the `GITHUB_ORG`, `TEAM_PERMISSION` and `ADMIN_TEAM_SLUG` variables
with the appropriate values for your GitHub organization and teams.

3. **Run the Script:**
- Open your terminal and navigate to the directory containing the script.
Expand Down
55 changes: 41 additions & 14 deletions github-management-script/add-team-to-repos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ add_team_to_repo() {
local org=$2
local repo=$3
local permission=$4
local owner=$5

curl -s -X PUT -H "Authorization: token ${GITHUB_TOKEN}" \
"https://api.github.com/orgs/${org}/teams/${team_slug}/repos/${owner}/${repo}" \
curl -L \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/orgs/${org}/teams/${team_slug}/repos/${repo}" \
-d "{\"permission\":\"${permission}\"}"
}

Expand All @@ -40,21 +42,46 @@ while :; do
TEAM_SLUG='backend'
elif [[ "${REPO}" == *frontend* ]]; then
TEAM_SLUG='frontend'
elif [[ "${REPO}" == *db* ]]; then
TEAM_SLUG='db'
else
TEAM_SLUG='devops'
fi

echo "Adding team \"${TEAM_SLUG}\" to repo \"${REPO}\" with permission \"${TEAM_PERMISSION}\""
add_team_to_repo "${TEAM_SLUG}" "${ORG_NAME}" "${REPO}" "${TEAM_PERMISSION}"
if [[ "${REPO}" == *db* ]] || [[ "${REPO}" == *data* ]]; then
TEAM_SLUG='data'
fi

if [[ "${REPO}" == *nachet* ]]; then
PRODUCT_SLUG='nachet'
elif [[ "${REPO}" == *finesse* ]]; then
PRODUCT_SLUG='finesse'
elif [[ "${REPO}" == *harvester* ]]; then
PRODUCT_SLUG='harvester'
fi

if [[ "${TEAM_SLUG}" != '' ]]; then
echo "Adding team \"${TEAM_SLUG}\" to repo \"${REPO}\" with permission \"${TEAM_PERMISSION}\""
add_team_to_repo "${TEAM_SLUG}" "${ORG_NAME}" "${REPO}" "${TEAM_PERMISSION}"

if [[ "${TEAM_SLUG}" != "${ADMIN_TEAM_SLUG}" ]]; then
echo "Adding team \"${ADMIN_TEAM_SLUG}\" to repo \"${REPO}\" with permission \"${TEAM_PERMISSION}\""
add_team_to_repo "${ADMIN_TEAM_SLUG}" "${ORG_NAME}" "${REPO}" "${TEAM_PERMISSION}"
if [[ "${TEAM_SLUG}" != "${ADMIN_TEAM_SLUG}" ]]; then
echo "Adding team \"${ADMIN_TEAM_SLUG}\" to repo \"${REPO}\" with permission \"${TEAM_PERMISSION}\""
add_team_to_repo "${ADMIN_TEAM_SLUG}" "${ORG_NAME}" "${REPO}" "${TEAM_PERMISSION}"
else
echo "... Skipped adding team \"${ADMIN_TEAM_SLUG}\" as it is the same as \"${TEAM_SLUG}\""
fi
else
echo "... Skipped adding team \"${ADMIN_TEAM_SLUG}\" as it is the same as \"${TEAM_SLUG}\""
echo "Skipping.. No team found for repo \"${REPO}\""
fi

if [[ "${PRODUCT_SLUG}" != '' ]]; then
echo "Adding product team \"${PRODUCT_SLUG}\" to repo \"${REPO}\" with permission \"${TEAM_PERMISSION}\""
add_team_to_repo "${PRODUCT_SLUG}" "${ORG_NAME}" "${REPO}" "${TEAM_PERMISSION}"
else
echo "Skipping.. No product team found for repo \"${REPO}\""
fi

echo "Adding team devops to repo \"${REPO}\" with permission \"${TEAM_PERMISSION}\""
add_team_to_repo "devops" "${ORG_NAME}" "${REPO}" "${TEAM_PERMISSION}"

TEAM_SLUG=''
PRODUCT_SLUG=''
done

((PAGE++))
Expand Down
3 changes: 2 additions & 1 deletion github-management-script/codeowners-file-creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ the CFIA organization and applies tag rules based on repository names.
* **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.
`data`, `devops`, `finesse`, `harvester`, `nachet`) 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.
Expand Down
14 changes: 12 additions & 2 deletions github-management-script/codeowners-file-creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ generate_codeowners() {
content+="* @ai-cfia/backend\n"
elif [[ ${repo_name} == *"frontend"* ]]; then
content+="* @ai-cfia/frontend\n"
elif [[ ${repo_name} == *"db"* ]]; then
fi

if [[ ${repo_name} == *"db"* ]]; then
content+="* @ai-cfia/data\n"
fi

if [[ "${repo_name}" == *nachet* ]]; then
content+="* @ai-cfia/nachet\n"
elif [[ "${repo_name}" == *finesse* ]]; then
content+="* @ai-cfia/finesse\n"
elif [[ "${repo_name}" == *harvester* ]]; then
content+="* @ai-cfia/harvester\n"
fi

content+="/.github/ @ai-cfia/devops\n"
content+="Dockerfile @ai-cfia/devops\n"
content+="docker-compose.yml @ai-cfia/devops\n"
Expand Down Expand Up @@ -43,7 +53,7 @@ create_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}\"}"
json_data="{\"message\": \"Update CODEOWNERS file\", \"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}\"}"
Expand Down

0 comments on commit 1ad2c93

Please sign in to comment.