-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootstrap.sh
executable file
·165 lines (135 loc) · 3.77 KB
/
bootstrap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
set -u -e -o pipefail
REPO_TEAM=''
REPO_DESC=''
REPO_NAME="$(basename "$PWD")"
REPO_README=true
function usages() {
cat <<EOM
$(basename "$0") OPTIONS
OPTIONS:
[-t|--team <name>] repository team (default: "$REPO_TEAM")
[-d|--desc <description>] repository description (default: "$REPO_DESC")
[-n|--name <name>] repository name (default: "$REPO_NAME")
[-r|--readme produce a minimal readme (default: "$REPO_README")
[-h|--help] shows this help message
EOM
exit 0
}
# parse arguments
# https://stackoverflow.com/a/33826763
while [[ $# -gt 0 ]]; do
case $1 in
-t | --team)
REPO_TEAM="$2"
shift
;;
-d | --desc)
REPO_DESC="$2"
shift
;;
-n | --name)
REPO_NAME="$2"
shift
;;
-h | --help)
usages
;;
-r | --readme)
REPO_README="$2"
shift
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done
# install pre-commit
pre-commit install --install-hooks -t pre-commit -t commit-msg --overwrite
echo
if [ -z "$REPO_TEAM" ]; then
# read a team
TEAMS="$(grep "@DynamicYield" .github/CODEOWNERS | sed 's/^# //g')"
COLUMNS=1
PS3=$'\n'"Pick a team number: "
select REPO_TEAM in $TEAMS; do break; done
else
echo "Repository team: ${REPO_TEAM}"
fi
# define code-ownership
echo "* $REPO_TEAM" >>.github/CODEOWNERS
# define team permissions for code-ownership
REPO_TEAM_SHORT="$(echo "$REPO_TEAM" | cut -d / -f 2)"
PATCH=$(
cat <<-EOS
teams:
- name: $REPO_TEAM_SHORT
permission: push
EOS
)
perl -i -p0e "s/^teams:/$PATCH/m" .github/settings.yml
if [ -z "$REPO_NAME" ]; then
# read repository name
read -erp "Repository name: " -i "$REPO_NAME" REPO_NAME
fi
# set repository name
perl -i -p0e "s/# name: template/name: $REPO_NAME/m" .github/settings.yml
if [ -z "$REPO_DESC" ]; then
# read repository description
read -erp "Repository description: " -i "$REPO_DESC" REPO_DESC
else
echo "Repository description: ${REPO_DESC}"
fi
# set repository description
perl -i -p0e "s/description: GitHub Template Repository/description: $REPO_DESC/m" .github/settings.yml
# # delete this script
rm -f "$0"
# settings pr
git checkout main
git checkout -b settings
git add .github/settings.yml bootstrap.sh
git commit -S -m "build(repo): settings" -q
git push origin "$(git branch --show-current)"
gh pr create --base main --draft --body "# merge this first!" --title "build(repo): settings" --fill
# codeowners pr
git checkout main
git checkout -b codeowners
git add .github/CODEOWNERS
git commit -S -m "build(repo): codeowners" -q
git push origin "$(git branch --show-current)"
gh pr create --base main --draft --body "# merge this second! (after #1)" --title "build(repo): codeowners" --fill
if [ "$REPO_README" = "true" ]; then
# readme
cat >README.md <<EOM
# $REPO_NAME
$REPO_DESC
## Prerequisites
Describe what needs to be done for the [Getting Started](#getting-started) to work
## Getting Started
Describe what need to be done in order to work with the project
## Contributing
1. Install pre-commit
\`\`\`shell
pre-commit install --install-hooks -t pre-commit -t commit-msg
\`\`\`
2. Open a pull-request
This repository was created from [template](https://github.com/DynamicYield/template)
EOM
# readme pr
git checkout main
git checkout -b docs
git add README.md
git commit -S -m "docs(README): initial docs" -q
git push origin "$(git branch --show-current)"
gh pr create --base main --draft --title "docs(README): initial docs" --fill
fi
for PREFIX in DY MCD; do
gh api '/repos/{owner}/{repo}/autolinks' \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-f key_prefix="${PREFIX}-" \
-f url_template="https://dynamicyield.atlassian.net/browse/${PREFIX}-<num>"
done
git checkout main