Upstream issues tracking #28
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Issue | |
on: | |
issues: | |
types: | |
- opened | |
- edited | |
jobs: | |
labeler: | |
name: Labeler | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
issues: write | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/github-script@v7 | |
with: | |
script: | | |
for (const { section, prefix } of [ | |
{ section: "Scope", prefix: "scope: " }, | |
{ section: "Environment", prefix: "env: " }, | |
]) { | |
const captured = (context.payload.issue.body.match(new RegExp(`### ${section}\\s*(?<labels>.*?)\\s*###`))?.groups?.labels ?? "").trim() | |
console.log(captured, captured.split(",").map(name => `${prefix}${name.trim()}`)) | |
if (!captured) { | |
continue | |
} | |
// Validate labels | |
const { data: allowed } = await github.rest.issues.listLabelsForRepo({ ...context.repo, per_page: 100 }).catch(() => ({ data: [] })) | |
console.log(allowed.map(label => label.name)) | |
const labels = captured.split(",").map(name => `${prefix}${name.trim()}`).filter(name => allowed.some(label => label.name === name)) | |
console.log(`Issue #${context.issue.number} ${prefix}${JSON.stringify(labels)}`) | |
// Add new labels | |
await github.rest.issues.addLabels({ ...context.repo, issue_number: context.issue.number, labels }).catch(() => null) | |
// Remove previous labels | |
const { data: previous } = await github.rest.issues.listLabelsOnIssue({ ...context.repo, issue_number: context.issue.number, per_page: 100 }).catch(() => ({ data: [] })) | |
for (const { name } of previous.filter(label => label.name.startsWith(prefix) && !labels.includes(label.name))) { | |
console.log(`Removing previous label: ${name}`) | |
await github.rest.issues.removeLabel({ ...context.repo, issue_number: context.issue.number, name }) | |
} | |
} |