name: Publish snapshot

on:
  workflow_run:
    workflows:
      - Snapshot
    types:
      - completed

jobs:
  upload:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    steps:
      - name: Download artifact
        uses: actions/github-script@v8
        with:
          script: |
            var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
              owner: context.repo.owner,
              repo: context.repo.repo,
              run_id: ${{github.event.workflow_run.id }},
            });
            var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "dist"
            })[0];
            var download = await github.rest.actions.downloadArtifact({
              owner: context.repo.owner,
              repo: context.repo.repo,
              artifact_id: matchArtifact.id,
              archive_format: 'zip',
            });
            var fs = require('fs');
            fs.writeFileSync('${{github.workspace}}/dist.zip', Buffer.from(download.data));
      - name: Unzip artifact
        run: |
          mkdir dist && cd dist
          unzip ../dist.zip
          mv NAME ..
          mv SHA ..
          mv PR .. || true
      - name: Publish artifacts
        run: |
          NAME="$(cat NAME)"
          cd dist
          rm -rf */ nextdns.rb config.yaml checksums.txt
          version="$(ls -1 nextdns_*-SNAPSHOT-*_linux_amd64.tar.gz)"
          version="${version#*-}"
          version="$NAME/${version%%_*}"
          for f in *; do mv $f "nextdns-SNAPSHOT${f#*SNAPSHOT}"; done
          (
            echo "<html><body>"
            echo "<h2>Install</h2>"
            echo "<code><pre>NEXTDNS_VERSION=$version sh -c 'sh -c \"\$(curl -sL https://nextdns.io/install)\"'</pre></code>"
            echo "<h2>Files</h2>"
            for f in *; do
              if [ "$f" != "index.html" ]; then
                echo "<a href=\"$f\">$f</a><br>"
              fi
            done
            echo "</html>"
          ) > index.html
          aws s3 sync --no-progress --delete \
            --storage-class REDUCED_REDUNDANCY --acl public-read \
            . "s3://${AWS_BUCKET}/${NAME}/"
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.S3_REPO_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_REPO_SECRET }}
          AWS_DEFAULT_REGION: us-east-1
          AWS_BUCKET: snapshot.nextdns.io
      - name: Comment Pull Request
        uses: actions/github-script@v8
        if: github.event.workflow_run.event == 'pull_request'
        with:
          script: |
            const fs = require('fs'),
                  issue_number = Number(fs.readFileSync('PR')),
                  name = fs.readFileSync('NAME');
            github.rest.issues.createComment({
              issue_number: issue_number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `📷 <a href="https://snapshot.nextdns.io/${name}/index.html">Snapshot created</a>`
            })
