name: "release-please"

on:
  push:
    branches:
      - "master"

concurrency:
  group: "${{ github.workflow }}-${{ github.ref }}"

jobs:
  release-please:
    runs-on: ubuntu-latest

    outputs:
      releases-created: ${{ steps.release.outputs.releases_created }}
      pr: ${{ steps.release.outputs.pr }}

      marine-release-created: ${{ steps.release.outputs['tools/cli--release_created'] }}
      marine-tag-name: ${{ steps.release.outputs['tools/cli--tag_name'] }}

      mrepl-release-created: ${{ steps.release.outputs['tools/repl--release_created'] }}
      mrepl-tag-name: ${{ steps.release.outputs['tools/repl--tag_name'] }}

      marine-js-release-created: ${{ steps.release.outputs['marine-js--release_created'] }}
      marine-js-tag-name: ${{ steps.release.outputs['marine-js--tag_name'] }}

    steps:
      - name: Run release-please
        id: release
        uses: google-github-actions/release-please-action@v3
        with:
          token: ${{ secrets.FLUENCEBOT_RELEASE_PLEASE_PAT }}
          command: manifest
          config-file: .github/release-please/config.json
          manifest-file: .github/release-please/manifest.json

      - name: Show output from release-please
        if: steps.release.outputs.releases_created
        env:
          RELEASE_PLEASE_OUTPUT: ${{ toJSON(steps.release.outputs) }}
        run: echo "${RELEASE_PLEASE_OUTPUT}" | jq

  bump-version:
    if: needs.release-please.outputs.pr != null
    runs-on: ubuntu-latest
    needs:
      - release-please

    permissions:
      contents: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ fromJson(needs.release-please.outputs.pr).headBranchName }}
          token: ${{ secrets.FLUENCEBOT_RELEASE_PLEASE_PAT }}

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

        # workaround for marine depending on itself messing with lock file
      - name: Update Cargo.lock
        run: cargo update --workspace

      - name: Commit updated Cargo.lock
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: "chore: Regenerate Cargo.lock"
          branch: ${{ fromJson(needs.release-please.outputs.pr).headBranchName }}
          commit_user_name: fluencebot
          commit_user_email: devops@fluence.one
          commit_author: fluencebot <devops@fluence.one>

      - name: Install wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0

      - name: Build marine-js package for node
        working-directory: marine-js
        run: ./build.sh

      - name: Get marine-js version
        id: version
        run: |
          version="$(jq -r '.["marine-js"]' .github/release-please/manifest.json)"
          echo "version=${version}" >> $GITHUB_OUTPUT

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://registry.npmjs.org"

      - name: Set marine-js npm version
        run: npm version ${{ steps.version.outputs.version }}
        # ignore when it fails when new version is same as current
        continue-on-error: true
        working-directory: marine-js/npm-package

      - name: Commit version bump
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: "chore: Bump marine-js version to ${{ steps.version.outputs.version }}"
          branch: ${{ fromJson(needs.release-please.outputs.pr).headBranchName }}
          commit_user_name: fluencebot
          commit_user_email: devops@fluence.one
          commit_author: fluencebot <devops@fluence.one>

  crates:
    if: needs.release-please.outputs.releases-created
    runs-on: builder
    needs:
      - release-please

    permissions:
      contents: write
      id-token: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Import secrets
        uses: hashicorp/vault-action@cb841f2c86fb6d07cff94fda240828c1abc5ba43 # v2.7.3
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          exportToken: false
          secrets: |
            kv/crates.io/fluencebot token | CARGO_REGISTRY_TOKEN

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

      - name: Install cargo-workspaces
        uses: baptiste0928/cargo-install@v2.2.0
        with:
          crate: cargo-workspaces

      - name: Publish to crates.io
        run: |
          cargo ws publish \
            --no-git-commit \
            --allow-dirty \
            --from-git \
            --skip-published \
            --yes

  marine:
    name: Build marine
    needs: release-please
    if: needs.release-please.outputs.marine-release-created
    permissions:
      contents: write
    uses: ./.github/workflows/binary.yml
    with:
      binary: marine
      tag: ${{ needs.release-please.outputs.marine-tag-name }}

  mrepl:
    name: Build
    needs: release-please
    if: needs.release-please.outputs.mrepl-release-created
    permissions:
      contents: write
    uses: ./.github/workflows/binary.yml
    with:
      binary: mrepl
      tag: ${{ needs.release-please.outputs.mrepl-tag-name }}

  marine-js:
    if: needs.release-please.outputs.marine-js-release-created
    runs-on: builder
    needs:
      - release-please

    permissions:
      contents: read
      id-token: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Import secrets
        uses: hashicorp/vault-action@cb841f2c86fb6d07cff94fda240828c1abc5ba43 # v2.7.3
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          exportToken: false
          secrets: |
            kv/npmjs/fluencebot token | NODE_AUTH_TOKEN

      - name: Setup Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1

      - name: Install wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0

      - name: Build marine-js package for node
        working-directory: marine-js
        run: ./build.sh

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://registry.npmjs.org"

      - run: npm i
        working-directory: marine-js/npm-package

      - run: npm run build
        working-directory: marine-js/npm-package

      - name: Publish to npm registry
        run: npm publish --access public
        working-directory: marine-js/npm-package

  slack:
    if: always()
    name: "Notify"
    runs-on: ubuntu-latest

    needs:
      - release-please
      - crates
      - marine-js

    permissions:
      contents: read
      id-token: write

    steps:
      - uses: lwhiteley/dependent-jobs-result-check@v1
        id: status
        with:
          statuses: failure
          dependencies: ${{ toJSON(needs) }}

      - name: Log output
        run: |
          echo "statuses:" "${{ steps.status.outputs.statuses }}"
          echo "jobs:" "${{ steps.status.outputs.jobs }}"
          echo "found any?:" "${{ steps.status.outputs.found }}"

      - name: Import secrets
        uses: hashicorp/vault-action@cb841f2c86fb6d07cff94fda240828c1abc5ba43 # v2.7.3
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          exportToken: false
          secrets: |
            kv/slack/release-please webhook | SLACK_WEBHOOK_URL

      - uses: ravsamhq/notify-slack-action@v2
        if: steps.status.outputs.found == 'true'
        with:
          status: "failure"
          notification_title: "*{workflow}* has {status_message}"
          message_format: "${{ steps.status.outputs.jobs }} {status_message} in <{repo_url}|{repo}>"
          footer: "<{run_url}>"
