Skip to content

Commit 66c9311

Browse files
committed
another quick fix
1 parent 37e2cbf commit 66c9311

3 files changed

Lines changed: 125 additions & 50 deletions

File tree

prevent-cli/src/prevent_cli/commands/upload.py

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,19 @@
55

66
import click
77
import sentry_sdk
8+
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
89
from codecov_cli.helpers.args import get_cli_args
910
from codecov_cli.helpers.upload_type import report_type_from_str
1011
from codecov_cli.opentelemetry import close_telem
1112
from codecov_cli.services.upload_coverage import upload_coverage_logic
1213

14+
from prevent_cli.options import global_options
15+
1316

1417
def _turn_env_vars_into_dict(ctx, params, value):
1518
return dict((v, os.getenv(v, None)) for v in value)
1619

1720

18-
_global_options = [
19-
click.option(
20-
"-C",
21-
"--sha",
22-
"--commit-sha",
23-
"commit_sha",
24-
help="Commit SHA (with 40 chars)",
25-
required=True,
26-
),
27-
click.option(
28-
"-Z",
29-
"--fail-on-error",
30-
"fail_on_error",
31-
is_flag=True,
32-
help="Exit with non-zero code in case of error",
33-
),
34-
click.option(
35-
"--git-service",
36-
help="Git service provider",
37-
type=click.Choice(
38-
[
39-
"github",
40-
"gitlab",
41-
"bitbucket",
42-
"github_enterprise",
43-
"gitlab_enterprise",
44-
"bitbucket_server",
45-
]
46-
),
47-
),
48-
click.option(
49-
"-t",
50-
"--token",
51-
help="Codecov upload token",
52-
envvar="TOKEN",
53-
),
54-
click.option(
55-
"-r",
56-
"--slug",
57-
help="owner/repo slug used instead of the private repo token in Self-hosted",
58-
envvar="SLUG",
59-
),
60-
]
61-
6221
_global_upload_options = [
6322
click.option(
6423
"--code",
@@ -130,16 +89,22 @@ def _turn_env_vars_into_dict(ctx, params, value):
13089
"--build",
13190
"--build-code",
13291
"build_code",
92+
cls=CodecovOption,
13393
help="Specify the build number manually",
94+
fallback_field=FallbackFieldEnum.build_code,
13495
),
13596
click.option(
13697
"--build-url",
13798
"build_url",
99+
cls=CodecovOption,
138100
help="The URL of the build where this is running",
101+
fallback_field=FallbackFieldEnum.build_url,
139102
),
140103
click.option(
141104
"--job-code",
105+
cls=CodecovOption,
142106
help="Specify the job code manually",
107+
fallback_field=FallbackFieldEnum.job_code,
143108
),
144109
click.option(
145110
"-n",
@@ -149,14 +114,18 @@ def _turn_env_vars_into_dict(ctx, params, value):
149114
click.option(
150115
"-B",
151116
"--branch",
117+
cls=CodecovOption,
152118
help="Branch to which this commit belongs to",
119+
fallback_field=FallbackFieldEnum.branch,
153120
),
154121
click.option(
155122
"-P",
156123
"--pr",
157124
"--pull-request-number",
158125
"pull_request_number",
126+
cls=CodecovOption,
159127
help="Specify the pull request number manually. Used to override pre-existing CI environment variables",
128+
fallback_field=FallbackFieldEnum.pull_request_number,
160129
),
161130
click.option(
162131
"-e",
@@ -243,12 +212,6 @@ def _turn_env_vars_into_dict(ctx, params, value):
243212
]
244213

245214

246-
def global_options(func):
247-
for option in reversed(_global_options):
248-
func = option(func)
249-
return func
250-
251-
252215
def global_upload_options(func):
253216
for option in reversed(_global_upload_options):
254217
func = option(func)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import typing
2+
from enum import Enum, auto
3+
4+
import click
5+
6+
7+
class FallbackFieldEnum(Enum):
8+
branch = auto()
9+
build_code = auto()
10+
build_url = auto()
11+
commit_sha = auto()
12+
git_service = auto()
13+
job_code = auto()
14+
pull_request_number = auto()
15+
service = auto()
16+
slug = auto()
17+
18+
19+
class CodecovOption(click.Option):
20+
def __init__(self, *args, **kwargs):
21+
self.fallback_field = kwargs.pop("fallback_field", None)
22+
super().__init__(*args, **kwargs)
23+
24+
def get_default(
25+
self, ctx: click.Context, call: bool = True
26+
) -> typing.Optional[typing.Union[typing.Any, typing.Callable[[], typing.Any]]]:
27+
res = super().get_default(ctx, call=call)
28+
if res is not None:
29+
return res
30+
if self.fallback_field is not None:
31+
if ctx.obj.get("ci_adapter") is not None:
32+
res = ctx.obj.get("ci_adapter").get_fallback_value(self.fallback_field)
33+
if res is not None:
34+
return res
35+
if ctx.obj.get("versioning_system") is not None:
36+
res = ctx.obj.get("versioning_system").get_fallback_value(
37+
self.fallback_field
38+
)
39+
if res is not None:
40+
return res
41+
return None
42+
43+
44+
class BrandedOption(click.Option):
45+
def resolve_envvar_value(self, ctx: click.Context) -> typing.Optional[str]:
46+
actual_var = self.envvar
47+
self.envvar = [
48+
f"{brand.value.upper()}_{actual_var}" for brand in ctx.obj["branding"]
49+
]
50+
res = super().resolve_envvar_value(ctx)
51+
self.envvar = actual_var
52+
return res
53+
54+
55+
class BrandedCodecovOption(CodecovOption, BrandedOption):
56+
pass
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import click
2+
from codecov_cli.fallbacks import (
3+
BrandedCodecovOption,
4+
BrandedOption,
5+
CodecovOption,
6+
FallbackFieldEnum,
7+
)
8+
from codecov_cli.helpers.git import GitService
9+
10+
_global_options = [
11+
click.option(
12+
"-C",
13+
"--sha",
14+
"--commit-sha",
15+
"commit_sha",
16+
help="Commit SHA (with 40 chars)",
17+
cls=CodecovOption,
18+
fallback_field=FallbackFieldEnum.commit_sha,
19+
required=True,
20+
),
21+
click.option(
22+
"-Z",
23+
"--fail-on-error",
24+
"fail_on_error",
25+
is_flag=True,
26+
help="Exit with non-zero code in case of error",
27+
),
28+
click.option(
29+
"--git-service",
30+
cls=CodecovOption,
31+
fallback_field=FallbackFieldEnum.git_service,
32+
type=click.Choice([service.value for service in GitService]),
33+
),
34+
click.option(
35+
"-t",
36+
"--token",
37+
help="Codecov upload token",
38+
cls=BrandedOption,
39+
envvar="TOKEN",
40+
),
41+
click.option(
42+
"-r",
43+
"--slug",
44+
"slug",
45+
cls=BrandedCodecovOption,
46+
fallback_field=FallbackFieldEnum.slug,
47+
help="owner/repo slug used instead of the private repo token in Self-hosted",
48+
envvar="SLUG",
49+
),
50+
]
51+
52+
53+
def global_options(func):
54+
for option in reversed(_global_options):
55+
func = option(func)
56+
return func

0 commit comments

Comments
 (0)