|
| 1 | +# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +# or more contributor license agreements. Licensed under the Elastic License 2.0; |
| 3 | +# you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +import time |
| 6 | +import argparse |
| 7 | +from os import makedirs |
| 8 | + |
| 9 | + |
| 10 | +timestamp = round(time.time()) |
| 11 | + |
| 12 | +fragments_path = "changelog/fragments/" |
| 13 | +fragments_counter = 0 |
| 14 | + |
| 15 | +repo_dict = { |
| 16 | + "agent": "https://github.com/elastic/elastic-agent", |
| 17 | + "beats": "https://github.com/elastic/beats" |
| 18 | +} |
| 19 | +default_repolink = "https://github.com/elastic/elastic-agent" |
| 20 | + |
| 21 | +kind_dict = { |
| 22 | + "Breaking changes": "breaking-change", |
| 23 | + "Bugfixes": "bug-fix", |
| 24 | + "New features": "feature", |
| 25 | +} |
| 26 | +kind_token = "====" |
| 27 | +field_token = "-" |
| 28 | + |
| 29 | + |
| 30 | +def write_fragment(title, fragment_dict): |
| 31 | + path = "".join([fragments_path, |
| 32 | + str(timestamp + fragments_counter), |
| 33 | + "-", |
| 34 | + title, |
| 35 | + ".yaml"]) |
| 36 | + |
| 37 | + with open(path, 'w+') as f: |
| 38 | + for k, v in fragment_dict.items(): |
| 39 | + f.write(f"{k}: {v}\n") |
| 40 | + |
| 41 | +def parse_line(line, kind): |
| 42 | + global fragments_counter |
| 43 | + fragments_counter += 1 |
| 44 | + |
| 45 | + summary, *entries = line.split(" {") |
| 46 | + if len(entries) == 0: |
| 47 | + print(f"Warning: {line} has no PR/issue fields!\n") |
| 48 | + |
| 49 | + fragment_dict = {"kind": kind} |
| 50 | + fragment_dict["summary"] = summary.lstrip(field_token).strip() |
| 51 | + fragment_dict["summary"] = fragment_dict["summary"].replace(":", "") |
| 52 | + |
| 53 | + title = fragment_dict["summary"] |
| 54 | + title = title.replace(" ", "-") |
| 55 | + title = title.replace("/", "|") |
| 56 | + title = title.rstrip(".") |
| 57 | + |
| 58 | + repo_link = "" |
| 59 | + |
| 60 | + for entry in entries: |
| 61 | + number = entry[entry.find("[")+1:entry.find("]")] |
| 62 | + number = ''.join(filter(lambda n: n.isdigit(), number)) |
| 63 | + |
| 64 | + if "pull" in entry: |
| 65 | + fragment_field, default_repo_token = ["pr", "pull}"] |
| 66 | + if "issue" in entry: |
| 67 | + fragment_field, default_repo_token = ["issue", "issue}"] |
| 68 | + |
| 69 | + if fragment_field in fragment_dict.keys(): |
| 70 | + print(f"Skipping {line} -> multiple PRs/issues found!\n") |
| 71 | + return |
| 72 | + |
| 73 | + fragment_dict[fragment_field] = number |
| 74 | + |
| 75 | + if entry.startswith(default_repo_token): |
| 76 | + if repo_link: |
| 77 | + if repo_link != default_repolink: |
| 78 | + print(f"Skipping {line} -> multiple repositories found!\n") |
| 79 | + return |
| 80 | + repo_link = default_repolink |
| 81 | + else: |
| 82 | + for repo in repo_dict.keys(): |
| 83 | + if repo in entry: |
| 84 | + if repo_link and repo_link != repo_dict[repo]: |
| 85 | + print(f"Skipping {line} -> multiple repositories found!\n") |
| 86 | + return |
| 87 | + repo_link = repo_dict[repo] |
| 88 | + |
| 89 | + if repo_link: |
| 90 | + fragment_dict["repository"] = repo_link |
| 91 | + |
| 92 | + write_fragment(title, fragment_dict) |
| 93 | + |
| 94 | + |
| 95 | +def iterate_lines(f, kind='', skip=True): |
| 96 | + line = next(f, None) |
| 97 | + if line is None: |
| 98 | + return |
| 99 | + |
| 100 | + if line.startswith(kind_token): |
| 101 | + iterate_lines(f, kind_dict[line.lstrip(kind_token).strip()], skip=False) |
| 102 | + |
| 103 | + elif line.isspace(): |
| 104 | + iterate_lines(f, kind, skip) |
| 105 | + |
| 106 | + elif line.startswith(field_token) and skip is False: |
| 107 | + parse_line(line, kind) |
| 108 | + |
| 109 | + else: |
| 110 | + iterate_lines(f, kind, skip=True) |
| 111 | + |
| 112 | + iterate_lines(f, kind, skip) |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + parser = argparse.ArgumentParser() |
| 116 | + parser.add_argument("--path", help="Changelog file path", required=True) |
| 117 | + parser.add_argument("--workdir", help="Working directory path") |
| 118 | + parser.add_argument("--repo", help="Repository name") |
| 119 | + args = parser.parse_args() |
| 120 | + |
| 121 | + if args.workdir: |
| 122 | + args.path = ''.join([args.workdir, '/', args.path]) |
| 123 | + fragments_path = ''.join([args.workdir, '/', fragments_path]) |
| 124 | + |
| 125 | + if args.repo: |
| 126 | + default_repolink = repo_dict[args.repo] |
| 127 | + |
| 128 | + try: |
| 129 | + makedirs(fragments_path) |
| 130 | + except FileExistsError as e: |
| 131 | + pass |
| 132 | + |
| 133 | + print("Skipped entries should be manually created") |
| 134 | + with open(args.path, 'r') as f: |
| 135 | + iterate_lines(f) |
0 commit comments