-
Notifications
You must be signed in to change notification settings - Fork 341
fix: resolve push_files commit action per file #643
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
Closed
Closed
Changes from all commits
Commits
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
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,220 @@ | ||
| import { describe, test } from "node:test"; | ||
| import assert from "node:assert"; | ||
| import { spawn } from "child_process"; | ||
| import { MockGitLabServer, findMockServerPort } from "./utils/mock-gitlab-server.js"; | ||
|
|
||
| const MOCK_TOKEN = "mock-token-push-files"; | ||
| const PROJECT_ID = "42"; | ||
| const BRANCH = "feature/update"; | ||
|
|
||
| const MOCK_COMMIT = { | ||
| id: "0123456789abcdef0123456789abcdef01234567", | ||
| short_id: "01234567", | ||
| title: "Update files", | ||
| author_name: "Tester", | ||
| author_email: "tester@example.com", | ||
| authored_date: "2025-01-01T00:00:00.000Z", | ||
| committer_name: "Tester", | ||
| committer_email: "tester@example.com", | ||
| committed_date: "2025-01-01T00:00:00.000Z", | ||
| web_url: "https://gitlab.example.com/group/project/-/commit/01234567", | ||
| parent_ids: [], | ||
| }; | ||
|
|
||
| interface CommitAction { | ||
| action: string; | ||
| file_path: string; | ||
| content: string; | ||
| } | ||
|
|
||
| async function callPushFiles( | ||
| args: Record<string, unknown>, | ||
| env: NodeJS.ProcessEnv | ||
| ): Promise<unknown> { | ||
| return new Promise((resolve, reject) => { | ||
| const proc = spawn("node", ["build/index.js"], { | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| env: { ...process.env, ...env }, | ||
| }); | ||
|
|
||
| let output = ""; | ||
| let errorOutput = ""; | ||
| proc.stdout?.on("data", (d: Buffer) => (output += d)); | ||
| proc.stderr?.on("data", (d: Buffer) => (errorOutput += d)); | ||
|
|
||
| proc.on("close", code => { | ||
| if (code !== 0) { | ||
| return reject(new Error(`Process exited with code ${code}: ${errorOutput}`)); | ||
| } | ||
|
|
||
| const line = output.split("\n").find(l => l.startsWith("{")); | ||
| if (!line) return reject(new Error("No JSON output found")); | ||
|
|
||
| try { | ||
| const response = JSON.parse(line); | ||
| if (response.error) { | ||
| reject(new Error(response.error?.message ?? String(response.error))); | ||
| } else { | ||
| const content = response.result?.content?.[0]?.text; | ||
| if (response.result?.isError) { | ||
| return reject(new Error(content ?? "Tool call failed")); | ||
| } | ||
| resolve(content ? JSON.parse(content) : response.result); | ||
| } | ||
| } catch (e) { | ||
| reject(e); | ||
| } | ||
| }); | ||
|
|
||
| proc.stdin?.end( | ||
| JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| method: "tools/call", | ||
| params: { name: "push_files", arguments: args }, | ||
| }) + "\n" | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Starts a mock GitLab where `existingPaths` are already tracked in the branch and | ||
| * everything else 404s, then runs one push_files call and returns the commit actions | ||
| * the server received. | ||
| */ | ||
| async function actionsForPush( | ||
| existingPaths: string[], | ||
| files: { file_path: string; content: string }[] | ||
| ): Promise<CommitAction[]> { | ||
| const mockPort = await findMockServerPort(); | ||
| const mockServer = new MockGitLabServer({ port: mockPort, validTokens: [MOCK_TOKEN] }); | ||
| let receivedActions: CommitAction[] = []; | ||
|
|
||
| for (const path of existingPaths) { | ||
| mockServer.addMockHandler( | ||
| "head", | ||
| `/projects/${PROJECT_ID}/repository/files/${encodeURIComponent(path)}`, | ||
| (req, res) => { | ||
| assert.strictEqual(req.query.ref, BRANCH); | ||
| res.status(200).end(); | ||
| } | ||
| ); | ||
| } | ||
|
nikes marked this conversation as resolved.
|
||
|
|
||
| mockServer.addMockHandler("post", `/projects/${PROJECT_ID}/repository/commits`, (req, res) => { | ||
| receivedActions = (req.body as { actions: CommitAction[] }).actions; | ||
| res.status(201).json(MOCK_COMMIT); | ||
| }); | ||
|
|
||
| await mockServer.start(); | ||
|
|
||
| try { | ||
| await callPushFiles( | ||
| { | ||
| project_id: PROJECT_ID, | ||
| branch: BRANCH, | ||
| commit_message: "Update files", | ||
| files, | ||
| }, | ||
| { | ||
| GITLAB_API_URL: `${mockServer.getUrl()}/api/v4`, | ||
| GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN, | ||
| } | ||
| ); | ||
| } finally { | ||
| await mockServer.stop(); | ||
| } | ||
|
|
||
| return receivedActions; | ||
| } | ||
|
|
||
| describe("When push_files commits a file that already exists", () => { | ||
| test("should send action 'update' instead of 'create'", async () => { | ||
| // Regression: every action was hardcoded to "create", and GitLab answers | ||
| // `400 A file with this name already exists` for a tracked path — so push_files | ||
| // could only ever add new files. | ||
| const actions = await actionsForPush( | ||
| ["src/index.ts"], | ||
| [{ file_path: "src/index.ts", content: "export const x = 1;\n" }] | ||
| ); | ||
|
|
||
| assert.strictEqual(actions.length, 1); | ||
| assert.strictEqual(actions[0].action, "update"); | ||
| assert.strictEqual(actions[0].file_path, "src/index.ts"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("When push_files commits a file that does not exist", () => { | ||
| test("should send action 'create'", async () => { | ||
| const actions = await actionsForPush( | ||
| [], | ||
| [{ file_path: "docs/new-page.md", content: "# New\n" }] | ||
| ); | ||
|
|
||
| assert.strictEqual(actions.length, 1); | ||
| assert.strictEqual(actions[0].action, "create"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("When checking whether a file exists fails", () => { | ||
| test("should propagate the error without sending a commit request", async () => { | ||
| const mockPort = await findMockServerPort(); | ||
| const mockServer = new MockGitLabServer({ port: mockPort, validTokens: [MOCK_TOKEN] }); | ||
| let commitRequested = false; | ||
|
|
||
| mockServer.addMockHandler( | ||
| "head", | ||
| `/projects/${PROJECT_ID}/repository/files/${encodeURIComponent("src/index.ts")}`, | ||
| (req, res) => { | ||
| assert.strictEqual(req.query.ref, BRANCH); | ||
| res.status(403).end(); | ||
| } | ||
| ); | ||
| mockServer.addMockHandler("post", `/projects/${PROJECT_ID}/repository/commits`, (_req, res) => { | ||
| commitRequested = true; | ||
| res.status(201).json(MOCK_COMMIT); | ||
| }); | ||
|
|
||
| await mockServer.start(); | ||
|
|
||
| try { | ||
| await assert.rejects( | ||
| callPushFiles( | ||
| { | ||
| project_id: PROJECT_ID, | ||
| branch: BRANCH, | ||
| commit_message: "Update files", | ||
| files: [{ file_path: "src/index.ts", content: "export const x = 1;\n" }], | ||
| }, | ||
| { | ||
| GITLAB_API_URL: `${mockServer.getUrl()}/api/v4`, | ||
| GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN, | ||
| } | ||
| ), | ||
| /GitLab API error: 403 Forbidden/ | ||
| ); | ||
| } finally { | ||
| await mockServer.stop(); | ||
| } | ||
|
|
||
| assert.strictEqual(commitRequested, false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("When push_files commits a mix of new and existing files", () => { | ||
| test("should resolve the action per file", async () => { | ||
| const actions = await actionsForPush( | ||
| ["src/index.ts"], | ||
| [ | ||
| { file_path: "src/index.ts", content: "export const x = 2;\n" }, | ||
| { file_path: "src/new-module.ts", content: "export const y = 3;\n" }, | ||
| ] | ||
| ); | ||
|
|
||
| const byPath = Object.fromEntries(actions.map(a => [a.file_path, a.action])); | ||
| assert.deepStrictEqual(byPath, { | ||
| "src/index.ts": "update", | ||
| "src/new-module.ts": "create", | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.