name: rust-release-prepare
on:
  workflow_dispatch:
  schedule:
    - cron: "0 */4 * * *"

concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: false

permissions:
  contents: write
  pull-requests: write

jobs:
  prepare:
    # Prevent scheduled runs on forks (no secrets, wastes Actions minutes)
    if: github.repository == 'openai/codex'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          ref: main
          fetch-depth: 0

      - name: Update models.json
        env:
          OPENAI_API_KEY: ${{ secrets.CODEX_OPENAI_API_KEY }}
        run: |
          set -euo pipefail

          client_version="99.99.99"
          terminal_info="github-actions"
          user_agent="codex_cli_rs/99.99.99 (Linux $(uname -r); $(uname -m)) ${terminal_info}"
          base_url="${OPENAI_BASE_URL:-https://chatgpt.com/backend-api/codex}"

          headers=(
            -H "Authorization: Bearer ${OPENAI_API_KEY}"
            -H "User-Agent: ${user_agent}"
          )

          url="${base_url%/}/models?client_version=${client_version}"
          curl --http1.1 --fail --show-error --location "${headers[@]}" "${url}" | jq '.' > codex-rs/models-manager/models.json

      - name: Open pull request (if changed)
        uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8
        with:
          commit-message: "Update models.json"
          title: "Update models.json"
          body: "Automated update of models.json."
          branch: "bot/update-models-json"
          reviewers: "pakrym-oai,aibrahim-oai"
          delete-branch: true
