Skip to content

Instantly share code, notes, and snippets.

@wajda
Last active April 1, 2022 17:56
Show Gist options
  • Save wajda/176a748fb2d27ad2d4ad5a4e3f1ea751 to your computer and use it in GitHub Desktop.
Save wajda/176a748fb2d27ad2d4ad5a4e3f1ea751 to your computer and use it in GitHub Desktop.
List the GitHub Project (beta) workflow statuses' names and IDs
#!/bin/bash
: '
Copyright 2022 ABSA Group Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'
# Check the number of command line arguments and print usage if needed
if [ "$#" -ne 2 ]; then
if [ "$#" -gt 0 ]; then
echo "Error: Invalid number of arguments"
fi
echo "Usage: $(basename "$0") <owner> <project-number>"
exit 1
fi
# Validate the 1st command line argument [owner]
if [[ $1 =~ ^[^/]+$ ]]; then
_org="$1"
else
echo "Error: Invalid format of owner: $1"
exit 1
fi
# Validate the 2nd command line argument [project-number]
if [[ $2 -gt 0 ]]; then
_prj_num=$2
else
echo "Error: GitHub Project number must be a positive integer: $2"
exit 1
fi
# Check if the 'gh' command is available and the user is logged in
echo "Checking GitHub CLI..."
gh auth status
if [ ! $? -eq 0 ]; then
exit 1
fi
# Fetch the project information from GitHub
_prj_info=$(gh api graphql -f org="$_org" -F prj_number="$_prj_num" -f query='
query ($org: String!, $prj_number: Int!) {
organization(login: $org) {
projectNext(number: $prj_number) {
id
title
fields(first: 100) {
nodes {
id
name
settings
}
}
}
}
}
' | jq -r '.data.organization.projectNext | select (.!=null)')
if [ $? -eq 0 ] && [ ! -z "$_prj_info" ]; then
_fld_status_info=$(echo "$_prj_info" | jq -r '.fields.nodes | map(select(.name=="Status")) | .[0]')
_fld_status_settings=$(echo "$_fld_status_info" | jq -r '.settings')
printf "[Status ID]\t[Status Name]\n"
printf -- '-%.0s' {1..30}; printf "\n"
echo "$_fld_status_settings" | jq -r '.options | map(.id + "\t" + .name) | join("\n")'
else
echo "Error: Failed to fetch project information from '$_org': $_prj_num"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment