1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { ApiResultSimpleInfo, ApiResultMessage, ApiBaseInfo } from '../../typedef/checker/result_type'; 16import { Check } from './src/api_check_plugin'; 17import { LogUtil } from '../../utils/logUtil'; 18import { GenerateFile } from '../../utils/checkUtils'; 19import { compositiveResult, compositiveLocalResult, apiCheckResult } from '../../utils/checkUtils'; 20import { DOC, DEFINE, CHANEGE } from './config/api_check_config.json'; 21import { ApiChangeCheck } from './src/check_api_diff'; 22 23/** 24 * local entrance 25 */ 26export class LocalEntry { 27 28 static checkEntryLocal(filePathArr: string[], fileRuleArr: string[], output: string, prId: string, excel: string): ApiResultMessage[] { 29 let allResult: ApiResultMessage[] = apiCheckResult; 30 try { 31 Check.scanEntry(filePathArr, prId); 32 LocalEntry.maskAlarm(compositiveResult, fileRuleArr); 33 } catch (error) { 34 LogUtil.e('API_CHECK_ERROR', error); 35 } finally { 36 GenerateFile.writeFile(apiCheckResult, output, {}); 37 38 if (excel === 'true') { 39 GenerateFile.writeExcelFile(apiCheckResult); 40 } 41 } 42 return allResult; 43 } 44 45 static maskAlarm(allResultInfo: ApiResultSimpleInfo[], fileRuleArr: string[]): void { 46 const localScan: boolean = (fileRuleArr.length === 1 && fileRuleArr[0] === 'all') ? true : false; 47 const apiCheckInfos: Map<string, string> = new Map(Object.entries({ ...DOC, ...DEFINE, ...CHANEGE })); 48 let apiCheckAdmissiveSet: Set<string> = new Set(); 49 if (localScan) { 50 apiCheckAdmissiveSet = new Set([...apiCheckInfos.values()]); 51 } else { 52 fileRuleArr.forEach((apiCheckItem: string) => { 53 const apiCheckItemMessage: string | undefined = apiCheckInfos.get(apiCheckItem); 54 if (apiCheckItemMessage) { 55 apiCheckAdmissiveSet.add(apiCheckItemMessage); 56 } 57 }); 58 } 59 let allResultInfoSet: Set<ApiResultSimpleInfo> = new Set(allResultInfo); 60 const maskResult: ApiResultSimpleInfo[] = LocalEntry.filterAllResultInfo(allResultInfo, 61 apiCheckInfos, apiCheckAdmissiveSet); 62 maskResult.forEach(resultItem => { 63 const apiBaseInfos: ApiBaseInfo = new ApiBaseInfo(); 64 apiBaseInfos 65 .setApiName(resultItem.apiName) 66 .setApiType(resultItem.apiType) 67 .setHierarchicalRelations(resultItem.hierarchicalRelations) 68 .setParentModuleName(resultItem.parentModuleName); 69 70 const apiChecktErrorLog: ApiResultMessage = new ApiResultMessage(); 71 apiChecktErrorLog 72 .setFilePath(resultItem.filePath) 73 .setLocation(resultItem.location) 74 .setLevel(resultItem.level) 75 .setType(resultItem.type) 76 .setMessage(resultItem.message) 77 .setMainBuggyCode(resultItem.apiText) 78 .setMainBuggyLine(resultItem.location) 79 .setExtendInfo(apiBaseInfos); 80 apiCheckResult.push(apiChecktErrorLog); 81 }); 82 } 83 84 static filterAllResultInfo(allResultInfo: ApiResultSimpleInfo[], apiCheckInfos: Map<string, string>, 85 apiCheckAdmissiveSet: Set<string>): ApiResultSimpleInfo[] { 86 return allResultInfo.filter((resultItem: ApiResultSimpleInfo) => { 87 let resultItemInfo: string = resultItem.message.replace(/API check error of \[.*\]: /g, ''); 88 const regex1 = /Prohibited word in \[.*\]:{option}.The word allowed is \[.*\]\./g; 89 const regex2 = /Prohibited word in \[.*\]:{ability} in the \[.*\] file\./g; 90 const regex3 = /please confirm whether it needs to be corrected to a common word./g; 91 const regex4 = /tag does not exist. Please use a valid JSDoc tag./g; 92 const regex5 = /The event name should be named by small hump./g; 93 if (/\d/g.test(resultItemInfo)) { 94 resultItemInfo = resultItemInfo.replace(/\d+/g, '1'); 95 } 96 if (regex1.test(resultItemInfo)) { 97 resultItemInfo = JSON.stringify(apiCheckInfos.get('API_DEFINE_NAME_01')).replace(/\"/g, ''); 98 } 99 if (regex2.test(resultItemInfo)) { 100 resultItemInfo = JSON.stringify(apiCheckInfos.get('API_DEFINE_NAME_02')).replace(/\"/g, ''); 101 } 102 if (regex3.test(resultItemInfo)) { 103 resultItemInfo = resultItemInfo.replace(/\{.*\}/g, '{XXXX}'); 104 } 105 if (regex4.test(resultItemInfo)) { 106 resultItemInfo = resultItemInfo.replace(/\[.*\]/g, '[XXXX]'); 107 } 108 if (regex5.test(resultItemInfo)) { 109 resultItemInfo = resultItemInfo.replace(/\[.*\]/g, '[XXXX]'); 110 } 111 if (/This name \[.*\] should be named by/g.test(resultItemInfo)) { 112 resultItemInfo = resultItemInfo.replace(/\[.*\]/g, '[XXXX]'); 113 } 114 if (apiCheckAdmissiveSet.has(resultItemInfo)) { 115 const key: string = LocalEntry.filterApiCheckInfos(apiCheckInfos, resultItemInfo); 116 if (key !== '') { 117 resultItem.setType(key); 118 } 119 } 120 return apiCheckAdmissiveSet.has(resultItemInfo); 121 }); 122 } 123 124 static filterApiCheckInfos(apiCheckInfos: Map<string, string>, resultItemInfo: string): string { 125 for (let [key, value] of apiCheckInfos.entries()) { 126 if (value === resultItemInfo) { 127 return key; 128 } 129 } 130 return ''; 131 } 132 133 static apiChangeCheckEntryLocal(prId: string, fileRuleArr: string[], output: string, excel: string): ApiResultMessage[] { 134 let apiChangeCheckResult: ApiResultMessage[] = apiCheckResult; 135 try { 136 ApiChangeCheck.checkApiChange(prId); 137 LocalEntry.maskAlarm(compositiveResult, fileRuleArr); 138 } catch (error) { 139 LogUtil.e('API_CHECK_ERROR', error); 140 } finally { 141 GenerateFile.writeFile(apiCheckResult, output, {}); 142 if (excel === 'true') { 143 GenerateFile.writeExcelFile(apiCheckResult); 144 } 145 } 146 return apiChangeCheckResult; 147 } 148} 149