Skip to content

Commit 10c2562

Browse files
openqa-bats-review: Finally call openqa-cli
1 parent a9cfee6 commit 10c2562

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

openqa-bats-review

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import itertools
1212
import logging
1313
import os
1414
import re
15+
import subprocess
1516
import sys
1617
from concurrent.futures import ThreadPoolExecutor
1718
from functools import cache
@@ -21,6 +22,7 @@ import requests
2122
from requests.exceptions import RequestException
2223

2324

25+
PASSED = "label:force_result:passed:" + os.path.basename(__file__)
2426
TIMEOUT = 30
2527
USER_AGENT = "openqa-bats-review (https://github.com/os-autoinst/scripts)"
2628

@@ -36,6 +38,46 @@ session = requests.Session()
3638
NOT_OK = re.compile(r"^not ok \d+ (?:\[\d+\] )?(.*?)(?: #? in \d+ ?ms)?$")
3739

3840

41+
client_args = [
42+
"openqa-cli",
43+
"api",
44+
"--header",
45+
f"User-Agent: {USER_AGENT}",
46+
]
47+
48+
49+
def call(cmds: list[str], dry_run: bool = False) -> str:
50+
"""
51+
Call openqa-cli
52+
"""
53+
log.debug("call: %s", " ".join(cmds))
54+
res = subprocess.run(
55+
(["echo", "Simulating: "] if dry_run else []) + cmds,
56+
stdout=subprocess.PIPE,
57+
stderr=subprocess.PIPE,
58+
check=False,
59+
)
60+
if len(res.stderr):
61+
log.warning("call() %s stderr: %s", cmds[0], res.stderr)
62+
res.check_returncode()
63+
return res.stdout.decode("utf-8")
64+
65+
66+
def openqa_comment(job: int, host: str, comment: str, dry_run: bool = False) -> str:
67+
"""
68+
Comment a job
69+
"""
70+
args = client_args + [
71+
"--host",
72+
host,
73+
"-X",
74+
"POST",
75+
"jobs/" + str(job) + "/comments",
76+
"text=" + comment,
77+
]
78+
return call(args, dry_run)
79+
80+
3981
def get_file(url: str) -> str:
4082
"""
4183
Get a text file from URL
@@ -144,9 +186,9 @@ def main(url: str, dry_run: bool = False) -> None:
144186
url = f"https://{url}"
145187
urlx = urlparse(url)
146188
openqa_host = f"{urlx.scheme}://{urlx.netloc}"
147-
job_id = int(os.path.basename(urlx.path))
189+
my_job_id = int(os.path.basename(urlx.path))
148190

149-
chain = resolve_clone_chain(openqa_host, job_id)
191+
chain = resolve_clone_chain(openqa_host, my_job_id)
150192
if len(chain) <= 1:
151193
log.info("No clones. Exiting")
152194
sys.exit(0)
@@ -180,7 +222,10 @@ def main(url: str, dry_run: bool = False) -> None:
180222
package = job["settings"]["BATS_PACKAGE"]
181223

182224
# conmon may also test crun in addition to runc on openSUSE where it's available
183-
if job["settings"]["DISTRI"] == "opensuse" and "OCI_RUNTIME" not in job["settings"]:
225+
if (
226+
job["settings"]["DISTRI"] == "opensuse"
227+
and "OCI_RUNTIME" not in job["settings"]
228+
):
184229
expected["conmon"] = 4
185230

186231
if len(logs) != expected[package]:
@@ -199,11 +244,13 @@ def main(url: str, dry_run: bool = False) -> None:
199244
if not common_failures:
200245
if not dry_run:
201246
log.info("No common failures across clone chain. Would tag as PASSED.")
202-
# TODO: Tag job as passed
203247
else:
204248
log.info("No common failures across clone chain. Tagging as PASSED.")
249+
print(openqa_comment(my_job_id, openqa_host, PASSED, dry_run))
205250
else:
206-
log.info("Common failures found across clone chain: %s", " ".join(common_failures))
251+
log.error(
252+
"Common failures found across clone chain: %s", "\n".join(common_failures)
253+
)
207254
sys.exit(1)
208255

209256

@@ -215,5 +262,5 @@ def parse_args() -> argparse.Namespace:
215262

216263

217264
if __name__ == "__main__":
218-
args = parse_args()
219-
main(args.url, dry_run=args.dry_run)
265+
opts = parse_args()
266+
main(opts.url, dry_run=opts.dry_run)

0 commit comments

Comments
 (0)