name: Manage PR Labels Based on Review Status

on:
    pull_request_review:
        types: [ submitted ]
    pull_request:
        types: [ review_requested, ready_for_review ]

jobs:
    manage-labels:
        runs-on: ubuntu-22.04

        steps:
            -   name: Checkout repository
                uses: actions/checkout@v2

            # Accepted
            -   name: Add 'Status Accepted'
                if: ${{ github.event.action == 'submitted' && github.event.review.state == 'approved' }}
                uses: actions-ecosystem/action-add-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Accepted"

            -   name: Remove 'Status Review Needed' on approval
                if: ${{ github.event.action == 'submitted' && github.event.review.state == 'approved' }}
                uses: actions-ecosystem/action-remove-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Review Needed"

            -   name: Remove 'Status Changes Requested' on approval
                if: ${{ github.event.action == 'submitted' && github.event.review.state == 'approved' }}
                uses: actions-ecosystem/action-remove-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Changes Requested"

            # Changes requested
            -   name: Add 'Status Changes Requested'
                if: ${{ github.event.action == 'submitted' && github.event.review.state == 'changes_requested' }}
                uses: actions-ecosystem/action-add-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Changes Requested"

            -   name: Remove 'Status Review Needed' on changes requested
                if: ${{ github.event.action == 'submitted' && github.event.review.state == 'changes_requested' }}
                uses: actions-ecosystem/action-remove-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Review Needed"

            -   name: Remove 'Status Accepted' on changes requested
                if: ${{ github.event.action == 'submitted' && github.event.review.state == 'changes_requested' }}
                uses: actions-ecosystem/action-remove-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Accepted"

            # Review needed
            -   name: Add 'Status Review Needed'
                if: ${{ github.event.action == 'review_requested' || github.event.action == 'ready_for_review' }}
                uses: actions-ecosystem/action-add-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Review Needed"

            -   name: Remove 'Status Accepted' on review request
                if: ${{ github.event.action == 'review_requested' || github.event.action == 'ready_for_review' }}
                uses: actions-ecosystem/action-remove-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Accepted"

            -   name: Remove 'Status Changes Requested' on review request
                if: ${{ github.event.action == 'review_requested' || github.event.action == 'ready_for_review' }}
                uses: actions-ecosystem/action-remove-labels@v1
                with:
                    github_token: ${{ secrets.GITHUB_TOKEN }}
                    labels: "Status: Changes Requested"
