Skip to content

Commit 66ed826

Browse files
committed
backporting: remove the repo after the agent run
...unless dry_run Signed-off-by: Tomas Tomecek <[email protected]>
1 parent f2e0b0f commit 66ed826

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

beeai/agents/backport_agent.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import logging
33
import os
4+
from shutil import rmtree
45
import subprocess
56
import sys
67
import time
@@ -172,7 +173,8 @@ async def run_with_schema(self, input: TInputSchema) -> TOutputSchema:
172173
requirement._source_tool = None
173174

174175

175-
def prepare_package(package: str, jira_issue: str, dist_git_branch: str, input_schema: InputSchema) -> str:
176+
def prepare_package(package: str, jira_issue: str, dist_git_branch: str,
177+
input_schema: InputSchema) -> tuple[str, str]:
176178
"""
177179
Prepare the package for backporting by cloning the dist-git repository, switching to the appropriate branch,
178180
and downloading the sources.
@@ -210,15 +212,14 @@ def prepare_package(package: str, jira_issue: str, dist_git_branch: str, input_s
210212
raise ValueError(
211213
f"Expected exactly one unpacked source, got {unpacked_sources}"
212214
)
213-
unpacked_source = unpacked_sources[0]
214-
return unpacked_source
215-
215+
return unpacked_sources[0], local_clone
216216

217217
async def main() -> None:
218218
logging.basicConfig(level=logging.INFO)
219219

220220
setup_observability(os.getenv("COLLECTOR_ENDPOINT"))
221221
agent = BackportAgent()
222+
dry_run = os.getenv("DRY_RUN", "False").lower() == "true"
222223

223224
if (
224225
(package := os.getenv("PACKAGE", None))
@@ -233,13 +234,15 @@ async def main() -> None:
233234
jira_issue=jira_issue,
234235
dist_git_branch=branch,
235236
)
236-
unpacked_source = prepare_package(package, jira_issue, branch, input)
237-
input.unpacked_sources = unpacked_source
237+
input.unpacked_sources, local_clone = prepare_package(package, jira_issue, branch, input)
238238
try:
239239
output = await agent.run_with_schema(input)
240-
except Exception:
241-
logger.info(f"Sleeping, you can now debug")
242-
time.sleep(999999)
240+
finally:
241+
if not dry_run:
242+
logger.info(f"Removing {local_clone}")
243+
rmtree(local_clone)
244+
else:
245+
logger.info(f"DRY RUN: Not removing {local_clone}")
243246
logger.info(f"Direct run completed: {output.model_dump_json(indent=4)}")
244247
return
245248

@@ -273,6 +276,8 @@ class Task(BaseModel):
273276
jira_issue=backport_data.jira_issue,
274277
dist_git_branch=backport_data.branch,
275278
)
279+
input.unpacked_sources, local_clone = prepare_package(backport_data.package,
280+
backport_data.jira_issue, backport_data.branch, input)
276281

277282
async def retry(task, error):
278283
task.attempts += 1
@@ -296,7 +301,9 @@ async def retry(task, error):
296301
await retry(
297302
task, ErrorData(details=error, jira_issue=input.jira_issue).model_dump_json()
298303
)
304+
rmtree(local_clone)
299305
else:
306+
rmtree(local_clone)
300307
if output.success:
301308
logger.info(f"Backport successful for {backport_data.jira_issue}, "
302309
f"adding to completed list")

0 commit comments

Comments
 (0)