Initialize git repo in workspace when no GitHub repo is selected (#7904)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Robert Brennan 2025-04-21 18:34:42 -04:00 committed by GitHub
parent b3bd3924a0
commit bf9f2aa7a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 21 deletions

View File

@ -115,18 +115,16 @@ def initialize_repository_for_runtime(
)
provider_tokens = secret_store.provider_tokens if secret_store else None
repo_directory = None
if selected_repository and provider_tokens:
logger.debug(f'Selected repository {selected_repository}.')
repo_directory = call_async_from_sync(
runtime.clone_repo,
GENERAL_TIMEOUT,
provider_tokens,
selected_repository,
None,
)
# Run setup script if it exists
runtime.maybe_run_setup_script()
logger.debug(f'Selected repository {selected_repository}.')
repo_directory = call_async_from_sync(
runtime.clone_or_init_repo,
GENERAL_TIMEOUT,
provider_tokens,
selected_repository,
None,
)
# Run setup script if it exists
runtime.maybe_run_setup_script()
return repo_directory

View File

@ -309,13 +309,28 @@ class Runtime(FileEditRuntimeMixin):
return
self.event_stream.add_event(observation, source) # type: ignore[arg-type]
async def clone_repo(
async def clone_or_init_repo(
self,
git_provider_tokens: PROVIDER_TOKEN_TYPE,
selected_repository: str | Repository,
git_provider_tokens: PROVIDER_TOKEN_TYPE | None,
selected_repository: str | Repository | None,
selected_branch: str | None,
repository_provider: ProviderType = ProviderType.GITHUB,
) -> str:
if not selected_repository:
if self.config.workspace_base:
logger.info(
'In workspace mount mode, not initializing a new git repository.'
)
return ''
logger.debug(
'No repository selected. Initializing a new git repository in the workspace.'
)
action = CmdRunAction(
command='git init',
)
self.run_action(action)
return ''
provider_domains = {
ProviderType.GITHUB: 'github.com',
ProviderType.GITLAB: 'gitlab.com',
@ -327,9 +342,11 @@ class Runtime(FileEditRuntimeMixin):
else selected_repository.git_provider
)
if not git_provider_tokens:
raise RuntimeError('Need git provider tokens to clone repo')
git_token = git_provider_tokens[chosen_provider].token
if not git_token:
raise RuntimeError('Require valid git token to clone repo')
raise RuntimeError('Need a valid git token to clone repo')
domain = provider_domains[chosen_provider]
repository = (

View File

@ -323,11 +323,10 @@ class AgentSession:
)
return False
if selected_repository and git_provider_tokens:
await self.runtime.clone_repo(
git_provider_tokens, selected_repository, selected_branch
)
await call_sync_from_async(self.runtime.maybe_run_setup_script)
await self.runtime.clone_or_init_repo(
git_provider_tokens, selected_repository, selected_branch
)
await call_sync_from_async(self.runtime.maybe_run_setup_script)
self.logger.debug(
f'Runtime initialized with plugins: {[plugin.name for plugin in self.runtime.plugins]}'