Skip to content

Commit

Permalink
add function description and fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
JackTn committed Dec 20, 2024
1 parent be7b892 commit 0072a75
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion eng/tools/sdk-suppressions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions eng/tools/sdk-suppressions/src/sdkSuppressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 = {
Expand Down Expand Up @@ -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" }));
}
}
39 changes: 29 additions & 10 deletions eng/tools/sdk-suppressions/src/updateSdkSuppressionsLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit 0072a75

Please sign in to comment.