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
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,34 @@ describe("PullRequestRuntimeManager direct checkout PR linking", () => {
},
]);
});

test("preserves existing pullRequestId when head lookup fails", async () => {
const state = makeState("fix/sidebar");
state.workspace = {
...state.workspace,
headSha: "abc123",
upstreamOwner: "fork-owner",
upstreamRepo: "fork-repo",
upstreamBranch: "fix/sidebar",
pullRequestId: "pr-existing",
};
const manager = createManager(state, {
execGh: async () => {
throw new Error("gh unavailable");
},
github: async () => {
throw new Error("octokit unavailable");
},
});

const originalWarn = console.warn;
console.warn = () => {};
try {
await manager.refreshPullRequestsByWorkspaces([WORKSPACE_ID]);
} finally {
console.warn = originalWarn;
}

expect(state.workspace.pullRequestId).toBe("pr-existing");
});
});
36 changes: 24 additions & 12 deletions packages/host-service/src/runtime/pull-requests/pull-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,8 @@ export class PullRequestRuntimeManager {
}
}

const keyToPullRequest = await this.fetchRepoPullRequests(
projectId,
repo,
wantedRefs,
options,
);
const { failedKeys, matched: keyToPullRequest } =
await this.fetchRepoPullRequests(projectId, repo, wantedRefs, options);

for (const workspace of projectWorkspaces) {
const key = upstreamKey(
Expand Down Expand Up @@ -673,9 +669,20 @@ export class PullRequestRuntimeManager {
continue;
}
const match = keyToPullRequest.get(key);
if (match) {
this.db
.update(workspaces)
.set({ pullRequestId: match.id })
.where(eq(workspaces.id, workspace.id))
.run();
continue;
}

if (failedKeys.has(key)) continue;

this.db
.update(workspaces)
.set({ pullRequestId: match?.id ?? null })
.set({ pullRequestId: null })
.where(eq(workspaces.id, workspace.id))
.run();
}
Expand Down Expand Up @@ -918,8 +925,13 @@ export class PullRequestRuntimeManager {
repo: NormalizedRepoIdentity,
wantedRefs: Map<string, GitHubPullRequestHeadRef>,
options: { bypassCache?: boolean } = {},
): Promise<Map<string, { id: string }>> {
if (wantedRefs.size === 0) return new Map();
): Promise<{
matched: Map<string, { id: string }>;
failedKeys: Set<string>;
}> {
const matched = new Map<string, { id: string }>();
const failedKeys = new Set<string>();
if (wantedRefs.size === 0) return { matched, failedKeys };

const latestByKey = new Map<string, GitHubPullRequestNode>();
await Promise.all(
Expand All @@ -939,6 +951,7 @@ export class PullRequestRuntimeManager {
);
if (nodeKey === key) latestByKey.set(key, node);
} catch (error) {
failedKeys.add(key);
console.warn(
"[host-service:pull-request-runtime] Failed to fetch PR by head",
{
Expand All @@ -953,7 +966,6 @@ export class PullRequestRuntimeManager {
}),
);

const keyToRow = new Map<string, { id: string }>();
const now = Date.now();

const checksByNumber = new Map<
Expand Down Expand Up @@ -1038,9 +1050,9 @@ export class PullRequestRuntimeManager {
now,
});

keyToRow.set(key, { id: rowId });
matched.set(key, { id: rowId });
}

return keyToRow;
return { matched, failedKeys };
}
}
Loading