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

pantsbuild / pants / 18913303678

29 Oct 2025 03:29PM UTC coverage: 80.004% (-0.3%) from 80.283%
18913303678

push

github

web-flow
Updating 3rd party lockfiles for PBS script and MyPy (#22796)

Also small bumps to fastapi and starlette, which updated over the last few days. Starlette updated for features, and then a security vulnerability.

Fastapi bumped just for starlette.

88 of 93 new or added lines in 73 files covered. (94.62%)

221 existing lines in 15 files now uncovered.

77334 of 96663 relevant lines covered (80.0%)

5.2 hits per line

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

89.53
/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
11✔
5

6
import dataclasses
11✔
7
from dataclasses import dataclass
11✔
8
from enum import Enum
11✔
9
from typing import Any
11✔
10

11
import yaml
11✔
12

13
from pants.backend.helm.target_types import HelmChartMetaSourceField
11✔
14
from pants.backend.helm.util_rules.sources import HelmChartRootRequest, find_chart_source_root
11✔
15
from pants.backend.helm.utils.yaml import snake_case_attr_dict
11✔
16
from pants.base.glob_match_error_behavior import GlobMatchErrorBehavior
11✔
17
from pants.engine.engine_aware import EngineAwareParameter
11✔
18
from pants.engine.fs import (
11✔
19
    CreateDigest,
20
    Digest,
21
    DigestSubset,
22
    FileContent,
23
    GlobExpansionConjunction,
24
    PathGlobs,
25
)
26
from pants.engine.internals.graph import hydrate_sources
11✔
27
from pants.engine.internals.native_engine import RemovePrefix
11✔
28
from pants.engine.internals.selectors import concurrently
11✔
29
from pants.engine.intrinsics import (
11✔
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
11✔
36
from pants.engine.target import HydrateSourcesRequest
11✔
37
from pants.util.frozendict import FrozenDict
11✔
38
from pants.util.strutil import bullet_list
11✔
39

40

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

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

47

48
class InvalidChartTypeValueError(ValueError):
11✔
49
    def __init__(self, value: str) -> None:
11✔
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):
11✔
56
    pass
11✔
57

58

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

62

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

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

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

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

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

98

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

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

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

117

118
DEFAULT_API_VERSION = "v2"
11✔
119

120

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

139
    @classmethod
11✔
140
    def from_dict(cls, d: dict[str, Any]) -> HelmChartMetadata:
11✔
141
        chart_type: ChartType | None = None
2✔
142
        type_str = d.pop("type", None)
2✔
143
        if type_str:
2✔
144
            try:
2✔
145
                chart_type = ChartType(type_str)
2✔
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")
2✔
151
        dependencies = [HelmChartDependency.from_dict(d) for d in d.pop("dependencies", [])]
2✔
152
        maintainers = [HelmChartMaintainer.from_dict(d) for d in d.pop("maintainers", [])]
2✔
153
        keywords = d.pop("keywords", [])
2✔
154
        sources = d.pop("sources", [])
2✔
155
        annotations = d.pop("annotations", {})
2✔
156

157
        attrs = snake_case_attr_dict(d)
2✔
158

159
        return cls(
2✔
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
11✔
171
    def from_bytes(cls, content: bytes) -> HelmChartMetadata:
11✔
172
        return cls.from_dict(yaml.safe_load(content))
2✔
173

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

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

210
    def to_yaml(self) -> str:
11✔
211
        return yaml.dump(self.to_json_dict())
×
212

213

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

216

217
@dataclass(frozen=True)
11✔
218
class ParseHelmChartMetadataDigest(EngineAwareParameter):
11✔
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
11✔
225
    description_of_origin: str
11✔
226

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

230

231
@rule
11✔
232
async def parse_chart_metadata_from_digest(
11✔
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
11✔
262
async def parse_chart_metadata_from_field(field: HelmChartMetaSourceField) -> HelmChartMetadata:
11✔
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
11✔
286
async def render_chart_metadata(metadata: HelmChartMetadata) -> Digest:
11✔
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():
11✔
294
    return collect_rules()
10✔
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