Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions .github/workflows/auto-label-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ name: auto-label-issues
on:
issues:
types: [opened]
# TEST ONLY. Do not merge. Run this workflow on a test pull request.
pull_request:
types: [opened, edited, synchronize]

jobs:
auto-label-issues:
runs-on: ubuntu-latest
permissions:
issues: write
# TEST ONLY. A label on a pull request needs this permission.
pull-requests: write
steps:
- name: Analyze issue content
- name: Analyze issue content and apply labels
id: analyze_content
uses: actions/github-script@v7
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_TITLE: ${{ github.event.issue.title }}
ISSUE_TITLE: ${{ github.event.issue.title || github.event.pull_request.title }}
ISSUE_BODY: ${{ github.event.issue.body }}
with:
script: |
Expand Down Expand Up @@ -211,15 +216,35 @@ jobs:
labels.push('Training');
}

core.setOutput('labels', labels.join(','));
core.setOutput('has_labels', labels.length > 0);
// =============================================================================
// APPLY LABELS
// Call the API from this script. Do not pass values to a shell step.
// Apply only the labels that exist in the repository. The name match ignores case.
// =============================================================================
if (labels.length > 0) {
const repoLabels = await github.paginate(github.rest.issues.listLabelsForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});
const existingLabels = new Map(repoLabels.map(l => [l.name.toLowerCase(), l.name]));

- name: Apply labels to issue
if: steps.analyze_content.outputs.has_labels == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
IFS=',' read -ra LABELS <<< "${{ steps.analyze_content.outputs.labels }}"
for label in "${LABELS[@]}"; do
gh issue edit ${{ github.event.issue.number }} --add-label "$label" -R ${{ github.repository }}
done
const labelsToApply = [];
for (const label of labels) {
const name = existingLabels.get(label.toLowerCase());
if (name) {
labelsToApply.push(name);
} else {
core.warning(`Label "${label}" does not exist in the repository. Create the label to apply it.`);
}
}

if (labelsToApply.length > 0) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: labelsToApply
});
}
}
Loading