##// 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 from rhodecode.integrations.types.base import EEIntegration
33 from rhodecode.integrations.types.base import EEIntegration
34 from rhodecode.lib.caching_query import FromCache
34 from rhodecode.lib.caching_query import FromCache
35 from rhodecode.model import BaseModel
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 from rhodecode.integrations import integration_type_registry
37 from rhodecode.integrations import integration_type_registry
38
38
39 log = logging.getLogger(__name__)
39 log = logging.getLogger(__name__)
@@ -155,6 +155,7 b' class IntegrationModel(BaseModel):'
155 """
155 """
156 Get integrations that match an event
156 Get integrations that match an event
157 """
157 """
158 # base query
158 query = self.sa.query(
159 query = self.sa.query(
159 Integration
160 Integration
160 ).filter(
161 ).filter(
@@ -164,7 +165,7 b' class IntegrationModel(BaseModel):'
164 global_integrations_filter = and_(
165 global_integrations_filter = and_(
165 Integration.repo_id == None,
166 Integration.repo_id == None,
166 Integration.repo_group_id == None,
167 Integration.repo_group_id == None,
167 Integration.child_repos_only == False,
168 Integration.child_repos_only == false(),
168 )
169 )
169
170
170 if isinstance(event, events.RepoEvent):
171 if isinstance(event, events.RepoEvent):
@@ -177,42 +178,61 b' class IntegrationModel(BaseModel):'
177 clauses = [
178 clauses = [
178 global_integrations_filter,
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
186 # repo group integrations
182 if event.repo.repo_id: # pre create events dont have a repo_id yet
187 if event.repo.group:
183 clauses.append(
188 # repo group with only root level repos
184 Integration.repo_id == event.repo.repo_id
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:
194 clauses.append(group_child_repos_filter)
188 clauses.append(
195 cases.append(
189 and_(
196 (group_child_repos_filter, 3),
190 Integration.repo_group_id == event.repo.group.group_id,
191 Integration.child_repos_only == true()
192 )
193 )
197 )
198
194 # repo group cascade to kids
199 # repo group cascade to kids
195 clauses.append(
200 group_recursive_repos_filter = and_(
196 and_(
201 Integration.repo_group_id.in_(
197 Integration.repo_group_id.in_(
202 [group.group_id for group in event.repo.groups_with_parents]
198 [group.group_id for group in
203 ),
199 event.repo.groups_with_parents]
204 Integration.child_repos_only == false()
200 ),
205 )
201 Integration.child_repos_only == false()
206 clauses.append(group_recursive_repos_filter)
202 )
207 cases.append(
208 (group_recursive_repos_filter, 4),
203 )
209 )
204
210
205 if not event.repo.group: # root repo
211 if not event.repo.group: # root repo
206 clauses.append(root_repos_integrations_filter)
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 query = query.filter(or_(*clauses))
224 query = query.filter(or_(*clauses))
225 query = query.order_by(order_by_criterion)
209
226
210 if cache:
227 if cache:
211 cache_key = "get_enabled_repo_integrations_%i" % event.repo.repo_id
228 cache_key = "get_enabled_repo_integrations_%i" % event.repo.repo_id
212 query = query.options(
229 query = query.options(
213 FromCache("sql_cache_short", cache_key))
230 FromCache("sql_cache_short", cache_key))
214 else: # only global integrations
231 else: # only global integrations
232 order_by_criterion = Integration.integration_id
233
215 query = query.filter(global_integrations_filter)
234 query = query.filter(global_integrations_filter)
235 query = query.order_by(order_by_criterion)
216 if cache:
236 if cache:
217 query = query.options(
237 query = query.options(
218 FromCache("sql_cache_short", "get_enabled_global_integrations"))
238 FromCache("sql_cache_short", "get_enabled_global_integrations"))
@@ -128,7 +128,7 b' def integration_repos(request, StubInteg'
128 'root_repo': root_repo,
128 'root_repo': root_repo,
129 'other_repo': other_repo,
129 'other_repo': other_repo,
130 'parent_repo': parent_repo,
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 assert triggered_integrations == [
152 assert triggered_integrations == [
153 integrations['global'],
153 integrations['global'],
154 integrations['other_repo'],
155 integrations['other_group'],
154 integrations['other_group'],
156 integrations['other_group_recursive'],
155 integrations['other_group_recursive'],
156 integrations['other_repo'],
157 ]
157 ]
158
158
159 triggered_integrations = IntegrationModel().get_for_event(
159 triggered_integrations = IntegrationModel().get_for_event(
@@ -161,9 +161,9 b' def test_enabled_integration_repo_scopes'
161
161
162 assert triggered_integrations == [
162 assert triggered_integrations == [
163 integrations['global'],
163 integrations['global'],
164 integrations['parent_repo'],
165 integrations['parent_group'],
164 integrations['parent_group'],
166 integrations['parent_group_recursive'],
165 integrations['parent_group_recursive'],
166 integrations['parent_repo'],
167 ]
167 ]
168
168
169 triggered_integrations = IntegrationModel().get_for_event(
169 triggered_integrations = IntegrationModel().get_for_event(
@@ -171,10 +171,10 b' def test_enabled_integration_repo_scopes'
171
171
172 assert triggered_integrations == [
172 assert triggered_integrations == [
173 integrations['global'],
173 integrations['global'],
174 integrations['child_repo'],
174 integrations['child_group'],
175 integrations['parent_group_recursive'],
175 integrations['parent_group_recursive'],
176 integrations['child_group'],
177 integrations['child_group_recursive'],
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