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 59b1899
Showing 1 changed file with 74 additions and 4 deletions.
78 changes: 74 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 @@ -57,15 +58,81 @@ help() {

check() {
has_jq=$(which jq)
has_jq=$?
has_jq_exit=$?
has_errored=0
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

exit ${has_errored}
}

process_notifications() {
Expand Down Expand Up @@ -95,7 +162,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 +200,9 @@ do
fi
done;;

c)
check;;

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

0 comments on commit 59b1899

Please sign in to comment.