From 3ffb47e695a8a2f721a3282632d33c375a7921e6 Mon Sep 17 00:00:00 2001 From: Gustavo Padovan Date: Thu, 16 Jan 2025 11:35:57 -0300 Subject: [PATCH] subcommands: create maestro_common files We are already duplicating a lot of code, even in such an earlier phase of the project. Let's change this trend and start consolidating our code base. Signed-off-by: Gustavo Padovan --- kcidev/libs/maestro_common.py | 14 ++++++++++++++ kcidev/subcommands/checkout.py | 10 +++------- kcidev/subcommands/maestro_results.py | 12 ++++-------- kcidev/subcommands/patch.py | 8 +++----- kcidev/subcommands/testretry.py | 9 +++------ 5 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 kcidev/libs/maestro_common.py diff --git a/kcidev/libs/maestro_common.py b/kcidev/libs/maestro_common.py new file mode 100644 index 0000000..422cde6 --- /dev/null +++ b/kcidev/libs/maestro_common.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import json + +import click + +from kcidev.libs.common import * + + +def maestro_print_api_call(host, data=None): + click.secho("maestro api endpoint: " + host, fg="green") + if data: + kci_msg(json.dumps(data, indent=4)) diff --git a/kcidev/subcommands/checkout.py b/kcidev/subcommands/checkout.py index 7d4cbf8..bb6e35b 100644 --- a/kcidev/subcommands/checkout.py +++ b/kcidev/subcommands/checkout.py @@ -13,11 +13,7 @@ from git import Repo from kcidev.libs.common import * - - -def api_connection(host): - click.secho("api connect: " + host, fg="green") - return host +from kcidev.libs.maestro_common import * def display_api_error(response): @@ -46,7 +42,7 @@ def send_checkout_full(baseurl, token, **kwargs): if "platformfilter" in kwargs: data["platformfilter"] = kwargs["platformfilter"] jdata = json.dumps(data) - print(jdata) + maestro_print_api_call(url, data) try: response = requests.post(url, headers=headers, data=jdata, timeout=30) except requests.exceptions.RequestException as e: @@ -247,7 +243,7 @@ def checkout( ): cfg = ctx.obj.get("CFG") instance = ctx.obj.get("INSTANCE") - url = api_connection(cfg[instance]["pipeline"]) + url = cfg[instance]["pipeline"] apiurl = cfg[instance]["api"] token = cfg[instance]["token"] if not jobfilter: diff --git a/kcidev/subcommands/maestro_results.py b/kcidev/subcommands/maestro_results.py index 95ffde8..eafc526 100644 --- a/kcidev/subcommands/maestro_results.py +++ b/kcidev/subcommands/maestro_results.py @@ -10,11 +10,7 @@ from git import Repo from kcidev.libs.common import * - - -def api_connection(host): - click.secho("api connect: " + host, fg="green") - return host +from kcidev.libs.maestro_common import * def print_nodes(nodes, field): @@ -35,7 +31,7 @@ def get_node(url, nodeid, field): "Content-Type": "application/json; charset=utf-8", } url = url + "latest/node/" + nodeid - click.secho(url) + maestro_print_api_call(url) response = requests.get(url, headers=headers) try: response.raise_for_status() @@ -59,7 +55,7 @@ def get_nodes(url, limit, offset, filter, field): # if we need anything more complex than eq(=) url = url + "&" + f - click.secho(url) + maestro_print_api_call(url) response = requests.get(url, headers=headers) try: response.raise_for_status() @@ -114,7 +110,7 @@ def get_nodes(url, limit, offset, filter, field): def maestro_results(ctx, nodeid, nodes, limit, offset, filter, field): config = ctx.obj.get("CFG") instance = ctx.obj.get("INSTANCE") - url = api_connection(config[instance]["api"]) + url = config[instance]["api"] if nodeid: get_node(url, nodeid, field) if nodes: diff --git a/kcidev/subcommands/patch.py b/kcidev/subcommands/patch.py index 1ba1c02..b61a5b8 100644 --- a/kcidev/subcommands/patch.py +++ b/kcidev/subcommands/patch.py @@ -8,10 +8,7 @@ import toml from git import Repo - -def api_connection(host): - click.secho("api connect: " + host, fg="green") - return host +from kcidev.libs.maestro_common import * def send_build(url, patch, branch, treeurl, token): @@ -26,6 +23,7 @@ def send_build(url, patch, branch, treeurl, token): "kbuildname": "example", "testname": "example", } + maestro_print_api_call(url, values) response = requests.post(url, headers=headers, files={"patch": patch}, data=values) click.secho(response.status_code, fg="green") click.secho(response.json(), fg="green") @@ -49,7 +47,7 @@ def send_build(url, patch, branch, treeurl, token): def patch(ctx, repository, branch, private, patch): config = ctx.obj.get("CFG") instance = ctx.obj.get("INSTANCE") - url = api_connection(config[instance]["pipeline"]) + url = config[instance]["pipeline"] patch = open(patch, "rb") send_build(url, patch, branch, repository, config[instance]["token"]) diff --git a/kcidev/subcommands/testretry.py b/kcidev/subcommands/testretry.py index 79e01c5..a908913 100644 --- a/kcidev/subcommands/testretry.py +++ b/kcidev/subcommands/testretry.py @@ -8,11 +8,7 @@ from git import Repo from kcidev.libs.common import * - - -def api_connection(host): - click.secho("api connect: " + host, fg="green") - return host +from kcidev.libs.maestro_common import * def display_api_error(response): @@ -32,6 +28,7 @@ def send_jobretry(baseurl, jobid, token): } data = {"nodeid": jobid} jdata = json.dumps(data) + maestro_print_api_call(url, data) try: response = requests.post(url, headers=headers, data=jdata) except requests.exceptions.RequestException as e: @@ -54,7 +51,7 @@ def send_jobretry(baseurl, jobid, token): def testretry(ctx, nodeid): cfg = ctx.obj.get("CFG") instance = ctx.obj.get("INSTANCE") - url = api_connection(cfg[instance]["pipeline"]) + url = cfg[instance]["pipeline"] resp = send_jobretry(url, nodeid, cfg[instance]["token"]) if resp and "message" in resp: click.secho(resp["message"], fg="green")