release_upgrade_notes.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env python3
  2. """Move upgrade notes that shipped in a release under that release's heading.
  3. Upgrade notes for changes that are merged but not yet released live under the
  4. "Next release (unreleased)" section of upgrade_notes.adoc. When a release is
  5. tagged, every note that was in the unreleased section *at the tagged commit*
  6. moves into a new "== <version>" section. Notes added to the unreleased section
  7. after the release was cut stay where they are.
  8. Each version section links back to the release on GitHub, which has the full
  9. release notes, and has an anchor that the GitHub release links to.
  10. Usage:
  11. release_upgrade_notes.py <version> <released-file> <target-file>
  12. release_upgrade_notes.py --release-summary <version> <notes-file>
  13. <released-file> is upgrade_notes.adoc as it was at the release tag, and
  14. <target-file> is the copy to update in place (normally on the next branch).
  15. --release-summary prints the "Upgrade warnings, or breaking changes" list for
  16. the GitHub release of <version>, from the unreleased notes in <notes-file>.
  17. """
  18. from __future__ import annotations
  19. import sys
  20. from pathlib import Path
  21. UNRELEASED_HEADING = "== Next release (unreleased)"
  22. GITHUB_RELEASE_URL = "https://github.com/OliveTin/OliveTin/releases/tag/{version}"
  23. UPGRADE_NOTES_URL = "https://docs.olivetin.app/upgrade/upgrade_notes.html"
  24. NO_UPGRADE_NOTES = "- No such issues between the last release and this version.\n"
  25. def split_sections(lines: list[str], prefix: str) -> tuple[list[str], list[list[str]]]:
  26. """Split lines into a preamble and blocks that each start at a heading.
  27. An anchor line such as [#some-id] directly above a heading belongs to that
  28. heading's block, so that anchors move with their notes.
  29. """
  30. preamble: list[str] = []
  31. blocks: list[list[str]] = []
  32. for line in lines:
  33. if line.startswith(prefix) and not line.startswith(prefix + "="):
  34. anchor = _take_anchor(preamble if not blocks else blocks[-1])
  35. blocks.append(anchor + [line])
  36. elif blocks:
  37. blocks[-1].append(line)
  38. else:
  39. preamble.append(line)
  40. return preamble, blocks
  41. def _take_anchor(block: list[str]) -> list[str]:
  42. if block and block[-1].startswith("[#"):
  43. return [block.pop()]
  44. return []
  45. def note_key(note: list[str]) -> str:
  46. """Identify a note by its anchor, falling back to its heading."""
  47. for line in note:
  48. if line.startswith("[#") or line.startswith("=== "):
  49. return line.strip()
  50. return ""
  51. def section_heading(section: list[str]) -> str:
  52. for line in section:
  53. if not line.startswith("[#"):
  54. return line.rstrip()
  55. return ""
  56. def unreleased_notes(lines: list[str]) -> tuple[list[str], list[list[str]]] | None:
  57. _, sections = split_sections(lines, "== ")
  58. for section in sections:
  59. if section_heading(section) == UNRELEASED_HEADING:
  60. return split_sections(section, "=== ")
  61. return None
  62. def released_keys(released_lines: list[str]) -> set[str]:
  63. found = unreleased_notes(released_lines)
  64. if found is None:
  65. return set()
  66. return {note_key(note) for note in found[1]}
  67. def version_anchor(version: str) -> str:
  68. return "release-" + version.replace(".", "-")
  69. def build_version_section(version: str, notes: list[list[str]]) -> list[str]:
  70. release_url = GITHUB_RELEASE_URL.format(version=version)
  71. section = [
  72. f"[#{version_anchor(version)}]\n",
  73. f"== {version}\n",
  74. "\n",
  75. f"See the {release_url}[{version} release notes on GitHub] for everything in this release.\n",
  76. "\n",
  77. ]
  78. for note in notes:
  79. section.extend(note)
  80. return section
  81. def build_unreleased_section(heading: list[str], notes: list[list[str]]) -> list[str]:
  82. if not notes:
  83. return []
  84. section = list(heading)
  85. for note in notes:
  86. section.extend(note)
  87. return section
  88. def move_released_notes(version: str, released_lines: list[str], target_lines: list[str]) -> list[str] | None:
  89. """Return the updated target lines, or None when there is nothing to move."""
  90. keys = released_keys(released_lines)
  91. if not keys:
  92. return None
  93. preamble, sections = split_sections(target_lines, "== ")
  94. if any(section_heading(section) == f"== {version}" for section in sections):
  95. return None
  96. result = list(preamble)
  97. moved = False
  98. for section in sections:
  99. if section_heading(section) != UNRELEASED_HEADING:
  100. result.extend(section)
  101. continue
  102. heading, notes = split_sections(section, "=== ")
  103. shipped = [note for note in notes if note_key(note) in keys]
  104. if not shipped:
  105. result.extend(section)
  106. continue
  107. remaining = [note for note in notes if note_key(note) not in keys]
  108. result.extend(build_unreleased_section(heading, remaining))
  109. result.extend(build_version_section(version, shipped))
  110. moved = True
  111. return result if moved else None
  112. def note_title(note: list[str]) -> str:
  113. for line in note:
  114. if line.startswith("=== "):
  115. return line[4:].strip()
  116. return ""
  117. def note_anchor(note: list[str]) -> str:
  118. for line in note:
  119. if line.startswith("[#"):
  120. return line.strip()[2:-1]
  121. return ""
  122. def markdown_link(text: str, url: str) -> str:
  123. escaped = text.replace("[", "\\[").replace("]", "\\]")
  124. return f"[{escaped}]({url})"
  125. def release_summary(version: str, lines: list[str]) -> str:
  126. """Markdown for the GitHub release, linking to the notes that ship in it.
  127. Links point at the version section that the release workflow creates after
  128. the tag is pushed, and at each note's own anchor, which exists already.
  129. """
  130. found = unreleased_notes(lines)
  131. notes = found[1] if found else []
  132. if not notes:
  133. return NO_UPGRADE_NOTES
  134. version_url = f"{UPGRADE_NOTES_URL}#{version_anchor(version)}"
  135. summary = [f"- This release has {markdown_link('upgrade notes', version_url)}. Read them before you upgrade:\n"]
  136. for note in notes:
  137. anchor = note_anchor(note)
  138. url = f"{UPGRADE_NOTES_URL}#{anchor}" if anchor else version_url
  139. summary.append(f" - {markdown_link(note_title(note), url)}\n")
  140. return "".join(summary)
  141. def print_release_summary(argv: list[str]) -> int:
  142. if len(argv) != 4:
  143. print(__doc__, file=sys.stderr)
  144. return 2
  145. version, notes_path = argv[2], Path(argv[3])
  146. print(release_summary(version, notes_path.read_text().splitlines(keepends=True)), end="")
  147. return 0
  148. def main(argv: list[str]) -> int:
  149. if len(argv) > 1 and argv[1] == "--release-summary":
  150. return print_release_summary(argv)
  151. if len(argv) != 4:
  152. print(__doc__, file=sys.stderr)
  153. return 2
  154. version, released_path, target_path = argv[1], Path(argv[2]), Path(argv[3])
  155. released_lines = released_path.read_text().splitlines(keepends=True)
  156. target_lines = target_path.read_text().splitlines(keepends=True)
  157. updated = move_released_notes(version, released_lines, target_lines)
  158. if updated is None:
  159. print(f"Nothing to move for {version}: no unreleased upgrade notes shipped, or the section already exists.")
  160. return 0
  161. target_path.write_text("".join(updated))
  162. print(f"Moved released upgrade notes under '== {version}' in {target_path}.")
  163. return 0
  164. if __name__ == "__main__":
  165. sys.exit(main(sys.argv))