diff --git a/release-assistant/javcra/application/modifypart/modifyentrance.py b/release-assistant/javcra/application/modifypart/modifyentrance.py index 34041f3494383e0be3ee08032f28c51b49b351c1..d5bbef3c33d7b2e8b8546f2e08572c7a754f52ed 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 datetime import re +import requests + from javcra.api.gitee_api import Issue from javcra.libs.log import logger +from javcra.libs.read_excel import download_file class Operation(Issue): @@ -238,6 +242,55 @@ class Operation(Issue): block_end_idx) return "".join(final_lines) + +class CveIssue(Operation): + """ + operation CVE in issue + """ + def __init__(self, repo, token, issue_num): + super().__init__(repo, token, issue_num) + + def create_cve_list(self, user_email): + """ + The CVE-Manager is triggered to generate the CVE list and archive it + Args: + user_email (str): gitee user email + """ + # Take cve within three months + start_time = (datetime.datetime.now() + datetime.timedelta(days=-90)).strftime('%Y-%m-%d') + email_name = user_email.split('@')[0] + url = "https://api.openeuler.org/cve-manager/v1/download/excel/triggerCveData?startTime=" + \ + start_time + "&typeName=" + email_name + try: + response = requests.get(url, headers=self.headers) + if response.status_code == 200: + logger.info("The CVE-Manager is triggered to generate the CVE list and archive the CVE list") + return True + logger.error("The CVE List file fails to be archived %s" % response.status_code) + return False + except requests.RequestException as error: + logger.error("The CVE List file fails to be archived because %s " % error) + return False + + def get_cve_list(self): + """ + Obtain cVE-related information provided by the CVE-Manager. + Returns: + cve_list: Data in Excel in dictionary form + """ + + now_time = datetime.date(datetime.date.today().year, datetime.date.today().month, + datetime.date.today().day).strftime('%Y-%m-%d') + branch_name = self.get_update_issue_branch() + if not branch_name: + logger.error("Failed to obtain branch") + return [] + cve_list = download_file(now_time, "{}_updateinfo.xlsx".format(branch_name)) + if not cve_list: + logger.error("Failed to obtain CVE data") + return [] + return cve_list + def add_for_specific_block(self, body_str, issues, table_head, block_name): """ add info in specific block diff --git a/release-assistant/javcra/common/constant.py b/release-assistant/javcra/common/constant.py index 80f8c112ad22cf67072bbb44992a8367f3565fa1..313ee0c14d2bd4fa16b839a64fa8eef681cc517f 100644 --- a/release-assistant/javcra/common/constant.py +++ b/release-assistant/javcra/common/constant.py @@ -26,6 +26,10 @@ PERMISSION_DICT = { 'cvrfok': 'security', 'start': 'manager' } + +CVE_MANAGE_SERVER = "obs.ap-southeast-1.myhuaweicloud.com" +CVE_MANAGE_BUCKET_NAME = "openeuler-cve-cvrf" +CVE_UPDATE_INFO = "cve-manager-updateinfo" PERMISSION_INFO = { "version_manager": ["/start-update", "/no-release"], "security_committee": [ diff --git a/release-assistant/javcra/libs/read_excel.py b/release-assistant/javcra/libs/read_excel.py new file mode 100644 index 0000000000000000000000000000000000000000..07edb6f3361f304c600941e82317327ecea03e03 --- /dev/null +++ b/release-assistant/javcra/libs/read_excel.py @@ -0,0 +1,104 @@ +#!/usr/bin/python3 +# ****************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# ******************************************************************************/ +""" +The cloud downloads the file and reads the data inside +""" + +import os +import shutil +import uuid + +import pandas as pd + +from javcra.api.obscloud import ObsCloud +from javcra.common.constant import CVE_MANAGE_BUCKET_NAME +from javcra.common.constant import CVE_MANAGE_SERVER +from javcra.common.constant import CVE_UPDATE_INFO +from javcra.libs.log import logger + + +def read_excel_xlsx(curr_path): + """ + to read excel file content + Args: + curr_path: curr_path + Returns: + + """ + df = pd.read_excel(curr_path, sheet_name="cve_list") + df.fillna("", inplace=True) + df_list = [] + for i in df.index.values: + df_line = df.loc[i, ['cve编号', 'issue编号', 'issue所属仓库', + 'score', "version", "abi是否变化"]].to_dict() + df_list.append(df_line) + out_list = [] + for data_dict in df_list: + df_dict = { + "CVE": "#" + + data_dict.get("issue编号") + + ":" + + data_dict.get("cve编号"), + "abi是否变化": data_dict.get("abi是否变化"), + "score": data_dict.get("score"), + "version": data_dict.get("version"), + "仓库": data_dict.get("issue所属仓库"), + "status": "已完成", + } + out_list.append(df_dict) + + return out_list + + +def download_file(now_time, file_name): + """ + download file content + Args: + now_time: now_time + file_name: file_name + Returns: + cve_list: + """ + file_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) + temp_path = os.path.join(file_path, "tmp{}".format(str(uuid.uuid1().hex))) + try: + obs_client = ObsCloud(os.getenv("ak"), os.getenv("sk"), CVE_MANAGE_SERVER, CVE_MANAGE_BUCKET_NAME) + # Determine whether the file to be downloaded is in the object of the bucket + files = obs_client.bucket_list("{}/{}".format(CVE_UPDATE_INFO, now_time)) + file_object = "" + for file in files: + if "{CVE_UPDATE_INFO}/{date}/{title}".format( + CVE_UPDATE_INFO=CVE_UPDATE_INFO, date=now_time, title=file_name) == file: + file_object = file + if not file_object: + logger.error("The object does not exist in the bucket") + return [] + if not os.path.exists(temp_path): + os.mkdir(temp_path) + file_path = os.path.join(temp_path, 'Temporary_Files.xlsx') + + # The download file + res = obs_client.down_load_file(file_object, file_path) + if not res: + logger.error("The object does not exist in the bucket %s" % file_path) + return [] + + # Read the file contents in Excel + cve_list = read_excel_xlsx(file_path) + return cve_list + except (OSError, FileNotFoundError) as error: + logger.error(error) + return [] + finally: + if os.path.exists(temp_path): + shutil.rmtree(temp_path)