Skip to content
Draft
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v6

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v6
- name: Set up uv with Python ${{ matrix.python-version }}
uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python-version }}

activate-environment: true

- name: Install dependencies
run: pip install -e .[test]
run: uv pip install -e '.[test]'

- name: Run tests
run: pytest tests --ignore-glob='tests/test_docs_*.py' -k "not example"
Expand Down
91 changes: 86 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ jobs:
echo "Release must run against a tag, got '${{ github.ref }}'" >&2
exit 1

- uses: actions/setup-python@v6
- uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python-version }}
activate-environment: true

- name: Install dependencies
run: pip install -e .[test]
run: uv pip install -e '.[test]'

- name: Run tests
run: pytest --ignore=tests/test_docs_publishing.py -k "not example"
Expand Down Expand Up @@ -71,9 +72,79 @@ jobs:
path: dist/
if-no-files-found: error

stubs-check:
name: Check stubs
uses: ./.github/workflows/stubs.yml

stubs-series:
name: Check for an existing stub release
runs-on: ubuntu-latest
outputs:
publish: ${{ steps.check.outputs.publish }}
steps:
- uses: actions/checkout@v6

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

- id: check
name: Require a matching tag and publish only once per SDK series
run: python stubs/tools/release_version.py --series-check "$GITHUB_REF_NAME"

stubs-build:
name: Build stub distribution
needs: [test, stubs-check, stubs-series]
if: needs.stubs-series.outputs.publish == 'true'
uses: ./.github/workflows/stubs-build.yml

# Stubs are published first so that `serpapi[typing]` resolves as soon as the SDK is available.
publish-stubs:
name: Publish serpapi-stubs
needs: [build, stubs-series, stubs-build]
if: needs.stubs-series.outputs.publish == 'true'
runs-on: ubuntu-latest
environment:
name: pypi-stubs
url: https://pypi.org/p/serpapi-stubs
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v6

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

- uses: actions/download-artifact@v8
with:
name: stubs-dist
path: dist/

- id: final-check
name: Do not supersede an installable stub series
run: python stubs/tools/release_version.py --series-check "$GITHUB_REF_NAME"

- name: Publish to PyPI
if: steps.final-check.outputs.publish == 'true'
uses: pypa/gh-action-pypi-publish@release/v1

publish:
name: Publish release
needs: [build]
needs: [test, build, stubs-check, stubs-series, stubs-build, publish-stubs]
# A status function is needed so skipped stub jobs do not skip this job; unlike `always()`,
# `!cancelled()` still lets a cancelled run stop before the SDK is published.
if: >-
!cancelled() &&
needs.test.result == 'success' &&
needs.build.result == 'success' &&
needs.stubs-check.result == 'success' &&
needs.stubs-series.result == 'success' &&
(needs.stubs-build.result == 'success' ||
(needs.stubs-build.result == 'skipped' && needs.stubs-series.outputs.publish == 'false')) &&
(needs.publish-stubs.result == 'success' ||
(needs.publish-stubs.result == 'skipped' && needs.stubs-series.outputs.publish == 'false'))
runs-on: ubuntu-latest
environment:
name: pypi
Expand Down Expand Up @@ -101,6 +172,9 @@ jobs:
smoke-test:
name: Smoke test published package
needs: [publish]
# `publish` still runs when the stub jobs are skipped, but a plain `needs` would skip this job
# because its grandparents were skipped.
if: ${{ !cancelled() && needs.publish.result == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v6
Expand All @@ -110,9 +184,10 @@ jobs:
- name: Wait for PyPI availability
run: |
VERSION=${GITHUB_REF_NAME#v}
echo "Waiting for serpapi==$VERSION on PyPI..."
echo "Waiting for serpapi==$VERSION and its serpapi-stubs series on PyPI..."
for i in $(seq 1 12); do
if pip index versions serpapi 2>/dev/null | grep -q "$VERSION"; then
if pip index versions serpapi 2>/dev/null | grep -q "$VERSION" \
&& pip index versions serpapi-stubs 2>/dev/null | grep -qF "$VERSION."; then
echo "Available!"
exit 0
fi
Expand All @@ -126,6 +201,12 @@ jobs:
VERSION=${GITHUB_REF_NAME#v}
pip install "serpapi==$VERSION"

- name: Install typing extra
run: |
VERSION=${GITHUB_REF_NAME#v}
pip install "serpapi[typing]==$VERSION"
pip show serpapi-stubs

- name: Verify import and version
env:
API_KEY: ${{ secrets.API_KEY }}
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/stubs-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build serpapi-stubs

# Builds the stub distribution under its next release version and uploads it as the `stubs-dist` artifact.
on:
workflow_call:

permissions:
contents: read

jobs:
build:
name: Build stub distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

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

- name: Build sdist and wheel
working-directory: stubs
run: |
pip install build
python tools/release_version.py
python -m build

- uses: actions/upload-artifact@v7
with:
name: stubs-dist
path: stubs/dist/
if-no-files-found: error
118 changes: 118 additions & 0 deletions .github/workflows/stubs-refresh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: Refresh serpapi-stubs

# Regenerates the engine snapshot and stubs from the SerpApi playground and opens (or updates) a PR.
# The PR is closed again if a later run finds that the stubs match the default branch.
# Pull requests opened with GITHUB_TOKEN do not trigger other workflows, so set the
# STUBS_REFRESH_TOKEN secret (fine-grained PAT or GitHub App token) to run checks on the PR.
on:
schedule:
- cron: "17 4 * * 1"
workflow_dispatch:

concurrency:
group: serpapi-stubs-refresh
cancel-in-progress: false

permissions:
contents: write
pull-requests: write

env:
BRANCH: bot/refresh-serpapi-stubs

jobs:
refresh:
name: Refresh stubs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
ref: ${{ github.event.repository.default_branch }}
token: ${{ secrets.STUBS_REFRESH_TOKEN || github.token }}
fetch-depth: 0

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

- name: Fetch engine metadata
run: python stubs/tools/fetch_engines.py

- name: Regenerate stubs
run: python stubs/tools/generate_stubs.py

- name: Open or update pull request
env:
GH_TOKEN: ${{ secrets.STUBS_REFRESH_TOKEN || github.token }}
run: |
bot="github-actions[bot]"

# Ignore fork PRs that happen to use the same branch name.
open_pr=$(gh pr list --head "$BRANCH" --state open --json number,isCrossRepository \
--jq '[.[] | select(.isCrossRepository | not)][0].number // empty')

# A maintainer may have pushed fixes to the open refresh branch. Never discard those. An open pull
# request always has its branch, so a failed fetch is an error and has to stop the run.
branch_tip=""
human_commits=false
if [ -n "$open_pr" ]; then
git fetch --quiet origin "$BRANCH"
branch_tip=$(git rev-parse FETCH_HEAD)
commit_authors=$(git log --format='%an/%cn' "HEAD..$branch_tip")
if [ -n "$commit_authors" ] && grep -qFxv "$bot/$bot" <<< "$commit_authors"; then
human_commits=true
fi
fi

if [ "$human_commits" = true ]; then
echo "::warning::origin/$BRANCH has commits that this workflow did not make, so pull request #$open_pr was left unchanged."
exit 0
fi

if [ -z "$(git status --porcelain -- stubs/)" ]; then
echo "Stubs are up to date."
if [ -n "$open_pr" ]; then
gh pr close "$open_pr" --delete-branch \
--comment "Closing this refresh PR because the generated stubs now match the default branch."
fi
exit 0
fi

git config user.name "$bot"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git switch -C "$BRANCH"
git add stubs/
git commit -m "Refresh serpapi-stubs from the SerpApi playground"

if [ -n "$open_pr" ]; then
# Leave the branch and its checks alone when it already has these stubs, whatever else changed
# on the default branch.
if [ "$(git rev-parse 'HEAD:stubs')" = "$(git rev-parse "$branch_tip:stubs")" ]; then
echo "Pull request #$open_pr already has these stubs."
exit 0
fi
# The lease rejects the push if the branch changed after it was fetched above.
git push --force-with-lease="$BRANCH:$branch_tip" origin "$BRANCH"
else
# Without an open PR the remote branch is left over from a merged or closed one. Updating it
# would carry every change made to the default branch since then, and GitHub can refuse a
# push that changes workflow files that way, so start from a fresh branch.
git push --quiet origin --delete "$BRANCH" 2>/dev/null || true
git push --force origin "$BRANCH"
fi

{
echo "Automated weekly refresh of \`stubs/data/engines.json\` and the generated stubs."
echo
echo "Merging this PR publishes a new \`serpapi-stubs\` release when \`serpapi/\` is unchanged since the latest SDK tag; otherwise the stubs ship with the next SDK release."
echo
echo '```'
git --no-pager diff --stat HEAD~1 HEAD
echo '```'
} > "$RUNNER_TEMP/body.md"
if [ -n "$open_pr" ]; then
gh pr edit "$open_pr" --body-file "$RUNNER_TEMP/body.md"
else
gh pr create --base "${{ github.event.repository.default_branch }}" --head "$BRANCH" \
--title "Refresh serpapi-stubs" --body-file "$RUNNER_TEMP/body.md"
fi
Loading
Loading