name: Build & Release

on:
  push:
    branches: ["main"]
    tags:
      - "v*"
  pull_request:
    branches: ["main"]
    types: [opened, synchronize, labeled, unlabeled]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  check-version-changed:
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    env:
      CURRENT_TAG: ${{ github.ref_name }}
    outputs:
      RELEASE_NOTES: ${{ steps.versions.outputs.RELEASE_NOTES }}
    steps:
      - uses: actions/checkout@v4

      - name: Check versions
        id: versions
        run: |
          if [[ $CURRENT_TAG == 'main' ]]; then
            echo "::notice::Tag $CURRENT_TAG is not a release tag, skipping the check in the main branch"
            exit 0
          fi

          if [[ $CURRENT_TAG != "v"* ]]; then
            echo "::notice::Tag $CURRENT_TAG is not a release tag, skipping the check in a PR"
            exit 0
          fi

          export PKG_VER=v$(cat crates/resolc/Cargo.toml | grep -A 5 package] | grep version | cut -d '=' -f 2 | tr -d '"' | tr -d " ")
          echo "Current tag $CURRENT_TAG"
          echo "Package version $PKG_VER"

          if [[ $CURRENT_TAG != $PKG_VER ]]; then
            echo "::error::Tag $CURRENT_TAG doesn't match package version $PKG_VER in Cargo.toml, please fix"
            exit 1
          fi

          export RELEASE_NOTES="$(sed '/^## '${CURRENT_TAG}'/,/^## v/!d' CHANGELOG.md | sed -e '1d' -e '$d')"

          echo "Release notes:"
          echo "$RELEASE_NOTES"

          echo 'RELEASE_NOTES<<EOF' >> $GITHUB_OUTPUT
          echo "$RELEASE_NOTES" >> $GITHUB_OUTPUT
          echo 'EOF' >> $GITHUB_OUTPUT

  build:
    needs: [check-version-changed]
    uses: ./.github/workflows/reusable-build.yml
    with:
      is_release: true
      retention_days: 1

  create-release:
    if: startsWith(github.ref_name, 'v')
    needs: [check-version-changed, build]
    runs-on: macos-15
    environment: tags
    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Create macOS Fat Binary
        run: |
          lipo resolc-aarch64-apple-darwin resolc-x86_64-apple-darwin -create -output resolc-universal-apple-darwin

      - name: Make Executable
        run: |
          chmod +x resolc-x86_64-unknown-linux-musl
          chmod +x resolc-universal-apple-darwin

      - name: Create sha-256 checksum
        run: |
          shasum -a 256 resolc-x86_64-unknown-linux-musl > checksums.txt
          shasum -a 256 resolc-universal-apple-darwin >> checksums.txt
          shasum -a 256 resolc-x86_64-pc-windows-msvc.exe >> checksums.txt
          shasum -a 256 resolc.js >> checksums.txt
          shasum -a 256 resolc.wasm >> checksums.txt
          shasum -a 256 resolc_web.js >> checksums.txt

      - uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: ${{ secrets.REVIVE_RELEASE_APP_ID }}
          private-key: ${{ secrets.REVIVE_RELEASE_APP_KEY }}

      - name: create-release
        uses: softprops/action-gh-release@v2
        with:
          body: |
            ## Changelog
            ${{ needs.check-version-changed.outputs.RELEASE_NOTES }}

            ## Note for macOS Users
            The macOS binary is unsigned and it needs to be made runnable using `xattr -c resolc-universal-apple-darwin`.
          tag_name: ${{ github.ref_name }}
          name: ${{ github.ref_name }}
          prerelease: true
          token: ${{ steps.app-token.outputs.token }}
          target_commitish: ${{ github.sha }}
          files: |
            resolc-x86_64-unknown-linux-musl
            resolc-universal-apple-darwin
            resolc-x86_64-pc-windows-msvc.exe
            resolc.js
            resolc.wasm
            resolc_web.js
            checksums.txt

  npm-release:
    needs: [create-release]
    runs-on: macos-15
    environment: tags
    steps:
      - uses: actions/checkout@v4

      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Set Up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "20"

      - run: npm ci -w js/resolc

      - name: Build
        run: |
          cp -f resolc.{wasm,js} js/resolc/src/resolc
          npm -w js/resolc run build

      - name: npm pack
        run: npm -w js/resolc pack

      - uses: actions/upload-artifact@v4
        with:
          name: npm_package
          path: "parity-resolc-*.tgz"

      - uses: octokit/request-action@bbedc70b1981e610d89f1f8de88311a1fc02fb83
        with:
          route: POST /repos/paritytech/npm_publish_automation/actions/workflows/publish.yml/dispatches
          ref: main
          inputs: '${{ format(''{{ "artifact_name": "npm_package", "repo": "{0}", "run_id": "{1}" }}'', github.repository, github.run_id) }}'
        env:
          GITHUB_TOKEN: ${{ secrets.NPM_PUBLISH_AUTOMATION_TOKEN }}
