Skip to content

Instantly share code, notes, and snippets.

@flipdazed
Created November 12, 2023 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flipdazed/ce40985eefd389237b8e4fb40a67c05f to your computer and use it in GitHub Desktop.
Save flipdazed/ce40985eefd389237b8e4fb40a67c05f to your computer and use it in GitHub Desktop.
This script fetches issues or pull requests from the specified project number with a specific status from the given owner's repository.
#!/bin/bash
# Usage: ./script.sh -o [owner-name] <PROJECT_NUMBER> <STATUS>
#
# This script fetches issues or pull requests from the specified project number with a specific status
# from the given owner's repository.
# The mandatory parameters are 'owner' passed as a flag -o and PROJECT_NUMBER, STATUS passed as positional arguments.
# It also accepts an optional parameter '-j' for JSON output. If provided, it pretty prints the output in markdown format.
# Example: ./script.sh -o my-org 123 "In Progress"
# ./script.sh -o my-org -j json 123 "In Progress"
# Replace 'my-org' with your GitHub organization name, '123' with your project number and 'In Progress' with your status.
# Note: Be careful with case sensitivity!
# Owner of repository
OWNER=""
# Check for optional parameters
while getopts o:j: flag
do
case "${flag}" in
o) OWNER=${OPTARG};;
j) json=${OPTARG};;
esac
done
shift $((OPTIND -1))
# Positional parameters
PROJECT_NUMBER=$1
STATUS=$2
# Find the project id for given project number
PROJECT_ID=$(gh api graphql -f query='
query {
organization(login: "'"${OWNER}"'") {
projectsV2(first: 50) {
nodes {
id
number
}
}
}
}' | jq --argjson PROJECT_NUMBER "$PROJECT_NUMBER" -r '.data.organization.projectsV2.nodes[] | select(.number == $PROJECT_NUMBER) | .id')
# Now use this project id to get issues with specific status
issues=$(gh api graphql -f query='
query {
node(id: "'"${PROJECT_ID}"'") {
... on ProjectV2 {
items(first: 50) {
nodes {
fieldValues(first: 8) {
edges {
node {
... on ProjectV2ItemFieldSingleSelectValue {
name
field{
... on ProjectV2FieldCommon {
name
}
}
item {
... on ProjectV2Item {
content {
... on Issue {
number
}
... on PullRequest {
number
}
}
}
}
}
... on ProjectV2ItemFieldNumberValue {
number
field {
... on ProjectV2FieldCommon{
name
}
}
item {
... on ProjectV2Item {
content {
... on Issue {
number
}
... on PullRequest {
number
}
}
}
}
}
... on ProjectV2ItemFieldRepositoryValue {
repository {
name
}
field{
... on ProjectV2FieldCommon{
name
}
}
}
}
}
}
}
}
}
}
}' | jq '[.data.node.items.nodes[] | {status: (.fieldValues.edges[]? | select(.node.field?.name == "Status") | .node.name), repository: (.fieldValues.edges[]? | select(.node.field?.name == "Repository") | .node.repository.name), number: (.fieldValues.edges[]? | select(.node.field?.name == "Status") | .node.item.content.number)}]')
issues_wip=$(echo $issues | jq -r '[.[] | select(.status == "'"${STATUS}"'")]')
if [[ -z "$issues_wip" ]]; then
echo "Error: No issues with status for Status: $STATUS in Project: $PROJECT_NUMBER"
exit 1
else
if [[ -z "$json" ]]; then
echo $issues_wip
else
echo "All Issues for with Status: "$STATUS" in Project: $PROJECT_NUMBER ..."
echo $issues_wip | jq -r '.[] | "* `'"${OWNER}"'/\(.repository)#\(.number)`: \(.status)"'
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment