name: Generate
on:
  push:
    paths:
      - 'scripts/generate-popular-actions/main.go'
      - 'scripts/generate-webhook-events/main.go'
    branches:
      - main
    tags-ignore:
      - '*'
  schedule:
    # Every Saturday 10:21 in JST
    - cron: '21 1 * * 6'
  workflow_dispatch:

jobs:
  go-generate:
    if: ${{ github.repository == 'rhysd/actionlint' }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version: '1.26'
          check-latest: true
      - name: Check new release on GitHub
        run: go run ./scripts/generate-popular-actions -d
      - run: go generate
      - run: |
          if git diff-files --quiet; then
            echo "pr=false" >> "$GITHUB_OUTPUT"
          else
            git diff
            echo "pr=true" >> "$GITHUB_OUTPUT"
          fi
        id: diff
      - uses: peter-evans/create-pull-request@v8
        with:
          branch: 'ci/${{ github.run_id }}'
          commit-message: 'update generated files by `go generate` on CI'
          committer: 'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>'
          author: 'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>'
          title: 'Update generated files by CI'
          body: |
            This PR updates some generated files to the latest by running `go generate`.
            This PR was created by [this workflow run](https://github.com/rhysd/actionlint/actions/runs/${{ github.run_id }}) automatically.
            List of workflow runs is [here](https://github.com/rhysd/actionlint/actions/workflows/generate.yaml).
          assignees: rhysd
        if: ${{ steps.diff.outputs.pr == 'true' }}
