name: "Project: Tidy"
on:
  pull_request_target:
    types: [opened, ready_for_review, synchronize, reopened, labeled, unlabeled]
    branches:
      - main

permissions: read-all

jobs:
  setup-environment:
    permissions:
      contents: write # for Git to git push
    timeout-minutes: 30
    runs-on: ubuntu-latest
    if: ${{ !contains(github.event.pull_request.labels.*.name, 'dependency-major-update') && (github.actor == 'renovate[bot]' || contains(github.event.pull_request.labels.*.name, 'renovatebot')) && github.event.pull_request.head.repo.fork == false }}
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: ${{ github.head_ref }}
      - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          go-version: oldstable
          cache: false
      - name: Cache Go
        id: go-cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/bin
            ~/go/pkg/mod
          key: go-cache-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/go.sum') }}
      - name: Install dependencies
        if: steps.go-cache.outputs.cache-hit != 'true'
        run: make -j2 gomoddownload
      - name: go mod tidy
        run: |
          make gotidy
          git config user.name otelbot
          git config user.email 197425009+otelbot@users.noreply.github.com
          echo "git diff --exit-code || (git add . && git commit -m \"go mod tidy\" && git push)"
          git diff --exit-code || (git add . && git commit -m "go mod tidy" && git push)
      - uses: actions-ecosystem/action-remove-labels@2ce5d41b4b6aa8503e285553f75ed56e0a40bae0 # v1.3.0
        with:
          labels: renovatebot
