##// END OF EJS Templates
events: ensure stable execution of integrations
marcink -
r3806:5bb0de26 stable
parent child Browse files
Show More
@@ -33,7 +33,7 b' from rhodecode import events'
33 33 from rhodecode.integrations.types.base import EEIntegration
34 34 from rhodecode.lib.caching_query import FromCache
35 35 from rhodecode.model import BaseModel
36 from rhodecode.model.db import Integration, Repository, RepoGroup, true, false
36 from rhodecode.model.db import Integration, Repository, RepoGroup, true, false, case
37 37 from rhodecode.integrations import integration_type_registry
38 38
39 39 log = logging.getLogger(__name__)
@@ -155,6 +155,7 b' class IntegrationModel(BaseModel):'
155 155 """
156 156 Get integrations that match an event
157 157 """
158 # base query
158 159 query = self.sa.query(
159 160 Integration
160 161 ).filter(
@@ -164,7 +165,7 b' class IntegrationModel(BaseModel):'
164 165 global_integrations_filter = and_(
165 166 Integration.repo_id == None,
166 167 Integration.repo_group_id == None,
167 Integration.child_repos_only == False,
168 Integration.child_repos_only == false(),
168 169 )
169 170
170 171 if isinstance(event, events.RepoEvent):
@@ -177,42 +178,61 b' class IntegrationModel(BaseModel):'
177 178 clauses = [
178 179 global_integrations_filter,
179 180 ]
181 cases = [
182 (global_integrations_filter, 1),
183 (root_repos_integrations_filter, 2),
184 ]
180 185
181 # repo integrations
182 if event.repo.repo_id: # pre create events dont have a repo_id yet
183 clauses.append(
184 Integration.repo_id == event.repo.repo_id
186 # repo group integrations
187 if event.repo.group:
188 # repo group with only root level repos
189 group_child_repos_filter = and_(
190 Integration.repo_group_id == event.repo.group.group_id,
191 Integration.child_repos_only == true()
185 192 )
186 193
187 if event.repo.group:
188 clauses.append(
189 and_(
190 Integration.repo_group_id == event.repo.group.group_id,
191 Integration.child_repos_only == true()
192 )
194 clauses.append(group_child_repos_filter)
195 cases.append(
196 (group_child_repos_filter, 3),
193 197 )
198
194 199 # repo group cascade to kids
195 clauses.append(
196 and_(
197 Integration.repo_group_id.in_(
198 [group.group_id for group in
199 event.repo.groups_with_parents]
200 ),
201 Integration.child_repos_only == false()
202 )
200 group_recursive_repos_filter = and_(
201 Integration.repo_group_id.in_(
202 [group.group_id for group in event.repo.groups_with_parents]
203 ),
204 Integration.child_repos_only == false()
205 )
206 clauses.append(group_recursive_repos_filter)
207 cases.append(
208 (group_recursive_repos_filter, 4),
203 209 )
204 210
205 211 if not event.repo.group: # root repo
206 212 clauses.append(root_repos_integrations_filter)
207 213
214 # repo integrations
215 if event.repo.repo_id: # pre create events dont have a repo_id yet
216 specific_repo_filter = Integration.repo_id == event.repo.repo_id
217 clauses.append(specific_repo_filter)
218 cases.append(
219 (specific_repo_filter, 5),
220 )
221
222 order_by_criterion = case(cases)
223
208 224 query = query.filter(or_(*clauses))
225 query = query.order_by(order_by_criterion)
209 226
210 227 if cache:
211 228 cache_key = "get_enabled_repo_integrations_%i" % event.repo.repo_id
212 229 query = query.options(
213 230 FromCache("sql_cache_short", cache_key))
214 231 else: # only global integrations
232 order_by_criterion = Integration.integration_id
233
215 234 query = query.filter(global_integrations_filter)
235 query = query.order_by(order_by_criterion)
216 236 if cache:
217 237 query = query.options(
218 238 FromCache("sql_cache_short", "get_enabled_global_integrations"))
@@ -128,7 +128,7 b' def integration_repos(request, StubInteg'
128 128 'root_repo': root_repo,
129 129 'other_repo': other_repo,
130 130 'parent_repo': parent_repo,
131 'child_repo': child_repo,
131 'child_repo': child_repo,
132 132 }
133 133 }
134 134
@@ -151,9 +151,9 b' def test_enabled_integration_repo_scopes'
151 151
152 152 assert triggered_integrations == [
153 153 integrations['global'],
154 integrations['other_repo'],
155 154 integrations['other_group'],
156 155 integrations['other_group_recursive'],
156 integrations['other_repo'],
157 157 ]
158 158
159 159 triggered_integrations = IntegrationModel().get_for_event(
@@ -161,9 +161,9 b' def test_enabled_integration_repo_scopes'
161 161
162 162 assert triggered_integrations == [
163 163 integrations['global'],
164 integrations['parent_repo'],
165 164 integrations['parent_group'],
166 165 integrations['parent_group_recursive'],
166 integrations['parent_repo'],
167 167 ]
168 168
169 169 triggered_integrations = IntegrationModel().get_for_event(
@@ -171,10 +171,10 b' def test_enabled_integration_repo_scopes'
171 171
172 172 assert triggered_integrations == [
173 173 integrations['global'],
174 integrations['child_repo'],
174 integrations['child_group'],
175 175 integrations['parent_group_recursive'],
176 integrations['child_group'],
177 176 integrations['child_group_recursive'],
177 integrations['child_repo'],
178 178 ]
179 179
180 180
General Comments 0
You need to be logged in to leave comments. Login now