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
18 changes: 14 additions & 4 deletions packages/backend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AccountPermissionSyncer } from './ee/accountPermissionSyncer.js';
import { PromClient } from './promClient.js';
import { RepoIndexManager } from './repoIndexManager.js';
import { createGitHubRepoRecord } from './repoCompileUtils.js';
import { isNotFound } from './errors.js';
import { Octokit } from '@octokit/rest';
import { SINGLE_TENANT_ORG_ID } from './constants.js';
import z from 'zod';
Expand Down Expand Up @@ -150,10 +151,19 @@ export class Api {
}

const octokit = new Octokit();
const response = await octokit.rest.repos.get({
owner: parsed.data.owner,
repo: parsed.data.repo,
});
let response;
try {
response = await octokit.rest.repos.get({
owner: parsed.data.owner,
repo: parsed.data.repo,
});
} catch (error) {
if (isNotFound(error)) {
res.status(404).json({ error: 'Repository not found on GitHub' });
return;
}
throw error;
}

const record = createGitHubRepoRecord({
repo: response.data,
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ const getStatus = (err: unknown): number | null => {

export const isUnauthorized = (err: unknown): boolean => getStatus(err) === 401;
export const isForbidden = (err: unknown): boolean => getStatus(err) === 403;
export const isNotFound = (err: unknown): boolean => getStatus(err) === 404;
export const isGone = (err: unknown): boolean => getStatus(err) === 410;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FileQuestion } from "lucide-react";
import Link from "next/link";
import { buttonVariants } from "@/components/ui/button";
import { cn } from "@/lib/utils";

interface Props {
owner: string;
repo: string;
}

export function RepoNotFound({ owner, repo }: Props) {
return (
<div className="flex flex-col items-center justify-center min-h-screen p-4">
<FileQuestion className="w-12 h-12 text-muted-foreground mb-4" />
<h2 className="text-2xl font-semibold mb-2">
Repository not found
</h2>
<p className="text-muted-foreground text-center max-w-md">
We couldn&apos;t find{" "}
<span className="font-medium text-foreground">{owner}/{repo}</span>{" "}
on GitHub. It may not exist, or it might be private.
</p>
<Link
href="/"
className={cn(buttonVariants({ variant: "outline" }), "mt-6")}
>
Go back home
</Link>
</div>
);
}
18 changes: 15 additions & 3 deletions packages/web/src/app/(app)/askgh/[owner]/[repo]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { addGithubRepo } from "@/features/workerApi/actions";
import { isServiceError } from "@/lib/utils";
import { ServiceErrorException } from "@/lib/serviceError";
import { ServiceError, ServiceErrorException } from "@/lib/serviceError";
import { ErrorCode } from "@/lib/errorCodes";
import { __unsafePrisma } from "@/prisma";
import { getRepoInfo } from "./api";
import { CustomSlateEditor } from "@/features/chat/customSlateEditor";
import { RepoIndexedGuard } from "./components/repoIndexedGuard";
import { RepoNotFound } from "./components/repoNotFound";
import { LandingPage } from "./components/landingPage";
import { getConfiguredLanguageModelsInfo } from "@/features/chat/utils.server";
import { auth } from "@/auth";
Expand All @@ -27,7 +29,7 @@ export default async function GitHubRepoPage(props: PageProps) {
return <ChatEntitlementMessage source="chat" returnPath={`/askgh/${owner}/${repo}`} />;
}

const repoId = await (async () => {
const repoIdOrError = await (async (): Promise<number | ServiceError> => {
// 1. Look up repo by owner/repo
const displayName = `${owner}/${repo}`;
const existingRepo = await __unsafePrisma.repo.findFirst({
Expand All @@ -46,12 +48,22 @@ export default async function GitHubRepoPage(props: PageProps) {
const response = await addGithubRepo(owner, repo);

if (isServiceError(response)) {
throw new ServiceErrorException(response);
return response;
}

return response.repoId;
})();

if (isServiceError(repoIdOrError)) {
if (repoIdOrError.errorCode === ErrorCode.REPOSITORY_NOT_FOUND) {
return <RepoNotFound owner={owner} repo={repo} />;
}

throw new ServiceErrorException(repoIdOrError);
}

const repoId = repoIdOrError;

const repoInfo = await getRepoInfo(repoId)
const languageModels = await getConfiguredLanguageModelsInfo()

Expand Down
5 changes: 4 additions & 1 deletion packages/web/src/features/workerApi/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use server';

import { sew } from "@/middleware/sew";
import { unexpectedError } from "@/lib/serviceError";
import { repositoryNotFound, unexpectedError } from "@/lib/serviceError";
import { withAuth, withOptionalAuth } from "@/middleware/withAuth";
import { withMinimumOrgRole } from "@/middleware/withMinimumOrgRole";
import { OrgRole } from "@sourcebot/db";
Expand Down Expand Up @@ -95,6 +95,9 @@ export const addGithubRepo = async (owner: string, repo: string) => sew(() =>
});

if (!response.ok) {
if (response.status === 404) {
return repositoryNotFound(`${owner}/${repo}`);
}
return unexpectedError('Failed to add GitHub repo');
}

Expand Down
8 changes: 8 additions & 0 deletions packages/web/src/lib/serviceError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ export const notFound = (message?: string): ServiceError => {
}
}

export const repositoryNotFound = (repository: string): ServiceError => {
return {
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.REPOSITORY_NOT_FOUND,
message: `Repository "${repository}" was not found`,
}
}

export const userNotFound = (): ServiceError => {
return {
statusCode: StatusCodes.NOT_FOUND,
Expand Down
Loading