Skip to content

Commit

Permalink
gh-38332: Add announcement banner also for PRs over stable release
Browse files Browse the repository at this point in the history
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes #12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes #12345". -->

Announcement banner should appear in sage doc both for beta release  and
PRs over beta or stable release.

Currently, as noticed while testing for
#38285, doc previews for github PRs
over a stable release won't have announcement banner at the top of the
doc. We fix it.

Test:
https://github.com/kwankyu/sage/actions/runs/9770500596/job/26971792855
https://doc-pr-53--sagemath-test.netlify.app/html/en/reference/

Changes is in https://github.com/sagemath/sage/pull/38332/commits/a9decf
4a57a11c723e38ce0dd8cdc28485fac98d, https://github.com/sagemath/sage/pul
l/38332/commits/a51b5715642b0e98fcbbd52fe8882ed75e2d059f

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - #12345: short description why this is a dependency -->
<!-- - #34567: ... -->

#38285
    
URL: #38332
Reported by: Kwankyu Lee
Reviewer(s):
  • Loading branch information
Release Manager committed Jul 9, 2024
2 parents 5ad7ee0 + 30638a1 commit 7982725
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 53 deletions.
88 changes: 54 additions & 34 deletions .github/workflows/doc-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,35 @@ jobs:
git config --global --add safe.directory $(pwd)
git config --global user.email "ci-sage@example.com"
git config --global user.name "Build documentation workflow"
# mathjax path in old doc (regex)
mathjax_path_from="[-./A-Za-z_]*/tex-chtml[.]js?v=[0-9a-f]*"
# mathjax path in new doc
mathjax_path_to=$(docker exec -e SAGE_USE_CDNS=yes BUILD /sage/sage -python -c "from sage_docbuild.conf import mathjax_path; print(mathjax_path)")
new_version=$(docker exec BUILD cat src/VERSION.txt)
mkdir -p doc/
docker cp BUILD:/sage/local/share/doc/sage/html doc/
# Wipe out chronic diffs between old doc and new doc
(cd doc && \
find . -name "*.html" | xargs sed -i -e '/class="sidebar-brand-text"/ s/Sage [0-9a-z.]* /Sage '"$new_version"' /' \
-e '/<link rel="stylesheet"/ s/?v=[0-9a-f]*"/"/' \
-e 's;'"$mathjax_path_from"';'"$mathjax_path_to"';' \
-e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d')
# Create git repo from old doc
(cd doc && \
git init && \
(echo "*.svg binary"; echo "*.pdf binary") >> .gitattributes && \
(echo ".buildinfo"; echo '*.inv'; echo '.git*'; echo '*.svg'; echo '*.pdf'; echo '*.png'; echo 'searchindex.js') > .gitignore; \
git add -A && git commit --quiet -m 'old')
# Check if we are on PR
PR_NUMBER=""
if [[ -n "$GITHUB_REF" ]]; then
if [[ "$GITHUB_REF" =~ refs/pull/([0-9]+)/merge ]]; then
PR_NUMBER="${BASH_REMATCH[1]}"
fi
fi
# If so, then prepare to create CHANGES.html
if [[ -n "$PR_NUMBER" ]]; then
# mathjax path in old doc (regex)
mathjax_path_from="[-./A-Za-z_]*/tex-chtml[.]js?v=[0-9a-f]*"
# mathjax path in new doc
mathjax_path_to=$(docker exec -e SAGE_USE_CDNS=yes BUILD /sage/sage -python -c "from sage_docbuild.conf import mathjax_path; print(mathjax_path)")
new_version=$(docker exec BUILD cat src/VERSION.txt)
mkdir -p doc/
docker cp BUILD:/sage/local/share/doc/sage/html doc/
# Wipe out chronic diffs between old doc and new doc
(cd doc && \
find . -name "*.html" | xargs sed -i -e '/class="sidebar-brand-text"/ s/Sage [0-9a-z.]* /Sage '"$new_version"' /' \
-e '/<link rel="stylesheet"/ s/?v=[0-9a-f]*"/"/' \
-e 's;'"$mathjax_path_from"';'"$mathjax_path_to"';' \
-e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d')
# Create git repo from old doc
(cd doc && \
git init && \
(echo "*.svg binary"; echo "*.pdf binary") >> .gitattributes && \
(echo ".buildinfo"; echo '*.inv'; echo '.git*'; echo '*.svg'; echo '*.pdf'; echo '*.png'; echo 'searchindex.js') > .gitignore; \
git add -A && git commit --quiet -m 'old')
fi
- name: Build doc
id: docbuild
Expand All @@ -174,21 +184,31 @@ jobs:
# We copy everything to a local folder
docker cp --follow-link BUILD:/sage/local/share/doc/sage/html doc
docker cp --follow-link BUILD:/sage/local/share/doc/sage/index.html doc
(cd doc && git commit -a -m 'new')
# Wipe out chronic diffs of new doc against old doc before creating CHANGES.html
(cd doc && \
find . -name "*.html" | xargs sed -i -e '/This is documentation for/ s/ built with GitHub PR .*. Doc/. Doc/' \
-e '/<link rel="stylesheet"/ s/?v=[0-9a-f]*"/"/' \
-e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d' \
&& git commit -a -m 'wipe-out')
# Since HEAD is at commit 'wipe-out', HEAD~1 is commit 'new' (new doc), HEAD~2 is commit 'old' (old doc)
.ci/create-changes-html.sh $(cd doc && git rev-parse HEAD~2) doc
# Restore the new doc with changes made in create-changes-html.sh but dropping changes by "wipe out"
(cd doc && git stash -q && git checkout -q -f HEAD~1 && git stash pop -q)
# Sometimes rm -rf .git errors out because of some diehard hidden files
# So we simply move it out of the doc directory
(cd doc && mv .git ../git && mv .gitattributes ../gitattributes)
mv CHANGES.html doc
# Check if we are on PR
PR_NUMBER=""
if [[ -n "$GITHUB_REF" ]]; then
if [[ "$GITHUB_REF" =~ refs/pull/([0-9]+)/merge ]]; then
PR_NUMBER="${BASH_REMATCH[1]}"
fi
fi
# If so, then create CHANGES.html
if [[ -n "$PR_NUMBER" ]]; then
(cd doc && git commit -a -m 'new')
# Wipe out chronic diffs of new doc against old doc before creating CHANGES.html
(cd doc && \
find . -name "*.html" | xargs sed -i -e '/This is documentation of/ s/ built with GitHub PR .* for development/ for development/' \
-e '/<link rel="stylesheet"/ s/?v=[0-9a-f]*"/"/' \
-e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d' \
&& git commit -a -m 'wipe-out')
# Since HEAD is at commit 'wipe-out', HEAD~1 is commit 'new' (new doc), HEAD~2 is commit 'old' (old doc)
.ci/create-changes-html.sh $(cd doc && git rev-parse HEAD~2) doc
# Restore the new doc with changes made in create-changes-html.sh but dropping changes by "wipe out"
(cd doc && git stash -q && git checkout -q -f HEAD~1 && git stash pop -q)
# Sometimes rm -rf .git errors out because of some diehard hidden files
# So we simply move it out of the doc directory
(cd doc && mv .git ../git && mv .gitattributes ../gitattributes)
mv CHANGES.html doc
fi
# Zip everything for increased performance
zip -r doc.zip doc
Expand Down
1 change: 0 additions & 1 deletion src/doc/en/website/versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# The sage-update-version script adds a new line for a new stable release
# when run by the Sage release manager to prepare a new release
#
10.4 doc-10-4--sagemath.netlify.app
10.3 doc-10-3--sagemath.netlify.app
10.2 doc-10-2--sagemath.netlify.app
10.1 doc-10-1--sagemath.netlify.app
Expand Down
37 changes: 19 additions & 18 deletions src/sage_docbuild/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,26 +441,27 @@ def linkcode_resolve(domain, info):
# "source_directory" is defined in conf.py customized for the doc
}

if not version.split('.')[-1].isnumeric(): # develop version
# Check the condition for announcement banner
github_ref = os.environ.get('GITHUB_REF', '')
if github_ref:
match = re.search(r'refs/pull/(\d+)/merge', github_ref)
if match:
pr_number = match.group(1)
is_develop_version = not version.split('.')[-1].isnumeric()
is_for_github_pr = github_ref and match and pr_number

if is_develop_version or is_for_github_pr: # condition for announcement banner
# This URL is hardcoded in the file .github/workflows/doc-publish.yml.
# See NETLIFY_ALIAS of the "Deploy to Netlify" step.
ver = f'<a href="https://doc-develop--sagemath.netlify.app/html/en/index.html">{version}</a>'
github_ref = os.environ.get('GITHUB_REF', '')
if github_ref:
match = re.search(r'refs/pull/(\d+)/merge', github_ref)
if match:
# As this doc is built for a GitHub PR, we plant links
# to the PR in the announcement banner.
pr_number = match.group(1)
pr_url = f'https://github.com/sagemath/sage/pull/{pr_number}'
pr_sha = os.environ.get('PR_SHA', '')
pr_commit = pr_url + f'/commits/{pr_sha}'
ver += f' built with GitHub PR <a href="{pr_url}">#{pr_number}</a>' \
f' on <a href="{pr_commit}">{pr_sha[:7]}</a>' \
f' [<a href="/changes.html">changes</a>]'
banner = f'This is documentation for Sage development version {ver}. ' \
'Documentation for the latest stable version is ' \
'<a href="https://doc.sagemath.org/html/en/index.html">here</a>.'
if is_for_github_pr:
pr_url = f'https://github.com/sagemath/sage/pull/{pr_number}'
pr_sha = os.environ.get('PR_SHA', '')
pr_commit = pr_url + f'/commits/{pr_sha}'
ver += f' built with GitHub PR <a href="{pr_url}">#{pr_number}</a>' \
f' on <a href="{pr_commit}">{pr_sha[:7]}</a>' \
f' [<a href="/changes.html">changes</a>]'
banner = f'This is documentation for Sage version {ver} for development purpose.'
html_theme_options.update({ "announcement": banner })

# The name of the Pygments (syntax highlighting) style to use. This
Expand Down Expand Up @@ -680,7 +681,7 @@ def add_page_context(app, pagename, templatename, context, doctree):
path2 = os.path.join(SAGE_DOC, 'html', 'en')
relpath = os.path.relpath(path2, path1)
context['release'] = release
context['documentation_title'] = f'Version {release} Documentation '
context['documentation_title'] = f'Version {release} Documentation'
context['documentation_root'] = os.path.join(relpath, 'index.html')
if 'website' in path1:
context['title'] = 'Documentation'
Expand Down

0 comments on commit 7982725

Please sign in to comment.