Checking sync-status-across-projects.yml #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update Issue Status | |
on: | |
pull_request: | |
types: [closed] | |
jobs: | |
update-status: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Check for issue number in PR title or description | |
- name: Check for issue number in PR title or description | |
id: check-pr-for-issue | |
run: | | |
pr_title="${{ github.event.pull_request.title }}" | |
pr_body="${{ github.event.pull_request.body }}" | |
issue_number=$(echo "$pr_title" "$pr_body" | grep -oE '#[0-9]+' | head -n 1 | sed 's/#//') | |
if [ -z "$issue_number" ]; then | |
echo "No issue number found in PR title or description." | |
exit 1 | |
fi | |
echo "Issue number found: $issue_number" | |
echo "issue_number=$issue_number" >> $GITHUB_ENV | |
# Step 2: Get organization name, project ID, status field ID, and done option ID | |
- name: Retrieve project details | |
id: get-project-details | |
run: | | |
org_name="${{ github.repository_owner }}" | |
project_number="1314" | |
echo "Fetching details for organization: $org_name, project number: $project_number" | |
# GraphQL query to get project fields (including "Status" field and "Done" option) | |
query="{\"query\": \"{ organization(login: \\\"$org_name\\\") { projectV2(number: $project_number) { id fields(first: 20) { nodes { ... on ProjectV2SingleSelectField { id name options { id name } } } } } } }\"}" | |
echo "Query being sent to GraphQL API: $query" | |
response=$(curl -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" \ | |
-X POST \ | |
-H "Content-Type: application/json" \ | |
-d "$query" \ | |
https://api.github.com/graphql) | |
echo "GraphQL Response: $response" | |
# Extracting the status field ID and done option ID from the response | |
status_field_id=$(echo $response | jq -r '.data.organization.projectV2.fields.nodes[] | select(.name == "Status") | .id') | |
done_option_id=$(echo $response | jq -r '.data.organization.projectV2.fields.nodes[] | select(.name == "Status") | .options[] | select(.name == "Done") | .id') | |
echo "Status Field ID: $status_field_id" | |
echo "Done Option ID: $done_option_id" | |
# Set environment variables for later steps | |
echo "status_field_id=$status_field_id" >> $GITHUB_ENV | |
echo "done_option_id=$done_option_id" >> $GITHUB_ENV | |
# Step 3: Get Issue ID (itemId) from project | |
- name: Get Issue ID from project | |
id: get-issue-id | |
run: | | |
issue_number=${{ env.issue_number }} | |
project_number="1314" | |
query="{\"query\": \"{ organization(login: \\\"${{ github.repository_owner }}\\\") { projectV2(number: $project_number) { items(first: 100, orderBy: {field: POSITION, direction: DESC}) { nodes { id content { ... on Issue { number id } } } } } } }\"}" | |
response=$(curl -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" \ | |
-X POST \ | |
-H "Content-Type: application/json" \ | |
-d "$query" \ | |
https://api.github.com/graphql) | |
issue_id=$(echo $response | jq -r ".data.organization.projectV2.items.nodes[] | select(.content.number == $issue_number) | .id") | |
if [ -z "$issue_id" ]; then | |
echo "Issue ID not found for issue #$issue_number." | |
exit 1 | |
fi | |
echo "Issue ID: $issue_id" | |
echo "issue_id=$issue_id" >> $GITHUB_ENV | |
# Step 4: Update 'Status' field of the issue to 'Done' | |
- name: Update Issue Status to 'Done' | |
run: | | |
issue_id=${{ env.issue_id }} | |
project_number="1314" | |
status_field_id="${{ env.status_field_id }}" | |
done_option_id="${{ env.done_option_id }}" | |
echo "Updating issue #$issue_number status to 'Done'" | |
# Query to get the global project ID | |
project_query="{\"query\": \"{ organization(login: \\\"department-of-veterans-affairs\\\") { projectV2(number: $project_number) { id } } }\"}" | |
# Get the project ID | |
project_response=$(curl -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" \ | |
-X POST \ | |
-H "Content-Type: application/json" \ | |
-d "$project_query" \ | |
https://api.github.com/graphql) | |
project_id=$(echo $project_response | jq -r '.data.organization.projectV2.id') | |
if [ -z "$project_id" ]; then | |
echo "Project ID not found for project number $project_number." | |
exit 1 | |
fi | |
echo "Found global project ID: $project_id" | |
# GraphQL Mutation to update status | |
mutation_query="{\"query\": \"mutation { updateProjectV2ItemFieldValue(input: { projectId: \\\"$project_id\\\", itemId: \\\"$issue_id\\\", fieldId: \\\"$status_field_id\\\", value: { singleSelectOptionId: \\\"$done_option_id\\\" } }) { projectV2Item { id content { ... on Issue { id number title } } } } }\"}" | |
response=$(curl -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" \ | |
-X POST \ | |
-H "Content-Type: application/json" \ | |
-d "$mutation_query" \ | |
https://api.github.com/graphql) | |
echo "Response: $response" | |