Coverage for src/gitlabracadabra/mixins/webhooks.py: 80%

1#

2# Copyright (C) 2019-2022 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 import logging

18 from copy import deepcopy

19

20 from gitlabracadabra.objects.object import GitLabracadabraObject

21

22 logger = logging.getLogger(__name__)

23

24

25 class WebhooksMixin(GitLabracadabraObject):

26 """Object with webhooks."""

27

28 """_process_webhooks()

29

30 Process the webhooks param.

31 """

32

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

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

35 assert not skip_save # noqa: S101

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

37 current_webhooks = dict(

38 [[current_webhook.url, current_webhook] for current_webhook in self._obj.hooks.list(all=True)]

39 )

40 target_webhooks = dict([[target_webhook["url"], deepcopy(target_webhook)] for target_webhook in param_value])

41 # We first check for already existing webhooks

42 for current_webhook_url, current_webhook in sorted(current_webhooks.items()):

43 if current_webhook_url in target_webhooks:

44 for target_webhook_param_name, target_webhook_param_value in target_webhooks[

45 current_webhook_url

46 ].items():

47 current_webhook_param_value = getattr(current_webhook, target_webhook_param_name, None)

48 if current_webhook_param_value != target_webhook_param_value:

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

50 logger.info(

51 "[%s] NOT Changing webhook %s %s: %s -> %s (dry-run)",

52 self._name,

53 current_webhook_url,

54 target_webhook_param_name,

55 current_webhook_param_value,

56 target_webhook_param_value,

57 )

58 else:

59 logger.info(

60 "[%s] Changing webhook %s %s: %s -> %s",

61 self._name,

62 current_webhook_url,

63 target_webhook_param_name,

64 current_webhook_param_value,

65 target_webhook_param_value,

66 )

67 setattr(current_webhook, target_webhook_param_name, target_webhook_param_value)

68 current_webhook.save()

69 target_webhooks.pop(current_webhook_url)

70 elif unknown_webhooks in ["delete", "remove"]: 70 ↛ 76line 70 didn't jump to line 76 because the condition on line 70 was always true

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

72 logger.info("[%s] NOT Removing webhook %s (dry-run)", self._name, current_webhook_url)

73 else:

74 logger.info("[%s] Removing webhook %s", self._name, current_webhook_url)

75 current_webhook.delete()

76 elif unknown_webhooks not in ["ignore", "skip"]:

77 logger.warning("[%s] NOT Removing webhook: %s", self._name, current_webhook_url)

78 # Remaining webhooks

79 for target_webhook_name, target_webhook in sorted(target_webhooks.items()):

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

81 logger.info(

82 "[%s] NOT Adding webhook %s: %s -> %s (dry-run)",

83 self._name,

84 target_webhook_name,

85 None,

86 target_webhook,

87 )

88 else:

89 logger.info("[%s] Adding webhook %s: %s -> %s", self._name, target_webhook_name, None, target_webhook)

90 self._obj.hooks.create(target_webhook)