|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import typing |
| 5 | + |
| 6 | +import click |
| 7 | +import sentry_sdk |
| 8 | +from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum |
| 9 | +from codecov_cli.helpers.args import get_cli_args |
| 10 | +from codecov_cli.helpers.upload_type import report_type_from_str |
| 11 | +from codecov_cli.opentelemetry import close_telem |
| 12 | +from codecov_cli.services.upload_coverage import upload_coverage_logic |
| 13 | + |
| 14 | +from prevent_cli.options import global_options |
| 15 | + |
| 16 | + |
| 17 | +def _turn_env_vars_into_dict(ctx, params, value): |
| 18 | + return dict((v, os.getenv(v, None)) for v in value) |
| 19 | + |
| 20 | + |
| 21 | +_global_upload_options = [ |
| 22 | + click.option( |
| 23 | + "--code", |
| 24 | + "--report-code", |
| 25 | + "report_code", |
| 26 | + help="The code of the report. If unsure, leave default", |
| 27 | + default="default", |
| 28 | + hidden=True, |
| 29 | + ), |
| 30 | + click.option( |
| 31 | + "--network-root-folder", |
| 32 | + help="Root folder from which to consider paths on the network section", |
| 33 | + type=click.Path(path_type=pathlib.Path), |
| 34 | + default=pathlib.Path.cwd, |
| 35 | + show_default="Current working directory", |
| 36 | + ), |
| 37 | + click.option( |
| 38 | + "-s", |
| 39 | + "--dir", |
| 40 | + "--coverage-files-search-root-folder", |
| 41 | + "--files-search-root-folder", |
| 42 | + "files_search_root_folder", |
| 43 | + help="Folder where to search for coverage files", |
| 44 | + type=click.Path(path_type=pathlib.Path), |
| 45 | + default=pathlib.Path.cwd, |
| 46 | + show_default="Current Working Directory", |
| 47 | + ), |
| 48 | + click.option( |
| 49 | + "--exclude", |
| 50 | + "--coverage-files-search-exclude-folder", |
| 51 | + "--files-search-exclude-folder", |
| 52 | + "files_search_exclude_folders", |
| 53 | + help="Folders to exclude from search", |
| 54 | + type=click.Path(path_type=pathlib.Path), |
| 55 | + multiple=True, |
| 56 | + default=[], |
| 57 | + ), |
| 58 | + click.option( |
| 59 | + "-f", |
| 60 | + "--file", |
| 61 | + "--coverage-files-search-direct-file", |
| 62 | + "--files-search-direct-file", |
| 63 | + "files_search_explicitly_listed_files", |
| 64 | + help="Explicit files to upload. These will be added to the coverage files found for upload. If you wish to only upload the specified files, please consider using --disable-search to disable uploading other files.", |
| 65 | + type=click.Path(path_type=pathlib.Path), |
| 66 | + multiple=True, |
| 67 | + default=[], |
| 68 | + ), |
| 69 | + click.option( |
| 70 | + "--recurse-submodules", |
| 71 | + help="Whether to enumerate files inside of submodules for path-fixing purposes. Off by default.", |
| 72 | + is_flag=True, |
| 73 | + default=False, |
| 74 | + ), |
| 75 | + click.option( |
| 76 | + "--disable-search", |
| 77 | + help="Disable search for coverage files. This is helpful when specifying what files you want to upload with the --file option.", |
| 78 | + is_flag=True, |
| 79 | + default=False, |
| 80 | + ), |
| 81 | + click.option( |
| 82 | + "--disable-file-fixes", |
| 83 | + help="Disable file fixes to ignore common lines from coverage (e.g. blank lines or empty brackets)", |
| 84 | + is_flag=True, |
| 85 | + default=False, |
| 86 | + ), |
| 87 | + click.option( |
| 88 | + "-b", |
| 89 | + "--build", |
| 90 | + "--build-code", |
| 91 | + "build_code", |
| 92 | + cls=CodecovOption, |
| 93 | + help="Specify the build number manually", |
| 94 | + fallback_field=FallbackFieldEnum.build_code, |
| 95 | + ), |
| 96 | + click.option( |
| 97 | + "--build-url", |
| 98 | + "build_url", |
| 99 | + cls=CodecovOption, |
| 100 | + help="The URL of the build where this is running", |
| 101 | + fallback_field=FallbackFieldEnum.build_url, |
| 102 | + ), |
| 103 | + click.option( |
| 104 | + "--job-code", |
| 105 | + cls=CodecovOption, |
| 106 | + help="Specify the job code manually", |
| 107 | + fallback_field=FallbackFieldEnum.job_code, |
| 108 | + ), |
| 109 | + click.option( |
| 110 | + "-n", |
| 111 | + "--name", |
| 112 | + help="Custom defined name of the upload. Visible in Codecov UI", |
| 113 | + ), |
| 114 | + click.option( |
| 115 | + "-B", |
| 116 | + "--branch", |
| 117 | + cls=CodecovOption, |
| 118 | + help="Branch to which this commit belongs to", |
| 119 | + fallback_field=FallbackFieldEnum.branch, |
| 120 | + ), |
| 121 | + click.option( |
| 122 | + "-P", |
| 123 | + "--pr", |
| 124 | + "--pull-request-number", |
| 125 | + "pull_request_number", |
| 126 | + cls=CodecovOption, |
| 127 | + help="Specify the pull request number manually. Used to override pre-existing CI environment variables", |
| 128 | + fallback_field=FallbackFieldEnum.pull_request_number, |
| 129 | + ), |
| 130 | + click.option( |
| 131 | + "-e", |
| 132 | + "--env", |
| 133 | + "--env-var", |
| 134 | + "env_vars", |
| 135 | + multiple=True, |
| 136 | + callback=_turn_env_vars_into_dict, |
| 137 | + help="Specify environment variables to be included with this build.", |
| 138 | + ), |
| 139 | + click.option( |
| 140 | + "-F", |
| 141 | + "--flag", |
| 142 | + "flags", |
| 143 | + multiple=True, |
| 144 | + default=[], |
| 145 | + help="Flag the upload to group coverage metrics. Multiple flags allowed.", |
| 146 | + ), |
| 147 | + click.option( |
| 148 | + "--plugin", |
| 149 | + "plugin_names", |
| 150 | + multiple=True, |
| 151 | + default=["xcode", "gcov", "pycoverage"], |
| 152 | + ), |
| 153 | + click.option( |
| 154 | + "-d", |
| 155 | + "--dry-run", |
| 156 | + "dry_run", |
| 157 | + is_flag=True, |
| 158 | + help="Don't upload files to Codecov", |
| 159 | + ), |
| 160 | + click.option( |
| 161 | + "--legacy", |
| 162 | + "--use-legacy-uploader", |
| 163 | + "use_legacy_uploader", |
| 164 | + is_flag=True, |
| 165 | + help="Use the legacy upload endpoint", |
| 166 | + ), |
| 167 | + click.option( |
| 168 | + "--handle-no-reports-found", |
| 169 | + "handle_no_reports_found", |
| 170 | + is_flag=True, |
| 171 | + help="Raise no exceptions when no coverage reports found.", |
| 172 | + ), |
| 173 | + click.option( |
| 174 | + "--report-type", |
| 175 | + "report_type_str", |
| 176 | + help="The type of report to upload", |
| 177 | + default="coverage", |
| 178 | + type=click.Choice(["coverage", "test-results", "test_results"]), |
| 179 | + ), |
| 180 | + click.option( |
| 181 | + "--network-filter", |
| 182 | + help="Specify a filter on the files listed in the network section of the Codecov report. This will only add files whose path begin with the specified filter. Useful for upload-specific path fixing", |
| 183 | + ), |
| 184 | + click.option( |
| 185 | + "--network-prefix", |
| 186 | + help="Specify a prefix on files listed in the network section of the Codecov report. Useful to help resolve path fixing", |
| 187 | + ), |
| 188 | + click.option( |
| 189 | + "--gcov-args", |
| 190 | + help="Extra arguments to pass to gcov", |
| 191 | + ), |
| 192 | + click.option( |
| 193 | + "--gcov-ignore", |
| 194 | + help="Paths to ignore during gcov gathering", |
| 195 | + ), |
| 196 | + click.option( |
| 197 | + "--gcov-include", |
| 198 | + help="Paths to include during gcov gathering", |
| 199 | + ), |
| 200 | + click.option( |
| 201 | + "--gcov-executable", |
| 202 | + help="gcov executable to run. Defaults to 'gcov'", |
| 203 | + ), |
| 204 | + click.option( |
| 205 | + "--swift-project", |
| 206 | + help="Specify the swift project", |
| 207 | + ), |
| 208 | + click.option( |
| 209 | + "--parent-sha", |
| 210 | + help="SHA (with 40 chars) of what should be the parent of this commit", |
| 211 | + ), |
| 212 | +] |
| 213 | + |
| 214 | + |
| 215 | +def global_upload_options(func): |
| 216 | + for option in reversed(_global_upload_options): |
| 217 | + func = option(func) |
| 218 | + return func |
| 219 | + |
| 220 | + |
| 221 | +logger = logging.getLogger("codecovcli") |
| 222 | + |
| 223 | + |
| 224 | +@click.command() |
| 225 | +@global_options |
| 226 | +@global_upload_options |
| 227 | +@click.pass_context |
| 228 | +def upload( |
| 229 | + ctx: click.Context, |
| 230 | + branch: typing.Optional[str], |
| 231 | + build_code: typing.Optional[str], |
| 232 | + build_url: typing.Optional[str], |
| 233 | + commit_sha: str, |
| 234 | + disable_file_fixes: bool, |
| 235 | + disable_search: bool, |
| 236 | + dry_run: bool, |
| 237 | + env_vars: typing.Dict[str, str], |
| 238 | + fail_on_error: bool, |
| 239 | + files_search_exclude_folders: typing.List[pathlib.Path], |
| 240 | + files_search_explicitly_listed_files: typing.List[pathlib.Path], |
| 241 | + files_search_root_folder: pathlib.Path, |
| 242 | + flags: typing.List[str], |
| 243 | + gcov_args: typing.Optional[str], |
| 244 | + gcov_executable: typing.Optional[str], |
| 245 | + gcov_ignore: typing.Optional[str], |
| 246 | + gcov_include: typing.Optional[str], |
| 247 | + git_service: typing.Optional[str], |
| 248 | + handle_no_reports_found: bool, |
| 249 | + job_code: typing.Optional[str], |
| 250 | + name: typing.Optional[str], |
| 251 | + network_filter: typing.Optional[str], |
| 252 | + network_prefix: typing.Optional[str], |
| 253 | + network_root_folder: pathlib.Path, |
| 254 | + parent_sha: typing.Optional[str], |
| 255 | + plugin_names: typing.List[str], |
| 256 | + pull_request_number: typing.Optional[str], |
| 257 | + recurse_submodules: bool, |
| 258 | + report_code: str, |
| 259 | + report_type_str: str, |
| 260 | + slug: typing.Optional[str], |
| 261 | + swift_project: typing.Optional[str], |
| 262 | + token: typing.Optional[str], |
| 263 | + use_legacy_uploader: bool, |
| 264 | +): |
| 265 | + with sentry_sdk.start_transaction(op="task", name="Upload"): |
| 266 | + with sentry_sdk.start_span(name="upload"): |
| 267 | + args = get_cli_args(ctx) |
| 268 | + logger.debug( |
| 269 | + "Starting upload", |
| 270 | + extra=dict( |
| 271 | + extra_log_attributes=args, |
| 272 | + ), |
| 273 | + ) |
| 274 | + |
| 275 | + report_type = report_type_from_str(report_type_str) |
| 276 | + |
| 277 | + versioning_system = ctx.obj["versioning_system"] |
| 278 | + codecov_yaml = ctx.obj["codecov_yaml"] or {} |
| 279 | + cli_config = codecov_yaml.get("cli", {}) |
| 280 | + ci_adapter = ctx.obj.get("ci_adapter") |
| 281 | + enterprise_url = ctx.obj.get("enterprise_url") |
| 282 | + |
| 283 | + upload_coverage_logic( |
| 284 | + cli_config, |
| 285 | + versioning_system, |
| 286 | + ci_adapter, |
| 287 | + branch=branch, |
| 288 | + build_code=build_code, |
| 289 | + build_url=build_url, |
| 290 | + commit_sha=commit_sha, |
| 291 | + disable_file_fixes=disable_file_fixes, |
| 292 | + disable_search=disable_search, |
| 293 | + dry_run=dry_run, |
| 294 | + enterprise_url=enterprise_url, |
| 295 | + env_vars=env_vars, |
| 296 | + fail_on_error=fail_on_error, |
| 297 | + files_search_exclude_folders=files_search_exclude_folders, |
| 298 | + files_search_explicitly_listed_files=files_search_explicitly_listed_files, |
| 299 | + files_search_root_folder=files_search_root_folder, |
| 300 | + flags=flags, |
| 301 | + gcov_args=gcov_args, |
| 302 | + gcov_executable=gcov_executable, |
| 303 | + gcov_ignore=gcov_ignore, |
| 304 | + gcov_include=gcov_include, |
| 305 | + git_service=git_service, |
| 306 | + handle_no_reports_found=handle_no_reports_found, |
| 307 | + job_code=job_code, |
| 308 | + name=name, |
| 309 | + network_filter=network_filter, |
| 310 | + network_prefix=network_prefix, |
| 311 | + network_root_folder=network_root_folder, |
| 312 | + parent_sha=parent_sha, |
| 313 | + plugin_names=plugin_names, |
| 314 | + pull_request_number=pull_request_number, |
| 315 | + recurse_submodules=recurse_submodules, |
| 316 | + report_code=report_code, |
| 317 | + slug=slug, |
| 318 | + swift_project=swift_project, |
| 319 | + token=token, |
| 320 | + report_type=report_type, |
| 321 | + use_legacy_uploader=use_legacy_uploader, |
| 322 | + args=args, |
| 323 | + ) |
| 324 | + close_telem() |
0 commit comments