|
| 1 | +name: Label New Issues |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + label-new-issue: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/checkout@v2 |
| 12 | + |
| 13 | + - name: Label New Issue |
| 14 | + uses: actions/github-script@v6 |
| 15 | + with: |
| 16 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 17 | + script: | |
| 18 | + const fs = require('fs').promises; |
| 19 | + |
| 20 | + async function parseYaml(filePath) { |
| 21 | + const content = await fs.readFile(filePath, 'utf8'); |
| 22 | + const lines = content.split('\n'); |
| 23 | + const result = {}; |
| 24 | + let currentKey = null; |
| 25 | + |
| 26 | + for (const line of lines) { |
| 27 | + const trimmedLine = line.trim(); |
| 28 | + if (trimmedLine === '' || trimmedLine.startsWith('#')) continue; |
| 29 | + |
| 30 | + if (trimmedLine.includes(':')) { |
| 31 | + [currentKey, value] = trimmedLine.split(':').map(s => s.trim()); |
| 32 | + result[currentKey] = []; |
| 33 | + if (value) { |
| 34 | + result[currentKey].push(value.replace(/^['"]|['"]$/g, '')); |
| 35 | + } |
| 36 | + } else if (trimmedLine.startsWith('-') && currentKey) { |
| 37 | + let value = trimmedLine.slice(1).trim(); |
| 38 | + value = value.replace(/^['"]|['"]$/g, ''); |
| 39 | + result[currentKey].push(value); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + async function addLabelsToIssue(octokit, owner, repo, issueNumber, labels) { |
| 47 | + await octokit.rest.issues.addLabels({ |
| 48 | + owner: owner, |
| 49 | + repo: repo, |
| 50 | + issue_number: issueNumber, |
| 51 | + labels: labels |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + async function labelNewIssue() { |
| 56 | + try { |
| 57 | + console.log('Reading label rules file...'); |
| 58 | + const labelRules = await parseYaml('.github/label-rules.yml'); |
| 59 | + console.log('Parsed label rules:', JSON.stringify(labelRules, null, 2)); |
| 60 | + |
| 61 | + const issue = context.payload.issue; |
| 62 | + const title = issue.title.toLowerCase(); |
| 63 | + const labelsToAdd = []; |
| 64 | + |
| 65 | + for (const [label, keywords] of Object.entries(labelRules)) { |
| 66 | + if (keywords.some(keyword => { |
| 67 | + if (keyword.startsWith('/') && keyword.endsWith('/')) { |
| 68 | + const regex = new RegExp(keyword.slice(1, -1), 'i'); |
| 69 | + return regex.test(title); |
| 70 | + } |
| 71 | + return title.includes(keyword.toLowerCase()); |
| 72 | + })) { |
| 73 | + labelsToAdd.push(label); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (labelsToAdd.length > 0) { |
| 78 | + await addLabelsToIssue(github, context.repo.owner, context.repo.repo, issue.number, labelsToAdd); |
| 79 | + console.log(`Added labels to new issue #${issue.number}: ${labelsToAdd.join(', ')}`); |
| 80 | + } else { |
| 81 | + console.log(`No matching labels found for new issue #${issue.number}`); |
| 82 | + } |
| 83 | + } catch (error) { |
| 84 | + console.error('Error in workflow:', error.message); |
| 85 | + console.error('Error stack:', error.stack); |
| 86 | + core.setFailed(error.message); |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + labelNewIssue(); |
0 commit comments