---
name: build-and-test

on:
  workflow_call:
    secrets:
      github-test-token:
        required: true
      github-token-private:
        required: true
      github-token-public:
        required: true
      gitlab-api-token:
        required: true
      gitlab-project-id:
        required: true

permissions:
  contents: read

jobs:
  standard:
    strategy:
      matrix:
        platform: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false

      - uses: cachix/install-nix-action@2126ae7fc54c9df00dd18f7f18754393182c73cd # v31.9.1

      - name: build with nix-build
        run: |
          nix-build -A default --argstr version "${{ github.sha }}"

          echo "### Build details" >> "$GITHUB_STEP_SUMMARY"
          echo "```" >> "$GITHUB_STEP_SUMMARY"
          echo "$ file ./result/bin/git-bug" >> "$GITHUB_STEP_SUMMARY"
          file ./result/bin/git-bug >> "$GITHUB_STEP_SUMMARY"
          echo
          echo "$ go version -m ./result/bin/git-bug" >> "$GITHUB_STEP_SUMMARY"
          nix-shell --run 'go version -m ./result/bin/git-bug' >> "$GITHUB_STEP_SUMMARY"

  # We run this job in order to test the release derivation, to ensure that any
  # changes to the derivation configuration does not affect the release build
  release-binaries:
    strategy:
      matrix:
        platform: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false

      - uses: cachix/install-nix-action@2126ae7fc54c9df00dd18f7f18754393182c73cd # v31.9.1

      - name: build with nix-build
        run: |
          nix-build -A release --argstr version "${{ github.sha }}"

          echo "### Build details" >> "$GITHUB_STEP_SUMMARY"
          echo "```" >> "$GITHUB_STEP_SUMMARY"
          echo "$ file ./result/bin/git-bug" >> "$GITHUB_STEP_SUMMARY"
          file ./result/bin/git-bug >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_SUMARY"
          echo "$ ls -sh ./result/bin/git-bug" >> "$GITHUB_STEP_SUMMARY"
          ls -sh ./result/bin/git-bug >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_SUMMARY"
          echo "$ nix-shell --run 'go version -m ./result/bin/git-bug'" >> "$GITHUB_STEP_SUMMARY"
          nix-shell --run 'go version -m ./result/bin/git-bug' >> "$GITHUB_STEP_SUMMARY"

  web-ui:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x, 20.x]
    defaults:
      run:
        working-directory: webui
    steps:
      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ matrix.node-version }}

      - name: Check out code
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false

      - uses: DeterminateSystems/nix-installer-action@21a544727d0c62386e78b4befe52d19ad12692e3 # v17

      - uses: nicknovitski/nix-develop@9be7cfb4b10451d3390a75dc18ad0465bed4932a # v1.2.1

      - name: Install
        run: make install

      - name: Build
        run: make build

      - name: Test
        run: make test

