|
| 1 | +import logging |
| 2 | +import pprint |
| 3 | +import subprocess |
| 4 | +import time |
| 5 | + |
| 6 | +import click |
| 7 | +import requests |
| 8 | + |
| 9 | +from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum |
| 10 | + |
| 11 | +logger = logging.getLogger("codecovcli") |
| 12 | + |
| 13 | + |
| 14 | +@click.command() |
| 15 | +@click.option("--token", required=True) |
| 16 | +@click.option( |
| 17 | + "--head-sha", |
| 18 | + "head_commit_sha", |
| 19 | + help="Commit SHA (with 40 chars)", |
| 20 | + cls=CodecovOption, |
| 21 | + fallback_field=FallbackFieldEnum.commit_sha, |
| 22 | + required=True, |
| 23 | +) |
| 24 | +@click.option( |
| 25 | + "--base-sha", |
| 26 | + "base_commit_sha", |
| 27 | + help="Commit SHA (with 40 chars)", |
| 28 | + cls=CodecovOption, |
| 29 | + required=True, |
| 30 | +) |
| 31 | +def label_analysis(token, head_commit_sha, base_commit_sha): |
| 32 | + url = "https://api.codecov.io/labels/labels-analysis" |
| 33 | + token_header = f"Repotoken {token}" |
| 34 | + # this needs to be more flexible to support multiple elements |
| 35 | + runner = ThisRunner() |
| 36 | + requested_labels = runner.collect_tests() |
| 37 | + payload = { |
| 38 | + "base_commit": base_commit_sha, |
| 39 | + "head_commit": head_commit_sha, |
| 40 | + "requested_labels": requested_labels, |
| 41 | + } |
| 42 | + try: |
| 43 | + response = requests.post( |
| 44 | + url, json=payload, headers={"Authorization": token_header} |
| 45 | + ) |
| 46 | + if response.status_code >= 500: |
| 47 | + raise click.ClickException("Sorry. Codecov is having problems") |
| 48 | + if response.status_code >= 400: |
| 49 | + logger.warning( |
| 50 | + "Got a 4XX status code back from Codecov", |
| 51 | + extra=dict( |
| 52 | + extra_log_attributes=dict( |
| 53 | + status_code=response.status_code, response_json=response.json() |
| 54 | + ) |
| 55 | + ), |
| 56 | + ) |
| 57 | + raise click.ClickException( |
| 58 | + "There is some problem with the submitted information" |
| 59 | + ) |
| 60 | + except requests.RequestException: |
| 61 | + raise click.ClickException(click.style("Unable to reach Codecov", fg="red")) |
| 62 | + eid = response.json()["external_id"] |
| 63 | + has_result = False |
| 64 | + logger.info("Label request sent") |
| 65 | + time.sleep(2) |
| 66 | + while not has_result: |
| 67 | + resp_data = requests.get( |
| 68 | + f"https://api.codecov.io/labels/labels-analysis/{eid}", |
| 69 | + headers={"Authorization": token_header}, |
| 70 | + ) |
| 71 | + resp_json = resp_data.json() |
| 72 | + if resp_json["state"] == "finished": |
| 73 | + runner.do_something_with_result(resp_data.json()["result"]) |
| 74 | + return |
| 75 | + if resp_json["state"] == "error": |
| 76 | + logger.error( |
| 77 | + "Request had problems calculating", |
| 78 | + extra=dict(extra_log_attributes=dict(resp_json=resp_json)), |
| 79 | + ) |
| 80 | + return |
| 81 | + logger.info("Waiting more time for result") |
| 82 | + time.sleep(5) |
| 83 | + |
| 84 | + |
| 85 | +class ThisRunner(object): |
| 86 | + def collect_tests(self): |
| 87 | + return [ |
| 88 | + x |
| 89 | + for x in subprocess.run( |
| 90 | + ["python", "-m", "pytest", "-q", "--collect-only"], capture_output=True |
| 91 | + ) |
| 92 | + .stdout.decode() |
| 93 | + .split() |
| 94 | + if "::" in x |
| 95 | + ] |
| 96 | + |
| 97 | + def do_something_with_result(self, result): |
| 98 | + command_array = ["python", "-m", "pytest"] |
| 99 | + all_labels = ( |
| 100 | + result["absent_labels"] |
| 101 | + + result["present_diff_labels"] |
| 102 | + + result["global_level_labels"] |
| 103 | + ) |
| 104 | + all_labels = set(all_labels) |
| 105 | + all_labels = [x.rsplit("[", 1)[0] if "[" in x else x for x in all_labels] |
| 106 | + pprint.pprint(all_labels) |
| 107 | + # Not safe from the customer perspective, in general, probably. |
| 108 | + # This is just to check it working |
| 109 | + command_array.extend(all_labels) |
| 110 | + print(command_array) |
| 111 | + subprocess.run(command_array) |
0 commit comments