name: Generate testing snapshot
on: 
  push:
    branches: [develop]

jobs:
  Clean:
    name: Create testing release
    runs-on: windows
    steps:
      - name: Diagnostics
        run: |
          echo "repository: ${{ gitea.repository }}"
          echo "ref:  ${{ gitea.ref }}"
          echo "ref_name: ${{ gitea.ref_name }}"
          echo "sha: ${{ gitea.sha }}"
          echo "api_url: ${{ gitea.api_url }}"
          echo "workflow: ${{ gitea.workflow }}"

      - name: Checkout framework
        uses: actions/checkout@v3
        with:
          path: MissionFramework

      - name: Clean framework
        run: |
          Remove-Item -Recurse MissionFramework\aaFrameworkUtils
          Remove-Item -Recurse MissionFramework\framework\x
          Remove-Item -Recurse MissionFramework\.git*

      - name: Backup artifacts from runner
        uses: actions/upload-artifact@v3
        with:
          name: framework-test-snapshot
          path: MissionFramework

      - name: Create archive
        run: Compress-Archive MissionFramework framework-test-snapshot.zip

      - name: Delete old testing-release release
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token ${{ gitea.token }}"
          }
          Invoke-WebRequest -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/releases/tags/testing-release" -Method Delete -Headers $headers

      - name: Delete old testing-release tag
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token ${{ gitea.token }}"
          }
          Invoke-WebRequest -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/tags/testing-release" -Method Delete -Headers $headers

      - name: Create new testing-release tag
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token ${{ gitea.token }}"
          }
          Invoke-WebRequest -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/tags" -Method Post -Headers $headers -ContentType "application/json" -Body "{`"message`": `"testing-release`", `"tag_name`": `"testing-release`", `"target`": `"${{ gitea.sha }}`"}"

      - name: Create new testing-release
        uses: https://gitea.com/actions/gitea-release-action@v1.3.0
        with:
          tag_name: 'testing-release'
          name: 'testing-release'
          prerelease: true
          files: |-
            framework-test-snapshot.zip
