Skip to content

Commit

Permalink
✨ Add check switch
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbarton90 committed Dec 24, 2023
1 parent c725faa commit 59db3ca
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -26,6 +28,7 @@ gh clean-notifications [Flags]
|-------------|-------------------------------------------------------------------------------------|----------------------------------------------------------|
| <none> | 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` |
Expand Down
83 changes: 79 additions & 4 deletions gh-clean-notifications
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +205,9 @@ do
fi
done;;

c)
check;;

\?)
echo "Error: Invalid option was specified." >&2;
echo "" >&2
Expand Down

0 comments on commit 59db3ca

Please sign in to comment.