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
13 changes: 9 additions & 4 deletions kernel_patches_daemon/patchwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def __init__(
server: str,
search_patterns: List[Dict[str, Any]],
auth_token: Optional[str] = None,
lookback_in_days: int = 7,
lookback_in_days: int = -1,
api_version: str = "1.2",
http_retries: int = DEFAULT_HTTP_RETRIES,
) -> None:
Expand All @@ -513,7 +513,9 @@ def __init__(
if not auth_token:
logger.warning("Patchwork client runs in read-only mode")
self.search_patterns = search_patterns
self.since = self.format_since(lookback_in_days)
self.since = (
self.format_since(lookback_in_days) if lookback_in_days > 0 else None
)
# member variable initializations
self.known_series: Dict[int, Series] = {}
self.known_subjects: Dict[str, Subject] = {}
Expand Down Expand Up @@ -716,17 +718,20 @@ async def get_relevant_subjects(self) -> Sequence[Subject]:
for pattern in self.search_patterns:
patch_filters = MultiDict(
[
("since", self.since),
("archived", str(False)),
*[("state", val) for val in RELEVANT_STATES.values()],
]
)
if self.since is not None:
patch_filters.add("since", self.since)
patch_filters.update({k: str(v) for k, v in pattern.items()})
logger.info(
f"Searching for Patchwork patches that match the criteria: {patch_filters}"
)
all_patches = await self.__get_objects_recursive(
"patches", params=patch_filters
"patches",
# pyre-ignore
params=patch_filters,
)

series_ids = set()
Expand Down
19 changes: 12 additions & 7 deletions tests/common/patchwork_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)


def get_default_pw_client() -> PatchworkMock:
return PatchworkMock(
server="127.0.0.1",
api_version="1.1",
search_patterns=[{"archived": False, "project": PROJECT, "delegate": DELEGATE}],
auth_token="mocktoken",
)
def get_default_pw_client(**kwargs) -> PatchworkMock:
default_params = {
"server": "127.0.0.1",
"api_version": "1.1",
"search_patterns": [
{"archived": False, "project": PROJECT, "delegate": DELEGATE}
],
"auth_token": "mocktoken",
"lookback_in_days": 7,
}
default_params.update(kwargs)
return PatchworkMock(**default_params)


def init_pw_responses(m: aioresponses, data: Dict[str, Any]) -> None:
Expand Down
26 changes: 25 additions & 1 deletion tests/test_patchwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import re
import unittest
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Set, Union
from typing import Any, Callable, Dict, List, Optional, Set, Union

from aioresponses import aioresponses

Expand Down Expand Up @@ -262,6 +262,30 @@ class TestCase:
case.expected,
)

async def _test_lookback(
self, m: aioresponses, lookback: int, assert_func: Callable[[str], None]
) -> None:
self._pw = get_default_pw_client(lookback_in_days=lookback)
m.get(re.compile(r"^.*$"), status=200, body=b"[]")
await self._pw.get_relevant_subjects()
for request in m.requests.keys():
url = str(request[1])
assert_func(url)

@aioresponses()
async def test_get_objects_with_lookback(self, m: aioresponses) -> None:
"""
If lookback is > 0, the 'since' query parameter must be present in the requests.
"""
await self._test_lookback(m, 7, lambda url: self.assertIn("since=", url))

@aioresponses()
async def test_get_objects_no_lookback(self, m: aioresponses) -> None:
"""
If lookback is <= 0, the 'since' query parameter must not be present in the requests.
"""
await self._test_lookback(m, -1, lambda url: self.assertNotIn("since=", url))


class TestSeries(PatchworkTestCase):
@aioresponses()
Expand Down