[7.2.0] Update release notes script (#21844)

PiperOrigin-RevId: 618964109
Change-Id: I79810e2a8c8a9f2174baccb6c8df240d1b927e5d

Commit
https://github.com/bazelbuild/bazel/commit/9a333bc59e04e3c926121b11eb97547ba823c6ed

Co-authored-by: Googler <keerthanakumar@google.com>
Co-authored-by: Ian (Hee) Cha <heec@google.com>
diff --git a/scripts/release/relnotes.py b/scripts/release/relnotes.py
index a90c433..0caf749 100644
--- a/scripts/release/relnotes.py
+++ b/scripts/release/relnotes.py
@@ -26,7 +26,7 @@
                                  list(args)).decode("utf-8").strip().split("\n")
 
 
-def extract_pr_title(commit_message_lines):
+def extract_title(commit_message_lines):
   """Extracts first line from commit message (passed in as a list of lines)."""
   return re.sub(
       r"\[\d+\.\d+\.\d\]\s?", "", commit_message_lines[0].strip()
@@ -37,26 +37,38 @@
   """Extracts relnotes from a commit message (passed in as a list of lines)."""
   relnote_lines = []
   in_relnote = False
+
+  title = extract_title(commit_message_lines)
+  issue_id = re.search(r"\(\#[0-9]+\)$", title.strip().split()[-1])
+
   for line in commit_message_lines:
-    if not line or line.startswith("PiperOrigin-RevId:"):
+    line = line.strip()
+    if (
+        not line
+        or line.startswith("PiperOrigin-RevId:")
+        or re.match(r"^\s*(Fixes|Closes)\s+#\d+\.?\s*$", line)
+    ):
       in_relnote = False
     m = re.match(r"^RELNOTES(?:\[(INC|NEW)\])?:", line)
     if m is not None:
       in_relnote = True
-      line = line[len(m[0]):]
+      line = line[len(m[0]) :]
+      if line.strip().lower().rstrip(".") in ["n/a", "na", "none"]:
+        return None
       if m[1] == "INC":
         line = "**[Incompatible]** " + line.strip()
     line = line.strip()
     if in_relnote and line:
       relnote_lines.append(line)
   relnote = " ".join(relnote_lines)
-  relnote_lower = relnote.strip().lower().rstrip(".")
-  if relnote_lower == "n/a" or relnote_lower == "none" or not relnote_lower:
-    return None
+
+  if issue_id and relnote:
+    relnote += " " + issue_id.group(0).strip()
+
   return relnote
 
 
-def get_relnotes_between(base, head, is_major_release):
+def get_relnotes_between(base, head, is_patch_release):
   """Gets all relnotes for commits between `base` and `head`."""
   commits = git("rev-list", f"{base}..{head}")
   if commits == [""]:
@@ -75,7 +87,7 @@
       # The rollback commit itself is also skipped.
       continue
     relnote = (
-        extract_relnotes(lines) if is_major_release else extract_pr_title(lines)
+        extract_title(lines) if is_patch_release else extract_relnotes(lines)
     )
     if relnote is not None:
       relnotes.append(relnote)
@@ -166,7 +178,7 @@
       print("Error: Not a release branch.")
       sys.exit(1)
 
-  is_major = bool(re.fullmatch(r"\d+.0.0", current_release))
+  is_patch = not current_release.endswith(".0")
 
   tags = [tag for tag in git("tag", "--sort=refname") if "pre" not in tag]
 
@@ -181,13 +193,14 @@
   # base with the last release so that we know which commits to generate notes
   # for.
   merge_base = git("merge-base", "HEAD", last_release)[0]
-  print("Baseline: ", merge_base)
 
   # Generate notes for all commits from last branch cut to HEAD, but filter out
   # any identical notes from the previous release branch.
-  cur_release_relnotes = get_relnotes_between(merge_base, "HEAD", is_major)
+  cur_release_relnotes = get_relnotes_between(
+      merge_base, "HEAD", is_patch
+  )
   last_release_relnotes = set(
-      get_relnotes_between(merge_base, last_release, is_major)
+      get_relnotes_between(merge_base, last_release, is_patch)
   )
   filtered_relnotes = [
       note for note in cur_release_relnotes if note not in last_release_relnotes
@@ -197,19 +210,14 @@
   filtered_relnotes.reverse()
   print()
   print("Release Notes:")
+  print()
 
-  if len(sys.argv) >= 2 and sys.argv[1] == "sort":
-    print()
-    categorized_release_notes = get_categorized_relnotes(filtered_relnotes)
-    for label in categorized_release_notes:
-      print(label + ":")
-      for note in categorized_release_notes[label]:
-        print("+", note)
-      print()
-  else:
-    print()
-    for note in filtered_relnotes:
+  categorized_release_notes = get_categorized_relnotes(filtered_relnotes)
+  for label in categorized_release_notes:
+    print(label + ":")
+    for note in categorized_release_notes[label]:
       print("+", note)
+    print()
 
   print()
   print("Acknowledgements:")