# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Generate Skills

on:
  schedule:
    - cron: "0 * * * *" # Hourly — keeps skills in sync with Discovery API changes
  workflow_dispatch: # Manual trigger
  push:
    branches-ignore:
      - main # main is kept up to date by PR merges

concurrency:
  group: generate-skills-${{ github.ref_name }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  generate:
    name: Generate and commit skills
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          # For cron/dispatch: check out main. For push: check out the branch.
          ref: ${{ github.head_ref || github.ref_name }}
          token: ${{ secrets.GOOGLEWORKSPACE_BOT_TOKEN }}

      - name: Install Rust
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@2df7dbab909c49ab7d3382d05da469f3f975c2d6 # v0.0.7

      - name: Cache cargo
        uses: Swatinem/rust-cache@ad397744b0d591a723ab90405b7247fac0e6b8db # v2
        with:
          key: generate-skills-ubuntu

      - name: Generate skills
        run: cargo run -- generate-skills

      - name: Check for changes
        id: diff
        run: |
          if git diff --quiet skills/ docs/skills.md; then
            echo "changed=false" >> "$GITHUB_OUTPUT"
          else
            echo "changed=true" >> "$GITHUB_OUTPUT"
          fi

      # --- Cron / workflow_dispatch: open a PR against main ---
      - name: Create changeset for sync PR
        if: >-
          steps.diff.outputs.changed == 'true' &&
          (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
        run: |
          mkdir -p .changeset
          cat > .changeset/sync-skills.md << 'EOF'
          ---
          "@googleworkspace/cli": patch
          ---

          Sync generated skills with latest Google Discovery API specs
          EOF

      - name: Create or update sync PR
        if: >-
          steps.diff.outputs.changed == 'true' &&
          (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
        uses: peter-evans/create-pull-request@22a9089034f40e5a961c8808d113e2c98fb63676 # v7
        with:
          token: ${{ secrets.GOOGLEWORKSPACE_BOT_TOKEN }}
          branch: chore/sync-skills
          title: "chore: sync skills with Discovery API"
          body: |
            Automated PR — the Google Discovery API specs have changed and the
            generated skill files are out of date.

            Created by the **Generate Skills** workflow (`generate-skills.yml`).
          commit-message: "chore: regenerate skills from Discovery API"
          add-paths: |
            skills/
            docs/skills.md
            .changeset/sync-skills.md
          delete-branch: true

      # --- Push events (non-main branches): commit directly ---
      - name: Commit and push if changed
        if: >-
          steps.diff.outputs.changed == 'true' &&
          github.event_name == 'push'
        env:
          GITHUB_TOKEN: ${{ secrets.GOOGLEWORKSPACE_BOT_TOKEN }}
        run: |
          git config user.name  "googleworkspace-bot"
          git config user.email "googleworkspace-bot@users.noreply.github.com"

          git add skills/ docs/skills.md
          git commit -m "chore: regenerate skills [skip ci]"
          git push
