name: Coordinator

on:
  push:
    branches:
      - main
      - staging
      - develop
      - alpha
    paths:
      - 'coordinator/**'
      - 'common/**'
      - 'database/**'
      - '.github/workflows/coordinator.yml'
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review
    paths:
      - 'coordinator/**'
      - 'common/**'
      - 'database/**'
      - '.github/workflows/coordinator.yml'

jobs:
  check:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    steps:
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: nightly-2022-12-10
        override: true
        components: rustfmt, clippy
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.19.x
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Lint
      working-directory: 'coordinator'
      run: |
        rm -rf $HOME/.cache/golangci-lint
        make lint
  goimports-lint:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    steps:
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.19.x
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Install goimports
      run: go install golang.org/x/tools/cmd/goimports
    - name: Run goimports lint
      working-directory: 'coordinator'
      run: goimports -local scroll-tech/coordinator/ -w .
    - name: Run go mod tidy
      working-directory: 'coordinator'
      run: go mod tidy
    # If there are any diffs from goimports or go mod tidy, fail.
    - name: Verify no changes from goimports and go mod tidy
      working-directory: 'coordinator'
      run: |
        if [ -n "$(git status --porcelain)" ]; then
          exit 1
        fi
  # docker-build:
  #   if: github.event.pull_request.draft == false
  #   runs-on: ubuntu-latest
  #   steps:
  #   - name: Checkout code
  #     uses: actions/checkout@v2
  #   - name: Set up Docker Buildx
  #     uses: docker/setup-buildx-action@v2
  #   - name: Build and push
  #     uses: docker/build-push-action@v2
  #     with:
  #       context: .
  #       file: ./build/dockerfiles/coordinator.Dockerfile
  #       push: false
  #       # cache-from: type=gha,scope=${{ github.workflow }}
  #       # cache-to: type=gha,scope=${{ github.workflow }}
  tests:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-latest
    steps:
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.19.x
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Install Solc
      uses: supplypike/setup-bin@v3
      with:
        uri: 'https://github.com/ethereum/solidity/releases/download/v0.8.16/solc-static-linux'
        name: 'solc'
        version: '0.8.16'
    - name: Install Geth Tools
      uses: gacts/install-geth-tools@v1
    - name: Build prerequisites
      run: |
        make dev_docker
    - name: Test coordinator packages
      working-directory: 'coordinator'
      run: |
        # go test -exec "env LD_LIBRARY_PATH=${PWD}/verifier/lib" -v -race -gcflags="-l" -ldflags="-s=false" -coverpkg="scroll-tech/coordinator" -coverprofile=coverage.txt -covermode=atomic ./...
        go test -v -race -gcflags="-l" -ldflags="-s=false" -coverprofile=coverage.txt -covermode=atomic -tags mock_verifier ./...
    - name: Upload coverage reports to Codecov
      uses: codecov/codecov-action@v3
      env:
        CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
      with:
        flags: coordinator
