ycdemo / fastapi / .github/ workflows/ translate.yml

name: Translate

on:
  schedule:
    - cron: "0 5 1,15 * *"  # Run at 05:00 on the 1st and 15th of every month

  workflow_dispatch:
    inputs:
      debug_enabled:
        description: Run with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)
        required: false
        default: "false"
      command:
        description: Command to run
        type: choice
        required: true
        options:
          - translate-page
          - translate-lang
          - update-outdated
          - add-missing
          - update-and-add
          - remove-removable
      language:
        description: Language to translate to as a letter code (e.g. "es" for Spanish)
        type: string
        required: true
      en_path:
        description: File path in English to translate (e.g. docs/en/docs/index.md)
        type: string
        required: false
        default: ""
      max:
        description: Maximum number of items to translate (e.g. 10)
        type: number
        required: false
        default: 10

permissions: {}

jobs:
  langs:
    runs-on: ubuntu-latest
    outputs:
      langs: ${{ steps.show-langs.outputs.langs }}
      commands: ${{ steps.show-langs.outputs.commands }}
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Set up Python
        uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
        with:
          python-version-file: ".python-version"
      - name: Setup uv
        uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
        with:
          version: "latest-known"
          cache-dependency-glob: |
            pyproject.toml
            uv.lock
      - name: Install Dependencies
        run: uv sync --locked --no-dev --group github-actions --group translations
      - name: Export Language Codes
        id: show-langs
        run: |
          echo "langs=$(uv run ./scripts/translate.py llm-translatable-json)" >> $GITHUB_OUTPUT
          echo "commands=$(uv run ./scripts/translate.py commands-json)" >> $GITHUB_OUTPUT
        env:
          LANGUAGE: ${{ github.event.inputs.language }}
          COMMAND: ${{ github.event.inputs.command }}

  translate:
    if: github.repository_owner == 'fastapi'
    needs: langs
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    strategy:
      matrix:
        lang: ${{ fromJson(needs.langs.outputs.langs) }}
        command: ${{ fromJson(needs.langs.outputs.commands) }}
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Set up Python
        uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
        with:
          python-version-file: ".python-version"
      - name: Setup uv
        uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
        with:
          version: "latest-known"
          cache-dependency-glob: |
            pyproject.toml
            uv.lock
      - name: Install Dependencies
        run: uv sync --locked --no-dev --group github-actions --group translations
      # Allow debugging with tmate
      - name: Setup tmate session
        uses: mxschmitt/action-tmate@35b54afac29c97fb54faba5b513f8fbd1882f113 # v3.24
        if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }}
        with:
          limit-access-to-actor: true
        env:
          GITHUB_TOKEN: ${{ github.token }}
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} # zizmor: ignore[secrets-outside-env]
      - name: FastAPI Translate
        run: uv run ./scripts/translate.py "$COMMAND"
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} # zizmor: ignore[secrets-outside-env]
          LANGUAGE: ${{ matrix.lang }}
          EN_PATH: ${{ github.event.inputs.en_path }}
          COMMAND: ${{ matrix.command }}
          MAX: ${{ github.event.inputs.max }}
      - name: Get PR Submit token
        if: github.event_name == 'schedule'
        id: pr-submit
        uses: tiangolo/pr-submit@d802fdf59bde80bc3eb8bd3259f4cbeec63de4aa # 0.0.1
      - name: Create pull request
        if: github.event_name == 'schedule'
        run: |
          gh auth setup-git
          uv run ./scripts/translate.py make-pr
        env:
          GITHUB_TOKEN: ${{ steps.pr-submit.outputs.token }}
          LANGUAGE: ${{ matrix.lang }}
          COMMAND: ${{ matrix.command }}
      - name: Get PR Push token
        if: github.event_name == 'workflow_dispatch'
        id: pr-push
        uses: tiangolo/pr-push@f336b3817f32ea9b8273a8c15f8ecb739ac38167 # 0.0.4
      - name: Commit and push changes
        if: github.event_name == 'workflow_dispatch'
        env:
          COMMAND: ${{ matrix.command }}
          GH_TOKEN: ${{ steps.pr-push.outputs.token }}
          LANGUAGE: ${{ matrix.lang }}
        run: |
          gh auth setup-git
          uv run ./scripts/translate.py push