From 8755557d0fc987ea3051aee3cb428b8c648b45b8 Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Mon, 16 Oct 2023 18:54:28 +0800 Subject: [PATCH 1/9] restructed some code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 一些重复性代码被重构 其中一部分使用列表推导 --- .../ChangelogGenerator/changelog_generator.py | 78 ++++++++----------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index de62786647..e98e232c5a 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -79,53 +79,37 @@ def print_commits(commits: dict, indent: str = "", need_sort: bool = True) -> (s if need_sort and indent == "": for commit_hash, commit_info in commits.items(): commit_message = commit_info["message"] - if False: - pass - elif commit_message.find("修复") != -1: - sorted_commits["fix"].update({commit_hash: commit_info}) - elif commit_message.find("新增") != -1: - sorted_commits["feat"].update({commit_hash: commit_info}) - elif commit_message.find("改进") != -1 or commit_message.find("更新") != -1 or commit_message.find("优化") != -1 or commit_message.find("重构") != -1: - sorted_commits["perf"].update({commit_hash: commit_info}) - elif commit_message.startswith("feat"): - sorted_commits["feat"].update({commit_hash: commit_info}) - elif commit_message.startswith("perf"): - sorted_commits["perf"].update({commit_hash: commit_info}) - elif commit_message.startswith("fix"): - sorted_commits["fix"].update({commit_hash: commit_info}) - else: - sorted_commits["other"].update({commit_hash: commit_info}) - - if sorted_commits["feat"]: - ret_message += "\n### 新增\n\n" - mes, ctrs = print_commits(sorted_commits["feat"], "", False) - ret_message += mes - for ctr in ctrs: - if ret_contributor.count(ctr) == 0: - ret_contributor.append(ctr) - - if sorted_commits["perf"]: - ret_message += "\n### 改进\n\n" - mes, ctrs = print_commits(sorted_commits["perf"], "", False) - ret_message += mes - for ctr in ctrs: - if ret_contributor.count(ctr) == 0: - ret_contributor.append(ctr) - if sorted_commits["fix"]: - ret_message += "\n### 修复\n\n" - mes, ctrs = print_commits(sorted_commits["fix"], "", False) - ret_message += mes - for ctr in ctrs: - if ret_contributor.count(ctr) == 0: - ret_contributor.append(ctr) - if sorted_commits["other"]: - ret_message += "\n### 其他\n\n" - mes, ctrs = print_commits(sorted_commits["other"], "", False) - ret_message += mes - for ctr in ctrs: - if ret_contributor.count(ctr) == 0: - ret_contributor.append(ctr) - + # XXX + # 重复性高,可重构为factory结构 + # Done at 2023.10.16 + CNtoEN_Factory = { + ('修复'): 'fix', + ('新增'): 'feat', + ('改进', '更新', '优化', '重构'): 'perf', + ('其他'): 'other' + } + def updateCommits(): + # 中文operation + push = [trans for keys, trans in CNtoEN_Factory.items() if any(key in commit_message for key in keys)] + if not push: # 英文operation + push = [key for key in CNtoEN_Factory.values() if commit_message.startswith(key)] + oper = push[0] if push else 'other' + + sorted_commits[oper].update({commit_hash: commit_info}) + + def updateMessages(): + for keys, trans in CNtoEN_Factory.items(): + if sorted_commits[trans]: + ret_message += f"\n### {keys[0]}\n\n" + mes, ctrs = print_commits(sorted_commits[trans], "", False) + ret_message += mes + for ctr in ctrs: + if ret_contributor.count(ctr) == 0: + ret_contributor.append(ctr) + + updateCommits() + updateMessages() + else: for commit_hash, commit_info in commits.items(): commit_message = commit_info["message"] From d4a2e2c0c8062678f2a6a2d376ef8fcc744f31fc Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Mon, 16 Oct 2023 19:01:01 +0800 Subject: [PATCH 2/9] Update changelog_generator.py deleted XXX label --- tools/ChangelogGenerator/changelog_generator.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index e98e232c5a..8135516556 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -79,9 +79,7 @@ def print_commits(commits: dict, indent: str = "", need_sort: bool = True) -> (s if need_sort and indent == "": for commit_hash, commit_info in commits.items(): commit_message = commit_info["message"] - # XXX - # 重复性高,可重构为factory结构 - # Done at 2023.10.16 + CNtoEN_Factory = { ('修复'): 'fix', ('新增'): 'feat', From 96ab86dae04a0805e109f7d326446da7675ed8fb Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Mon, 16 Oct 2023 19:12:18 +0800 Subject: [PATCH 3/9] fixed err --- tools/ChangelogGenerator/changelog_generator.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index 8135516556..91ff99c250 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -79,7 +79,9 @@ def print_commits(commits: dict, indent: str = "", need_sort: bool = True) -> (s if need_sort and indent == "": for commit_hash, commit_info in commits.items(): commit_message = commit_info["message"] - + # XXX + # 重复性高,可重构为factory结构 + # Done at 2023.10.16 CNtoEN_Factory = { ('修复'): 'fix', ('新增'): 'feat', @@ -95,18 +97,19 @@ def print_commits(commits: dict, indent: str = "", need_sort: bool = True) -> (s sorted_commits[oper].update({commit_hash: commit_info}) - def updateMessages(): + def updateMessages(return_message): for keys, trans in CNtoEN_Factory.items(): if sorted_commits[trans]: - ret_message += f"\n### {keys[0]}\n\n" + return_message += f"\n### {keys[0]}\n\n" mes, ctrs = print_commits(sorted_commits[trans], "", False) - ret_message += mes + return_message += mes for ctr in ctrs: if ret_contributor.count(ctr) == 0: ret_contributor.append(ctr) - + updateCommits() - updateMessages() + ret_message = updateMessages(ret_message) + else: for commit_hash, commit_info in commits.items(): From 62b1dfdf38dad7fa2520b0973140f171d915193b Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Mon, 16 Oct 2023 19:35:44 +0800 Subject: [PATCH 4/9] Update changelog_generator.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit deleted XXX label 编辑器里忘了删了导致再次出现这个label --- tools/ChangelogGenerator/changelog_generator.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index 91ff99c250..a19ca23f51 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -79,9 +79,7 @@ def print_commits(commits: dict, indent: str = "", need_sort: bool = True) -> (s if need_sort and indent == "": for commit_hash, commit_info in commits.items(): commit_message = commit_info["message"] - # XXX - # 重复性高,可重构为factory结构 - # Done at 2023.10.16 + CNtoEN_Factory = { ('修复'): 'fix', ('新增'): 'feat', From 56585c63031761c297c83066e0a37d7bb28a5538 Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:43:14 +0800 Subject: [PATCH 5/9] restructed the func print_commits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 注意:暂未测试 --- .../ChangelogGenerator/changelog_generator.py | 160 +++++++++--------- 1 file changed, 83 insertions(+), 77 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index a19ca23f51..38d9cd4225 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -17,6 +17,89 @@ ignore_merge_author = False contributors = {} raw_commits_info = {} +def print_commits(commits: dict, indent: str = "") -> (str, list): + if not commits: return ("", []) + ret_message = "" + ret_contributor = [] + + for commit_hash, commit_info in commits.items(): + commit_message = commit_info["message"] + + if not with_commitizen: + commitizens = r"(?:build|chore|ci|docs?|feat|fix|perf|refactor|rft|style|test)" + commit_message = re.sub(rf"^(?:{commitizens}, *)*{commitizens} *(?:\([^\)]*\))*: *", "", commit_message) + + ret_message += indent + "- " + commit_message + + mes, ctrs = print_commits(commit_info["branch"], indent + " ") + + if not ignore_merge_author or not commit_info["branch"]: + author = commit_info["author"] + if author not in ctrs: ctrs.append(author) + if committer_is_author: + committer = commit_info["committer"] + if committer not in ctrs: ctrs.append(committer) + + for ctr in ctrs: + if ctr == "web-flow": continue # 这个账号是 GitHub 在 Merge PR 时的 committer + if ret_contributor.count(ctr) == 0: + ret_contributor.append(ctr) + ret_message += " @" + ctr + + if with_hash: + ret_message += f" ({commit_hash})" + + ret_message += "\n" + mes + + return ret_message, ret_contributor + +def updateCommits(commit_message, sorted_commits, update_dict): + translations = { + '修复': 'fix', + '新增': 'feat', + '改进': 'perf', + '更新': 'perf', + '优化': 'perf', + '重构': 'perf', + '其他': 'other' + } + oper = 'other' + for key, trans in translations.items(): + if key in commit_message or commit_message.startswith(trans): + oper = trans + break + sorted_commits[oper].update(update_dict) + +def updateMessage(sorted_commits, ret_message, ret_contributor): + translations_resort = { + '新增': 'feat', + '改进': 'perf', + '修复': 'fix', + '其他': 'other' + } + for key, trans in translations_resort.items(): + if sorted_commits[trans]: + ret_message += f"\n### {key}\n\n" + mes, ctrs = print_commits(sorted_commits[trans], "") + ret_message += mes + for ctr in ctrs: + if ret_contributor.count(ctr) == 0: + ret_contributor.append(ctr) + return (ret_message, ) + +def individual_commits(commits: dict): + sorted_commits = { + "perf": {}, + "feat": {}, + "fix": {}, + "other": {}, + } + for commit_hash, commit_info in commits.items(): + commit_message = commit_info["message"] + updateCommits(commit_message, sorted_commits, {commit_hash: commit_info}) + + return updateMessage(sorted_commits, '', []) + def build_commits_tree(commit_hash: str): ''' 返回值为当前 commit 与其 parents 的信息。 @@ -65,83 +148,6 @@ def build_commits_tree(commit_hash: str): res.pop(commit_hash) return res -def print_commits(commits: dict, indent: str = "", need_sort: bool = True) -> (str, list): - if not commits: return ("", []) - ret_message = "" - ret_contributor = [] - - sorted_commits = { - "perf": {}, - "feat": {}, - "fix": {}, - "other": {}, - } - if need_sort and indent == "": - for commit_hash, commit_info in commits.items(): - commit_message = commit_info["message"] - - CNtoEN_Factory = { - ('修复'): 'fix', - ('新增'): 'feat', - ('改进', '更新', '优化', '重构'): 'perf', - ('其他'): 'other' - } - def updateCommits(): - # 中文operation - push = [trans for keys, trans in CNtoEN_Factory.items() if any(key in commit_message for key in keys)] - if not push: # 英文operation - push = [key for key in CNtoEN_Factory.values() if commit_message.startswith(key)] - oper = push[0] if push else 'other' - - sorted_commits[oper].update({commit_hash: commit_info}) - - def updateMessages(return_message): - for keys, trans in CNtoEN_Factory.items(): - if sorted_commits[trans]: - return_message += f"\n### {keys[0]}\n\n" - mes, ctrs = print_commits(sorted_commits[trans], "", False) - return_message += mes - for ctr in ctrs: - if ret_contributor.count(ctr) == 0: - ret_contributor.append(ctr) - - updateCommits() - ret_message = updateMessages(ret_message) - - - else: - for commit_hash, commit_info in commits.items(): - commit_message = commit_info["message"] - - if not with_commitizen: - commitizens = r"(?:build|chore|ci|docs?|feat|fix|perf|refactor|rft|style|test)" - commit_message = re.sub(rf"^(?:{commitizens}, *)*{commitizens} *(?:\([^\)]*\))*: *", "", commit_message) - - ret_message += indent + "- " + commit_message - - mes, ctrs = print_commits(commit_info["branch"], indent + " ", False) - - if not ignore_merge_author or not commit_info["branch"]: - author = commit_info["author"] - if author not in ctrs: ctrs.append(author) - if committer_is_author: - committer = commit_info["committer"] - if committer not in ctrs: ctrs.append(committer) - - for ctr in ctrs: - if ctr == "web-flow": continue # 这个账号是 GitHub 在 Merge PR 时的 committer - if ret_contributor.count(ctr) == 0: - ret_contributor.append(ctr) - ret_message += " @" + ctr - - if with_hash: - ret_message += f" ({commit_hash})" - - ret_message += "\n" + mes - - return ret_message, ret_contributor - - def retry_urlopen(*args, **kwargs): import time import http.client From 19fecf4e69c33c12aeb939fc54a8977b43afec3e Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Tue, 17 Oct 2023 07:44:37 +0800 Subject: [PATCH 6/9] fix and correct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正比对顺序 修改函数入口(调整函数名) --- .../ChangelogGenerator/changelog_generator.py | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index 38d9cd4225..8391080af6 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -17,7 +17,24 @@ ignore_merge_author = False contributors = {} raw_commits_info = {} -def print_commits(commits: dict, indent: str = "") -> (str, list): +translations = { + '修复': 'fix', + '新增': 'feat', + '更新': 'perf', + '改进': 'perf', + '优化': 'perf', + '重构': 'perf', + '其他': 'other' + } + +translations_resort = { + '新增': 'feat', + '改进': 'perf', + '修复': 'fix', + '其他': 'other' + } + +def individual_commits(commits: dict, indent: str = "") -> (str, list): if not commits: return ("", []) ret_message = "" ret_contributor = [] @@ -31,7 +48,7 @@ def print_commits(commits: dict, indent: str = "") -> (str, list): ret_message += indent + "- " + commit_message - mes, ctrs = print_commits(commit_info["branch"], indent + " ") + mes, ctrs = individual_commits(commit_info["branch"], indent + " ") if not ignore_merge_author or not commit_info["branch"]: author = commit_info["author"] @@ -54,40 +71,30 @@ def print_commits(commits: dict, indent: str = "") -> (str, list): return ret_message, ret_contributor def updateCommits(commit_message, sorted_commits, update_dict): - translations = { - '修复': 'fix', - '新增': 'feat', - '改进': 'perf', - '更新': 'perf', - '优化': 'perf', - '重构': 'perf', - '其他': 'other' - } oper = 'other' for key, trans in translations.items(): - if key in commit_message or commit_message.startswith(trans): + if key in commit_message: oper = trans break + else: + for key in set(translations.values()): + if commit_message.startswith(key): + oper = key + break sorted_commits[oper].update(update_dict) def updateMessage(sorted_commits, ret_message, ret_contributor): - translations_resort = { - '新增': 'feat', - '改进': 'perf', - '修复': 'fix', - '其他': 'other' - } for key, trans in translations_resort.items(): if sorted_commits[trans]: ret_message += f"\n### {key}\n\n" - mes, ctrs = print_commits(sorted_commits[trans], "") + mes, ctrs = individual_commits(sorted_commits[trans], "") ret_message += mes for ctr in ctrs: if ret_contributor.count(ctr) == 0: ret_contributor.append(ctr) return (ret_message, ) -def individual_commits(commits: dict): +def print_commits(commits: dict): sorted_commits = { "perf": {}, "feat": {}, From f827408e63b25c6de9f69a410a9552978909f8ea Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Tue, 17 Oct 2023 10:30:49 +0800 Subject: [PATCH 7/9] Update changelog_generator.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 经讨论决定修改代码逻辑: 以开头为优先标记 其次中文(隐)包含标记 --- tools/ChangelogGenerator/changelog_generator.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index 8391080af6..64269490ac 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -73,14 +73,9 @@ def individual_commits(commits: dict, indent: str = "") -> (str, list): def updateCommits(commit_message, sorted_commits, update_dict): oper = 'other' for key, trans in translations.items(): - if key in commit_message: + if key in commit_message or commit_message.startswith(trans): oper = trans break - else: - for key in set(translations.values()): - if commit_message.startswith(key): - oper = key - break sorted_commits[oper].update(update_dict) def updateMessage(sorted_commits, ret_message, ret_contributor): From 5d9a0ea940749da9bde3a75d3f5db8bc4f544bd8 Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Tue, 17 Oct 2023 18:46:15 +0800 Subject: [PATCH 8/9] renamed functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用蛇形命名法 --- tools/ChangelogGenerator/changelog_generator.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index 64269490ac..1ca6d99862 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -70,15 +70,20 @@ def individual_commits(commits: dict, indent: str = "") -> (str, list): return ret_message, ret_contributor -def updateCommits(commit_message, sorted_commits, update_dict): +def update_commits(commit_message, sorted_commits, update_dict): oper = 'other' for key, trans in translations.items(): - if key in commit_message or commit_message.startswith(trans): + if key in commit_message: oper = trans break + else: + for key in set(translations.values()): + if commit_message.startswith(key): + oper = key + break sorted_commits[oper].update(update_dict) -def updateMessage(sorted_commits, ret_message, ret_contributor): +def update_message(sorted_commits, ret_message, ret_contributor): for key, trans in translations_resort.items(): if sorted_commits[trans]: ret_message += f"\n### {key}\n\n" @@ -98,9 +103,9 @@ def print_commits(commits: dict): } for commit_hash, commit_info in commits.items(): commit_message = commit_info["message"] - updateCommits(commit_message, sorted_commits, {commit_hash: commit_info}) + update_commits(commit_message, sorted_commits, {commit_hash: commit_info}) - return updateMessage(sorted_commits, '', []) + return update_message(sorted_commits, '', []) def build_commits_tree(commit_hash: str): ''' From da34081a5330f0b7d6836266b02a43538595bc17 Mon Sep 17 00:00:00 2001 From: quyansiyuanwang <132176401+quyansiyuanwang@users.noreply.github.com> Date: Tue, 17 Oct 2023 19:53:53 +0800 Subject: [PATCH 9/9] deleted extra indent --- .../ChangelogGenerator/changelog_generator.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/ChangelogGenerator/changelog_generator.py b/tools/ChangelogGenerator/changelog_generator.py index 1ca6d99862..a2f15bdd87 100644 --- a/tools/ChangelogGenerator/changelog_generator.py +++ b/tools/ChangelogGenerator/changelog_generator.py @@ -18,21 +18,21 @@ contributors = {} raw_commits_info = {} translations = { - '修复': 'fix', - '新增': 'feat', - '更新': 'perf', - '改进': 'perf', - '优化': 'perf', - '重构': 'perf', - '其他': 'other' - } + '修复': 'fix', + '新增': 'feat', + '更新': 'perf', + '改进': 'perf', + '优化': 'perf', + '重构': 'perf', + '其他': 'other' +} translations_resort = { - '新增': 'feat', - '改进': 'perf', - '修复': 'fix', - '其他': 'other' - } + '新增': 'feat', + '改进': 'perf', + '修复': 'fix', + '其他': 'other' +} def individual_commits(commits: dict, indent: str = "") -> (str, list): if not commits: return ("", [])