name: ci
on:
  push:
    branches:
      - main
  pull_request:
  schedule:
    - cron: '0 2 * * *' # Run every day, at 2AM UTC.
env:
  GOPATH: ${{ github.workspace }}
  WORKING_DIR: ./src/github.com/google/pprof/
jobs:
  test-mac:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        working-directory: ${{ env.WORKING_DIR }}
    strategy:
      fail-fast: false
      matrix:
        go: ['1.20', '1.21', 'tip']
        # Supported macOS versions can be found in
        # https://github.com/actions/virtual-environments#available-environments.
        # TODO: Add macos-13. As of now there are build errors when installing graphviz.
        os: ['macos-12']
        # Supported Xcode versions can be found in:
        # - https://github.com/actions/virtual-environments/blob/main/images/macos/macos-12-Readme.md#xcode
        # - https://github.com/actions/virtual-environments/blob/main/images/macos/macos-13-Readme.md#xcode
        xcode-version: ['14.2', '14.1', '14.0.1', '13.4.1', '13.3.1', '13.2.1', '13.1']
    steps:
      - name: Update Go version using setup-go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        if: matrix.go != 'tip'
        with:
          go-version: ${{ matrix.go }}

      - name: Update Go version manually
        if: matrix.go == 'tip'
        working-directory: ${{ github.workspace }}
        run: |
          git clone https://go.googlesource.com/go $HOME/gotip
          cd $HOME/gotip/src
          ./make.bash
          echo "GOROOT=$HOME/gotip" >> $GITHUB_ENV
          echo "RUN_STATICCHECK=false" >> $GITHUB_ENV
          echo "RUN_GOLANGCI_LINTER=false" >> $GITHUB_ENV
          echo "$HOME/gotip/bin:$PATH" >> $GITHUB_PATH

      - name: Checkout the repo
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          path: ${{ env.WORKING_DIR }}

      - name: Set up Xcode
        uses: maxim-lobanov/setup-xcode@60606e260d2fc5762a71e64e74b2174e8ea3c8bd # v1.6.0
        with:
          xcode-version: ${{ matrix.xcode-version }}

      - name: Fetch dependencies
        run: |
          brew install graphviz
          # Do not let tools interfere with the main module's go.mod.
          cd && go mod init tools
          go install honnef.co/go/tools/cmd/staticcheck@v0.4.6
          go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.0
          # Add PATH for installed tools.
          echo "$GOPATH/bin:$PATH" >> $GITHUB_PATH

      - name: Run the script
        run: |
          go version
          ./test.sh

      - name: Code coverage
        uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0
        with:
          file: ${{ env.WORKING_DIR }}/coverage.txt

  test-linux:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        working-directory: ${{ env.WORKING_DIR }}
    strategy:
      fail-fast: false
      matrix:
        go: ['1.20', '1.21', 'tip']
        os: ['ubuntu-22.04', 'ubuntu-20.04']
    steps:
      - name: Update Go version using setup-go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        if: matrix.go != 'tip'
        with:
          go-version: ${{ matrix.go }}

      - name: Update Go version manually
        if: matrix.go == 'tip'
        working-directory: ${{ github.workspace }}
        run: |
          git clone https://go.googlesource.com/go $HOME/gotip
          cd $HOME/gotip/src
          ./make.bash
          echo "GOROOT=$HOME/gotip" >> $GITHUB_ENV
          echo "RUN_STATICCHECK=false" >> $GITHUB_ENV
          echo "RUN_GOLANGCI_LINTER=false" >> $GITHUB_ENV
          echo "$HOME/gotip/bin" >> $GITHUB_PATH

      - name: Checkout the repo
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          path: ${{ env.WORKING_DIR }}

      - name: Check chrome for browser tests
        run: |
          google-chrome --version

      - name: Fetch dependencies
        run: |
          sudo apt-get install graphviz
          # Do not let tools interfere with the main module's go.mod.
          cd && go mod init tools
          go install honnef.co/go/tools/cmd/staticcheck@v0.4.6
          go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.0
          # Add PATH for installed tools.
          echo "$GOPATH/bin:$PATH" >> $GITHUB_PATH

      - name: Run the script
        run: |
          go version
          ./test.sh

      - name: Code coverage
        uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0
        with:
          file: ${{ env.WORKING_DIR }}/coverage.txt

  test-windows:
    runs-on: windows-2019
    strategy:
      fail-fast: false
      matrix:
        go: ['1.20', '1.21']
    steps:
      - name: Update Go version using setup-go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          go-version: ${{ matrix.go }}

      - name: Checkout the repo
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          path: ${{ env.WORKING_DIR }}

      - name: Fetch Windows dependency
        uses: crazy-max/ghaction-chocolatey@0e015857dd851f84fcb7fb53380eb5c4c8202333 # v3.0.0
        with:
          args: install graphviz llvm

      - name: Run the test
        run: |
          go version
          # This is a workaround to make graphviz installed through choco work.
          # It generates a config file to tell dot what layout engine and
          # format types are available. See
          # https://github.com/google/pprof/issues/585 for more details.
          dot -c
          go env
          go build github.com/google/pprof
          go test -v ./...
        working-directory: ${{ env.WORKING_DIR }}

  check:
    if: always()
    runs-on: ubuntu-latest
    needs:
    - test-mac
    - test-linux
    - test-windows
    steps:
    - name: Decide whether the needed jobs succeeded or failed
      uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # v1.2.2
      with:
        jobs: ${{ toJSON(needs) }}
