diff --git a/.github/workflows/auto-label-issues.yml b/.github/workflows/auto-label-issues.yml index ca921a7..37ef9cc 100644 --- a/.github/workflows/auto-label-issues.yml +++ b/.github/workflows/auto-label-issues.yml @@ -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: | @@ -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 + }); + } + }