| 1 | +name: π― Issues/PRs Open Handler |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + pull_request_target: |
| 7 | + types: [opened] |
| 8 | + |
| 9 | +jobs: |
| 10 | + label-maintainer: |
| 11 | + name: Label if Author is a Repo Maintainer |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Label if Author is a Repo Maintainer |
| 16 | + # GitHub Script |
| 17 | + # https://github.com/marketplace/actions/github-script |
| 18 | + uses: actions/github-script@v6 |
| 19 | + with: |
| 20 | + script: | |
| 21 | + // Get basic info |
| 22 | + const { owner, repo } = context.repo; |
| 23 | + const payload = context.payload; |
| 24 | + // Determine if it's an issue or a pull request |
| 25 | + const eventData = payload.issue || payload.pull_request; |
| 26 | + const username = eventData.user.login; |
| 27 | + const issue_number = eventData.number; |
| 28 | + |
| 29 | + // Get the collaborator permission level for the user |
| 30 | + const { data: permissionData } = await github.repos.getCollaboratorPermissionLevel({ |
| 31 | + owner, |
| 32 | + repo, |
| 33 | + username, |
| 34 | + }); |
| 35 | + |
| 36 | + const permission = permissionData.permission; |
| 37 | + console.log(`User ${username} has permission level: ${permission}`); |
| 38 | + |
| 39 | + // Check if user is an admin or has maintain permissions |
| 40 | + if (['admin', 'maintain'].includes(permission)) { |
| 41 | + console.log(`Adding "Maintainer" label to issue/PR #${issue_number}`); |
| 42 | + await github.issues.addLabels({ |
| 43 | + owner, |
| 44 | + repo, |
| 45 | + issue_number, |
| 46 | + labels: ['Maintainer'], |
| 47 | + }); |
| 48 | + } else { |
| 49 | + console.log(`User ${username} is not a repo maintainer.`); |
| 50 | + } |