name: Clean up closed issues

on:
  issues:
    types: [closed]

permissions:
  issues: write

jobs:
  remove-labels:
    runs-on: ubuntu-latest
    steps:
      - name: Remove feedback label
        uses: actions/github-script@v9
        with:
          script: |
            const issue = context.payload.issue;
            const hasLabel = issue.labels.some(l => l.name === 'feedback');

            if (hasLabel) {
              try {
                await github.rest.issues.removeLabel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  issue_number: issue.number,
                  name: 'feedback'
                });
                console.log(`Removed 'feedback' label from issue #${issue.number}`);
              } catch (error) {
                if (error.status === 404) {
                  console.log(`Label 'feedback' already removed from issue #${issue.number}`);
                } else {
                  throw error;
                }
              }
            }
