Coverage for src/gitlabracadabra/mixins/package_mirrors.py: 89%

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/>.

16

17 from __future__ import annotations

18

19 from copy import deepcopy

20 from logging import getLogger

21 from typing import TYPE_CHECKING, Any

22

23 from gitlabracadabra.objects.object import GitLabracadabraObject

24 from gitlabracadabra.packages.github import Github

25 from gitlabracadabra.packages.gitlab import Gitlab

26 from gitlabracadabra.packages.helm import Helm

27 from gitlabracadabra.packages.pulp_manifest import PulpManifestSource

28 from gitlabracadabra.packages.pypi import PyPI

29 from gitlabracadabra.packages.raw import RawSource

30

31 if 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

33

34 logger = getLogger(__name__)

35

36

37 class PackageMirrorsMixin(GitLabracadabraObject):

38 """Object (Project) with package mirrors."""

39

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.

49

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

58

59 destination = Gitlab(

60 connection=self.connection,

61 full_path=self._name,

62 project_id=self._obj.id,

63 )

64

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 )

73

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)