[PATCH] pwclient: Add check-get subcommand

Olof Johansson olof at lixom.net
Fri Sep 11 10:11:09 AEST 2020


Provide a subcommand to query the checks ran against a specific patch_id.

Also allows formatting of the output, to make it easier to query whether
a specific patch has seen any tests (and what their status is).

Signed-off-by: Olof Johansson <olof at lixom.net>
---
 pwclient/checks.py   | 29 +++++++++++++++++++++++++++++
 pwclient/parser.py   | 13 +++++++++++++
 pwclient/shell.py    |  5 +++++
 tests/fakes.py       | 14 ++++++++++++++
 tests/test_checks.py | 19 +++++++++++++++++++
 5 files changed, 80 insertions(+)

diff --git a/pwclient/checks.py b/pwclient/checks.py
index fcb8d07..767729d 100644
--- a/pwclient/checks.py
+++ b/pwclient/checks.py
@@ -5,6 +5,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 import sys
+import re
 
 from pwclient.xmlrpc import xmlrpclib
 
@@ -29,6 +30,34 @@ def action_info(rpc, check_id):
         print("- %- 14s: %s" % (key, value))
 
 
+def action_get(rpc, patch_id, format_str=None):
+    checks_list = rpc.patch_check_get(patch_id)
+    checks = checks_list.get('checks', None)
+    if checks == None:
+        return
+
+    if format_str:
+        format_field_re = re.compile("%{([a-z0-9_]+)}")
+
+        def check_field(matchobj):
+            fieldname = matchobj.group(1)
+
+            return str(check[fieldname])
+
+        for check in checks:
+            print(format_field_re.sub(check_field, format_str))
+    else:
+        s = "Check information for patch id %d" % patch_id
+        print(s)
+        print('-' * len(s))
+        out = []
+        for check in checks:
+            cout = []
+            for key, value in sorted(check.items()):
+                cout.append("- %- 14s: %s" % (key, value))
+            out.append("\n".join(cout))
+        print("\n\n".join(out))
+
 def action_create(rpc, patch_id, context, state, url, description):
     try:
         rpc.check_create(patch_id, context, state, url, description)
diff --git a/pwclient/parser.py b/pwclient/parser.py
index dc3934a..54c9bdd 100644
--- a/pwclient/parser.py
+++ b/pwclient/parser.py
@@ -111,6 +111,19 @@ installed locales.
         help="list all projects")
     projects_parser.set_defaults(subcmd='projects')
 
+    check_get_parser = subparsers.add_parser(
+        'check-get', add_help=False,
+        help="get checks for a patch"
+    )
+    check_get_parser.add_argument(
+        'patch_id', metavar='PATCH_ID', action='store', type=int,
+        help="patch ID")
+    check_get_parser.add_argument(
+        '-f', '--format', metavar='FORMAT',
+        help=("print output in the given format. You can use tags matching "
+              "fields, e.g. %%{context}, %%{state}, or %%{msgid}."))
+    check_get_parser.set_defaults(subcmd='check_get')
+
     check_list_parser = subparsers.add_parser(
         'check-list', add_help=False,
         help="list all checks"
diff --git a/pwclient/shell.py b/pwclient/shell.py
index 1f127ea..02165ba 100644
--- a/pwclient/shell.py
+++ b/pwclient/shell.py
@@ -211,6 +211,11 @@ def main(argv=sys.argv[1:]):
                 rpc, patch_id, state=args.state, archived=args.archived,
                 commit=args.commit_ref)
 
+    elif action == 'check_get':
+        patch_id = args.patch_id
+        format_str = args.format
+        checks.action_get(rpc, patch_id, format_str)
+
     elif action == 'check_list':
         checks.action_list(rpc)
 
diff --git a/tests/fakes.py b/tests/fakes.py
index a2dcc8c..5a8761b 100644
--- a/tests/fakes.py
+++ b/tests/fakes.py
@@ -69,6 +69,20 @@ def fake_checks():
         },
     ]
 
+def fake_patch_check():
+    return {
+        'checks': [{
+            'context': 'hello-world',
+            'id': 1,
+            'patch_id': 1,
+            'state': 'success',
+            'target_url': '',
+            'user_id': 1
+        }],
+        'state': 'success',
+        'total': 1
+    }
+
 
 def fake_states():
     return [
diff --git a/tests/test_checks.py b/tests/test_checks.py
index 625d375..1f4038e 100644
--- a/tests/test_checks.py
+++ b/tests/test_checks.py
@@ -6,6 +6,25 @@ from pwclient import xmlrpc
 from . import fakes
 
 
+def test_action_check_get(capsys):
+    rpc = mock.Mock()
+    rpc.patch_check_get.return_value = fakes.fake_patch_check()
+
+    checks.action_get(rpc, 1)
+
+    captured = capsys.readouterr()
+
+    assert captured.out == """\
+Check information for patch id 1
+--------------------------------
+- context       : hello-world
+- id            : 1
+- patch_id      : 1
+- state         : success
+- target_url    : 
+- user_id       : 1
+"""
+
 def test_action_check_list(capsys):
     rpc = mock.Mock()
     rpc.check_list.return_value = fakes.fake_checks()
-- 
2.20.1



More information about the Patchwork mailing list