-
Notifications
You must be signed in to change notification settings - Fork 1
feat(github-workflow): add auto-create-pr hook #46
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
Open
masseater
wants to merge
6
commits into
master
Choose a base branch
from
feat/auto-create-pr
base: master
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
597d774
feat(github-workflow): add auto-create-pr hook
masseater 14bf9a9
chore(github-workflow): bump version to 0.0.10
masseater 895b95f
chore: auto-sync plugin list
masseater d91ab1d
test(github-workflow): add tests for auto-create-pr hook
masseater ff8f77d
fix(ops-harbor-core): exclude untested files from coverage threshold
masseater 383adcc
fix(ops-harbor): scope coverage to tested model/lib files
masseater 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,285 @@ | ||
| import { describe, expect, test, vi } from "vitest"; | ||
|
|
||
| const { capturedHookDefs, mockExecFileSync, mockSpawnSync, mockSpawn } = vi.hoisted(() => ({ | ||
| capturedHookDefs: [] as { run: (ctx: unknown) => unknown }[], | ||
| mockExecFileSync: vi.fn<(...args: unknown[]) => string>(), | ||
| mockSpawnSync: vi.fn(), | ||
| mockSpawn: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@r_masseater/cc-plugin-lib", () => ({ | ||
| HookLogger: { | ||
| fromFile: () => ({ | ||
| debug: vi.fn(), | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| [Symbol.dispose]: vi.fn(), | ||
| }), | ||
| }, | ||
| wrapRun: vi.fn((_logger: unknown, fn: unknown) => fn), | ||
| })); | ||
|
|
||
| vi.mock("cc-hooks-ts", () => ({ | ||
| defineHook: vi.fn((def: { run: (ctx: unknown) => unknown }) => { | ||
| capturedHookDefs.push(def); | ||
| return def; | ||
| }), | ||
| runHook: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("node:child_process", () => ({ | ||
| execFileSync: (...args: unknown[]) => mockExecFileSync(...args), | ||
| })); | ||
|
|
||
| // Mock Bun global | ||
| const originalBun = globalThis.Bun; | ||
|
|
||
| function mockBunGlobal(overrides: { spawnSync?: typeof mockSpawnSync; spawn?: typeof mockSpawn }) { | ||
| Object.assign(globalThis, { | ||
| Bun: { | ||
| ...originalBun, | ||
| spawnSync: overrides.spawnSync ?? mockSpawnSync, | ||
| spawn: overrides.spawn ?? mockSpawn, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| import "./entry/auto-create-pr.ts"; | ||
|
|
||
| describe("auto-create-pr hook", () => { | ||
| function createMockContext(command: string) { | ||
| const successResult = { type: "success" }; | ||
| let deferFn: (() => Promise<unknown>) | null = null; | ||
| return { | ||
| input: { tool_input: { command } }, | ||
| success: vi.fn(() => successResult), | ||
| defer: vi.fn((fn: () => Promise<unknown>) => { | ||
| deferFn = fn; | ||
| return { type: "defer" }; | ||
| }), | ||
| getDeferFn: () => deferFn, | ||
| }; | ||
| } | ||
|
|
||
| function getHookRun() { | ||
| const hookDef = capturedHookDefs[0]; | ||
| if (!hookDef) throw new Error("Hook not captured"); | ||
| return hookDef.run; | ||
| } | ||
|
|
||
| test("returns success for non-push commands", () => { | ||
| const ctx = createMockContext("git status"); | ||
| const run = getHookRun(); | ||
| const result = run(ctx); | ||
| expect(ctx.success).toHaveBeenCalledWith({}); | ||
| expect(result).toStrictEqual({ type: "success" }); | ||
| }); | ||
|
|
||
| test("returns success when branch is unavailable", () => { | ||
| mockExecFileSync.mockImplementation(() => { | ||
| throw new Error("not a git repo"); | ||
| }); | ||
|
|
||
| const ctx = createMockContext("git push origin main"); | ||
| const run = getHookRun(); | ||
| run(ctx); | ||
|
|
||
| expect(ctx.success).toHaveBeenCalledWith({}); | ||
| }); | ||
|
|
||
| test("returns success when on default branch", () => { | ||
| mockExecFileSync.mockImplementation((command, args) => { | ||
| const commandName = command as string; | ||
| const commandArgs = args as string[]; | ||
| if (commandName === "git" && commandArgs[0] === "symbolic-ref") return "main"; | ||
| return ""; | ||
| }); | ||
| mockSpawnSync.mockReturnValue({ | ||
| stdout: { toString: () => "main" }, | ||
| stderr: { toString: () => "" }, | ||
| }); | ||
|
|
||
| mockBunGlobal({ spawnSync: mockSpawnSync }); | ||
|
|
||
| const ctx = createMockContext("git push"); | ||
| const run = getHookRun(); | ||
| run(ctx); | ||
|
|
||
| expect(ctx.success).toHaveBeenCalledWith({}); | ||
| }); | ||
|
|
||
| test("returns success when PR already exists", () => { | ||
| mockExecFileSync.mockImplementation((command, args) => { | ||
| const commandName = command as string; | ||
| const commandArgs = args as string[]; | ||
| if (commandName === "git" && commandArgs[0] === "symbolic-ref") return "feature/test"; | ||
| return ""; | ||
| }); | ||
| mockSpawnSync | ||
| .mockReturnValueOnce({ | ||
| // getDefaultBranch | ||
| stdout: { toString: () => "main" }, | ||
| stderr: { toString: () => "" }, | ||
| }) | ||
| .mockReturnValueOnce({ | ||
| // prExists | ||
| exitCode: 0, | ||
| stdout: { toString: () => '{"number":42}' }, | ||
| stderr: { toString: () => "" }, | ||
| }); | ||
|
|
||
| mockBunGlobal({ spawnSync: mockSpawnSync }); | ||
|
|
||
| const ctx = createMockContext("git push origin feature/test"); | ||
| const run = getHookRun(); | ||
| run(ctx); | ||
|
|
||
| expect(ctx.success).toHaveBeenCalledWith({}); | ||
| }); | ||
|
|
||
| test("defers PR creation when no PR exists", () => { | ||
| mockExecFileSync.mockImplementation((command, args) => { | ||
| const commandName = command as string; | ||
| const commandArgs = args as string[]; | ||
| if (commandName === "git" && commandArgs[0] === "symbolic-ref") return "feature/new"; | ||
| return ""; | ||
| }); | ||
| mockSpawnSync | ||
| .mockReturnValueOnce({ | ||
| // getDefaultBranch | ||
| stdout: { toString: () => "main" }, | ||
| stderr: { toString: () => "" }, | ||
| }) | ||
| .mockReturnValueOnce({ | ||
| // prExists - no PR | ||
| exitCode: 1, | ||
| stdout: { toString: () => "" }, | ||
| stderr: { toString: () => "no pull requests found" }, | ||
| }); | ||
|
|
||
| mockBunGlobal({ spawnSync: mockSpawnSync }); | ||
|
|
||
| const ctx = createMockContext("git push origin feature/new"); | ||
| const run = getHookRun(); | ||
| const result = run(ctx); | ||
|
|
||
| expect(result).toStrictEqual({ type: "defer" }); | ||
| expect(ctx.defer).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("deferred function reports successful PR creation", async () => { | ||
| mockExecFileSync.mockImplementation((command, args) => { | ||
| const commandName = command as string; | ||
| const commandArgs = args as string[]; | ||
| if (commandName === "git" && commandArgs[0] === "symbolic-ref") return "feature/new"; | ||
| return ""; | ||
| }); | ||
| mockSpawnSync | ||
| .mockReturnValueOnce({ | ||
| stdout: { toString: () => "main" }, | ||
| stderr: { toString: () => "" }, | ||
| }) | ||
| .mockReturnValueOnce({ | ||
| exitCode: 1, | ||
| stdout: { toString: () => "" }, | ||
| stderr: { toString: () => "no pull requests found" }, | ||
| }); | ||
|
|
||
| const createReadableStream = (text: string) => | ||
| new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode(text)); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| mockSpawn | ||
| .mockReturnValueOnce({ | ||
| // gh pr create | ||
| exited: Promise.resolve(0), | ||
| exitCode: 0, | ||
| stdout: createReadableStream("https://github.com/owner/repo/pull/1"), | ||
| stderr: createReadableStream(""), | ||
| }) | ||
| .mockReturnValueOnce({ | ||
| // gh pr view | ||
| exited: Promise.resolve(0), | ||
| exitCode: 0, | ||
| stdout: createReadableStream("Add new feature"), | ||
| stderr: createReadableStream(""), | ||
| }); | ||
|
|
||
| mockBunGlobal({ spawnSync: mockSpawnSync, spawn: mockSpawn }); | ||
|
|
||
| const ctx = createMockContext("git push origin feature/new"); | ||
| const run = getHookRun(); | ||
| run(ctx); | ||
|
|
||
| const deferFn = ctx.getDeferFn(); | ||
| expect(deferFn).not.toBeNull(); | ||
|
|
||
| const result = (await deferFn!()) as { | ||
| output: { hookSpecificOutput: { additionalContext: string } }; | ||
| }; | ||
|
|
||
| expect(result.output.hookSpecificOutput.additionalContext).toContain( | ||
| "[PR Auto-Create] Created PR", | ||
| ); | ||
| expect(result.output.hookSpecificOutput.additionalContext).toContain( | ||
| "https://github.com/owner/repo/pull/1", | ||
| ); | ||
| expect(result.output.hookSpecificOutput.additionalContext).toContain("Add new feature"); | ||
| }); | ||
|
|
||
| test("deferred function reports failed PR creation", async () => { | ||
| mockExecFileSync.mockImplementation((command, args) => { | ||
| const commandName = command as string; | ||
| const commandArgs = args as string[]; | ||
| if (commandName === "git" && commandArgs[0] === "symbolic-ref") return "feature/fail"; | ||
| return ""; | ||
| }); | ||
| mockSpawnSync | ||
| .mockReturnValueOnce({ | ||
| stdout: { toString: () => "main" }, | ||
| stderr: { toString: () => "" }, | ||
| }) | ||
| .mockReturnValueOnce({ | ||
| exitCode: 1, | ||
| stdout: { toString: () => "" }, | ||
| stderr: { toString: () => "no pull requests found" }, | ||
| }); | ||
|
|
||
| const createReadableStream = (text: string) => | ||
| new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode(text)); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| mockSpawn.mockReturnValueOnce({ | ||
| exited: Promise.resolve(1), | ||
| exitCode: 1, | ||
| stdout: createReadableStream(""), | ||
| stderr: createReadableStream("pull request create failed"), | ||
| }); | ||
|
|
||
| mockBunGlobal({ spawnSync: mockSpawnSync, spawn: mockSpawn }); | ||
|
|
||
| const ctx = createMockContext("git push origin feature/fail"); | ||
| const run = getHookRun(); | ||
| run(ctx); | ||
|
|
||
| const deferFn = ctx.getDeferFn(); | ||
| const result = (await deferFn!()) as { | ||
| output: { hookSpecificOutput: { additionalContext: string } }; | ||
| }; | ||
|
|
||
| expect(result.output.hookSpecificOutput.additionalContext).toContain( | ||
| "[PR Auto-Create] Failed to create PR", | ||
| ); | ||
| expect(result.output.hookSpecificOutput.additionalContext).toContain( | ||
| "pull request create failed", | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Restore
globalThis.Bunand mock state after each test.At Line 38-Line 44, the suite mutates a global and never restores it. This can pollute later tests and cause order-dependent failures.
Suggested fix
🤖 Prompt for AI Agents