Skip to content

Commit 68f119c

Browse files
Automatically create docs PR on release (#1965)
* Adding auto switcher bump, sending main branch docs to dev * Use auto commit action to push switcher.json updates * Adding switcher update script * Using custom commit message * Point latest to actually latest version * Use PR instead of directly pushing to main * Refine comment * Use auto-commits * Remove unused permissions * Remove obsolete token * Setup permission, only run on release * Create PR with updated switcher.json * Format with stock black * Update .github/workflows/deploy-docs.yml * Update .github/workflows/deploy-docs.yml --------- Co-authored-by: Frank Anema <[email protected]>
1 parent 2921126 commit 68f119c

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

.github/workflows/deploy-docs.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ on:
99
types:
1010
- published
1111

12+
permissions:
13+
# Give the GITHUB_TOKEN write permission to open a PR with the changes to the switcher.json file.
14+
contents: write
15+
pull-requests: write
16+
1217
jobs:
1318
run:
1419
runs-on: ubuntu-latest
20+
1521
steps:
1622
- uses: actions/checkout@v4
1723
with:
1824
fetch-depth: 0
25+
ref: main
1926

2027
- name: Setup Micromamba env
2128
uses: mamba-org/setup-micromamba@v1
@@ -38,13 +45,29 @@ jobs:
3845
make clean html linkcheck
3946
popd
4047
48+
- name: Update switcher and latest version
49+
if: ${{ github.event_name == 'release' }}
50+
run: |
51+
python docs/update_switcher.py --version ${{ github.ref_name }}
52+
53+
- name: Create PR
54+
if: ${{ github.event_name == 'release' }}
55+
uses: peter-evans/create-pull-request@v3
56+
with:
57+
commit-message: "docs: Update switcher.json for ${{ github.ref_name }}"
58+
title: "docs: Update switcher.json for ${{ github.ref_name }}"
59+
body: "This PR updates the switcher.json file."
60+
branch: "docs/update-switcher-${{ github.ref_name }}"
61+
base: "main"
62+
labels: "documentation"
63+
4164
- name: Publish to Github Pages on main
4265
if: ${{ github.ref == 'refs/heads/main' }}
4366
uses: peaceiris/actions-gh-pages@v4
4467
with:
4568
github_token: ${{ secrets.GITHUB_TOKEN }}
4669
publish_dir: docs/_build/html/
47-
destination_dir: latest
70+
destination_dir: dev
4871
keep_files: false
4972

5073
- name: Publish to Github Pages on release

docs/_static/switcher.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
[
2+
{
3+
"version": "dev",
4+
"url": "https://python-visualization.github.io/folium/dev/"
5+
},
26
{
37
"version": "latest",
4-
"url": "https://python-visualization.github.io/folium/latest/"
8+
"url": "https://python-visualization.github.io/folium/v0.16.0/"
59
},
610
{
711
"version": "v0.16.0",

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@
108108
# documentation.
109109
html_theme_options = {
110110
"switcher": {
111-
"json_url": "https://python-visualization.github.io/folium/latest/_static/switcher.json",
112-
"version_match": "latest" if ".dev" in version else version,
111+
"json_url": "https://python-visualization.github.io/folium/dev/_static/switcher.json",
112+
"version_match": "dev" if ".dev" in version else version,
113113
},
114114
"navbar_start": ["navbar-logo", "version-switcher"],
115115
"footer_start": ["version", "copyright", "sphinx-version", "theme-version"],

docs/update_switcher.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
This script is used to update switcher.json on docs releases. It adds the new version to
3+
the list of versions and sets the latest version to the new version.
4+
"""
5+
6+
import argparse
7+
import json
8+
import os
9+
10+
11+
def main():
12+
# Define CLI arguments
13+
parser = argparse.ArgumentParser(description="Update switcher.json")
14+
parser.add_argument(
15+
"--version", "-v", required=True, type=str, help="The new version to add"
16+
)
17+
args = parser.parse_args()
18+
19+
# Setup path to switcher.json (relative to this script) and load it
20+
switcher_path = os.path.join(os.path.dirname(__file__), "_static", "switcher.json")
21+
with open(switcher_path) as f:
22+
switcher = json.load(f)
23+
24+
# Find index of 'latest' entry
25+
latest_index = None
26+
for i, version in enumerate(switcher):
27+
if version["version"] == "latest":
28+
latest_index = i
29+
break
30+
if latest_index is None:
31+
raise ValueError("'latest' version not found in switcher.json")
32+
33+
# Add the new version to the list of versions (we always insert it after latest)
34+
new_version = {
35+
"version": args.version,
36+
"url": f"https://python-visualization.github.io/folium/{args.version}/",
37+
}
38+
39+
# Update the latest version
40+
switcher[latest_index]["url"] = new_version["url"]
41+
42+
# Make sure version is unique
43+
if any(version["version"] == args.version for version in switcher):
44+
print(
45+
f"Version {args.version} already exists in switcher.json. Not adding it again."
46+
)
47+
else:
48+
switcher.insert(latest_index + 1, new_version)
49+
50+
# Write the updated switcher.json
51+
with open(switcher_path, "w") as f:
52+
json.dump(switcher, f, indent=2)
53+
54+
55+
if __name__ == "__main__":
56+
main()

0 commit comments

Comments
 (0)