Skip to content

Commit 7cd29d3

Browse files
commit_and_push: check staged content before commit
...and halt if nothing is staged Signed-off-by: Tomas Tomecek <[email protected]> Assisted-by: Cursor(Claude) Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 3a5ed9f commit 7cd29d3

File tree

4 files changed

+71
-50
lines changed

4 files changed

+71
-50
lines changed

beeai/agents/backport_agent.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -255,31 +255,37 @@ async def run_build_agent(state):
255255
return "run_backport_agent"
256256

257257
async def commit_push_and_open_mr(state):
258-
state.merge_request_url = await tasks.commit_push_and_open_mr(
259-
local_clone=state.local_clone,
260-
files_to_commit=["*.spec", f"{state.jira_issue}.patch"],
261-
commit_message=(
262-
f"Fix {state.jira_issue}\n\n"
263-
f"{f'CVE: {state.cve_id}\n' if state.cve_id else ''}"
264-
f"{f'Upstream fix: {state.upstream_fix}\n'}"
265-
f"Resolves: {state.jira_issue}\n\n"
266-
f"This commit was backported {I_AM_JOTNAR}\n\n"
267-
f"Assisted-by: Jotnar\n"
268-
),
269-
fork_url=state.fork_url,
270-
dist_git_branch=state.dist_git_branch,
271-
update_branch=state.update_branch,
272-
mr_title=f"Resolves {state.jira_issue}",
273-
mr_description=(
274-
f"This merge request was created {I_AM_JOTNAR}\n"
275-
f"{CAREFULLY_REVIEW_CHANGES}\n\n"
276-
f"Upstream patch: {state.upstream_fix}\n\n"
277-
"Backporting steps:\n\n"
278-
f"{state.backport_result.status}"
279-
),
280-
available_tools=gateway_tools,
281-
commit_only=dry_run,
282-
)
258+
try:
259+
state.merge_request_url = await tasks.commit_push_and_open_mr(
260+
local_clone=state.local_clone,
261+
files_to_commit=["*.spec", f"{state.jira_issue}.patch"],
262+
commit_message=(
263+
f"Fix {state.jira_issue}\n\n"
264+
f"{f'CVE: {state.cve_id}\n' if state.cve_id else ''}"
265+
f"{f'Upstream fix: {state.upstream_fix}\n'}"
266+
f"Resolves: {state.jira_issue}\n\n"
267+
f"This commit was backported {I_AM_JOTNAR}\n\n"
268+
f"Assisted-by: Jotnar\n"
269+
),
270+
fork_url=state.fork_url,
271+
dist_git_branch=state.dist_git_branch,
272+
update_branch=state.update_branch,
273+
mr_title=f"Resolves {state.jira_issue}",
274+
mr_description=(
275+
f"This merge request was created {I_AM_JOTNAR}\n"
276+
f"{CAREFULLY_REVIEW_CHANGES}\n\n"
277+
f"Upstream patch: {state.upstream_fix}\n\n"
278+
"Backporting steps:\n\n"
279+
f"{state.backport_result.status}"
280+
),
281+
available_tools=gateway_tools,
282+
commit_only=dry_run,
283+
)
284+
except Exception as e:
285+
logger.warning(f"Error committing and opening MR: {e}")
286+
state.merge_request_url = None
287+
state.backport_result.success = False
288+
state.backport_result.error = f"Could not commit and open MR: {e}"
283289
return "comment_in_jira"
284290

285291
async def comment_in_jira(state):

beeai/agents/rebase_agent.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,35 @@ async def commit_push_and_open_mr(state):
256256
# Use files specified by rebase agent, fallback to *.spec if none specified
257257
files_to_git_add = state.rebase_result.files_to_git_add or ["*.spec"]
258258

259-
state.merge_request_url = await tasks.commit_push_and_open_mr(
260-
local_clone=state.local_clone,
261-
files_to_commit=files_to_git_add,
262-
commit_message=(
263-
f"Rebase to version {state.version}\n\n"
264-
f"Resolves: {state.jira_issue}\n\n"
265-
f"This commit was created {I_AM_JOTNAR}\n\n"
266-
f"Assisted-by: Jotnar\n"
267-
),
268-
fork_url=state.fork_url,
269-
dist_git_branch=state.dist_git_branch,
270-
update_branch=state.update_branch,
271-
mr_title=f"Update to version {state.version}",
272-
mr_description=(
273-
f"This merge request was created {I_AM_JOTNAR}\n"
274-
f"{CAREFULLY_REVIEW_CHANGES}\n\n"
275-
f"Resolves: {state.jira_issue}\n\n"
276-
"Status of the rebase:\n\n"
277-
f"{state.rebase_result.status}"
278-
),
279-
available_tools=gateway_tools,
280-
commit_only=dry_run,
281-
)
259+
try:
260+
state.merge_request_url = await tasks.commit_push_and_open_mr(
261+
local_clone=state.local_clone,
262+
files_to_commit=files_to_git_add,
263+
commit_message=(
264+
f"Rebase to version {state.version}\n\n"
265+
f"Resolves: {state.jira_issue}\n\n"
266+
f"This commit was created {I_AM_JOTNAR}\n\n"
267+
f"Assisted-by: Jotnar\n"
268+
),
269+
fork_url=state.fork_url,
270+
dist_git_branch=state.dist_git_branch,
271+
update_branch=state.update_branch,
272+
mr_title=f"Update to version {state.version}",
273+
mr_description=(
274+
f"This merge request was created {I_AM_JOTNAR}\n"
275+
f"{CAREFULLY_REVIEW_CHANGES}\n\n"
276+
f"Resolves: {state.jira_issue}\n\n"
277+
"Status of the rebase:\n\n"
278+
f"{state.rebase_result.status}"
279+
),
280+
available_tools=gateway_tools,
281+
commit_only=dry_run,
282+
)
283+
except Exception as e:
284+
logger.warning(f"Error committing and opening MR: {e}")
285+
state.merge_request_url = None
286+
state.rebase_result.success = False
287+
state.rebase_result.error = f"Could not commit and open MR: {e}"
282288
return "comment_in_jira"
283289

284290
async def comment_in_jira(state):

beeai/agents/tasks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from common.utils import is_cs_branch
1111
from constants import BRANCH_PREFIX, JIRA_COMMENT_TEMPLATE, JiraLabels
12-
from utils import check_subprocess, run_tool, mcp_tools
12+
from utils import check_subprocess, run_subprocess, run_tool, mcp_tools
1313

1414
logger = logging.getLogger(__name__)
1515

@@ -69,7 +69,15 @@ async def commit_push_and_open_mr(
6969
files_to_commit = [files_to_commit]
7070
for path in itertools.chain(*(local_clone.glob(pat) for pat in files_to_commit)):
7171
await check_subprocess(["git", "add", str(path)], cwd=local_clone)
72-
# TODO: check for empty commit (the command below will fail anyway, but we need to handle this somehow)
72+
# Check if any files are staged before committing, if none, bail
73+
exit_code, _, _ = await run_subprocess(
74+
["git", "diff", "--cached", "--quiet"],
75+
cwd=local_clone,
76+
)
77+
# 1 = staged, 0 = none staged
78+
if exit_code == 0:
79+
logger.info("No files staged for commit, halting.")
80+
raise RuntimeError("No files staged for commit, halting.")
7381
await check_subprocess(["git", "commit", "-m", commit_message], cwd=local_clone)
7482
if commit_only:
7583
return None

beeai/agents/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ async def run_subprocess(
4040
cwd: Path | None = None,
4141
env: dict[str, str] | None = None,
4242
) -> Tuple[int, str | None, str | None]:
43+
"""Run a subprocess and return the exit code, stdout, and stderr."""
4344
kwargs = {
4445
"stdout": asyncio.subprocess.PIPE,
4546
"stderr": asyncio.subprocess.PIPE,

0 commit comments

Comments
 (0)