|
| 1 | +name: Label Pull Requests |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: # zizmor: ignore[dangerous-triggers] this is needed to label PRs from forks |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - reopened |
| 8 | + - unlabeled |
| 9 | + |
| 10 | +permissions: {} |
| 11 | + |
| 12 | +jobs: |
| 13 | + labeler: |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + pull-requests: write |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # v5.0.0 |
| 20 | + |
| 21 | + size-pr: |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + pull-requests: write |
| 25 | + runs-on: ubuntu-latest |
| 26 | + name: Label the PR size |
| 27 | + steps: |
| 28 | + - uses: codelytv/pr-size-labeler@1c3422395d899286d5ee2c809fd5aed264d5eb9b # v1.10.2 |
| 29 | + with: |
| 30 | + files_to_ignore: | |
| 31 | + "protocol/**/*" |
| 32 | + "docs/**/*" |
| 33 | +
|
| 34 | + external-contributor: |
| 35 | + permissions: |
| 36 | + pull-requests: write |
| 37 | + runs-on: ubuntu-latest |
| 38 | + steps: |
| 39 | + - name: Check Author Association and Label PR |
| 40 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7.0.1 |
| 41 | + with: |
| 42 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 43 | + script: | |
| 44 | + const pr = context.payload.pull_request; |
| 45 | + if (!pr) { |
| 46 | + core.error("Could not get PR from context"); |
| 47 | + return; |
| 48 | + } |
| 49 | +
|
| 50 | + // Define associations considered "internal" |
| 51 | + const internalAssociations = ["MEMBER"]; |
| 52 | +
|
| 53 | + // Label to add if the author is external |
| 54 | + const externalLabel = "external-contributor"; |
| 55 | +
|
| 56 | + const authorAssociation = pr.author_association; |
| 57 | + const isExternal = !internalAssociations.includes(authorAssociation); |
| 58 | +
|
| 59 | + const prLabels = pr.labels.map(label => label.name); |
| 60 | + const hasExternalLabel = prLabels.includes(externalLabel); |
| 61 | +
|
| 62 | + core.info(`Event: ${context.eventName}, Action: ${context.payload.action}`); |
| 63 | + core.info(`Author: ${pr.user.login}, Association: ${authorAssociation}, Is External: ${isExternal}`); |
| 64 | + core.info(`Current PR Labels: ${prLabels.join(', ')}`); |
| 65 | +
|
| 66 | + // Logic for 'unlabeled' event: only re-add if *our* label was removed and author is still external |
| 67 | + if (context.eventName === 'pull_request_target' && context.payload.action === 'unlabeled') { |
| 68 | + const removedLabel = context.payload.label.name; |
| 69 | + core.info(`Label removed: ${removedLabel}`); |
| 70 | + if (removedLabel === externalLabel && isExternal) { |
| 71 | + core.info(`External label was removed, author is still external. Re-adding label: ${externalLabel}`); |
| 72 | + await github.rest.issues.addLabels({ |
| 73 | + owner: context.repo.owner, |
| 74 | + repo: context.repo.repo, |
| 75 | + issue_number: pr.number, |
| 76 | + labels: [externalLabel] |
| 77 | + }); |
| 78 | + core.info(`Label "${externalLabel}" re-added successfully.`); |
| 79 | + } else { |
| 80 | + core.info(`Removed label was not the external label or author is not external. No action needed for unlabeled event.`); |
| 81 | + } |
| 82 | + } |
| 83 | + // Logic for 'opened' or 'reopened' events: add label if external and not already present |
| 84 | + else if (['opened', 'reopened'].includes(context.payload.action)) { |
| 85 | + if (isExternal && !hasExternalLabel) { |
| 86 | + core.info(`Author association "${authorAssociation}" is external and label is missing. Adding label: ${externalLabel}`); |
| 87 | + await github.rest.issues.addLabels({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + issue_number: pr.number, |
| 91 | + labels: [externalLabel] |
| 92 | + }); |
| 93 | + core.info(`Label "${externalLabel}" added successfully.`); |
| 94 | + } else if (isExternal && hasExternalLabel) { |
| 95 | + core.info(`Author is external, but label "${externalLabel}" is already present.`); |
| 96 | + } else { |
| 97 | + if (hasExternalLabel) { |
| 98 | + core.warning(`Author association "${authorAssociation}" is internal. However, external contributor label is present.`) |
| 99 | + } else { |
| 100 | + core.info(`Author association "${authorAssociation}" is internal. No label added.`) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
0 commit comments