|
| 1 | +name: Auto Label Issues |
| 2 | +on: |
| 3 | + issues: |
| 4 | + types: [opened] |
| 5 | + |
| 6 | +permissions: |
| 7 | + issues: write |
| 8 | + contents: read |
| 9 | + |
| 10 | +jobs: |
| 11 | + triage: |
| 12 | + if: github.repository == 'circlefin/arc-node' |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Check opener eligibility |
| 16 | + id: check-opener |
| 17 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 18 | + with: |
| 19 | + script: | |
| 20 | + const opener = context.payload.issue.user.login; |
| 21 | + const association = context.payload.issue.author_association; |
| 22 | + const allowedAssociations = ['MEMBER', 'OWNER', 'COLLABORATOR']; |
| 23 | +
|
| 24 | + if (allowedAssociations.includes(association)) { |
| 25 | + console.log(`Opener ${opener} has allowed association ${association}`); |
| 26 | + core.setOutput('skip_label', 'true'); |
| 27 | + return; |
| 28 | + } |
| 29 | +
|
| 30 | + let codeownerUsernames = []; |
| 31 | + try { |
| 32 | + const response = await github.rest.repos.getContent({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + path: '.github/CODEOWNERS', |
| 36 | + ref: context.payload.repository.default_branch |
| 37 | + }); |
| 38 | + const content = Buffer.from(response.data.content, 'base64').toString('utf-8'); |
| 39 | + const codeowners = content.match(/@[\w-]+/g) || []; |
| 40 | + codeownerUsernames = codeowners.map(c => c.substring(1).toLowerCase()); |
| 41 | + } catch (error) { |
| 42 | + console.log(`Could not read CODEOWNERS: ${error.message}`); |
| 43 | + } |
| 44 | +
|
| 45 | + if (codeownerUsernames.includes(opener.toLowerCase())) { |
| 46 | + console.log(`Opener ${opener} is a codeowner`); |
| 47 | + core.setOutput('skip_label', 'true'); |
| 48 | + return; |
| 49 | + } |
| 50 | +
|
| 51 | + console.log(`Opener ${opener} needs triage label`); |
| 52 | + core.setOutput('skip_label', 'false'); |
| 53 | +
|
| 54 | + - name: Add need-triage label |
| 55 | + if: steps.check-opener.outputs.skip_label == 'false' |
| 56 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 57 | + with: |
| 58 | + script: | |
| 59 | + try { |
| 60 | + await github.rest.issues.addLabels({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + issue_number: context.issue.number, |
| 64 | + labels: ['need-triage'] |
| 65 | + }); |
| 66 | + console.log('Successfully added `need-triage` label'); |
| 67 | + } catch (error) { |
| 68 | + console.log('Error adding label:', error); |
| 69 | + } |
0 commit comments