generated from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.sh
executable file
·207 lines (165 loc) · 5.9 KB
/
entrypoint.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
set -euo pipefail
# ==============================================================================
datetime=${BUILD_DATETIME:-$(date -u +"%Y-%m-%dT%H:%M:%S%z")}
build_datetime_local=$(date --date="$datetime" +"%Y-%m-%dT%H:%M:%S%z")
build_datetime=$datetime
build_timestamp=$(date --date="$datetime" -u +"%Y%m%d%H%M%S")
branch_name=${BRANCH_NAME:-"main"}
org=$(echo "$REPOSITORY_TO_UPDATE" | cut -d'/' -f2)
work_dir=/github/workspace
src_dir=$work_dir/repository-template
dest_dir=$work_dir/repository-to-update
list_of_files_to_update_json=$work_dir/update-from-template.json
git_user_name=${GIT_USER_NAME:-"unknown"}
git_user_email=${GIT_USER_EMAIL:-"unknown@users.noreply.github.com"}
GITHUB_APP_PK_FILE=$work_dir/gh_app_pk.pem
if ! [ -f $GITHUB_APP_PK_FILE ]; then
echo "$GITHUB_APP_PK_CONTENT" > $GITHUB_APP_PK_FILE
fi
GITHUB_APP_SK_CONTENT_FILE=$work_dir/gh_app_sk.pem
if ! [ -f $GITHUB_APP_SK_CONTENT_FILE ]; then
echo "$GITHUB_APP_SK_CONTENT" > $GITHUB_APP_SK_CONTENT_FILE
fi
# ==============================================================================
function main() {
github_token=$(create-github-token)
configure-git-access
fetch-repositories-content
prune-legacy-updates
checkout-new-branch
produce-list-of-files-to-update
update-files
push-and-create-pull-request
}
function create-github-token() {
is-arg-true "${DRY_RUN:-false}" && return
header=$(echo -n '{"alg":"RS256","typ":"JWT"}' | base64 | tr -d '=' | tr -d '\n=' | tr -- '+/' '-_')
payload=$(echo -n '{"iat":'$(date +%s)',"exp":'$(($(date +%s)+600))',"iss":"'$GITHUB_APP_ID'"}' | base64 | tr -d '\n=' | tr -- '+/' '-_')
signature=$(echo -n "$header.$payload" | openssl dgst -binary -sha256 -sign $GITHUB_APP_PK_FILE | openssl base64 | tr -d '\n=' | tr -- '+/' '-_')
jwt="$header.$payload.$signature"
installations_response=$(curl -X GET \
-H "Authorization: Bearer $jwt" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations)
installation_id=$(echo $installations_response | jq '.[] | select(.account.login == "'"$org"'") .id')
token_response=$(curl -X POST \
-H "Authorization: Bearer $jwt" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/$installation_id/access_tokens)
github_token=$(echo $token_response | jq .token -r)
echo "$github_token"
}
function configure-git-access() {
git config --global --add safe.directory $dest_dir
git config --global user.name "$git_user_name"
git config --global user.email "$git_user_email"
[ -z "$github_token" ] && return
gpg --import --batch $GITHUB_APP_SK_CONTENT_FILE
git config --global user.signingkey $GITHUB_APP_SK_ID
git config --global commit.gpgsign true
git config --global gpg.program /gpg.sh
echo "$github_token" | gh auth login --with-token
gh auth status
gh auth setup-git
}
function fetch-repositories-content() {
mkdir -p $work_dir
cd $work_dir
if [ -n "$github_token" ] && ! [ -d $src_dir ] && ! [ -d $dest_dir ]; then
git clone https://x-access-token:${github_token}@${REPOSITORY_TEMPLATE} $src_dir
git clone https://x-access-token:${github_token}@${REPOSITORY_TO_UPDATE} $dest_dir
else
if ! [ -d $src_dir ]; then
git clone https://${REPOSITORY_TEMPLATE} $src_dir
else
(
cd $src_dir
git fetch origin
default_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
git reset --hard origin/$default_branch
)
fi
if ! [ -d $dest_dir ]; then
git clone https://${REPOSITORY_TO_UPDATE} $dest_dir
else
(
cd $dest_dir
git fetch origin
default_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
git reset --hard origin/$default_branch
)
fi
fi
cd $src_dir
git checkout $branch_name
}
function prune-legacy-updates() {
[ -z "$github_token" ] && return
cd $dest_dir
# Close legacy PRs
pr_numbers=$(gh pr list --search "Update from template" --json number,title | jq '.[] | select(.title | startswith("Update from template")).number')
for pr_number in $pr_numbers; do
gh pr close $pr_number
done
# Delete legacy branches
for branch in $(git branch -r | grep 'origin/update-from-template'); do
git push origin --delete ${branch#origin/}
done
}
function checkout-new-branch() {
cd $dest_dir
git checkout -b update-from-template-${build_timestamp}
}
function produce-list-of-files-to-update() {
cd $dest_dir
/update-from-template \
--source-dir $src_dir \
--destination-dir $dest_dir \
--app-config-file /update-from-template.yaml \
--template-config-file $dest_dir/scripts/config/repository-template.yaml \
> $list_of_files_to_update_json
}
function update-files() {
# Update files
to_update=$(
cat $list_of_files_to_update_json \
| jq -r '.comparison | to_entries[] | select(.value.action == "update") | .key'
)
echo "$to_update" | while IFS= read -r file; do
dir=$(dirname "$file")
mkdir -p $dest_dir/$dir
cp $src_dir/$file $dest_dir/$file
done
# Delete files
to_delete=$(
cat $list_of_files_to_update_json \
| jq -r '.comparison | to_entries[] | select(.value.action == "delete") | .key'
)
echo "$to_delete" | while IFS= read -r file; do
rm -rf $dest_dir/$file
done
}
function push-and-create-pull-request() {
cd $dest_dir
# Add and commit changes
git add -A
git commit -m "Update from template ${build_datetime_local}"
[ -z "$github_token" ] && return
# Push and create new PR
git push -u origin update-from-template-${build_timestamp}
gh pr create \
--title "Update from template" \
--body "Update from template ${build_datetime_local}"
}
function is-arg-true() {
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return 0
else
return 1
fi
}
# ==============================================================================
is-arg-true "${VERBOSE:-false}" && set -x
main $*
exit 0