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
14 changes: 12 additions & 2 deletions apps/dashboard/src/lib/github-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type GetOrRevalidateGitHubResourceOptions<TData> = {
getNamespaceVersions?: (
namespaceKeys: string[],
) => Promise<Record<string, number>>;
merge?: (existing: TData, fresh: TData) => TData;
now?: () => number;
};

Expand Down Expand Up @@ -615,6 +616,7 @@ export async function getOrRevalidateGitHubResource<TData>({
cacheMode = "legacy",
payloadRetentionSeconds = DEFAULT_GITHUB_PAYLOAD_RETENTION_SECONDS,
fetcher,
merge,
now = Date.now,
store,
payloadStore,
Expand Down Expand Up @@ -755,14 +757,22 @@ export async function getOrRevalidateGitHubResource<TData>({
return parseCachedPayload<TData>(existingEntry.payloadJson);
}

const mergedData =
merge && existingEntry
? merge(
parseCachedPayload<TData>(existingEntry.payloadJson),
result.data,
)
: result.data;

const nextEntry = {
cacheKey,
userId,
resource,
paramsJson,
etag: result.metadata.etag,
lastModified: result.metadata.lastModified,
payloadJson: JSON.stringify(result.data),
payloadJson: JSON.stringify(mergedData),
fetchedAt: currentTime,
freshUntil:
currentTime +
Expand All @@ -780,7 +790,7 @@ export async function getOrRevalidateGitHubResource<TData>({
payloadRetentionSeconds,
});

return result.data;
return mergedData;
})();

resolvedInFlightCache?.set(cacheKey, task);
Expand Down
48 changes: 18 additions & 30 deletions apps/dashboard/src/lib/github.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3787,34 +3787,20 @@ async function getMySearchSources(
deadlineAt: number,
): Promise<GitHubGraphQLSearchSource[]> {
let installations: GitHubAppInstallation[] = [];
let organizations: GitHubOrganization[] = [];
try {
const [installationResult, organizationResult] =
await withGitHubOperationTimeout(
"github search source discovery",
getRemainingSearchTimeoutMs(deadlineAt, MY_SEARCH_SOURCE_TIMEOUT_MS),
() =>
Promise.all([
getGitHubAppUserInstallations(context.session.user.id),
getGitHubAuthenticatedOrganizations(context),
]),
);
const installationResult = await withGitHubOperationTimeout(
"github search source discovery",
getRemainingSearchTimeoutMs(deadlineAt, MY_SEARCH_SOURCE_TIMEOUT_MS),
() => getGitHubAppUserInstallations(context.session.user.id),
);
installations = installationResult.installations;
organizations = organizationResult;
} catch (error) {
console.error("[github-search] failed to discover search sources", error);
}

const sources: GitHubGraphQLSearchSource[] = [];
const excludedOAuthOwners = new Map<string, GitHubSearchOwnerScope>();

for (const organization of organizations) {
addExcludedOwnerScope(excludedOAuthOwners, {
login: organization.login,
targetType: "Organization",
});
}

for (const installation of installations) {
const owner = toSearchOwnerScope(installation);
if (!owner) {
Expand All @@ -3832,17 +3818,6 @@ async function getMySearchSources(
break;
}

if (owner.targetType === "Organization") {
addExcludedOwnerScope(excludedOAuthOwners, owner);
}
if (
owner.targetType === "User" &&
installation.repositorySelection === "all" &&
normalizeLogin(owner.login) === normalizeLogin(viewerLogin)
) {
addExcludedOwnerScope(excludedOAuthOwners, owner);
}

const installationContext = await withGitHubOperationTimeout(
`github installation context ${installation.id}`,
contextTimeoutMs,
Expand All @@ -3859,6 +3834,17 @@ async function getMySearchSources(
continue;
}

if (owner.targetType === "Organization") {
addExcludedOwnerScope(excludedOAuthOwners, owner);
}
if (
owner.targetType === "User" &&
installation.repositorySelection === "all" &&
normalizeLogin(owner.login) === normalizeLogin(viewerLogin)
) {
addExcludedOwnerScope(excludedOAuthOwners, owner);
}

sources.push({
label: `installation:${installation.id}:${owner.login}`,
context: installationContext,
Expand Down Expand Up @@ -3959,6 +3945,7 @@ async function getMyPullsResult({
signalKeys: [githubRevalidationSignalKeys.pullsMine],
namespaceKeys: [githubRevalidationSignalKeys.pullsMine],
cacheMode: "split",
merge: (existing, fresh) => mergeMyPullsResults([existing, fresh]),
fetcher: async () => {
const deadlineAt = Date.now() + MY_SEARCH_TOTAL_TIMEOUT_MS;
const sources = await getMySearchSources(context, username, deadlineAt);
Expand Down Expand Up @@ -4130,6 +4117,7 @@ async function getMyIssuesResult({
signalKeys: [githubRevalidationSignalKeys.issuesMine],
namespaceKeys: [githubRevalidationSignalKeys.issuesMine],
cacheMode: "split",
merge: (existing, fresh) => mergeMyIssuesResults([existing, fresh]),
fetcher: async () => {
const deadlineAt = Date.now() + MY_SEARCH_TOTAL_TIMEOUT_MS;
const sources = await getMySearchSources(context, username, deadlineAt);
Expand Down
Loading