|
| 1 | +name: Sync PR status to project |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, reopened, closed, ready_for_review, converted_to_draft] |
| 6 | + |
| 7 | +jobs: |
| 8 | + update-project: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Update project item status |
| 12 | + uses: actions/github-script@v7 |
| 13 | + with: |
| 14 | + github-token: ${{ secrets.ADD_TO_PROJECT_PAT }} |
| 15 | + script: | |
| 16 | + const pr = context.payload.pull_request; |
| 17 | + const projectId = "PVT_kwDOBa9-UM4ApBgm"; |
| 18 | + const statusFieldId = "PVTSSF_lADOBa9-UM4ApBgmzgggAKY"; |
| 19 | + const statusOptions = { |
| 20 | + "Backlog": "558f5162", |
| 21 | + "Todo": "6b029af1", |
| 22 | + "In Progress": "2c5afb50", |
| 23 | + "In Review": "6c721920", |
| 24 | + "Blocked": "90d338b2", |
| 25 | + "Done": "21877bf4", |
| 26 | + }; |
| 27 | +
|
| 28 | + let targetStatus; |
| 29 | + if (pr.merged) { |
| 30 | + targetStatus = "Done"; |
| 31 | + } else if (pr.state === "closed") { |
| 32 | + return; |
| 33 | + } else if (pr.draft) { |
| 34 | + targetStatus = "In Progress"; |
| 35 | + } else { |
| 36 | + targetStatus = "In Review"; |
| 37 | + } |
| 38 | +
|
| 39 | + const query = `query($id: ID!) { |
| 40 | + node(id: $id) { |
| 41 | + ... on ProjectV2 { |
| 42 | + items(first: 100) { |
| 43 | + nodes { |
| 44 | + id |
| 45 | + content { |
| 46 | + ... on PullRequest { number repository { name owner { login } } } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + }`; |
| 53 | + const result = await github.graphql(query, { id: projectId }); |
| 54 | + const item = result.node.items.nodes.find(n => |
| 55 | + n.content?.number === pr.number && |
| 56 | + n.content?.repository?.name === context.repo.repo && |
| 57 | + n.content?.repository?.owner?.login === context.repo.owner |
| 58 | + ); |
| 59 | + if (!item) { |
| 60 | + console.log("PR not found in project, skipping"); |
| 61 | + return; |
| 62 | + } |
| 63 | +
|
| 64 | + const mutation = `mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) { |
| 65 | + updateProjectV2ItemFieldValue(input: { |
| 66 | + projectId: $projectId |
| 67 | + itemId: $itemId |
| 68 | + fieldId: $fieldId |
| 69 | + value: { singleSelectOptionId: $optionId } |
| 70 | + }) { projectV2Item { id } } |
| 71 | + }`; |
| 72 | + await github.graphql(mutation, { |
| 73 | + projectId, |
| 74 | + itemId: item.id, |
| 75 | + fieldId: statusFieldId, |
| 76 | + optionId: statusOptions[targetStatus], |
| 77 | + }); |
| 78 | + console.log("Updated PR #" + pr.number + " to " + targetStatus); |
0 commit comments