-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Analyze change ts #3415
Open
timotheeguerin
wants to merge
18
commits into
microsoft:main
Choose a base branch
from
timotheeguerin:analyze-change-ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Analyze change ts #3415
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9cc8c63
Initial
timotheeguerin 5b97e83
Convert analyze to TS
timotheeguerin 3a12323
Extra steps
timotheeguerin a59a8b7
Run
timotheeguerin 7cb87e8
Fix
timotheeguerin 13846b5
.
timotheeguerin 7013d85
Std out write
timotheeguerin 4c43771
test
timotheeguerin 653e66b
Fix
timotheeguerin 1299917
Tweaks
timotheeguerin 6ff90f0
Add tests
timotheeguerin c87669a
fix
timotheeguerin 3b7f1fc
Update eng/common/pipelines/ci.yml
timotheeguerin c795842
Merge branch 'main' of https://github.com/Microsoft/typespec into ana…
timotheeguerin 08d63ff
Merge branch 'analyze-change-ts' of https://github.com/timotheeguerin…
timotheeguerin 364a31b
Fix
timotheeguerin c6ca3f2
don't bump other deps
timotheeguerin 7527bdc
fix
timotheeguerin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AreaPaths } from "./labels.js"; | ||
|
||
/** | ||
* Path that should trigger every CI build. | ||
*/ | ||
const all = ["eng/common/", "vitest.config.ts"]; | ||
|
||
export const CIRules = { | ||
CSharp: [...AreaPaths["emitter:client:csharp"], ".editorconfig", ...all], | ||
Core: [ | ||
"**/*", | ||
"!.prettierignore", | ||
"!.prettierrc.json", | ||
"!cspell.yaml", | ||
"!esling.config.json", | ||
...ignore(AreaPaths["emitter:client:csharp"]), | ||
], | ||
}; | ||
|
||
function ignore(paths: string[]) { | ||
return paths.map((x) => `!${x}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,18 +22,20 @@ extends: | |
- job: InitJob | ||
displayName: Initialize | ||
steps: | ||
- script: | | ||
corepack enable | ||
corepack prepare pnpm --activate | ||
displayName: Install pnpm | ||
|
||
- script: pnpm install | ||
displayName: Install JavaScript Dependencies | ||
|
||
- script: node $(Build.SourcesDirectory)/eng/common/scripts/resolve-target-branch.js | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That should be able to be combined directly in the next script? |
||
displayName: Resolve target branch | ||
|
||
- task: PowerShell@2 | ||
- script: pnpm tsx ./eng/common/scripts/dispatch-area-triggers.ts --target-branch $(TARGET_BRANCH) | ||
displayName: "Analyze PR changes" | ||
name: InitStep | ||
inputs: | ||
pwsh: true | ||
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Analyze-Changes.ps1 | ||
arguments: > | ||
-TargetBranch $(TARGET_BRANCH) | ||
workingDirectory: $(Build.SourcesDirectory) | ||
|
||
# Run csharp stages if RunCSharp == true | ||
- template: /packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { parseArgs } from "util"; | ||
import { setOutputVariable } from "./utils/ado.js"; | ||
import { repoRoot } from "./utils/common.js"; | ||
import { findAreasChanged } from "./utils/find-area-changed.js"; | ||
import { listChangedFilesSince } from "./utils/git.js"; | ||
|
||
const args = parseArgs({ | ||
args: process.argv.slice(2), | ||
options: { | ||
"target-branch": { type: "string" }, | ||
}, | ||
}); | ||
|
||
const targetBranch = args.values["target-branch"]; | ||
if (!targetBranch) { | ||
console.error("--target-branch is required"); | ||
process.exit(1); | ||
} | ||
|
||
console.log("Checking for changes in current branch compared to $TargetBranch"); | ||
|
||
const files = await listChangedFilesSince(`origin/${targetBranch}`, { repositoryPath: repoRoot }); | ||
|
||
console.log("##[group]Files changed in this pr"); | ||
console.log(files.map((x) => ` - ${x}`).join("\n")); | ||
console.log("##[endgroup]"); | ||
|
||
const areaChanged = findAreasChanged(files); | ||
|
||
for (const area of areaChanged) { | ||
console.log(`Setting output variable Run${area} to true`); | ||
setOutputVariable(`Run${area}`, "true"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function setOutputVariable(name: string, value: string) { | ||
process.stdout.write(`##vso[task.setvariable variable=${name};isOutput=true]${value}\n`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { spawn, type SpawnOptions } from "child_process"; | ||
|
||
export interface ExecResult { | ||
readonly code: number | null; | ||
readonly stdall: Buffer; | ||
readonly stdout: Buffer; | ||
readonly stderr: Buffer; | ||
} | ||
export function execAsync( | ||
cmd: string, | ||
args: string[], | ||
opts: SpawnOptions = {} | ||
): Promise<ExecResult> { | ||
return new Promise((resolve, reject) => { | ||
const child = spawn(cmd, args, opts); | ||
let stdall = Buffer.from(""); | ||
let stdout = Buffer.from(""); | ||
let stderr = Buffer.from(""); | ||
|
||
if (child.stdout) { | ||
child.stdout.on("data", (data) => { | ||
stdout = Buffer.concat([stdout, data]); | ||
stdall = Buffer.concat([stdall, data]); | ||
}); | ||
} | ||
|
||
if (child.stderr) { | ||
child.stderr.on("data", (data) => { | ||
stderr = Buffer.concat([stderr, data]); | ||
stdall = Buffer.concat([stdall, data]); | ||
}); | ||
} | ||
|
||
child.on("error", (err) => { | ||
reject(err); | ||
}); | ||
|
||
child.on("close", (code) => { | ||
resolve({ code, stdout, stderr, stdall }); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { findAreasChanged } from "./find-area-changed.js"; | ||
|
||
describe("paths that should trigger CSharp CI", () => { | ||
it.each(["packages/http-client-csharp/src/constants.ts"])("%s", (path) => { | ||
const areas = findAreasChanged([path]); | ||
expect(areas).toEqual(["CSharp"]); | ||
}); | ||
}); | ||
|
||
describe("paths that should trigger Core CI", () => { | ||
it.each([ | ||
"packages/compiler/package.json", | ||
"packages/http/package.json", | ||
"packages/openapi3/package.json", | ||
])("%s", (path) => { | ||
const areas = findAreasChanged([path]); | ||
expect(areas).toEqual(["Core"]); | ||
}); | ||
}); | ||
|
||
it("Should return a combination of core and isolated packages", () => { | ||
const areas = findAreasChanged([ | ||
"packages/http-client-csharp/src/constants.ts", | ||
"packages/compiler/package.json", | ||
]); | ||
expect(areas).toEqual(["CSharp", "Core"]); | ||
}); | ||
|
||
it("Should return CSharp and Core if .editorconfig is changed", () => { | ||
const areas = findAreasChanged([".editorconfig"]); | ||
expect(areas).toEqual(["CSharp", "Core"]); | ||
}); | ||
|
||
it("Should not return Core for .prettierignore, .prettierrc.json, cspell.yaml, esling.config.json", () => { | ||
const areas = findAreasChanged([ | ||
".prettierignore", | ||
".prettierrc.json", | ||
"cspell.yaml", | ||
"esling.config.json", | ||
"packages/http-client-csharp/emitter/src/constants.ts", | ||
]); | ||
expect(areas).toEqual(["CSharp"]); | ||
}); | ||
|
||
it("should return Core for random files at the root", () => { | ||
const areas = findAreasChanged(["some.file", "file/in/deep/directory"]); | ||
expect(areas).toEqual(["Core"]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import micromatch from "micromatch"; | ||
import pc from "picocolors"; | ||
import { CIRules } from "../../config/ci-triggers.js"; | ||
|
||
export function findAreasChanged(files: string[]): (keyof typeof CIRules)[] { | ||
const result: (keyof typeof CIRules)[] = []; | ||
for (const [name, patterns] of Object.entries(CIRules)) { | ||
const expandedPatterns = patterns.map(expandFolder); | ||
console.log(`Checking trigger ${name}, with patterns:`, expandedPatterns); | ||
const match = micromatch(files, expandedPatterns, { dot: true }); | ||
|
||
if (match.length > 0) { | ||
result.push(name as any); | ||
console.log(`Changes matched for trigger ${pc.cyan(name)}`, files); | ||
} else { | ||
console.log(`No changes matched for trigger ${pc.cyan(name)}`); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function expandFolder(maybeFolder: string) { | ||
if (maybeFolder.endsWith("/")) { | ||
return `${maybeFolder}**/*`; | ||
} | ||
return maybeFolder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { execAsync, type ExecResult } from "./exec-async.js"; | ||
|
||
export async function listChangedFilesSince( | ||
ref: string, | ||
{ repositoryPath }: { repositoryPath: string } | ||
) { | ||
return splitStdoutLines(await execGit(["diff", "--name-only", `${ref}...`], { repositoryPath })); | ||
} | ||
|
||
async function execGit( | ||
args: string[], | ||
{ repositoryPath }: { repositoryPath: string } | ||
): Promise<ExecResult> { | ||
const result = await execAsync("git", args, { cwd: repositoryPath }); | ||
|
||
if (result.code !== 0) { | ||
throw new GitError(args, result.stderr.toString()); | ||
} | ||
return result; | ||
} | ||
|
||
export class GitError extends Error { | ||
args: string[]; | ||
|
||
constructor(args: string[], stderr: string) { | ||
super(`GitError running: git ${args.join(" ")}\n${stderr}`); | ||
this.args = args; | ||
} | ||
} | ||
|
||
function splitStdoutLines(result: ExecResult): string[] { | ||
return result.stdout | ||
.toString() | ||
.split("\n") | ||
.filter((a) => a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { defineConfig, mergeConfig } from "vitest/config"; | ||
import { defaultTypeSpecVitestConfig } from "../vitest.workspace.js"; | ||
|
||
export default mergeConfig(defaultTypeSpecVitestConfig, defineConfig({})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"extends": "./tsconfig.base.json", | ||
"include": ["eng"] | ||
"include": ["eng"], | ||
"exclude": ["eng/vitest.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept it here for now, if we feel like this should move in here or another file, can do that.