Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions .github/workflows/update_platform_api_client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: Update Platform API client

on:
workflow_dispatch:
inputs:
version:
description: "Published Platform API client version (for example, 4.42.0)"
required: true
type: string
repository_dispatch:
types: [platform-api-client-published]

permissions:
contents: read

concurrency:
group: update-platform-api-client-${{ inputs.version || github.event.client_payload.version }}
cancel-in-progress: false

jobs:
update:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v6
with:
ref: ${{ github.event.repository.default_branch }}
persist-credentials: false

- uses: actions/setup-python@v6
with:
python-version: "3.12"

- name: Update dependency and compatibility note
id: update
env:
REQUESTED_VERSION: ${{ inputs.version || github.event.client_payload.version }}
run: |
python - <<'PY'
import os
import re
from pathlib import Path

version = os.environ["REQUESTED_VERSION"].removeprefix("v")
stable_version = r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)"
if not re.fullmatch(stable_version, version):
raise SystemExit("version must be a stable X.Y.Z release, optionally prefixed with v")

requirements_path = Path("requirements.txt")
requirements = requirements_path.read_text()
pattern = r"^platform-api-python-client==([^\s]+)$"
matches = re.findall(pattern, requirements, flags=re.MULTILINE)
if len(matches) != 1 or not re.fullmatch(stable_version, matches[0]):
raise SystemExit("Expected exactly one stable Platform API client pin")
current = matches[0]

readme_path = Path("README.md")
readme = readme_path.read_text()
note = f"This client uses Platform API `{current}`."
if readme.count(note) != 1:
raise SystemExit("Expected one README compatibility note matching the current pin")

# Ignore duplicate or out-of-order publication events; never downgrade main.
changed = tuple(map(int, version.split("."))) > tuple(map(int, current.split(".")))
if changed:
requirements_path.write_text(re.sub(
pattern, f"platform-api-python-client=={version}", requirements, flags=re.MULTILINE
))
readme_path.write_text(readme.replace(note, f"This client uses Platform API `{version}`."))
else:
print(f"Skipping {version}: main already uses {current}")

with open(os.environ["GITHUB_OUTPUT"], "a") as output:
output.write(f"version={version}\nprevious={current}\nchanged={str(changed).lower()}\n")
PY

- name: Verify the release is installable from PyPI
if: steps.update.outputs.changed == 'true'
env:
CLIENT_VERSION: ${{ steps.update.outputs.version }}
run: >-
python -m pip download --no-deps --python-version 3.10
--index-url https://pypi.org/simple
--dest "$RUNNER_TEMP/platform-api-client"
"platform-api-python-client==$CLIENT_VERSION"

- name: Check PR token configuration
if: steps.update.outputs.changed == 'true'
env:
UPDATE_TOKEN: ${{ secrets.SDK_UPDATE_TOKEN }}
run: |
if [ -z "$UPDATE_TOKEN" ]; then
echo "::error::Configure SDK_UPDATE_TOKEN with contents and pull requests write access. See README.md."
exit 1
fi

- name: Open upgrade PR without rewriting existing branches
if: steps.update.outputs.changed == 'true'
env:
GH_TOKEN: ${{ secrets.SDK_UPDATE_TOKEN }}
CLIENT_VERSION: ${{ steps.update.outputs.version }}
PREVIOUS_VERSION: ${{ steps.update.outputs.previous }}
BASE_BRANCH: ${{ github.event.repository.default_branch }}
run: |
UPDATE_BRANCH="automation/platform-api-client-$CLIENT_VERSION"
EXISTING_PR=$(gh pr list --repo "$GITHUB_REPOSITORY" \
--head "$UPDATE_BRANCH" --state all --json url --jq '.[0].url // empty')
if [ -n "$EXISTING_PR" ]; then
echo "Leaving existing PR and its commits unchanged: $EXISTING_PR"
exit 0
fi

gh auth setup-git --hostname github.com
if git ls-remote --exit-code --heads origin "refs/heads/$UPDATE_BRANCH"; then
# A previous run may have pushed successfully but failed to open the PR.
echo "Using existing branch without changing its commits: $UPDATE_BRANCH"
else
STATUS=$?
if [ "$STATUS" -ne 2 ]; then
exit "$STATUS"
fi
git switch -c "$UPDATE_BRANCH"
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git add -- requirements.txt README.md
git commit -m "[SDK] Upgrade Platform API client to $CLIENT_VERSION"
# Never force-push: a concurrently created branch must also be preserved.
git push origin "HEAD:refs/heads/$UPDATE_BRANCH"
fi

BODY_FILE="$RUNNER_TEMP/platform-api-client-pr.md"
cat > "$BODY_FILE" <<EOF
Upgrade platform-api-python-client from $PREVIOUS_VERSION to $CLIENT_VERSION and update the README compatibility note.

The package was found on PyPI for Python 3.10. The existing PR workflows run the SDK/CLI sanity tests, formatting, lint, and type checks.

Review API compatibility and add any required SDK, CLI, or example changes before merging. Retries preserve this PR and its commits. This PR does not bump the SDK version or publish a release.
EOF

gh pr create --repo "$GITHUB_REPOSITORY" --base "$BASE_BRANCH" \
--head "$UPDATE_BRANCH" \
--title "[SDK] Upgrade Platform API client to $CLIENT_VERSION" \
--body-file "$BODY_FILE"
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,58 @@ To run all the tests, use:
pytest
```

### Automated Platform API client updates

The `Update Platform API client` workflow opens an upgrade PR in this repository
after a generated API client is published. It updates `requirements.txt` and the
compatibility note above. Existing PR workflows run the SDK/CLI sanity tests,
formatting, lint, and type checks. Merging requires review; SDK version bumps and
releases remain separate.

Configure the repository Actions secret `SDK_UPDATE_TOKEN` with a bot's fine-grained
personal access token scoped to this repository, with **Contents: Read and write**
and **Pull requests: Read and write**. Using a bot token lets the resulting PR run
CI automatically; PR events created with `GITHUB_TOKEN` require workflow approval
([GitHub token documentation](https://docs.github.com/en/actions/concepts/security/github_token)).

After the generated client's PyPI publish job succeeds, add this step to its
publishing workflow (use `needs` if it is in a separate job):

```yaml
- name: Request SDK upgrade
env:
GH_TOKEN: ${{ secrets.SDK_UPDATE_TOKEN }}
# Replace this expression with the publishing workflow's version output.
CLIENT_VERSION: ${{ steps.version.outputs.version }}
run: |
jq -n --arg version "$CLIENT_VERSION" \
'{event_type: "platform-api-client-published", client_payload: {version: $version}}' \
| gh api --method POST repos/CentML/centml-python-client/dispatches --input -
```

The publishing repository also needs the `SDK_UPDATE_TOKEN` secret. Its token
must have **Contents: Read and write** access to this SDK repository to dispatch
the event; the publisher's default `GITHUB_TOKEN` cannot access another repository.

For a manual run or a retry after PyPI propagation, use the Actions UI or:

```bash
gh workflow run update_platform_api_client.yml \
--repo CentML/centml-python-client -f version=4.42.0
```

The workflow runs on Python 3.12 and accepts stable `X.Y.Z` versions with an optional
`v` prefix. It explicitly checks that PyPI has a distribution compatible with the
SDK's minimum supported version, Python 3.10. It skips versions at or below the pin
on the default branch. Repeated requests for the same newer version
leave its existing PR unchanged, including handwritten SDK/CLI fixes. A closed PR
is also left alone; reopen it manually if the upgrade should proceed. If a run
pushed the branch but failed to open a PR, retrying opens a PR from that branch
without rewriting it. Different versions get separate branches so a delayed event
cannot replace a newer pending upgrade. Runs for different version inputs use
separate concurrency groups so incoming events do not replace another version's
pending run. After merging a newer version, close any older pending upgrade PRs.

### Common Issues

- **`SSL` certificate on `MacOS`**
Expand Down
Loading