From 61aed80cdcaca8a497047084828c007dc25dadfa Mon Sep 17 00:00:00 2001 From: tushenmei Date: Thu, 22 Jul 2021 15:09:23 +0800 Subject: [PATCH 1/2] add md operation --- release-assistant/javcra/api/gitee_api.py | 4 +- .../application/modifypart/modifyentrance.py | 129 +++++++++++++++++- 2 files changed, 130 insertions(+), 3 deletions(-) diff --git a/release-assistant/javcra/api/gitee_api.py b/release-assistant/javcra/api/gitee_api.py index 79a9b3c..df9d5b0 100644 --- a/release-assistant/javcra/api/gitee_api.py +++ b/release-assistant/javcra/api/gitee_api.py @@ -71,7 +71,7 @@ class Issue: retry_count = 0 while resp.status_code == 408 and retry_count < retry: - logger.error("api request timed out, retrying %s.", retry_count) + logger.error("api request timed out, retrying %s." % retry_count) resp = requests.request(method.lower(), url, params=params, headers=self.headers) retry_count += 1 @@ -297,7 +297,7 @@ class Issue: if block_res: pkg_set.update(self.__process_body_for_pkglist(block_res[block_name], block_name=block_name)) else: - logger.warning("not found %s content when getting pkglist from specific part.", block_name) + logger.warning("not found %s content when getting pkglist from specific part." % block_name) return pkg_set def get_update_list(self): diff --git a/release-assistant/javcra/application/modifypart/modifyentrance.py b/release-assistant/javcra/application/modifypart/modifyentrance.py index 742654b..d318501 100644 --- a/release-assistant/javcra/application/modifypart/modifyentrance.py +++ b/release-assistant/javcra/application/modifypart/modifyentrance.py @@ -13,10 +13,14 @@ """ Description: modify entrance """ +import re +from javcra.libs.log import logger class Operation: - + """ + md operation for release issue description + """ def init_md_table(self, t_head=None, body_info=None, block_title="", prefix="", suffix=""): """ initialize the md table of specific part like "CVE part" for release issue @@ -104,3 +108,126 @@ class Operation: break return issue_body_lines[block_start_idx:block_end_idx], block_start_idx, block_end_idx + + @staticmethod + def modify_block_lines(origin_lines, block_lines, block_start, block_end): + """ + modify block lines for add or delete operation + + Args: + origin_lines: list, issue body splitlines + block_lines: list, block str splitlines + block_start: start index of block + block_end: end index of block + + Returns: + new lines for issue body, list + """ + # to get count and then modify str "修复CVE xxx个" + fix_line_idx = -1 + count = 0 + for index, cur_line in enumerate(block_lines): + if cur_line.startswith("修复"): + fix_line_idx = index + if cur_line.startswith("|#"): + count += 1 + + if fix_line_idx != -1: + block_lines[fix_line_idx] = re.sub( + "\d+", str(count), block_lines[fix_line_idx] + ) + + # modify block lines + origin_lines[block_start:block_end] = block_lines + return origin_lines + + @staticmethod + def __append_info_in_specific_block(append_info, block_lines): + """ + append info in specific block for add operation + + Args: + append_info: issue info or requires info, dict + block_lines: lines of specific block + + Returns: + block_lines: block lines after append + """ + + for key, value in append_info.items(): + # if the issue to be added is already in the table, then continue + if any([key in line for line in block_lines]): + logger.warning("issue {} already exists in body content.".format(key)) + continue + + # if the requires info to be added already in the table, then not add + value_lines = value.splitlines(keepends=True) + append_value_lines = [] + for line in value_lines: + if line not in block_lines: + append_value_lines.append(line) + + value = "".join(append_value_lines) + block_lines.append(value) + + return block_lines + + @staticmethod + def __delete_issue_in_specific_block(delete_issue, block_lines): + """ + delete issue in specific block for delete operation + + Args: + block_lines: lines of specific block + delete_issue: issue to delete + + Returns: + block_lines: block lines after delete + """ + to_remove_idx = -1 + for idx, block_line in enumerate(block_lines): + if delete_issue in block_line: + to_remove_idx = idx + break + + if to_remove_idx != -1: + block_lines.pop(to_remove_idx) + else: + logger.warning("The issue {} does not exist in release issue description." + "".format(delete_issue)) + return block_lines + + def get_new_body_lines(self, old_issue_body, append_info=None, delete_issue=None, + start_flag="", end_flag="\n"): + """ + generating a new issue body by add and delete operation + + Args: + old_issue_body: old issue body + append_info: issues to add. like {issue_id:{"repo":..,"status":...},...} + delete_issue: issues to delete. + start_flag: start flag of block + end_flag: end flag of block. + + Raises: + ValueError: + append_info、 delete_info need at least one + + Returns: + new body lines + """ + if not any((append_info, delete_issue)): + raise ValueError("append_info and delete_info need at least one") + + issue_body_lines = old_issue_body.splitlines(keepends=True) + block_lines, block_start_idx, block_end_idx = self.get_block_lines( + issue_body_lines, start_flag, end_flag) + + if append_info: + block_lines = self.__append_info_in_specific_block(append_info, block_lines) + else: + block_lines = self.__delete_issue_in_specific_block(delete_issue, block_lines) + + final_lines = self.modify_block_lines(issue_body_lines, block_lines, block_start_idx, + block_end_idx) + return "".join(final_lines) -- Gitee From f1f925ee9cd2045c06b6d0ccb0e0da0ca268cb23 Mon Sep 17 00:00:00 2001 From: tushenmei Date: Tue, 27 Jul 2021 20:39:51 +0800 Subject: [PATCH 2/2] fix code for review --- .../javcra/application/modifypart/modifyentrance.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/release-assistant/javcra/application/modifypart/modifyentrance.py b/release-assistant/javcra/application/modifypart/modifyentrance.py index d318501..155b6c0 100644 --- a/release-assistant/javcra/application/modifypart/modifyentrance.py +++ b/release-assistant/javcra/application/modifypart/modifyentrance.py @@ -21,6 +21,7 @@ class Operation: """ md operation for release issue description """ + def init_md_table(self, t_head=None, body_info=None, block_title="", prefix="", suffix=""): """ initialize the md table of specific part like "CVE part" for release issue @@ -127,8 +128,11 @@ class Operation: fix_line_idx = -1 count = 0 for index, cur_line in enumerate(block_lines): + # demo: 修复CVE xxx个 if cur_line.startswith("修复"): fix_line_idx = index + + # demo: |#I41R53:CVE-2021-36222|krb5| if cur_line.startswith("|#"): count += 1 @@ -157,7 +161,7 @@ class Operation: for key, value in append_info.items(): # if the issue to be added is already in the table, then continue if any([key in line for line in block_lines]): - logger.warning("issue {} already exists in body content.".format(key)) + logger.info("issue {} already exists in body content.".format(key)) continue # if the requires info to be added already in the table, then not add @@ -193,8 +197,8 @@ class Operation: if to_remove_idx != -1: block_lines.pop(to_remove_idx) else: - logger.warning("The issue {} does not exist in release issue description." - "".format(delete_issue)) + logger.info("The issue {} does not exist in release issue description." + "".format(delete_issue)) return block_lines def get_new_body_lines(self, old_issue_body, append_info=None, delete_issue=None, -- Gitee