name: Remove outdated labels
on:
  pull_request_target:
    types:
      - closed
      - converted_to_draft
      - ready_for_review

permissions: {}

jobs:
  remove-closed-pr-labels:
    name: Remove closed and merged pull request labels
    if: github.event.action == 'closed'
    runs-on: ubuntu-slim

    permissions:
      pull-requests: write

    steps:
      - name: Remove labels
        shell: bash
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_URL: ${{ github.event.pull_request.html_url }}
        run: |
          labels='PR: waiting for review,'
          labels+='PR: WIP,'
          labels+='PR: changes requested,'
          labels+='PR: merge conflicts / rebase needed,'
          labels+='PR/Issue: dependent,'
          labels+='PR: stale'

          gh pr edit "$PR_URL" --remove-label "$labels"

  remove-draft-pr-labels:
    name: Remove labels from draft pull requests
    if: |
      github.event.action == 'converted_to_draft' && contains(github.event.pull_request.labels.*.name, 'PR: waiting for review')
    runs-on: ubuntu-slim

    permissions:
      pull-requests: write

    steps:
      - name: Remove label
        shell: bash
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_URL: ${{ github.event.pull_request.html_url }}
        run: |
          gh pr edit "$PR_URL" --remove-label 'PR: waiting for review'

  remove-ready-pr-labels:
    name: Remove labels when draft pr is marked ready for review
    if: |
      github.event.action == 'ready_for_review' && contains(github.event.pull_request.labels.*.name, 'PR: WIP')
    runs-on: ubuntu-slim

    permissions:
      pull-requests: write

    steps:
      - name: Remove label
        shell: bash
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_URL: ${{ github.event.pull_request.html_url }}
        run: |
          gh pr edit "$PR_URL" --remove-label 'PR: WIP'
