Skip to content

Commit bf2698f

Browse files
committed
patchwork: allow lookback config to not be set
"lookback" config controls the max age of open patches to process. Enable setting this config to a value, that would disable filtering by age. Ported from internal Meta diff D65621764 Signed-off-by: Ihor Solodrai <[email protected]>
1 parent 25d8efa commit bf2698f

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

kernel_patches_daemon/patchwork.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def __init__(
504504
server: str,
505505
search_patterns: List[Dict[str, Any]],
506506
auth_token: Optional[str] = None,
507-
lookback_in_days: int = 7,
507+
lookback_in_days: int = -1,
508508
api_version: str = "1.2",
509509
http_retries: int = DEFAULT_HTTP_RETRIES,
510510
) -> None:
@@ -513,7 +513,9 @@ def __init__(
513513
if not auth_token:
514514
logger.warning("Patchwork client runs in read-only mode")
515515
self.search_patterns = search_patterns
516-
self.since = self.format_since(lookback_in_days)
516+
self.since = (
517+
self.format_since(lookback_in_days) if lookback_in_days > 0 else None
518+
)
517519
# member variable initializations
518520
self.known_series: Dict[int, Series] = {}
519521
self.known_subjects: Dict[str, Subject] = {}
@@ -716,17 +718,20 @@ async def get_relevant_subjects(self) -> Sequence[Subject]:
716718
for pattern in self.search_patterns:
717719
patch_filters = MultiDict(
718720
[
719-
("since", self.since),
720721
("archived", str(False)),
721722
*[("state", val) for val in RELEVANT_STATES.values()],
722723
]
723724
)
725+
if self.since is not None:
726+
patch_filters.add("since", self.since)
724727
patch_filters.update({k: str(v) for k, v in pattern.items()})
725728
logger.info(
726729
f"Searching for Patchwork patches that match the criteria: {patch_filters}"
727730
)
728731
all_patches = await self.__get_objects_recursive(
729-
"patches", params=patch_filters
732+
"patches",
733+
# pyre-ignore
734+
params=patch_filters,
730735
)
731736

732737
series_ids = set()

tests/common/patchwork_mock.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
3737
super().__init__(*args, **kwargs)
3838

3939

40-
def get_default_pw_client() -> PatchworkMock:
41-
return PatchworkMock(
42-
server="127.0.0.1",
43-
api_version="1.1",
44-
search_patterns=[{"archived": False, "project": PROJECT, "delegate": DELEGATE}],
45-
auth_token="mocktoken",
46-
)
40+
def get_default_pw_client(**kwargs) -> PatchworkMock:
41+
default_params = {
42+
"server": "127.0.0.1",
43+
"api_version": "1.1",
44+
"search_patterns": [
45+
{"archived": False, "project": PROJECT, "delegate": DELEGATE}
46+
],
47+
"auth_token": "mocktoken",
48+
"lookback_in_days": 7,
49+
}
50+
default_params.update(kwargs)
51+
return PatchworkMock(**default_params)
4752

4853

4954
def init_pw_responses(m: aioresponses, data: Dict[str, Any]) -> None:

tests/test_patchwork.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import re
1212
import unittest
1313
from dataclasses import dataclass
14-
from typing import Any, Dict, List, Optional, Set, Union
14+
from typing import Any, Callable, Dict, List, Optional, Set, Union
1515

1616
from aioresponses import aioresponses
1717

@@ -262,6 +262,30 @@ class TestCase:
262262
case.expected,
263263
)
264264

265+
async def _test_lookback(
266+
self, m: aioresponses, lookback: int, assert_func: Callable[[str], None]
267+
) -> None:
268+
self._pw = get_default_pw_client(lookback_in_days=lookback)
269+
m.get(re.compile(r"^.*$"), status=200, body=b"[]")
270+
await self._pw.get_relevant_subjects()
271+
for request in m.requests.keys():
272+
url = str(request[1])
273+
assert_func(url)
274+
275+
@aioresponses()
276+
async def test_get_objects_with_lookback(self, m: aioresponses) -> None:
277+
"""
278+
If lookback is > 0, the 'since' query parameter must be present in the requests.
279+
"""
280+
await self._test_lookback(m, 7, lambda url: self.assertIn("since=", url))
281+
282+
@aioresponses()
283+
async def test_get_objects_no_lookback(self, m: aioresponses) -> None:
284+
"""
285+
If lookback is <= 0, the 'since' query parameter must not be present in the requests.
286+
"""
287+
await self._test_lookback(m, -1, lambda url: self.assertNotIn("since=", url))
288+
265289

266290
class TestSeries(PatchworkTestCase):
267291
@aioresponses()

0 commit comments

Comments
 (0)