|
| 1 | +name: Add Issues to Project |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + |
| 8 | +jobs: |
| 9 | + add-to-project: |
| 10 | + name: Add issue to project |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - uses: actions/github-script@v6 |
| 14 | + with: |
| 15 | + github-token: ${{ secrets.ADD_TO_PROJECT_PAT }} |
| 16 | + script: | |
| 17 | + const issue = context.payload.issue; |
| 18 | + |
| 19 | + // Get project ID |
| 20 | + const projectOwner = 'drf-forms'; |
| 21 | + const projectNumber = 1; |
| 22 | + |
| 23 | + // Query for the project ID |
| 24 | + const projectQuery = ` |
| 25 | + query($owner: String!, $number: Int!) { |
| 26 | + user(login: $owner) { |
| 27 | + projectV2(number: $number) { |
| 28 | + id |
| 29 | + fields(first: 20) { |
| 30 | + nodes { |
| 31 | + ... on ProjectV2SingleSelectField { |
| 32 | + id |
| 33 | + name |
| 34 | + options { |
| 35 | + id |
| 36 | + name |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + `; |
| 45 | + |
| 46 | + const projectData = await github.graphql(projectQuery, { |
| 47 | + owner: projectOwner, |
| 48 | + number: projectNumber |
| 49 | + }); |
| 50 | + |
| 51 | + const projectId = projectData.user.projectV2.id; |
| 52 | + |
| 53 | + // Find status field ID and the "Idea" option ID |
| 54 | + let statusFieldId; |
| 55 | + let ideaOptionId; |
| 56 | + |
| 57 | + const fields = projectData.user.projectV2.fields.nodes; |
| 58 | + for (const field of fields) { |
| 59 | + if (field.name === 'Status') { |
| 60 | + statusFieldId = field.id; |
| 61 | + for (const option of field.options) { |
| 62 | + if (option.name === 'Idea') { |
| 63 | + ideaOptionId = option.id; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Add issue to project |
| 72 | + const addToProjectMutation = ` |
| 73 | + mutation($projectId: ID!, $contentId: ID!) { |
| 74 | + addProjectV2ItemById(input: { |
| 75 | + projectId: $projectId |
| 76 | + contentId: $contentId |
| 77 | + }) { |
| 78 | + item { |
| 79 | + id |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + `; |
| 84 | + |
| 85 | + const addResult = await github.graphql(addToProjectMutation, { |
| 86 | + projectId: projectId, |
| 87 | + contentId: issue.node_id |
| 88 | + }); |
| 89 | + |
| 90 | + // If status field and Idea option were found, set the status |
| 91 | + if (statusFieldId && ideaOptionId) { |
| 92 | + const itemId = addResult.addProjectV2ItemById.item.id; |
| 93 | + |
| 94 | + const updateFieldMutation = ` |
| 95 | + mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) { |
| 96 | + updateProjectV2ItemFieldValue(input: { |
| 97 | + projectId: $projectId |
| 98 | + itemId: $itemId |
| 99 | + fieldId: $fieldId |
| 100 | + value: { |
| 101 | + singleSelectOptionId: $optionId |
| 102 | + } |
| 103 | + }) { |
| 104 | + projectV2Item { |
| 105 | + id |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + `; |
| 110 | + |
| 111 | + await github.graphql(updateFieldMutation, { |
| 112 | + projectId: projectId, |
| 113 | + itemId: itemId, |
| 114 | + fieldId: statusFieldId, |
| 115 | + optionId: ideaOptionId |
| 116 | + }); |
| 117 | + } |
0 commit comments