|
| 1 | +import re |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | +from jira import JIRA |
| 5 | +from jira.exceptions import JIRAError |
| 6 | +import json |
| 7 | + |
| 8 | +commit_pattern = re.compile(r'^(\w*):\s*(.*?)?:\s*(.*?)\s*(\(#(\d+)\))?$') |
| 9 | + |
| 10 | +PREVIOUS_REF = os.getenv("PREVIOUS_REF").strip('\"') |
| 11 | +BASE_BRANCH = os.getenv("BASE_BRANCH", "origin/develop").strip('\"') |
| 12 | + |
| 13 | +JIRA_USERNAME = os. getenv( "JIRA_USERNAME"). strip( '\"') # in email format, e.g [email protected] |
| 14 | +JIRA_TOKEN = os.getenv("JIRA_TOKEN").strip('\"') # https://id.atlassian.com/manage-profile/security/api-tokens |
| 15 | +JIRA_SERVER = os.getenv("JIRA_SERVER", "https://heartex.atlassian.net").strip('\"') |
| 16 | +JIRA_RN_FIELD = os.getenv("JIRA_RN_FIELD", "customfield_10064").strip('\"') |
| 17 | +OUTPUT_FILE_MD = os.getenv("OUTPUT_FILE_MD", None) |
| 18 | +OUTPUT_FILE_JSON = os.getenv("OUTPUT_FILE_JSON", None) |
| 19 | + |
| 20 | +jira = JIRA(JIRA_SERVER, basic_auth=(JIRA_USERNAME, JIRA_TOKEN)) |
| 21 | + |
| 22 | +title_map = { |
| 23 | + 'feat': 'New Features', |
| 24 | + 'fix': 'Bug Fixes', |
| 25 | + 'perf': 'Performance Improvements', |
| 26 | + 'refactor': 'Code Refactoring', |
| 27 | + 'docs': 'Documentation' |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +def execute_git_cmd(cmd): |
| 32 | + git_command = " ".join(["git", cmd]) |
| 33 | + return execute_cmd(git_command) |
| 34 | + |
| 35 | + |
| 36 | +def execute_cmd(cmd): |
| 37 | + try: |
| 38 | + out = subprocess.check_output(cmd, shell=True, universal_newlines=True, stderr=subprocess.STDOUT) |
| 39 | + except subprocess.CalledProcessError as err: |
| 40 | + print(f"Aborting: Got error while calling \"{cmd}\"") |
| 41 | + print(f"Details: {err}") |
| 42 | + print(f"Details: {err.output}") |
| 43 | + exit(1) |
| 44 | + return out.strip() |
| 45 | + |
| 46 | + |
| 47 | +def execute_cmd_visible(cmd): |
| 48 | + with subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, |
| 49 | + universal_newlines=True, stderr=subprocess.STDOUT) as proc: |
| 50 | + for line in proc.stdout: |
| 51 | + print(line, end='') |
| 52 | + if proc.returncode != 0: |
| 53 | + print(f"Aborting: Got error while calling \"{cmd}\"") |
| 54 | + exit(1) |
| 55 | + |
| 56 | + |
| 57 | +def get_head_sha(): |
| 58 | + return execute_git_cmd("rev-parse HEAD") |
| 59 | + |
| 60 | + |
| 61 | +def generate_changelog(tag_new, previous_release_id): |
| 62 | + out = execute_git_cmd(f"log --reverse --pretty=format:%s {tag_new}..{previous_release_id}") |
| 63 | + return out |
| 64 | + |
| 65 | + |
| 66 | +def get_branch_diverge_commit(branch, base_branch): |
| 67 | + out = execute_git_cmd(f"rev-list --boundary {branch}...{base_branch} | grep '^-' | cut -c2- | tail -n1") |
| 68 | + return out |
| 69 | + |
| 70 | + |
| 71 | +def changelog_list(raw_changelog) -> list: |
| 72 | + changelog_list = [] |
| 73 | + for line in raw_changelog.splitlines(): |
| 74 | + if (match := re.match(commit_pattern, line)) is not None: |
| 75 | + if match.group(1) in title_map.keys(): |
| 76 | + title = match.group(1) |
| 77 | + jira = match.group(2) |
| 78 | + changelog_list.append(title + jira) |
| 79 | + return list(set(changelog_list)) |
| 80 | + |
| 81 | + |
| 82 | +def jira_release_note(ticket): |
| 83 | + try: |
| 84 | + issue = jira.issue(ticket) |
| 85 | + return issue.get_field(JIRA_RN_FIELD) |
| 86 | + except JIRAError: |
| 87 | + return '' |
| 88 | + |
| 89 | + |
| 90 | +def changelog_map(raw_changelog, tasks_list): |
| 91 | + changelog_dict = {} |
| 92 | + changelog_dup_dict = {} |
| 93 | + for line in raw_changelog.splitlines(): |
| 94 | + if (match := re.match(commit_pattern, line)) is not None: |
| 95 | + if match.group(1) in title_map.keys(): |
| 96 | + title = match.group(1) |
| 97 | + ticket = match.group(2) |
| 98 | + msg = match.group(3) |
| 99 | + pr_id = match.group(5) if match.group(5) else '' |
| 100 | + jira_rn = jira_release_note(ticket) |
| 101 | + desc = jira_rn.replace("\n", " ") if jira_rn else msg |
| 102 | + list_key = title + ticket |
| 103 | + entry = {'desc': desc, 'pr': pr_id} |
| 104 | + if list_key in tasks_list: |
| 105 | + if title in changelog_dup_dict: |
| 106 | + changelog_dup_dict[title].update({ticket: entry}) |
| 107 | + else: |
| 108 | + changelog_dup_dict[title] = {ticket: entry} |
| 109 | + elif title in changelog_dict: |
| 110 | + changelog_dict[title].update({ticket: entry}) |
| 111 | + else: |
| 112 | + changelog_dict[title] = {ticket: entry} |
| 113 | + return changelog_dict, changelog_dup_dict |
| 114 | + |
| 115 | + |
| 116 | +def format_changelog(changelog) -> str: |
| 117 | + result_lines = [] |
| 118 | + for key in title_map.keys(): |
| 119 | + if key in changelog.keys(): |
| 120 | + result_lines.append(f"### {title_map[key]}") |
| 121 | + for jira, line in changelog[key].items(): |
| 122 | + result_lines.append(f"- {line['desc']} [{jira.upper()}]") |
| 123 | + return '\n'.join(result_lines) |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + new_tag = os.getenv("NEW_TAG", get_head_sha()) |
| 128 | + |
| 129 | + previous_tag_base_commit = get_branch_diverge_commit(PREVIOUS_REF, BASE_BRANCH) |
| 130 | + branch_diff_changelog = generate_changelog(previous_tag_base_commit, PREVIOUS_REF) |
| 131 | + previous_tag_tasks_list = changelog_list(branch_diff_changelog) |
| 132 | + |
| 133 | + new_changelog = generate_changelog(previous_tag_base_commit, new_tag) |
| 134 | + changelog_new, changelog_dup = changelog_map(new_changelog, previous_tag_tasks_list) |
| 135 | + |
| 136 | + if OUTPUT_FILE_MD: |
| 137 | + with open(OUTPUT_FILE_MD, 'w') as f: |
| 138 | + print(f"Creating a markdown output file: '{OUTPUT_FILE_MD}'") |
| 139 | + f.write(format_changelog(changelog_new)) |
| 140 | + |
| 141 | + if OUTPUT_FILE_JSON: |
| 142 | + with open(OUTPUT_FILE_JSON, 'w') as f: |
| 143 | + print(f"Creating a json output file: '{OUTPUT_FILE_JSON}'") |
| 144 | + json.dump(changelog_new, f) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + main() |
0 commit comments