Skip to content

Commit a982b70

Browse files
committed
add scripts to add CODEOWNERS and .gitignore files
1 parent b1bb756 commit a982b70

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

scripts/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ FluffyCarlton
2020

2121
Migrate work items from Azure DevOps to GitHub issues - this just links out to a [separate repo](https://github.com/joshjohanning/ado_workitems_to_github_issues)
2222

23+
## add-codeowners-file-to-repositories.sh
24+
25+
Adds a `CODEOWNERS` file to a list of repositories.
26+
27+
1. Run: `./generate-repos.sh <org> > repos.csv`
28+
- Or create a list of repos in a csv file, 1 per line, with a trailing empty line at the end of the file
29+
2. Run: `./add-codeowners-file-to-repositories.sh repos.csv ./CODEOWNERS false`
30+
- For the 3rd argument, pass `true` if you want to overwrite existing file, otherwise it appends to existing
31+
32+
> **Note**
33+
> This is currently only checking for CODEOWNERS files in the root
34+
35+
## add-gitignore-file-to-repositories.sh
36+
37+
Adds a `.gitignore` file to a list of repositories.
38+
39+
1. Run: `./generate-repos.sh <org> > repos.csv`
40+
- Or create a list of repos in a csv file, 1 per line, with a trailing empty line at the end of the file
41+
2. Run: `./add-gitignore-file-to-repositories.sh repos.csv ./.gitignore false`
42+
- For the 3rd argument, pass `true` if you want to overwrite existing file, otherwise it appends to existing
43+
2344
## create-enterprise-organizations.sh
2445

2546
Loops through a list of orgs and creates them.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
# DOT NOT REMOVE TRAILING NEW LINE IN THE INPUT CSV FILE
3+
4+
# Need to run this to get the repo delete scope: gh auth refresh -h github.com -s delete_repo
5+
6+
# Usage:
7+
# Step 1: Run ./generate-repos.sh <org> > repos.csv
8+
# (or create a list of repos in a csv file, 1 per line, with a trailing empty line at the end of the file)
9+
# Step 2: ./add-codeowners-file-to-repositories.sh repos.csv ./CODEOWNERS false
10+
#
11+
# Overwrite or append:
12+
# - Defaults to append
13+
# - If you want to overwrite the CODEOWNERS file, pass true as the 3rd argument, otherwise, it will append to the existing file
14+
#
15+
16+
if [ $# -lt "2" ]; then
17+
echo "Usage: $0 <reposfilename> <codeowners-file> [overwrite: true|false]"
18+
exit 1
19+
fi
20+
21+
if [ ! -f "$1" ]; then
22+
echo "Repo import file $1 does not exist"
23+
exit 1
24+
fi
25+
26+
if [ ! -f "$2" ]; then
27+
echo "Codeowners file $2 does not exist"
28+
exit 1
29+
fi
30+
31+
filename="$1"
32+
codeownersfile="$2"
33+
overwrite="$3"
34+
35+
while read -r repofull ;
36+
do
37+
IFS='/' read -ra data <<< "$repofull"
38+
39+
org=${data[0]}
40+
repo=${data[1]}
41+
42+
echo "Checking $org/$repo ..."
43+
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
46+
if file=$(gh api /repos/$org/$repo/contents/CODEOWNERS 2>&1); then
47+
sha=$(echo $file | jq -r '.sha')
48+
echo " ... CODEOWNERS file already exists"
49+
# if $overwrite is true, then delete the CODEOWNERS file
50+
if [ "$overwrite" != true ] ; then
51+
content=$(echo $file | jq -r '.content' | base64 -d)
52+
# create temp CODEOWNERS file and add existing to top
53+
echo "$content" | cat - $codeownersfile > ./CODEOWNERS.tmp
54+
codeownersfile=./CODEOWNERS.tmp
55+
fi
56+
else
57+
echo " ... codeowners file doesn't exist"
58+
sha=""
59+
codeownersfile=$codeownersfile
60+
fi
61+
62+
# 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
65+
66+
# Delete the temp CODEOWNERS file if it exists
67+
if [ -f "./CODEOWNERS.tmp" ]; then
68+
rm ./CODEOWNERS.tmp
69+
fi
70+
71+
done < "$filename"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
# DOT NOT REMOVE TRAILING NEW LINE IN THE INPUT CSV FILE
3+
4+
# Need to run this to get the repo delete scope: gh auth refresh -h github.com -s delete_repo
5+
6+
# Usage:
7+
# Step 1: Run ./generate-repos.sh <org> > repos.csv
8+
# (or create a list of repos in a csv file, 1 per line, with a trailing empty line at the end of the file)
9+
# Step 2: ./add-gitignore-file-to-repositories.sh repos.csv ./.gitignore false
10+
#
11+
# Overwrite or append:
12+
# - 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
14+
#
15+
16+
if [ $# -lt "2" ]; then
17+
echo "Usage: $0 <reposfilename> <gitignore-file> [overwrite: true|false]"
18+
exit 1
19+
fi
20+
21+
if [ ! -f "$1" ]; then
22+
echo "Repo import file $1 does not exist"
23+
exit 1
24+
fi
25+
26+
if [ ! -f "$2" ]; then
27+
echo "gitignore file $2 does not exist"
28+
exit 1
29+
fi
30+
31+
filename="$1"
32+
gitignorefile="$2"
33+
overwrite="$3"
34+
35+
while read -r repofull ;
36+
do
37+
IFS='/' read -ra data <<< "$repofull"
38+
39+
org=${data[0]}
40+
repo=${data[1]}
41+
42+
echo "Checking $org/$repo ..."
43+
44+
# check if it exists first
45+
if file=$(gh api /repos/$org/$repo/contents/.gitignore 2>&1); then
46+
sha=$(echo $file | jq -r '.sha')
47+
echo " ... gitignore file already exists"
48+
# if $overwrite is true, then delete the gitignore file
49+
if [ "$overwrite" != true ] ; then
50+
content=$(echo $file | jq -r '.content' | base64 -d)
51+
# create temp gitignore file and add existing to top
52+
echo "$content" | cat - $gitignorefile > ./.gitignore.tmp
53+
gitignorefile=./.gitignore.tmp
54+
fi
55+
else
56+
echo " ... gitignore file doesn't exist"
57+
sha=""
58+
gitignorefile=$gitignorefile
59+
fi
60+
61+
# Commit the gitignore file
62+
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
64+
65+
# Delete the temp gitignore file if it exists
66+
if [ -f "./.gitignore.tmp" ]; then
67+
rm ./.gitignore.tmp
68+
fi
69+
70+
done < "$filename"

0 commit comments

Comments
 (0)