* Add GitHub OAuth authentication routes * Add GitHub API endpoints for repos and user data * Add session and crypto utilities * Add database schema and client setup * Add database migrations * Add GitHub utility functions and client * Add authentication components and hooks * Add repo info to chat context * Add GitHub token to sandbox API * Pass GitHub token to chat sandbox * Integrate repo selection and display in chat * Add dependencies for auth and database * Add branch selector API endpoint * Create branch selector component * Add UI component library base components * Add cmdk and popover dependencies * Replace input field with branch selector component * update repo selector to be combo box * fix scroll gutter bg * update branch selector component width * remove storage and update todos * fix cc comments * add caching * cc fixes
208 lines
5.3 KiB
TypeScript
208 lines
5.3 KiB
TypeScript
import "server-only";
|
|
import { unstable_cache } from "next/cache";
|
|
|
|
const CACHE_REVALIDATE_SECONDS = 300; // 5 minutes
|
|
|
|
interface GitHubUser {
|
|
login: string;
|
|
name: string | null;
|
|
avatar_url: string;
|
|
}
|
|
|
|
interface GitHubOrg {
|
|
login: string;
|
|
avatar_url: string;
|
|
}
|
|
|
|
interface GitHubRepo {
|
|
name: string;
|
|
full_name: string;
|
|
description: string | null;
|
|
private: boolean;
|
|
clone_url: string;
|
|
updated_at: string;
|
|
language: string | null;
|
|
}
|
|
|
|
interface GitHubBranch {
|
|
name: string;
|
|
}
|
|
|
|
interface GitHubRepoInfo {
|
|
default_branch: string;
|
|
}
|
|
|
|
async function fetchGitHubAPI<T>(
|
|
endpoint: string,
|
|
token: string,
|
|
): Promise<T | null> {
|
|
const response = await fetch(`https://api.github.com${endpoint}`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
Accept: "application/vnd.github.v3+json",
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return null;
|
|
}
|
|
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
export const getCachedGitHubUser = unstable_cache(
|
|
async (userId: string, token: string) => {
|
|
const user = await fetchGitHubAPI<GitHubUser>("/user", token);
|
|
if (!user) return null;
|
|
|
|
return {
|
|
login: user.login,
|
|
name: user.name,
|
|
avatar_url: user.avatar_url,
|
|
};
|
|
},
|
|
["github-user"],
|
|
{ revalidate: CACHE_REVALIDATE_SECONDS },
|
|
);
|
|
|
|
export const getCachedGitHubOrgs = unstable_cache(
|
|
async (userId: string, token: string) => {
|
|
const orgs = await fetchGitHubAPI<GitHubOrg[]>("/user/orgs", token);
|
|
if (!orgs) return null;
|
|
|
|
return orgs.map((org) => ({
|
|
login: org.login,
|
|
name: org.login,
|
|
avatar_url: org.avatar_url,
|
|
}));
|
|
},
|
|
["github-orgs"],
|
|
{ revalidate: CACHE_REVALIDATE_SECONDS },
|
|
);
|
|
|
|
export const getCachedGitHubRepos = unstable_cache(
|
|
async (userId: string, token: string, owner: string) => {
|
|
// Check if owner is the authenticated user
|
|
const currentUser = await fetchGitHubAPI<{ login: string }>("/user", token);
|
|
if (!currentUser) return null;
|
|
|
|
const isAuthenticatedUser = currentUser.login === owner;
|
|
|
|
// Determine endpoint type
|
|
let apiEndpointType: "user" | "org" | "other" = "other";
|
|
if (isAuthenticatedUser) {
|
|
apiEndpointType = "user";
|
|
} else {
|
|
const orgResponse = await fetchGitHubAPI<unknown>(
|
|
`/orgs/${owner}`,
|
|
token,
|
|
);
|
|
if (orgResponse) {
|
|
apiEndpointType = "org";
|
|
}
|
|
}
|
|
|
|
// Fetch all repos with pagination
|
|
const allRepos: GitHubRepo[] = [];
|
|
let page = 1;
|
|
const perPage = 100;
|
|
const maxPages = 50;
|
|
|
|
while (page <= maxPages) {
|
|
let endpoint: string;
|
|
|
|
if (apiEndpointType === "user") {
|
|
endpoint = `/user/repos?sort=name&direction=asc&per_page=${perPage}&page=${page}&visibility=all&affiliation=owner`;
|
|
} else if (apiEndpointType === "org") {
|
|
endpoint = `/orgs/${owner}/repos?sort=name&direction=asc&per_page=${perPage}&page=${page}`;
|
|
} else {
|
|
endpoint = `/users/${owner}/repos?sort=name&direction=asc&per_page=${perPage}&page=${page}`;
|
|
}
|
|
|
|
const repos = await fetchGitHubAPI<GitHubRepo[]>(endpoint, token);
|
|
if (!repos) {
|
|
// API error on first page means failure; on subsequent pages, return what we have
|
|
if (page === 1) return null;
|
|
break;
|
|
}
|
|
if (repos.length === 0) break;
|
|
|
|
allRepos.push(...repos);
|
|
if (repos.length < perPage) break;
|
|
page++;
|
|
}
|
|
|
|
// Dedupe and sort
|
|
const uniqueRepos = allRepos.filter(
|
|
(repo, index, self) =>
|
|
index === self.findIndex((r) => r.full_name === repo.full_name),
|
|
);
|
|
uniqueRepos.sort((a, b) =>
|
|
a.name.toLowerCase().localeCompare(b.name.toLowerCase()),
|
|
);
|
|
|
|
return uniqueRepos.map((repo) => ({
|
|
name: repo.name,
|
|
full_name: repo.full_name,
|
|
description: repo.description,
|
|
private: repo.private,
|
|
clone_url: repo.clone_url,
|
|
updated_at: repo.updated_at,
|
|
language: repo.language,
|
|
}));
|
|
},
|
|
["github-repos"],
|
|
{ revalidate: CACHE_REVALIDATE_SECONDS },
|
|
);
|
|
|
|
export const getCachedGitHubBranches = unstable_cache(
|
|
async (userId: string, token: string, owner: string, repo: string) => {
|
|
// Fetch repo info for default branch
|
|
const repoInfo = await fetchGitHubAPI<GitHubRepoInfo>(
|
|
`/repos/${owner}/${repo}`,
|
|
token,
|
|
);
|
|
if (!repoInfo) return null;
|
|
|
|
const defaultBranch = repoInfo.default_branch;
|
|
|
|
// Fetch all branches with pagination
|
|
const allBranches: string[] = [];
|
|
let page = 1;
|
|
const perPage = 100;
|
|
const maxPages = 50;
|
|
|
|
while (page <= maxPages) {
|
|
const branches = await fetchGitHubAPI<GitHubBranch[]>(
|
|
`/repos/${owner}/${repo}/branches?per_page=${perPage}&page=${page}`,
|
|
token,
|
|
);
|
|
|
|
if (!branches) {
|
|
// API error on first page means failure; on subsequent pages, return what we have
|
|
if (page === 1) return null;
|
|
break;
|
|
}
|
|
if (branches.length === 0) break;
|
|
|
|
allBranches.push(...branches.map((b) => b.name));
|
|
if (branches.length < perPage) break;
|
|
page++;
|
|
}
|
|
|
|
// Sort with default branch first
|
|
allBranches.sort((a, b) => {
|
|
if (a === defaultBranch) return -1;
|
|
if (b === defaultBranch) return 1;
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
});
|
|
|
|
return {
|
|
branches: allBranches,
|
|
defaultBranch,
|
|
};
|
|
},
|
|
["github-branches"],
|
|
{ revalidate: CACHE_REVALIDATE_SECONDS },
|
|
);
|