Skip to content

Commit

Permalink
[ARMIncrementalTsp] Reduce checkout time (#32695)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeharder authored Feb 19, 2025
1 parent eda4ed6 commit af7e664
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 193 deletions.
125 changes: 0 additions & 125 deletions .github/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"@types/github-script": "github:actions/github-script",
"@octokit/webhooks-types": "^7.5.1",
"@vitest/coverage-v8": "^3.0.5",
"memfs": "^4.17.0",
"prettier": "^3.3.3",
"vitest": "^3.0.5"
},
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/arm-incremental-typespec-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
with:
# Required to detect changed files in PR
fetch-depth: 2
# Check only needs to view contents of changed files as a string, so can use
# "git show" on specific files instead of cloning whole repo
sparse-checkout: |
.github
# Output is "true" if PR contains only incremental changes to an existing TypeSpec RP
- id: incremental-typespec
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/src/arm-incremental-typespec-preview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-check

import { readFile } from "fs/promises";
import { dirname, join } from "path";
import { dirname } from "path";
import { getChangedResourceManagerSwaggerFiles } from "./changed-files.js";
import { lsTree, show } from "./git.js";

Expand All @@ -26,10 +25,7 @@ export default async function incrementalTypeSpec({ github, context, core }) {

// If any changed file is not typespec-generated, return false
for (const file of changedRmSwaggerFiles) {
const swagger = await readFile(
join(process.env.GITHUB_WORKSPACE || "", file),
{ encoding: "utf8" },
);
const swagger = await show("HEAD", file, core);

const swaggerObj = JSON.parse(swagger);

Expand Down
49 changes: 18 additions & 31 deletions .github/workflows/src/changed-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ export async function getChangedFiles(
headCommitish = "HEAD",
diffFilter = "d",
) {
return await getChangedFilesImpl(
`Get-ChangedFiles ${baseCommitish} ${headCommitish} ${diffFilter}"`,
const result = await execRoot(
`git -c core.quotepath=off diff --name-only --diff-filter=${diffFilter} ${baseCommitish} ${headCommitish}`,
core,
);

const files = result.trim().split("\n");

core.info("Changed Files:");
for (const file of files) {
core.info(` ${file}`);
}
core.info("");

return files;
}

/**
Expand All @@ -34,10 +44,9 @@ export async function getChangedSwaggerFiles(
headCommitish = "HEAD",
diffFilter = "d",
) {
return await getChangedFilesImpl(
`Get-ChangedSwaggerFiles(Get-ChangedFiles ${baseCommitish} ${headCommitish} ${diffFilter})`,
core,
);
return (
await getChangedFiles(core, baseCommitish, headCommitish, diffFilter)
).filter((f) => f.endsWith(".json"));
}

/**
Expand All @@ -55,9 +64,9 @@ export async function getChangedResourceManagerSwaggerFiles(
) {
const changedSwaggerFiles = await getChangedSwaggerFiles(
core,
"HEAD^",
"HEAD",
"",
baseCommitish,
headCommitish,
diffFilter,
);
const changedResourceManagerSwaggerFiles = changedSwaggerFiles.filter(
(f) => f.includes("/resource-manager/") && !f.includes("/examples/"),
Expand All @@ -67,25 +76,3 @@ export async function getChangedResourceManagerSwaggerFiles(
);
return changedResourceManagerSwaggerFiles;
}

/**
* @param {string} command
* @param {import('github-script').AsyncFunctionArguments['core']} core
* @returns {Promise<string[]>}
*/
async function getChangedFilesImpl(command, core) {
const result = await execRoot(
`pwsh -command ". ./eng/scripts/ChangedFiles-Functions.ps1; ${command}"`,
core,
);

const files = result.trim().split("\n");

core.info("Changed Files:");
for (const file of files) {
core.info(` ${file}`);
}
core.info("");

return files;
}
34 changes: 4 additions & 30 deletions .github/workflows/test/arm-incremental-typespec-preview.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { vol } from "memfs";
import { join } from "path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { createMockCore } from "../../test/mocks.js";
import incrementalTypeSpec from "../src/arm-incremental-typespec-preview.js";
import * as changedFiles from "../src/changed-files.js";
import * as git from "../src/git.js";

vi.mock("fs/promises", async () => {
const memfs = await import("memfs");
return {
...memfs.fs.promises,
};
});

const core = createMockCore();

const swaggerTypeSpecGenerated = JSON.stringify({
Expand All @@ -22,11 +13,6 @@ const swaggerTypeSpecGenerated = JSON.stringify({
});

describe("incrementalTypeSpec", () => {
beforeEach(() => {
// TODO: Reset other global mocks like "core"
vol.reset();
});

it("rejects if inputs null", async () => {
await expect(incrementalTypeSpec({})).rejects.toThrow();
});
Expand All @@ -49,9 +35,7 @@ describe("incrementalTypeSpec", () => {
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vol.fromJSON({
[join(process.env.GITHUB_WORKSPACE || "", swaggerPath)]: '"foo"',
});
vi.spyOn(git, "show").mockResolvedValue('"foo"');

await expect(incrementalTypeSpec({ core })).resolves.toBe(false);
});
Expand All @@ -65,10 +49,7 @@ describe("incrementalTypeSpec", () => {
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vol.fromJSON({
[join(process.env.GITHUB_WORKSPACE || "", swaggerPath)]:
swaggerTypeSpecGenerated,
});
vi.spyOn(git, "show").mockResolvedValue(swaggerTypeSpecGenerated);

// "git ls-tree" returns "" if the spec folder doesn't exist in the base branch
vi.spyOn(git, "lsTree").mockResolvedValue("");
Expand All @@ -85,10 +66,7 @@ describe("incrementalTypeSpec", () => {
"getChangedResourceManagerSwaggerFiles",
).mockResolvedValue([swaggerPath]);

vol.fromJSON({
[join(process.env.GITHUB_WORKSPACE || "", swaggerPath)]:
swaggerTypeSpecGenerated,
});
vi.spyOn(git, "show").mockResolvedValue(swaggerTypeSpecGenerated);

vi.spyOn(git, "lsTree").mockImplementation(
async (_treeIsh, _path, _core, options) => {
Expand All @@ -98,10 +76,6 @@ describe("incrementalTypeSpec", () => {
},
);

vi.spyOn(git, "show").mockImplementation(async (_treeIsh, _path, _core) => {
return swaggerTypeSpecGenerated;
});

await expect(incrementalTypeSpec({ core })).resolves.toBe(true);
});
});

0 comments on commit af7e664

Please sign in to comment.