#Wkf
name: Release zip package

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    runs-on: native
    steps:
      - name: Checkout
        run: |
          git clone "$GITEA_SERVER_URL/$GITEA_REPO.git" repo
          cd repo && git checkout "$GITEA_REF_NAME"
        env:
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_REPO:       ${{ gitea.repository }}
          GITEA_REF_NAME:   ${{ gitea.ref_name }}

      - name: Build zip
        run: |
          cd repo
          VERSION="${{ gitea.ref_name }}"
          ZIP="SharePoint_ToolBox_${VERSION}.zip"
          zip -r "../${ZIP}" Sharepoint_ToolBox.ps1 lang/ examples/
          echo "ZIP=${ZIP}"         >> "$GITHUB_ENV"
          echo "VERSION=${VERSION}" >> "$GITHUB_ENV"

      - name: Create release
        run: |
          RELEASE_ID=$(curl -sf -X POST \
            "${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/releases" \
            -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d "{\"tag_name\":\"${{ env.VERSION }}\",\"name\":\"SharePoint ToolBox ${{ env.VERSION }}\",\"body\":\"1. Download and extract the archive\\n2. Launch Sharepoint_ToolBox.ps1 with PowerShell\\n\"}" \
            | python3 -c "import sys,json; print(json.load(sys.stdin)['id'])")
          echo "RELEASE_ID=${RELEASE_ID}" >> "$GITHUB_ENV"

      - name: Upload asset
        run: |
          curl -sf -X POST \
            "${{ gitea.server_url }}/api/v1/repos/${{ gitea.repository }}/releases/${{ env.RELEASE_ID }}/assets" \
            -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" \
            -F "attachment=@${{ env.ZIP }}"
