Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding pagination to find item ids #1238

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

staylorTT
Copy link
Contributor

The existing workflow will only work if the issue ID is found in the first 100 ITEM_IDs.

These updates should enable pagination to occur through all the existing issues.

I attempt to use items(last: 100, before: $cursor) {
under the assumption that it would be faster to find the latest issues, however this functionality wasn't working with the API when I was testing it locally.

I got this code to run locally and paginate through the issues as expected(code below so this workflow update is to add this to the get_item_id_by_issue_id section of the workflow.

Please let me know what feedback or if you think there is a better way to accomplish this.

#!/bin/bash

# Replace these with your actual values
GITHUB_TOKEN="YOUR_GITHUB_TOKEN"
PROJECT_ID="PVT_kwDOA9MHEM4AjeTl"  # Example Project ID
ISSUE_NODE_ID="I_kwDOMKpl8c6eFjOV"  # Example Issue Node ID to search for

# Initialize variables
CURSOR="null"
ITEM_ID=""

# Define GraphQL query as a string
QUERY='query($projectId: ID!, $cursor: String) {
  node(id: $projectId) {
    ... on ProjectV2 {
      items(first: 100, after: $cursor) {
        nodes {
          id
          content {
            ... on Issue {
              id
            }
          }
        }
        pageInfo {
          hasNextPage
          endCursor
        }
      }
    }
  }
}'

while : ; do
  # Construct JSON payload using jq for proper formatting
  JSON_PAYLOAD=$(jq -n \
    --arg query "$QUERY" \
    --arg projectId "$PROJECT_ID" \
    --arg cursor "$CURSOR" \
    '{ query: $query, variables: { projectId: $projectId, cursor: $cursor }}')

  # Make the GraphQL request
  RESPONSE=$(curl -s -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
                       -H "Content-Type: application/json" \
                       -d "$JSON_PAYLOAD" \
                       https://api.github.com/graphql)

  # Debug: print entire response
  echo "RESPONSE: $RESPONSE"

  # Check if the response contains `items` data
  ITEMS_DATA=$(echo "$RESPONSE" | jq -r '.data.node.items.nodes' 2>/dev/null)
  if [[ "$ITEMS_DATA" == "null" ]]; then
    echo "Error: Items data not found. Please check your PROJECT_ID and GITHUB_TOKEN permissions."
    break
  fi

  # Parse the item ID if it matches the ISSUE_NODE_ID
  ITEM_ID=$(echo "$RESPONSE" | jq -r --arg ISSUE_NODE_ID "$ISSUE_NODE_ID" \
             '.data.node.items.nodes[] | select(.content.id==$ISSUE_NODE_ID) | .id')

  # If ITEM_ID is found, output it and stop the loop
  if [[ -n "$ITEM_ID" && "$ITEM_ID" != "null" ]]; then
    echo "Found ITEM_ID: $ITEM_ID"
    break
  fi

  # Extract pagination information
  HAS_NEXT_PAGE=$(echo "$RESPONSE" | jq -r '.data.node.items.pageInfo.hasNextPage')
  CURSOR=$(echo "$RESPONSE" | jq -r '.data.node.items.pageInfo.endCursor')

  # If no more pages, exit loop
  if [[ "$HAS_NEXT_PAGE" != "true" ]]; then
    echo "Issue not found in project items."
    break
  fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant