-
Notifications
You must be signed in to change notification settings - Fork 86
/
schedules_processor.py
122 lines (108 loc) 路 4.95 KB
/
schedules_processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from logging import debug
from typing import Dict, List
from gitlabform.gitlab import GitLab
from gitlabform.processors.abstract_processor import AbstractProcessor
class SchedulesProcessor(AbstractProcessor):
def __init__(self, gitlab: GitLab):
super().__init__("schedules", gitlab)
def _process_configuration(self, project_and_group: str, configuration: dict):
existing_schedules = self.gitlab.get_all_pipeline_schedules(project_and_group)
schedule_ids_by_description = self.__group_schedule_ids_by_description(
existing_schedules
)
for schedule_description in sorted(configuration["schedules"]):
schedule_ids = schedule_ids_by_description.get(schedule_description)
if configuration.get("schedules|" + schedule_description + "|delete"):
if schedule_ids:
debug("Deleting pipeline schedules '%s'", schedule_description)
for schedule_id in schedule_ids:
self.gitlab.delete_pipeline_schedule(
project_and_group, schedule_id
)
else:
debug(
"Not deleting pipeline schedules '%s', because none exist",
schedule_description,
)
else:
if schedule_ids and len(schedule_ids) == 1:
debug(
"Changing existing pipeline schedule '%s'", schedule_description
)
updated_schedule = self.gitlab.update_pipeline_schedule(
project_and_group,
schedule_ids[0],
configuration.get("schedules|" + schedule_description),
)
self.gitlab.take_ownership(
project_and_group, updated_schedule["id"]
)
self.__set_schedule_variables(
project_and_group,
updated_schedule.get("id"),
configuration.get(
"schedules|" + schedule_description + "|variables"
),
)
elif schedule_ids:
debug(
"Replacing existing pipeline schedules '%s'",
schedule_description,
)
for schedule_id in schedule_ids:
self.gitlab.delete_pipeline_schedule(
project_and_group, schedule_id
)
self.create_schedule_with_variables(
configuration, project_and_group, schedule_description
)
else:
debug("Creating pipeline schedule '%s'", schedule_description)
self.create_schedule_with_variables(
configuration, project_and_group, schedule_description
)
def create_schedule_with_variables(
self, configuration, project_and_group, schedule_description
):
data = configuration.get("schedules|" + schedule_description)
created_schedule = self.gitlab.create_pipeline_schedule(
project_and_group,
schedule_description,
data.get("ref"),
data.get("cron"),
optional_data=data,
)
self.__set_schedule_variables(
project_and_group,
created_schedule.get("id"),
configuration.get("schedules|" + schedule_description + "|variables"),
)
def __set_schedule_variables(self, project_and_group, schedule_id, variables):
schedule = self.gitlab.get_pipeline_schedule(project_and_group, schedule_id)
existing_variables = schedule.get("variables")
if existing_variables:
debug(
"Deleting variables for pipeline schedule '%s'", schedule["description"]
)
for variable in existing_variables:
self.gitlab.delete_pipeline_schedule_variable(
project_and_group, schedule_id, variable.get("key")
)
if variables:
for variable_key, variable_data in variables.items():
self.gitlab.create_pipeline_schedule_variable(
project_and_group,
schedule_id,
variable_key,
variable_data.get("value"),
variable_data,
)
@staticmethod
def __group_schedule_ids_by_description(schedules) -> Dict[str, List[str]]:
schedule_ids_by_description: Dict[str, List[str]] = {}
for schedule in schedules:
description = schedule["description"]
schedule_ids_by_description.setdefault(description, []).append(
schedule["id"]
)
return schedule_ids_by_description