• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.74
/src/python/pants/backend/helm/util_rules/chart_metadata.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
3✔
5

6
import dataclasses
3✔
7
from dataclasses import dataclass
3✔
8
from enum import Enum
3✔
9
from typing import Any, cast
3✔
10

11
import yaml
3✔
12

13
from pants.backend.helm.target_types import HelmChartMetaSourceField
3✔
14
from pants.backend.helm.util_rules.sources import HelmChartRootRequest, find_chart_source_root
3✔
15
from pants.backend.helm.utils.yaml import snake_case_attr_dict
3✔
16
from pants.base.glob_match_error_behavior import GlobMatchErrorBehavior
3✔
17
from pants.engine.engine_aware import EngineAwareParameter
3✔
18
from pants.engine.fs import (
3✔
19
    CreateDigest,
20
    Digest,
21
    DigestSubset,
22
    FileContent,
23
    GlobExpansionConjunction,
24
    PathGlobs,
25
)
26
from pants.engine.internals.graph import hydrate_sources
3✔
27
from pants.engine.internals.native_engine import RemovePrefix
3✔
28
from pants.engine.internals.selectors import concurrently
3✔
29
from pants.engine.intrinsics import (
3✔
30
    create_digest,
31
    digest_subset_to_digest,
32
    get_digest_contents,
33
    remove_prefix,
34
)
35
from pants.engine.rules import collect_rules, implicitly, rule
3✔
36
from pants.engine.target import HydrateSourcesRequest
3✔
37
from pants.util.frozendict import FrozenDict
3✔
38
from pants.util.strutil import bullet_list
3✔
39

40

41
class ChartType(Enum):
3✔
42
    """Type of Helm Chart."""
43

44
    APPLICATION = "application"
3✔
45
    LIBRARY = "library"
3✔
46

47

48
class InvalidChartTypeValueError(ValueError):
3✔
49
    def __init__(self, value: str) -> None:
3✔
50
        super().__init__(
×
51
            f"Invalid value '{value}' for Helm Chart `type`. Valid values are: {[t.value for t in list(ChartType)]}"
52
        )
53

54

55
class MissingChartMetadataException(Exception):
3✔
56
    pass
3✔
57

58

59
class AmbiguousChartMetadataException(Exception):
3✔
60
    pass
3✔
61

62

63
@dataclass(frozen=True)
3✔
64
class HelmChartDependency:
3✔
65
    name: str
3✔
66
    repository: str | None = None
3✔
67
    version: str | None = None
3✔
68
    alias: str | None = None
3✔
69
    condition: str | None = None
3✔
70
    tags: tuple[str, ...] = dataclasses.field(default_factory=tuple)
3✔
71
    import_values: tuple[str, ...] = dataclasses.field(default_factory=tuple)
3✔
72

73
    @classmethod
3✔
74
    def from_dict(cls, d: dict[str, Any]) -> HelmChartDependency:
3✔
75
        attrs = snake_case_attr_dict(d)
×
76

77
        tags = attrs.pop("tags", [])
×
78
        import_values = attrs.pop("import_values", [])
×
79

80
        return cls(tags=tuple(tags), import_values=tuple(import_values), **attrs)
×
81

82
    def to_json_dict(self) -> dict[str, Any]:
3✔
83
        d: dict[str, Any] = {"name": self.name}
×
84
        if self.repository:
×
85
            d["repository"] = self.repository.rstrip("/")
×
86
        if self.version:
×
87
            d["version"] = self.version
×
88
        if self.alias:
×
89
            d["alias"] = self.alias
×
90
        if self.condition:
×
91
            d["condition"] = self.condition
×
92
        if self.tags:
×
93
            d["tags"] = list(self.tags)
×
94
        if self.import_values:
×
95
            d["import-values"] = list(self.import_values)
×
96
        return d
×
97

98

99
@dataclass(frozen=True)
3✔
100
class HelmChartMaintainer:
3✔
101
    name: str
3✔
102
    email: str | None = None
3✔
103
    url: str | None = None
3✔
104

105
    @classmethod
3✔
106
    def from_dict(cls, d: dict[str, Any]) -> HelmChartMaintainer:
3✔
107
        return cls(**d)
×
108

109
    def to_json_dict(self) -> dict[str, Any]:
3✔
110
        d = {"name": self.name}
×
111
        if self.email:
×
112
            d["email"] = self.email
×
113
        if self.url:
×
114
            d["url"] = self.url
×
115
        return d
×
116

117

118
DEFAULT_API_VERSION = "v2"
3✔
119

120

121
@dataclass(frozen=True)
3✔
122
class HelmChartMetadata:
3✔
123
    name: str
3✔
124
    version: str
3✔
125
    api_version: str = DEFAULT_API_VERSION
3✔
126
    type: ChartType = ChartType.APPLICATION
3✔
127
    kube_version: str | None = None
3✔
128
    app_version: str | None = None
3✔
129
    icon: str | None = None
3✔
130
    description: str | None = None
3✔
131
    dependencies: tuple[HelmChartDependency, ...] = dataclasses.field(default_factory=tuple)
3✔
132
    keywords: tuple[str, ...] = dataclasses.field(default_factory=tuple)
3✔
133
    sources: tuple[str, ...] = dataclasses.field(default_factory=tuple)
3✔
134
    home: str | None = None
3✔
135
    maintainers: tuple[HelmChartMaintainer, ...] = dataclasses.field(default_factory=tuple)
3✔
136
    deprecated: bool | None = None
3✔
137
    annotations: FrozenDict[str, str] = dataclasses.field(default_factory=FrozenDict)
3✔
138

139
    @classmethod
3✔
140
    def from_dict(cls, d: dict[str, Any]) -> HelmChartMetadata:
3✔
141
        chart_type: ChartType | None = None
×
142
        type_str = d.pop("type", None)
×
143
        if type_str:
×
144
            try:
×
145
                chart_type = ChartType(type_str)
×
146
            except KeyError:
×
147
                raise InvalidChartTypeValueError(type_str)
×
148

149
        # If the `apiVersion` is missing in the original `dict`, then we assume we are dealing with `v1` charts.
150
        api_version = d.pop("apiVersion", "v1")
×
151
        dependencies = [HelmChartDependency.from_dict(d) for d in d.pop("dependencies", [])]
×
152
        maintainers = [HelmChartMaintainer.from_dict(d) for d in d.pop("maintainers", [])]
×
153
        keywords = d.pop("keywords", [])
×
154
        sources = d.pop("sources", [])
×
155
        annotations = d.pop("annotations", {})
×
156

157
        attrs = snake_case_attr_dict(d)
×
158

159
        return cls(
×
160
            api_version=api_version,
161
            dependencies=tuple(dependencies),
162
            maintainers=tuple(maintainers),
163
            keywords=tuple(keywords),
164
            type=chart_type or ChartType.APPLICATION,
165
            annotations=FrozenDict(annotations),
166
            sources=tuple(sources),
167
            **attrs,
168
        )
169

170
    @classmethod
3✔
171
    def from_bytes(cls, content: bytes) -> HelmChartMetadata:
3✔
172
        return cls.from_dict(yaml.safe_load(content))
×
173

174
    @property
3✔
175
    def artifact_name(self) -> str:
3✔
176
        return f"{self.name}-{self.version}"
×
177

178
    def to_json_dict(self) -> dict[str, Any]:
3✔
179
        d: dict[str, Any] = {
×
180
            "apiVersion": self.api_version,
181
            "name": self.name,
182
            "version": self.version,
183
        }
184
        if self.api_version != "v1":
×
185
            d["type"] = self.type.value
×
186
        if self.icon:
×
187
            d["icon"] = self.icon
×
188
        if self.description:
×
189
            d["description"] = self.description
×
190
        if self.app_version:
×
191
            d["appVersion"] = self.app_version
×
192
        if self.kube_version:
×
193
            d["kubeVersion"] = self.kube_version
×
194
        if self.dependencies:
×
195
            d["dependencies"] = [item.to_json_dict() for item in self.dependencies]
×
196
        if self.maintainers:
×
197
            d["maintainers"] = [item.to_json_dict() for item in self.maintainers]
×
198
        if self.annotations:
×
199
            d["annotations"] = dict(self.annotations.items())
×
200
        if self.keywords:
×
201
            d["keywords"] = list(self.keywords)
×
202
        if self.sources:
×
203
            d["sources"] = list(self.sources)
×
204
        if self.home:
×
205
            d["home"] = self.home
×
206
        if self.deprecated:
×
207
            d["deprecated"] = self.deprecated
×
208
        return d
×
209

210
    def to_yaml(self) -> str:
3✔
211
        return cast("str", yaml.dump(self.to_json_dict()))
×
212

213

214
HELM_CHART_METADATA_FILENAMES = ["Chart.yaml", "Chart.yml"]
3✔
215

216

217
@dataclass(frozen=True)
3✔
218
class ParseHelmChartMetadataDigest(EngineAwareParameter):
3✔
219
    """Request to parse the Helm chart definition file (i.e. `Chart.yaml`) from the given digest.
220

221
    The definition file is expected to be at the root of the digest.
222
    """
223

224
    digest: Digest
3✔
225
    description_of_origin: str
3✔
226

227
    def debug_hint(self) -> str | None:
3✔
228
        return self.description_of_origin
×
229

230

231
@rule
3✔
232
async def parse_chart_metadata_from_digest(
3✔
233
    request: ParseHelmChartMetadataDigest,
234
) -> HelmChartMetadata:
235
    subset = await digest_subset_to_digest(
×
236
        DigestSubset(
237
            request.digest,
238
            PathGlobs(
239
                HELM_CHART_METADATA_FILENAMES,
240
                glob_match_error_behavior=GlobMatchErrorBehavior.error,
241
                conjunction=GlobExpansionConjunction.any_match,
242
                description_of_origin=request.description_of_origin,
243
            ),
244
        )
245
    )
246

247
    file_contents = await get_digest_contents(subset)
×
248

249
    if len(file_contents) == 0:
×
250
        raise MissingChartMetadataException(
×
251
            f"Could not find any file that matched with either {HELM_CHART_METADATA_FILENAMES} in target at {request.description_of_origin}."
252
        )
253
    if len(file_contents) > 1:
×
254
        raise AmbiguousChartMetadataException(
×
255
            f"Found more than one Helm chart metadata file at '{request.description_of_origin}':\n{bullet_list([f.path for f in file_contents])}"
256
        )
257

258
    return HelmChartMetadata.from_bytes(file_contents[0].content)
×
259

260

261
@rule
3✔
262
async def parse_chart_metadata_from_field(field: HelmChartMetaSourceField) -> HelmChartMetadata:
3✔
263
    chart_root, source_files = await concurrently(
×
264
        find_chart_source_root(HelmChartRootRequest(field)),
265
        hydrate_sources(
266
            HydrateSourcesRequest(
267
                field, for_sources_types=(HelmChartMetaSourceField,), enable_codegen=True
268
            ),
269
            **implicitly(),
270
        ),
271
    )
272

273
    metadata_digest = await remove_prefix(
×
274
        RemovePrefix(source_files.snapshot.digest, chart_root.path)
275
    )
276

277
    return await parse_chart_metadata_from_digest(
×
278
        ParseHelmChartMetadataDigest(
279
            metadata_digest,
280
            description_of_origin=f"the `helm_chart` {field.address.spec}",
281
        )
282
    )
283

284

285
@rule
3✔
286
async def render_chart_metadata(metadata: HelmChartMetadata) -> Digest:
3✔
287
    yaml_contents = bytes(metadata.to_yaml(), "utf-8")
×
288
    return await create_digest(
×
289
        CreateDigest([FileContent(HELM_CHART_METADATA_FILENAMES[0], yaml_contents)])
290
    )
291

292

293
def rules():
3✔
294
    return collect_rules()
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc