|
| 1 | +"""Shared conflict handling helpers for sync and restack workflows. |
| 2 | +
|
| 3 | +Provides the common logic that both the sync PR creation and restack |
| 4 | +workflows use when a cherry-pick or rebase produces conflicts: |
| 5 | +
|
| 6 | +- Writing the modify/delete manifest and invoking the conflict-resolution |
| 7 | + agent (``run_agent_with_manifest``). |
| 8 | +- Adding the ``repo-sync:conflict`` label (``add_conflict_label``). |
| 9 | +- Determining and assigning a reviewer with the ``Repo-Sync-Assigned`` |
| 10 | + trailer (``assign_conflict_reviewer``). |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import json |
| 16 | +import logging |
| 17 | +import os |
| 18 | +from datetime import datetime, timezone |
| 19 | + |
| 20 | +from repo_sync.stack.gh_ops import GhOps |
| 21 | +from repo_sync.stack.git_ops import GitOps |
| 22 | +from repo_sync.stack.trailers import ( |
| 23 | + append_trailer, |
| 24 | + format_assigned_trailer, |
| 25 | +) |
| 26 | +from repo_sync.workflows.sync import determine_sync_reviewer |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +def run_agent_with_manifest( |
| 32 | + git: GitOps, |
| 33 | + md_conflicts: list[dict[str, str]], |
| 34 | + manifest_context: str, |
| 35 | + agent_context: str, |
| 36 | +) -> bool: |
| 37 | + """Write the modify/delete manifest, invoke the agent, and clean up. |
| 38 | +
|
| 39 | + The manifest (``.repo-sync-conflicts.json``) is a transient file |
| 40 | + consumed by the conflict-resolution agent. It is never committed. |
| 41 | +
|
| 42 | + Must be called *after* the conflict markers have been committed (so |
| 43 | + the agent sees committed markers, not a paused git operation). |
| 44 | +
|
| 45 | + Returns True if the agent resolved the conflicts, False otherwise. |
| 46 | + """ |
| 47 | + manifest_path = os.path.join(git.repo_dir, ".repo-sync-conflicts.json") |
| 48 | + |
| 49 | + if md_conflicts: |
| 50 | + logger.info( |
| 51 | + "Detected %d modify/delete conflict(s): %s", |
| 52 | + len(md_conflicts), |
| 53 | + [c["path"] for c in md_conflicts], |
| 54 | + ) |
| 55 | + manifest = { |
| 56 | + "context": manifest_context, |
| 57 | + "modify_delete_conflicts": md_conflicts, |
| 58 | + } |
| 59 | + with open(manifest_path, "w") as f: |
| 60 | + json.dump(manifest, f, indent=2) |
| 61 | + f.write("\n") |
| 62 | + |
| 63 | + from repo_sync.workflows.agent import run_conflict_resolution_agent |
| 64 | + |
| 65 | + agent_succeeded = run_conflict_resolution_agent( |
| 66 | + repo_dir=git.repo_dir, |
| 67 | + context=agent_context, |
| 68 | + ) |
| 69 | + |
| 70 | + # Clean up the manifest if the agent left it behind. |
| 71 | + if os.path.exists(manifest_path): |
| 72 | + os.remove(manifest_path) |
| 73 | + |
| 74 | + if agent_succeeded: |
| 75 | + logger.info("Conflict-resolution agent succeeded.") |
| 76 | + else: |
| 77 | + logger.warning( |
| 78 | + "Conflict-resolution agent did not resolve conflicts. " |
| 79 | + "PR will contain raw conflict markers." |
| 80 | + ) |
| 81 | + |
| 82 | + return agent_succeeded |
| 83 | + |
| 84 | + |
| 85 | +def add_conflict_label(gh: GhOps, pr_number: int) -> None: |
| 86 | + """Create (if needed) and apply the ``repo-sync:conflict`` label.""" |
| 87 | + gh._run( |
| 88 | + ["label", "create", "repo-sync:conflict", |
| 89 | + "--color", "D93F0B", |
| 90 | + "--description", "Sync PR has merge conflicts", |
| 91 | + "--repo", gh.repo], |
| 92 | + check=False, |
| 93 | + ) |
| 94 | + gh._run( |
| 95 | + ["pr", "edit", str(pr_number), "--repo", gh.repo, |
| 96 | + "--add-label", "repo-sync:conflict"], |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def assign_conflict_reviewer( |
| 101 | + gh: GhOps, |
| 102 | + pr_number: int, |
| 103 | + source_repo: str | None, |
| 104 | + source_sha: str | None, |
| 105 | + escalate_to: str, |
| 106 | +) -> str: |
| 107 | + """Determine a reviewer, assign them, and append the Assigned trailer. |
| 108 | +
|
| 109 | + Looks up the original commit author via ``determine_sync_reviewer`` |
| 110 | + when *source_repo* and *source_sha* are provided; falls back to |
| 111 | + *escalate_to* otherwise. |
| 112 | +
|
| 113 | + Returns the reviewer login or team slug. |
| 114 | + """ |
| 115 | + if source_repo and source_sha: |
| 116 | + source_gh = GhOps(source_repo, token=os.environ.get("GH_TOKEN")) |
| 117 | + reviewer = determine_sync_reviewer( |
| 118 | + source_gh=source_gh, |
| 119 | + source_sha=source_sha, |
| 120 | + fallback_team=escalate_to, |
| 121 | + ) |
| 122 | + else: |
| 123 | + reviewer = escalate_to |
| 124 | + |
| 125 | + # Request review. |
| 126 | + gh._run( |
| 127 | + ["pr", "edit", str(pr_number), "--repo", gh.repo, |
| 128 | + "--add-reviewer", reviewer], |
| 129 | + check=False, |
| 130 | + ) |
| 131 | + |
| 132 | + # Append Repo-Sync-Assigned trailer to the PR body. |
| 133 | + assigned_trailer = format_assigned_trailer( |
| 134 | + reviewer, datetime.now(timezone.utc), |
| 135 | + ) |
| 136 | + current_body = gh._run( |
| 137 | + ["pr", "view", str(pr_number), "--repo", gh.repo, |
| 138 | + "--json", "body", "--jq", ".body"], |
| 139 | + check=False, |
| 140 | + ) or "" |
| 141 | + updated_body = append_trailer(current_body, assigned_trailer) |
| 142 | + gh.update_pr_body(pr_number, updated_body) |
| 143 | + |
| 144 | + logger.info("Assigned reviewer %s on PR #%d.", reviewer, pr_number) |
| 145 | + return reviewer |
0 commit comments