From b02bdf5e4e376ce85cba120ff008f71d6612941c Mon Sep 17 00:00:00 2001 From: jmcgrathTT Date: Wed, 19 Feb 2025 23:37:08 +0000 Subject: [PATCH] working --- .../actions/issue_add_last_updated/Readme.md | 32 +++++ .../actions/issue_add_last_updated/action.yml | 132 ++++++++++++++++++ .gitignore | 2 + 3 files changed, 166 insertions(+) create mode 100644 .github/actions/issue_add_last_updated/Readme.md create mode 100644 .github/actions/issue_add_last_updated/action.yml diff --git a/.github/actions/issue_add_last_updated/Readme.md b/.github/actions/issue_add_last_updated/Readme.md new file mode 100644 index 0000000..188826f --- /dev/null +++ b/.github/actions/issue_add_last_updated/Readme.md @@ -0,0 +1,32 @@ +### Update Project date on issue update +A event automation that updates the tt-forge project date with the last issue date. + +### Testing +##### Manually test has to be done since it diffcult to simulate a GH project at this time. + +Create a branch/fork of this repo and a branch of your desired test repo ex. `tt-milr` + +On the `issue-last-updated.yml`, update the branch reference to your the `tt-github-actions` +```bash +ex. uses: jmcgrathTT/tt-github-actions/.github/actions/${action_name}@${your_tt_github_actions_branch_name} +uses: jmcgrathTT/tt-github-actions/.github/actions/issue_add_last_updated@add-last-updated-workflow +``` + + +In your test repo, create a test issue and your test repo's workflow +```bash +TEST_REPO='tenstorrent/tt-mlir' +TEST_REPO_BRANCH='2165-point-last-updated-workflow-to-tt-github-actions' + +ISSUE_NUMBER=$(gh issue create -R $TEST_REPO --title 'Test issue last updated' --body 'Test issue last updated' | grep -oP '(?<=issues\/)\d+') + +gh workflow run issue-last-updated.yml -R $TEST_REPO --ref $TEST_REPO_BRANCH -f issue_number=$ISSUE_NUMBER +``` + +After you confirmed the workflow passses (in this case `tt-mlir`) you can delete your test issue. + +```bash +gh issue delete -R tenstorrent/tt-mlir $ISSUE_NUMBER +``` + +Be sure to test show your test results in your PR! diff --git a/.github/actions/issue_add_last_updated/action.yml b/.github/actions/issue_add_last_updated/action.yml new file mode 100644 index 0000000..e793ccd --- /dev/null +++ b/.github/actions/issue_add_last_updated/action.yml @@ -0,0 +1,132 @@ +# .github/actions/execute-action/action.yml + +name: Update Project date on issue update +inputs: + project_id: + description: "Github Project ID" + type: string + required: true + field_id: + description: "Github Project Field ID" + type: string + required: true + issue_number: + description: "Github issue number used for testing" + type: string + required: true + gh_token: + description: "Github Token" + type: string + required: true + +runs: + using: "composite" + steps: + + - name: Get Issue ID + id: get_issue_id + shell: bash + run: | + issue_number="${{ inputs.issue_number }}" + echo "Issue number: ${issue_number}" + issue_details=$(curl -H "Authorization: Bearer ${{ inputs.gh_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}" + echo "issue_id=$issue_id" >> $GITHUB_ENV + + - name: Get Item ID for Issue + shell: bash + 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 "${{ inputs.project_id }}" \ + --arg cursor "$CURSOR" \ + '{ query: $query, variables: { projectId: $projectId, cursor: $cursor }}') + + + # Make the GraphQL request + RESPONSE=$(curl -s -X POST -H "Authorization: Bearer ${{ inputs.gh_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." + exit 1 + 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 + shell: bash + if: env.ITEM_ID # Only runs if ITEM_ID was set + run: echo "The ITEM_ID is ${{ env.ITEM_ID }}" + + + - name: Update Project Field + shell: bash + run: | + current_date=$(date +%Y-%m-%d) + curl -H "Authorization: Bearer ${{ inputs.gh_token }}" \ + -H "Content-Type: application/json" \ + -d "{ \"query\": \"mutation { updateProjectV2ItemFieldValue(input: { projectId: \\\"${{ inputs.project_id }}\\\", itemId: \\\"${{ env.ITEM_ID }}\\\", fieldId: \\\"${{ inputs.field_id }}\\\", value: { date: \\\"$current_date\\\" } }) { clientMutationId } }\" }" \ + -X POST \ + "https://api.github.com/graphql" diff --git a/.gitignore b/.gitignore index 13f3a60..b9d3567 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ .github/actions/collect_data/test/data/tt_torch_models/models/** .github/actions/collect_data/generated/** .env +venv/** +venv