##// END OF EJS Templates
chore(warnings): fixed sqlalchemy warning for use of cases syntax
super-admin -
r5200:382130ed default
parent child Browse files
Show More
@@ -1,237 +1,237 b''
1 1 # Copyright (C) 2011-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19
20 20 """
21 21 Model for integrations
22 22 """
23 23
24 24
25 25 import logging
26 26
27 27 from sqlalchemy import or_, and_
28 28
29 29 from rhodecode import events
30 30 from rhodecode.integrations.types.base import EEIntegration
31 31 from rhodecode.lib.caching_query import FromCache
32 32 from rhodecode.model import BaseModel
33 33 from rhodecode.model.db import Integration, Repository, RepoGroup, true, false, case, null
34 34 from rhodecode.integrations import integration_type_registry
35 35
36 36 log = logging.getLogger(__name__)
37 37
38 38
39 39 class IntegrationModel(BaseModel):
40 40
41 41 cls = Integration
42 42
43 43 def __get_integration(self, integration):
44 44 if isinstance(integration, Integration):
45 45 return integration
46 46 elif isinstance(integration, int):
47 47 return self.sa.query(Integration).get(integration)
48 48 else:
49 49 if integration:
50 50 raise Exception('integration must be int or Instance'
51 51 ' of Integration got %s' % type(integration))
52 52
53 53 def create(self, IntegrationType, name, enabled, repo, repo_group, child_repos_only, settings):
54 54 """ Create an IntegrationType integration """
55 55 integration = Integration()
56 56 integration.integration_type = IntegrationType.key
57 57 self.sa.add(integration)
58 58 self.update_integration(integration, name, enabled, repo, repo_group,
59 59 child_repos_only, settings)
60 60 self.sa.commit()
61 61 return integration
62 62
63 63 def update_integration(self, integration, name, enabled, repo, repo_group,
64 64 child_repos_only, settings):
65 65 integration = self.__get_integration(integration)
66 66
67 67 integration.repo = repo
68 68 integration.repo_group = repo_group
69 69 integration.child_repos_only = child_repos_only
70 70 integration.name = name
71 71 integration.enabled = enabled
72 72 integration.settings = settings
73 73
74 74 return integration
75 75
76 76 def delete(self, integration):
77 77 integration = self.__get_integration(integration)
78 78 if integration:
79 79 self.sa.delete(integration)
80 80 return True
81 81 return False
82 82
83 83 def get_integration_handler(self, integration):
84 84 TypeClass = integration_type_registry.get(integration.integration_type)
85 85 if not TypeClass:
86 86 log.error('No class could be found for integration type: {}'.format(
87 87 integration.integration_type))
88 88 return None
89 89 elif isinstance(TypeClass, EEIntegration) or issubclass(TypeClass, EEIntegration):
90 90 log.error('EE integration cannot be '
91 91 'executed for integration type: {}'.format(
92 92 integration.integration_type))
93 93 return None
94 94
95 95 return TypeClass(integration.settings)
96 96
97 97 def send_event(self, integration, event):
98 98 """ Send an event to an integration """
99 99 handler = self.get_integration_handler(integration)
100 100 if handler:
101 101 log.debug(
102 102 'events: sending event %s on integration %s using handler %s',
103 103 event, integration, handler)
104 104 handler.send_event(event)
105 105
106 106 def get_integrations(self, scope, IntegrationType=None):
107 107 """
108 108 Return integrations for a scope, which must be one of:
109 109
110 110 'all' - every integration, global/repogroup/repo
111 111 'global' - global integrations only
112 112 <Repository> instance - integrations for this repo only
113 113 <RepoGroup> instance - integrations for this repogroup only
114 114 """
115 115
116 116 if isinstance(scope, Repository):
117 117 query = self.sa.query(Integration).filter(
118 118 Integration.repo == scope)
119 119 elif isinstance(scope, RepoGroup):
120 120 query = self.sa.query(Integration).filter(
121 121 Integration.repo_group == scope)
122 122 elif scope == 'global':
123 123 # global integrations
124 124 query = self.sa.query(Integration).filter(
125 125 and_(Integration.repo_id == null(), Integration.repo_group_id == null())
126 126 )
127 127 elif scope == 'root-repos':
128 128 query = self.sa.query(Integration).filter(
129 129 and_(Integration.repo_id == null(),
130 130 Integration.repo_group_id == null(),
131 131 Integration.child_repos_only == true())
132 132 )
133 133 elif scope == 'all':
134 134 query = self.sa.query(Integration)
135 135 else:
136 136 raise Exception(
137 137 "invalid `scope`, must be one of: "
138 138 "['global', 'all', <Repository>, <RepoGroup>]")
139 139
140 140 if IntegrationType is not None:
141 141 query = query.filter(
142 142 Integration.integration_type==IntegrationType.key)
143 143
144 144 result = []
145 145 for integration in query.all():
146 146 IntType = integration_type_registry.get(integration.integration_type)
147 147 result.append((IntType, integration))
148 148 return result
149 149
150 150 def get_for_event(self, event, cache=False):
151 151 """
152 152 Get integrations that match an event
153 153 """
154 154 # base query
155 155 query = self.sa.query(
156 156 Integration
157 157 ).filter(
158 158 Integration.enabled == true()
159 159 )
160 160
161 161 global_integrations_filter = and_(
162 162 Integration.repo_id == null(),
163 163 Integration.repo_group_id == null(),
164 164 Integration.child_repos_only == false(),
165 165 )
166 166
167 167 if isinstance(event, events.RepoEvent):
168 168 root_repos_integrations_filter = and_(
169 169 Integration.repo_id == null(),
170 170 Integration.repo_group_id == null(),
171 171 Integration.child_repos_only == true(),
172 172 )
173 173
174 174 clauses = [
175 175 global_integrations_filter,
176 176 ]
177 177 cases = [
178 178 (global_integrations_filter, 1),
179 179 (root_repos_integrations_filter, 2),
180 180 ]
181 181
182 182 # repo group integrations
183 183 if event.repo.group:
184 184 # repo group with only root level repos
185 185 group_child_repos_filter = and_(
186 186 Integration.repo_group_id == event.repo.group.group_id,
187 187 Integration.child_repos_only == true()
188 188 )
189 189
190 190 clauses.append(group_child_repos_filter)
191 191 cases.append(
192 192 (group_child_repos_filter, 3),
193 193 )
194 194
195 195 # repo group cascade to kids
196 196 group_recursive_repos_filter = and_(
197 197 Integration.repo_group_id.in_(
198 198 [group.group_id for group in event.repo.groups_with_parents]
199 199 ),
200 200 Integration.child_repos_only == false()
201 201 )
202 202 clauses.append(group_recursive_repos_filter)
203 203 cases.append(
204 204 (group_recursive_repos_filter, 4),
205 205 )
206 206
207 207 if not event.repo.group: # root repo
208 208 clauses.append(root_repos_integrations_filter)
209 209
210 210 # repo integrations
211 211 if event.repo.repo_id: # pre create events dont have a repo_id yet
212 212 specific_repo_filter = Integration.repo_id == event.repo.repo_id
213 213 clauses.append(specific_repo_filter)
214 214 cases.append(
215 215 (specific_repo_filter, 5),
216 216 )
217 217
218 order_by_criterion = case(cases)
218 order_by_criterion = case(*cases)
219 219
220 220 query = query.filter(or_(*clauses))
221 221 query = query.order_by(order_by_criterion)
222 222
223 223 if cache:
224 224 cache_key = f"get_enabled_repo_integrations_{event.repo.repo_id}"
225 225 query = query.options(
226 226 FromCache("sql_cache_short", cache_key))
227 227 else: # only global integrations
228 228 order_by_criterion = Integration.integration_id
229 229
230 230 query = query.filter(global_integrations_filter)
231 231 query = query.order_by(order_by_criterion)
232 232 if cache:
233 233 query = query.options(
234 234 FromCache("sql_cache_short", "get_enabled_global_integrations"))
235 235
236 236 result = query.all()
237 237 return result
General Comments 0
You need to be logged in to leave comments. Login now