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