-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.sh
404 lines (389 loc) · 12.1 KB
/
package.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/zsh
autoload -U colors && colors
txtcolor=yellow
#### SHOW ####
# PRINTS ACTIVE TICKET KEYS AND THEIR STATUSES
_jira_show() {
if _1pass_load; then
return
fi
ticks=$(_read_ticks)
token=$JIRA_TOKEN
uname=$JIRA_UNAME
ghtok=$GITHUB_TOKEN
while IFS= read -r tick; do
stat=$(_print_tick $tick 2>/dev/null &)
echo $stat
done <<< $ticks
wait
}
_print_tick() {
tick=$1
_prsl $tick': '
blob=$(curl -s -u $uname:$token -X GET -H "Content-Type: application/json" https://energysage.atlassian.net/rest/api/3/issue/$tick)
stat=$(jq -r '.fields.status.name' <<< $blob)
if [[ $stat == "Awaiting Deployment" ]]; then
opts=$(git branch --format='%(refname:short)' | sed '/^develop$/d')
line=$(echo $opts | grep -E "$tick" | sed 's/^[[:blank:]]*//')
if [[ -n $line ]]; then
resp=$(curl -s -X GET -H "Authorization: token $ghtok" --url "https://api.github.com/repos/EnergySage/es-site/pulls?state=closed&per_page=100&head=EnergySage:$line")
if [ $(jq -r '. | length' --jsonargs <<< $resp) -ne 0 ]; then
open=$(jq -r '.[].state' --jsonargs <<< $resp)
if [[ -n $open ]]; then
stat='Merged'
fi
fi
fi
elif [[ $stat == "Code Review" ]]; then
opts=$(git branch --format='%(refname:short)' | sed '/^develop$/d')
line=$(echo $opts | grep -E "$tick" | sed 's/^[[:blank:]]*//')
if [[ -n $line ]]; then
resp=$(curl -s -X GET -H "Authorization: token $ghtok" --url "https://api.github.com/repos/EnergySage/es-site/pulls?state=open&per_page=100&head=EnergySage:$line")
indx=$(jq -r '.[].number' --jsonargs <<< $resp)
if [ -z $indx ]; then
stat+=" (no PR)"
else
resp=$(curl -s -X GET -H "Authorization: token $ghtok" --url "https://api.github.com/repos/EnergySage/es-site/issues/$indx/comments")
ncom=$(jq -r 'map(select(.user.login != "swarmia[bot]")) | length' --jsonargs <<< $resp)
resp=$(curl -s -X GET -H "Authorization: token $ghtok" --url "https://api.github.com/repos/EnergySage/es-site/pulls/$indx/reviews")
nres=$(jq -r 'length' --jsonargs <<< $resp)
stat+=' ('$(($ncom + $nres))')'
fi
fi
fi
echo $stat
}
# UPDATES REGISTRY IN curr.txt WITH ACTIVE TICKETS
_jira_pull() {
if _1pass_load; then
return
fi
token=$JIRA_TOKEN
uname=$JIRA_UNAME
encod=$(printf "%s" $uname | jq -s -R @uri)
_prnl updating local ticket registry
blob=$(curl -s -u $uname:$token -X GET -H "Content-Type: application/json" "https://energysage.atlassian.net/rest/api/3/search?jql=assignee=$encod")
keys=$(jq -r '.issues[] | select(.fields.status.name != "Done") | .key' <<< $blob)
echo $keys > ~/BranchSage/curr.txt
}
#### TRIM ####
# REMOVES REMAINING BRANCHES FOR COMPLETED TICKETS
_git_trim() {
ticks=$(_read_ticks)
brans=$(git branch --format='%(refname:short)' | sed '/^[A-Z]\{1,\}-[0-9]\{4\}-.*$/!d')
count=0
if [[ $(git branch --show-current) != "develop" ]]; then
if [[ -z $(git status -s) ]]; then
_prnl switching to develop
git checkout develop
else
_prnl stash or commit changes
return
fi
fi
while IFS= read -r bran; do
key=$(echo $bran | awk -F'-' '{print $1 "-" $2}')
if [[ $ticks != *"$key"* ]]; then
git branch --delete $bran
((count++))
fi
done <<< $brans
if [[ $count -gt 0 ]]; then
_prnl branches trimmed!
else
_prnl no branches to trim
fi
}
#### GROW ####
# CREATES BRANCHES FOR ACTIVE TICKETS WITHOUT THEM
_git_grow () {
ticks=$(_read_ticks)
brans=$(git branch --format='%(refname:short)' | sed '/^develop$/d')
count=0
if [[ $(git branch --show-current) != "develop" ]]; then
if [ -z $(git status -s) ]; then
_prnl switching to develop
git checkout develop
else
_prnl stash or commit changes
return
fi
fi
if [[ -n $(git status -s) ]]; then
_prnl stash or commit changes
return
fi
while IFS= read -r tick; do
if [[ $brans != *"$tick"* ]]; then
if [[ $(git branch --show-current) != "develop" ]]; then
git restore .
fi
if [[ $count == 0 ]]; then
_git_sync
fi
name=$(_jira_name $tick)
_prnl creating branch $name
git checkout -b $name
git push -u origin HEAD
((count++))
fi
done <<< $ticks
if [[ $count -gt 0 ]]; then
_prnl branches grown!
else
_prnl no branches to grow
fi
}
_jira_name() { # CREATES NAME FROM TICKET
if _1pass_load; then
return
fi
token=$JIRA_TOKEN
uname=$JIRA_UNAME
blob=$(curl -s -u $uname:$token -X GET -H "Content-Type: application/json" https://energysage.atlassian.net/rest/api/3/issue/$1)
tags=$(jq -r '.key' <<< $blob)
desc=$(jq -r '.fields.summary' <<< $blob)
name=$(echo $desc | tr '[:upper:]' '[:lower:]')
name=$(echo $name | tr ' ' '-')
name=$(echo $name | tr -cd '[:alnum:]-')
name=$(echo $name | sed 's/-$//')
tags+='-'$name
echo $tags
}
#### SYNC ####
# PREPS DEVELOP FOR NEW BRANCH USING EGS PROCESS
_git_sync() {
if _1pass_load; then
return
fi
awsid=$AWS_PREF
if [[ -n $(git status -s) ]]; then
_prnl stash or commit changes
return
fi
cd ~/Dev/es-project/es-site/es
_prnl pulling upstream
git pull --ff-only
if aws sts get-caller-identity 2>&1 | grep -q -e "Unable to locate\|Token has expired"; then
_prnl login to aws
source ~/Dev/es-dev-utils/aws_login.sh $awsid
fi
_prnl running make update
make update
}
#### SWAP ####
# TAKES ISSUE IDX OR PRINTS PROMPT TO SWITCH BRANCH
_git_swap() {
if [[ -n $(git status -s) ]]; then
_prnl stash or commit changes
return
fi
idx=$1
if [ -z $idx ]; then
_git_opts
opts=$(git branch --format='%(refname:short)' | sed '/develop/d')
_prsl "issue # >>> "
read idx
if [ -z $idx ]; then
return
fi
fi
if [[ $idx == '.' ]]; then
line=develop
else
line=$(echo $opts | grep -E "$idx" | sed 's/^[[:blank:]]*//')
if [ -z $line ]; then
_prnl branch not found
return
fi
fi
git checkout $line
}
# PRINTS BRANCH OPTS
_git_opts() {
opts=$(git branch | sed -e '/develop$/d')
_prnl available branches:
echo $opts
}
# PRINTS CHANGED FILES, INDEXED
_git_file() {
opts=$(git status -s | sed 's/^[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*//')
if [[ -n $opts ]]
then
opts=$(echo $opts | nl -w1 -s": " -)
_prnl changed files:
echo $opts
fi
}
#### REST ####
# TAKES FILE IDX OR PRINTS PROMPT TO RESTORE FILE
_git_rest() {
if [ -z "$(git status -s)" ]; then
return
fi
idx=$1
if [ -z $idx ]; then
_git_file
_prsl "file # >>> "
read idx
if [ -z $idx ]; then
return
fi
fi
if [[ $idx == '.' ]]; then
line=.
name=all
else
opts=$(git status -s | sed 's/^[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*//')
line=$(echo $opts | sed "$idx q;d")
name=$(echo $line | sed 's|.*/||')
fi
_prnl restoring $name
git restore $line
}
#### DIFF ####
# TODO
_git_diff() {
if [ -z "$(git status -s)" ]; then
return
fi
_git_file
_prsl "file # >>> "
read idx
if [ -z $idx ]; then
return
fi
opts=$(git status -s | sed 's/^[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*//')
line=$(echo $opts | sed "$idx q;d")
name=$(echo $line | sed 's|.*/||')
_prnl opening $name
git diff $line
}
# TODO
_1pass_load() {
if [[ -z $(echo $JIRA_UNAME) ]]; then
_prnl sign in to 1password
export JIRA_UNAME=$(op item get 'BranchSage Credentials' --fields label=jirausername)
if [[ -z $(echo $JIRA_UNAME) ]]; then
return 0
fi
export JIRA_TOKEN=$(op item get 'BranchSage Credentials' --fields label=jiratoken)
export GITHUB_TOKEN=$(op item get 'BranchSage Credentials' --fields label=githubtoken)
export AWS_PREF=$(op item get 'BranchSage Credentials' --fields label=awspref)
fi
return 1
}
#### PREP ####
# SETS UP DIRECTORY, VENV, AND AWS
_egs_prep() {
if _1pass_load; then
return
fi
awsid=$AWS_PREF
_prnl opening directory
cd ~/Dev/es-project/es-site/es
_prnl starting virtual env
source ~/Dev/es-project/venv/bin/activate
if aws sts get-caller-identity 2>&1 | grep -q "Unable to locate\|Token has expired"; then
_prnl login to aws
source ~/Dev/es-dev-utils/aws_login.sh $awsid
fi
}
#### LOGO ####
# PRINTS ASCII ART OF THE ENERGYSAGE LOGO
_egs_logo() {
_prnl '****************************************'
sleep 0.05
_prnl '*****************=. .=*****************'
sleep 0.05
_prnl '**************+- :: -+**************'
sleep 0.05
_prnl '************+- :+**+: -+************'
sleep 0.05
_prnl '***********= :+******+: =***********'
sleep 0.05
_prnl '*********=. :+*****:****+: .=*********'
sleep 0.05
_prnl '*******+: .+******: *****+. :+*******'
sleep 0.05
_prnl '******+. -*******- = -******- =******'
sleep 0.05
_prnl '*****= .+*******= -= =*******+. =*****'
sleep 0.05
_prnl '****= .+*******+ :*= +********+. =****'
sleep 0.05
_prnl '***+ +*******= -**=::: :******+. +***'
sleep 0.05
_prnl '***- -*******+ :=****=: +*******- -***'
sleep 0.05
_prnl '***- =*******: :::=**- =********= -***'
sleep 0.05
_prnl '***+ :**********+ =*: +*********: +***'
sleep 0.05
_prnl '****. =*********= =- =*********= .****'
sleep 0.05
_prnl '****+ =********- = -*********= +****'
sleep 0.05
_prnl '*****+. :+******* :********+: .+*****'
sleep 0.05
_prnl '*******= :=******:*******=: =*******'
sleep 0.05
_prnl '*********=. .:-=++++=-:. .=*********'
sleep 0.05
_prnl '***********+=:. .:=+***********'
sleep 0.05
_prnl '****************++- -++****************'
sleep 0.05
_prnl '******************= =******************'
sleep 0.05
_prnl '******************= =******************'
sleep 0.05
_prnl '****************************************'
}
#### INIT ####
# LOGS RELEVANT DATA TO 1password
_egs_init() {
if [[ -z $(op item get 'BranchSage Credentials' 2>/dev/null) ]]; then
_prnl creating new 1password item
op item create --category apicredential --title 'BranchSage Credentials' --vault 'Private'
fi
_prsl "Jira Email >>> "
read email
if [[ -n $email ]]; then
op item edit 'BranchSage Credentials' jirausername=$email > /dev/null
export JIRA_UNAME=$email
fi
_prsl "Jira API Token >>> "
read token
if [[ -n $token ]]; then
op item edit 'BranchSage Credentials' jiratoken=$token > /dev/null
export JIRA_TOKEN=$token
fi
_prsl "Github API Token >>> "
read gttok
if [[ -n $gttok ]]; then
op item edit 'BranchSage Credentials' githubtoken=$gttok > /dev/null
export GITHUB_TOKEN=$gttok
fi
_prsl "AWS Login Preference (optional) >>> "
read awsid
if [[ -n $awsid ]]; then
op item edit 'BranchSage Credentials' awspref=$awsid > /dev/null
export AWS_PREF=$awsid
fi
op item get 'BranchSage Credentials' | tail -n 4
}
_egs_repo() {
_prnl under construction
# ADD AND WITHDRAW REPO FROM 1PASS
}
#### HELP ####
_egs_help() {
echo to use this tool, type egs followed by a command.
echo you can find a full list here: https://github.com/johnfav03/BranchSage
_prnl try running 'egs logo' for a fun surprise!
}
# BASIC FUNCTIONS
_read_ticks() { cat ~/BranchSage/curr.txt; }
_prnl() { echo $fg[$txtcolor]$@$reset_color; }
_prsl() { echo -n $fg[$txtcolor]$@$reset_color; }