Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Select new repo if we have permissions

Vasek Mlejnskyf9067ebca76c
diff --git a/components/SelectRepository/ExistingRepositories/RepositoriesList.tsx b/components/SelectRepository/ExistingRepositories/RepositoriesList.tsx
index 5ffb6191b..5090ea584 100644
--- a/components/SelectRepository/ExistingRepositories/RepositoriesList.tsx
+++ b/components/SelectRepository/ExistingRepositories/RepositoriesList.tsx
@@ -3,16 +3,10 @@ import {
} from 'lucide-react'
export interface Props {
- repos: {
- id: number
- owner: string
- name: string
- language?: string
- }[]
- onRepoSelection: (repoID: number) => void
+ repos: any[]
+ onRepoSelection: (repo: any) => void
}
-
function RepositoriesList({
repos,
onRepoSelection,
@@ -24,7 +18,7 @@ function RepositoriesList({
<div className="flex flex-col items-start justify-start">
<div className="flex items-center space-x-1">
<Github color="#fff" size={14} />
- <p className="text-xs leading-6 text-white">{r.owner}</p>
+ <p className="text-xs leading-6 text-white">{r.owner.login}</p>
<p className="text-xs text-white">/</p>
<p className="text-sm font-semibold leading-6 text-white">{r.name}</p>
</div>
@@ -35,7 +29,7 @@ function RepositoriesList({
<button
type="button"
className="rounded bg-white/10 px-2 py-1 text-sm font-medium text-white shadow-sm hover:bg-white/20"
- onClick={() => onRepoSelection(r.id)}
+ onClick={() => onRepoSelection(r)}
>
Select
</button>
diff --git a/components/SelectRepository/ExistingRepositories/index.tsx b/components/SelectRepository/ExistingRepositories/index.tsx
index b00f293ca..3f198fbef 100644
--- a/components/SelectRepository/ExistingRepositories/index.tsx
+++ b/components/SelectRepository/ExistingRepositories/index.tsx
@@ -1,6 +1,3 @@
-
-
-
import ConfigureGitHubButton from 'components/ConfigureGitHubButton'
import SpinnerIcon from 'components/Spinner'
import RepositoriesList from './RepositoriesList'
@@ -9,7 +6,7 @@ export interface Props {
accessToken?: string
repos?: any[]
onConfigureGitHubAppClick: (e: any) => void
- onRepoSelection: (repoID: number) => void
+ onRepoSelection: (repo: any) => void
}
function ExistingRepositories({
@@ -46,12 +43,7 @@ function ExistingRepositories({
)}
{repos && repos.length > 0 && (
<RepositoriesList
- repos={repos.sort((a, b) => (new Date(b.updated_at) as any) - (new Date(a.updated_at) as any)).map(r => ({
- id: r.id,
- owner: r.owner.login,
- name: r.name,
- language: r.language,
- }))}
+ repos={repos.sort((a, b) => (new Date(b.updated_at) as any) - (new Date(a.updated_at) as any))}
onRepoSelection={onRepoSelection}
/>
)}
diff --git a/components/SelectRepository/NewRepository/RepoAccountSelect.tsx b/components/SelectRepository/NewRepository/RepoAccountSelect.tsx
index 1c5ccdac3..0e9d412d5 100644
--- a/components/SelectRepository/NewRepository/RepoAccountSelect.tsx
+++ b/components/SelectRepository/NewRepository/RepoAccountSelect.tsx
@@ -17,7 +17,7 @@ export interface GitHubAccount {
export interface Props {
accounts: GitHubAccount[]
- selectedAccount?: GitHubAccount
+ selectedAccount: GitHubAccount | null
onSelectedAccountChange: (account: GitHubAccount) => void
onAddGithubAccountClick: () => void
}
diff --git a/components/SelectRepository/NewRepository/index.tsx b/components/SelectRepository/NewRepository/index.tsx
index e4d74ffca..64b56f002 100644
--- a/components/SelectRepository/NewRepository/index.tsx
+++ b/components/SelectRepository/NewRepository/index.tsx
@@ -1,6 +1,6 @@
import {
- useState,
useEffect,
+ useReducer,
} from 'react'
import clsx from 'clsx'
@@ -14,61 +14,69 @@ import { fetchRepos } from 'hooks/useRepositories'
import RepoAccountSelect, { GitHubAccount } from './RepoAccountSelect'
import RepoNameInput from './RepoNameInput'
+import {
+ creationReducer,
+ ActionType,
+} from './newRepoState'
export interface Props {
accessToken?: string
accounts: GitHubAccount[]
onConfigureGitHubAppClick: (e: any) => void
+ onRepoSelection: (repo: any) => void
}
+
function NewRepository({
accessToken,
accounts,
onConfigureGitHubAppClick,
+ onRepoSelection,
}: Props) {
- const [name, setName] = useState('')
const ghClient = useGitHubClient(accessToken)
- const [selectedAccount, setSelectedAccount] = useState<GitHubAccount | undefined>()
- const [isLoading, setIsLoading] = useState(false)
- const [error, setError] = useState('')
+ const [state, dispatch] = useReducer(creationReducer, {
+ name: '',
+ account: null,
+ isCreating: false,
+ })
async function handleCreateClick() {
- if (!selectedAccount || !accessToken || !ghClient) return
- if (!name) return
- if (isLoading) return
+ if (!accessToken || !ghClient) return
+ if (!state.account) return
+ if (!state.name) return
+ if (state.isCreating) return
try {
// Create a repository
- setError('')
- setIsLoading(true)
+ dispatch({ type: ActionType.Create, payload: {} })
const { id: newRepoID } = await createRepo({
client: ghClient,
- org: selectedAccount.isOrg ? selectedAccount.name : undefined,
- name,
+ org: state.account.isOrg ? state.account.name : undefined,
+ name: state.name,
})
-
// Check if we have permissions to the new repository,
// if not - present UI that asks user for a permission to the new repository
- // if we do - select the new repo
+ // if yes - select the new repo
const repos = await fetchRepos(ghClient)
+ // Check if newly created repo is in the list of repos we have access to.
const newRepo = repos.find(r => r.id === newRepoID)
+
if (newRepo) {
- console.log('HAS ACCESS TO REPO')
- //onRepoSelection(newRepo)
+ dispatch({ type: ActionType.Success, payload: {} })
+ onRepoSelection(newRepo)
} else {
// TODO
- console.log('DO NOT HAVE ACCESS TO REPO')
+ console.log('DO NOT HAVE ACCESS TO REPO', newRepo)
}
} catch (err: any) {
console.error('Error creating repository', err)
- setError(err.message)
- } finally {
- setIsLoading(false)
+ dispatch({ type: ActionType.Fail, payload: { error: err } })
}
}
useEffect(function selectDefaultAccount() {
- if (accounts.length > 0) setSelectedAccount(accounts[0])
+ if (accounts.length === 0) return
+ dispatch({ type: ActionType.SelectAccount, payload: { account: accounts[0] } })
}, [accounts])
return (
@@ -93,31 +101,31 @@ function NewRepository({
<>
<RepoAccountSelect
accounts={accounts}
- selectedAccount={selectedAccount}
- onSelectedAccountChange={setSelectedAccount}
+ selectedAccount={state.account}
+ onSelectedAccountChange={acc => dispatch({ type: ActionType.SelectAccount, payload: { account: acc } })}
onAddGithubAccountClick={configureGitHubApp}
/>
<RepoNameInput
- value={name}
- onChange={e => setName(e.target.value)}
+ value={state.name}
+ onChange={e => dispatch({ type: ActionType.UpdateName, payload: { name: e.target.value } })}
/>
<button
className="w-8 min-w-[64px] h-[24px] min-h-[24px] flex items-center justify-center rounded bg-white/10 px-2 py-1 text-sm text-white font-medium hover:bg-white/20"
onClick={handleCreateClick}
>
- {isLoading ? (
+ {state.isCreating ? (
<SpinnerIcon />
) : (
<span>Create</span>
)}
</button>
- {error && (
+ {state.error && (
<AlertError
title="Error creating repository"
- infoItems={[error]}
+ infoItems={[state.error.message]}
/>
)}
</>
diff --git a/components/SelectRepository/NewRepository/newRepoState.ts b/components/SelectRepository/NewRepository/newRepoState.ts
new file mode 100644
index 000000000..1e0a40525
--- /dev/null
+++ b/components/SelectRepository/NewRepository/newRepoState.ts
@@ -0,0 +1,118 @@
+import { GitHubAccount } from './RepoAccountSelect'
+
+export enum ActionType {
+ Initial,
+ SelectAccount,
+ UpdateName,
+ Create,
+ Fail,
+ Success,
+ RequestPermissions,
+ SelectRepo,
+}
+
+export interface Create {
+ type: ActionType.Create
+ payload: {}
+}
+
+export interface Success {
+ type: ActionType.Success
+ payload: {}
+}
+
+export interface SelectAccount {
+ type: ActionType.SelectAccount
+ payload: {
+ account: GitHubAccount
+ }
+}
+
+export interface UpdateName {
+ type: ActionType.UpdateName
+ payload: {
+ name: string
+ }
+}
+
+export interface Fail {
+ type: ActionType.Fail
+ payload: {
+ error: Error
+ }
+}
+
+export interface RequestPermissions {
+ type: ActionType.RequestPermissions
+ payload: {}
+}
+
+export interface SelectRepo {
+ type: ActionType.SelectRepo;
+ payload: {
+ repo: any
+ }
+}
+
+export type Action = Create |
+ Success |
+ UpdateName |
+ SelectAccount |
+ Fail |
+ RequestPermissions |
+ SelectRepo
+
+export interface State {
+ name: string
+ account: GitHubAccount | null
+ isCreating: boolean
+ error?: Error
+ // TODO: Maybe pass the whole repo?
+ selectedRepo?: number
+}
+
+export function creationReducer(state: State, action: Action) {
+ const { type, payload } = action
+ switch (type) {
+ case ActionType.Create:
+ return {
+ ...state,
+ isCreating: true,
+ error: undefined,
+ }
+ case ActionType.Success:
+ return {
+ ...state,
+ isCreating: false,
+ }
+ case ActionType.Fail:
+ return {
+ ...state,
+ error: payload.error,
+ isCreating: false,
+ }
+ case ActionType.UpdateName:
+ return {
+ ...state,
+ name: payload.name,
+ }
+ case ActionType.SelectAccount:
+ return {
+ ...state,
+ account: payload.account,
+ }
+ case ActionType.RequestPermissions:
+ return {
+ ...state,
+ }
+ case ActionType.SelectRepo:
+ return {
+ ...state,
+ // TODO: Maybe pass the whole repo?
+ selectedRepo: payload.repo,
+ }
+ default:
+ return state
+ }
+}
+
diff --git a/components/SelectRepository/index.tsx b/components/SelectRepository/index.tsx
index 10a6b111f..4ec71944a 100644
--- a/components/SelectRepository/index.tsx
+++ b/components/SelectRepository/index.tsx
@@ -21,14 +21,12 @@ import { GitHubAccount } from './NewRepository/RepoAccountSelect'
export interface Props {
repos?: any[]
accessToken?: string
- selectedRepositoryID?: number
- onRepoSelection: (repoID: number) => void
+ onRepoSelection: (repo: any) => void
}
function SelectRepository({
repos,
accessToken,
- selectedRepositoryID,
onRepoSelection,
}: Props) {
const user = useUser()
@@ -92,7 +90,7 @@ function SelectRepository({
accounts={githubAccounts}
accessToken={accessToken}
onConfigureGitHubAppClick={configureGitHubApp}
- // onRepoSelection={onRepoSelection}
+ onRepoSelection={onRepoSelection}
/>
)}
<button
diff --git a/pages/agent/smol-developer/setup.tsx b/pages/agent/smol-developer/setup.tsx
index 27656212e..558d239c8 100644
--- a/pages/agent/smol-developer/setup.tsx
+++ b/pages/agent/smol-developer/setup.tsx
@@ -85,7 +85,7 @@ async function handlePostAgent(url: string, { arg }: { arg: PostAgentBody }) {
function Setup() {
const supabaseClient = useSupabaseClient()
const [currentStep, setCurrentStep] = useState(0)
- const [selectedRepositoryID, setSelectedRepositoryID] = useState<number>()
+ const [selectedRepository, setSelectedRepository] = useState<any | undefined>()
// const supabaseClient = useSupabaseClient()
// const session = useSession()
// const sessionCtx = useSessionContext()
@@ -127,8 +127,9 @@ function Setup() {
// })
// }
- function handleRepoSelection(repoID: number) {
- setSelectedRepositoryID(repoID)
+ function handleRepoSelection(repo: any) {
+ console.log('SELECTED REPO', repo)
+ setSelectedRepository(repo)
setCurrentStep(val => {
const newVal = val + 1
steps[val].status = 'complete'
@@ -160,7 +161,6 @@ function Setup() {
{currentStep === 0 ? (
<SelectRepository
repos={repos}
- selectedRepositoryID={selectedRepositoryID}
onRepoSelection={handleRepoSelection}
accessToken={accessToken}
/>