name: Label contributor intent on: issues: types: [opened, edited] permissions: issues: write env: INTENDS_TO_PR_LABEL: intends-to-pr jobs: label: if: ${{ !github.event.issue.pull_request }} runs-on: ubuntu-latest steps: - name: Sync intends-to-pr label uses: actions/github-script@v9 with: script: | const label = process.env.INTENDS_TO_PR_LABEL; const body = context.payload.issue.body || ''; const issueNumber = context.payload.issue.number; const { owner, repo } = context.repo; const checkedIntentPattern = /^\s*-\s*\[[xX]\]\s*I intend to open a PR for this (?:change|issue)\.\s*$/m; const uncheckedIntentPattern = /^\s*-\s*\[ \]\s*I intend to open a PR for this (?:change|issue)\.\s*$/m; const cliIntentPattern = /^\s*\/i-intend-to-pr\s*$/m; const shouldAddLabel = checkedIntentPattern.test(body) || cliIntentPattern.test(body); const shouldRemoveLabel = uncheckedIntentPattern.test(body); const labels = context.payload.issue.labels.map((item) => typeof item === 'string' ? item : item.name, ); const hasLabel = labels.includes(label); if (shouldAddLabel && hasLabel) { console.log(`${label} is already present on #${issueNumber}.`); return; } if (shouldRemoveLabel && !hasLabel) { console.log(`${label} is already absent from #${issueNumber}.`); return; } if (!shouldAddLabel && !shouldRemoveLabel) { console.log(`No contribution intent signal found for #${issueNumber}.`); return; } if (shouldAddLabel) { try { await github.rest.issues.getLabel({ owner, repo, name: label }); } catch (error) { if (error.status !== 404) { throw error; } await github.rest.issues.createLabel({ owner, repo, name: label, color: '0E8A16', description: 'Contributor intends to open a PR for this issue.', }); } await github.rest.issues.addLabels({ owner, repo, issue_number: issueNumber, labels: [label], }); console.log(`Added ${label} to #${issueNumber}.`); return; } try { await github.rest.issues.removeLabel({ owner, repo, issue_number: issueNumber, name: label, }); console.log(`Removed ${label} from #${issueNumber}.`); } catch (error) { if (error.status !== 404) { throw error; } console.log(`${label} was already absent from #${issueNumber}.`); }