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, hierarchicalRelationsSet } 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 process.env.IS_INCREMENT_CHECK = 'true'; 48 rootDir = onlineDir; 49 } else if (fs.existsSync(localDir)) { 50 process.env.IS_INCREMENT_CHECK = 'true'; 51 rootDir = localDir; 52 } else { 53 process.env.IS_INCREMENT_CHECK = undefined; 54 } 55 const oldFileDir: string = path.resolve(rootDir, './old'); 56 const newFileDir: string = path.resolve(rootDir, './new'); 57 if (!fs.existsSync(oldFileDir) || !fs.existsSync(newFileDir)) { 58 return; 59 } 60 const status: fs.Stats = fs.statSync(oldFileDir); 61 let diffInfos: BasicDiffInfo[] = []; 62 if (status.isDirectory()) { 63 const oldSDKApiMap: FilesMap = Parser.parseDir(oldFileDir); 64 const newSDKApiMap: FilesMap = Parser.parseDir(newFileDir); 65 diffInfos = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, false, true); 66 } else { 67 const oldSDKApiMap: FilesMap = Parser.parseFile(path.resolve(oldFileDir, '..'), oldFileDir); 68 const newSDKApiMap: FilesMap = Parser.parseFile(path.resolve(newFileDir, '..'), newFileDir); 69 diffInfos = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, false, true); 70 } 71 diffInfos.forEach((diffInfo: BasicDiffInfo) => { 72 hierarchicalRelationsSet.add(diffInfo.oldHierarchicalRelations.join('|')) 73 .add(diffInfo.newHierarchicalRelations.join('|')); 74 if (diffInfo.getIsCompatible() !== false) { 75 return; 76 } 77 const errorInfo: ErrorMessage | undefined = incompatibleApiDiffTypes.get(diffInfo.getDiffType()); 78 if (diffInfo.getDiffType() === ApiDiffType.REDUCE) { 79 const dtsName = path.basename(diffInfo.getOldDtsName()); 80 let apiInfoDiff: ApiCheckInfo = new ApiCheckInfo(); 81 const hierarchicalRelations: string[] = diffInfo.getOldHierarchicalRelations(); 82 const parentModuleName: string = hierarchicalRelations[hierarchicalRelations.length - 1]; 83 apiInfoDiff 84 .setErrorID(ErrorID.API_CHANGE_ERRORS_ID) 85 .setErrorLevel(ErrorLevel.MIDDLE) 86 .setFilePath(dtsName) 87 .setApiPostion(diffInfo.getOldPos()) 88 .setErrorType(ErrorType.API_CHANGE_ERRORS) 89 .setLogType(LogType.LOG_JSDOC) 90 .setSinceNumber(-1) 91 .setApiName(diffInfo.getOldApiName()) 92 .setApiType(diffInfo.getApiType()) 93 .setApiText(diffInfo.getOldApiDefinedText()) 94 .setErrorInfo(errorInfo as string) 95 .setHierarchicalRelations(diffInfo.getOldHierarchicalRelations().join('|')) 96 .setParentModuleName(parentModuleName); 97 98 AddErrorLogs.addAPICheckErrorLogs(apiInfoDiff, compositiveResult, compositiveLocalResult); 99 } else { 100 const dtsName = path.basename(diffInfo.getNewDtsName()); 101 let apiInfoDiff: ApiCheckInfo = new ApiCheckInfo(); 102 const hierarchicalRelations: string[] = diffInfo.getNewHierarchicalRelations(); 103 const parentModuleName: string = hierarchicalRelations[hierarchicalRelations.length - 1]; 104 apiInfoDiff 105 .setErrorID(ErrorID.API_CHANGE_ERRORS_ID) 106 .setErrorLevel(ErrorLevel.MIDDLE) 107 .setFilePath(dtsName) 108 .setApiPostion(diffInfo.getOldPos()) 109 .setErrorType(ErrorType.API_CHANGE_ERRORS) 110 .setLogType(LogType.LOG_JSDOC) 111 .setSinceNumber(-1) 112 .setApiName(diffInfo.getNewApiName()) 113 .setApiType(diffInfo.getApiType()) 114 .setApiText(diffInfo.getNewApiDefinedText()) 115 .setErrorInfo(errorInfo as string) 116 .setHierarchicalRelations(diffInfo.getNewHierarchicalRelations().join('|')) 117 .setParentModuleName(parentModuleName); 118 AddErrorLogs.addAPICheckErrorLogs(apiInfoDiff, compositiveResult, compositiveLocalResult); 119 } 120 }); 121 } 122} 123