diff --git a/package.json b/package.json index 1dddf9b..348917c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@octomind/octomind", - "version": "3.4.1", + "version": "3.5.0", "description": "a command line client for octomind apis", "main": "./dist/index.js", "packageManager": "pnpm@10.26.0+sha512.3b3f6c725ebe712506c0ab1ad4133cf86b1f4b687effce62a9b38b4d72e3954242e643190fc51fa1642949c735f403debd44f5cb0edd657abe63a8b6a7e1e402", diff --git a/src/tools/sync/git.ts b/src/tools/sync/git.ts index 3e07f65..8479e2f 100644 --- a/src/tools/sync/git.ts +++ b/src/tools/sync/git.ts @@ -47,16 +47,22 @@ export const getDefaultBranch = async ( allowedMethod: "symbolicRef+origin" | "origin" = "symbolicRef+origin", ): Promise => { if (allowedMethod === "symbolicRef+origin") { - const symbolicRef = ( - await simpleGit().raw("symbolic-ref", "refs/remotes/origin/HEAD") - ).trim(); - const symbolicRefBranch = symbolicRef.replace( - "refs/remotes/origin/", - "refs/heads/", - ); - - if (symbolicRefBranch) { - return symbolicRefBranch; + try { + const symbolicRef = ( + await simpleGit().raw("symbolic-ref", "refs/remotes/origin/HEAD") + ).trim(); + const symbolicRefBranch = symbolicRef.replace( + "refs/remotes/origin/", + "refs/heads/", + ); + if (symbolicRefBranch) { + return symbolicRefBranch; + } + } catch (e) { + console.warn( + "could not identify symbolic ref, falling back to origin parsing", + e, + ); } } @@ -92,7 +98,11 @@ export const getGitContext = async (): Promise => { defaultBranch, }; return ctx; - } catch { + } catch (e) { + console.warn( + "could not identify git context, falling back to undefined", + e, + ); return undefined; } }; diff --git a/src/tools/sync/push.ts b/src/tools/sync/push.ts index 379b15d..647e01e 100644 --- a/src/tools/sync/push.ts +++ b/src/tools/sync/push.ts @@ -19,6 +19,7 @@ type ErrorResponse = type PushOptions = { testTargetId: string; sourceDir: string; + branchName?: string; client: Client; onError: (error: ErrorResponse) => void; }; @@ -29,7 +30,8 @@ export const push = async ( const testCases = readTestCasesFromDir(options.sourceDir); checkForConsistency(testCases); const context = await getGitContext(); - const isDefaultBranch = context?.defaultBranch === context?.ref; + const refName = options.branchName ?? context?.ref; + const isDefaultBranch = !!context && context.defaultBranch === refName; const body: TestTargetSyncData = { testCases, diff --git a/tests/tools/sync/git.integration.spec.ts b/tests/tools/sync/git.integration.spec.ts index 84e5738..4706687 100644 --- a/tests/tools/sync/git.integration.spec.ts +++ b/tests/tools/sync/git.integration.spec.ts @@ -1,6 +1,11 @@ -import {getDefaultBranch, parseGitRemote} from "../../../src/tools/sync/git"; +import {getDefaultBranch, getGitContext, parseGitRemote} from "../../../src/tools/sync/git"; describe('git', () => { + + beforeEach(() => { + console.warn = jest.fn(); + }) + it("should return the actual owner and repo", async () => { const remote = await parseGitRemote(); @@ -14,5 +19,9 @@ describe('git', () => { expect(defaultBranch).toEqual("refs/heads/main"); }); + it("returns a context", async () => { + const context = await getGitContext() -}) \ No newline at end of file + expect(context).toBeDefined() + }); +}) diff --git a/tests/tools/sync/push.spec.ts b/tests/tools/sync/push.spec.ts index f43fa19..976c95c 100644 --- a/tests/tools/sync/push.spec.ts +++ b/tests/tools/sync/push.spec.ts @@ -63,6 +63,19 @@ describe("push", () => { expect(mockedClient.POST).toHaveBeenCalledWith("/apiKey/beta/test-targets/{testTargetId}/draft/push", expect.anything()) }) + it("pushes to draft if no git context", async () => { + jest.mocked(getGitContext).mockResolvedValue(undefined) + + await push({ + testTargetId: "someId", + sourceDir: ".", + client: mockedClient, + onError: jest.fn(), + }) + + expect(mockedClient.POST).toHaveBeenCalledWith("/apiKey/beta/test-targets/{testTargetId}/draft/push", expect.anything()) + }) + it("calls the handleError callback on error", async () => { jest.mocked(getGitContext).mockResolvedValue({ defaultBranch: "refs/heads/main",