name: Format PR

on:
  pull_request:
    branches: [ main ]

jobs:
  format:
    name: Format PR
    runs-on: ubuntu-18.04
    steps:
      - name: Setup Go 1.x
        uses: actions/setup-go@v2
        with:
          go-version: ^1.17
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN || secrets.GITHUB_TOKEN }}
      - name: Install goimports
        run: go get golang.org/x/tools/cmd/goimports
      - name: Format repo and update licenses
        working-directory: ./go
        run: |
          ./utils/repofmt/format_repo.sh
          ./Godeps/update.sh
        env:
          BRANCH_NAME: ${{ github.head_ref }}
          CHANGE_TARGET: ${{ github.base_ref }}
      - name: Changes detected
        id: detect-changes
        run: |
          changes=$(git status --porcelain)
          if [ ! -z "$changes" ]; then
             echo "::set-output name=has-changes::true"
          fi
      - uses: EndBug/add-and-commit@v7
        if: ${{ steps.detect-changes.outputs.has-changes == 'true' }}
        with:
          message: "[ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/update.sh"
          add: "."
          cwd: "."
