fix: handle branches betterer
Node.js CI / CI (push) Has been cancelled
Security Scan and Upload / Security & DefectDojo Upload (push) Has been cancelled

This commit is contained in:
2026-02-03 19:25:50 -08:00
parent 0dbedfe546
commit 565581570c
2 changed files with 49 additions and 2 deletions
+11 -2
View File
@@ -224,8 +224,17 @@ const handleExistingBranch = async(
= options; = options;
const { path: repoPath } = clonedRepo; const { path: repoPath } = clonedRepo;
await runGitCommand(logger, repoPath, `git checkout ${branchName}`); // Delete local branch if it exists (stale from previous run)
await runGitCommand(logger, repoPath, `git pull origin ${branchName}`); const localBranches = await runGitCommand(logger, repoPath, "git branch");
if (localBranches.includes(branchName)) {
await runGitCommand(logger, repoPath, `git branch -D ${branchName}`);
}
await runGitCommand(
logger,
repoPath,
`git checkout -b ${branchName} origin/${branchName}`,
);
const currentVersion = await getCurrentVersionOnBranch(repoPath, packageName); const currentVersion = await getCurrentVersionOnBranch(repoPath, packageName);
+38
View File
@@ -203,6 +203,44 @@ describe("gitService", () => {
expect(result.status).toBe("up-to-date"); expect(result.status).toBe("up-to-date");
}); });
it("should delete stale local branch before checking out remote", async() => {
expect.assertions(2);
const branchDeleteCalls: Array<string> = [];
mockExecAsync.mockImplementation((command: string) => {
if (command.includes("git branch -r")) {
return Promise.resolve({
stderr: "",
stdout: " origin/dependencies/update-test-package\n",
});
}
if (command.includes("git branch") && !command.includes("-")) {
// Return local branches including the stale one
return Promise.resolve({
stderr: "",
stdout: "* main\n dependencies/update-test-package\n",
});
}
if (command.includes("git branch -D")) {
branchDeleteCalls.push(command);
}
return Promise.resolve({ stderr: "", stdout: "" });
});
vi.mocked(readFile).mockResolvedValue(JSON.stringify({
dependencies: { "test-package": "2.0.0" },
}));
const { createOrUpdateBranch } = await import("../../src/services/gitService.js");
const mockClonedRepo = createMockClonedRepo();
const result = await createOrUpdateBranch({
branchName: "dependencies/update-test-package",
clonedRepo: mockClonedRepo,
logger: mockLogger,
packageName: "test-package",
targetVersion: "2.0.0",
});
expect(result.status).toBe("up-to-date");
expect(branchDeleteCalls).toHaveLength(1);
});
it("should fail when package is not found", async() => { it("should fail when package is not found", async() => {
expect.assertions(2); expect.assertions(2);
mockExecAsync.mockResolvedValue({ stderr: "", stdout: "" }); mockExecAsync.mockResolvedValue({ stderr: "", stdout: "" });