name: "publish-release"

on:
  push:
    tags:
      - "v*"

jobs:
  npm-publish:
    name: "Publish"
    runs-on: builder
    container: rust
    defaults:
      run:
        shell: bash

    steps:
### Setup
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set env
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: "Install marine"
        uses: baptiste0928/cargo-install@v1
        with:
          crate: marine

      - uses: actions/setup-java@v2
        with:
          distribution: 'adopt'
          java-version: '11'

      - name: Cache npm
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-v03-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-v03-

      - uses: actions/setup-node@v2
        with:
          node-version: "15"
          registry-url: "https://registry.npmjs.org"

      - name: Cache cargo
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ~/.cargo/bin
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Install Rust
        working-directory: ./service
        run: |
          rustup toolchain install nightly-2022-09-01-x86_64-unknown-linux-gnu
          rustup default nightly-2022-09-01-x86_64-unknown-linux-gnu
          rustup override set nightly-2022-09-01-x86_64-unknown-linux-gnu
          rustup target add wasm32-wasi --toolchain nightly-2022-09-01-x86_64-unknown-linux-gnu

### Build
      - name: Build registry.wasm
        working-directory: ./service
        run: ./build.sh

      - name: Check Aqua compiles
        working-directory: ./aqua
        run: |
          npm i
          npm run build

      - name: Create builtin distribution package
        run: |
          ./builtin-package/package.sh

      - name: Build Changelog
        id: changelog
        uses: mikepenz/release-changelog-builder-action@v1
        with:
          configuration: ".github/workflows/changelog_config.json"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

## Publish
      - name: Release
        id: release
        uses: softprops/action-gh-release@v1
        with:
          name: registry ${{ env.RELEASE_VERSION }}
          tag_name: ${{ env.RELEASE_VERSION }}
          files: |
            registry.tar.gz
          body: ${{steps.changelog.outputs.changelog}}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      ### Publish Aqua API
      - name: Publish Aqua API
        run: |
          npm version ${{ env.RELEASE_VERSION }} --allow-same-version
          npm publish --access public
        env:
            NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        working-directory: ./aqua

## Update node-distro repo
      - name: Get tar.gz URL
        id: package-url
        uses: actions/github-script@v4
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          result-encoding: string
          script: |
            try {
                let assets = await github.repos.listReleaseAssets({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  release_id: "${{ steps.release.outputs.id }}",
                });
                console.dir(assets);
                let package = assets.data.find((a) => a.name === 'registry.tar.gz');
                let url = package.browser_download_url;
                console.log("URL: " + url);
                return url;
            } catch (e) {
              console.log("Err: " + e);
              throw e;
            }

      - name: Calculate SHA256
        run: |
          du -hs registry.tar.gz
          echo $(sha256sum registry.tar.gz)
          echo "SHA256=$(sha256sum registry.tar.gz | awk '{ print $1 }')" >> $GITHUB_ENV

      - name: Update version in node-distro repo
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: update_service
          repo: fluencelabs/node-distro
          ref: 'main'
          token: ${{ secrets.PERSONAL_TOKEN }}
          inputs: '{
            "name": "registry",
            "version": "${{ env.RELEASE_VERSION }}",
            "url": "${{ steps.package-url.outputs.result }}",
            "sha256": "${{ env.SHA256 }}"
          }'

      - name: Log notice
        uses: actions/github-script@v4
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
              console.dir(core);
              core.info("registry was updated to ${{ env.RELEASE_VERSION }} in node-distro repo");
