| # This workflow allows any user to add one of the `awaiting-review`, | |
| # `awaiting-author`, `WIP`, `release-ci`, `lake-ci`, or a `changelog-XXX` label | |
| # by commenting on the PR or issue. If any labels from the set | |
| # {`awaiting-review`, `awaiting-author`, `WIP`} are added, other labels from | |
| # that set are removed automatically at the same time. Similarly, if any | |
| # `changelog-XXX` label is added, other `changelog-YYY` labels are removed. | |
| # | |
| # It also allows adding the `downstream`/`downstream-force` labels by commenting | |
| # them, but only for accounts with write access to `leanprover/downstream-lean4` | |
| # (see `update-downstream-label` below), since we only want PR toolchains of | |
| # trusted users running in that repo. | |
| name: Label PR based on Comment | |
| on: | |
| issue_comment: | |
| types: [created] | |
| jobs: | |
| update-label: | |
| if: > | |
| github.event.issue.pull_request != null && | |
| ( | |
| contains(github.event.comment.body, 'awaiting-review') || | |
| contains(github.event.comment.body, 'awaiting-author') || | |
| contains(github.event.comment.body, 'WIP') || | |
| contains(github.event.comment.body, 'release-ci') || | |
| contains(github.event.comment.body, 'lake-ci') || | |
| contains(github.event.comment.body, 'changelog-') | |
| ) | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Add label based on comment | |
| uses: actions/github-script@v9 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const { owner, repo, number: issue_number } = context.issue; | |
| const commentLines = context.payload.comment.body.split(/\r?\n/); | |
| const awaitingReview = commentLines.includes('awaiting-review'); | |
| const awaitingAuthor = commentLines.includes('awaiting-author'); | |
| const wip = commentLines.includes('WIP'); | |
| const releaseCI = commentLines.includes('release-ci'); | |
| const lakeCI = commentLines.includes('lake-ci'); | |
| const changelogMatch = commentLines.find(line => line.startsWith('changelog-')); | |
| if (awaitingReview || awaitingAuthor || wip) { | |
| await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'awaiting-review' }).catch(() => {}); | |
| await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'awaiting-author' }).catch(() => {}); | |
| await github.rest.issues.removeLabel({ owner, repo, issue_number, name: 'WIP' }).catch(() => {}); | |
| } | |
| if (awaitingReview) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['awaiting-review'] }); | |
| } | |
| if (awaitingAuthor) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['awaiting-author'] }); | |
| } | |
| if (wip) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['WIP'] }); | |
| } | |
| if (releaseCI) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['release-ci'] }); | |
| } | |
| if (lakeCI) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['lake-ci'] }); | |
| } | |
| if (changelogMatch) { | |
| const changelogLabel = changelogMatch.trim(); | |
| const { data: existingLabels } = await github.rest.issues.listLabelsOnIssue({ owner, repo, issue_number }); | |
| const changelogLabels = existingLabels.filter(label => label.name.startsWith('changelog-')); | |
| // Remove all other changelog labels | |
| for (const label of changelogLabels) { | |
| if (label.name !== changelogLabel) { | |
| await github.rest.issues.removeLabel({ owner, repo, issue_number, name: label.name }).catch(() => {}); | |
| } | |
| } | |
| // Add the new changelog label | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [changelogLabel] }); | |
| } | |
| update-downstream-label: | |
| if: > | |
| github.event.issue.pull_request != null && | |
| contains(github.event.comment.body, 'downstream') | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create GitHub App token | |
| uses: actions/create-github-app-token@v3 | |
| id: app-token | |
| with: | |
| client-id: ${{ vars.DOWNSTREAM_LEAN4_APP_CLIENT_ID }} | |
| private-key: ${{ secrets.DOWNSTREAM_LEAN4_APP_PRIVATE_KEY }} | |
| repositories: lean4,downstream-lean4 | |
| - name: Add label based on comment | |
| uses: actions/github-script@v9 | |
| with: | |
| github-token: ${{ steps.app-token.outputs.token }} | |
| script: | | |
| const { owner, repo, number: issue_number } = context.issue; | |
| const commentLines = context.payload.comment.body.split(/\r?\n/); | |
| const downstream = commentLines.includes('downstream'); | |
| const downstreamForce = commentLines.includes('downstream-force'); | |
| if (!(downstream || downstreamForce)) { | |
| console.log(`Comment does not contain any of the downstream labels, skipping.`); | |
| return; | |
| } | |
| // Limit to users with write access to `leanprover/downstream-lean4` | |
| const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ | |
| owner: 'leanprover', | |
| repo: 'downstream-lean4', | |
| username: context.payload.comment.user.login, | |
| }); | |
| const allowed = ['admin', 'write', 'maintain'].includes(data.permission); | |
| if (!allowed) { | |
| console.log(`User ${context.payload.comment.user.login} does not have sufficient permissions to add downstream labels.`); | |
| return; | |
| } | |
| if (downstream) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['downstream'] }); | |
| } | |
| if (downstreamForce) { | |
| await github.rest.issues.addLabels({ owner, repo, issue_number, labels: ['downstream-force'] }); | |
| } |