Skip to content

Commit 307cb98

Browse files
committed
feat: separate repo settings from rulesets
1 parent 5f9bbed commit 307cb98

File tree

2 files changed

+131
-90
lines changed

2 files changed

+131
-90
lines changed

.github/workflows/admin_update_repo_settings.yml

+41-7
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,53 @@ env:
1313
GH_TOKEN: ${{ secrets.GH_TOKEN }}
1414

1515
jobs:
16-
admin-update-repo-settings:
17-
name: 'GitHub: Update Repo Settings'
16+
admin-update-general-repo-settings:
17+
name: 'GitHub: Update General Repository Settings'
1818
runs-on: ubuntu-latest
1919
steps:
2020
- name: Checkout repository
2121
uses: actions/checkout@v4
2222
with:
2323
ref: ${{ github.ref_name }}
2424

25-
- name: Update Repository Settings
25+
- name: Update General Repository Settings
2626
run: |
27-
bash ./admin/update_repo_settings.sh \
28-
-o ${{ github.repository_owner }} \
29-
-e ${{ github.ref_name }}
27+
source ./admin/update_repo_settings.sh
28+
29+
update_repo_general_settings ${{ github.repository_owner }}
30+
31+
echo "::notice::General Settings Updated"
32+
33+
admin-update-tf-module-rulesets:
34+
name: 'GitHub: Update tf-module Rulesets'
35+
runs-on: ubuntu-latest
36+
steps:
37+
- name: Checkout repository
38+
uses: actions/checkout@v4
39+
with:
40+
ref: ${{ github.ref_name }}
41+
42+
- name: Update tf-module Rulesets
43+
run: |
44+
source ./admin/update_repo_settings.sh
45+
46+
update_tf_module_rulesets ${{ github.repository_owner }} ${{ github.ref_name }}
47+
48+
echo "::notice::tf-module ${{ github.ref_name }} Rulesets Updated"
49+
50+
admin-update-tf-iac-rulesets:
51+
name: 'GitHub: Update tf-iac Rulesets'
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Checkout repository
55+
uses: actions/checkout@v4
56+
with:
57+
ref: ${{ github.ref_name }}
58+
59+
- name: Update tf-iac Rulesets
60+
run: |
61+
source ./admin/update_repo_settings.sh
62+
63+
update_tf_iac_rulesets ${{ github.repository_owner }} ${{ github.ref_name }}
3064
31-
echo "::notice::Release Published"
65+
echo "::notice::tf-iac ${{ github.ref_name }} Rulesets Updated"

admin/update_repo_settings.sh

+90-83
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
#!/bin/bash
22

3-
set -e
4-
5-
REPOSITORY_OWNER="code-kern-ai"
6-
REPOSITORY_NAME=""
7-
DEV_ADMIN_GITHUB_TEAM_ID=10188509
8-
DEVOPS_ADMIN_GITHUB_TEAM_ID=10188507
9-
10-
ENVIRONMENT_NAME="dev"
11-
12-
while getopts o:e: flag
13-
do
14-
case "${flag}" in
15-
o) REPOSITORY_OWNER=${OPTARG};;
16-
e) ENVIRONMENT_NAME=${OPTARG};;
17-
esac
18-
done
3+
export DEV_ADMIN_GITHUB_TEAM_ID=10188509
4+
export DEVOPS_ADMIN_GITHUB_TEAM_ID=10188507
195

206
source admin/repo_list/app_iac.sh
217
source admin/repo_list/tf_iac.sh
228
source admin/repo_list/tf_module.sh
239

24-
RULESET_CONTENT=$(echo $(sed \
25-
-e "s|\${ENVIRONMENT_NAME}|${ENVIRONMENT_NAME}|g" \
26-
-e "s|\${DEV_ADMIN_GITHUB_TEAM_ID}|${DEV_ADMIN_GITHUB_TEAM_ID}|g" \
27-
-e "s|\${DEVOPS_ADMIN_GITHUB_TEAM_ID}|${DEVOPS_ADMIN_GITHUB_TEAM_ID}|g" \
28-
admin/repo_static/${ENVIRONMENT_NAME}/ruleset.json.tmpl))
29-
30-
3110
function get_ruleset_by_name() {
32-
REPOSITORY_NAME=${1}
33-
RULESET_NAME=${2}
11+
REPOSITORY_OWNER=${1}
12+
REPOSITORY_NAME=${2}
13+
RULESET_NAME=${3}
3414

3515
echo $(gh api \
3616
-H "Accept: application/vnd.github+json" \
@@ -40,7 +20,17 @@ function get_ruleset_by_name() {
4020
}
4121

4222
function create_ruleset() {
43-
REPOSITORY_NAME=${1}
23+
REPOSITORY_OWNER=${1}
24+
REPOSITORY_NAME=${2}
25+
ENVIRONMENT_NAME=${3}
26+
27+
echo "Creating ruleset for ${REPOSITORY_NAME} - ${ENVIRONMENT_NAME}"
28+
29+
RULESET_CONTENT=$(echo $(sed \
30+
-e "s|\${ENVIRONMENT_NAME}|${ENVIRONMENT_NAME}|g" \
31+
-e "s|\${DEV_ADMIN_GITHUB_TEAM_ID}|${DEV_ADMIN_GITHUB_TEAM_ID}|g" \
32+
-e "s|\${DEVOPS_ADMIN_GITHUB_TEAM_ID}|${DEVOPS_ADMIN_GITHUB_TEAM_ID}|g" \
33+
admin/repo_static/${ENVIRONMENT_NAME}/ruleset.json.tmpl))
4434

4535
echo "${RULESET_CONTENT}" | gh api \
4636
--method POST \
@@ -51,8 +41,18 @@ function create_ruleset() {
5141
}
5242

5343
function update_ruleset() {
54-
REPOSITORY_NAME=${1}
55-
RULESET_ID=${2}
44+
REPOSITORY_OWNER=${1}
45+
REPOSITORY_NAME=${2}
46+
ENVIRONMENT_NAME=${3}
47+
RULESET_ID=${4}
48+
49+
echo "Updating ruleset for ${REPOSITORY_NAME} - ${ENVIRONMENT_NAME}"
50+
51+
RULESET_CONTENT=$(echo $(sed \
52+
-e "s|\${ENVIRONMENT_NAME}|${ENVIRONMENT_NAME}|g" \
53+
-e "s|\${DEV_ADMIN_GITHUB_TEAM_ID}|${DEV_ADMIN_GITHUB_TEAM_ID}|g" \
54+
-e "s|\${DEVOPS_ADMIN_GITHUB_TEAM_ID}|${DEVOPS_ADMIN_GITHUB_TEAM_ID}|g" \
55+
admin/repo_static/${ENVIRONMENT_NAME}/ruleset.json.tmpl))
5656

5757
echo "${RULESET_CONTENT}" | gh api \
5858
--method PUT \
@@ -62,58 +62,65 @@ function update_ruleset() {
6262
--input - 1>/dev/null
6363
}
6464

65-
echo "::group::Updating repository settings"
66-
COMBINED_ARRAY=(${REPO_LIST_APP_IAC[@]} ${REPO_LIST_TF_IAC[@]} ${REPO_LIST_TF_MODULE[@]})
67-
for REPOSITORY_NAME in ${COMBINED_ARRAY[@]}; do
68-
echo "Updating ${REPOSITORY_OWNER}/${REPOSITORY_NAME}"
69-
gh api \
70-
--method PATCH \
71-
-H "Accept: application/vnd.github+json" \
72-
-H "X-GitHub-Api-Version: 2022-11-28" \
73-
/repos/${REPOSITORY_OWNER}/${REPOSITORY_NAME} \
74-
-F "has_issues=true" \
75-
-F "has_projects=false" \
76-
-F "has_wiki=false" \
77-
-F "allow_squash_merge=true" \
78-
-F "allow_merge_commit=true" \
79-
-F "allow_rebase_merge=false" \
80-
-F "allow_auto_merge=false" \
81-
-F "delete_branch_on_merge=true" \
82-
-F "allow_update_branch=true" 1>/dev/null
83-
done
84-
echo "::endgroup::"
85-
86-
echo "::group::tf-module repository rulesets"
87-
88-
for REPOSITORY_NAME in ${REPO_LIST_TF_MODULE[@]}; do
89-
if [ "${ENVIRONMENT_NAME}" = "prod" ]; then
90-
# Module repositories do not need a prod ruleset
91-
continue
92-
fi
93-
94-
ruleset_id=$(get_ruleset_by_name ${REPOSITORY_NAME} ${ENVIRONMENT_NAME})
95-
if [ -z "${ruleset_id}" ]; then
96-
echo "Creating ruleset for ${REPOSITORY_NAME}/${ENVIRONMENT_NAME}"
97-
create_ruleset ${REPOSITORY_NAME}
98-
else
99-
echo "Updating ruleset for ${REPOSITORY_NAME}/${ENVIRONMENT_NAME}"
100-
update_ruleset ${REPOSITORY_NAME} ${ruleset_id}
101-
fi
102-
done
103-
104-
echo "::endgroup::"
105-
106-
107-
echo "::group::app-tf-iac repository rulesets"
108-
COMBINED_ARRAY=(${REPO_LIST_APP_IAC[@]} ${REPO_LIST_TF_IAC[@]})
109-
for REPOSITORY_NAME in ${COMBINED_ARRAY[@]}; do
110-
ruleset_id=$(get_ruleset_by_name ${REPOSITORY_NAME} ${ENVIRONMENT_NAME})
111-
if [ -z "${ruleset_id}" ]; then
112-
echo "Creating ruleset for ${REPOSITORY_NAME}/${ENVIRONMENT_NAME}"
113-
create_ruleset ${REPOSITORY_NAME}
114-
else
115-
echo "Updating ruleset for ${REPOSITORY_NAME}/${ENVIRONMENT_NAME}"
116-
update_ruleset ${REPOSITORY_NAME} ${ruleset_id}
117-
fi
118-
done
119-
echo "::endgroup::"
65+
function update_repo_general_settings() {
66+
REPOSITORY_OWNER=${1}
67+
68+
echo "::group::Updating repository settings"
69+
COMBINED_ARRAY=(${REPO_LIST_APP_IAC[@]} ${REPO_LIST_TF_IAC[@]} ${REPO_LIST_TF_MODULE[@]})
70+
for REPOSITORY_NAME in ${COMBINED_ARRAY[@]}; do
71+
echo "Updating ${REPOSITORY_OWNER}/${REPOSITORY_NAME}"
72+
gh api \
73+
--method PATCH \
74+
-H "Accept: application/vnd.github+json" \
75+
-H "X-GitHub-Api-Version: 2022-11-28" \
76+
/repos/${REPOSITORY_OWNER}/${REPOSITORY_NAME} \
77+
-F "has_issues=true" \
78+
-F "has_projects=false" \
79+
-F "has_wiki=false" \
80+
-F "allow_squash_merge=true" \
81+
-F "allow_merge_commit=true" \
82+
-F "allow_rebase_merge=false" \
83+
-F "allow_auto_merge=false" \
84+
-F "delete_branch_on_merge=true" \
85+
-F "allow_update_branch=true" 1>/dev/null
86+
done
87+
echo "::endgroup::"
88+
}
89+
90+
function update_tf_module_rulesets() {
91+
REPOSITORY_OWNER=${1}
92+
ENVIRONMENT_NAME=${2}
93+
94+
echo "::group::tf-module repository rulesets"
95+
for REPOSITORY_NAME in ${REPO_LIST_TF_MODULE[@]}; do
96+
if [ "${ENVIRONMENT_NAME}" = "prod" ]; then
97+
# Module repositories do not need a prod ruleset
98+
continue
99+
fi
100+
101+
ruleset_id=$(get_ruleset_by_name ${REPOSITORY_OWNER} ${REPOSITORY_NAME} ${ENVIRONMENT_NAME})
102+
if [ -z "${ruleset_id}" ]; then
103+
create_ruleset ${REPOSITORY_OWNER} ${REPOSITORY_NAME} ${ENVIRONMENT_NAME}
104+
else
105+
update_ruleset ${REPOSITORY_OWNER} ${REPOSITORY_NAME} ${ENVIRONMENT_NAME} ${ruleset_id}
106+
fi
107+
done
108+
echo "::endgroup::"
109+
}
110+
111+
function update_tf_iac_rulesets() {
112+
REPOSITORY_OWNER=${1}
113+
ENVIRONMENT_NAME=${2}
114+
115+
echo "::group::app-tf-iac repository rulesets"
116+
COMBINED_ARRAY=(${REPO_LIST_APP_IAC[@]} ${REPO_LIST_TF_IAC[@]})
117+
for REPOSITORY_NAME in ${COMBINED_ARRAY[@]}; do
118+
ruleset_id=$(get_ruleset_by_name ${REPOSITORY_OWNER} ${REPOSITORY_NAME} ${ENVIRONMENT_NAME})
119+
if [ -z "${ruleset_id}" ]; then
120+
create_ruleset ${REPOSITORY_OWNER} ${REPOSITORY_NAME} ${ENVIRONMENT_NAME}
121+
else
122+
update_ruleset ${REPOSITORY_OWNER} ${REPOSITORY_NAME} ${ENVIRONMENT_NAME} ${ruleset_id}
123+
fi
124+
done
125+
echo "::endgroup::"
126+
}

0 commit comments

Comments
 (0)