Skip to content

Commit 0f8b288

Browse files
authored
Sync sandbox runner code (#141)
1 parent de9d6f3 commit 0f8b288

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

src/codegen/runner/sandbox/repo.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import logging
22

3-
from codegen.git.schemas.enums import FetchResult
4-
from codegen.git.utils.branch_sync import BranchSyncResult, fetch_highside_branch, get_highside_origin
3+
from codegen.git.schemas.github import GithubType
54
from codegen.runner.models.codemod import Codemod
5+
from codegen.runner.utils.branch_sync import get_remote_for_github_type
66
from codegen.sdk.codebase.factory.codebase_factory import CodebaseType
77

88
logger = logging.getLogger(__name__)
@@ -22,9 +22,12 @@ def set_up_base_branch(self, base_branch: str | None) -> None:
2222
if self.codebase.op.is_branch_checked_out(base_branch):
2323
return
2424

25-
res = self._pull_highside_to_lowside(base_branch)
26-
if res is BranchSyncResult.SUCCESS:
27-
self.codebase.checkout(branch=base_branch, remote=True)
25+
# fetch the base branch from highside (do not checkout yet)
26+
highside_remote = get_remote_for_github_type(op=self.codebase.op, github_type=GithubType.Github)
27+
self.codebase.op.fetch_remote(highside_remote.name, refspec=f"{base_branch}:{base_branch}")
28+
29+
# checkout the base branch (and possibly sync graph)
30+
self.codebase.checkout(branch=base_branch)
2831

2932
def set_up_head_branch(self, head_branch: str, force_push_head_branch: bool):
3033
"""Set-up head branch by pushing latest highside branch to lowside and fetching the branch (so that it can be checked out later)."""
@@ -43,22 +46,9 @@ def set_up_head_branch(self, head_branch: str, force_push_head_branch: bool):
4346
if force_push_head_branch:
4447
return
4548

46-
res = self._pull_highside_to_lowside(head_branch)
47-
if res is BranchSyncResult.SUCCESS:
48-
self.codebase.op.fetch_remote("origin", refspec=f"{head_branch}:{head_branch}")
49-
50-
def _pull_highside_to_lowside(self, branch_name: str):
51-
"""Grabs the latest highside branch `branch_name` and pushes it to the lowside."""
52-
# Step 1: checkout branch that tracks highside remote
53-
res = fetch_highside_branch(op=self.codebase.op, branch_name=branch_name)
54-
if res == FetchResult.REFSPEC_NOT_FOUND:
55-
return BranchSyncResult.BRANCH_NOT_FOUND
56-
57-
# Step 2: push branch up to lowside
58-
logger.info(f"Pushing branch: {branch_name} from highside to lowside w/ force=False ...")
59-
lowside_origin = self.codebase.op.git_cli.remote("origin")
60-
self.codebase.op.push_changes(remote=lowside_origin, refspec=f"{branch_name}:{branch_name}", force=False)
61-
return BranchSyncResult.SUCCESS
49+
# fetch the head branch from highside (do not checkout yet)
50+
highside_remote = get_remote_for_github_type(op=self.codebase.op, github_type=GithubType.Github)
51+
self.codebase.op.fetch_remote(highside_remote.name, refspec=f"{head_branch}:{head_branch}")
6252

6353
def reset_branch(self, base_branch: str, head_branch: str) -> None:
6454
logger.info(f"Checking out base branch {base_branch} ...")
@@ -76,8 +66,8 @@ def push_changes_to_remote(self, codemod: Codemod, head_branch: str, force_push:
7666
return False
7767

7868
# =====[ Push changes highside ]=====
79-
highside_origin = get_highside_origin(self.codebase.op)
80-
highside_res = self.codebase.op.push_changes(remote=highside_origin, refspec=f"{head_branch}:{head_branch}", force=force_push)
69+
highside_remote = get_remote_for_github_type(op=self.codebase.op, github_type=GithubType.Github)
70+
highside_res = self.codebase.op.push_changes(remote=highside_remote, refspec=f"{head_branch}:{head_branch}", force=force_push)
8171
return not any(push_info.flags & push_info.ERROR for push_info in highside_res)
8272

8373
# TODO: move bunch of codebase git operations into this class.

src/codegen/runner/sandbox/runner.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from git import Commit as GitCommit
66

77
from codegen.git.repo_operator.remote_repo_operator import RemoteRepoOperator
8+
from codegen.git.schemas.github import GithubType
89
from codegen.git.schemas.repo_config import RepoConfig
910
from codegen.runner.models.apis import CreateBranchRequest, CreateBranchResponse, GetDiffRequest, GetDiffResponse
1011
from codegen.runner.models.configs import get_codebase_config
@@ -39,7 +40,7 @@ def __init__(
3940
) -> None:
4041
self.container_id = container_id
4142
self.repo = repo_config
42-
self.op = RemoteRepoOperator(repo_config, base_dir=repo_config.base_dir)
43+
self.op = RemoteRepoOperator(repo_config, base_dir=repo_config.base_dir, github_type=GithubType.Github)
4344
self.commit = self.op.git_cli.head.commit
4445

4546
async def warmup(self) -> None:
@@ -76,13 +77,13 @@ def reset_runner(self) -> None:
7677
self.codebase.checkout(branch=self.codebase.default_branch, create_if_missing=True)
7778

7879
@staticmethod
79-
def _set_sentry_tags(epic_id: int, is_customer: bool) -> None:
80+
def _set_sentry_tags(epic_id: int, is_admin: bool) -> None:
8081
"""Set the sentry tags for a CodemodRun"""
8182
sentry_sdk.set_tag("epic_id", epic_id) # To easily get to the epic in the UI
82-
sentry_sdk.set_tag("is_customer", is_customer) # To filter "prod" level errors, ex if customer hits an error vs an admin
83+
sentry_sdk.set_tag("is_admin", is_admin) # To filter "prod" level errors, ex if customer hits an error vs an admin
8384

8485
async def get_diff(self, request: GetDiffRequest) -> GetDiffResponse:
85-
self._set_sentry_tags(epic_id=request.codemod.epic_id, is_customer=request.codemod.is_customer)
86+
self._set_sentry_tags(epic_id=request.codemod.epic_id, is_admin=request.codemod.is_admin)
8687
custom_scope = {"context": request.codemod.codemod_context} if request.codemod.codemod_context else {}
8788
code_to_exec = create_execute_function_from_codeblock(codeblock=request.codemod.user_code, custom_scope=custom_scope)
8889
session_options = SessionOptions(max_transactions=request.max_transactions, max_seconds=request.max_seconds)
@@ -92,7 +93,7 @@ async def get_diff(self, request: GetDiffRequest) -> GetDiffResponse:
9293
return GetDiffResponse(result=res)
9394

9495
async def create_branch(self, request: CreateBranchRequest) -> CreateBranchResponse:
95-
self._set_sentry_tags(epic_id=request.codemod.epic_id, is_customer=request.codemod.is_customer)
96+
self._set_sentry_tags(epic_id=request.codemod.epic_id, is_admin=request.codemod.is_admin)
9697
custom_scope = {"context": request.codemod.codemod_context} if request.codemod.codemod_context else {}
9798
code_to_exec = create_execute_function_from_codeblock(codeblock=request.codemod.user_code, custom_scope=custom_scope)
9899
branch_config = request.branch_config
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from git.remote import Remote
2+
3+
from codegen.git.configs.constants import HIGHSIDE_REMOTE_NAME, LOWSIDE_REMOTE_NAME
4+
from codegen.git.repo_operator.remote_repo_operator import RemoteRepoOperator
5+
from codegen.git.schemas.github import GithubScope, GithubType
6+
from codegen.git.utils.clone_url import get_authenticated_clone_url_for_repo_config
7+
8+
9+
def get_remote_for_github_type(op: RemoteRepoOperator, github_type: GithubType = GithubType.GithubEnterprise) -> Remote:
10+
if op.github_type == github_type:
11+
return op.git_cli.remote(name="origin")
12+
13+
remote_name = HIGHSIDE_REMOTE_NAME if github_type == GithubType.Github else LOWSIDE_REMOTE_NAME
14+
remote_url = get_authenticated_clone_url_for_repo_config(repo=op.repo_config, github_type=github_type, github_scope=GithubScope.WRITE)
15+
16+
if remote_name in op.git_cli.remotes:
17+
remote = op.git_cli.remote(remote_name)
18+
remote.set_url(remote_url)
19+
else:
20+
remote = op.git_cli.create_remote(remote_name, remote_url)
21+
return remote

0 commit comments

Comments
 (0)