Skip to content

Commit

Permalink
subcommands: create maestro_common files
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
padovan committed Jan 16, 2025
1 parent c37610b commit 3ffb47e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
14 changes: 14 additions & 0 deletions kcidev/libs/maestro_common.py
Original file line number Diff line number Diff line change
@@ -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))
10 changes: 3 additions & 7 deletions kcidev/subcommands/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 4 additions & 8 deletions kcidev/subcommands/maestro_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions kcidev/subcommands/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand All @@ -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"])

Expand Down
9 changes: 3 additions & 6 deletions kcidev/subcommands/testretry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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")
Expand Down

0 comments on commit 3ffb47e

Please sign in to comment.