Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning about being ahead of release #525

Merged
merged 10 commits into from
Dec 22, 2024
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
14 changes: 14 additions & 0 deletions docs/.overrides/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block content %}
<div id="version-warning" style="min-height: 120px"></div>
<script>
fetch('/version-warning.html').then(r => {
if (r.ok) {
r.text().then(text => { document.getElementById('version-warning').innerHTML = text })
} else {
r.text().then(text => { console.error('failed to fetch ahead-warning.html:', {r, text})})
}
})
</script>
{{ super() }}
{% endblock %}
62 changes: 62 additions & 0 deletions docs/_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// cloudflare worker to building warning if the docs are ahead of the latest release
// see https://developers.cloudflare.com/pages/functions/advanced-mode/

export default {
async fetch(request, env) {
const url = new URL(request.url)
if (url.pathname === '/version-warning.html') {
try {
const html = await versionWarning(request, env)
return new Response(html, { headers: {'Content-Type': 'text/html', 'Cache-Control': 'max-age=1800'} })
} catch (e) {
console.error(e)
return new Response(
`Error getting ahead HTML: ${e}`,
{ status: 500, headers: {'Content-Type': 'text/plain'} }
)
}
} else {
return env.ASSETS.fetch(request)
}
},
}

// env looks like
// {"CF_PAGES":"1","CF_PAGES_BRANCH":"ahead-warning","CF_PAGES_COMMIT_SHA":"...","CF_PAGES_URL":"https://..."}
async function versionWarning(request, env) {
const headers = new Headers({
'User-Agent': request.headers.get('User-Agent') || 'pydantic-ai-docs',
'Accept': 'application/vnd.github.v3+json',
})
const r1 = await fetch('https://api.github.com/repos/pydantic/pydantic-ai/releases/latest', {headers})
if (!r1.ok) {
const text = await r1.text()
throw new Error(`Failed to fetch latest release, response status ${r1.status}:\n${text}`)
}
const {html_url, name, tag_name} = await r1.json()
const r2 = await fetch(
`https://api.github.com/repos/pydantic/pydantic-ai/compare/${tag_name}...${env.CF_PAGES_COMMIT_SHA}`,
{headers}
)
if (!r2.ok) {
const text = await r2.text()
throw new Error(`Failed to fetch compare, response status ${r2.status}:\n${text}`)
}
const {ahead_by} = await r2.json()

if (ahead_by === 0) {
return `<div class="admonition success" style="margin: 0">
<p class="admonition-title">Version</p>
<p>Showing documentation for the latest release <a href="${html_url}">${name}</a>.</p>
</div>`
}

return `<div class="admonition info" style="margin: 0">
<p class="admonition-title">Version Notice</p>
<p>
${env.CF_PAGES_BRANCH === 'main' ? '' : `(<b>${env.CF_PAGES_BRANCH}</b> preview)`}
This documentation is ahead of the latest release by <b>${ahead_by}</b> commit${ahead_by === 1 ? '' : 's'}.
You may see documentation for features not yet supported in the latest release <a href="${html_url}">${name}</a>.
</p>
</div>`
}
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extra:

theme:
name: "material"
custom_dir: docs/overrides
custom_dir: docs/.overrides
palette:
- media: "(prefers-color-scheme)"
scheme: default
Expand Down Expand Up @@ -105,7 +105,7 @@ validation:
anchors: warn

extra_css:
- 'extra/tweaks.css'
- "extra/tweaks.css"
# used for analytics
extra_javascript:
- "/flarelytics/client.js"
Expand Down
Loading