|
| 1 | +import re |
| 2 | +import textwrap |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from gardenlinux.constants import GLVD_BASE_URL, REQUESTS_TIMEOUTS |
| 7 | +from gardenlinux.logger import LoggerSetup |
| 8 | + |
| 9 | +from .helpers import compare_apt_repo_versions |
| 10 | + |
| 11 | +LOGGER = LoggerSetup.get_logger("gardenlinux.github.release_notes", "INFO") |
| 12 | + |
| 13 | + |
| 14 | +def release_notes_changes_section(gardenlinux_version): |
| 15 | + """ |
| 16 | + Get list of fixed CVEs, grouped by upgraded package. |
| 17 | + Note: This result is not perfect, feel free to edit the generated release notes and |
| 18 | + file issues in glvd for improvement suggestions https://github.com/gardenlinux/glvd/issues |
| 19 | + """ |
| 20 | + try: |
| 21 | + url = f"{GLVD_BASE_URL}/patchReleaseNotes/{gardenlinux_version}" |
| 22 | + response = requests.get(url, timeout=REQUESTS_TIMEOUTS) |
| 23 | + response.raise_for_status() |
| 24 | + data = response.json() |
| 25 | + |
| 26 | + if len(data["packageList"]) == 0: |
| 27 | + return "" |
| 28 | + |
| 29 | + output = [ |
| 30 | + "## Changes", |
| 31 | + "The following packages have been upgraded, to address the mentioned CVEs:", |
| 32 | + ] |
| 33 | + for package in data["packageList"]: |
| 34 | + upgrade_line = ( |
| 35 | + f"- upgrade '{package['sourcePackageName']}' from `{package['oldVersion']}` " |
| 36 | + f"to `{package['newVersion']}`" |
| 37 | + ) |
| 38 | + output.append(upgrade_line) |
| 39 | + |
| 40 | + if package["fixedCves"]: |
| 41 | + for fixedCve in package["fixedCves"]: |
| 42 | + output.append(f" - {fixedCve}") |
| 43 | + |
| 44 | + return "\n".join(output) + "\n\n" |
| 45 | + except Exception as exn: |
| 46 | + # There are expected error cases, |
| 47 | + # for example with versions not supported by glvd (1443.x) |
| 48 | + # or when the api is not available |
| 49 | + # Fail gracefully by adding the placeholder we previously used, |
| 50 | + # so that the release note generation does not fail. |
| 51 | + LOGGER.error(f"Failed to process GLVD API output: {exn}") |
| 52 | + return textwrap.dedent( |
| 53 | + """ |
| 54 | + ## Changes |
| 55 | + The following packages have been upgraded, to address the mentioned CVEs: |
| 56 | + **todo release facilitator: fill this in** |
| 57 | + """ |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def release_notes_software_components_section(package_list): |
| 62 | + output = "## Software Component Versions\n" |
| 63 | + output += "```" |
| 64 | + output += "\n" |
| 65 | + packages_regex = re.compile( |
| 66 | + r"^linux-image-amd64$|^systemd$|^containerd$|^runc$|^curl$|^openssl$|^openssh-server$|^libc-bin$" |
| 67 | + ) |
| 68 | + for entry in package_list.values(): |
| 69 | + if packages_regex.match(entry.deb_source): |
| 70 | + output += f"{entry!r}\n" |
| 71 | + output += "```" |
| 72 | + output += "\n\n" |
| 73 | + return output |
| 74 | + |
| 75 | + |
| 76 | +def release_notes_compare_package_versions_section(gardenlinux_version, package_list): |
| 77 | + output = "" |
| 78 | + version_components = gardenlinux_version.split(".") |
| 79 | + # Assumes we always have version numbers like 1443.2 |
| 80 | + if len(version_components) == 2: |
| 81 | + try: |
| 82 | + major = int(version_components[0]) |
| 83 | + patch = int(version_components[1]) |
| 84 | + |
| 85 | + if patch > 0: |
| 86 | + previous_version = f"{major}.{patch - 1}" |
| 87 | + |
| 88 | + output += ( |
| 89 | + f"## Changes in Package Versions Compared to {previous_version}\n" |
| 90 | + ) |
| 91 | + output += compare_apt_repo_versions( |
| 92 | + previous_version, gardenlinux_version |
| 93 | + ) |
| 94 | + elif patch == 0: |
| 95 | + output += f"## Full List of Packages in Garden Linux version {major}\n" |
| 96 | + output += "<details><summary>Expand to see full list</summary>\n" |
| 97 | + output += "<pre>" |
| 98 | + output += "\n" |
| 99 | + for entry in package_list.values(): |
| 100 | + output += f"{entry!r}\n" |
| 101 | + output += "</pre>" |
| 102 | + output += "\n</details>\n\n" |
| 103 | + |
| 104 | + except ValueError: |
| 105 | + LOGGER.error( |
| 106 | + f"Could not parse {gardenlinux_version} as the Garden Linux version, skipping version compare section" |
| 107 | + ) |
| 108 | + else: |
| 109 | + LOGGER.error( |
| 110 | + f"Unexpected version number format {gardenlinux_version}, expected format (major is int).(patch is int)" |
| 111 | + ) |
| 112 | + return output |
0 commit comments