From 0072a75c2c5e97b1728b96eb98fa6d3645b1abd0 Mon Sep 17 00:00:00 2001 From: v-tianxi Date: Fri, 20 Dec 2024 14:45:11 +0800 Subject: [PATCH] add function description and fix comment --- eng/tools/sdk-suppressions/src/index.ts | 2 +- .../sdk-suppressions/src/sdkSuppressions.ts | 8 ++-- .../src/updateSdkSuppressionsLabel.ts | 39 ++++++++++++++----- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/eng/tools/sdk-suppressions/src/index.ts b/eng/tools/sdk-suppressions/src/index.ts index a9318a61608a..5100ec2104d8 100644 --- a/eng/tools/sdk-suppressions/src/index.ts +++ b/eng/tools/sdk-suppressions/src/index.ts @@ -24,7 +24,7 @@ export async function main() { const changeFiles: string = args[2]; const lables: string = args[3]; const outputFile = process.env.OUTPUT_FILE as string; - const changedLabels: {labelsToAdd: String[], labelsToRemove: String[]} = await updateSdkSuppressionsLabels(lables, changeFiles, outputFile, baseCommitHash, headCommitHash); + const changedLabels: {labelsToAdd: String[], labelsToRemove: String[]} = await updateSdkSuppressionsLabels(lables, changeFiles, baseCommitHash, headCommitHash, outputFile); console.log(JSON.stringify(changedLabels)); exit(0); } else { diff --git a/eng/tools/sdk-suppressions/src/sdkSuppressions.ts b/eng/tools/sdk-suppressions/src/sdkSuppressions.ts index fba2341bebd9..06b7ceb3607a 100644 --- a/eng/tools/sdk-suppressions/src/sdkSuppressions.ts +++ b/eng/tools/sdk-suppressions/src/sdkSuppressions.ts @@ -22,7 +22,7 @@ export type SdkPackageSuppressionsEntry = { "breaking-changes": string[]; }; -function errorAndExist(error: string): never { +function exitWithError(error: string): never { console.error("Error:", error); process.exit(1); } @@ -34,11 +34,11 @@ export function validateSdkSuppressionsFile( message: string; } { if (suppressionContent === null) { - errorAndExist("This suppression file is a empty file"); + exitWithError("This suppression file is a empty file"); } if (!suppressionContent) { - errorAndExist("This suppression file is not a valid yaml. Refer to https://aka.ms/azsdk/sdk-suppression for more information."); + exitWithError("This suppression file is not a valid yaml. Refer to https://aka.ms/azsdk/sdk-suppression for more information."); } const suppressionFileSchema = { @@ -80,6 +80,6 @@ export function validateSdkSuppressionsFile( message: "This suppression file is a valid yaml.", }; } else { - errorAndExist("This suppression file is a valid yaml but the schema is wrong: " + suppressionAjv.errorsText(suppressionAjvCompile.errors, { separator: "\n" })); + exitWithError("This suppression file is a valid yaml but the schema is wrong: " + suppressionAjv.errorsText(suppressionAjvCompile.errors, { separator: "\n" })); } } diff --git a/eng/tools/sdk-suppressions/src/updateSdkSuppressionsLabel.ts b/eng/tools/sdk-suppressions/src/updateSdkSuppressionsLabel.ts index dcf066d38e2d..da9997d6a69f 100644 --- a/eng/tools/sdk-suppressions/src/updateSdkSuppressionsLabel.ts +++ b/eng/tools/sdk-suppressions/src/updateSdkSuppressionsLabel.ts @@ -12,7 +12,9 @@ import { parseYamlContent, runGitCommand } from "./common.js"; /** * - * @param pr + * @param prChangeFiles + * @param baseCommitHash + * @param headCommitHash * @returns SdkName list * This part compares the suppression files of the head branch and the base branch. * To get the SDK, we need to identify which package name, SDK name, or breaking changes are different and apply the SDK suppression label accordingly in the next step. @@ -72,7 +74,7 @@ export async function getSdkSuppressionsFileContent( console.log(`Found content in ${ref}#${path}`); return parseYamlContent(suppressionFileContent, path).result; } catch (error) { - console.log(`Not found content in ${ref}#${path}`); + console.log(`Not found content in ${ref}#${path}, Error: ${error}`); return null; } } @@ -81,6 +83,15 @@ function getSdksWithSuppressionsDefined(suppressions: SdkSuppressionsSection): S return _.keys(suppressions) as SdkName[]; } +/** + * + * @param headSuppressionFile + * @param baseSuppressionFile + * @returns SdkName[] + * + * Analyze the suppression files across three dimensions: language, package, and breaking-change. Finally, determine the outermost sdkName. + */ + export function getSdkNamesWithChangedSuppressions( headSuppressionFile: SdkSuppressionsYml, baseSuppressionFile: SdkSuppressionsYml, @@ -163,18 +174,22 @@ export function getSdkNamesWithChangedSuppressions( /** * - * @param pr - * This code segment is responsible for managing the sdk suppression label (e.g. BreakingChange-Go-Sdk-Suppression) for PRs. - * 1. It applies the label automatically when a PR includes a new suppression file. - * 2. It removes the label if the PR no longer contains the suppression file. - * 3. It leaves the label unchanged if the PR already has both the suppression and the suppression approved labels. + * @param prLabels + * @param prChangeFiles + * @param baseCommitHash + * @param headCommitHash + * @param outputFile + * @returns { labelsToAdd: String[]; labelsToRemove: String[] } + * This code performs two key functions: + * First, it retrieves the corresponding SDKNames based on the differences between the two sdk-suppression files. + * Second, it compares the SDKNames obtained in the previous step with the existing PR labels and processes the PR labels accordingly. */ export async function updateSdkSuppressionsLabels( prLabels: string, prChangeFiles: string, - outputFile: string, baseCommitHash: string, headCommitHash: string, + outputFile?: string, ): Promise<{ labelsToAdd: String[]; labelsToRemove: String[] }> { try { const status = await runGitCommand("git status"); @@ -194,8 +209,10 @@ export async function updateSdkSuppressionsLabels( const result = processLabels(presentLabels, sdkNames); - writeFileSync(outputFile, JSON.stringify(result)); - console.log(`😊 JSON output saved to ${outputFile}`); + if(outputFile){ + writeFileSync(outputFile, JSON.stringify(result)); + console.log(`😊 JSON output saved to ${outputFile}`); + } return result; } @@ -206,6 +223,8 @@ export async function updateSdkSuppressionsLabels( * @param sdkNames * @returns {labelsToAdd: String[], labelsToRemove: String[]} * + * Based on the various sdknames and existing labels, process the suppression label of PR. + * * Add logic: If the breakingChangeSuppression label corresponding to an SDK in sdkNames is not in the current presentLabels list, * add the label to labelsToAdd. * Remove logic: If a label is in presentLabels and the corresponding breakingChangeSuppression is not in sdkNames