name: "Pipeline: Test, Lint, Build"
on:
  push:
    branches:
      - master
    tags:
      - "v*"
  pull_request:
    branches:
      - master

jobs:
  go-lint:
    name: Lint Go code
    runs-on: ubuntu-latest
    container: deluan/ci-goreleaser:1.22.3-1
    steps:
      - uses: actions/checkout@v4

      - name: Config workspace folder as trusted
        run: git config --global --add safe.directory $GITHUB_WORKSPACE;  git describe --dirty --always --tags

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v6
        with:
          version: latest
          github-token: ${{ secrets.GITHUB_TOKEN }}
          problem-matchers: true
          args: --timeout 2m

      - name: Install goimports
        run: go install golang.org/x/tools/cmd/goimports@latest

      - run: goimports -w `find . -name '*.go' | grep -v '_gen.go$'`
      - run: go mod tidy
      - name: Verify no changes from goimports and go mod tidy
        run: |
          git status --porcelain
          if [ -n "$(git status --porcelain)" ]; then
            echo 'To fix this check, run "make format" and commit the changes'
            exit 1
          fi

  go:
    name: Test Go code
    runs-on: ubuntu-latest
    container: deluan/ci-goreleaser:1.22.3-1
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v4

      - name: Config workspace folder as trusted
        run: git config --global --add safe.directory $GITHUB_WORKSPACE;  git describe --dirty --always --tags

      - name: Download dependencies
        if: steps.cache-go.outputs.cache-hit != 'true'
        continue-on-error: ${{contains(matrix.go_version, 'beta') || contains(matrix.go_version, 'rc')}}
        run: go mod download

      - name: Test
        continue-on-error: ${{contains(matrix.go_version, 'beta') || contains(matrix.go_version, 'rc')}}
        run: go test -shuffle=on -race -cover ./... -v

  js:
    name: Build JS bundle
    runs-on: ubuntu-latest
    env:
      NODE_OPTIONS: "--max_old_space_size=4096"
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "npm"
          cache-dependency-path: "**/package-lock.json"

      - name: npm install dependencies
        run: |
          cd ui
          npm ci

      - name: npm lint
        run: |
          cd ui
          npm run check-formatting && npm run lint

      - name: npm test
        run: |
          cd ui
          npm test

      - name: npm build
        run: |
          cd ui
          npm run build

      - uses: actions/upload-artifact@v4
        with:
          name: js-bundle
          path: ui/build
          retention-days: 7

  binaries:
    name: Build binaries
    needs: [js, go, go-lint]
    runs-on: ubuntu-latest
    container: deluan/ci-goreleaser:1.22.3-1
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Config workspace folder as trusted
        run: git config --global --add safe.directory $GITHUB_WORKSPACE;  git describe --dirty --always --tags

      - uses: actions/download-artifact@v4
        with:
          name: js-bundle
          path: ui/build

      - name: Run GoReleaser - SNAPSHOT
        if: startsWith(github.ref, 'refs/tags/') != true
        run: goreleaser release --clean --skip=publish --snapshot
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run GoReleaser - RELEASE
        if: startsWith(github.ref, 'refs/tags/')
        run: goreleaser release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v4
        with:
          name: binaries
          path: |
            dist
            !dist/*.tar.gz
            !dist/*.zip
          retention-days: 7

  docker:
    name: Build and publish Docker images
    needs: [binaries]
    runs-on: ubuntu-latest
    env:
      DOCKER_IMAGE: ${{secrets.DOCKER_IMAGE}}
    steps:
      - name: Set up QEMU
        id: qemu
        uses: docker/setup-qemu-action@v3
        if: env.DOCKER_IMAGE != ''

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v3
        if: env.DOCKER_IMAGE != ''

      - uses: actions/checkout@v4
        if: env.DOCKER_IMAGE != ''

      - uses: actions/download-artifact@v4
        if: env.DOCKER_IMAGE != ''
        with:
          name: binaries
          path: dist

      - name: Login to Docker Hub
        if: env.DOCKER_IMAGE != ''
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Login to GitHub Container Registry
        if: env.DOCKER_IMAGE != ''
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata for Docker
        if: env.DOCKER_IMAGE != ''
        id: meta
        uses: docker/metadata-action@v5
        with:
          labels: |
            maintainer=deluan
          images: |
            name=${{secrets.DOCKER_IMAGE}}
            name=ghcr.io/${{ github.repository }}
          tags: |
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=raw,value=develop,enable={{is_default_branch}}

      - name: Build and Push
        if: env.DOCKER_IMAGE != ''
        uses: docker/build-push-action@v5
        with:
          context: .
          file: .github/workflows/pipeline.dockerfile
          platforms: linux/amd64,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
