Skip to content

Commit c63c3ab

Browse files
committed
branch_worker: helper renames
A couple of helper functions refered to "series/123" part of a branch name ("series/123=>target") as "base" which is quite confusing. Rename relevant functions and vars to make them more clear. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent 55c868f commit c63c3ab

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

kernel_patches_daemon/branch_worker.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,21 +398,22 @@ def create_color_labels(labels_cfg: Dict[str, str], repo: Repository) -> None:
398398
repo.create_label(name=label, color=color)
399399

400400

401-
def get_base_branch_from_ref(ref: str) -> str:
402-
return ref.split(HEAD_BASE_SEPARATOR)[0]
401+
def parse_pr_ref(ref: str) -> List[str]:
402+
# "series123=>target" -> ["series123", "target"]
403+
return ref.split(HEAD_BASE_SEPARATOR)
403404

404405

405-
def has_same_base_different_remote(ref: str, other_ref: str) -> bool:
406+
def same_series_different_remote(ref: str, other_ref: str) -> bool:
406407
if ref == other_ref:
407408
return False
409+
series = parse_pr_ref(ref)[0]
410+
other_series = parse_pr_ref(other_ref)[0]
411+
return series == other_series
408412

409-
base = get_base_branch_from_ref(ref)
410-
other_base = get_base_branch_from_ref(other_ref)
411413

412-
if base != other_base:
413-
return False
414-
415-
return True
414+
def series_id_from_ref(ref: str) -> int:
415+
series_str = parse_pr_ref(ref)[0]
416+
return int(series_str.split("/")[1])
416417

417418

418419
def _reset_repo(repo, branch: str) -> None:

kernel_patches_daemon/github_sync.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from github.PullRequest import PullRequest
1616
from kernel_patches_daemon.branch_worker import (
1717
BranchWorker,
18-
get_base_branch_from_ref,
19-
has_same_base_different_remote,
18+
same_series_different_remote,
19+
series_id_from_ref,
2020
HEAD_BASE_SEPARATOR,
2121
NewPRWithNoChangeException,
2222
)
@@ -143,20 +143,20 @@ async def get_mapped_branches(self, series: Series) -> List[str]:
143143
logging.info(f"Mapped to default branch order: {mapped_branches}")
144144
return mapped_branches
145145

146-
def close_existing_prs_with_same_base(
146+
def close_existing_prs_for_series(
147147
self, workers: Sequence["BranchWorker"], pr: PullRequest
148148
) -> None:
149-
"""Close existing pull requests with the same base, but different remote branch
149+
"""Close existing pull requests for the same series, but different target branch
150150
151151
For given pull request, find all other pull requests with
152-
the same base, but different remote branch and close them.
152+
the same series name, but different remote branch and close them.
153153
"""
154154

155155
prs_to_close = [
156156
existing_pr
157157
for worker in workers
158158
for existing_pr in worker.prs.values()
159-
if has_same_base_different_remote(pr.head.ref, existing_pr.head.ref)
159+
if same_series_different_remote(pr.head.ref, existing_pr.head.ref)
160160
]
161161
# Remove matching PRs from other workers
162162
for pr_to_close in prs_to_close:
@@ -170,7 +170,7 @@ def close_existing_prs_with_same_base(
170170
del worker.prs[pr_to_close.title]
171171

172172
async def checkout_and_patch_safe(
173-
self, worker, branch_name: str, series_to_apply: Series
173+
self, worker: BranchWorker, branch_name: str, series_to_apply: Series
174174
) -> Optional[PullRequest]:
175175
try:
176176
self.increment_counter("all_known_subjects")
@@ -221,6 +221,7 @@ async def sync_relevant_subject(self, subject: Subject) -> None:
221221
continue
222222
else:
223223
logging.info(msg + "no more next, staying.")
224+
224225
logging.info(f"Choosing branch {branch} to create/update PR.")
225226
pr = await self.checkout_and_patch_safe(worker, pr_branch_name, series)
226227
if pr is None:
@@ -231,7 +232,7 @@ async def sync_relevant_subject(self, subject: Subject) -> None:
231232
)
232233
await worker.sync_checks(pr, series)
233234
# Close out other PRs if exists
234-
self.close_existing_prs_with_same_base(list(self.workers.values()), pr)
235+
self.close_existing_prs_for_series(list(self.workers.values()), pr)
235236

236237
break
237238
pass
@@ -289,7 +290,7 @@ async def sync_patches(self) -> None:
289290
if "/" not in pr.head.ref or HEAD_BASE_SEPARATOR not in pr.head.ref:
290291
continue
291292

292-
series_id = int(get_base_branch_from_ref(pr.head.ref.split("/")[1]))
293+
series_id = series_id_from_ref(pr.head.ref)
293294
series = await self.pw.get_series_by_id(series_id)
294295
subject = self.pw.get_subject_by_series(series)
295296
if subject_name != subject.subject:

tests/test_branch_worker.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
email_in_submitter_allowlist,
3535
EmailBodyContext,
3636
furnish_ci_email_body,
37-
has_same_base_different_remote,
37+
same_series_different_remote,
3838
HEAD_BASE_SEPARATOR,
3939
temporary_patch_file,
4040
UPSTREAM_REMOTE_NAME,
@@ -869,21 +869,23 @@ def test_create_color_labels(self) -> None:
869869
repo_mock.create_label.assert_not_called()
870870
repo_label_mock.edit.assert_called_once_with(name="label", color="00000")
871871

872-
def test_has_same_base_different_remote(self) -> None:
873-
with self.subTest("same_base_different_remote"):
872+
def same_series_different_remote(self) -> None:
873+
with self.subTest("same_series_different_remote"):
874874
self.assertTrue(
875-
has_same_base_different_remote("base=>remote", "base=>other_remote")
875+
same_series_different_remote(
876+
"series/1=>remote", "series/1=>other_remote"
877+
)
876878
)
877879

878-
with self.subTest("different_base_same_remote"):
880+
with self.subTest("different_series_same_remote"):
879881
self.assertFalse(
880-
has_same_base_different_remote("base=>remote", "other_base=>remote")
882+
same_series_different_remote("series/1=>remote", "series/2=>remote")
881883
)
882884

883-
with self.subTest("different_base_different_remote"):
885+
with self.subTest("different_series_different_remote"):
884886
self.assertFalse(
885-
has_same_base_different_remote(
886-
"base=>remote", "other_base=>other_remote"
887+
same_series_different_remote(
888+
"series/1=>remote", "series/2=>other_remote"
887889
)
888890
)
889891

tests/test_github_sync.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class TestCase:
118118
gh.workers[TEST_BRANCH].ci_repo_dir.startswith(case.prefix),
119119
)
120120

121-
def test_close_existing_prs_with_same_base(self) -> None:
121+
def test_close_existing_prs_for_series(self) -> None:
122122
matching_pr_mock = MagicMock()
123123
matching_pr_mock.title = "matching"
124124
matching_pr_mock.head.ref = "test_branch=>remote_branch"
@@ -137,7 +137,7 @@ def test_close_existing_prs_with_same_base(self) -> None:
137137
input_pr_mock.head.ref = "test_branch=>other_remote_branch"
138138

139139
workers = [copy.copy(branch_worker_mock) for _ in range(2)]
140-
self._gh.close_existing_prs_with_same_base(workers, input_pr_mock)
140+
self._gh.close_existing_prs_for_series(workers, input_pr_mock)
141141
for worker in workers:
142142
self.assertEqual(len(worker.prs), 1)
143143
self.assertTrue("irrelevant" in worker.prs)
@@ -212,7 +212,7 @@ async def test_sync_relevant_subject_success_first_branch(self) -> None:
212212
pr_mock.head.ref = series_branch_name
213213

214214
self._gh.checkout_and_patch_safe = AsyncMock(return_value=pr_mock)
215-
self._gh.close_existing_prs_with_same_base = MagicMock()
215+
self._gh.close_existing_prs_for_series = MagicMock()
216216

217217
worker_mock = self._gh.workers[TEST_BRANCH]
218218
worker_mock.sync_checks = AsyncMock()
@@ -229,7 +229,7 @@ async def test_sync_relevant_subject_success_first_branch(self) -> None:
229229
worker_mock, series_branch_name, series_mock
230230
)
231231
worker_mock.sync_checks.assert_called_once_with(pr_mock, series_mock)
232-
self._gh.close_existing_prs_with_same_base.assert_called_once_with(
232+
self._gh.close_existing_prs_for_series.assert_called_once_with(
233233
list(self._gh.workers.values()), pr_mock
234234
)
235235

@@ -246,7 +246,7 @@ async def test_sync_relevant_subject_success_second_branch(self) -> None:
246246
pr_mock.head.ref = good_branch_name
247247

248248
self._gh.checkout_and_patch_safe = AsyncMock(return_value=pr_mock)
249-
self._gh.close_existing_prs_with_same_base = MagicMock()
249+
self._gh.close_existing_prs_for_series = MagicMock()
250250

251251
bad_worker_mock = self._gh.workers[TEST_BRANCH]
252252
bad_worker_mock.sync_checks = AsyncMock()
@@ -274,7 +274,7 @@ async def test_sync_relevant_subject_success_second_branch(self) -> None:
274274
good_worker_mock, good_branch_name, series_mock
275275
)
276276
good_worker_mock.sync_checks.assert_called_once_with(pr_mock, series_mock)
277-
self._gh.close_existing_prs_with_same_base.assert_called_once_with(
277+
self._gh.close_existing_prs_for_series.assert_called_once_with(
278278
list(self._gh.workers.values()), pr_mock
279279
)
280280

0 commit comments

Comments
 (0)