review-tui: add support for setting the delegate in patchwork
cover review-tui: add support for setting the delegate in patchwork
Here's a series that adds support for setting the delegate in patchwork via the review-tui.
---
Brian Masney (4):
review-tui: show why a Patchwork update failed
review-tui: let ApplyStateModal update other patch fields
review: add pw_fetch_maintainers()
review-tui: change the Patchwork delegate from the pw browser
docs/maintainer/review.rst | 6 + src/b4/review/_review.py | 57 +++++++++ src/b4/review_tui/_modals.py | 220 ++++++++++++++++++++++++++++++++-- src/b4/review_tui/_pw_app.py | 165 ++++++++++++++++++++++++- src/tests/test_tui_pw.py | 279 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 718 insertions(+), 9 deletions(-)
---
base-commit: 6be7c6139d7734d84afddf989fa2874935b6d85b
change-id: 20261014-pw-delegate-b185593a1837
Best regards,
Signature · 1 line
Brian Masney <[email protected]>
PATCH 1/4 review-tui: show why a Patchwork update failed
When Patchwork rejects a state change, the Patchwork browser only says "N updated, M failed", and the server's reason is thrown away. That leaves the user guessing whether it was a permission problem, a bad value, or the server being down. Keep the reason from the first patch that failed and add it to the notice.
--- src/b4/review_tui/_modals.py | 36 +++++++++++++++++++++++++++-- src/b4/review_tui/_pw_app.py | 5 +++- src/tests/test_tui_pw.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-)
src/b4/review_tui/_modals.py +34 -2
@@ -2716,6 +2716,29 @@ def SetStateConfirmScreen( PW_PATCH_TIMEOUT = 30 +def _pw_error_text(ex: Exception) -> str: + """Describe a failed Patchwork request, preferring the server's reason. + + Patchwork answers a rejected PATCH with a JSON body mapping each field + to its errors, e.g. ``{"delegate": ["User 'x' is not a maintainer..."]}``. + """ + rsp = getattr(ex, 'response', None) + if rsp is None: + return str(ex) + try: + body = rsp.json() + except ValueError: + body = None + if isinstance(body, dict) and body: + parts = [] + for field, errs in body.items(): + if isinstance(errs, list): + errs = ' '.join(str(e) for e in errs) + parts.append(f'{field}: {errs}') + return '; '.join(parts) + return f'{rsp.status_code} {rsp.reason}' + + class ApplyStateResult(NamedTuple): """Outcome of an ApplyStateModal run."""
@@ -2725,6 +2748,8 @@ class ApplyStateResult(NamedTuple): new_state: str # True when the user stopped the run with Esc before it finished cancelled: bool + # Why the first failed patch failed, as the server put it ('' if none) + error: str = '' class ApplyStateModal(ModalScreen[ApplyStateResult]):
@@ -2785,6 +2810,7 @@ class ApplyStateModal(ModalScreen[ApplyStateResult]): self._series_name = series_name self._done_ids: Set[int] = set() self._fail = 0 + self._error = '' self._cancelled = False def compose(self) -> ComposeResult:
@@ -2812,7 +2838,11 @@ class ApplyStateModal(ModalScreen[ApplyStateResult]): def _result(self) -> ApplyStateResult: return ApplyStateResult( - set(self._done_ids), self._fail, self._new_state, self._cancelled + set(self._done_ids), + self._fail, + self._new_state, + self._cancelled, + self._error, ) def _apply_states(self) -> ApplyStateResult:
@@ -2841,8 +2871,10 @@ class ApplyStateModal(ModalScreen[ApplyStateResult]): ) rsp.raise_for_status() self._done_ids.add(patch_id) - except Exception: + except Exception as ex: self._fail += 1 + if not self._error: + self._error = _pw_error_text(ex) # Update progress from worker thread self.app.call_from_thread(self._update_progress, i + 1)
src/b4/review_tui/_pw_app.py +4 -1
@@ -821,7 +821,10 @@ class PwApp(LoreNodeShutdownMixin, App[None]): severity='warning', ) elif result.fail: - self.notify(f'{ok} updated, {result.fail} failed', severity='warning') + msg = f'{ok} updated, {result.fail} failed' + if result.error: + msg += f': {escape(result.error)}' + self.notify(msg, severity='warning') elif len(targets) == 1: self.notify(f'{ok} patch(es) set to {new_state}', severity='information') else:
src/tests/test_tui_pw.py +55 -0
64 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 2/4 review-tui: let ApplyStateModal update other patch fields
In reply to Brian MasneyApplyStateModal is the progress dialog for PATCHing Patchwork patches one at a time, with Esc to stop and a result that says which patches were done. The only thing tying it to state changes is the title and the fields it sends. Move those into _title() and _payload(), so a subclass can reuse the rest to change a different field, such as the delegate. There is no change in behavior.
--- src/b4/review_tui/_modals.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
src/b4/review_tui/_modals.py +11 -5
@@ -2758,6 +2758,9 @@ class ApplyStateModal(ModalScreen[ApplyStateResult]): Esc stops the run after the request that is in flight. The result lists the patch ids that were updated, so the caller can tell which series were finished and which were not. + + Subclasses can change other patch fields by overriding :meth:`_title` + and :meth:`_payload`. """ BINDINGS = [
@@ -2813,9 +2816,15 @@ class ApplyStateModal(ModalScreen[ApplyStateResult]): self._error = '' self._cancelled = False + def _title(self) -> str: + return f'Setting state to: {self._new_state}' + + def _payload(self) -> Dict[str, Any]: + return {'state': self._new_state, 'archived': self._archived} + def compose(self) -> ComposeResult: with Vertical(id='apply-dialog'): - yield Label(f'Setting state to: {self._new_state}', id='apply-title') + yield Label(self._title(), id='apply-title', markup=False) yield Label(self._series_name, id='apply-series', markup=False) yield Label( f'Processing 0/{len(self._patch_ids)} patches...', id='apply-status'
@@ -2858,10 +2867,7 @@ class ApplyStateModal(ModalScreen[ApplyStateResult]): if self._cancelled or worker_cancelled(): break patchid_url = '/'.join((patches_url, str(patch_id), '')) - data = { - 'state': self._new_state, - 'archived': self._archived, - } + data = self._payload() try: rsp = pses.patch( patchid_url,
Signature · 1 line
2.55.0
PATCH 3/4 review: add pw_fetch_maintainers()
In reply to Brian MasneyPatchwork only accepts a project maintainer as a patch delegate, so the project's maintainers are the users that a delegate picker should offer. Add a helper that reads them from the project endpoint.
--- src/b4/review/_review.py | 57 +++++++++++++++++++++++++++++++++++++++ src/tests/test_tui_pw.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+)
src/b4/review/_review.py +57 -0
@@ -4152,6 +4152,63 @@ def pw_fetch_states(pwkey: str, pwurl: str, pwproj: str) -> List[Dict[str, Any]] return [{'slug': s, 'name': s.replace('-', ' ').title()} for s in default_slugs] +def _pw_lookup_user_id( + pses: requests.Session, api_url: str, username: str +) -> Optional[int]: + """Return the Patchwork User id for *username*, or None if not found. + + The users endpoint searches username, names and email by substring, so + pick out the exact username match. + """ + users_url = '/'.join((api_url, 'users', '')) + resp = pses.get(users_url, params={'q': username}) + if resp.status_code != 200: + raise RuntimeError(f'Patchwork API error: {resp.status_code} {resp.reason}') + for u in resp.json(): + if u.get('username') == username and u.get('id'): + return int(u['id']) + return None + + +def pw_fetch_maintainers(pwkey: str, pwurl: str, pwproj: str) -> List[Dict[str, Any]]: + """Return the maintainers of a Patchwork project. + + Patchwork only accepts a project maintainer as a patch delegate, so + this is the list of users a patch can be delegated to. + + Returns a list of user dicts with 'id', 'username', 'name' and 'email' + keys, sorted by username. The 'id' is the User id that a patch's + delegate field takes. + """ + pses, api_url = b4.get_patchwork_session(pwkey, pwurl) + # The projects endpoint accepts the project linkname in place of its id. + project_url = '/'.join((api_url, 'projects', pwproj, '')) + resp = pses.get(project_url) + if resp.status_code != 200: + raise RuntimeError(f'Patchwork API error: {resp.status_code} {resp.reason}') + users: List[Dict[str, Any]] = [] + for m in resp.json().get('maintainers', []): + username = m.get('username') + if not username: + continue + # The maintainers list carries UserProfile ids, not the User ids + # that the delegate field expects, so resolve each one by username. + user_id = _pw_lookup_user_id(pses, api_url, username) + if user_id is None: + logger.debug('Could not find Patchwork user %s', username) + continue + name = ' '.join(p for p in (m.get('first_name'), m.get('last_name')) if p) + users.append( + { + 'id': user_id, + 'username': username, + 'name': name, + 'email': m.get('email') or '', + } + ) + return sorted(users, key=lambda u: u['username'].lower()) + + def pw_fetch_checks( pwkey: str, pwurl: str, patch_ids: List[int] ) -> List[Dict[str, Any]]:
src/tests/test_tui_pw.py +69 -0
72 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 4/4 review-tui: change the Patchwork delegate from the pw browser
In reply to Brian MasneyThe Patchwork browser shows who a series is delegated to, and can filter on it, but changing the delegate still means going to the web UI. Add a 'd' key that offers the project's maintainers, plus "(no delegate)", and sets the chosen one on every patch in the series. Like 's', it acts on every marked series when any are marked, asks for confirmation first, and can be stopped with Esc.
--- docs/maintainer/review.rst | 6 ++ src/b4/review_tui/_modals.py | 168 +++++++++++++++++++++++++++++++++++++++++++ src/b4/review_tui/_pw_app.py | 160 ++++++++++++++++++++++++++++++++++++++++- src/tests/test_tui_pw.py | 155 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 488 insertions(+), 1 deletion(-)
docs/maintainer/review.rst +6 -0
@@ -826,6 +826,7 @@ there you can: * View CI check details for a series (``c``) * Track a series directly from the Patchwork list (``t``) * Set or change the Patchwork state for a series (``s``) +* Change the Patchwork delegate for a series (``d``) * Hide and unhide series (``h``/``u``) * Toggle display of hidden series (``H``) * Refresh the series list from Patchwork (``r``)
@@ -843,6 +844,11 @@ To set the state of many series at once, mark them first (``space``, or changes anything. While the states are being set, press ``Esc`` to stop after the current patch. +Press ``d`` to delegate a series to another project maintainer, or to +clear its delegate. Patchwork only accepts project maintainers as +delegates, so the list offers only those. Like ``s``, it acts on every +marked series when any are marked. + B4 also keeps Patchwork up to date as you work through the review lifecycle. This works for any tracked series that Patchwork knows about, not only the series you track from the Patchwork browser. B4
src/b4/review_tui/_modals.py +168 -0
186 lines not on this page · raw message
src/b4/review_tui/_pw_app.py +159 -1
207 lines not on this page · raw message
src/tests/test_tui_pw.py +155 -0
185 lines not on this page · raw message
Signature · 1 line
2.55.0
Quoted 17 lines · Brian Masney
Here's a series that adds support for setting the delegate in patchwork via the review-tui.
Signed-off-by Brian Masney <[email protected]>---
Brian Masney (4):
review-tui: show why a Patchwork update failed
review-tui: let ApplyStateModal update other patch fields
review: addpw_fetch_maintainers()
review-tui: change the Patchwork delegate from the pw browserdocs/maintainer/review.rst | 6 + src/b4/review/_review.py | 57 +++++++++ src/b4/review_tui/_modals.py | 220 ++++++++++++++++++++++++++++++++-- src/b4/review_tui/_pw_app.py | 165 ++++++++++++++++++++++++- src/tests/test_tui_pw.py | 279 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 718 insertions(+), 9 deletions(-)
Quoted 8 lines
---
base-commit:6be7c6139d7734d84afddf989fa2874935b6d85b
change-id: 20261014-pw-delegate-b185593a1837Best regards,
Signature · 1 line
Brian Masney <[email protected]>
Signature · 1 line
Jerome
Konstantin Ryabitsev
AppliedQuoted 4 lines · Brian Masney
review-tui: add support for setting the delegate in patchwork
Here's a series that adds support for setting the delegate in patchwork via the review-tui.
Applied, thanks!
[1/4] review-tui: show why a Patchwork update failed
https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=f8dd05f102281fde48dbcc9ec4f919c64ec94817 [2/4] review-tui: let ApplyStateModal update other patch fields https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=bfb9b7ffcdafbdc680e22047127bc339a73b77a9 [3/4] review: add pw_fetch_maintainers() https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=0ddb22a5d1c70df923414c8c271068c08f0434c0 [4/4] review-tui: change the Patchwork delegate from the pw browser https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=12f7caba936e31563c15a6b20d196717e01162e0
Best regards,
Signature · 1 line
Konstantin Ryabitsev <[email protected]>