|
| 1 | +import json |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +from junitparser import JUnitXml |
| 7 | + |
| 8 | +oc = os.environ["HOME"] + "/oc_client/oc" |
| 9 | + |
| 10 | +ci_badge = { |
| 11 | + "schemaVersion": 1, |
| 12 | + "label": "Community test", |
| 13 | + "message": "", |
| 14 | + "color": "red", |
| 15 | + "openshiftVersion": "", |
| 16 | + "infraProvider": os.environ.get("INFRA_PROVIDER"), |
| 17 | + "patternName": os.environ.get("PATTERN_NAME"), |
| 18 | + "patternRepo": "", |
| 19 | + "patternBranch": "", |
| 20 | + "date": datetime.today().strftime("%Y-%m-%d"), |
| 21 | + "testSource": "Community", |
| 22 | + "debugInfo": None, |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +def get_openshift_version(): |
| 27 | + try: |
| 28 | + version_ret = subprocess.run([oc, "version", "-o", "json"], capture_output=True) |
| 29 | + version_out = version_ret.stdout.decode("utf-8") |
| 30 | + openshift_version = json.loads(version_out)["openshiftVersion"] |
| 31 | + major_minor = ".".join(openshift_version.split(".")[:-1]) |
| 32 | + return openshift_version, major_minor |
| 33 | + except KeyError as e: |
| 34 | + print("KeyError:" + str(e)) |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + versions = get_openshift_version() |
| 40 | + ci_badge["openshiftVersion"] = versions[0] |
| 41 | + |
| 42 | + pattern_repo = subprocess.run( |
| 43 | + ["git", "config", "--get", "remote.origin.url"], capture_output=True, text=True |
| 44 | + ) |
| 45 | + pattern_branch = subprocess.run( |
| 46 | + ["git", "branch", "--show-current"], capture_output=True, text=True |
| 47 | + ) |
| 48 | + |
| 49 | + ci_badge["patternRepo"] = pattern_repo.stdout.strip() |
| 50 | + ci_badge["patternBranch"] = pattern_branch.stdout.strip() |
| 51 | + |
| 52 | + # Check each xml file for failures |
| 53 | + results_dir = os.environ.get("WORKSPACE") |
| 54 | + failures = 0 |
| 55 | + |
| 56 | + for file in os.listdir(results_dir): |
| 57 | + if file.startswith("test_") and file.endswith(".xml"): |
| 58 | + with open(os.path.join(results_dir, file), "r") as result_file: # type: ignore |
| 59 | + xml = JUnitXml.fromfile(result_file) # type: ignore |
| 60 | + for suite in xml: |
| 61 | + for case in suite: |
| 62 | + if case.result: |
| 63 | + failures += 1 |
| 64 | + |
| 65 | + # Determine badge color from results |
| 66 | + if failures == 0: |
| 67 | + ci_badge["color"] = "green" |
| 68 | + |
| 69 | + # For now we assume `message` is the same as patternBranch |
| 70 | + ci_badge["message"] = ci_badge["patternBranch"] |
| 71 | + |
| 72 | + ci_badge_json_basename = ( |
| 73 | + os.environ.get("PATTERN_SHORTNAME") # type: ignore |
| 74 | + + "-" |
| 75 | + + os.environ.get("INFRA_PROVIDER") |
| 76 | + + "-" |
| 77 | + + versions[1] |
| 78 | + + "-stable-badge.json" |
| 79 | + ) |
| 80 | + ci_badge_json_filename = os.path.join(results_dir, ci_badge_json_basename) # type: ignore |
| 81 | + print(f"Creating CI badge file at: {ci_badge_json_filename}") |
| 82 | + |
| 83 | + with open(ci_badge_json_filename, "w") as ci_badge_file: |
| 84 | + json.dump(ci_badge, ci_badge_file) |
0 commit comments