Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcgrathTT committed Feb 11, 2025
1 parent 7eefddf commit 4ecf46c
Showing 1 changed file with 19 additions and 118 deletions.
137 changes: 19 additions & 118 deletions .github/workflows/issue-last-updated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,31 @@ name: Update Project Date on Issue Update


on:
workflow_dispatch:
inputs:
issue_number:
description: 'Run workflow against a issue number (Used for testing)'
required: false
default: ""
type: string

issues:
types: [edited, opened]
issue_comment:
types: [created]


env:
GITHUB_TOKEN: ${{ secrets.ISSUE_TOKEN }}


project_id: PVT_kwDOA9MHEM4AjeTl
field_id: PVTF_lADOA9MHEM4AjeTlzgiiU18
jobs:
update_project_date:
issue-last-updated:
runs-on: ubuntu-latest
steps:
- name: Set Environment Variables
run: |
echo "project_id=PVT_kwDOA9MHEM4AjeTl" >> $GITHUB_ENV
echo "field_id=PVTF_lADOA9MHEM4AjeTlzgiiU18" >> $GITHUB_ENV
- name: Get Issue ID
id: get_issue_id
run: |
issue_number=${{ github.event.issue.number }}
issue_details=$(curl -H "Authorization: Bearer ${{ secrets.ISSUE_TOKEN }}" -s "https://api.github.com/repos/${{ github.repository }}/issues/$issue_number")
issue_id=$(echo "$issue_details" | jq -r '.node_id')
echo "issue_id=$issue_id" >> $GITHUB_ENV
- name: Get Item ID for Issue
id: get_item_id_by_issue_id
run: |
# Initialize variables
CURSOR=null
ITEM_ID=""
# Define the 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 "${{ env.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_id
ITEM_ID=$(echo "$RESPONSE" | jq -r --arg issue_id "$issue_id" \
'.data.node.items.nodes[] | select(.content.id==$issue_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"
echo "ITEM_ID=$ITEM_ID" >> $GITHUB_ENV # Save ITEM_ID to environment for future steps
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
- name: Use Found ITEM_ID
if: env.ITEM_ID # Only runs if ITEM_ID was set
run: echo "The ITEM_ID is ${{ env.ITEM_ID }}"


- name: Update Project Field
run: |
current_date=$(date +%Y-%m-%d)
curl -H "Authorization: Bearer ${{ secrets.ISSUE_TOKEN }}" \
-H "Content-Type: application/json" \
-d "{ \"query\": \"mutation { updateProjectV2ItemFieldValue(input: { projectId: \\\"${{ env.project_id }}\\\", itemId: \\\"${{ env.ITEM_ID }}\\\", fieldId: \\\"${{ env.field_id }}\\\", value: { date: \\\"$current_date\\\" } }) { clientMutationId } }\" }" \
-X POST \
"https://api.github.com/graphql"
- name: Run issued last updated
uses: jmcgrathTT/tt-github-actions/.github/actions/add-last-updated@add-last-updated-workflow
with:
project_id: ${{ env.project_id }}
field_id: ${{ env.field_id }}
issue_number: ${{ github.event.issue.number || github.event.inputs.issue_number }}
gh_token: ${{ secrets.ISSUE_TOKEN }}

0 comments on commit 4ecf46c

Please sign in to comment.