name: Publish

concurrency:
  group: "publish"

on:
  push:
    branches:
      - "release/*"
jobs:
  publish:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    if: ${{ github.event.repository.full_name == 'findkit/wp-findkit' }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: valu-digital/slack-action@master
        with:
          token: ${{ secrets.SLACK_ACCESS_TOKEN }}
          channel: "${{ secrets.SLACK_CHANNEL }}"

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: "8.2"

      - name: Configure git
        run: |
          set -eu
          git config user.email "action@github.com"
          git config user.name "${{ github.actor }}"

      - name: Run composer install
        run: composer install --no-dev --optimize-autoloader

      - name: Run npm install
        run: |
          set -eu
          npm ci

      - name: Build and commit wp-scripts build
        run: |
          set -eu
          npm run build-commit

      - name: Update version number and tag
        run: |
          set -eu
          set -x

          echo "$GITHUB_REF"
          next_version=$(echo "$GITHUB_REF" | cut -d / -f 4)
          echo "version:$next_version"

          next_tag="v$next_version"

          prev_tag="$(git describe --abbrev=0 --match "v*" || true)"
          prev_version=$(echo "$prev_tag" | sed -E "s/v([0-9\.]+)\$/\1/")

          echo "prev_version:$prev_version"

          link_ver="$(echo $next_version | tr -cd '[:alnum:]')"
          changelog_link="https://github.com/${GITHUB_REPOSITORY}/blob/${{ github.event.repository.default_branch }}/CHANGELOG.md#v${link_ver}"

          if [ "$next_version" = "$prev_version" ]; then
            echo "Cannot release $prev_version again"
            exit 9
          fi

          sed -E -i "s/(.*Version:) .*/\1 ${next_version}/" plugin.php
          sed -E -i "s/(Stable tag:) .*/\1 ${next_version}/" readme.txt

          git add plugin.php readme.txt
          git commit -m "Update version numbers to v${next_version}"

          git tag -a "$next_tag" -m "v$next_version"


          echo "next_version=$next_version" >> $GITHUB_ENV
          echo "next_tag=$next_tag" >> $GITHUB_ENV
          echo "changelog_link=$changelog_link" >> $GITHUB_ENV

      - name: WordPress Plugin Deploy
        id: wp-deploy
        uses: 10up/action-wordpress-plugin-deploy@stable
        with:
          generate-zip: true
        env:
          SLUG: findkit
          SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
          SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
          VERSION: ${{ env.next_version }}

      - name: Push tags and commits
        run: |
          set -eu
          set -x

          git push origin HEAD:${{ github.event.repository.default_branch }}
          git push origin --tags

      - name: Create Github Release entry
        uses: actions/github-script@v6
        with:
          script: |
            let body = `Changelog ${process.env.changelog_link}\n\n`;
            body += 'by @${{ github.actor }}\n\n';

            const release = await github.rest.repos.createRelease({
              draft: false,
              body,
              generate_release_notes: false,
              name: process.env.next_tag,
              owner: context.repo.owner,
              prerelease: false,
              repo: context.repo.repo,
              tag_name: process.env.next_tag,
            });

            core.exportVariable("RELEASE_ID", release.data.id)

      - name: Make release zip
        run: zip -r /tmp/release.zip . -x "node_modules/*" -x ".git/*"

      - name: Upload zip to release
        run: |
          set -eux
          curl -L \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            -H "Content-Type: application/octet-stream" \
            https://uploads.github.com/repos/${{ github.event.repository.full_name }}/releases/${RELEASE_ID}/assets?name=findkit-vendor-v${next_version}.zip \
            --data-binary "@${{ steps.wp-deploy.outputs.zip-path }}"

      - name: Notify publish fail
        if: failure()
        run: |
          set -eu
          slack-message '<!channel> Failed to publish findkit/wp-findkit'
