# 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@v4
        with:
          # For cron/dispatch: check out main. For push: check out the branch.
          ref: ${{ github.head_ref || github.ref_name }}
          token: ${{ secrets.GITHUB_TOKEN }}

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

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

      - name: Cache cargo
        uses: Swatinem/rust-cache@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@v7
        with:
          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'
        run: |
          git config user.name  "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

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