Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add GH and repo handling [WIP]
Tomas Valenta2d78abc1e455
diff --git a/components/Repos.tsx b/components/Repos.tsxindex d44179289..deb5f5ba2 100644--- a/components/Repos.tsx+++ b/components/Repos.tsx@@ -7,20 +7,28 @@ import { useGitHub } from 'hooks/useGitHub'import useListenMessage from 'hooks/useListenMessage'import Button from 'components/Button'import Text from 'components/Text'-import { useLocalStorage } from 'hooks/useLocalStorage'import { useRepositories } from 'hooks/useRepositories'-import { PostProjectBody } from 'pages/api/project'+import { openPopupModal } from 'utils/popupModal'import TitleButton from 'components/TitleButton'import Input from 'components/Input'import SpinnerIcon from 'components/Spinner'+export interface PostProjectBody {+ repositoryID: number+ installationID: number+ accessToken: string+ branch: string+ path: string+ id: string+}+export interface Props {onRepoSelection: (repoSetup: Pick<PostProjectBody, 'accessToken' | 'installationID' | 'repositoryID'> & { fullName: string, defaultBranch: string, branches?: string[], url: string }) => void+ accessToken?: string}-function Repos({ onRepoSelection }: Props) {- const [accessToken, setAccessToken] = useLocalStorage<string | undefined>('gh_access_token', undefined)+function Repos({ onRepoSelection, accessToken }: Props) {const gitHub = useGitHub(accessToken)const { repos, refetch } = useRepositories(gitHub)const [query, setQuery] = useState<string>()@@ -37,11 +45,11 @@ function Repos({ onRepoSelection }: Props) {const handleEvent = useCallback((event: MessageEvent) => {if (event.data.accessToken) {- setAccessToken(event.data.accessToken)+ // setAccessToken(event.data.accessToken)} else if (event.data.installationID) {refetch()}- }, [setAccessToken, refetch])+ }, [refetch])useListenMessage(handleEvent)async function selectRepository(r: Pick<PostProjectBody, 'installationID' | 'repositoryID'> & { fullName: string, defaultBranch: string, url: string }) {@@ -66,14 +74,8 @@ function Repos({ onRepoSelection }: Props) {})}- async function signWithGitHubOAuth() {- const url = new URL('/login/oauth/authorize', 'https://github.com')- url.searchParams.set('client_id', process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID!)- openPopupModal(url)- }-function configureGitHubApp() {- const url = new URL('https://github.com/apps/devbook-for-github/installations/new')+ const url = new URL('https://github.com/apps/e2b-for-github/installations/new')openPopupModal(url)}@@ -88,7 +90,7 @@ function Repos({ onRepoSelection }: Props) {flex-1`,{ 'bg-white': accessToken },)}>- {!accessToken &&+ {/* {!accessToken &&<div className="flex flex-1 justify-center items-center"><Buttonvariant={Button.variant.Full}@@ -97,7 +99,7 @@ function Repos({ onRepoSelection }: Props) {icon={<GithubIcon />}/></div>- }+ } */}{accessToken &&<div className="flex overflow-hidden flex-col flex-1">@@ -174,10 +176,10 @@ function Repos({ onRepoSelection }: Props) {</div>{accessToken &&<div className="flex justify-between">- <TitleButton+ {/* <TitleButtononClick={signWithGitHubOAuth}text="Reauthenticate GitHub Account"- />+ /> */}<TitleButtononClick={configureGitHubApp}text="Configure GitHub App"diff --git a/github/repoProcessing.ts b/github/repoProcessing.tsdeleted file mode 100644index 68946548a..000000000--- a/github/repoProcessing.ts+++ /dev/null@@ -1,95 +0,0 @@-import { Octokit } from '@octokit/rest'-// import * as toml from '@iarna/toml'-import JSZip from 'jszip'--import path from 'path'--// const envConfigName = 'dbk.toml'--export type AppContentJSON = {- env?: {- id: string,- }- mdx: {- name: string- content: string- }[]- css?: {- name: string- content: string- }[]-}--export interface File {- content: string- name: string-}--export interface EnvConfig {- id: string-}--export async function getRepo({- owner,- repo,- ref,- github,-}: {- owner: string,- repo: string,- ref: string,- github: Octokit,-}) {- const archive = await github.repos.downloadZipballArchive({- accept: 'application/vnd.github+json',- owner,- repo,- ref,- })-- const zip = JSZip()- await zip.loadAsync(archive.data as string)- return zip-}--export async function getAppContentFromRepo({- dir,- repo,-}: {- repo: JSZip,- dir: string,-}): Promise<AppContentJSON | undefined> {- const rootRegex = dir === '.' || dir === './' ? new RegExp('^[^\/]+\/$') : new RegExp(`^[^\/]+\/${dir}\/$`)- const rootDir = repo.folder(rootRegex)-- if (rootDir.length === 0) return- const name = rootDir[0].name- const root = repo.folder(name)-- if (!root) return-- // const envConfigContentRaw = await root.file(envConfigName)?.async('string')- // if (!envConfigContentRaw) {- // throw new Error(`Invalid env config in ${name}`)- // }- // const env = toml.parse(envConfigContentRaw) as unknown as EnvConfig- const mdx = await Promise.all(root.file(/.+.mdx$/).map(f => getFile(f, dir)))- const css = await Promise.all(root.file(/.+.css$/).map(f => getFile(f, dir)))-- return {- // env,- css,- mdx,- }-}--async function getFile(o: JSZip.JSZipObject | null, root: string): Promise<File> {- if (!o) {- throw new Error('Cannot get file')- }-- return {- name: path.relative(root, o.name.split('/').slice(1).join('/')),- content: await o.async('string')- }-}diff --git a/hooks/useRepositories.ts b/hooks/useRepositories.tsindex eca5a0748..7dfbde69a 100644--- a/hooks/useRepositories.ts+++ b/hooks/useRepositories.ts@@ -7,20 +7,25 @@ async function fetchRepos(client: GitHubUserClient) {const repos = await Promise.all(installations.data.installations.map(async i => {- const repos = await client.apps.listInstallationReposForAuthenticatedUser({- installation_id: i.id,- // TODO: Add pagination so we fetch all the repos not just the first 100- per_page: 100,- headers: {- // We have to manually circumvent the caching mechanism here.- // This request is called only when the user changes or when the component mounts, so we don't exceed the GitHub API quotas.- 'If-Modified-Since': 'Sun, 14 Nov 2021 13:42:15 GMT',- },- })- return repos.data.repositories.map(r => ({- ...r,- installation_id: i.id,- }))+ try {+ const repos = await client.apps.listInstallationReposForAuthenticatedUser({+ installation_id: i.id,+ // TODO: Add pagination so we fetch all the repos not just the first 100+ per_page: 100,+ headers: {+ // We have to manually circumvent the caching mechanism here.+ // This request is called only when the user changes or when the component mounts, so we don't exceed the GitHub API quotas.+ 'If-Modified-Since': 'Sun, 14 Nov 2021 13:42:15 GMT',+ },+ })+ return repos.data.repositories.map(r => ({+ ...r,+ installation_id: i.id,+ }))+ } catch (err) {+ console.error(err)+ return []+ }}))return repos.flat()diff --git a/pages/agent/smol-developer.tsx b/pages/agent/smol-developer.tsxindex debb4ba3c..d7ba5c7cb 100644--- a/pages/agent/smol-developer.tsx+++ b/pages/agent/smol-developer.tsx@@ -1,24 +1,20 @@-import { useEffect } from 'react'-import { useSupabaseClient } from '@supabase/auth-helpers-react'+import { useSupabaseClient, useSession } from '@supabase/auth-helpers-react'import { useUser } from '@supabase/auth-helpers-react'import Text from 'components/Text'import Button from 'components/Button'+import Repos from 'components/Repos'function SmolDeveloper() {const supabaseClient = useSupabaseClient()const user = useUser()-- useEffect(function checkUser() {- console.log('user', user)- }, [user])+ const session = useSession()async function signInWithGitHub() {const { data, error } = await supabaseClient.auth.signInWithOAuth({provider: 'github',options: {redirectTo: window.location.href,- scopes: 'repo',}})console.log({ data, error })@@ -35,7 +31,6 @@ function SmolDeveloper() {flex-1flexflex-col- justify-startspace-y-4"><Text@@ -54,6 +49,13 @@ function SmolDeveloper() {onClick={signInWithGitHub}/>)}++ {user &&+ <Repos+ onRepoSelection={(r) => console.log('selected repo', r)}+ accessToken={session?.provider_token || undefined}+ />+ }</div>)}diff --git a/pages/api/github/oauth.ts b/pages/api/github/oauth.tsindex a6cf56908..c01ba9812 100644--- a/pages/api/github/oauth.ts+++ b/pages/api/github/oauth.ts@@ -19,6 +19,8 @@ export default async function handler(return}+ const callbackURL = 'https://ntjfcwpzsxugrykskdgi.supabase.co/auth/v1/callback'+const exchangeURL = new URL('login/oauth/access_token', 'https://github.com')exchangeURL.searchParams.set('client_id', process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID!)exchangeURL.searchParams.set('client_secret', process.env.GITHUB_CLIENT_SECRET!)diff --git a/pages/repo.tsx b/pages/repo.tsxindex a9608f0a2..b5d3620cb 100644--- a/pages/repo.tsx+++ b/pages/repo.tsx@@ -19,12 +19,12 @@ import dynamic from 'next/dynamic'import { PostProjectBody } from 'pages/api/project'import { apps } from 'database'-import Text from 'components/typography/Text'+import Text from 'components/Text'import Input from 'components/Input'import Select from 'components/Select'import { defaultRepoPath } from 'utils/constants'import Button from 'components/Button'-import SpinnerIcon from 'components/icons/Spinner'+import SpinnerIcon from 'components/Spinner'const Repos = dynamic(() => import('components/Repos'), { ssr: false })async function handlePostProject(url: string, { arg }: { arg: PostProjectBody }) {