Skip to content
Open
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
22 changes: 18 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ name: Docs

# FastAPI-style docs pipeline: every PR and main push builds the MkDocs
# Material site to validate it; pushes to main additionally deploy the HTML
# to GitHub Pages. Docs track main (latest), independent of the tag-driven
# release.yml flow.
# to GitHub Pages. Publishing a GitHub Release also rebuilds and deploys:
# the Release Notes page renders published releases at build time, so the
# tag-driven release.yml flow refreshes the docs without touching them.
on:
push:
branches: [main]
Expand All @@ -20,6 +21,8 @@ on:
- 'mkdocs.yml'
- 'requirements-doc.txt'
- '.github/workflows/docs.yml'
release:
types: [published]
workflow_dispatch:

permissions:
Expand All @@ -33,24 +36,35 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
# Release events carry a tag ref; docs always track main (latest).
- uses: actions/checkout@v4
with:
ref: ${{ github.event_name == 'release' && 'refs/heads/main' || github.ref }}
- uses: actions/setup-python@v5
with:
python-version: '3.12'
# mkdocstrings imports the real package, so install it alongside the
# doc toolchain (httpx/pydantic etc. come in as runtime dependencies).
- run: python -m pip install -r requirements-doc.txt .
- name: Build HTML
# GITHUB_TOKEN lifts the anonymous GitHub API rate limit for the
# release-notes rendering in docs/hooks.py (public data, read-only).
env:
GITHUB_TOKEN: ${{ github.token }}
run: mkdocs build --strict
- name: Upload site artifact
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
if: >-
(github.event_name == 'push' && github.ref == 'refs/heads/main')
|| github.event_name == 'release'
uses: actions/upload-pages-artifact@v3
with:
path: site

deploy:
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
if: >-
(github.event_name == 'push' && github.ref == 'refs/heads/main')
|| github.event_name == 'release'
runs-on: ubuntu-latest
permissions:
pages: write
Expand Down
14 changes: 4 additions & 10 deletions docs/en/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ description: "Release notes and changelog pointers for the dashscope Python SDK,

The authoritative, per-release changelog is published on
[GitHub Releases](https://github.com/dashscope/dashscope-sdk-python/releases).
This page summarizes the major lines and links to recent tags.
The recent-releases table below is rendered from GitHub Releases at docs
build time, so it refreshes automatically when a release is published;
each tag links to its full release notes.
Current SDK version: **{{ sdk_version }}**.

## Recent releases

| Tag | Notes |
| --- | --- |
| v2.1.0 | [See release page](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.1.0) |
| v2.0.4 | [See release page](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.4) |
| v2.0.3 | [See release page](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.3) |
| v2.0.2 | [See release page](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.2) |
| v2.0.1 | [See release page](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.1) |
{{ release_notes }}

## 2.0 highlights

Expand Down
92 changes: 88 additions & 4 deletions docs/hooks.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
# -*- coding: utf-8 -*-
"""MkDocs hooks: surface the SDK version from dashscope/version.py in pages.
"""MkDocs hooks: build-time substitution for version and release notes.

Pages opt in with the ``{{ sdk_version }}`` placeholder; the value is read
from the source of truth at build time so docs never drift from the package.
Pages opt in with placeholders; values are read from their source of
truth at build time so docs never drift:

- ``{{ sdk_version }}`` <- dashscope/version.py
- ``{{ release_notes }}`` <- published GitHub Releases (latest-release
table linking to each release page), so publishing a release is all it
takes to refresh the docs.
"""
import json
import os
import re
import urllib.request
from pathlib import Path

from mkdocs.structure.toc import AnchorLink, TableOfContents

# Reference pages listing dozens of API symbols: even a section+symbol
# outline floods the sidebar (80-110 entries), so their "On this page"
# nav keeps only the h2 section titles. Applies to both locales.
# release-notes.md joins them for a consistent section-only outline.
_SECTION_ONLY_TOC_PAGES = {
"reference/agentstudio.md",
"reference/legacy.md",
"reference/resources.md",
"release-notes.md",
}

_RELEASES_URL = (
"https://api.github.com/repos/dashscope/dashscope-sdk-python"
"/releases?per_page=20"
)
_RELEASES_PAGE = "https://github.com/dashscope/dashscope-sdk-python/releases"
_RELEASES_SHOWN = 5
# Fetch once per build process; the i18n plugin runs two locale passes.
_RELEASES_CACHE = None

# The theme's static 404.html template renders once per i18n pass and the
# last (zh) pass overwrites site/404.html, shipping a mixed-language shell.
# Stash the en pass render context and reuse it for every later pass.
Expand All @@ -33,6 +52,63 @@ def _read_version(docs_dir: str) -> str:
return match.group(1) if match else "unknown"


def _fetch_releases():
"""Published releases, newest first; None when the API is unreachable.

Drafts are excluded so the skeleton rendered by release.yml never
leaks into the docs before a maintainer publishes it. GITHUB_TOKEN
(set in CI) only lifts the anonymous rate limit; the data is public.
"""
global _RELEASES_CACHE
if _RELEASES_CACHE is not None:
return _RELEASES_CACHE or None
headers = {
"Accept": "application/vnd.github+json",
"User-Agent": "dashscope-sdk-docs-build",
}
token = os.environ.get("GITHUB_TOKEN")
if token:
headers["Authorization"] = "Bearer " + token
try:
request = urllib.request.Request(_RELEASES_URL, headers=headers)
with urllib.request.urlopen(request, timeout=10) as response:
releases = json.load(response)
releases = [r for r in releases if not r.get("draft")]
releases.sort(key=lambda r: r.get("published_at") or "", reverse=True)
_RELEASES_CACHE = releases
return releases or None
except Exception:
_RELEASES_CACHE = []
return None


def _render_release_notes(lang: str) -> str:
releases = _fetch_releases()
zh = lang == "zh"
if not releases:
return (
"构建时未能加载 GitHub Releases,完整变更日志请参阅"
f"[GitHub Releases]({_RELEASES_PAGE})。"
if zh
else "Release notes could not be loaded at build time; see "
f"[GitHub Releases]({_RELEASES_PAGE}) for the full changelog."
)
releases = releases[:_RELEASES_SHOWN]
parts = ["## 近期发布" if zh else "## Recent releases", ""]
parts.append("| Tag | 说明 |" if zh else "| Tag | Notes |")
parts.append("| --- | --- |")
for release in releases:
tag = release.get("tag_name") or ""
if release.get("prerelease"):
tag += "(预发布)" if zh else " (pre-release)"
date = (release.get("published_at") or "")[:10]
label = "发布说明" if zh else "Release notes"
parts.append(
f"| {tag} | {date} · [{label}]({release.get('html_url')}) |",
)
return "\n".join(parts)


def on_config(config):
config.extra["sdk_version"] = _read_version(config.docs_dir)
# mkdocstrings inserts its stylesheet on every per-locale on_config
Expand Down Expand Up @@ -65,10 +141,18 @@ def on_page_content(html, *, page, config, files):


def on_page_markdown(markdown, *, page, config, files):
return markdown.replace(
markdown = markdown.replace(
"{{ sdk_version }}",
config.extra.get("sdk_version", "unknown"),
)
if "{{ release_notes }}" in markdown:
# i18n folder structure prefixes src_uri with the locale (en/ zh/)
lang = "zh" if page.file.src_uri.startswith("zh/") else "en"
markdown = markdown.replace(
"{{ release_notes }}",
_render_release_notes(lang),
)
return markdown


def _snapshot_404_context(context, config):
Expand Down
13 changes: 3 additions & 10 deletions docs/zh/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ description: "dashscope Python SDK 发行说明与变更日志指引,含 2.0

逐版本的权威变更日志发布在
[GitHub Releases](https://github.com/dashscope/dashscope-sdk-python/releases)。
本页概述主要版本线并链接近期 tag。
下方近期发布表格在文档构建时从 GitHub Releases 渲染,
发布新版本后会自动刷新;点击各 tag 可查看完整发布说明。
当前 SDK 版本:**{{ sdk_version }}**。

## 近期发布

| Tag | 说明 |
| --- | --- |
| v2.1.0 | [请参阅发布页面](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.1.0) |
| v2.0.4 | [请参阅发布页面](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.4) |
| v2.0.3 | [请参阅发布页面](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.3) |
| v2.0.2 | [请参阅发布页面](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.2) |
| v2.0.1 | [请参阅发布页面](https://github.com/dashscope/dashscope-sdk-python/releases/tag/v2.0.1) |
{{ release_notes }}

## 2.0 亮点

Expand Down
28 changes: 28 additions & 0 deletions tests/cli/test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def isatty(self):
# sys.stderr patch made during fixture setup.
@pytest.fixture
def tty_stderr(monkeypatch):
# Rich gates live output on TERM/FORCE_COLOR/TTY_COMPATIBLE as well as
# isatty(); a runner exporting TERM=dumb (CI, sandboxed shells) would
# silence the spinner and fail the text assertions. Pin them so the
# fake TTY behaves identically on every runner; monkeypatch restores
# the process environment afterwards.
monkeypatch.setenv("TERM", "xterm-256color")
monkeypatch.delenv("FORCE_COLOR", raising=False)
monkeypatch.delenv("TTY_COMPATIBLE", raising=False)
monkeypatch.setenv("NO_COLOR", "1")

def install() -> _TtyStderr:
stream = _TtyStderr()
monkeypatch.setattr("sys.stderr", stream)
Expand Down Expand Up @@ -187,6 +197,24 @@ def test_repeated_polls_do_not_stack_the_suffix(
text = stream.getvalue()
assert text.count("elapsed) waiting for") == 0

def test_dumb_terminal_stays_silent(self, tty_stderr, monkeypatch):
"""Rich refuses escape sequences on TERM=dumb; silence is correct."""
stream = tty_stderr()
monkeypatch.setenv("TERM", "dumb")
monkeypatch.setattr(
"dashscope.cli.progress.time.sleep",
lambda s: None,
)
waiter = _RecordingWait(result=None)
wait_job(
_client(waiter),
"job-1",
timeout=5,
renderer=Renderer("table"),
)
waiter.calls[0][1]["_sleep"](0)
assert stream.getvalue() == ""

def test_custom_label(self, tty_stderr, monkeypatch):
stream = tty_stderr()
monkeypatch.setattr(
Expand Down
Loading