|
| 1 | +name: Update Database Team Project Fields |
| 2 | + |
| 3 | +env: |
| 4 | + PROJECT_NUMBER: '3' |
| 5 | + DOMAIN_LABELS: '["domain::database", "domain::core"]' |
| 6 | + STATUS_FIELD_NAME: 'Status' |
| 7 | + STATUS_TODO: 'Todo' |
| 8 | + |
| 9 | +on: |
| 10 | + issues: |
| 11 | + types: [labeled, opened] |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + issue_number: |
| 15 | + description: 'Issue number to process' |
| 16 | + required: true |
| 17 | + type: number |
| 18 | + |
| 19 | +jobs: |
| 20 | + update-project-fields: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Add Issue to Project |
| 24 | + uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + github-token: ${{ secrets.DATABASE_PROJECT_WORKFLOW_TOKEN }} |
| 27 | + script: | |
| 28 | + const issueNumber = context.eventName === 'workflow_dispatch' |
| 29 | + ? ${{ github.event.inputs.issue_number || 0 }} |
| 30 | + : context.payload.issue.number; |
| 31 | +
|
| 32 | + console.log(`Processing Issue #${issueNumber} (event: ${context.eventName}/${context.payload.action || 'manual'})`); |
| 33 | +
|
| 34 | + // ============================================================ |
| 35 | + // FETCH ALL DATA WITH SINGLE GRAPHQL QUERY |
| 36 | + // ============================================================ |
| 37 | +
|
| 38 | + const data = await github.graphql(` |
| 39 | + query($owner: String!, $repo: String!, $issue: Int!, $projectNumber: Int!) { |
| 40 | + organization(login: $owner) { |
| 41 | + projectV2(number: $projectNumber) { |
| 42 | + id |
| 43 | + fields(first: 20) { |
| 44 | + nodes { |
| 45 | + ... on ProjectV2SingleSelectField { |
| 46 | + id |
| 47 | + name |
| 48 | + options { id, name } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + repository(owner: $owner, name: $repo) { |
| 55 | + issue(number: $issue) { |
| 56 | + id |
| 57 | + labels(first: 20) { |
| 58 | + nodes { name } |
| 59 | + } |
| 60 | + projectItems(first: 10) { |
| 61 | + nodes { |
| 62 | + id |
| 63 | + project { id } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + `, { |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + issue: issueNumber, |
| 73 | + projectNumber: parseInt('${{ env.PROJECT_NUMBER }}') |
| 74 | + }); |
| 75 | +
|
| 76 | + const project = data.organization.projectV2; |
| 77 | + const issue = data.repository.issue; |
| 78 | +
|
| 79 | + // ============================================================ |
| 80 | + // CHECK DOMAIN LABELS (early exit if not relevant) |
| 81 | + // ============================================================ |
| 82 | +
|
| 83 | + const labels = issue.labels.nodes.map(l => l.name); |
| 84 | + const domainLabels = ${{ env.DOMAIN_LABELS }}; |
| 85 | + const hasDomainLabel = labels.some(label => |
| 86 | + domainLabels.some(domain => label.startsWith(domain)) |
| 87 | + ); |
| 88 | +
|
| 89 | + if (!hasDomainLabel) { |
| 90 | + console.log(`Issue #${issueNumber} has no domain label (${labels.join(', ') || 'none'}), skipping`); |
| 91 | + return; |
| 92 | + } |
| 93 | +
|
| 94 | + console.log(`Issue has domain label: ${labels.filter(l => l.startsWith('domain::')).join(', ')}`); |
| 95 | +
|
| 96 | + // ============================================================ |
| 97 | + // CHECK IF ALREADY IN PROJECT |
| 98 | + // ============================================================ |
| 99 | +
|
| 100 | + const existingItem = issue.projectItems.nodes.find( |
| 101 | + item => item.project.id === project.id |
| 102 | + ); |
| 103 | +
|
| 104 | + if (existingItem) { |
| 105 | + console.log('Issue is already in the project'); |
| 106 | + return; |
| 107 | + } |
| 108 | +
|
| 109 | + // ============================================================ |
| 110 | + // ADD ISSUE TO PROJECT |
| 111 | + // ============================================================ |
| 112 | +
|
| 113 | + const addResult = await github.graphql(` |
| 114 | + mutation($projectId: ID!, $contentId: ID!) { |
| 115 | + addProjectV2ItemById(input: { |
| 116 | + projectId: $projectId |
| 117 | + contentId: $contentId |
| 118 | + }) { |
| 119 | + item { |
| 120 | + id |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + `, { |
| 125 | + projectId: project.id, |
| 126 | + contentId: issue.id |
| 127 | + }); |
| 128 | +
|
| 129 | + const itemId = addResult.addProjectV2ItemById.item.id; |
| 130 | + console.log(`Issue added to project (item: ${itemId})`); |
| 131 | +
|
| 132 | + // ============================================================ |
| 133 | + // SET STATUS TO TODO |
| 134 | + // ============================================================ |
| 135 | +
|
| 136 | + const statusField = project.fields.nodes.find(f => f.name === '${{ env.STATUS_FIELD_NAME }}'); |
| 137 | + const todoOption = statusField?.options.find(o => o.name === '${{ env.STATUS_TODO }}'); |
| 138 | +
|
| 139 | + if (!statusField || !todoOption) { |
| 140 | + console.log('Warning: Status field or Todo option not found'); |
| 141 | + return; |
| 142 | + } |
| 143 | +
|
| 144 | + await github.graphql(` |
| 145 | + mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: ProjectV2FieldValue!) { |
| 146 | + updateProjectV2ItemFieldValue(input: { |
| 147 | + projectId: $projectId |
| 148 | + itemId: $itemId |
| 149 | + fieldId: $fieldId |
| 150 | + value: $value |
| 151 | + }) { |
| 152 | + projectV2Item { id } |
| 153 | + } |
| 154 | + } |
| 155 | + `, { |
| 156 | + projectId: project.id, |
| 157 | + itemId, |
| 158 | + fieldId: statusField.id, |
| 159 | + value: { singleSelectOptionId: todoOption.id } |
| 160 | + }); |
| 161 | +
|
| 162 | + console.log(`Status → ${{ env.STATUS_TODO }}`); |
| 163 | + console.log('Completed!'); |
0 commit comments