|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | +from github import Auth, Github |
| 9 | +from jinja2 import Environment, FileSystemLoader, select_autoescape |
| 10 | + |
| 11 | +# parse args |
| 12 | +parser = argparse.ArgumentParser() |
| 13 | +parser.add_argument("nic_version", help="NGINX Ingress Controller version") |
| 14 | +parser.add_argument("helm_chart_version", help="NGINX Ingress Controller Helm chart version") |
| 15 | +parser.add_argument("k8s_versions", help="Kubernetes versions") |
| 16 | +parser.add_argument("release_date", help="Release date") |
| 17 | +args = parser.parse_args() |
| 18 | +NIC_VERSION = args.nic_version |
| 19 | +HELM_CHART_VERSION = args.helm_chart_version |
| 20 | +K8S_VERSIONS = args.k8s_versions |
| 21 | +RELEASE_DATE = args.release_date |
| 22 | + |
| 23 | +# Set up Jinja2 environment |
| 24 | +template_dir = os.path.dirname(os.path.abspath(__file__)) |
| 25 | +env = Environment(loader=FileSystemLoader(template_dir), autoescape=select_autoescape(["j2"])) |
| 26 | +template = env.get_template("release-notes.j2") |
| 27 | + |
| 28 | +# Setup required variables |
| 29 | +github_org = os.getenv("GITHUB_ORG", "nginx") |
| 30 | +github_repo = os.getenv("GITHUB_REPO", "kubernetes-ingress") |
| 31 | +token = os.environ.get("GITHUB_TOKEN") |
| 32 | +docker_pr_strings = ["Docker image update", "docker group", "docker-images group", "in /build"] |
| 33 | +golang_pr_strings = ["go group", "go_modules group"] |
| 34 | + |
| 35 | +# Setup regex's |
| 36 | +# Matches: |
| 37 | +# My new change by @gihubhandle in https://github.com/<org>/<repo>/pull/<number> |
| 38 | +# Captures change title and PR URL |
| 39 | +change_regex = r"^(.*) by @.* in (.*)$" |
| 40 | +# Matches: |
| 41 | +# https://github.com/<org>/<repo>/pull/<number> |
| 42 | +# Captures PR number |
| 43 | +pull_request_regex = r"^.*pull/(\d+)$" |
| 44 | + |
| 45 | + |
| 46 | +def parse_sections(markdown: str): |
| 47 | + sections = {} |
| 48 | + section_name = None |
| 49 | + for line in markdown.splitlines(): |
| 50 | + # Check if the line starts with a section header |
| 51 | + # Section headers start with "### " |
| 52 | + # We will use the section header as the key in the sections dictionary |
| 53 | + # and the lines below it as the values (until the next section header) |
| 54 | + line = line.strip() |
| 55 | + if not line: |
| 56 | + continue # skip empty lines |
| 57 | + if line.startswith("### "): |
| 58 | + section_name = line[3:].strip() |
| 59 | + sections[section_name] = [] |
| 60 | + # If the line starts with "* " and contains "made their first contribution", |
| 61 | + # we will skip it as it is not a change but a contributor note |
| 62 | + elif section_name and line.startswith("* ") and "made their first contribution" in line: |
| 63 | + continue |
| 64 | + # Check if the line starts with "* " or "- " |
| 65 | + # If it does, we will add the line to the current section |
| 66 | + # We will also strip the "* " or "- " from the beginning of the line |
| 67 | + elif section_name and line.strip().startswith("* "): |
| 68 | + sections[section_name].append(line.strip()[2:].strip()) |
| 69 | + return sections |
| 70 | + |
| 71 | + |
| 72 | +def format_pr_groups(prs, title): |
| 73 | + # join the PR's into a comma, space separated string |
| 74 | + comma_sep_prs = "".join([f"{dep['details']}, " for dep in prs]) |
| 75 | + |
| 76 | + # strip the last comma and space, and add the first PR title |
| 77 | + trimmed_comma_sep_prs = f"{comma_sep_prs.rstrip(', ')} {title}" |
| 78 | + |
| 79 | + # split the string by the last comma and join with an ampersand |
| 80 | + split_result = trimmed_comma_sep_prs.rsplit(",", 1) |
| 81 | + return " &".join(split_result) |
| 82 | + |
| 83 | + |
| 84 | +# Get release text |
| 85 | +def get_github_release(version, github_org, github_repo, token): |
| 86 | + if token == "": |
| 87 | + print("ERROR: GITHUB token variable cannot be empty") |
| 88 | + return None |
| 89 | + auth = Auth.Token(token) |
| 90 | + g = Github(auth=auth) |
| 91 | + repo = g.get_organization(github_org).get_repo(github_repo) |
| 92 | + release = None |
| 93 | + releases = repo.get_releases() |
| 94 | + for rel in releases: |
| 95 | + if rel.tag_name == f"v{version}": |
| 96 | + release = rel |
| 97 | + break |
| 98 | + g.close() |
| 99 | + if release is not None: |
| 100 | + return release.body |
| 101 | + print(f"ERROR: Release v{NIC_VERSION} not found in {github_org}/{github_repo}.") |
| 102 | + return None |
| 103 | + |
| 104 | + |
| 105 | +## Main section of script |
| 106 | + |
| 107 | +release_body = get_github_release(NIC_VERSION, github_org, github_repo, token) |
| 108 | +if release_body is None: |
| 109 | + print("ERROR: Cannot get release from Github. Exiting...") |
| 110 | + sys.exit(1) |
| 111 | + |
| 112 | +# Parse the release body to extract sections |
| 113 | +sections = parse_sections(release_body or "") |
| 114 | + |
| 115 | +# Prepare the data for rendering |
| 116 | +# We will create a dictionary with the categories and their changes |
| 117 | +# Also, we will handle dependencies separately for Go and Docker images |
| 118 | +# and format them accordingly |
| 119 | +catagories = {} |
| 120 | +dependencies_title = "" |
| 121 | +for title, changes in sections.items(): |
| 122 | + if any(x in title for x in ["Other Changes", "Documentation", "Maintenance", "Tests"]): |
| 123 | + # These sections do not show up in the docs release notes |
| 124 | + continue |
| 125 | + parsed_changes = [] |
| 126 | + go_dependencies = [] |
| 127 | + docker_dependencies = [] |
| 128 | + for line in changes: |
| 129 | + change = re.search(change_regex, line) |
| 130 | + change_title = change.group(1) |
| 131 | + pr_link = change.group(2) |
| 132 | + pr_number = re.search(pull_request_regex, pr_link).group(1) |
| 133 | + pr = {"details": f"[{pr_number}]({pr_link})", "title": change_title.capitalize()} |
| 134 | + if "Dependencies" in title: |
| 135 | + # save section title for later use as lookup key to categories dict |
| 136 | + dependencies_title = title |
| 137 | + |
| 138 | + # Append Golang changes in to the go_dependencies list for later processing |
| 139 | + if any(str in change_title for str in golang_pr_strings): |
| 140 | + go_dependencies.append(pr) |
| 141 | + # Append Docker changes in to the docker_dependencies list for later processing |
| 142 | + elif any(str in change_title for str in docker_pr_strings): |
| 143 | + docker_dependencies.append(pr) |
| 144 | + # Treat this change like any other ungrouped change |
| 145 | + else: |
| 146 | + parsed_changes.append(f"{pr['details']} {pr['title']}") |
| 147 | + else: |
| 148 | + parsed_changes.append(f"{pr['details']} {pr['title']}") |
| 149 | + |
| 150 | + catagories[title] = parsed_changes |
| 151 | + |
| 152 | +# Add grouped dependencies to the Dependencies category |
| 153 | +catagories[dependencies_title].append(format_pr_groups(docker_dependencies, "Bump Docker dependencies")) |
| 154 | +catagories[dependencies_title].append(format_pr_groups(go_dependencies, "Bump Go dependencies")) |
| 155 | +catagories[dependencies_title].reverse() |
| 156 | + |
| 157 | +# Populates the data needed for rendering the template |
| 158 | +# The data will be passed to the Jinja2 template for rendering |
| 159 | +data = { |
| 160 | + "version": NIC_VERSION, |
| 161 | + "release_date": RELEASE_DATE, |
| 162 | + "sections": catagories, |
| 163 | + "HELM_CHART_VERSION": HELM_CHART_VERSION, |
| 164 | + "K8S_VERSIONS": K8S_VERSIONS, |
| 165 | +} |
| 166 | + |
| 167 | +# Render with Jinja2 |
| 168 | +print(template.render(**data)) |
0 commit comments