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
3 changes: 1 addition & 2 deletions apps/desktop/src/lib/trpc/routers/changes/git-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getSimpleGitWithShellPath,
} from "../workspaces/utils/git-client";
import {
clearGitHubStatusCacheForWorktree,
fetchGitHubPRStatus,
getPullRequestRepoArgs,
getRepoContext,
Expand Down Expand Up @@ -144,7 +143,7 @@ async function resolveExistingPullRequestPushTarget({
worktreePath: string;
fallbackRemote: string;
}): Promise<ExistingPullRequestPushTarget | null> {
clearGitHubStatusCacheForWorktree(worktreePath);
clearWorktreeStatusCaches(worktreePath);
const githubStatus = await fetchGitHubPRStatus(worktreePath);
const pr = githubStatus?.pr;
if (!pr || !isOpenPullRequestState(pr.state) || !pr.headRefName?.trim()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clearGitHubStatusCacheForWorktree } from "../../workspaces/utils/github";
import { clearGitHubCachesForWorktree } from "../../workspaces/utils/github";
import { clearStatusCacheForWorktree } from "./status-cache";

export function clearWorktreeStatusCaches(worktreePath: string): void {
clearGitHubStatusCacheForWorktree(worktreePath);
clearGitHubCachesForWorktree(worktreePath);
clearStatusCacheForWorktree(worktreePath);
}
4 changes: 4 additions & 0 deletions apps/desktop/src/lib/trpc/routers/external/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export const createExternalRouter = () => {
clipboard.writeText(input);
}),

copyText: publicProcedure.input(z.string()).mutation(async ({ input }) => {
clipboard.writeText(input);
}),

resolvePath: publicProcedure
.input(
z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync } from "node:fs";
import type { GitHubStatus } from "@superset/local-db";
import { workspaces, worktrees } from "@superset/local-db";
import { and, eq, isNull } from "drizzle-orm";
import { localDb } from "main/lib/local-db";
Expand All @@ -17,7 +18,52 @@ import {
listExternalWorktrees,
refreshDefaultBranch,
} from "../utils/git";
import { fetchGitHubPRStatus } from "../utils/github";
import {
fetchGitHubPRComments,
fetchGitHubPRStatus,
type PullRequestCommentsTarget,
} from "../utils/github";

const gitHubPRCommentsInputSchema = z.object({
workspaceId: z.string(),
prNumber: z.number().int().positive().optional(),
repoUrl: z.string().optional(),
upstreamUrl: z.string().optional(),
isFork: z.boolean().optional(),
});

function resolveCommentsPullRequestTarget({
input,
githubStatus,
}: {
input: z.infer<typeof gitHubPRCommentsInputSchema>;
githubStatus: GitHubStatus | null | undefined;
}): PullRequestCommentsTarget | null {
const prNumber = input.prNumber ?? githubStatus?.pr?.number;
if (!prNumber) {
return null;
}

const repoUrl = input.repoUrl ?? githubStatus?.repoUrl;
if (!repoUrl) {
return null;
}

const upstreamUrl =
input.upstreamUrl ?? githubStatus?.upstreamUrl ?? githubStatus?.repoUrl;
if (!upstreamUrl) {
return null;
}

return {
prNumber,
repoContext: {
repoUrl,
upstreamUrl,
isFork: input.isFork ?? githubStatus?.isFork ?? false,
},
};
}

export const createGitStatusProcedures = () => {
return router({
Expand Down Expand Up @@ -130,6 +176,32 @@ export const createGitStatusProcedures = () => {
return freshStatus;
}),

getGitHubPRComments: publicProcedure
.input(gitHubPRCommentsInputSchema)
.query(async ({ input }) => {
const workspace = getWorkspace(input.workspaceId);
if (!workspace) {
return [];
}

const worktree = workspace.worktreeId
? getWorktree(workspace.worktreeId)
: null;
if (!worktree) {
return [];
}

const cachedGitHubStatus = worktree.githubStatus ?? null;

return fetchGitHubPRComments({
worktreePath: worktree.path,
pullRequest: resolveCommentsPullRequestTarget({
input,
githubStatus: cachedGitHubStatus,
}),
});
}),

getWorktreeInfo: publicProcedure
.input(z.object({ workspaceId: z.string() }))
.query(({ input }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, test } from "bun:test";
import type { GitHubStatus, PullRequestComment } from "@superset/local-db";
import {
clearGitHubCachesForWorktree,
getCachedGitHubStatus,
getCachedPullRequestComments,
getCachedRepoContext,
makePullRequestCommentsCacheKey,
setCachedGitHubStatus,
setCachedPullRequestComments,
setCachedRepoContext,
} from "./cache";

describe("clearGitHubCachesForWorktree", () => {
test("clears status, repo context, and comment entries for one worktree", () => {
const worktreePath = "/tmp/worktrees/review-cache-test";
const otherWorktreePath = "/tmp/worktrees/review-cache-test-other";

const status: GitHubStatus = {
pr: null,
repoUrl: "https://github.com/superset-sh/superset",
upstreamUrl: "https://github.com/superset-sh/superset",
isFork: false,
branchExistsOnRemote: true,
lastRefreshed: Date.now(),
};
const comments: PullRequestComment[] = [
{
id: "review-1",
authorLogin: "octocat",
body: "Looks good",
kind: "review",
},
];

setCachedGitHubStatus(worktreePath, status);
setCachedRepoContext(worktreePath, {
repoUrl: "https://github.com/superset-sh/superset",
upstreamUrl: "https://github.com/superset-sh/superset",
isFork: false,
});

const commentsCacheKey = makePullRequestCommentsCacheKey({
worktreePath,
repoNameWithOwner: "superset-sh/superset",
pullRequestNumber: 2681,
});
const otherCommentsCacheKey = makePullRequestCommentsCacheKey({
worktreePath: otherWorktreePath,
repoNameWithOwner: "superset-sh/superset",
pullRequestNumber: 2682,
});

setCachedPullRequestComments(commentsCacheKey, comments);
setCachedPullRequestComments(otherCommentsCacheKey, comments);

clearGitHubCachesForWorktree(worktreePath);

expect(getCachedGitHubStatus(worktreePath)).toBeNull();
expect(getCachedRepoContext(worktreePath)).toBeNull();
expect(getCachedPullRequestComments(commentsCacheKey)).toBeNull();
expect(getCachedPullRequestComments(otherCommentsCacheKey)).toEqual(
comments,
);

clearGitHubCachesForWorktree(otherWorktreePath);
});
});
Loading
Loading