name: Clean Preview

#! Safety:
#! this workflow should not execute any untrusted input at all
#! see the docs on `pull_request_target` for more
on:
    pull_request_target:
        types: [unlabeled]

jobs:
    clean:
        runs-on: ubuntu-latest
        if: github.event.label.name == 'use-preview'
        env:
            BASE: refs/pull/${{ github.event.pull_request.number }}

        steps:
            - uses: actions/checkout@v2
              with:
                  ref: build-previews
                  persist-credentials: false

            - name: clean previews
              run: rm -rf "$BASE"

            - name: publish cleaned previews
              uses: JamesIves/github-pages-deploy-action@4.1.5
              with:
                  folder: .
                  branch: build-previews
                  commit-message: "Cleaning up build result for #${{ github.event.pull_request.number }}"

            - name: send comment
              uses: marocchino/sticky-pull-request-comment@v2
              with:
                  header: Preview environment
                  message: |
                      ## Preview environment
                      There is no longer a preview enviroment for this pull request due to the `use-preview` label being removed
