|
| 1 | +import argparse |
| 2 | +import yaml |
| 3 | +import requests |
| 4 | +import re |
| 5 | +import os |
| 6 | + |
| 7 | +parser = argparse.ArgumentParser() |
| 8 | +parser.add_argument('version') |
| 9 | +parser.add_argument('repo') |
| 10 | +parser.add_argument('version_tag') |
| 11 | +parser.add_argument('previous_tag') |
| 12 | +parser.add_argument('template_file') |
| 13 | + |
| 14 | +options = parser.parse_args() |
| 15 | + |
| 16 | +options.docs_version = options.version_tag[:options.version_tag.rfind('.')] |
| 17 | +options.github_api_base_url = 'https://api.github.com/repos/' |
| 18 | +options.github_api_key = os.environ.get("GITHUB_APIKEY") |
| 19 | +options.github_headers = { |
| 20 | + "Authorization": "Bearer {api_key}".format(api_key=options.github_api_key), |
| 21 | + "X-GitHub-Api-Version": "2022-11-28", |
| 22 | + "Accept": "application/vnd.github+json" |
| 23 | +} |
| 24 | + |
| 25 | +print("Preparing release notes for: tag {version_tag}, previous tag {previous_tag}".format(version_tag = options.version_tag, previous_tag = options.previous_tag)) |
| 26 | + |
| 27 | +def load_config(opts): |
| 28 | + print("Loading template...") |
| 29 | + with open(opts.template_file, 'r') as stream: |
| 30 | + try: |
| 31 | + opts.template = yaml.safe_load(stream) |
| 32 | + for section in opts.template["sections"]: |
| 33 | + if type(section) is dict: |
| 34 | + section["items"] = [] |
| 35 | + except yaml.YAMLError as e: |
| 36 | + print('Cannot load template file:', e) |
| 37 | + |
| 38 | + |
| 39 | +def mapPullRequest(pullRequest, opts): |
| 40 | + title = pullRequest["title"] |
| 41 | + for regex in opts.template["autoformat"]: |
| 42 | + title = re.sub(regex["match"], regex["replace"], title) |
| 43 | + |
| 44 | + return { |
| 45 | + "title": title, |
| 46 | + "labels": list(map(lambda l: l["name"], pullRequest["labels"])) |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +def is_in_section(pullrequest, section): |
| 51 | + if section is None: |
| 52 | + return False |
| 53 | + if type(section) is str: |
| 54 | + return False |
| 55 | + if "exclude-labels" in section: |
| 56 | + for lbl in section["exclude-labels"]: |
| 57 | + if lbl in pullrequest["labels"]: |
| 58 | + return False |
| 59 | + |
| 60 | + if "labels" in section: |
| 61 | + if section["labels"] == "*": |
| 62 | + return True |
| 63 | + for lbl in section["labels"]: |
| 64 | + if lbl in pullrequest["labels"]: |
| 65 | + return True |
| 66 | + return False |
| 67 | + |
| 68 | + return True |
| 69 | + |
| 70 | + |
| 71 | +def load_pull_requests(opts): |
| 72 | + print("Loading changeset...") |
| 73 | + page = 0 |
| 74 | + total_pages = 1 |
| 75 | + page_size = 100 |
| 76 | + commits_url = "{github_api_base_url}{repo}/compare/{previous_tag}...{version_tag}".format( |
| 77 | + github_api_base_url=opts.github_api_base_url, |
| 78 | + repo=opts.repo, |
| 79 | + previous_tag=opts.previous_tag, |
| 80 | + version_tag=opts.version_tag) |
| 81 | + ignore_section = opts.template["ignore"] |
| 82 | + |
| 83 | + while total_pages > page: |
| 84 | + response = requests.get(commits_url, params={'per_page': page_size, 'page': page}, headers=opts.github_headers) |
| 85 | + response.raise_for_status() |
| 86 | + commits = response.json() |
| 87 | + total_pages = commits["total_commits"] / page_size |
| 88 | + |
| 89 | + for commit in commits["commits"]: |
| 90 | + pullrequests_url = "{github_api_base_url}{repo}/commits/{commit_sha}/pulls".format( |
| 91 | + github_api_base_url=opts.github_api_base_url, |
| 92 | + repo=opts.repo, |
| 93 | + commit_sha=commit["sha"]) |
| 94 | + pullrequests = requests.get(pullrequests_url, headers=opts.github_headers).json() |
| 95 | + for pullrequest in pullrequests: |
| 96 | + mapped = mapPullRequest(pullrequest, opts) |
| 97 | + if is_in_section(mapped, ignore_section): |
| 98 | + break |
| 99 | + |
| 100 | + for section in opts.template["sections"]: |
| 101 | + if is_in_section(mapped, section): |
| 102 | + if mapped in section["items"]: |
| 103 | + break # PR was already added to the section |
| 104 | + section["items"].append(mapped) |
| 105 | + break # adding PR to the section, skip evaluating next sections |
| 106 | + else: |
| 107 | + opts.template["unclassified"].append(mapped) |
| 108 | + page = page + 1 |
| 109 | + |
| 110 | + |
| 111 | +def get_field(source, path): |
| 112 | + elements = path.split('.') |
| 113 | + for elem in elements: |
| 114 | + source = getattr(source, elem) |
| 115 | + return source |
| 116 | + |
| 117 | + |
| 118 | +def apply_template(template, parameters): |
| 119 | + return re.sub(r'\$\{([\w.]+)}', lambda m: get_field(parameters, m.group(1)), template) |
| 120 | + |
| 121 | + |
| 122 | +def process_section(section): |
| 123 | + if type(section) is str: |
| 124 | + return apply_template(section, options) |
| 125 | + if len(section["items"]) == 0: |
| 126 | + return "" |
| 127 | + |
| 128 | + content = "" |
| 129 | + title = section.get("title", "") |
| 130 | + if title != "": |
| 131 | + content = apply_template(title, options) + '\n' |
| 132 | + |
| 133 | + for pullrequest in section["items"]: |
| 134 | + content = content + '\n - ' + pullrequest["title"] |
| 135 | + |
| 136 | + return content |
| 137 | + |
| 138 | + |
| 139 | +def publish_release_notes(opts, title, content): |
| 140 | + print("Publishing release notes...") |
| 141 | + url = '{github_api_base_url}{repo}/releases/tags/{tag}'.format(github_api_base_url=opts.github_api_base_url, repo=opts.repo, tag=opts.version_tag) |
| 142 | + response = requests.get(url, headers=opts.github_headers) |
| 143 | + response.raise_for_status() |
| 144 | + if response.status_code != 404: |
| 145 | + raise SystemExit("Release with the tag already exists") |
| 146 | + |
| 147 | + post_data = { |
| 148 | + "tag_name": opts.version_tag, |
| 149 | + "name": title, |
| 150 | + "body": content, |
| 151 | + "draft": True, |
| 152 | + "generate_release_notes": False, |
| 153 | + "make_latest": "false" |
| 154 | + } |
| 155 | + response = requests.post(url, json=post_data, headers=opts.github_headers) |
| 156 | + response.raise_for_status() |
| 157 | + |
| 158 | + |
| 159 | +load_config(options) |
| 160 | +options.template["unclassified"] = [] |
| 161 | +load_pull_requests(options) |
| 162 | + |
| 163 | +print("Processing title...") |
| 164 | +release_title = apply_template(options.template["title"], options) |
| 165 | +print("Title: {title}".format(title=release_title)) |
| 166 | + |
| 167 | +print("Processing content...") |
| 168 | +release_content = "" |
| 169 | +for section in options.template["sections"]: |
| 170 | + section_content = process_section(section) |
| 171 | + if section_content != "": |
| 172 | + release_content += "\n\n" + section_content |
| 173 | + |
| 174 | +if len(options.template["unclassified"]) > 0: |
| 175 | + release_content += "\n\n================================" |
| 176 | + release_content += "\n\n!!!UNCLASSIFIED PULL REQUESTS!!!" |
| 177 | + for pr in options.template["unclassified"]: |
| 178 | + release_content += "\n" + pr["title"] |
| 179 | + release_content += "\n\n================================" |
| 180 | + |
| 181 | +print("----------") |
| 182 | +print(release_content) |
| 183 | +print("----------") |
| 184 | + |
| 185 | +publish_release_notes(options, release_title, release_content) |
| 186 | + |
| 187 | +print("Done.") |
0 commit comments