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