Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions scripts/fleet/repo-universe/pull_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
- A `reopened` delta reopens a resolved mirror (with a comment).
- A first run for a repo BASELINES it (the checker's rule): no
mirrors are created for pre-existing history.
- The target repo itself is never mirrored. Its issues already live
there, and mirroring it would mirror every mirror created in it,
one level deeper each cycle. Its state is still recorded.
- A `new` issue that is already closed gets no mirror (an open
mirror found for it is resolved), so a mirror never opens for
finished work.
- A failed fetch is an explicit ERROR line and a non-zero exit; the
repo's persisted state is left untouched and no mirror action is
taken for it, so a failure never masquerades as an empty repo or
Expand Down Expand Up @@ -428,7 +434,14 @@ def ensure_label(self, name: str, color: str, description: str) -> None:
self.planned.append(f"ensure-label {name}")


def run_pull_in(inventory_text: str, state: dict, fetch, store, now: str):
def run_pull_in(
inventory_text: str,
state: dict,
fetch,
store,
now: str,
target_repo: str = TARGET_REPO_DEFAULT,
):
"""Run one pull-in cycle over all watched repos.

Returns (new_state, report_lines, had_error, actions). Pure
Expand Down Expand Up @@ -484,6 +497,10 @@ def run_pull_in(inventory_text: str, state: dict, fetch, store, now: str):
)
lines.append("")
continue
if repo == target_repo:
lines.append("- target repo: its issues live here already; never mirrored")
lines.append("")
continue
repo_actions = _act_on_delta(repo, repo_class, result, store)
actions.extend(repo_actions)
if repo_actions:
Expand All @@ -505,6 +522,10 @@ def _act_on_delta(repo: str, repo_class: str, result: dict, store) -> list[str]:
for issue in sorted(result["new"], key=lambda i: i["number"]):
key = source_key(repo, issue["number"])
existing = store.find_mirror(key)
if issue["state"] == "closed":
if existing is not None and existing["state"] == "open":
actions.append(_resolve(store, existing, key, issue))
continue
if existing is not None:
if existing["state"] == "closed":
store.comment(
Expand Down Expand Up @@ -556,17 +577,19 @@ def _act_on_delta(repo: str, repo_class: str, result: dict, store) -> list[str]:
continue
if existing["state"] == "closed":
continue
store.comment(
existing["number"],
f"Source {key} closed ({issue['html_url']}); resolving this mirror.",
)
store.close(existing["number"])
actions.append(
f"resolved mirror #{existing['number']} for {key} (source closed)"
)
actions.append(_resolve(store, existing, key, issue))
return actions


def _resolve(store, existing: dict, key: str, issue: dict) -> str:
store.comment(
existing["number"],
f"Source {key} closed ({issue['html_url']}); resolving this mirror.",
)
store.close(existing["number"])
return f"resolved mirror #{existing['number']} for {key} (source closed)"


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--inventory", default="doc/fleet/repo-universe.yaml")
Expand Down Expand Up @@ -620,7 +643,7 @@ def main(argv: list[str] | None = None) -> int:
store.ensure_label(name, color, description)
try:
new_state, lines, had_error, actions = run_pull_in(
inventory_text, state, fetch, store, now
inventory_text, state, fetch, store, now, args.target_repo
)
except check_issues.FetchError as exc:
print(f"error: {exc}; no report or state written", file=sys.stderr)
Expand Down
63 changes: 63 additions & 0 deletions scripts/fleet/repo-universe/test_pull_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,69 @@ def test_dry_run_plans_without_mutating(self):
self.assertEqual(store.mirrors, [])
self.assertTrue(any(p.startswith("create:") for p in dry.planned))

def test_target_repo_is_never_mirrored(self):
inventory = INVENTORY + """\
- name: developing-today/code
url: https://github.com/developing-today/code
class: owned
watch: issues
sources: [github]
"""
mapping = {
"dezren39/hatch-personal": [],
"someone/upstream": [],
"developing-today/code": [issue(1, repo="developing-today/code")],
}
store = pull_in.FixtureMirrorStore()
state, _, _, _ = pull_in.run_pull_in(
inventory, {"version": 1, "repos": {}}, fetch_from(mapping), store, NOW1
)
mapping["developing-today/code"].append(
issue(2, "a new issue", repo="developing-today/code")
)
state, lines, had_error, actions = pull_in.run_pull_in(
inventory, state, fetch_from(mapping), store, NOW2
)
self.assertFalse(had_error)
self.assertEqual(actions, [])
self.assertEqual(store.mirrors, [])
self.assertIn("2", state["repos"]["developing-today/code"]["issues"])
self.assertTrue(any("never mirrored" in line for line in lines))
# A different target repo would mirror it as usual.
mapping["developing-today/code"].append(
issue(3, "another", repo="developing-today/code")
)
_, _, _, actions = pull_in.run_pull_in(
inventory, state, fetch_from(mapping), store, NOW3, "elsewhere/tracker"
)
self.assertTrue(any("created mirror" in a for a in actions))

def test_new_issue_already_closed_gets_no_mirror(self):
state, store = baselined_state(
{"dezren39/hatch-personal": [issue(1)], "someone/upstream": []}
)
store.create_mirror(
pull_in.mirror_title("dezren39/hatch-personal", issue(3, "gone")),
pull_in.mirror_body("dezren39/hatch-personal", "owned", issue(3, "gone")),
["source/universe", "universe/owned"],
)
closed = {
"dezren39/hatch-personal": [
issue(1),
issue(2, "done already", state="closed"),
issue(3, "gone", state="closed"),
],
"someone/upstream": [],
}
_, _, had_error, actions = pull_in.run_pull_in(
INVENTORY, state, fetch_from(closed), store, NOW2
)
self.assertFalse(had_error)
self.assertEqual(len(store.mirrors), 1)
self.assertEqual(store.mirrors[0]["state"], "closed")
self.assertEqual(len(actions), 1)
self.assertIn("resolved mirror", actions[0])

def test_inventory_classes_parse(self):
repos = pull_in.parse_inventory_classes(INVENTORY)
by_name = {r["name"]: r["class"] for r in repos}
Expand Down