Coverage for src/gitlabracadabra/mixins/variables.py: 64%

39 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-10 17:02 +0100

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 

17import logging 

18from copy import deepcopy 

19 

20from gitlabracadabra.objects.object import GitLabracadabraObject 

21 

22logger = logging.getLogger(__name__) 

23 

24 

25class VariablesMixin(GitLabracadabraObject): 

26 """Object with variables.""" 

27 

28 """_process_variables() 

29 

30 Process the variables param. 

31 """ 

32 

33 def _process_variables(self, param_name, param_value, *, dry_run=False, skip_save=False): 

34 assert param_name == "variables" # noqa: S101 

35 assert not skip_save # noqa: S101 

36 unknown_variables = self._content.get("unknown_variables", "warn") 

37 current_variables = dict( 

38 [[current_variable.key, current_variable] for current_variable in self._obj.variables.list(all=True)] 

39 ) 

40 target_variables = dict( 

41 [[target_variable["key"], deepcopy(target_variable)] for target_variable in param_value] 

42 ) 

43 # We first check for already existing variables 

44 for current_variable_key, current_variable in sorted(current_variables.items()): 

45 if current_variable_key in target_variables: 45 ↛ 83line 45 didn't jump to line 83 because the condition on line 45 was always true

46 for target_variable_param_name, target_variable_param_value in target_variables[ 

47 current_variable_key 

48 ].items(): 

49 try: 

50 current_variable_param_value = getattr(current_variable, target_variable_param_name) 

51 except AttributeError: 

52 logger.info( 

53 "[%s] NOT Changing variable %s %s: %s -> %s (current value is not available)", 

54 self._name, 

55 current_variable_key, 

56 target_variable_param_name, 

57 None, 

58 target_variable_param_value, 

59 ) 

60 continue 

61 if current_variable_param_value != target_variable_param_value: 

62 if dry_run: 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true

63 logger.info( 

64 "[%s] NOT Changing variable %s %s: %s -> %s (dry-run)", 

65 self._name, 

66 current_variable_key, 

67 target_variable_param_name, 

68 current_variable_param_value, 

69 target_variable_param_value, 

70 ) 

71 else: 

72 logger.info( 

73 "[%s] Changing variable %s %s: %s -> %s", 

74 self._name, 

75 current_variable_key, 

76 target_variable_param_name, 

77 current_variable_param_value, 

78 target_variable_param_value, 

79 ) 

80 setattr(current_variable, target_variable_param_name, target_variable_param_value) 

81 current_variable.save() 

82 target_variables.pop(current_variable_key) 

83 elif unknown_variables in ["delete", "remove"]: 

84 if dry_run: 

85 logger.info("[%s] NOT Removing variable %s (dry-run)", self._name, current_variable_key) 

86 else: 

87 logger.info("[%s] Removing variable %s", self._name, current_variable_key) 

88 current_variable.delete() 

89 elif unknown_variables not in ["ignore", "skip"]: 

90 logger.warning("[%s] NOT Removing variable: %s", self._name, current_variable_key) 

91 # Remaining variables 

92 for target_variable_name, target_variable in sorted(target_variables.items()): 

93 if dry_run: 93 ↛ 94line 93 didn't jump to line 94 because the condition on line 93 was never true

94 logger.info( 

95 "[%s] NOT Adding variable %s: %s -> %s (dry-run)", 

96 self._name, 

97 target_variable_name, 

98 None, 

99 target_variable, 

100 ) 

101 else: 

102 logger.info( 

103 "[%s] Adding variable %s: %s -> %s", self._name, target_variable_name, None, target_variable 

104 ) 

105 self._obj.variables.create(target_variable)