name: CI

on:
  push:
    branches:
      - "main"
jobs:
  test:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
      - name: Determine download URL for pkger
        id: pkger-download-url
        uses: actions/github-script@v2
        with:
          result-encoding: string
          script: |
            return github.repos.getReleaseByTag({
                owner: "markbates",
                repo: "pkger",
                tag: "v0.17.1"
            }).then(result => {
                return result.data.assets
                  .filter(a => a.name.includes('Linux_x86'))
                  .map(a => a.browser_download_url)[0];
            })
      - name: Install pkger
        run: |
          curl -s -L -o pkger.tgz ${{ steps.pkger-download-url.outputs.result }}
          tar xvzf pkger.tgz
      - name: Test
        run:  make test
        env:
          PKGER: "./${{ steps.pkger-binaries.outputs.binary }}"
      - name: Lint
        run: make check
    outputs:
      pkger: ${{ steps.pkger-download-url.outputs.result }}

  build-and-publish:
    needs: test
    runs-on: ubuntu-latest
    steps:
      # Create a release, or update the release PR
      - uses: GoogleCloudPlatform/release-please-action@v2.24.1
        id: release
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          release-type: simple
          bump-minor-pre-major: true
      # Checkout
      - uses: actions/checkout@v2
      # Tag
      # If a release was created, tag `vX.Y.Z` synchronously which:
      #   1. Triggers release-please to create draft release, allowing manual
      #      proofreading (and probably editing) of release notes.
      #      (often raw commit messages are a bit overwhelming, overly granular
      #      or at least could use some context when included as release notes).
      #   2. Ensures the tag exists for subsequent action tasks which rely on
      #      this metadata, such as including version in release binaries.
      #      The release-please action does add this tag, but asynchronously.
      # Note that tag is created annotated such that it shows tagging metadata
      # when queried rather than just the associated commit metadata.
      - name: tag
        if: ${{ steps.release.outputs.release_created }}
        run: |
          git config user.name github-actions[bot]
          git tag -d v${{steps.release.outputs.major}}.${{steps.release.outputs.minor}}.${{steps.release.outputs.patch}} || true
          git push origin :v${{steps.release.outputs.major}}.${{steps.release.outputs.minor}}.${{steps.release.outputs.patch}} || true
          git tag -a v${{steps.release.outputs.major}}.${{steps.release.outputs.minor}}.${{steps.release.outputs.patch}} -m "Release v${{steps.release.outputs.major}}.${{steps.release.outputs.minor}}.${{steps.release.outputs.patch}}"
          git push origin v${{steps.release.outputs.major}}.${{steps.release.outputs.minor}}.${{steps.release.outputs.patch}} || true

      - uses: actions/setup-go@v2
      - name: Install pkger
        run: |
          curl -s -L -o pkger.tgz ${{ needs.test.outputs.pkger }}
          tar xvzf pkger.tgz
  
      # Standard build tasks
      - name: Build
        run:  make cross-platform
        env:
          PKGER: "./pkger"
          # NOTE:
          # release-please adds the version asynchronously. Without using the
          # synchonous tagging step above, the version can be explicitly passed
          # to the build using the following environment variable.  However this
          # has the side-effect of causing inter-relese binaries to not include
          # verbose version information, because the special version `tip` is
          # overriden with a blank string in those cases.
          # VERS: ${{ steps.release.outputs.tag_name }}

      # Upload all build artifacts whether it's a release or not
      - uses: actions/upload-artifact@v2
        with:
          name: OSX Binary
          path: func_darwin_amd64
      - uses: actions/upload-artifact@v2
        with:
          name: Linux Binary
          path: func_linux_amd64
      - uses: actions/upload-artifact@v2
        with:
          name: Windows Binary
          path: func_windows_amd64.exe

      # The following steps are only executed if this is a release
      - name: Compress Binaries
        if: ${{ steps.release.outputs.release_created }}
        run: gzip func_darwin_amd64 func_linux_amd64 func_windows_amd64.exe

      # Upload all binaries
      - name: Upload Darwin Binary
        uses: actions/upload-release-asset@v1
        if: ${{ steps.release.outputs.release_created }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.release.outputs.upload_url }}
          asset_path: ./func_darwin_amd64.gz
          asset_name: func_darwin_amd64.gz
          asset_content_type: application/x-gzip
      - name: Upload Linux Binary
        uses: actions/upload-release-asset@v1
        if: ${{ steps.release.outputs.release_created }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.release.outputs.upload_url }}
          asset_path: ./func_linux_amd64.gz
          asset_name: func_linux_amd64.gz
          asset_content_type: application/x-gzip
      - name: Upload Windows Binary
        uses: actions/upload-release-asset@v1
        if: ${{ steps.release.outputs.release_created }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.release.outputs.upload_url }}
          asset_path: ./func_windows_amd64.exe.gz
          asset_name: func_windows_amd64.exe.gz
          asset_content_type: application/x-gzip
