name: "release"

on:
  push:
    branches:
      - "main"

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

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

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

    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: builder
    needs:
      - release-please

    permissions:
      contents: write

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

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

      - name: Install cargo-edit
        uses: baptiste0928/cargo-install@v2.0.0
        with:
          crate: cargo-edit

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

      - name: Set version in service
        working-directory: service
        run: cargo set-version ${{ steps.version.outputs.version }}

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: "16"

      - name: Set version in aqua
        working-directory: aqua
        run: npm version ${{ steps.version.outputs.version }}

      - name: Commit version bump
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: "chore: Bump registry 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>

  registry:
    runs-on: builder

    needs: release-please
    if: needs.release-please.outputs.release-created

    permissions:
      contents: write
      id-token: write

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

      - name: Import secrets
        uses: hashicorp/vault-action@v2.5.0
        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: Setup marine
        uses: fluencelabs/setup-marine@v1

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

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://registry.npmjs.org"
          cache-dependency-path: "aqua/package-lock.json"
          cache: "npm"

      - run: npm i
        working-directory: aqua

      - run: npm run build
        working-directory: aqua

      - name: Publish to NPM registry
        run: npm publish --access public
        working-directory: aqua

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

      - name: Calculate SHA256
        id: sha
        run: |
          # Calculate sha256
          du -hs registry.tar.gz
          sha256sum registry.tar.gz
          sha=($(sha256sum registry.tar.gz))
          echo "sha256=${sha}" >> $GITHUB_OUTPUT

      - name: Upload registry package
        if: needs.release-please.outputs.release-created
        uses: softprops/action-gh-release@v1
        with:
          files: registry.tar.gz
          tag_name: ${{ needs.release-please.outputs.tag-name }}

      - name: Update version in node-distro repo
        if: needs.release-please.outputs.release-created
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: update_service
          repo: fluencelabs/node-distro
          ref: "main"
          token: ${{ secrets.PERSONAL_TOKEN }}
          inputs: '{
            "name": "registry",
            "version": "${{ needs.release-please.outputs.version }}",
            "url": "https://github.com/fluencelabs/registry/releases/download/${{ needs.release-please.outputs.tag-name }}/registry.tar.gz",
            "sha256": "${{ steps.sha.outputs.sha256 }}"
          }'

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

    needs:
      - release-please
      - registry

    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@v2.5.0
        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}>"
