Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions eng/tools/openapi-diff-runner/src/command-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BREAKING_CHANGES_CHECK_TYPES } from "@azure-tools/specs-shared/breaking-change";
import { getChangedFilesStatuses, swagger } from "@azure-tools/specs-shared/changed-files";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
Expand Down Expand Up @@ -131,6 +131,7 @@
additions: string[];
modifications: string[];
deletions: string[];
renames: { from: string; to: string }[];
total: number;
}> {
try {
Expand All @@ -143,22 +144,48 @@
});

// Filter each array to only include Swagger files using the swagger filter from changed-files.js
const filteredAdditions = result.additions.filter(swagger);
let filteredAdditions = result.additions.filter(swagger);
const filteredModifications = result.modifications.filter(swagger);
const filteredDeletions = result.deletions.filter(swagger);
const filteredRenames = result.renames.filter(
let filteredDeletions = result.deletions.filter(swagger);
let filteredRenames = result.renames.filter(
(rename) => swagger(rename.from) && swagger(rename.to),
);

// Add renamed files to the additions array and deletions array
filteredAdditions.push(...filteredRenames.map((rename) => rename.to));
filteredDeletions.push(...filteredRenames.map((rename) => rename.from));
// Store matches in temp array, to avoid modifying "filteredDeletions" while iterating
const matchedRenames: { from: string; to: string }[] = [];

// Try to manually match deletions with additions to form renames
for (const deletion of filteredDeletions) {
const deletionDir = path.dirname(deletion).toLowerCase();

const additionMatches = filteredAdditions.filter(
(a) => path.dirname(a).toLowerCase() === deletionDir,
);

// If there are multiple matching additions (which should be rare), it's safest
// to add all the combinations as "renames". This will probably always trigger a check failure,
// but it's safest to "fail closed" this way, rather than ignoring the files.
for (const addition of additionMatches) {
matchedRenames.push({ from: deletion, to: addition });
}
}

const renamesFrom = matchedRenames.map((r) => r.from);
const renamesTo = matchedRenames.map((r) => r.to);
filteredAdditions = filteredAdditions.filter((a) => !renamesTo.includes(a));
filteredDeletions = filteredDeletions.filter((d) => !renamesFrom.includes(d));
filteredRenames = filteredRenames.concat(matchedRenames);

return {
additions: filteredAdditions,
modifications: filteredModifications,
deletions: filteredDeletions,
total: filteredAdditions.length + filteredModifications.length + filteredDeletions.length,
renames: filteredRenames,
total:
filteredAdditions.length +
filteredModifications.length +
filteredDeletions.length +
filteredRenames.length,
};
} catch (error) {
logError(`Error getting categorized changed files: ${error}`);
Expand All @@ -167,6 +194,7 @@
additions: [],
modifications: [],
deletions: [],
renames: [],
total: 0,
};
}
Expand Down
3 changes: 3 additions & 0 deletions eng/tools/openapi-diff-runner/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/**
* In the "breakingChanges directory invocation depth" this file has depth 1,
* i.e. it is invoked by files with depth 0 and invokes files with depth 2.
Expand Down Expand Up @@ -75,6 +75,8 @@

const deletedSwaggers = diffs.deletions || [];

const renamedSwaggers = diffs.renames || [];

const newExistingVersionDirs: string[] = [];

const addedVersionDirs = [...newSwaggers.map((f: string) => path.dirname(f))];
Expand Down Expand Up @@ -156,6 +158,7 @@
needCompareOldSwaggers,
newVersionSwaggers,
newVersionChangedSwaggers,
renamedSwaggers,
oadTracer,
);

Expand Down
17 changes: 17 additions & 0 deletions eng/tools/openapi-diff-runner/src/detect-breaking-change.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/**
* By design, the members exported from this file are functional breaking change detection utilities.
*
Expand Down Expand Up @@ -61,6 +61,7 @@
existingVersionSwaggers: string[]; // Files in existing API version directories
newVersionSwaggers: string[]; // Files in completely new API version directories
newVersionChangedSwaggers: string[]; // Files in existing API version directories that have changed
renamedSwaggers: { from: string; to: string }[];
oadTracer: OadTraceData;
msgs: ResultMessageRecord[];
runtimeErrors: RawMessageRecord[];
Expand All @@ -75,13 +76,15 @@
existingVersionSwaggers: string[],
newVersionSwaggers: string[],
newVersionChangedSwaggers: string[],
renamedSwaggers: { from: string; to: string }[],
oadTracer: OadTraceData,
): BreakingChangeDetectionContext {
return {
context,
existingVersionSwaggers,
newVersionSwaggers,
newVersionChangedSwaggers,
renamedSwaggers,
oadTracer,
msgs: [],
runtimeErrors: [],
Expand Down Expand Up @@ -132,6 +135,20 @@
logMessage("Processing completed", LogLevel.EndGroup);
}

for (const { from, to } of detectionContext.renamedSwaggers) {
logMessage(`Processing rename: ${from} -> ${to}`, LogLevel.Group);
const { oadViolationsCnt, errorCnt } = await doBreakingChangeDetection(
detectionContext,
path.resolve(detectionContext.context.prInfo!.tempRepoFolder, from),
to,
BREAKING_CHANGES_CHECK_TYPES.SAME_VERSION,
specIsPreview(to) ? ApiVersionLifecycleStage.PREVIEW : ApiVersionLifecycleStage.STABLE,
);
aggregateOadViolationsCnt += oadViolationsCnt;
aggregateErrorCnt += errorCnt;
logMessage("Processing completed", LogLevel.EndGroup);
}

logMessage(
`RETURN definition checkBreakingChangeOnSameVersion. ` +
`msgs.length: ${detectionContext.msgs.length}, ` +
Expand Down
48 changes: 25 additions & 23 deletions eng/tools/openapi-diff-runner/test/command-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BREAKING_CHANGES_CHECK_TYPES } from "@azure-tools/specs-shared/breaking-change";
import { getChangedFilesStatuses } from "@azure-tools/specs-shared/changed-files";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
Expand Down Expand Up @@ -347,16 +347,16 @@
headCommitish: TEST_CONSTANTS.COMMITS.HEAD,
});

// Expected result should have renames added to additions and deletions, and no renames property
const expectedResult = {
additions: [...mockResult.additions, ...mockResult.renames.map((rename) => rename.to)],
additions: mockResult.additions,
modifications: mockResult.modifications,
deletions: [...mockResult.deletions, ...mockResult.renames.map((rename) => rename.from)],
deletions: mockResult.deletions,
renames: mockResult.renames,
total:
mockResult.additions.length +
mockResult.modifications.length +
mockResult.deletions.length +
mockResult.renames.length * 2,
mockResult.renames.length,
};

expect(result).toEqual(expectedResult);
Expand All @@ -375,6 +375,7 @@
additions: [],
modifications: [],
deletions: [],
renames: [],
total: 0,
});
});
Expand Down Expand Up @@ -415,17 +416,16 @@

// Only Swagger files should be returned, with renames added to additions and deletions
expect(result).toEqual({
additions: [
TEST_CONSTANTS.SWAGGER_PATHS.FOO,
TEST_CONSTANTS.SWAGGER_PATHS.BAZ,
TEST_CONSTANTS.SWAGGER_PATHS.NEW_MGMT, // from valid rename
],
additions: [TEST_CONSTANTS.SWAGGER_PATHS.FOO, TEST_CONSTANTS.SWAGGER_PATHS.BAZ],
modifications: [TEST_CONSTANTS.SWAGGER_PATHS.QUX_MGMT],
deletions: [
TEST_CONSTANTS.SWAGGER_PATHS.OLD_DATA,
TEST_CONSTANTS.SWAGGER_PATHS.OLD_MGMT, // from valid rename
deletions: [TEST_CONSTANTS.SWAGGER_PATHS.OLD_DATA],
renames: [
{
from: TEST_CONSTANTS.SWAGGER_PATHS.OLD_MGMT,
to: TEST_CONSTANTS.SWAGGER_PATHS.NEW_MGMT,
},
],
total: 6, // 3 additions + 1 modification + 2 deletions (including rename files)
total: 5, // 3 additions + 1 modification + 2 deletions (including rename files)
});
});

Expand Down Expand Up @@ -474,18 +474,20 @@

// Renames should be added to additions and deletions, not returned as renames
expect(result).toEqual({
additions: [
TEST_CONSTANTS.SWAGGER_PATHS.FOO,
TEST_CONSTANTS.SWAGGER_PATHS.NEW_MGMT, // from rename.to
"specification/newapi/data-plane/stable/2023-01-01/newapi.json", // from rename.to
],
additions: [TEST_CONSTANTS.SWAGGER_PATHS.FOO],
modifications: [TEST_CONSTANTS.SWAGGER_PATHS.BAZ],
deletions: [
TEST_CONSTANTS.SWAGGER_PATHS.OLD_DATA,
TEST_CONSTANTS.SWAGGER_PATHS.OLD_MGMT, // from rename.from
"specification/oldapi/data-plane/stable/2023-01-01/oldapi.json", // from rename.from
deletions: [TEST_CONSTANTS.SWAGGER_PATHS.OLD_DATA],
renames: [
{
from: TEST_CONSTANTS.SWAGGER_PATHS.OLD_MGMT,
to: TEST_CONSTANTS.SWAGGER_PATHS.NEW_MGMT,
},
{
from: "specification/oldapi/data-plane/stable/2023-01-01/oldapi.json",
to: "specification/newapi/data-plane/stable/2023-01-01/newapi.json",
},
],
total: 7, // 3 additions + 1 modification + 3 deletions (including from renames)
total: 5, // 1 additions + 1 modification + 1 deletions + 2 renames
});
});
});
Expand Down
Loading
Loading