-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·365 lines (295 loc) · 9.26 KB
/
Copy pathentrypoint.sh
File metadata and controls
executable file
·365 lines (295 loc) · 9.26 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env bash
# Copyright Buda.com.SpA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o nounset
set -o pipefail
show_help() {
cat << EOF
Usage: $(basename "$0") <options>
-h, --help Display help
-m, --mode The mode in which to run the action (one of: sync, apply)
-d, --dry-run Run the action with dry-run
--committer-email The email to use when creating the commits
--committer-name The name to use when creating the commits
--template-name Specific template to apply (skips git diff detection)
--draft-pr Create the pull request as draft
EOF
}
# Global variable for cache
labels=""
main() {
local mode=
local dry_run=false
local committer_email=
local committer_name=
local draft_pr=false
local template_name=
parse_command_line $@
if [[ $mode = "sync" ]]; then
sync
elif [[ $mode = "apply" ]]; then
apply
fi
echo $mode
}
parse_command_line() {
while :; do
case "${1:-}" in
-h|--help)
show_help
exit
;;
-v|--version)
if [[ -n "${2:-}" ]]; then
version="$2"
shift
else
echo "ERROR: '-v|--version' cannot be empty." >&2
show_help
exit 1
fi
;;
-m|--mode)
if [[ -n "${2:-}" ]]; then
mode="$2"
shift
else
echo "ERROR: '-m|--mode' cannot be empty." >&2
show_help
exit 1
fi
;;
-d|--dry-run)
if [[ -n "${2:-}" ]]; then
dry_run="$2"
shift
else
echo "ERROR: '-d|--dry-run' cannot be empty." >&2
show_help
exit 1
fi
;;
--committer-email)
if [[ -n "${2:-}" ]]; then
committer_email="$2"
shift
else
echo "ERROR: '--committer-email' cannot be empty." >&2
show_help
exit 1
fi
;;
--committer-name)
if [[ -n "${2:-}" ]]; then
committer_name="$2"
shift
else
echo "ERROR: '--committer-name' cannot be empty." >&2
show_help
exit 1
fi
;;
--template-name)
if [[ -n "${2:-}" ]]; then
template_name="$2"
shift
fi
;;
*)
break
;;
esac
shift
done
if [[ -z "$mode" ]]; then
echo "ERROR: '-m|--mode' is required." >&2
show_help
exit 1
fi
if [[ ! "$mode" =~ ^(sync|apply)$ ]]; then
echo "ERROR: '$mode mode is not supported." >&2
show_help
exit 1
fi
if [[ $mode = 'sync' ]]; then
if [[ -z "$committer_name" ]]; then
echo "ERROR: '--committer-name' is required on sync mode" >&2
show_help
exit 1
fi
if [[ -z "$committer_email" ]]; then
echo "ERROR: '--committer-email' is required on sync mode" >&2
show_help
exit 1
fi
fi
}
sync() {
setup_git
setup_gh
echo "===> Running in syncronization mode"
sendsync get template
templates=$(get_changed_templates_on_workdir templates)
for template in $templates; do
echo "---> Stashing $template..."
git add -A templates/$template
git stash push -m $template -- templates/$template
echo ""
done
git stash list | while read line; do
stashed_template=$(git stash list | grep "stash@{0}" | cut -d' ' -f4)
if [ $dry_run = "false" ]; then
open_pr_for_branch=$(get_open_pr_by_branch $stashed_template)
if [[ -z $open_pr_for_branch ]]; then
### No pending open PR for the template
# We create a new pr and branch
create_pr_for_template $stashed_template
else
### There is a PR open for this template
# Compares the origin branch with the stashed index for the specific template
# looking for changes.
branch_changes=$(git diff --exit-code origin/$stashed_template stash@{0} -- templates/$stashed_template &>/dev/null; echo $?;)
if [[ $branch_changes == 0 ]]; then
# We skip this stash, just dropping it
echo "---> Skipping $template, no new changes..."
git stash drop
echo ""
elif [[ $branch_changes == 1 ]]; then
# We update the branch with the new changes
update_pr_for_template $stashed_template
fi
fi
else
echo "DRY RUN: would have created PR for ${stashed_template}"
fi
done
}
apply() {
setup_git
echo "===> Running in apply mode"
if [[ -n "$template_name" ]]; then
templates="$template_name"
echo "---> Using specified template: $template_name"
else
templates=$(get_changed_templates_on_commit templates HEAD~1 $GITHUB_SHA)
fi
for template in ${templates}; do
if [ $dry_run = "false" ]; then
sendsync apply -f templates/${template}/template.json
else
echo "DRY RUN: would have applied ${template}"
fi
done
}
setup_git() {
echo "===> Setting up git"
git config --global --add safe.directory $GITHUB_WORKSPACE
git pull --unshallow
git config --global user.email $committer_email
git config --global user.name $committer_name
git fetch origin
git merge origin/master
echo ""
}
setup_gh() {
echo "===> Setting up github cli"
echo ${GITHUB_TOKEN} | gh auth login --with-token
echo ""
}
get_changed_templates_on_workdir() {
local templates_path=$1
git status -s $templates_path | cut -d'/' -f 2 | sort | uniq
}
get_changed_templates_on_commit() {
local templates_path=$1
local base_commit=$2
local commit=$3
git diff --name-only -m $commit $base_commit $templates_path | cut -d'/' -f 2 | sort | uniq
}
create_pr_for_template() {
local template=$1
local template_path=templates/$template
local branch=$template
echo "---> Creating PR for $template..."
git checkout -b $branch
git stash pop
git add $template_path
git commit -m "feat(template): add template $template"
git push origin $branch
local _template_labels=$(extract_labels_args_from_template $template)
gh pr create -d --fill \
--label "auto" $_template_labels
update_body $template
git checkout master
echo ""
}
update_pr_for_template() {
local template=$1
local template_path=templates/$template
local branch=$template
echo "---> Updating PR for $template..."
git checkout $branch
stash_status=$(git stash pop &>/dev/null; echo $?;)
git status --porcelain
if [[ $stash_status == 1 ]]; then
git checkout --theirs $template_path
git stash drop
fi
git add $template_path
git commit -m "feat(template): update template $template"
git push origin $branch
update_body $template
git checkout master
echo ""
}
extract_labels_args_from_template() {
local _template=$1
local _labels=""
local _repo_labels=$(get_labels)
for _label in $_repo_labels; do
if [[ $_template =~ $_label ]]; then
_labels+="--label $_label "
fi
done
echo $_labels
}
get_open_pr_by_branch() {
local _branch=$1
# Returns pr number if there is one open, empty if none
open_pr=$(gh api --jq ".[].number" "repos/{owner}/{repo}/pulls?head={owner}:$_branch&state=open")
echo -n $open_pr
}
get_labels() {
# Use global labels variable to cache
if [[ -z "$labels" ]]; then
labels=$(gh label list --json name --jq ".[].name")
fi
echo $labels
}
update_body() {
local _template=$1
local _template_json=templates/$template/template.json
thumbnail_url=$(cat $_template_json | jq -r '.versions[0].thumbnail_url')
subject=$(cat $_template_json | jq -r '.versions[0].subject')
_body="
## $template.
**Subject:** $subject

Review this PR to approve.
Merge to deploy to production.
"
gh pr edit $_template \
--body "$_body"
}
main "$@"