name: go-licenses

on:
  # run action when a change lands in the main branch which updates go.mod or
  # our license template file. Also allow manual triggering.
  push:
    branches:
      - main
    paths:
      - go.mod
      - .github/licenses.tmpl
      - .github/workflows/go-licenses.yml
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-$${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  tailscale:
    runs-on: ubuntu-latest

    steps:
      - name: Check out code
        uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version-file: go.mod

      - name: Install go-licenses
        run: |
          go install github.com/google/go-licenses@v1.2.2-0.20220825154955-5eedde1c6584

      - name: Run go-licenses
        env:
          # include all build tags to include platform-specific dependencies
          GOFLAGS: "-tags=android,cgo,darwin,freebsd,ios,js,linux,openbsd,wasm,windows"
        run: |
          [ -d licenses ] || mkdir licenses
          go-licenses report tailscale.com/cmd/tailscale tailscale.com/cmd/tailscaled > licenses/tailscale.md --template .github/licenses.tmpl

      - name: Get access token
        uses: tibdex/github-app-token@f717b5ecd4534d3c4df4ce9b5c1c2214f0f7cd06 # v1.6.0
        id: generate-token
        with:
          app_id: ${{ secrets.LICENSING_APP_ID }}
          installation_id: ${{ secrets.LICENSING_APP_INSTALLATION_ID }}
          private_key: ${{ secrets.LICENSING_APP_PRIVATE_KEY }}

      - name: Send pull request
        uses: peter-evans/create-pull-request@18f90432bedd2afd6a825469ffd38aa24712a91d #v4.1.1
        with:
          token: ${{ steps.generate-token.outputs.token }}
          author: License Updater <noreply@tailscale.com>
          committer: License Updater <noreply@tailscale.com>
          branch: licenses/cli
          commit-message: "licenses: update tailscale{,d} licenses"
          title: "licenses: update tailscale{,d} licenses"
          body: Triggered by ${{ github.repository }}@${{ github.sha }}
          signoff: true
          delete-branch: true
          team-reviewers: opensource-license-reviewers
