Coverage for src/gitlabracadabra/mixins/package_mirrors.py: 89%
28 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-05 21:52 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-05 21:52 +0200
1#
2# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Lesser General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
17from __future__ import annotations
19from copy import deepcopy
20from logging import getLogger
21from typing import TYPE_CHECKING, Any
23from gitlabracadabra.objects.object import GitLabracadabraObject
24from gitlabracadabra.packages.github import Github
25from gitlabracadabra.packages.gitlab import Gitlab
26from gitlabracadabra.packages.helm import Helm
27from gitlabracadabra.packages.pulp_manifest import PulpManifestSource
28from gitlabracadabra.packages.pypi import PyPI
29from gitlabracadabra.packages.raw import RawSource
31if TYPE_CHECKING: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true
32 from gitlabracadabra.packages.source import Source
34logger = getLogger(__name__)
37class PackageMirrorsMixin(GitLabracadabraObject):
38 """Object (Project) with package mirrors."""
40 def _process_package_mirrors(
41 self,
42 param_name: str,
43 param_value: Any,
44 *,
45 dry_run: bool = False,
46 skip_save: bool = False,
47 ) -> None:
48 """Process the package_mirrors param.
50 Args:
51 param_name: "package_mirrors".
52 param_value: List of package mirror dicts.
53 dry_run: Dry run.
54 skip_save: False.
55 """
56 assert param_name == "package_mirrors" # noqa: S101
57 assert not skip_save # noqa: S101
59 destination = Gitlab(
60 connection=self.connection,
61 full_path=self._name,
62 project_id=self._obj.id,
63 )
65 for package_mirror in param_value:
66 if not package_mirror.pop("enabled", True): 66 ↛ 67line 66 didn't jump to line 67 because the condition on line 66 was never true
67 continue
68 for source_type, source_params in package_mirror.items():
69 destination.import_source(
70 self._get_source(source_type, deepcopy(source_params)),
71 dry_run=dry_run,
72 )
74 def _get_source(self, source_type: str, source_params: dict[str, Any]) -> Source:
75 source_class: type[Source] = {
76 "github": Github,
77 "helm": Helm,
78 "pulp_manifest": PulpManifestSource,
79 "pypi": PyPI,
80 "raw": RawSource,
81 }[source_type]
82 source_params["log_prefix"] = f"[{self._name}] "
83 return source_class(**source_params)