# Automatically fix license files on PRs that need updates
# Tries to auto-commit the fix, or comments with instructions if push fails

name: License Check
on:
  pull_request:
    branches:
      - main  # Only run when PR targets main
    paths:
      - "**.go"
      - go.mod
      - go.sum
      - ".github/licenses.tmpl"
      - "script/licenses*"
      - "third-party-licenses.*.md"
      - "third-party/**"
permissions:
  contents: write
  pull-requests: write

jobs:
  license-check:
    runs-on: ubuntu-latest

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

      # Check out the actual PR branch so we can push changes back if needed
      - name: Check out PR branch
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh pr checkout ${{ github.event.pull_request.number }}

      - name: Build UI
        uses: ./.github/actions/build-ui

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

      # actions/setup-go does not setup the installed toolchain to be preferred over the system install,
      # which causes go-licenses to raise "Package ... does not have module info" errors.
      # For more information, https://github.com/google/go-licenses/issues/244#issuecomment-1885098633
      - name: Regenerate licenses
        env:
          CI: "true"
        run: |
          export GOROOT=$(go env GOROOT)
          export PATH=${GOROOT}/bin:$PATH
          ./script/licenses

      - name: Check for changes
        id: changes
        continue-on-error: true
        run: script/licenses-check

      - name: Commit and push fixes
        if: steps.changes.outcome == 'failure'
        continue-on-error: true
        id: push
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add third-party-licenses.*.md third-party/
          git commit -m "chore: regenerate license files" -m "Auto-generated by license-check workflow"
          git push

      - name: Check if already commented
        if: steps.changes.outcome == 'failure' && steps.push.outcome == 'failure'
        id: check_comment
        uses: actions/github-script@v9
        with:
          script: |
            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number
            });
            
            const alreadyCommented = comments.some(comment => 
              comment.user.login === 'github-actions[bot]' &&
              comment.body.includes('## ⚠️ License files need updating')
            );
            
            core.setOutput('already_commented', alreadyCommented ? 'true' : 'false');

      - name: Comment with instructions if cannot push
        if: steps.changes.outcome == 'failure' && steps.push.outcome == 'failure' && steps.check_comment.outputs.already_commented == 'false'
        uses: actions/github-script@v9
        with:
          script: |
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
              body: `## ⚠️ License files need updating
            
            The license files are out of date. I tried to fix them automatically but don't have permission to push to this branch.
            
            **Please run:**
            \`\`\`bash
            script/licenses
            git add third-party-licenses.*.md third-party/
            git commit -m "chore: regenerate license files"
            git push
            \`\`\`
            
            Alternatively, enable "Allow edits by maintainers" in the PR settings so I can fix it automatically.`
            });

      - name: Fail check if changes needed
        if: steps.changes.outcome == 'failure'
        run: exit 1

