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,53 @@ sys.stderr.close() +@web.group(name="save-code-now", context_settings=CONTEXT_SETTINGS) +@click.pass_context +def savecodenow(ctx: Context,): + """Save code now subcommand to interact from the cli with the save code now feature + + """ + pass + + +@savecodenow.command() +@click.option("--delimiter", "-d", default=",") +@click.pass_context +def post(ctx, delimiter): + """Post new save code now through pipe + + Example: + + cat list-origins | swh web save-code-now post + + echo svn;https://svn-url\ngit;https://git-url | swh web save-code-now \ + post --delimiter ';' + + """ + import json + import logging + from sys import stdin + + client = ctx.obj["client"] + + processed_origins = [] + for origin in stdin: + visit_type, origin = origin.rstrip().split(delimiter) + + try: + saved_origin = client.origin_save(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