name: Release
on: [workflow_call, workflow_dispatch]

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: true


defaults:
  run:
    shell: 'bash'


jobs:

  draft:
    runs-on: ubuntu-latest
    outputs:
      release-note: ${{ steps.release-note.outputs.release-note }}
      version: ${{ steps.version.outputs.build-version }}

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: actions/setup-node@v3
        with:
          node-version: 14

      - name: Get last git tag
        id: tag
        run: echo "::set-output name=last-tag::$(git describe --tags --abbrev=0 || git rev-list --max-parents=0 ${{github.ref}})"

      - name: Generate release notes
        uses: ./.github/actions/release-notes
        id: release-note
        with:
          from: ${{ steps.tag.outputs.last-tag }}
          to: ${{ github.ref }}
          include-commit-body: true
          include-abbreviated-commit: true

      - name: Get version from current date
        id: version
        run: echo "::set-output name=build-version::$(node -e "try{console.log(require('./.electron-builder.config.js').extraMetadata.version)}catch(e){console.error(e);process.exit(1)}")"

      - name: Delete outdated drafts
        uses: hugo19941994/delete-draft-releases@v1.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create Release Draft
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.github_token }}
        with:
          prerelease: true
          draft: true
          tag_name: v${{ steps.version.outputs.build-version }}
          name: v${{ steps.version.outputs.build-version }}
          body: ${{ steps.release-note.outputs.release-note }}

  upload_artifacts:
    needs: [ draft ]

    strategy:
      matrix:
        os: [ windows-latest ]
    #    To compile the application for different platforms, use:
    #    os: [ macos-latest, ubuntu-latest, windows-latest ]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 16 # Need for npm >=7.7
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      # The easiest way to transfer release notes to a compiled application is create `release-notes.md` in the build resources.
      # See https://github.com/electron-userland/electron-builder/issues/1511#issuecomment-310160119
      - name: Prepare release notes
        env:
          RELEASE_NOTE: ${{ needs.draft.outputs.release-note }}
        run: echo "$RELEASE_NOTE" >> ./buildResources/release-notes.md

      # Compile app and upload artifacts
      - name: Compile & release Electron app
        uses: samuelmeuli/action-electron-builder@v1
        env:
          VITE_APP_VERSION: ${{ needs.draft.outputs.version }}
        with:
          build_script_name: build
          args: --config .electron-builder.config.js

          # GitHub token, automatically provided to the action
          # (No need to define this secret in the repo settings)
          github_token: ${{ secrets.github_token }}

          # If the commit is tagged with a version (e.g. "v1.0.0"),
          # release the app after building
          release: true

          # Sometimes the build may fail due to a connection problem with Apple, GitHub, etc. servers.
          # This option will restart the build as many attempts as possible
          max_attempts: 3


          # Code Signing params

          # Base64-encoded code signing certificate for Windows
          # windows_certs: ''

          # Password for decrypting `windows_certs`
          # windows_certs_password: ''

          # Base64-encoded code signing certificate for macOS
          # mac_certs: ''

          # Password for decrypting `mac_certs`
          # mac_certs_password: ''
