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

pantsbuild / pants / 18517631058

15 Oct 2025 04:18AM UTC coverage: 69.207% (-11.1%) from 80.267%
18517631058

Pull #22745

github

web-flow
Merge 642a76ca1 into 99919310e
Pull Request #22745: [windows] Add windows support in the stdio crate.

53815 of 77759 relevant lines covered (69.21%)

2.42 hits per line

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

67.68
/src/python/pants/backend/helm/resolve/artifacts.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
6✔
5

6
from abc import ABC, abstractmethod
6✔
7
from collections.abc import Iterable
6✔
8
from dataclasses import dataclass
6✔
9
from typing import Any, cast
6✔
10

11
from pants.backend.helm.subsystems.helm import HelmSubsystem
6✔
12
from pants.backend.helm.target_types import (
6✔
13
    AllHelmArtifactTargets,
14
    HelmArtifactFieldSet,
15
    HelmArtifactRegistryField,
16
    HelmArtifactRepositoryField,
17
    HelmChartTarget,
18
)
19
from pants.backend.helm.util_rules.chart_metadata import rules as metadata_rules
6✔
20
from pants.engine.addresses import Address
6✔
21
from pants.engine.engine_aware import EngineAwareReturnType
6✔
22
from pants.engine.rules import collect_rules, concurrently, implicitly, rule
6✔
23
from pants.engine.target import Target
6✔
24
from pants.util.frozendict import FrozenDict
6✔
25
from pants.util.strutil import bullet_list
6✔
26

27

28
class MissingHelmArtifactLocation(ValueError):
6✔
29
    def __init__(self, address: Address) -> None:
6✔
30
        super().__init__(
×
31
            f"Target at address '{address}' needs to specify either `{HelmArtifactRegistryField.alias}`, "
32
            f"`{HelmArtifactRepositoryField.alias}` or both."
33
        )
34

35

36
class DuplicateHelmChartNamesFound(Exception):
6✔
37
    def __init__(self, duplicates: Iterable[tuple[str, Address]]) -> None:
6✔
38
        super().__init__(
×
39
            f"Found more than one `{HelmChartTarget.alias}` target using the same chart name:\n\n"
40
            f"{bullet_list([f'{addr} -> {name}' for name, addr in duplicates])}"
41
        )
42

43

44
class HelmArtifactLocationSpec(ABC):
6✔
45
    @property
46
    @abstractmethod
47
    def spec(self) -> str: ...
48

49
    @property
6✔
50
    def is_url(self) -> bool:
6✔
51
        return len(self.spec.split("://")) == 2
×
52

53
    @property
6✔
54
    def is_alias(self) -> bool:
6✔
55
        return self.spec.startswith("@")
×
56

57

58
@dataclass(frozen=True)
6✔
59
class HelmArtifactRegistryLocationSpec(HelmArtifactLocationSpec):
6✔
60
    registry: str
6✔
61
    repository: str | None
6✔
62

63
    @property
6✔
64
    def spec(self) -> str:
6✔
65
        return self.registry
×
66

67

68
@dataclass(frozen=True)
6✔
69
class HelmArtifactClassicRepositoryLocationSpec(HelmArtifactLocationSpec):
6✔
70
    repository: str
6✔
71

72
    @property
6✔
73
    def spec(self) -> str:
6✔
74
        return self.repository
×
75

76

77
@dataclass(frozen=True)
6✔
78
class HelmArtifactRequirement:
6✔
79
    name: str
6✔
80
    version: str
6✔
81
    location: HelmArtifactLocationSpec
6✔
82

83

84
@dataclass(frozen=True)
6✔
85
class HelmArtifact:
6✔
86
    requirement: HelmArtifactRequirement
6✔
87
    address: Address
6✔
88

89
    @classmethod
6✔
90
    def from_target(cls, target: Target) -> HelmArtifact:
6✔
91
        return cls.from_field_set(HelmArtifactFieldSet.create(target))
×
92

93
    @classmethod
6✔
94
    def from_field_set(cls, field_set: HelmArtifactFieldSet) -> HelmArtifact:
6✔
95
        registry = field_set.registry.value
×
96
        repository = field_set.repository.value
×
97
        if not registry and not repository:
×
98
            raise MissingHelmArtifactLocation(field_set.address)
×
99

100
        registry_location: HelmArtifactRegistryLocationSpec | None = None
×
101
        if registry:
×
102
            registry_location = HelmArtifactRegistryLocationSpec(registry.rstrip("/"), repository)
×
103

104
        location = registry_location or HelmArtifactClassicRepositoryLocationSpec(
×
105
            cast(str, repository).rstrip("/")
106
        )
107
        req = HelmArtifactRequirement(
×
108
            name=cast(str, field_set.artifact.value),
109
            version=cast(str, field_set.version.value),
110
            location=location,
111
        )
112

113
        return cls(requirement=req, address=field_set.address)
×
114

115
    @property
6✔
116
    def name(self) -> str:
6✔
117
        return self.requirement.name
×
118

119
    @property
6✔
120
    def version(self) -> str:
6✔
121
        return self.requirement.version
×
122

123

124
@dataclass(frozen=True)
6✔
125
class ResolvedHelmArtifact(HelmArtifact, EngineAwareReturnType):
6✔
126
    location_url: str
6✔
127

128
    @classmethod
6✔
129
    def from_unresolved(cls, artifact: HelmArtifact, *, location_url: str) -> ResolvedHelmArtifact:
6✔
130
        return cls(
×
131
            requirement=artifact.requirement,
132
            address=artifact.address,
133
            location_url=location_url,
134
        )
135

136
    @property
6✔
137
    def chart_url(self) -> str:
6✔
138
        return f"{self.location_url}/{self.name}"
×
139

140
    def metadata(self) -> dict[str, Any] | None:
6✔
141
        return {
×
142
            "name": self.requirement.name,
143
            "version": self.requirement.version,
144
            "location": self.requirement.location.spec,
145
            "address": self.address.spec,
146
            "url": self.chart_url,
147
        }
148

149

150
@rule
6✔
151
async def resolved_helm_artifact(
6✔
152
    artifact: HelmArtifact, subsystem: HelmSubsystem
153
) -> ResolvedHelmArtifact:
154
    remotes = subsystem.remotes()
×
155

156
    candidate_remotes = list(remotes.get(artifact.requirement.location.spec))
×
157
    if candidate_remotes:
×
158
        loc_url = candidate_remotes[0].address
×
159
        if isinstance(artifact.requirement.location, HelmArtifactRegistryLocationSpec):
×
160
            loc_url = f"{loc_url}/{artifact.requirement.location.repository or ''}".rstrip("/")
×
161
    else:
162
        loc_url = artifact.requirement.location.spec
×
163

164
    return ResolvedHelmArtifact.from_unresolved(artifact, location_url=loc_url)
×
165

166

167
class ThirdPartyHelmArtifactMapping(FrozenDict[str, Address]):
6✔
168
    pass
6✔
169

170

171
@rule
6✔
172
async def third_party_helm_artifact_mapping(
6✔
173
    all_helm_artifact_tgts: AllHelmArtifactTargets,
174
) -> ThirdPartyHelmArtifactMapping:
175
    artifacts = await concurrently(
×
176
        resolved_helm_artifact(**implicitly({HelmArtifact.from_target(tgt): HelmArtifact}))
177
        for tgt in all_helm_artifact_tgts
178
    )
179
    return ThirdPartyHelmArtifactMapping(
×
180
        {artifact.chart_url: artifact.address for artifact in artifacts}
181
    )
182

183

184
def rules():
6✔
185
    return [*collect_rules(), *metadata_rules()]
5✔
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