From 59db3ca113add32ebca0b9973cbd86b0b7e45eb5 Mon Sep 17 00:00:00 2001 From: Paul Barton <28630076+paulbarton90@users.noreply.github.com> Date: Sun, 24 Dec 2023 18:04:09 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20check=20switch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++ gh-clean-notifications | 83 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 847a144..8c09e00 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,10 @@ The correct scopes can be verified via `gh auth status`. The last line it prints informs you of the "Token scopes". Other requirements: +* `awk` * `gh>=2.40` * `jq` +* `sort` ## Usage @@ -26,6 +28,7 @@ gh clean-notifications [Flags] |-------------|-------------------------------------------------------------------------------------|----------------------------------------------------------| | | Run default clean up | `gh clean-notifications` | | `-h` | Print help | `gh clean-notifications -h` | +| `-c` | Check gh version, and api scopes are all ok | `gh clean-notifications -c` | | `-l NUM` | Change the limit for the number of notifications grabbed per page. Default 50 | `gh clean-notifications -l 20` | | `-q QUERY` | Change the query used for getting the notifications. Default is `is:unread is:read` | `gh clean-notifications -q "is:unread org:paulbarton90"` | | `-s STATES` | The states to mark items as done with. Default is `merged,closed` | `gh clean-notifications -s merged` | diff --git a/gh-clean-notifications b/gh-clean-notifications index ca4996f..e7d3f60 100755 --- a/gh-clean-notifications +++ b/gh-clean-notifications @@ -39,6 +39,7 @@ BASE_QUERY="is:unread is:read" STATES='"CLOSED","MERGED"' BASE_LIMIT=50 VERBOSE=0 +MIN_GH_VERSION="v2.40" help() { echo "gh clean-notifications" @@ -52,20 +53,91 @@ help() { echo " Default: is:unread is:read" echo " -l -> Limit on pagination. Default 50." echo " -s -> Set the states to mark down as. Default is closed, and merged" + echo " -c -> Check that gh is correct version, and scopes are set." echo " -v -> Verbose messaging." } check() { + has_errored=0 has_jq=$(which jq) - has_jq=$? + has_jq_exit=$? if [ ${VERBOSE} -gt 0 ]; then echo "Has jq: ${has_jq}" fi - if [[ $has_jq != "0" ]]; then + if [[ $has_jq_exit != "0" ]]; then echo "jq is not installed. Please install jq." - exit 1 + has_errored=1 fi + + current_gh_ver=$(gh --version | \ + tail -n 1 | \ + awk 'match($0, "\/v.*$"){print substr($0,RSTART+1,RLENGTH)}' + ) + + if [ ${VERBOSE} -gt 0 ]; then + echo "Current gh cli version: ${current_gh_ver}" + fi + + min_version=$(printf "%s\n%s" "${MIN_GH_VERSION}" "${current_gh_ver}" | \ + sort --version-sort | \ + head -n 1 + ) + if [ "${min_version}" != "${MIN_GH_VERSION}" ]; then + echo "Minimum gh version needs to be ${MIN_GH_VERSION}. You have ${current_gh_ver}." + has_errored=1 + fi + + # Check the scopes of the API key to make sure we can run. + scopes_str=$(gh auth status | \ + grep -i "Token scopes" | \ + awk '{ gsub(/\047/, "", $0); split($0,s,": "); gsub(/, /, "\n", s[2]); print s[2] }' + ) + readarray -t scopes < <(echo "${scopes_str}") + + org=0 + notifications=0 + repo=0 + + for scope in "${scopes[@]}"; do + case "${scope}" in + repo) + repo=1;; + read:org) + org=1;; + org) + org=1;; + notifications) + notifications=1;; + esac + done + + if [ ${VERBOSE} -gt 0 ]; then + echo "The scope check returns:" + echo " repo: ${repo}" + echo " org: ${org}" + echo " notification: ${notifications}" + fi + + if [ ${notifications} -eq 0 ] || [ ${org} -eq 0 ] || [ ${repo} -eq 0 ]; then + has_errored=1 + echo "gh scopes are wrong. You need to add the following:" + if [ ${notifications} -eq 0 ]; then + echo " notifications" + fi + if [ ${repo} -eq 0 ]; then + echo " repo" + fi + if [ ${org} -eq 0 ]; then + echo " org:read" + fi + fi + + if [ ${has_errored} -eq 0 ]; then + echo "All OK!" + fi + + exit ${has_errored} } process_notifications() { @@ -95,7 +167,7 @@ process_notifications() { } -while getopts :hq:l:s:v flag +while getopts :hq:l:s:vc flag do case "${flag}" in h) # Display help @@ -133,6 +205,9 @@ do fi done;; + c) + check;; + \?) echo "Error: Invalid option was specified." >&2; echo "" >&2