Skip to content

Commit 8806c0f

Browse files
author
Giles Westwood
committed
port over latest github commands
1 parent 0f6e9d4 commit 8806c0f

1 file changed

Lines changed: 152 additions & 10 deletions

File tree

lib/github.sh

Lines changed: 152 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ alias ghpr="gh pr create"
66
alias al=sk-github-actionlint
77
alias ghal=sk-github-actionlint
88

9+
github_poke_slack_channel='tech-ops'
10+
github_version=2.54.0
11+
12+
913
sk-github-actionlint(){
1014
local file=${1:default}
1115
sk-asdf-install actionlint -p actionlint -v 1.6.15 --plugin_git_url git@github.com:crazy-matt/asdf-actionlint.git
@@ -34,8 +38,61 @@ sk-gitflow-conf(){
3438

3539
alias gfi=sk-github-flow-init
3640

41+
sk-github-repo(){
42+
git config --get remote.origin.url | perl -ne '/\/(.*)\.git/ && print $1'
43+
}
44+
45+
sk-github-cache-list(){
46+
47+
local github_org=$(sk-github-org)
48+
local github_repo=$(sk-github-repo)
49+
50+
# List all cache entries
51+
local cache_ids=$(gh api -H "Accept: application/vnd.github.v3+json" \
52+
/repos/$github_org/$github_repo/actions/caches)
53+
54+
echo "$cache_ids"
55+
}
56+
57+
sk-github-cache-list-ids(){
58+
59+
local github_org=$(sk-github-org)
60+
local github_repo=$(sk-github-repo)
61+
sk-asdf-install jq -p jq -v 1.6
62+
# List all cache entries
63+
local cache_ids=$(gh api -H "Accept: application/vnd.github.v3+json" \
64+
/repos/$github_org/$github_repo/actions/caches \
65+
| jq -r '.actions_caches[].id')
66+
67+
echo "$cache_ids"
68+
}
69+
70+
sk-github-clean-cache(){
71+
local cache_ids=$(sk-github-cache-list-ids)
72+
local github_org=$(sk-github-org)
73+
local github_repo=$(sk-github-repo)
74+
# Delete each cache entry
75+
for cache_id in $cache_ids; do
76+
echo "Deleting cache ID: $cache_id"
77+
gh api -X DELETE -H "Accept: application/vnd.github.v3+json" \
78+
/repos/$github_org/$github_repo/actions/caches/$cache_id
79+
done
80+
}
81+
82+
sk-github-pr-check-runs(){
83+
default_pr=$(_sk-github-pr-number-branch)
84+
local pr_number=${1:-$default_pr}
85+
commit_sha=$(gh pr view $pr_number --json commits --jq '.commits[-1].oid')
86+
echo $commit_sha
87+
_sk-github-config $(sk-github-org)
88+
sk-asdf-install jq -p jq -v 1.6
89+
90+
gh api repos/$(sk-github-org)/$(sk-github-repo)/commits/$commit_sha/check-runs --jq '.check_runs[] | {name, status, conclusion}'
91+
}
92+
93+
3794
sk-github-flow-init(){
38-
sk-asdf-install gh -p github-cli -v 2.13.0
95+
sk-asdf-install gh -p github-cli -v $github_version
3996
sk-gitflow-conf
4097
_sk-github-config $(sk-github-org)
4198

@@ -65,7 +122,7 @@ alias prp=sk-gitgub-pr-poke
65122
alias poke=sk-gitgub-pr-poke
66123

67124
sk-gitgub-pr-poke(){
68-
sk-asdf-install gh -p github-cli -v 2.13.0
125+
sk-asdf-install gh -p github-cli -v $github_version
69126
pr_url=$(_sk-github-pr-url-branch)
70127
if [[ "$pr_url" == '' ]];then
71128
sk-github-pr-create
@@ -79,15 +136,15 @@ sk-gitgub-pr-poke(){
79136
}
80137

81138
sk-github-pr-merge(){
82-
sk-asdf-install gh -p github-cli -v 2.13.0
139+
sk-asdf-install gh -p github-cli -v $github_version
83140
pr_url=$(_sk-github-pr-url-branch)
84141
gh pr merge $pr_url --merge
85142
}
86143

87144
alias review=sk-github-pr-review
88145

89146
sk-github-pr-review(){
90-
sk-asdf-install gh -p github-cli -v 2.13.0
147+
sk-asdf-install gh -p github-cli -v $github_version
91148
pr_url=$(_sk-github-pr-url-branch)
92149

93150
if [[ "$pr_url" == '' ]];then
@@ -121,9 +178,19 @@ sk-github-is-fork(){
121178
fi
122179
}
123180

181+
alias prm=sk-github-pr-merge
182+
183+
sk-github-pr-merge(){
184+
sk-asdf-install gh -p github-cli -v $github_version
185+
sk-gitflow-conf
186+
187+
sk-github-pr-create
188+
gh pr merge -m
189+
}
190+
124191
alias pr=sk-github-pr-create
125192
sk-github-pr-create(){
126-
sk-asdf-install gh -p github-cli -v 2.13.0
193+
sk-asdf-install gh -p github-cli -v $github_version
127194
sk-gitflow-conf
128195

129196
trello_id='unset'
@@ -213,7 +280,10 @@ sk-github-pr-create(){
213280
return
214281
fi
215282
sk-git-commit-force "$github_title"
216-
pr_details=$(gh pr create $github_base_arg $gf_base_branch $github_reviewer_arg --title "$github_title" --body-file "$body_file" 2> $pr_err_file)
283+
if ! pr_details=$(gh pr create $github_base_arg $gf_base_branch $github_reviewer_arg --title "$github_title" --body-file "$body_file" 2> $pr_err_file);then
284+
echo "unknown error" ; echo "pr creation failed with $(cat $pr_err_file)"; return
285+
fi
286+
217287
else
218288
echo "unknown error" ; echo "pr creation failed with $(cat $pr_err_file)"; return
219289
fi
@@ -280,21 +350,74 @@ sk-git-creds(){
280350

281351
alias ghw=sk-github-run-workflow-with-logs
282352

353+
354+
_sk-github-tf-parse-apply(){
355+
cat "$tf_apply_file" | grep 'Apply Plan' | cut -d' ' -f7-
356+
}
357+
358+
_sk-github-tf-parse-plan(){
359+
tf_plan_id=$(cat $tf_run_file | grep 'Run ID for this workflow is' | tail -n1 | sed 's/.*: //')
360+
echo "tf_plan_id=$tf_plan_id"
361+
362+
tf_plan_changes=$(cat $tf_run_file | cut -d' ' -f7- | awk '/perform the following actions:/ {found=1; next} found' | awk '/plan to: planfile/ {exit} {print}' )
363+
echo "tf_plan_changes:"
364+
echo "$tf_plan_changes"
365+
}
366+
367+
sk-github-tf-plan-run(){
368+
gh workflow run 'Terraform Plan'
369+
}
370+
371+
sk-github-tf-plan-view(){
372+
tmp_dir=$(sk-tmp-userdir)
373+
tf_run_file="$tmp_dir/tf.run"
374+
sk-github-workflow-latest 'Terraform Plan' > $tf_run_file
375+
_sk-github-tf-parse-plan
376+
}
377+
378+
sk-github-tf-apply-view(){
379+
tmp_dir=$(sk-tmp-userdir)
380+
tf_apply_file="$tmp_dir/tf.apply"
381+
sk-github-workflow-latest 'Terraform Apply' > $tf_apply_file
382+
_sk-github-tf-parse-apply
383+
echo "tf_apply_file=$tf_apply_file"
384+
}
385+
386+
sk-github-tf-apply-sleep(){
387+
echo "sleeping 100s"
388+
sleep 100
389+
sk-github-tf-plan-view
390+
if sk-prompt-confirm;then
391+
gh workflow run 'Terraform Apply' -f terraform_plan_checked=Yes -f plan-run-id="$tf_plan_id"
392+
fi
393+
}
394+
395+
sk-github-tf-apply(){
396+
sk-github-tf-plan-view
397+
if sk-prompt-confirm;then
398+
gh workflow run 'Terraform Apply' -f terraform_plan_checked=Yes -f plan-run-id="$tf_plan_id"
399+
fi
400+
}
401+
283402
sk-github-run-workflow-with-logs(){
284403
local workflow=${1:-default}
285404
_sk-github-config $(sk-github-org)
286-
gh workflow run $workflow
405+
gh workflow run "$workflow"
287406
latest_job_id=$(gh run list | head -1 | awk -F'\t' '{print $7}')
288407
gh run view $latest_job_id --log
289408
}
290409

291410
sk-github-workflow-latest(){
292411
_sk-github-config
293-
latest_job_id=$(gh run list | head -1 | awk -F'\t' '{print $7}')
412+
local workflow=${1:-all}
413+
if [[ "$workflow" = 'all' ]];then
414+
latest_job_id=$(gh run list | head -1 | awk -F'\t' '{print $7}')
415+
else
416+
latest_job_id=$(gh run list --workflow "$workflow" | head -1 | awk -F'\t' '{print $7}')
417+
fi
294418
gh run view $latest_job_id --log
295419
}
296420

297-
298421
_sk-github_curl(){
299422
curl -s -H "Accept: application/vnd.github.v3+json" -u "$GITHUB_USERNAME:$GITHUB_TOKEN" "$@"
300423
}
@@ -332,7 +455,7 @@ _sk-github-config(){
332455
}
333456

334457
sk-github-runner-ips(){
335-
sk-asdf-install gh -p github-cli -v 2.13.0 --silent
458+
sk-asdf-install gh -p github-cli -v $github_version --silent
336459
sk-asdf-install jq -p jq -v 1.6 --silent
337460

338461
gh api -H "Accept: application/vnd.github+json" /meta | jq -r .actions | grep -E '[0-9]\/' | sed 's/["|,| ]//g' | sk-ip-merge
@@ -454,3 +577,22 @@ sk-github-create-repo(){
454577
gh repo create $namespace/$repo $visibility_arg
455578

456579
}
580+
581+
sk-github-dependabot-alerts(){
582+
sk_help "
583+
Usage: $FUNCNAME
584+
585+
Description:
586+
587+
Lists all the dependabot alerts for the ORCID org, in tab separated format.
588+
You may need to enable the 'repos' permission for your github token.
589+
590+
Options:
591+
592+
" "$@" && return 1
593+
594+
sk-asdf-install gh -p github-cli -v $github_version --silent
595+
sk-asdf-install jq -p jq -v 1.6 --silent
596+
597+
gh api --paginate /orgs/ORCID/dependabot/alerts | jq -rs '{critical: 1, high: 2, medium: 3, low: 4} as $levels | flatten | sort_by(.repository.name, $levels[.security_advisory.severity]) | .[] | .repository.name + "\t" + .dependency.package.ecosystem + "\t" + .dependency.package.name + "\t" + .security_advisory.severity + "\t" + .html_url'
598+
}

0 commit comments

Comments
 (0)