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.pypi import PyPI

28 from gitlabracadabra.packages.raw import RawSource

29

30 if TYPE_CHECKING: 30 ↛ 31 line 30 didn't jump to line 31 because the condition on line 30 was never true

31 from gitlabracadabra.packages.source import Source

32

33 logger = getLogger(__name__)

34

35

36 class PackageMirrorsMixin(GitLabracadabraObject):

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

38

39 def _process_package_mirrors(

40 self,

41 param_name: str,

42 param_value: Any,

43 *,

44 dry_run: bool = False,

45 skip_save: bool = False,

46 ) -> None:

47 """Process the package_mirrors param.

48

49 Args:

50 param_name: "package_mirrors".

51 param_value: List of package mirror dicts.

52 dry_run: Dry run.

53 skip_save: False.

54 """

55 assert param_name == "package_mirrors" # noqa: S101

56 assert not skip_save # noqa: S101

57

58 destination = Gitlab(

59 connection=self.connection,

60 full_path=self._name,

61 project_id=self._obj.id,

62 )

63

64 for package_mirror in param_value:

65 if not package_mirror.get("enabled", True): 65 ↛ 66 line 65 didn't jump to line 66 because the condition on line 65 was never true

66 continue

67 for source_type, source_params in package_mirror.items():

68 destination.import_source(

69 self._get_source(source_type, deepcopy(source_params)),

70 dry_run=dry_run,

71 )

72

73 def _get_source(self, source_type: str, source_params: dict[str, Any]) -> Source:

74 source_class: type[Source] = {

75 "raw": RawSource,

76 "github": Github,

77 "helm": Helm,

78 "pypi": PyPI,

79 }[source_type]

80 source_params["log_prefix"] = f"[{self._name}] "

81 return source_class(**source_params)