diff --git a/swh/web/client/cli.py b/swh/web/client/cli.py --- a/swh/web/client/cli.py +++ b/swh/web/client/cli.py @@ -1,4 +1,4 @@ -# Copyright (C) 2020 The Software Heritage developers +# Copyright (C) 2020-2021 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -126,6 +126,60 @@ sys.stderr.close() +@web.group(name="save", context_settings=CONTEXT_SETTINGS) +@click.pass_context +def savecodenow(ctx: Context,): + """Subcommand to interact from the cli with the save code now feature + + """ + pass + + +@savecodenow.command("submit-request") +@click.option("--delimiter", "-d", default=",") +@click.pass_context +def submit_request(ctx, delimiter: str) -> None: + """Submit new save code now request through cli pipe. The expected format of the request + if one csv row ``,``. + + Example: + + cat list-origins | swh web save submit-request + + echo svn;https://svn-url\ngit;https://git-url | swh web save \ + submit-request --delimiter ';' + + Prints: + The output of save code now requests as json output. + + """ + import json + import logging + import sys + + logging.basicConfig(level=logging.INFO, stream=sys.stderr) + + client = ctx.obj["client"] + + processed_origins = [] + for origin in sys.stdin: + visit_type, origin = origin.rstrip().split(delimiter) + + try: + saved_origin = client.origin_save(visit_type, origin) + logging.info("Submitted origin (%s, %s)", visit_type, origin) + processed_origins.append(saved_origin) + except Exception as e: + logging.warning( + "Issue during processing of origin %s with type %s\n", + origin, + visit_type, + e, + ) + logging.debug("Origin saved: %s", len(processed_origins)) + print(json.dumps(processed_origins)) + + @web.group(name="auth", context_settings=CONTEXT_SETTINGS) @click.option( "--oidc-server-url", diff --git a/swh/web/client/client.py b/swh/web/client/client.py --- a/swh/web/client/client.py +++ b/swh/web/client/client.py @@ -608,3 +608,22 @@ q = r.links["next"]["url"] else: done = True + + def origin_save(self, visit_type: str, origin: str) -> Dict: + """Save code now query for the origin with visit_type. + + Args: + visit_type: Type of the visit + origin: the origin to save + + Returns: + The resulting dict of the visit saved + + Raises: + requests.HTTPError: if HTTP request fails + + """ + q = f"origin/save/{visit_type}/url/{origin}/" + r = self._call(q, http_method="post") + json = r.json() + return json diff --git a/swh/web/client/tests/api_data_static.py b/swh/web/client/tests/api_data_static.py new file mode 100644 --- /dev/null +++ b/swh/web/client/tests/api_data_static.py @@ -0,0 +1,38 @@ +# Copyright (C) 2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + + +API_DATA_STATIC = { + "origin/save/git/url/https://gitlab.org/gazelle/itest/": r""" + { + "visit_type": "git", + "origin_url": "https://gitlab.org/gazelle/itest", + "save_request_date": "2021-04-20T11:34:38.752929+00:00", + "save_request_status": "accepted", + "save_task_status": "not yet scheduled", + "visit_date": null + } + """, + "origin/save/git/url/https://git.renater.fr/anonscm/git/6po/6po.git/": r""" + { + "visit_type": "git", + "origin_url": "https://git.renater.fr/anonscm/git/6po/6po.git", + "save_request_date": "2021-04-20T11:34:40.115226+00:00", + "save_request_status": "accepted", + "save_task_status": "not yet scheduled", + "visit_date": null + } + """, + "origin/save/git/url/https://github.com/colobot/colobot/": r""" + { + "visit_type": "git", + "origin_url": "https://github.com/colobot/colobot", + "save_request_date": "2021-04-20T11:40:47.667492+00:00", + "save_request_status": "accepted", + "save_task_status": "not yet scheduled", + "visit_date": null + } + """, +} diff --git a/swh/web/client/tests/conftest.py b/swh/web/client/tests/conftest.py --- a/swh/web/client/tests/conftest.py +++ b/swh/web/client/tests/conftest.py @@ -1,4 +1,4 @@ -# Copyright (C) 2020 The Software Heritage developers +# Copyright (C) 2020-2021 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -8,6 +8,7 @@ from swh.web.client.client import WebAPIClient from .api_data import API_DATA, API_URL +from .api_data_static import API_DATA_STATIC @pytest.fixture @@ -42,6 +43,10 @@ requests_mock.register_uri("POST", f"{API_URL}/known/", json=known_callback) + # Add some other post urls to mock + for api_call, data in API_DATA_STATIC.items(): + requests_mock.post(f"{API_URL}/{api_call}", text=data) + return requests_mock