Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/host-router/src/ports/git-pr-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export interface IGitPrStatus {
cloudPrUrl: string | null,
): Promise<TaskPrStatus>;
getCachedPrUrl(taskId: string): CachedPrUrlOutput;
setPrimaryPrUrl(taskId: string, prUrl: string): void;
setPrimaryPrUrl(taskId: string, prUrl: string): Promise<void>;
}
46 changes: 39 additions & 7 deletions packages/workspace-server/src/services/git/task-pr-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,62 @@ describe("TaskPrStatusService revalidation PR detection", () => {
});

describe("TaskPrStatusService.setPrimaryPrUrl", () => {
it("emits the promoted url as prUrl even though the row column is stale", () => {
const PR_OLD = "https://github.com/acme/repo/pull/1";
const PR_NEW = "https://github.com/acme/repo/pull/2";
const gitService = {} as unknown as GitService;
const PR_OLD = "https://github.com/acme/repo/pull/1";
const PR_NEW = "https://github.com/acme/repo/pull/2";

function makeService(getPrDetailsByUrl: ReturnType<typeof vi.fn>) {
const gitService = { getPrDetailsByUrl } as unknown as GitService;
const workspaceService = { emit: vi.fn() };
const workspaceRepo = {
promotePrUrl: vi.fn(),
findByTaskId: vi.fn().mockReturnValue({ prUrl: PR_OLD, prState: "open" }),
updatePrCache: vi.fn(),
getPrUrls: vi.fn().mockReturnValue([PR_NEW, PR_OLD]),
};
const service = new TaskPrStatusService(
gitService,
workspaceRepo as unknown as IWorkspaceRepository,
workspaceService as unknown as WorkspaceService,
);
return { service, workspaceService, workspaceRepo };
}

it.each([
{
name: "recomputes and emits the promoted PR's live state, not the stale cache",
details: vi.fn().mockResolvedValue({
state: "open",
merged: false,
draft: false,
}),
expectedPrState: "open",
},
{
name: "emits a null state when the promoted PR's details are unavailable",
details: vi.fn().mockResolvedValue(null),
expectedPrState: null,
},
{
name: "falls back to a null state when the details fetch rejects",
details: vi.fn().mockRejectedValue(new Error("network down")),
expectedPrState: null,
},
])("$name", async ({ details, expectedPrState }) => {
const { service, workspaceService, workspaceRepo } = makeService(details);

service.setPrimaryPrUrl("task-1", PR_NEW);
await service.setPrimaryPrUrl("task-1", PR_NEW);

expect(workspaceRepo.promotePrUrl).toHaveBeenCalledWith("task-1", PR_NEW);
expect(details).toHaveBeenCalledWith(PR_NEW);
expect(workspaceRepo.updatePrCache).toHaveBeenCalledWith("task-1", {
prUrl: PR_NEW,
prState: expectedPrState,
accumulate: false,
});
expect(workspaceService.emit).toHaveBeenCalledWith("taskPrInfoChanged", {
taskId: "task-1",
prUrl: PR_NEW,
prUrls: [PR_NEW, PR_OLD],
prState: "open",
prState: expectedPrState,
});
});
});
16 changes: 13 additions & 3 deletions packages/workspace-server/src/services/git/task-pr-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,24 @@ export class TaskPrStatusService {
};
}

setPrimaryPrUrl(taskId: string, prUrl: string): void {
async setPrimaryPrUrl(taskId: string, prUrl: string): Promise<void> {
this.workspaceRepo.promotePrUrl(taskId, prUrl);
const row = this.workspaceRepo.findByTaskId(taskId);
const details = await this.gitService
.getPrDetailsByUrl(prUrl)
.catch(() => null);
const prState: SidebarPrState = details
? mapPrState(details.state, details.merged, details.draft)
: null;
this.workspaceRepo.updatePrCache(taskId, {
prUrl,
prState,
accumulate: false,
});
this.workspaceService.emit("taskPrInfoChanged", {
taskId,
prUrl,
prUrls: this.workspaceRepo.getPrUrls(taskId),
prState: row?.prState ?? null,
prState,
});
}
Comment thread
frankh marked this conversation as resolved.

Expand Down
Loading