diff options
Diffstat (limited to '.github/workflows')
| -rw-r--r-- | .github/workflows/label-intends-to-pr.yml | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/.github/workflows/label-intends-to-pr.yml b/.github/workflows/label-intends-to-pr.yml new file mode 100644 index 00000000..11136ec3 --- /dev/null +++ b/.github/workflows/label-intends-to-pr.yml @@ -0,0 +1,91 @@ +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@v7 + 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\.\s*$/m; + const uncheckedIntentPattern = /^\s*-\s*\[ \]\s*I intend to open a PR for this change\.\s*$/m; + const shouldAddLabel = checkedIntentPattern.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 checkbox 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}.`); + } |
