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, softwarecheck{ } 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 */ 15 16import path from 'path'; 17import fs from 'fs'; 18import { FilesMap, Parser } from '../../parser/parser'; 19import { DiffHelper } from '../../diff/diff'; 20import { FileUtils } from '../../../utils/FileUtils'; 21import { BasicDiffInfo } from '../../../typedef/diff/ApiInfoDiff'; 22import { AddErrorLogs } from './compile_info'; 23import { compositiveResult, compositiveLocalResult, CommonFunctions } from '../../../utils/checkUtils'; 24import { 25 ErrorType, 26 ErrorID, 27 LogType, 28 ErrorLevel, 29 ErrorMessage, 30 incompatibleApiDiffTypes, 31 ApiCheckInfo, 32 ErrorBaseInfo, 33} from '../../../typedef/checker/result_type'; 34import { ApiDiffType } from '../../../typedef/diff/ApiInfoDiff'; 35import { ApiInfo } from '../../../typedef/parser/ApiInfoDefination'; 36 37export class ApiChangeCheck { 38 static checkApiChange(prId: string): void { 39 let rootDir: string = ''; 40 const onlineDir: string = path.resolve( 41 FileUtils.getBaseDirName(), 42 `../../../../Archive/patch_info/openharmony_interface_sdk-js_${prId}` 43 ); 44 const localDir: string = path.resolve(FileUtils.getBaseDirName(), prId); 45 46 if (fs.existsSync(onlineDir)) { 47 rootDir = onlineDir; 48 } else if (fs.existsSync(localDir)) { 49 rootDir = localDir; 50 } 51 const oldFileDir: string = path.resolve(rootDir, './old'); 52 const newFileDir: string = path.resolve(rootDir, './new'); 53 if (!fs.existsSync(oldFileDir) || !fs.existsSync(newFileDir)) { 54 return; 55 } 56 const status: fs.Stats = fs.statSync(oldFileDir); 57 let diffInfos: BasicDiffInfo[] = []; 58 if (status.isDirectory()) { 59 const oldSDKApiMap: FilesMap = Parser.parseDir(oldFileDir); 60 const newSDKApiMap: FilesMap = Parser.parseDir(newFileDir); 61 diffInfos = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, false, true); 62 } else { 63 const oldSDKApiMap: FilesMap = Parser.parseFile(path.resolve(oldFileDir, '..'), oldFileDir); 64 const newSDKApiMap: FilesMap = Parser.parseFile(path.resolve(newFileDir, '..'), newFileDir); 65 diffInfos = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, false, true); 66 } 67 diffInfos.forEach((diffInfo: BasicDiffInfo) => { 68 if (diffInfo.getIsCompatible() !== false) { 69 return; 70 } 71 const errorInfo: ErrorMessage | undefined = incompatibleApiDiffTypes.get(diffInfo.getDiffType()); 72 if (diffInfo.getDiffType() === ApiDiffType.REDUCE) { 73 const dtsName = path.basename(diffInfo.getOldDtsName()); 74 let apiInfoDiff: ApiCheckInfo = new ApiCheckInfo(); 75 const hierarchicalRelations: string[] = diffInfo.getOldHierarchicalRelations(); 76 const parentModuleName: string = hierarchicalRelations[hierarchicalRelations.length - 1]; 77 apiInfoDiff 78 .setErrorID(ErrorID.API_CHANGE_ERRORS_ID) 79 .setErrorLevel(ErrorLevel.MIDDLE) 80 .setFilePath(dtsName) 81 .setApiPostion(diffInfo.getOldPos()) 82 .setErrorType(ErrorType.API_CHANGE_ERRORS) 83 .setLogType(LogType.LOG_JSDOC) 84 .setSinceNumber(-1) 85 .setApiName(diffInfo.getOldApiName()) 86 .setApiType(diffInfo.getApiType()) 87 .setApiText(diffInfo.getOldApiDefinedText()) 88 .setErrorInfo(errorInfo as string) 89 .setHierarchicalRelations(diffInfo.getOldHierarchicalRelations().join('|')) 90 .setParentModuleName(parentModuleName); 91 92 AddErrorLogs.addAPICheckErrorLogs(apiInfoDiff, compositiveResult, compositiveLocalResult); 93 } else { 94 const dtsName = path.basename(diffInfo.getNewDtsName()); 95 let apiInfoDiff: ApiCheckInfo = new ApiCheckInfo(); 96 const hierarchicalRelations: string[] = diffInfo.getOldHierarchicalRelations(); 97 const parentModuleName: string = hierarchicalRelations[hierarchicalRelations.length - 1]; 98 apiInfoDiff 99 .setErrorID(ErrorID.API_CHANGE_ERRORS_ID) 100 .setErrorLevel(ErrorLevel.MIDDLE) 101 .setFilePath(dtsName) 102 .setApiPostion(diffInfo.getOldPos()) 103 .setErrorType(ErrorType.API_CHANGE_ERRORS) 104 .setLogType(LogType.LOG_JSDOC) 105 .setSinceNumber(-1) 106 .setApiName(diffInfo.getNewApiName()) 107 .setApiType(diffInfo.getApiType()) 108 .setApiText(diffInfo.getNewApiDefinedText()) 109 .setErrorInfo(errorInfo as string) 110 .setHierarchicalRelations(diffInfo.getOldHierarchicalRelations().join('|')) 111 .setParentModuleName(parentModuleName); 112 AddErrorLogs.addAPICheckErrorLogs(apiInfoDiff, compositiveResult, compositiveLocalResult); 113 } 114 }); 115 } 116} 117