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 } from '../../../utils/checkUtils'; 24import { 25 ErrorType, 26 ErrorID, 27 LogType, 28 ErrorLevel, 29 ErrorMessage, 30 incompatibleApiDiffTypes, 31} from '../../../typedef/checker/result_type'; 32import { ApiDiffType } from '../../../typedef/diff/ApiInfoDiff'; 33 34export class ApiChangeCheck { 35 static checkApiChange(prId?: string): void { 36 const rootDir = path.resolve( 37 FileUtils.getBaseDirName(), 38 `../../../../../Archive/patch_info/openharmony_interface_sdk-js_${prId}` 39 ); 40 if (!fs.existsSync(rootDir)) { 41 return; 42 } 43 const oldFileDir: string = path.resolve(rootDir, './old'); 44 const newFileDir: string = path.resolve(rootDir, './new'); 45 46 const status: fs.Stats = fs.statSync(oldFileDir); 47 let diffInfos: BasicDiffInfo[] = []; 48 if (status.isDirectory()) { 49 const oldSDKApiMap: FilesMap = Parser.parseDir(oldFileDir); 50 const newSDKApiMap: FilesMap = Parser.parseDir(newFileDir); 51 diffInfos = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, true); 52 } else { 53 const oldSDKApiMap: FilesMap = Parser.parseFile(path.resolve(oldFileDir, '..'), oldFileDir); 54 const newSDKApiMap: FilesMap = Parser.parseFile(path.resolve(newFileDir, '..'), newFileDir); 55 diffInfos = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, true); 56 } 57 58 diffInfos.forEach((diffInfo: BasicDiffInfo) => { 59 if (diffInfo.getIsCompatible() !== false) { 60 return; 61 } 62 const errorInfo: ErrorMessage | undefined = incompatibleApiDiffTypes.get(diffInfo.getDiffType()); 63 if (diffInfo.getDiffType() === ApiDiffType.REDUCE) { 64 const dtsName = path.basename(diffInfo.getOldDtsName()); 65 AddErrorLogs.addAPICheckErrorLogs( 66 ErrorID.API_CHANGE_ERRORS_ID, 67 ErrorLevel.MIDDLE, 68 dtsName, 69 diffInfo.getOldPos(), 70 ErrorType.API_CHANGE_ERRORS, 71 LogType.LOG_API, 72 -1, 73 diffInfo.getOldApiName(), 74 diffInfo.getOldApiDefinedText(), 75 errorInfo as string, 76 compositiveResult, 77 compositiveLocalResult 78 ); 79 } else { 80 const dtsName = path.basename(diffInfo.getNewDtsName()); 81 AddErrorLogs.addAPICheckErrorLogs( 82 ErrorID.API_CHANGE_ERRORS_ID, 83 ErrorLevel.MIDDLE, 84 dtsName, 85 diffInfo.getOldPos(), 86 ErrorType.API_CHANGE_ERRORS, 87 LogType.LOG_API, 88 -1, 89 diffInfo.getNewApiName(), 90 diffInfo.getNewApiDefinedText(), 91 errorInfo as string, 92 compositiveResult, 93 compositiveLocalResult 94 ); 95 } 96 }); 97 } 98} 99