Skip to content

Commit 4e20aa7

Browse files
committed
looking for other codeowners locations in repo
1 parent df5794d commit 4e20aa7

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

scripts/add-codeowners-file-to-repositories.sh

+31-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if [ ! -f "$1" ]; then
2424
fi
2525

2626
if [ ! -f "$2" ]; then
27-
echo "Codeowners file $2 does not exist"
27+
echo "CODEOWNERS file $2 does not exist"
2828
exit 1
2929
fi
3030

@@ -41,13 +41,31 @@ do
4141

4242
echo "Checking $org/$repo ..."
4343

44-
# check if it exists first
45-
# TO DO : check for all valid spots for CODEOWNERS file (docs, root, .github), right now just checks root
44+
# check if it exists first - CODEOWNERS file can be in the root, .github, or docs folder
4645
if file=$(gh api /repos/$org/$repo/contents/CODEOWNERS 2>&1); then
46+
exists=1
47+
path="CODEOWNERS"
48+
message="Updating CODEOWNERS file"
49+
elif file=$(gh api /repos/$org/$repo/contents/.github/CODEOWNERS 2>&1); then
50+
exists=1
51+
path=".github/CODEOWNERS"
52+
message="Updating CODEOWNERS file"
53+
elif file=$(gh api /repos/$org/$repo/contents/docs/CODEOWNERS 2>&1); then
54+
exists=1
55+
path="docs/CODEOWNERS"
56+
message="Updating CODEOWNERS file"
57+
else
58+
exists=0
59+
path=CODEOWNERS
60+
message="Adding CODEOWNERS file"
61+
fi
62+
63+
if [ $exists=1 ]; then
4764
sha=$(echo $file | jq -r '.sha')
4865
echo " ... CODEOWNERS file already exists"
4966
# if $overwrite is true, then delete the CODEOWNERS file
5067
if [ "$overwrite" != true ] ; then
68+
echo " ... replacing CODEOWNERS file"
5169
content=$(echo $file | jq -r '.content' | base64 -d)
5270
# create temp CODEOWNERS file and add existing to top
5371
echo "$content" | cat - $codeownersfile > ./CODEOWNERS.tmp
@@ -60,8 +78,16 @@ do
6078
fi
6179

6280
# Commit the CODEOWNERS file
63-
echo "comitting CODEOWNERS file to $org/$repo"
64-
gh api /repos/$org/$repo/contents/CODEOWNERS -f message="Add CODEOWNERS file" -f content="$(base64 -i $codeownersfile)" -f sha=$sha -X PUT
81+
echo " ... comitting $path file to $org/$repo"
82+
if response=$(gh api -X PUT /repos/$org/$repo/contents/$path -f message="$message" -f content="$(base64 -i $codeownersfile)" -f sha=$sha 2>/dev/null); then
83+
path=$(echo $response | jq -r '.content.path')
84+
sha=$(echo $response | jq -r '.content.sha')
85+
date=$(echo $response | jq -r '.commit.committer.date')
86+
echo " ... committed $path with sha $sha on $date"
87+
else
88+
error=$(echo $response | jq -r ".message")
89+
echo " ... failed to commit $path; $error"
90+
fi
6591

6692
# Delete the temp CODEOWNERS file if it exists
6793
if [ -f "./CODEOWNERS.tmp" ]; then

scripts/add-gitignore-file-to-repositories.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#
1111
# Overwrite or append:
1212
# - Defaults to append
13-
# - If you want to overwrite the gitignore file, pass true as the 3rd argument, otherwise, it will append to the existing file
13+
# - If you want to overwrite the .gitignore file, pass true as the 3rd argument, otherwise, it will append to the existing file
1414
#
1515

1616
if [ $# -lt "2" ]; then
17-
echo "Usage: $0 <reposfilename> <gitignore-file> [overwrite: true|false]"
17+
echo "Usage: $0 <reposfilename> <.gitignore-file> [overwrite: true|false]"
1818
exit 1
1919
fi
2020

@@ -24,7 +24,7 @@ if [ ! -f "$1" ]; then
2424
fi
2525

2626
if [ ! -f "$2" ]; then
27-
echo "gitignore file $2 does not exist"
27+
echo ".gitignore file $2 does not exist"
2828
exit 1
2929
fi
3030

@@ -44,23 +44,23 @@ do
4444
# check if it exists first
4545
if file=$(gh api /repos/$org/$repo/contents/.gitignore 2>&1); then
4646
sha=$(echo $file | jq -r '.sha')
47-
echo " ... gitignore file already exists"
47+
echo " ... .gitignore file already exists"
4848
# if $overwrite is true, then delete the gitignore file
4949
if [ "$overwrite" != true ] ; then
5050
content=$(echo $file | jq -r '.content' | base64 -d)
51-
# create temp gitignore file and add existing to top
51+
# create temp .gitignore file and add existing to top
5252
echo "$content" | cat - $gitignorefile > ./.gitignore.tmp
5353
gitignorefile=./.gitignore.tmp
5454
fi
5555
else
56-
echo " ... gitignore file doesn't exist"
56+
echo " ... .gitignore file doesn't exist"
5757
sha=""
5858
gitignorefile=$gitignorefile
5959
fi
6060

61-
# Commit the gitignore file
61+
# Commit the .gitignore file
6262
echo " ... comitting .gitignore file to $org/$repo"
63-
gh api /repos/$org/$repo/contents/.gitignore -f message="Add gitignore file" -f content="$(base64 -i $gitignorefile)" -f sha=$sha -X PUT
63+
gh api -X PUT /repos/$org/$repo/contents/.gitignore -f message="Adding .gitignore file" -f content="$(base64 -i $gitignorefile)" -f sha=$sha
6464

6565
# Delete the temp gitignore file if it exists
6666
if [ -f "./.gitignore.tmp" ]; then

0 commit comments

Comments
 (0)