name: Build flow
on:
  push:
   branches-ignore:
     - "master"
  pull_request:
    types:
      - "closed"
jobs:
  build-project:
    name: "Build project"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Setup node env
        uses: actions/setup-node@v3
        with:
          node-version: "18"
          cache: "yarn"
          cache-dependency-path: "./assets/frontend"
      - name: Yarn install dependencies
        working-directory: "./assets/frontend"
        run: yarn install
      - name: Run frontend build script
        working-directory: "./assets/frontend"
        run: yarn build:metabox && yarn build:orders
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: aftership-woocommerce-${{ github.run_number }}
          path: |
            ./*
            !.github/
            !assets/frontend/node_modules

  compute-next-tag:
    name: Compute next tag
    runs-on: ubuntu-latest
    if: |
      github.event_name == 'pull_request' &&
      github.event.pull_request.merged == true &&
      github.base_ref == 'master'
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: "0"
      - name: Compute next tag
        id: compute-tag
        uses: anothrNick/github-tag-action@1.36.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          WITH_V: false
          DRY_RUN: true
          RELEASE_BRANCHES: ".*"
    outputs:
      next_tag: ${{ steps.compute-tag.outputs.new_tag }}

  create-release:
    name: Create release
    runs-on: ubuntu-latest
    needs:
      - build-project
      - compute-next-tag
    env:
      next_tag: ${{ needs.compute-next-tag.outputs.next_tag }}
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v3
        with:
          name: aftership-woocommerce-${{ github.run_number }}
          path: aftership-woocommerce-tracking
      - name: Archive project
        run: 7z a -tzip aftership-woocommerce-tracking.zip aftership-woocommerce-tracking/*
      - name: Create new release
        id: create-new-release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.next_tag }}
          release_name: Release ${{ env.next_tag }}
          draft: true
          prerelease: false
          body: |
            This draft release is auto create by #${{github.event.pull_request.number}}
      - name: Upload release asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create-new-release.outputs.upload_url }}
          asset_path: ./aftership-woocommerce-tracking.zip
          asset_name: aftership-woocommerce-tracking-${{ env.next_tag }}.zip
          asset_content_type: application/zip
      # - name: Create comment
      #   uses: peter-evans/create-or-update-comment@v2
      #   with:
      #     issue-number: ${{github.event.pull_request.number}}
      #     body: |
      #       Github Action auto generate draft [**Release ${{ env.next_tag }}**][1] for you,

      #       check it out if you need to create a release.

      #       [1]: ${{ steps.create-new-release.outputs.html_url }}
