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

Continuous Integration Optimization #97

Merged
merged 10 commits into from
Oct 13, 2024
90 changes: 90 additions & 0 deletions .github/scripts/make-github-release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os
import re
import subprocess
import sys
from pathlib import Path

from pypandoc import convert_text, download_pandoc


CWD = Path().cwd()


date_pattern = r"\(\d\d\d\d-\d\d-\d\d\)"
version_pattern = r"\d+\.\d+(\.\d+)?(rc\d+)?"

matches_version_header = re.compile(rf"^{version_pattern} {date_pattern}$").match


def extract_relevant_contents(expected_version: str) -> str:
# This isn't solved with a pandoc filter as pandoc only recognizes the level 3
# headers as such.

line_feed = iter((CWD / "CHANGES.rst").read_text().splitlines())

while True:
line = next(line_feed)
if matches_version_header(line) and line.startswith(expected_version):
break

assert next(line_feed)[0] == "-"
assert next(line_feed) == ""

lines: list[str] = []
while not matches_version_header(line := next(line_feed)):
lines.append(line)

while lines[-1] == "":
lines.pop()

return "\n".join(lines)


def make_github_release(notes: str, version: str):
options = []
options.extend(["--notes-file", "-"])
options.extend(["--title", f"delb {version}"])
options.append("--verify-tag")
if "rc" in version:
options.append("--prerelease")

result = subprocess.run(
[
"gh",
"release",
"create",
]
+ options
+ [version],
capture_output=True,
encoding="utf-8",
input=notes,
)
print(result.stdout)
if result.returncode != 0:
print(result.stdout)
result.check_returncode()


def make_release_notes(version: str) -> str:
os.environ["DELB_DOCS_BASE_URL"] = f"https://delb.readthedocs.io/en/{version}/"
return (
convert_text(
extract_relevant_contents(version),
format="rst",
to="markdown_strict",
extra_args=["--shift-heading-level-by=1", "--wrap=none"],
filters=[".github/scripts/release-notes-pandoc-filter.py"],
)
+ "\n\n----\n\nThe package distributions are available at the "
+ f"[Python Package Index](https://pypi.org/project/delb/)."
)


def main(version: str):
download_pandoc()
make_github_release(notes=make_release_notes(version), version=version)


if __name__ == "__main__":
main(sys.argv[1])
59 changes: 59 additions & 0 deletions .github/scripts/release-notes-pandoc-filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import posixpath
import sys
from pathlib import Path

from panflute import run_filter, Code, Link, Str
from sphinx.util.inventory import InventoryFile


BASE_URL = os.environ["DELB_DOCS_BASE_URL"]
CWD = Path.cwd()
ROLES_MAPPING = {
"attr": "py:attribute",
"class": "py:class",
"doc": "std:doc",
# ?"exception"?: "py:exception",
"func": "py:function",
"meth": "py:method",
"mod": "py:module",
# ???: "py:property",
"term": "std:term",
}


def bend_links(elem, doc):
if not (isinstance(elem, Code) and "interpreted-text" in elem.classes):
return elem

inventory_section = ROLES_MAPPING[elem.attributes["role"]]
target = doc.inventory[inventory_section].get(elem.text)

if target is None:
# this is okay for non-existing entries such as :meth:`NodeBase.…` and
# objects in other inventories like Python's docs
print(f"WARNING: No inventory object for '{elem.text}' found.", file=sys.stderr)
return Code(elem.text)

if (label_text := target[3]) == "-":
label_text = elem.text

if inventory_section.startswith("py:"):
label = Code(label_text)
else:
label = Str(f"„{label_text}”")

return Link(label, url=target[2])


def prepare(doc):
with (CWD / "docs" / "build" / "html" / "objects.inv").open("rb") as f:
doc.inventory = InventoryFile.load(f, BASE_URL, posixpath.join)


def main(doc=None):
return run_filter(bend_links, prepare=prepare)


if __name__ == "__main__":
main()
14 changes: 12 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---

name: Publish delb
on:
on: # yamllint disable-line
funkyfuture marked this conversation as resolved.
Show resolved Hide resolved
push:
tags: ["*"]

Expand All @@ -21,7 +21,9 @@ jobs:
name: pypi
url: https://pypi.org/p/delb
permissions:
id-token: write
id-token: write
contents: write

steps:
- name: Download package
uses: actions/download-artifact@v4
Expand All @@ -30,5 +32,13 @@ jobs:
path: dist
- name: Upload package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
- name: Install dependencies for creating GH release
run: pip install panflute pypandoc sphinx
# TODO build docs, so that objects.inv is available
- name: Create GH release
run: >-
python .github/scripts/make-github-release.py ${{ github.ref_name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

...