|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import List |
| 6 | + |
| 7 | +import click |
| 8 | +from test_results_parser import ( |
| 9 | + Outcome, |
| 10 | + ParserError, |
| 11 | + Testrun, |
| 12 | + build_message, |
| 13 | + parse_junit_xml, |
| 14 | +) |
| 15 | + |
| 16 | +from codecov_cli.helpers.request import ( |
| 17 | + log_warnings_and_errors_if_any, |
| 18 | + send_post_request, |
| 19 | +) |
| 20 | +from codecov_cli.services.upload.file_finder import select_file_finder |
| 21 | + |
| 22 | +logger = logging.getLogger("codecovcli") |
| 23 | + |
| 24 | + |
| 25 | +_process_test_results_options = [ |
| 26 | + click.option( |
| 27 | + "-s", |
| 28 | + "--dir", |
| 29 | + "--files-search-root-folder", |
| 30 | + "dir", |
| 31 | + help="Folder where to search for test results files", |
| 32 | + type=click.Path(path_type=pathlib.Path), |
| 33 | + default=pathlib.Path.cwd, |
| 34 | + show_default="Current Working Directory", |
| 35 | + ), |
| 36 | + click.option( |
| 37 | + "-f", |
| 38 | + "--file", |
| 39 | + "--files-search-direct-file", |
| 40 | + "files", |
| 41 | + help="Explicit files to upload. These will be added to the test results files to be processed. If you wish to only process the specified files, please consider using --disable-search to disable processing other files.", |
| 42 | + type=click.Path(path_type=pathlib.Path), |
| 43 | + multiple=True, |
| 44 | + default=[], |
| 45 | + ), |
| 46 | + click.option( |
| 47 | + "--exclude", |
| 48 | + "--files-search-exclude-folder", |
| 49 | + "exclude_folders", |
| 50 | + help="Folders to exclude from search", |
| 51 | + type=click.Path(path_type=pathlib.Path), |
| 52 | + multiple=True, |
| 53 | + default=[], |
| 54 | + ), |
| 55 | + click.option( |
| 56 | + "--disable-search", |
| 57 | + help="Disable search for coverage files. This is helpful when specifying what files you want to upload with the --file option.", |
| 58 | + is_flag=True, |
| 59 | + default=False, |
| 60 | + ), |
| 61 | + click.option( |
| 62 | + "--provider-token", |
| 63 | + help="Token used to make calls to Repo provider API", |
| 64 | + type=str, |
| 65 | + default=None, |
| 66 | + ), |
| 67 | +] |
| 68 | + |
| 69 | + |
| 70 | +def process_test_results_options(func): |
| 71 | + for option in reversed(_process_test_results_options): |
| 72 | + func = option(func) |
| 73 | + return func |
| 74 | + |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class TestResultsNotificationPayload: |
| 78 | + failures: List[Testrun] |
| 79 | + failed: int = 0 |
| 80 | + passed: int = 0 |
| 81 | + skipped: int = 0 |
| 82 | + |
| 83 | + |
| 84 | +@click.command() |
| 85 | +@process_test_results_options |
| 86 | +def process_test_results( |
| 87 | + dir=None, files=None, exclude_folders=None, disable_search=None, provider_token=None |
| 88 | +): |
| 89 | + if provider_token is None: |
| 90 | + raise click.ClickException( |
| 91 | + "Provider token was not provided. Make sure to pass --provider-token option with the contents of the GITHUB_TOKEN secret, so we can make a comment." |
| 92 | + ) |
| 93 | + |
| 94 | + summary_file_path = os.getenv("GITHUB_STEP_SUMMARY") |
| 95 | + if summary_file_path is None: |
| 96 | + raise click.ClickException( |
| 97 | + "Error getting step summary file path from environment. Can't find GITHUB_STEP_SUMMARY environment variable." |
| 98 | + ) |
| 99 | + |
| 100 | + slug = os.getenv("GITHUB_REPOSITORY") |
| 101 | + if slug is None: |
| 102 | + raise click.ClickException( |
| 103 | + "Error getting repo slug from environment. Can't find GITHUB_REPOSITORY environment variable." |
| 104 | + ) |
| 105 | + |
| 106 | + ref = os.getenv("GITHUB_REF") |
| 107 | + if ref is None or "pull" not in ref: |
| 108 | + raise click.ClickException( |
| 109 | + "Error getting PR number from environment. Can't find GITHUB_REF environment variable." |
| 110 | + ) |
| 111 | + |
| 112 | + file_finder = select_file_finder( |
| 113 | + dir, exclude_folders, files, disable_search, report_type="test_results" |
| 114 | + ) |
| 115 | + |
| 116 | + upload_collection_results = file_finder.find_files() |
| 117 | + if len(upload_collection_results) == 0: |
| 118 | + raise click.ClickException( |
| 119 | + "No JUnit XML files were found. Make sure to specify them using the --file option." |
| 120 | + ) |
| 121 | + |
| 122 | + payload = generate_message_payload(upload_collection_results) |
| 123 | + |
| 124 | + message = build_message(payload) |
| 125 | + |
| 126 | + # write to step summary file |
| 127 | + with open(summary_file_path, "w") as f: |
| 128 | + f.write(message) |
| 129 | + |
| 130 | + # GITHUB_REF is documented here: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables |
| 131 | + pr_number = ref.split("/")[2] |
| 132 | + |
| 133 | + create_github_comment(provider_token, slug, pr_number, message) |
| 134 | + |
| 135 | + |
| 136 | +def create_github_comment(token, repo_slug, pr_number, message): |
| 137 | + url = f"https://api.github.com/repos/{repo_slug}/issues/{pr_number}/comments" |
| 138 | + |
| 139 | + headers = { |
| 140 | + "Accept": "application/vnd.github+json", |
| 141 | + "Authorization": f"Bearer {token}", |
| 142 | + "X-GitHub-Api-Version": "2022-11-28", |
| 143 | + } |
| 144 | + logger.info("Posting github comment") |
| 145 | + |
| 146 | + log_warnings_and_errors_if_any( |
| 147 | + send_post_request(url=url, data={"body": message}, headers=headers), |
| 148 | + "Posting test results comment", |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def generate_message_payload(upload_collection_results): |
| 153 | + payload = TestResultsNotificationPayload(failures=[]) |
| 154 | + |
| 155 | + for result in upload_collection_results: |
| 156 | + testruns = [] |
| 157 | + try: |
| 158 | + logger.info(f"Parsing {result.get_filename()}") |
| 159 | + testruns = parse_junit_xml(result.get_content()) |
| 160 | + for testrun in testruns: |
| 161 | + if ( |
| 162 | + testrun.outcome == Outcome.Failure |
| 163 | + or testrun.outcome == Outcome.Error |
| 164 | + ): |
| 165 | + payload.failed += 1 |
| 166 | + payload.failures.append(testrun) |
| 167 | + elif testrun.outcome == Outcome.Skip: |
| 168 | + payload.skipped += 1 |
| 169 | + else: |
| 170 | + payload.passed += 1 |
| 171 | + except ParserError as err: |
| 172 | + raise click.ClickException( |
| 173 | + f"Error parsing {str(result.get_filename(), 'utf8')} with error: {err}" |
| 174 | + ) |
| 175 | + return payload |
0 commit comments