-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfallbacks.py
More file actions
78 lines (65 loc) · 2.38 KB
/
fallbacks.py
File metadata and controls
78 lines (65 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import typing
from enum import Enum, auto
import click
class FallbackFieldEnum(Enum):
branch = auto()
build_code = auto()
build_url = auto()
commit_sha = auto()
git_service = auto()
job_code = auto()
pull_request_number = auto()
service = auto()
slug = auto()
job_name = auto()
_FIELDS_WITH_VERSIONING_FALLBACK = frozenset(
{
FallbackFieldEnum.branch,
FallbackFieldEnum.commit_sha,
FallbackFieldEnum.slug,
FallbackFieldEnum.git_service,
}
)
class CodecovOption(click.Option):
fallback_fields: typing.Optional[tuple[FallbackFieldEnum, ...]]
def __init__(self, *args, **kwargs):
fallback_fields = kwargs.pop("fallback_fields", None)
fallback_field = kwargs.pop("fallback_field", None)
if fallback_fields is not None:
self.fallback_fields = tuple(fallback_fields)
elif fallback_field is not None:
self.fallback_fields = (fallback_field,)
else:
self.fallback_fields = None
super().__init__(*args, **kwargs)
def get_default(
self, ctx: click.Context, call: bool = True
) -> typing.Optional[typing.Union[typing.Any, typing.Callable[[], typing.Any]]]:
res = super().get_default(ctx, call=call)
if res is not None:
return res
if self.fallback_fields is not None:
for field in self.fallback_fields:
if ctx.obj.get("ci_adapter") is not None:
res = ctx.obj.get("ci_adapter").get_fallback_value(field)
if res is not None:
return res
if (
ctx.obj.get("versioning_system") is not None
and field in _FIELDS_WITH_VERSIONING_FALLBACK
):
res = ctx.obj.get("versioning_system").get_fallback_value(field)
if res is not None:
return res
return None
class BrandedOption(click.Option):
def resolve_envvar_value(self, ctx: click.Context) -> typing.Optional[str]:
actual_var = self.envvar
self.envvar = [
f"{brand.value.upper()}_{actual_var}" for brand in ctx.obj["branding"]
]
res = super().resolve_envvar_value(ctx)
self.envvar = actual_var
return res
class BrandedCodecovOption(CodecovOption, BrandedOption):
pass