|
| 1 | +# Workflow: Automatically set project status to "Review Needed" when a reviewer is requested |
| 2 | +name: Set Project Status on Review Request |
| 3 | + |
| 4 | +# Trigger: Runs whenever a reviewer is requested on a pull request |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + types: [review_requested] |
| 8 | + |
| 9 | +jobs: |
| 10 | + update-project-status: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Update Project Status to Review Needed |
| 14 | + uses: actions/github-script@v7 |
| 15 | + with: |
| 16 | + # Use the PAT stored in repository secrets (has project write access) |
| 17 | + github-token: ${{ secrets.PROJECT_TOKEN }} |
| 18 | + script: | |
| 19 | + // GraphQL query to find the PR's project items |
| 20 | + // This fetches all projects the PR is linked to |
| 21 | + const query = ` |
| 22 | + query($owner: String!, $repo: String!, $pr: Int!) { |
| 23 | + repository(owner: $owner, name: $repo) { |
| 24 | + pullRequest(number: $pr) { |
| 25 | + projectItems(first: 10) { |
| 26 | + nodes { |
| 27 | + id |
| 28 | + project { |
| 29 | + number |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + `; |
| 37 | +
|
| 38 | + // Execute the query with current repo/PR context |
| 39 | + const result = await github.graphql(query, { |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + pr: context.payload.pull_request.number |
| 43 | + }); |
| 44 | +
|
| 45 | + // Find the project item that belongs to project #8 |
| 46 | + const projectItems = result.repository.pullRequest.projectItems.nodes; |
| 47 | + const projectItem = projectItems.find(item => item.project.number === 8); |
| 48 | +
|
| 49 | + // Exit early if PR isn't in project #8 |
| 50 | + if (!projectItem) { |
| 51 | + console.log('PR is not in project #8, skipping...'); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + // GraphQL mutation to update the Status field |
| 56 | + const mutation = ` |
| 57 | + mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) { |
| 58 | + updateProjectV2ItemFieldValue( |
| 59 | + input: { |
| 60 | + projectId: $projectId |
| 61 | + itemId: $itemId |
| 62 | + fieldId: $fieldId |
| 63 | + value: { singleSelectOptionId: $optionId } |
| 64 | + } |
| 65 | + ) { |
| 66 | + projectV2Item { |
| 67 | + id |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + `; |
| 72 | +
|
| 73 | + // Execute the mutation using IDs stored in repository variables |
| 74 | + // PROJECT_ID: The project's unique identifier |
| 75 | + // STATUS_FIELD_ID: The "Status" field's unique identifier |
| 76 | + // REVIEW_NEEDED_OPTION_ID: The "Review Needed" option's unique identifier |
| 77 | + await github.graphql(mutation, { |
| 78 | + projectId: "${{ secrets.PROJECT_ID }}", |
| 79 | + itemId: projectItem.id, |
| 80 | + fieldId: "${{ secrets.STATUS_FIELD_ID }}", |
| 81 | + optionId: "${{ secrets.REVIEW_NEEDED_OPTION_ID }}" |
| 82 | + }); |
| 83 | +
|
| 84 | + console.log('Successfully updated project status to Review Needed'); |
0 commit comments