##// END OF EJS Templates
pull-requests: make the renderer stored and saved for each pull requests....
marcink -
r2903:6d16d1cd default
parent child Browse files
Show More
@@ -0,0 +1,40 b''
1 import logging
2
3 from sqlalchemy import *
4
5 from rhodecode.model import meta
6 from rhodecode.lib.dbmigrate.versions import _reset_base, notify
7
8 log = logging.getLogger(__name__)
9
10
11 def upgrade(migrate_engine):
12 """
13 Upgrade operations go here.
14 Don't create your own engine; bind migrate_engine to your metadata
15 """
16 _reset_base(migrate_engine)
17 from rhodecode.lib.dbmigrate.schema import db_4_11_0_0 as db
18
19 pull_request_table = db.PullRequest.__table__
20 pull_request_version_table = db.PullRequestVersion.__table__
21
22 renderer = Column('description_renderer', Unicode(64), nullable=True)
23 renderer.create(table=pull_request_table)
24
25 renderer_ver = Column('description_renderer', Unicode(64), nullable=True)
26 renderer_ver.create(table=pull_request_version_table)
27
28 # issue fixups
29 fixups(db, meta.Session)
30
31
32 def downgrade(migrate_engine):
33 meta = MetaData()
34 meta.bind = migrate_engine
35
36
37 def fixups(models, _SESSION):
38 pass
39
40
@@ -1,63 +1,63 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2018 RhodeCode GmbH
3 # Copyright (C) 2010-2018 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 RhodeCode, a web based repository management software
23 RhodeCode, a web based repository management software
24 versioning implementation: http://www.python.org/dev/peps/pep-0386/
24 versioning implementation: http://www.python.org/dev/peps/pep-0386/
25 """
25 """
26
26
27 import os
27 import os
28 import sys
28 import sys
29 import platform
29 import platform
30
30
31 VERSION = tuple(open(os.path.join(
31 VERSION = tuple(open(os.path.join(
32 os.path.dirname(__file__), 'VERSION')).read().split('.'))
32 os.path.dirname(__file__), 'VERSION')).read().split('.'))
33
33
34 BACKENDS = {
34 BACKENDS = {
35 'hg': 'Mercurial repository',
35 'hg': 'Mercurial repository',
36 'git': 'Git repository',
36 'git': 'Git repository',
37 'svn': 'Subversion repository',
37 'svn': 'Subversion repository',
38 }
38 }
39
39
40 CELERY_ENABLED = False
40 CELERY_ENABLED = False
41 CELERY_EAGER = False
41 CELERY_EAGER = False
42
42
43 # link to config for pyramid
43 # link to config for pyramid
44 CONFIG = {}
44 CONFIG = {}
45
45
46 # Populated with the settings dictionary from application init in
46 # Populated with the settings dictionary from application init in
47 # rhodecode.conf.environment.load_pyramid_environment
47 # rhodecode.conf.environment.load_pyramid_environment
48 PYRAMID_SETTINGS = {}
48 PYRAMID_SETTINGS = {}
49
49
50 # Linked module for extensions
50 # Linked module for extensions
51 EXTENSIONS = {}
51 EXTENSIONS = {}
52
52
53 __version__ = ('.'.join((str(each) for each in VERSION[:3])))
53 __version__ = ('.'.join((str(each) for each in VERSION[:3])))
54 __dbversion__ = 86 # defines current db version for migrations
54 __dbversion__ = 87 # defines current db version for migrations
55 __platform__ = platform.system()
55 __platform__ = platform.system()
56 __license__ = 'AGPLv3, and Commercial License'
56 __license__ = 'AGPLv3, and Commercial License'
57 __author__ = 'RhodeCode GmbH'
57 __author__ = 'RhodeCode GmbH'
58 __url__ = 'https://code.rhodecode.com'
58 __url__ = 'https://code.rhodecode.com'
59
59
60 is_windows = __platform__ in ['Windows']
60 is_windows = __platform__ in ['Windows']
61 is_unix = not is_windows
61 is_unix = not is_windows
62 is_test = False
62 is_test = False
63 disable_error_handler = False
63 disable_error_handler = False
@@ -1,1203 +1,1206 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2018 RhodeCode GmbH
3 # Copyright (C) 2010-2018 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 import mock
20 import mock
21 import pytest
21 import pytest
22
22
23 import rhodecode
23 import rhodecode
24 from rhodecode.lib.vcs.backends.base import MergeResponse, MergeFailureReason
24 from rhodecode.lib.vcs.backends.base import MergeResponse, MergeFailureReason
25 from rhodecode.lib.vcs.nodes import FileNode
25 from rhodecode.lib.vcs.nodes import FileNode
26 from rhodecode.lib import helpers as h
26 from rhodecode.lib import helpers as h
27 from rhodecode.model.changeset_status import ChangesetStatusModel
27 from rhodecode.model.changeset_status import ChangesetStatusModel
28 from rhodecode.model.db import (
28 from rhodecode.model.db import (
29 PullRequest, ChangesetStatus, UserLog, Notification, ChangesetComment)
29 PullRequest, ChangesetStatus, UserLog, Notification, ChangesetComment)
30 from rhodecode.model.meta import Session
30 from rhodecode.model.meta import Session
31 from rhodecode.model.pull_request import PullRequestModel
31 from rhodecode.model.pull_request import PullRequestModel
32 from rhodecode.model.user import UserModel
32 from rhodecode.model.user import UserModel
33 from rhodecode.tests import (
33 from rhodecode.tests import (
34 assert_session_flash, TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN)
34 assert_session_flash, TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN)
35 from rhodecode.tests.utils import AssertResponse
35 from rhodecode.tests.utils import AssertResponse
36
36
37
37
38 def route_path(name, params=None, **kwargs):
38 def route_path(name, params=None, **kwargs):
39 import urllib
39 import urllib
40
40
41 base_url = {
41 base_url = {
42 'repo_changelog': '/{repo_name}/changelog',
42 'repo_changelog': '/{repo_name}/changelog',
43 'repo_changelog_file': '/{repo_name}/changelog/{commit_id}/{f_path}',
43 'repo_changelog_file': '/{repo_name}/changelog/{commit_id}/{f_path}',
44 'pullrequest_show': '/{repo_name}/pull-request/{pull_request_id}',
44 'pullrequest_show': '/{repo_name}/pull-request/{pull_request_id}',
45 'pullrequest_show_all': '/{repo_name}/pull-request',
45 'pullrequest_show_all': '/{repo_name}/pull-request',
46 'pullrequest_show_all_data': '/{repo_name}/pull-request-data',
46 'pullrequest_show_all_data': '/{repo_name}/pull-request-data',
47 'pullrequest_repo_refs': '/{repo_name}/pull-request/refs/{target_repo_name:.*?[^/]}',
47 'pullrequest_repo_refs': '/{repo_name}/pull-request/refs/{target_repo_name:.*?[^/]}',
48 'pullrequest_repo_destinations': '/{repo_name}/pull-request/repo-destinations',
48 'pullrequest_repo_destinations': '/{repo_name}/pull-request/repo-destinations',
49 'pullrequest_new': '/{repo_name}/pull-request/new',
49 'pullrequest_new': '/{repo_name}/pull-request/new',
50 'pullrequest_create': '/{repo_name}/pull-request/create',
50 'pullrequest_create': '/{repo_name}/pull-request/create',
51 'pullrequest_update': '/{repo_name}/pull-request/{pull_request_id}/update',
51 'pullrequest_update': '/{repo_name}/pull-request/{pull_request_id}/update',
52 'pullrequest_merge': '/{repo_name}/pull-request/{pull_request_id}/merge',
52 'pullrequest_merge': '/{repo_name}/pull-request/{pull_request_id}/merge',
53 'pullrequest_delete': '/{repo_name}/pull-request/{pull_request_id}/delete',
53 'pullrequest_delete': '/{repo_name}/pull-request/{pull_request_id}/delete',
54 'pullrequest_comment_create': '/{repo_name}/pull-request/{pull_request_id}/comment',
54 'pullrequest_comment_create': '/{repo_name}/pull-request/{pull_request_id}/comment',
55 'pullrequest_comment_delete': '/{repo_name}/pull-request/{pull_request_id}/comment/{comment_id}/delete',
55 'pullrequest_comment_delete': '/{repo_name}/pull-request/{pull_request_id}/comment/{comment_id}/delete',
56 }[name].format(**kwargs)
56 }[name].format(**kwargs)
57
57
58 if params:
58 if params:
59 base_url = '{}?{}'.format(base_url, urllib.urlencode(params))
59 base_url = '{}?{}'.format(base_url, urllib.urlencode(params))
60 return base_url
60 return base_url
61
61
62
62
63 @pytest.mark.usefixtures('app', 'autologin_user')
63 @pytest.mark.usefixtures('app', 'autologin_user')
64 @pytest.mark.backends("git", "hg")
64 @pytest.mark.backends("git", "hg")
65 class TestPullrequestsView(object):
65 class TestPullrequestsView(object):
66
66
67 def test_index(self, backend):
67 def test_index(self, backend):
68 self.app.get(route_path(
68 self.app.get(route_path(
69 'pullrequest_new',
69 'pullrequest_new',
70 repo_name=backend.repo_name))
70 repo_name=backend.repo_name))
71
71
72 def test_option_menu_create_pull_request_exists(self, backend):
72 def test_option_menu_create_pull_request_exists(self, backend):
73 repo_name = backend.repo_name
73 repo_name = backend.repo_name
74 response = self.app.get(h.route_path('repo_summary', repo_name=repo_name))
74 response = self.app.get(h.route_path('repo_summary', repo_name=repo_name))
75
75
76 create_pr_link = '<a href="%s">Create Pull Request</a>' % route_path(
76 create_pr_link = '<a href="%s">Create Pull Request</a>' % route_path(
77 'pullrequest_new', repo_name=repo_name)
77 'pullrequest_new', repo_name=repo_name)
78 response.mustcontain(create_pr_link)
78 response.mustcontain(create_pr_link)
79
79
80 def test_create_pr_form_with_raw_commit_id(self, backend):
80 def test_create_pr_form_with_raw_commit_id(self, backend):
81 repo = backend.repo
81 repo = backend.repo
82
82
83 self.app.get(
83 self.app.get(
84 route_path('pullrequest_new',
84 route_path('pullrequest_new',
85 repo_name=repo.repo_name,
85 repo_name=repo.repo_name,
86 commit=repo.get_commit().raw_id),
86 commit=repo.get_commit().raw_id),
87 status=200)
87 status=200)
88
88
89 @pytest.mark.parametrize('pr_merge_enabled', [True, False])
89 @pytest.mark.parametrize('pr_merge_enabled', [True, False])
90 def test_show(self, pr_util, pr_merge_enabled):
90 def test_show(self, pr_util, pr_merge_enabled):
91 pull_request = pr_util.create_pull_request(
91 pull_request = pr_util.create_pull_request(
92 mergeable=pr_merge_enabled, enable_notifications=False)
92 mergeable=pr_merge_enabled, enable_notifications=False)
93
93
94 response = self.app.get(route_path(
94 response = self.app.get(route_path(
95 'pullrequest_show',
95 'pullrequest_show',
96 repo_name=pull_request.target_repo.scm_instance().name,
96 repo_name=pull_request.target_repo.scm_instance().name,
97 pull_request_id=pull_request.pull_request_id))
97 pull_request_id=pull_request.pull_request_id))
98
98
99 for commit_id in pull_request.revisions:
99 for commit_id in pull_request.revisions:
100 response.mustcontain(commit_id)
100 response.mustcontain(commit_id)
101
101
102 assert pull_request.target_ref_parts.type in response
102 assert pull_request.target_ref_parts.type in response
103 assert pull_request.target_ref_parts.name in response
103 assert pull_request.target_ref_parts.name in response
104 target_clone_url = pull_request.target_repo.clone_url()
104 target_clone_url = pull_request.target_repo.clone_url()
105 assert target_clone_url in response
105 assert target_clone_url in response
106
106
107 assert 'class="pull-request-merge"' in response
107 assert 'class="pull-request-merge"' in response
108 assert (
108 assert (
109 'Server-side pull request merging is disabled.'
109 'Server-side pull request merging is disabled.'
110 in response) != pr_merge_enabled
110 in response) != pr_merge_enabled
111
111
112 def test_close_status_visibility(self, pr_util, user_util, csrf_token):
112 def test_close_status_visibility(self, pr_util, user_util, csrf_token):
113 # Logout
113 # Logout
114 response = self.app.post(
114 response = self.app.post(
115 h.route_path('logout'),
115 h.route_path('logout'),
116 params={'csrf_token': csrf_token})
116 params={'csrf_token': csrf_token})
117 # Login as regular user
117 # Login as regular user
118 response = self.app.post(h.route_path('login'),
118 response = self.app.post(h.route_path('login'),
119 {'username': TEST_USER_REGULAR_LOGIN,
119 {'username': TEST_USER_REGULAR_LOGIN,
120 'password': 'test12'})
120 'password': 'test12'})
121
121
122 pull_request = pr_util.create_pull_request(
122 pull_request = pr_util.create_pull_request(
123 author=TEST_USER_REGULAR_LOGIN)
123 author=TEST_USER_REGULAR_LOGIN)
124
124
125 response = self.app.get(route_path(
125 response = self.app.get(route_path(
126 'pullrequest_show',
126 'pullrequest_show',
127 repo_name=pull_request.target_repo.scm_instance().name,
127 repo_name=pull_request.target_repo.scm_instance().name,
128 pull_request_id=pull_request.pull_request_id))
128 pull_request_id=pull_request.pull_request_id))
129
129
130 response.mustcontain('Server-side pull request merging is disabled.')
130 response.mustcontain('Server-side pull request merging is disabled.')
131
131
132 assert_response = response.assert_response()
132 assert_response = response.assert_response()
133 # for regular user without a merge permissions, we don't see it
133 # for regular user without a merge permissions, we don't see it
134 assert_response.no_element_exists('#close-pull-request-action')
134 assert_response.no_element_exists('#close-pull-request-action')
135
135
136 user_util.grant_user_permission_to_repo(
136 user_util.grant_user_permission_to_repo(
137 pull_request.target_repo,
137 pull_request.target_repo,
138 UserModel().get_by_username(TEST_USER_REGULAR_LOGIN),
138 UserModel().get_by_username(TEST_USER_REGULAR_LOGIN),
139 'repository.write')
139 'repository.write')
140 response = self.app.get(route_path(
140 response = self.app.get(route_path(
141 'pullrequest_show',
141 'pullrequest_show',
142 repo_name=pull_request.target_repo.scm_instance().name,
142 repo_name=pull_request.target_repo.scm_instance().name,
143 pull_request_id=pull_request.pull_request_id))
143 pull_request_id=pull_request.pull_request_id))
144
144
145 response.mustcontain('Server-side pull request merging is disabled.')
145 response.mustcontain('Server-side pull request merging is disabled.')
146
146
147 assert_response = response.assert_response()
147 assert_response = response.assert_response()
148 # now regular user has a merge permissions, we have CLOSE button
148 # now regular user has a merge permissions, we have CLOSE button
149 assert_response.one_element_exists('#close-pull-request-action')
149 assert_response.one_element_exists('#close-pull-request-action')
150
150
151 def test_show_invalid_commit_id(self, pr_util):
151 def test_show_invalid_commit_id(self, pr_util):
152 # Simulating invalid revisions which will cause a lookup error
152 # Simulating invalid revisions which will cause a lookup error
153 pull_request = pr_util.create_pull_request()
153 pull_request = pr_util.create_pull_request()
154 pull_request.revisions = ['invalid']
154 pull_request.revisions = ['invalid']
155 Session().add(pull_request)
155 Session().add(pull_request)
156 Session().commit()
156 Session().commit()
157
157
158 response = self.app.get(route_path(
158 response = self.app.get(route_path(
159 'pullrequest_show',
159 'pullrequest_show',
160 repo_name=pull_request.target_repo.scm_instance().name,
160 repo_name=pull_request.target_repo.scm_instance().name,
161 pull_request_id=pull_request.pull_request_id))
161 pull_request_id=pull_request.pull_request_id))
162
162
163 for commit_id in pull_request.revisions:
163 for commit_id in pull_request.revisions:
164 response.mustcontain(commit_id)
164 response.mustcontain(commit_id)
165
165
166 def test_show_invalid_source_reference(self, pr_util):
166 def test_show_invalid_source_reference(self, pr_util):
167 pull_request = pr_util.create_pull_request()
167 pull_request = pr_util.create_pull_request()
168 pull_request.source_ref = 'branch:b:invalid'
168 pull_request.source_ref = 'branch:b:invalid'
169 Session().add(pull_request)
169 Session().add(pull_request)
170 Session().commit()
170 Session().commit()
171
171
172 self.app.get(route_path(
172 self.app.get(route_path(
173 'pullrequest_show',
173 'pullrequest_show',
174 repo_name=pull_request.target_repo.scm_instance().name,
174 repo_name=pull_request.target_repo.scm_instance().name,
175 pull_request_id=pull_request.pull_request_id))
175 pull_request_id=pull_request.pull_request_id))
176
176
177 def test_edit_title_description(self, pr_util, csrf_token):
177 def test_edit_title_description(self, pr_util, csrf_token):
178 pull_request = pr_util.create_pull_request()
178 pull_request = pr_util.create_pull_request()
179 pull_request_id = pull_request.pull_request_id
179 pull_request_id = pull_request.pull_request_id
180
180
181 response = self.app.post(
181 response = self.app.post(
182 route_path('pullrequest_update',
182 route_path('pullrequest_update',
183 repo_name=pull_request.target_repo.repo_name,
183 repo_name=pull_request.target_repo.repo_name,
184 pull_request_id=pull_request_id),
184 pull_request_id=pull_request_id),
185 params={
185 params={
186 'edit_pull_request': 'true',
186 'edit_pull_request': 'true',
187 'title': 'New title',
187 'title': 'New title',
188 'description': 'New description',
188 'description': 'New description',
189 'csrf_token': csrf_token})
189 'csrf_token': csrf_token})
190
190
191 assert_session_flash(
191 assert_session_flash(
192 response, u'Pull request title & description updated.',
192 response, u'Pull request title & description updated.',
193 category='success')
193 category='success')
194
194
195 pull_request = PullRequest.get(pull_request_id)
195 pull_request = PullRequest.get(pull_request_id)
196 assert pull_request.title == 'New title'
196 assert pull_request.title == 'New title'
197 assert pull_request.description == 'New description'
197 assert pull_request.description == 'New description'
198
198
199 def test_edit_title_description_closed(self, pr_util, csrf_token):
199 def test_edit_title_description_closed(self, pr_util, csrf_token):
200 pull_request = pr_util.create_pull_request()
200 pull_request = pr_util.create_pull_request()
201 pull_request_id = pull_request.pull_request_id
201 pull_request_id = pull_request.pull_request_id
202 repo_name = pull_request.target_repo.repo_name
202 repo_name = pull_request.target_repo.repo_name
203 pr_util.close()
203 pr_util.close()
204
204
205 response = self.app.post(
205 response = self.app.post(
206 route_path('pullrequest_update',
206 route_path('pullrequest_update',
207 repo_name=repo_name, pull_request_id=pull_request_id),
207 repo_name=repo_name, pull_request_id=pull_request_id),
208 params={
208 params={
209 'edit_pull_request': 'true',
209 'edit_pull_request': 'true',
210 'title': 'New title',
210 'title': 'New title',
211 'description': 'New description',
211 'description': 'New description',
212 'csrf_token': csrf_token}, status=200)
212 'csrf_token': csrf_token}, status=200)
213 assert_session_flash(
213 assert_session_flash(
214 response, u'Cannot update closed pull requests.',
214 response, u'Cannot update closed pull requests.',
215 category='error')
215 category='error')
216
216
217 def test_update_invalid_source_reference(self, pr_util, csrf_token):
217 def test_update_invalid_source_reference(self, pr_util, csrf_token):
218 from rhodecode.lib.vcs.backends.base import UpdateFailureReason
218 from rhodecode.lib.vcs.backends.base import UpdateFailureReason
219
219
220 pull_request = pr_util.create_pull_request()
220 pull_request = pr_util.create_pull_request()
221 pull_request.source_ref = 'branch:invalid-branch:invalid-commit-id'
221 pull_request.source_ref = 'branch:invalid-branch:invalid-commit-id'
222 Session().add(pull_request)
222 Session().add(pull_request)
223 Session().commit()
223 Session().commit()
224
224
225 pull_request_id = pull_request.pull_request_id
225 pull_request_id = pull_request.pull_request_id
226
226
227 response = self.app.post(
227 response = self.app.post(
228 route_path('pullrequest_update',
228 route_path('pullrequest_update',
229 repo_name=pull_request.target_repo.repo_name,
229 repo_name=pull_request.target_repo.repo_name,
230 pull_request_id=pull_request_id),
230 pull_request_id=pull_request_id),
231 params={'update_commits': 'true',
231 params={'update_commits': 'true',
232 'csrf_token': csrf_token})
232 'csrf_token': csrf_token})
233
233
234 expected_msg = str(PullRequestModel.UPDATE_STATUS_MESSAGES[
234 expected_msg = str(PullRequestModel.UPDATE_STATUS_MESSAGES[
235 UpdateFailureReason.MISSING_SOURCE_REF])
235 UpdateFailureReason.MISSING_SOURCE_REF])
236 assert_session_flash(response, expected_msg, category='error')
236 assert_session_flash(response, expected_msg, category='error')
237
237
238 def test_missing_target_reference(self, pr_util, csrf_token):
238 def test_missing_target_reference(self, pr_util, csrf_token):
239 from rhodecode.lib.vcs.backends.base import MergeFailureReason
239 from rhodecode.lib.vcs.backends.base import MergeFailureReason
240 pull_request = pr_util.create_pull_request(
240 pull_request = pr_util.create_pull_request(
241 approved=True, mergeable=True)
241 approved=True, mergeable=True)
242 pull_request.target_ref = 'branch:invalid-branch:invalid-commit-id'
242 pull_request.target_ref = 'branch:invalid-branch:invalid-commit-id'
243 Session().add(pull_request)
243 Session().add(pull_request)
244 Session().commit()
244 Session().commit()
245
245
246 pull_request_id = pull_request.pull_request_id
246 pull_request_id = pull_request.pull_request_id
247 pull_request_url = route_path(
247 pull_request_url = route_path(
248 'pullrequest_show',
248 'pullrequest_show',
249 repo_name=pull_request.target_repo.repo_name,
249 repo_name=pull_request.target_repo.repo_name,
250 pull_request_id=pull_request_id)
250 pull_request_id=pull_request_id)
251
251
252 response = self.app.get(pull_request_url)
252 response = self.app.get(pull_request_url)
253
253
254 assertr = AssertResponse(response)
254 assertr = AssertResponse(response)
255 expected_msg = PullRequestModel.MERGE_STATUS_MESSAGES[
255 expected_msg = PullRequestModel.MERGE_STATUS_MESSAGES[
256 MergeFailureReason.MISSING_TARGET_REF]
256 MergeFailureReason.MISSING_TARGET_REF]
257 assertr.element_contains(
257 assertr.element_contains(
258 'span[data-role="merge-message"]', str(expected_msg))
258 'span[data-role="merge-message"]', str(expected_msg))
259
259
260 def test_comment_and_close_pull_request_custom_message_approved(
260 def test_comment_and_close_pull_request_custom_message_approved(
261 self, pr_util, csrf_token, xhr_header):
261 self, pr_util, csrf_token, xhr_header):
262
262
263 pull_request = pr_util.create_pull_request(approved=True)
263 pull_request = pr_util.create_pull_request(approved=True)
264 pull_request_id = pull_request.pull_request_id
264 pull_request_id = pull_request.pull_request_id
265 author = pull_request.user_id
265 author = pull_request.user_id
266 repo = pull_request.target_repo.repo_id
266 repo = pull_request.target_repo.repo_id
267
267
268 self.app.post(
268 self.app.post(
269 route_path('pullrequest_comment_create',
269 route_path('pullrequest_comment_create',
270 repo_name=pull_request.target_repo.scm_instance().name,
270 repo_name=pull_request.target_repo.scm_instance().name,
271 pull_request_id=pull_request_id),
271 pull_request_id=pull_request_id),
272 params={
272 params={
273 'close_pull_request': '1',
273 'close_pull_request': '1',
274 'text': 'Closing a PR',
274 'text': 'Closing a PR',
275 'csrf_token': csrf_token},
275 'csrf_token': csrf_token},
276 extra_environ=xhr_header,)
276 extra_environ=xhr_header,)
277
277
278 journal = UserLog.query()\
278 journal = UserLog.query()\
279 .filter(UserLog.user_id == author)\
279 .filter(UserLog.user_id == author)\
280 .filter(UserLog.repository_id == repo) \
280 .filter(UserLog.repository_id == repo) \
281 .order_by('user_log_id') \
281 .order_by('user_log_id') \
282 .all()
282 .all()
283 assert journal[-1].action == 'repo.pull_request.close'
283 assert journal[-1].action == 'repo.pull_request.close'
284
284
285 pull_request = PullRequest.get(pull_request_id)
285 pull_request = PullRequest.get(pull_request_id)
286 assert pull_request.is_closed()
286 assert pull_request.is_closed()
287
287
288 status = ChangesetStatusModel().get_status(
288 status = ChangesetStatusModel().get_status(
289 pull_request.source_repo, pull_request=pull_request)
289 pull_request.source_repo, pull_request=pull_request)
290 assert status == ChangesetStatus.STATUS_APPROVED
290 assert status == ChangesetStatus.STATUS_APPROVED
291 comments = ChangesetComment().query() \
291 comments = ChangesetComment().query() \
292 .filter(ChangesetComment.pull_request == pull_request) \
292 .filter(ChangesetComment.pull_request == pull_request) \
293 .order_by(ChangesetComment.comment_id.asc())\
293 .order_by(ChangesetComment.comment_id.asc())\
294 .all()
294 .all()
295 assert comments[-1].text == 'Closing a PR'
295 assert comments[-1].text == 'Closing a PR'
296
296
297 def test_comment_force_close_pull_request_rejected(
297 def test_comment_force_close_pull_request_rejected(
298 self, pr_util, csrf_token, xhr_header):
298 self, pr_util, csrf_token, xhr_header):
299 pull_request = pr_util.create_pull_request()
299 pull_request = pr_util.create_pull_request()
300 pull_request_id = pull_request.pull_request_id
300 pull_request_id = pull_request.pull_request_id
301 PullRequestModel().update_reviewers(
301 PullRequestModel().update_reviewers(
302 pull_request_id, [(1, ['reason'], False, []), (2, ['reason2'], False, [])],
302 pull_request_id, [(1, ['reason'], False, []), (2, ['reason2'], False, [])],
303 pull_request.author)
303 pull_request.author)
304 author = pull_request.user_id
304 author = pull_request.user_id
305 repo = pull_request.target_repo.repo_id
305 repo = pull_request.target_repo.repo_id
306
306
307 self.app.post(
307 self.app.post(
308 route_path('pullrequest_comment_create',
308 route_path('pullrequest_comment_create',
309 repo_name=pull_request.target_repo.scm_instance().name,
309 repo_name=pull_request.target_repo.scm_instance().name,
310 pull_request_id=pull_request_id),
310 pull_request_id=pull_request_id),
311 params={
311 params={
312 'close_pull_request': '1',
312 'close_pull_request': '1',
313 'csrf_token': csrf_token},
313 'csrf_token': csrf_token},
314 extra_environ=xhr_header)
314 extra_environ=xhr_header)
315
315
316 pull_request = PullRequest.get(pull_request_id)
316 pull_request = PullRequest.get(pull_request_id)
317
317
318 journal = UserLog.query()\
318 journal = UserLog.query()\
319 .filter(UserLog.user_id == author, UserLog.repository_id == repo) \
319 .filter(UserLog.user_id == author, UserLog.repository_id == repo) \
320 .order_by('user_log_id') \
320 .order_by('user_log_id') \
321 .all()
321 .all()
322 assert journal[-1].action == 'repo.pull_request.close'
322 assert journal[-1].action == 'repo.pull_request.close'
323
323
324 # check only the latest status, not the review status
324 # check only the latest status, not the review status
325 status = ChangesetStatusModel().get_status(
325 status = ChangesetStatusModel().get_status(
326 pull_request.source_repo, pull_request=pull_request)
326 pull_request.source_repo, pull_request=pull_request)
327 assert status == ChangesetStatus.STATUS_REJECTED
327 assert status == ChangesetStatus.STATUS_REJECTED
328
328
329 def test_comment_and_close_pull_request(
329 def test_comment_and_close_pull_request(
330 self, pr_util, csrf_token, xhr_header):
330 self, pr_util, csrf_token, xhr_header):
331 pull_request = pr_util.create_pull_request()
331 pull_request = pr_util.create_pull_request()
332 pull_request_id = pull_request.pull_request_id
332 pull_request_id = pull_request.pull_request_id
333
333
334 response = self.app.post(
334 response = self.app.post(
335 route_path('pullrequest_comment_create',
335 route_path('pullrequest_comment_create',
336 repo_name=pull_request.target_repo.scm_instance().name,
336 repo_name=pull_request.target_repo.scm_instance().name,
337 pull_request_id=pull_request.pull_request_id),
337 pull_request_id=pull_request.pull_request_id),
338 params={
338 params={
339 'close_pull_request': 'true',
339 'close_pull_request': 'true',
340 'csrf_token': csrf_token},
340 'csrf_token': csrf_token},
341 extra_environ=xhr_header)
341 extra_environ=xhr_header)
342
342
343 assert response.json
343 assert response.json
344
344
345 pull_request = PullRequest.get(pull_request_id)
345 pull_request = PullRequest.get(pull_request_id)
346 assert pull_request.is_closed()
346 assert pull_request.is_closed()
347
347
348 # check only the latest status, not the review status
348 # check only the latest status, not the review status
349 status = ChangesetStatusModel().get_status(
349 status = ChangesetStatusModel().get_status(
350 pull_request.source_repo, pull_request=pull_request)
350 pull_request.source_repo, pull_request=pull_request)
351 assert status == ChangesetStatus.STATUS_REJECTED
351 assert status == ChangesetStatus.STATUS_REJECTED
352
352
353 def test_create_pull_request(self, backend, csrf_token):
353 def test_create_pull_request(self, backend, csrf_token):
354 commits = [
354 commits = [
355 {'message': 'ancestor'},
355 {'message': 'ancestor'},
356 {'message': 'change'},
356 {'message': 'change'},
357 {'message': 'change2'},
357 {'message': 'change2'},
358 ]
358 ]
359 commit_ids = backend.create_master_repo(commits)
359 commit_ids = backend.create_master_repo(commits)
360 target = backend.create_repo(heads=['ancestor'])
360 target = backend.create_repo(heads=['ancestor'])
361 source = backend.create_repo(heads=['change2'])
361 source = backend.create_repo(heads=['change2'])
362
362
363 response = self.app.post(
363 response = self.app.post(
364 route_path('pullrequest_create', repo_name=source.repo_name),
364 route_path('pullrequest_create', repo_name=source.repo_name),
365 [
365 [
366 ('source_repo', source.repo_name),
366 ('source_repo', source.repo_name),
367 ('source_ref', 'branch:default:' + commit_ids['change2']),
367 ('source_ref', 'branch:default:' + commit_ids['change2']),
368 ('target_repo', target.repo_name),
368 ('target_repo', target.repo_name),
369 ('target_ref', 'branch:default:' + commit_ids['ancestor']),
369 ('target_ref', 'branch:default:' + commit_ids['ancestor']),
370 ('common_ancestor', commit_ids['ancestor']),
370 ('common_ancestor', commit_ids['ancestor']),
371 ('pullrequest_title', 'Title'),
371 ('pullrequest_desc', 'Description'),
372 ('pullrequest_desc', 'Description'),
372 ('pullrequest_title', 'Title'),
373 ('description_renderer', 'markdown'),
373 ('__start__', 'review_members:sequence'),
374 ('__start__', 'review_members:sequence'),
374 ('__start__', 'reviewer:mapping'),
375 ('__start__', 'reviewer:mapping'),
375 ('user_id', '1'),
376 ('user_id', '1'),
376 ('__start__', 'reasons:sequence'),
377 ('__start__', 'reasons:sequence'),
377 ('reason', 'Some reason'),
378 ('reason', 'Some reason'),
378 ('__end__', 'reasons:sequence'),
379 ('__end__', 'reasons:sequence'),
379 ('__start__', 'rules:sequence'),
380 ('__start__', 'rules:sequence'),
380 ('__end__', 'rules:sequence'),
381 ('__end__', 'rules:sequence'),
381 ('mandatory', 'False'),
382 ('mandatory', 'False'),
382 ('__end__', 'reviewer:mapping'),
383 ('__end__', 'reviewer:mapping'),
383 ('__end__', 'review_members:sequence'),
384 ('__end__', 'review_members:sequence'),
384 ('__start__', 'revisions:sequence'),
385 ('__start__', 'revisions:sequence'),
385 ('revisions', commit_ids['change']),
386 ('revisions', commit_ids['change']),
386 ('revisions', commit_ids['change2']),
387 ('revisions', commit_ids['change2']),
387 ('__end__', 'revisions:sequence'),
388 ('__end__', 'revisions:sequence'),
388 ('user', ''),
389 ('user', ''),
389 ('csrf_token', csrf_token),
390 ('csrf_token', csrf_token),
390 ],
391 ],
391 status=302)
392 status=302)
392
393
393 location = response.headers['Location']
394 location = response.headers['Location']
394 pull_request_id = location.rsplit('/', 1)[1]
395 pull_request_id = location.rsplit('/', 1)[1]
395 assert pull_request_id != 'new'
396 assert pull_request_id != 'new'
396 pull_request = PullRequest.get(int(pull_request_id))
397 pull_request = PullRequest.get(int(pull_request_id))
397
398
398 # check that we have now both revisions
399 # check that we have now both revisions
399 assert pull_request.revisions == [commit_ids['change2'], commit_ids['change']]
400 assert pull_request.revisions == [commit_ids['change2'], commit_ids['change']]
400 assert pull_request.source_ref == 'branch:default:' + commit_ids['change2']
401 assert pull_request.source_ref == 'branch:default:' + commit_ids['change2']
401 expected_target_ref = 'branch:default:' + commit_ids['ancestor']
402 expected_target_ref = 'branch:default:' + commit_ids['ancestor']
402 assert pull_request.target_ref == expected_target_ref
403 assert pull_request.target_ref == expected_target_ref
403
404
404 def test_reviewer_notifications(self, backend, csrf_token):
405 def test_reviewer_notifications(self, backend, csrf_token):
405 # We have to use the app.post for this test so it will create the
406 # We have to use the app.post for this test so it will create the
406 # notifications properly with the new PR
407 # notifications properly with the new PR
407 commits = [
408 commits = [
408 {'message': 'ancestor',
409 {'message': 'ancestor',
409 'added': [FileNode('file_A', content='content_of_ancestor')]},
410 'added': [FileNode('file_A', content='content_of_ancestor')]},
410 {'message': 'change',
411 {'message': 'change',
411 'added': [FileNode('file_a', content='content_of_change')]},
412 'added': [FileNode('file_a', content='content_of_change')]},
412 {'message': 'change-child'},
413 {'message': 'change-child'},
413 {'message': 'ancestor-child', 'parents': ['ancestor'],
414 {'message': 'ancestor-child', 'parents': ['ancestor'],
414 'added': [
415 'added': [
415 FileNode('file_B', content='content_of_ancestor_child')]},
416 FileNode('file_B', content='content_of_ancestor_child')]},
416 {'message': 'ancestor-child-2'},
417 {'message': 'ancestor-child-2'},
417 ]
418 ]
418 commit_ids = backend.create_master_repo(commits)
419 commit_ids = backend.create_master_repo(commits)
419 target = backend.create_repo(heads=['ancestor-child'])
420 target = backend.create_repo(heads=['ancestor-child'])
420 source = backend.create_repo(heads=['change'])
421 source = backend.create_repo(heads=['change'])
421
422
422 response = self.app.post(
423 response = self.app.post(
423 route_path('pullrequest_create', repo_name=source.repo_name),
424 route_path('pullrequest_create', repo_name=source.repo_name),
424 [
425 [
425 ('source_repo', source.repo_name),
426 ('source_repo', source.repo_name),
426 ('source_ref', 'branch:default:' + commit_ids['change']),
427 ('source_ref', 'branch:default:' + commit_ids['change']),
427 ('target_repo', target.repo_name),
428 ('target_repo', target.repo_name),
428 ('target_ref', 'branch:default:' + commit_ids['ancestor-child']),
429 ('target_ref', 'branch:default:' + commit_ids['ancestor-child']),
429 ('common_ancestor', commit_ids['ancestor']),
430 ('common_ancestor', commit_ids['ancestor']),
431 ('pullrequest_title', 'Title'),
430 ('pullrequest_desc', 'Description'),
432 ('pullrequest_desc', 'Description'),
431 ('pullrequest_title', 'Title'),
433 ('description_renderer', 'markdown'),
432 ('__start__', 'review_members:sequence'),
434 ('__start__', 'review_members:sequence'),
433 ('__start__', 'reviewer:mapping'),
435 ('__start__', 'reviewer:mapping'),
434 ('user_id', '2'),
436 ('user_id', '2'),
435 ('__start__', 'reasons:sequence'),
437 ('__start__', 'reasons:sequence'),
436 ('reason', 'Some reason'),
438 ('reason', 'Some reason'),
437 ('__end__', 'reasons:sequence'),
439 ('__end__', 'reasons:sequence'),
438 ('__start__', 'rules:sequence'),
440 ('__start__', 'rules:sequence'),
439 ('__end__', 'rules:sequence'),
441 ('__end__', 'rules:sequence'),
440 ('mandatory', 'False'),
442 ('mandatory', 'False'),
441 ('__end__', 'reviewer:mapping'),
443 ('__end__', 'reviewer:mapping'),
442 ('__end__', 'review_members:sequence'),
444 ('__end__', 'review_members:sequence'),
443 ('__start__', 'revisions:sequence'),
445 ('__start__', 'revisions:sequence'),
444 ('revisions', commit_ids['change']),
446 ('revisions', commit_ids['change']),
445 ('__end__', 'revisions:sequence'),
447 ('__end__', 'revisions:sequence'),
446 ('user', ''),
448 ('user', ''),
447 ('csrf_token', csrf_token),
449 ('csrf_token', csrf_token),
448 ],
450 ],
449 status=302)
451 status=302)
450
452
451 location = response.headers['Location']
453 location = response.headers['Location']
452
454
453 pull_request_id = location.rsplit('/', 1)[1]
455 pull_request_id = location.rsplit('/', 1)[1]
454 assert pull_request_id != 'new'
456 assert pull_request_id != 'new'
455 pull_request = PullRequest.get(int(pull_request_id))
457 pull_request = PullRequest.get(int(pull_request_id))
456
458
457 # Check that a notification was made
459 # Check that a notification was made
458 notifications = Notification.query()\
460 notifications = Notification.query()\
459 .filter(Notification.created_by == pull_request.author.user_id,
461 .filter(Notification.created_by == pull_request.author.user_id,
460 Notification.type_ == Notification.TYPE_PULL_REQUEST,
462 Notification.type_ == Notification.TYPE_PULL_REQUEST,
461 Notification.subject.contains(
463 Notification.subject.contains(
462 "wants you to review pull request #%s" % pull_request_id))
464 "wants you to review pull request #%s" % pull_request_id))
463 assert len(notifications.all()) == 1
465 assert len(notifications.all()) == 1
464
466
465 # Change reviewers and check that a notification was made
467 # Change reviewers and check that a notification was made
466 PullRequestModel().update_reviewers(
468 PullRequestModel().update_reviewers(
467 pull_request.pull_request_id, [(1, [], False, [])],
469 pull_request.pull_request_id, [(1, [], False, [])],
468 pull_request.author)
470 pull_request.author)
469 assert len(notifications.all()) == 2
471 assert len(notifications.all()) == 2
470
472
471 def test_create_pull_request_stores_ancestor_commit_id(self, backend,
473 def test_create_pull_request_stores_ancestor_commit_id(self, backend,
472 csrf_token):
474 csrf_token):
473 commits = [
475 commits = [
474 {'message': 'ancestor',
476 {'message': 'ancestor',
475 'added': [FileNode('file_A', content='content_of_ancestor')]},
477 'added': [FileNode('file_A', content='content_of_ancestor')]},
476 {'message': 'change',
478 {'message': 'change',
477 'added': [FileNode('file_a', content='content_of_change')]},
479 'added': [FileNode('file_a', content='content_of_change')]},
478 {'message': 'change-child'},
480 {'message': 'change-child'},
479 {'message': 'ancestor-child', 'parents': ['ancestor'],
481 {'message': 'ancestor-child', 'parents': ['ancestor'],
480 'added': [
482 'added': [
481 FileNode('file_B', content='content_of_ancestor_child')]},
483 FileNode('file_B', content='content_of_ancestor_child')]},
482 {'message': 'ancestor-child-2'},
484 {'message': 'ancestor-child-2'},
483 ]
485 ]
484 commit_ids = backend.create_master_repo(commits)
486 commit_ids = backend.create_master_repo(commits)
485 target = backend.create_repo(heads=['ancestor-child'])
487 target = backend.create_repo(heads=['ancestor-child'])
486 source = backend.create_repo(heads=['change'])
488 source = backend.create_repo(heads=['change'])
487
489
488 response = self.app.post(
490 response = self.app.post(
489 route_path('pullrequest_create', repo_name=source.repo_name),
491 route_path('pullrequest_create', repo_name=source.repo_name),
490 [
492 [
491 ('source_repo', source.repo_name),
493 ('source_repo', source.repo_name),
492 ('source_ref', 'branch:default:' + commit_ids['change']),
494 ('source_ref', 'branch:default:' + commit_ids['change']),
493 ('target_repo', target.repo_name),
495 ('target_repo', target.repo_name),
494 ('target_ref', 'branch:default:' + commit_ids['ancestor-child']),
496 ('target_ref', 'branch:default:' + commit_ids['ancestor-child']),
495 ('common_ancestor', commit_ids['ancestor']),
497 ('common_ancestor', commit_ids['ancestor']),
498 ('pullrequest_title', 'Title'),
496 ('pullrequest_desc', 'Description'),
499 ('pullrequest_desc', 'Description'),
497 ('pullrequest_title', 'Title'),
500 ('description_renderer', 'markdown'),
498 ('__start__', 'review_members:sequence'),
501 ('__start__', 'review_members:sequence'),
499 ('__start__', 'reviewer:mapping'),
502 ('__start__', 'reviewer:mapping'),
500 ('user_id', '1'),
503 ('user_id', '1'),
501 ('__start__', 'reasons:sequence'),
504 ('__start__', 'reasons:sequence'),
502 ('reason', 'Some reason'),
505 ('reason', 'Some reason'),
503 ('__end__', 'reasons:sequence'),
506 ('__end__', 'reasons:sequence'),
504 ('__start__', 'rules:sequence'),
507 ('__start__', 'rules:sequence'),
505 ('__end__', 'rules:sequence'),
508 ('__end__', 'rules:sequence'),
506 ('mandatory', 'False'),
509 ('mandatory', 'False'),
507 ('__end__', 'reviewer:mapping'),
510 ('__end__', 'reviewer:mapping'),
508 ('__end__', 'review_members:sequence'),
511 ('__end__', 'review_members:sequence'),
509 ('__start__', 'revisions:sequence'),
512 ('__start__', 'revisions:sequence'),
510 ('revisions', commit_ids['change']),
513 ('revisions', commit_ids['change']),
511 ('__end__', 'revisions:sequence'),
514 ('__end__', 'revisions:sequence'),
512 ('user', ''),
515 ('user', ''),
513 ('csrf_token', csrf_token),
516 ('csrf_token', csrf_token),
514 ],
517 ],
515 status=302)
518 status=302)
516
519
517 location = response.headers['Location']
520 location = response.headers['Location']
518
521
519 pull_request_id = location.rsplit('/', 1)[1]
522 pull_request_id = location.rsplit('/', 1)[1]
520 assert pull_request_id != 'new'
523 assert pull_request_id != 'new'
521 pull_request = PullRequest.get(int(pull_request_id))
524 pull_request = PullRequest.get(int(pull_request_id))
522
525
523 # target_ref has to point to the ancestor's commit_id in order to
526 # target_ref has to point to the ancestor's commit_id in order to
524 # show the correct diff
527 # show the correct diff
525 expected_target_ref = 'branch:default:' + commit_ids['ancestor']
528 expected_target_ref = 'branch:default:' + commit_ids['ancestor']
526 assert pull_request.target_ref == expected_target_ref
529 assert pull_request.target_ref == expected_target_ref
527
530
528 # Check generated diff contents
531 # Check generated diff contents
529 response = response.follow()
532 response = response.follow()
530 assert 'content_of_ancestor' not in response.body
533 assert 'content_of_ancestor' not in response.body
531 assert 'content_of_ancestor-child' not in response.body
534 assert 'content_of_ancestor-child' not in response.body
532 assert 'content_of_change' in response.body
535 assert 'content_of_change' in response.body
533
536
534 def test_merge_pull_request_enabled(self, pr_util, csrf_token):
537 def test_merge_pull_request_enabled(self, pr_util, csrf_token):
535 # Clear any previous calls to rcextensions
538 # Clear any previous calls to rcextensions
536 rhodecode.EXTENSIONS.calls.clear()
539 rhodecode.EXTENSIONS.calls.clear()
537
540
538 pull_request = pr_util.create_pull_request(
541 pull_request = pr_util.create_pull_request(
539 approved=True, mergeable=True)
542 approved=True, mergeable=True)
540 pull_request_id = pull_request.pull_request_id
543 pull_request_id = pull_request.pull_request_id
541 repo_name = pull_request.target_repo.scm_instance().name,
544 repo_name = pull_request.target_repo.scm_instance().name,
542
545
543 response = self.app.post(
546 response = self.app.post(
544 route_path('pullrequest_merge',
547 route_path('pullrequest_merge',
545 repo_name=str(repo_name[0]),
548 repo_name=str(repo_name[0]),
546 pull_request_id=pull_request_id),
549 pull_request_id=pull_request_id),
547 params={'csrf_token': csrf_token}).follow()
550 params={'csrf_token': csrf_token}).follow()
548
551
549 pull_request = PullRequest.get(pull_request_id)
552 pull_request = PullRequest.get(pull_request_id)
550
553
551 assert response.status_int == 200
554 assert response.status_int == 200
552 assert pull_request.is_closed()
555 assert pull_request.is_closed()
553 assert_pull_request_status(
556 assert_pull_request_status(
554 pull_request, ChangesetStatus.STATUS_APPROVED)
557 pull_request, ChangesetStatus.STATUS_APPROVED)
555
558
556 # Check the relevant log entries were added
559 # Check the relevant log entries were added
557 user_logs = UserLog.query().order_by('-user_log_id').limit(3)
560 user_logs = UserLog.query().order_by('-user_log_id').limit(3)
558 actions = [log.action for log in user_logs]
561 actions = [log.action for log in user_logs]
559 pr_commit_ids = PullRequestModel()._get_commit_ids(pull_request)
562 pr_commit_ids = PullRequestModel()._get_commit_ids(pull_request)
560 expected_actions = [
563 expected_actions = [
561 u'repo.pull_request.close',
564 u'repo.pull_request.close',
562 u'repo.pull_request.merge',
565 u'repo.pull_request.merge',
563 u'repo.pull_request.comment.create'
566 u'repo.pull_request.comment.create'
564 ]
567 ]
565 assert actions == expected_actions
568 assert actions == expected_actions
566
569
567 user_logs = UserLog.query().order_by('-user_log_id').limit(4)
570 user_logs = UserLog.query().order_by('-user_log_id').limit(4)
568 actions = [log for log in user_logs]
571 actions = [log for log in user_logs]
569 assert actions[-1].action == 'user.push'
572 assert actions[-1].action == 'user.push'
570 assert actions[-1].action_data['commit_ids'] == pr_commit_ids
573 assert actions[-1].action_data['commit_ids'] == pr_commit_ids
571
574
572 # Check post_push rcextension was really executed
575 # Check post_push rcextension was really executed
573 push_calls = rhodecode.EXTENSIONS.calls['post_push']
576 push_calls = rhodecode.EXTENSIONS.calls['post_push']
574 assert len(push_calls) == 1
577 assert len(push_calls) == 1
575 unused_last_call_args, last_call_kwargs = push_calls[0]
578 unused_last_call_args, last_call_kwargs = push_calls[0]
576 assert last_call_kwargs['action'] == 'push'
579 assert last_call_kwargs['action'] == 'push'
577 assert last_call_kwargs['pushed_revs'] == pr_commit_ids
580 assert last_call_kwargs['pushed_revs'] == pr_commit_ids
578
581
579 def test_merge_pull_request_disabled(self, pr_util, csrf_token):
582 def test_merge_pull_request_disabled(self, pr_util, csrf_token):
580 pull_request = pr_util.create_pull_request(mergeable=False)
583 pull_request = pr_util.create_pull_request(mergeable=False)
581 pull_request_id = pull_request.pull_request_id
584 pull_request_id = pull_request.pull_request_id
582 pull_request = PullRequest.get(pull_request_id)
585 pull_request = PullRequest.get(pull_request_id)
583
586
584 response = self.app.post(
587 response = self.app.post(
585 route_path('pullrequest_merge',
588 route_path('pullrequest_merge',
586 repo_name=pull_request.target_repo.scm_instance().name,
589 repo_name=pull_request.target_repo.scm_instance().name,
587 pull_request_id=pull_request.pull_request_id),
590 pull_request_id=pull_request.pull_request_id),
588 params={'csrf_token': csrf_token}).follow()
591 params={'csrf_token': csrf_token}).follow()
589
592
590 assert response.status_int == 200
593 assert response.status_int == 200
591 response.mustcontain(
594 response.mustcontain(
592 'Merge is not currently possible because of below failed checks.')
595 'Merge is not currently possible because of below failed checks.')
593 response.mustcontain('Server-side pull request merging is disabled.')
596 response.mustcontain('Server-side pull request merging is disabled.')
594
597
595 @pytest.mark.skip_backends('svn')
598 @pytest.mark.skip_backends('svn')
596 def test_merge_pull_request_not_approved(self, pr_util, csrf_token):
599 def test_merge_pull_request_not_approved(self, pr_util, csrf_token):
597 pull_request = pr_util.create_pull_request(mergeable=True)
600 pull_request = pr_util.create_pull_request(mergeable=True)
598 pull_request_id = pull_request.pull_request_id
601 pull_request_id = pull_request.pull_request_id
599 repo_name = pull_request.target_repo.scm_instance().name
602 repo_name = pull_request.target_repo.scm_instance().name
600
603
601 response = self.app.post(
604 response = self.app.post(
602 route_path('pullrequest_merge',
605 route_path('pullrequest_merge',
603 repo_name=repo_name,
606 repo_name=repo_name,
604 pull_request_id=pull_request_id),
607 pull_request_id=pull_request_id),
605 params={'csrf_token': csrf_token}).follow()
608 params={'csrf_token': csrf_token}).follow()
606
609
607 assert response.status_int == 200
610 assert response.status_int == 200
608
611
609 response.mustcontain(
612 response.mustcontain(
610 'Merge is not currently possible because of below failed checks.')
613 'Merge is not currently possible because of below failed checks.')
611 response.mustcontain('Pull request reviewer approval is pending.')
614 response.mustcontain('Pull request reviewer approval is pending.')
612
615
613 def test_merge_pull_request_renders_failure_reason(
616 def test_merge_pull_request_renders_failure_reason(
614 self, user_regular, csrf_token, pr_util):
617 self, user_regular, csrf_token, pr_util):
615 pull_request = pr_util.create_pull_request(mergeable=True, approved=True)
618 pull_request = pr_util.create_pull_request(mergeable=True, approved=True)
616 pull_request_id = pull_request.pull_request_id
619 pull_request_id = pull_request.pull_request_id
617 repo_name = pull_request.target_repo.scm_instance().name
620 repo_name = pull_request.target_repo.scm_instance().name
618
621
619 model_patcher = mock.patch.multiple(
622 model_patcher = mock.patch.multiple(
620 PullRequestModel,
623 PullRequestModel,
621 merge_repo=mock.Mock(return_value=MergeResponse(
624 merge_repo=mock.Mock(return_value=MergeResponse(
622 True, False, 'STUB_COMMIT_ID', MergeFailureReason.PUSH_FAILED)),
625 True, False, 'STUB_COMMIT_ID', MergeFailureReason.PUSH_FAILED)),
623 merge_status=mock.Mock(return_value=(True, 'WRONG_MESSAGE')))
626 merge_status=mock.Mock(return_value=(True, 'WRONG_MESSAGE')))
624
627
625 with model_patcher:
628 with model_patcher:
626 response = self.app.post(
629 response = self.app.post(
627 route_path('pullrequest_merge',
630 route_path('pullrequest_merge',
628 repo_name=repo_name,
631 repo_name=repo_name,
629 pull_request_id=pull_request_id),
632 pull_request_id=pull_request_id),
630 params={'csrf_token': csrf_token}, status=302)
633 params={'csrf_token': csrf_token}, status=302)
631
634
632 assert_session_flash(response, PullRequestModel.MERGE_STATUS_MESSAGES[
635 assert_session_flash(response, PullRequestModel.MERGE_STATUS_MESSAGES[
633 MergeFailureReason.PUSH_FAILED])
636 MergeFailureReason.PUSH_FAILED])
634
637
635 def test_update_source_revision(self, backend, csrf_token):
638 def test_update_source_revision(self, backend, csrf_token):
636 commits = [
639 commits = [
637 {'message': 'ancestor'},
640 {'message': 'ancestor'},
638 {'message': 'change'},
641 {'message': 'change'},
639 {'message': 'change-2'},
642 {'message': 'change-2'},
640 ]
643 ]
641 commit_ids = backend.create_master_repo(commits)
644 commit_ids = backend.create_master_repo(commits)
642 target = backend.create_repo(heads=['ancestor'])
645 target = backend.create_repo(heads=['ancestor'])
643 source = backend.create_repo(heads=['change'])
646 source = backend.create_repo(heads=['change'])
644
647
645 # create pr from a in source to A in target
648 # create pr from a in source to A in target
646 pull_request = PullRequest()
649 pull_request = PullRequest()
647 pull_request.source_repo = source
650 pull_request.source_repo = source
648 # TODO: johbo: Make sure that we write the source ref this way!
651 # TODO: johbo: Make sure that we write the source ref this way!
649 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
652 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
650 branch=backend.default_branch_name, commit_id=commit_ids['change'])
653 branch=backend.default_branch_name, commit_id=commit_ids['change'])
651 pull_request.target_repo = target
654 pull_request.target_repo = target
652
655
653 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
656 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
654 branch=backend.default_branch_name,
657 branch=backend.default_branch_name,
655 commit_id=commit_ids['ancestor'])
658 commit_id=commit_ids['ancestor'])
656 pull_request.revisions = [commit_ids['change']]
659 pull_request.revisions = [commit_ids['change']]
657 pull_request.title = u"Test"
660 pull_request.title = u"Test"
658 pull_request.description = u"Description"
661 pull_request.description = u"Description"
659 pull_request.author = UserModel().get_by_username(
662 pull_request.author = UserModel().get_by_username(
660 TEST_USER_ADMIN_LOGIN)
663 TEST_USER_ADMIN_LOGIN)
661 Session().add(pull_request)
664 Session().add(pull_request)
662 Session().commit()
665 Session().commit()
663 pull_request_id = pull_request.pull_request_id
666 pull_request_id = pull_request.pull_request_id
664
667
665 # source has ancestor - change - change-2
668 # source has ancestor - change - change-2
666 backend.pull_heads(source, heads=['change-2'])
669 backend.pull_heads(source, heads=['change-2'])
667
670
668 # update PR
671 # update PR
669 self.app.post(
672 self.app.post(
670 route_path('pullrequest_update',
673 route_path('pullrequest_update',
671 repo_name=target.repo_name,
674 repo_name=target.repo_name,
672 pull_request_id=pull_request_id),
675 pull_request_id=pull_request_id),
673 params={'update_commits': 'true',
676 params={'update_commits': 'true',
674 'csrf_token': csrf_token})
677 'csrf_token': csrf_token})
675
678
676 # check that we have now both revisions
679 # check that we have now both revisions
677 pull_request = PullRequest.get(pull_request_id)
680 pull_request = PullRequest.get(pull_request_id)
678 assert pull_request.revisions == [
681 assert pull_request.revisions == [
679 commit_ids['change-2'], commit_ids['change']]
682 commit_ids['change-2'], commit_ids['change']]
680
683
681 # TODO: johbo: this should be a test on its own
684 # TODO: johbo: this should be a test on its own
682 response = self.app.get(route_path(
685 response = self.app.get(route_path(
683 'pullrequest_new',
686 'pullrequest_new',
684 repo_name=target.repo_name))
687 repo_name=target.repo_name))
685 assert response.status_int == 200
688 assert response.status_int == 200
686 assert 'Pull request updated to' in response.body
689 assert 'Pull request updated to' in response.body
687 assert 'with 1 added, 0 removed commits.' in response.body
690 assert 'with 1 added, 0 removed commits.' in response.body
688
691
689 def test_update_target_revision(self, backend, csrf_token):
692 def test_update_target_revision(self, backend, csrf_token):
690 commits = [
693 commits = [
691 {'message': 'ancestor'},
694 {'message': 'ancestor'},
692 {'message': 'change'},
695 {'message': 'change'},
693 {'message': 'ancestor-new', 'parents': ['ancestor']},
696 {'message': 'ancestor-new', 'parents': ['ancestor']},
694 {'message': 'change-rebased'},
697 {'message': 'change-rebased'},
695 ]
698 ]
696 commit_ids = backend.create_master_repo(commits)
699 commit_ids = backend.create_master_repo(commits)
697 target = backend.create_repo(heads=['ancestor'])
700 target = backend.create_repo(heads=['ancestor'])
698 source = backend.create_repo(heads=['change'])
701 source = backend.create_repo(heads=['change'])
699
702
700 # create pr from a in source to A in target
703 # create pr from a in source to A in target
701 pull_request = PullRequest()
704 pull_request = PullRequest()
702 pull_request.source_repo = source
705 pull_request.source_repo = source
703 # TODO: johbo: Make sure that we write the source ref this way!
706 # TODO: johbo: Make sure that we write the source ref this way!
704 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
707 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
705 branch=backend.default_branch_name, commit_id=commit_ids['change'])
708 branch=backend.default_branch_name, commit_id=commit_ids['change'])
706 pull_request.target_repo = target
709 pull_request.target_repo = target
707 # TODO: johbo: Target ref should be branch based, since tip can jump
710 # TODO: johbo: Target ref should be branch based, since tip can jump
708 # from branch to branch
711 # from branch to branch
709 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
712 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
710 branch=backend.default_branch_name,
713 branch=backend.default_branch_name,
711 commit_id=commit_ids['ancestor'])
714 commit_id=commit_ids['ancestor'])
712 pull_request.revisions = [commit_ids['change']]
715 pull_request.revisions = [commit_ids['change']]
713 pull_request.title = u"Test"
716 pull_request.title = u"Test"
714 pull_request.description = u"Description"
717 pull_request.description = u"Description"
715 pull_request.author = UserModel().get_by_username(
718 pull_request.author = UserModel().get_by_username(
716 TEST_USER_ADMIN_LOGIN)
719 TEST_USER_ADMIN_LOGIN)
717 Session().add(pull_request)
720 Session().add(pull_request)
718 Session().commit()
721 Session().commit()
719 pull_request_id = pull_request.pull_request_id
722 pull_request_id = pull_request.pull_request_id
720
723
721 # target has ancestor - ancestor-new
724 # target has ancestor - ancestor-new
722 # source has ancestor - ancestor-new - change-rebased
725 # source has ancestor - ancestor-new - change-rebased
723 backend.pull_heads(target, heads=['ancestor-new'])
726 backend.pull_heads(target, heads=['ancestor-new'])
724 backend.pull_heads(source, heads=['change-rebased'])
727 backend.pull_heads(source, heads=['change-rebased'])
725
728
726 # update PR
729 # update PR
727 self.app.post(
730 self.app.post(
728 route_path('pullrequest_update',
731 route_path('pullrequest_update',
729 repo_name=target.repo_name,
732 repo_name=target.repo_name,
730 pull_request_id=pull_request_id),
733 pull_request_id=pull_request_id),
731 params={'update_commits': 'true',
734 params={'update_commits': 'true',
732 'csrf_token': csrf_token},
735 'csrf_token': csrf_token},
733 status=200)
736 status=200)
734
737
735 # check that we have now both revisions
738 # check that we have now both revisions
736 pull_request = PullRequest.get(pull_request_id)
739 pull_request = PullRequest.get(pull_request_id)
737 assert pull_request.revisions == [commit_ids['change-rebased']]
740 assert pull_request.revisions == [commit_ids['change-rebased']]
738 assert pull_request.target_ref == 'branch:{branch}:{commit_id}'.format(
741 assert pull_request.target_ref == 'branch:{branch}:{commit_id}'.format(
739 branch=backend.default_branch_name,
742 branch=backend.default_branch_name,
740 commit_id=commit_ids['ancestor-new'])
743 commit_id=commit_ids['ancestor-new'])
741
744
742 # TODO: johbo: This should be a test on its own
745 # TODO: johbo: This should be a test on its own
743 response = self.app.get(route_path(
746 response = self.app.get(route_path(
744 'pullrequest_new',
747 'pullrequest_new',
745 repo_name=target.repo_name))
748 repo_name=target.repo_name))
746 assert response.status_int == 200
749 assert response.status_int == 200
747 assert 'Pull request updated to' in response.body
750 assert 'Pull request updated to' in response.body
748 assert 'with 1 added, 1 removed commits.' in response.body
751 assert 'with 1 added, 1 removed commits.' in response.body
749
752
750 def test_update_target_revision_with_removal_of_1_commit_git(self, backend_git, csrf_token):
753 def test_update_target_revision_with_removal_of_1_commit_git(self, backend_git, csrf_token):
751 backend = backend_git
754 backend = backend_git
752 commits = [
755 commits = [
753 {'message': 'master-commit-1'},
756 {'message': 'master-commit-1'},
754 {'message': 'master-commit-2-change-1'},
757 {'message': 'master-commit-2-change-1'},
755 {'message': 'master-commit-3-change-2'},
758 {'message': 'master-commit-3-change-2'},
756
759
757 {'message': 'feat-commit-1', 'parents': ['master-commit-1']},
760 {'message': 'feat-commit-1', 'parents': ['master-commit-1']},
758 {'message': 'feat-commit-2'},
761 {'message': 'feat-commit-2'},
759 ]
762 ]
760 commit_ids = backend.create_master_repo(commits)
763 commit_ids = backend.create_master_repo(commits)
761 target = backend.create_repo(heads=['master-commit-3-change-2'])
764 target = backend.create_repo(heads=['master-commit-3-change-2'])
762 source = backend.create_repo(heads=['feat-commit-2'])
765 source = backend.create_repo(heads=['feat-commit-2'])
763
766
764 # create pr from a in source to A in target
767 # create pr from a in source to A in target
765 pull_request = PullRequest()
768 pull_request = PullRequest()
766 pull_request.source_repo = source
769 pull_request.source_repo = source
767 # TODO: johbo: Make sure that we write the source ref this way!
770 # TODO: johbo: Make sure that we write the source ref this way!
768 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
771 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
769 branch=backend.default_branch_name,
772 branch=backend.default_branch_name,
770 commit_id=commit_ids['master-commit-3-change-2'])
773 commit_id=commit_ids['master-commit-3-change-2'])
771
774
772 pull_request.target_repo = target
775 pull_request.target_repo = target
773 # TODO: johbo: Target ref should be branch based, since tip can jump
776 # TODO: johbo: Target ref should be branch based, since tip can jump
774 # from branch to branch
777 # from branch to branch
775 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
778 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
776 branch=backend.default_branch_name,
779 branch=backend.default_branch_name,
777 commit_id=commit_ids['feat-commit-2'])
780 commit_id=commit_ids['feat-commit-2'])
778
781
779 pull_request.revisions = [
782 pull_request.revisions = [
780 commit_ids['feat-commit-1'],
783 commit_ids['feat-commit-1'],
781 commit_ids['feat-commit-2']
784 commit_ids['feat-commit-2']
782 ]
785 ]
783 pull_request.title = u"Test"
786 pull_request.title = u"Test"
784 pull_request.description = u"Description"
787 pull_request.description = u"Description"
785 pull_request.author = UserModel().get_by_username(
788 pull_request.author = UserModel().get_by_username(
786 TEST_USER_ADMIN_LOGIN)
789 TEST_USER_ADMIN_LOGIN)
787 Session().add(pull_request)
790 Session().add(pull_request)
788 Session().commit()
791 Session().commit()
789 pull_request_id = pull_request.pull_request_id
792 pull_request_id = pull_request.pull_request_id
790
793
791 # PR is created, now we simulate a force-push into target,
794 # PR is created, now we simulate a force-push into target,
792 # that drops a 2 last commits
795 # that drops a 2 last commits
793 vcsrepo = target.scm_instance()
796 vcsrepo = target.scm_instance()
794 vcsrepo.config.clear_section('hooks')
797 vcsrepo.config.clear_section('hooks')
795 vcsrepo.run_git_command(['reset', '--soft', 'HEAD~2'])
798 vcsrepo.run_git_command(['reset', '--soft', 'HEAD~2'])
796
799
797 # update PR
800 # update PR
798 self.app.post(
801 self.app.post(
799 route_path('pullrequest_update',
802 route_path('pullrequest_update',
800 repo_name=target.repo_name,
803 repo_name=target.repo_name,
801 pull_request_id=pull_request_id),
804 pull_request_id=pull_request_id),
802 params={'update_commits': 'true',
805 params={'update_commits': 'true',
803 'csrf_token': csrf_token},
806 'csrf_token': csrf_token},
804 status=200)
807 status=200)
805
808
806 response = self.app.get(route_path(
809 response = self.app.get(route_path(
807 'pullrequest_new',
810 'pullrequest_new',
808 repo_name=target.repo_name))
811 repo_name=target.repo_name))
809 assert response.status_int == 200
812 assert response.status_int == 200
810 response.mustcontain('Pull request updated to')
813 response.mustcontain('Pull request updated to')
811 response.mustcontain('with 0 added, 0 removed commits.')
814 response.mustcontain('with 0 added, 0 removed commits.')
812
815
813 def test_update_of_ancestor_reference(self, backend, csrf_token):
816 def test_update_of_ancestor_reference(self, backend, csrf_token):
814 commits = [
817 commits = [
815 {'message': 'ancestor'},
818 {'message': 'ancestor'},
816 {'message': 'change'},
819 {'message': 'change'},
817 {'message': 'change-2'},
820 {'message': 'change-2'},
818 {'message': 'ancestor-new', 'parents': ['ancestor']},
821 {'message': 'ancestor-new', 'parents': ['ancestor']},
819 {'message': 'change-rebased'},
822 {'message': 'change-rebased'},
820 ]
823 ]
821 commit_ids = backend.create_master_repo(commits)
824 commit_ids = backend.create_master_repo(commits)
822 target = backend.create_repo(heads=['ancestor'])
825 target = backend.create_repo(heads=['ancestor'])
823 source = backend.create_repo(heads=['change'])
826 source = backend.create_repo(heads=['change'])
824
827
825 # create pr from a in source to A in target
828 # create pr from a in source to A in target
826 pull_request = PullRequest()
829 pull_request = PullRequest()
827 pull_request.source_repo = source
830 pull_request.source_repo = source
828 # TODO: johbo: Make sure that we write the source ref this way!
831 # TODO: johbo: Make sure that we write the source ref this way!
829 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
832 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
830 branch=backend.default_branch_name,
833 branch=backend.default_branch_name,
831 commit_id=commit_ids['change'])
834 commit_id=commit_ids['change'])
832 pull_request.target_repo = target
835 pull_request.target_repo = target
833 # TODO: johbo: Target ref should be branch based, since tip can jump
836 # TODO: johbo: Target ref should be branch based, since tip can jump
834 # from branch to branch
837 # from branch to branch
835 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
838 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
836 branch=backend.default_branch_name,
839 branch=backend.default_branch_name,
837 commit_id=commit_ids['ancestor'])
840 commit_id=commit_ids['ancestor'])
838 pull_request.revisions = [commit_ids['change']]
841 pull_request.revisions = [commit_ids['change']]
839 pull_request.title = u"Test"
842 pull_request.title = u"Test"
840 pull_request.description = u"Description"
843 pull_request.description = u"Description"
841 pull_request.author = UserModel().get_by_username(
844 pull_request.author = UserModel().get_by_username(
842 TEST_USER_ADMIN_LOGIN)
845 TEST_USER_ADMIN_LOGIN)
843 Session().add(pull_request)
846 Session().add(pull_request)
844 Session().commit()
847 Session().commit()
845 pull_request_id = pull_request.pull_request_id
848 pull_request_id = pull_request.pull_request_id
846
849
847 # target has ancestor - ancestor-new
850 # target has ancestor - ancestor-new
848 # source has ancestor - ancestor-new - change-rebased
851 # source has ancestor - ancestor-new - change-rebased
849 backend.pull_heads(target, heads=['ancestor-new'])
852 backend.pull_heads(target, heads=['ancestor-new'])
850 backend.pull_heads(source, heads=['change-rebased'])
853 backend.pull_heads(source, heads=['change-rebased'])
851
854
852 # update PR
855 # update PR
853 self.app.post(
856 self.app.post(
854 route_path('pullrequest_update',
857 route_path('pullrequest_update',
855 repo_name=target.repo_name,
858 repo_name=target.repo_name,
856 pull_request_id=pull_request_id),
859 pull_request_id=pull_request_id),
857 params={'update_commits': 'true',
860 params={'update_commits': 'true',
858 'csrf_token': csrf_token},
861 'csrf_token': csrf_token},
859 status=200)
862 status=200)
860
863
861 # Expect the target reference to be updated correctly
864 # Expect the target reference to be updated correctly
862 pull_request = PullRequest.get(pull_request_id)
865 pull_request = PullRequest.get(pull_request_id)
863 assert pull_request.revisions == [commit_ids['change-rebased']]
866 assert pull_request.revisions == [commit_ids['change-rebased']]
864 expected_target_ref = 'branch:{branch}:{commit_id}'.format(
867 expected_target_ref = 'branch:{branch}:{commit_id}'.format(
865 branch=backend.default_branch_name,
868 branch=backend.default_branch_name,
866 commit_id=commit_ids['ancestor-new'])
869 commit_id=commit_ids['ancestor-new'])
867 assert pull_request.target_ref == expected_target_ref
870 assert pull_request.target_ref == expected_target_ref
868
871
869 def test_remove_pull_request_branch(self, backend_git, csrf_token):
872 def test_remove_pull_request_branch(self, backend_git, csrf_token):
870 branch_name = 'development'
873 branch_name = 'development'
871 commits = [
874 commits = [
872 {'message': 'initial-commit'},
875 {'message': 'initial-commit'},
873 {'message': 'old-feature'},
876 {'message': 'old-feature'},
874 {'message': 'new-feature', 'branch': branch_name},
877 {'message': 'new-feature', 'branch': branch_name},
875 ]
878 ]
876 repo = backend_git.create_repo(commits)
879 repo = backend_git.create_repo(commits)
877 commit_ids = backend_git.commit_ids
880 commit_ids = backend_git.commit_ids
878
881
879 pull_request = PullRequest()
882 pull_request = PullRequest()
880 pull_request.source_repo = repo
883 pull_request.source_repo = repo
881 pull_request.target_repo = repo
884 pull_request.target_repo = repo
882 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
885 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
883 branch=branch_name, commit_id=commit_ids['new-feature'])
886 branch=branch_name, commit_id=commit_ids['new-feature'])
884 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
887 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
885 branch=backend_git.default_branch_name,
888 branch=backend_git.default_branch_name,
886 commit_id=commit_ids['old-feature'])
889 commit_id=commit_ids['old-feature'])
887 pull_request.revisions = [commit_ids['new-feature']]
890 pull_request.revisions = [commit_ids['new-feature']]
888 pull_request.title = u"Test"
891 pull_request.title = u"Test"
889 pull_request.description = u"Description"
892 pull_request.description = u"Description"
890 pull_request.author = UserModel().get_by_username(
893 pull_request.author = UserModel().get_by_username(
891 TEST_USER_ADMIN_LOGIN)
894 TEST_USER_ADMIN_LOGIN)
892 Session().add(pull_request)
895 Session().add(pull_request)
893 Session().commit()
896 Session().commit()
894
897
895 vcs = repo.scm_instance()
898 vcs = repo.scm_instance()
896 vcs.remove_ref('refs/heads/{}'.format(branch_name))
899 vcs.remove_ref('refs/heads/{}'.format(branch_name))
897
900
898 response = self.app.get(route_path(
901 response = self.app.get(route_path(
899 'pullrequest_show',
902 'pullrequest_show',
900 repo_name=repo.repo_name,
903 repo_name=repo.repo_name,
901 pull_request_id=pull_request.pull_request_id))
904 pull_request_id=pull_request.pull_request_id))
902
905
903 assert response.status_int == 200
906 assert response.status_int == 200
904 assert_response = AssertResponse(response)
907 assert_response = AssertResponse(response)
905 assert_response.element_contains(
908 assert_response.element_contains(
906 '#changeset_compare_view_content .alert strong',
909 '#changeset_compare_view_content .alert strong',
907 'Missing commits')
910 'Missing commits')
908 assert_response.element_contains(
911 assert_response.element_contains(
909 '#changeset_compare_view_content .alert',
912 '#changeset_compare_view_content .alert',
910 'This pull request cannot be displayed, because one or more'
913 'This pull request cannot be displayed, because one or more'
911 ' commits no longer exist in the source repository.')
914 ' commits no longer exist in the source repository.')
912
915
913 def test_strip_commits_from_pull_request(
916 def test_strip_commits_from_pull_request(
914 self, backend, pr_util, csrf_token):
917 self, backend, pr_util, csrf_token):
915 commits = [
918 commits = [
916 {'message': 'initial-commit'},
919 {'message': 'initial-commit'},
917 {'message': 'old-feature'},
920 {'message': 'old-feature'},
918 {'message': 'new-feature', 'parents': ['initial-commit']},
921 {'message': 'new-feature', 'parents': ['initial-commit']},
919 ]
922 ]
920 pull_request = pr_util.create_pull_request(
923 pull_request = pr_util.create_pull_request(
921 commits, target_head='initial-commit', source_head='new-feature',
924 commits, target_head='initial-commit', source_head='new-feature',
922 revisions=['new-feature'])
925 revisions=['new-feature'])
923
926
924 vcs = pr_util.source_repository.scm_instance()
927 vcs = pr_util.source_repository.scm_instance()
925 if backend.alias == 'git':
928 if backend.alias == 'git':
926 vcs.strip(pr_util.commit_ids['new-feature'], branch_name='master')
929 vcs.strip(pr_util.commit_ids['new-feature'], branch_name='master')
927 else:
930 else:
928 vcs.strip(pr_util.commit_ids['new-feature'])
931 vcs.strip(pr_util.commit_ids['new-feature'])
929
932
930 response = self.app.get(route_path(
933 response = self.app.get(route_path(
931 'pullrequest_show',
934 'pullrequest_show',
932 repo_name=pr_util.target_repository.repo_name,
935 repo_name=pr_util.target_repository.repo_name,
933 pull_request_id=pull_request.pull_request_id))
936 pull_request_id=pull_request.pull_request_id))
934
937
935 assert response.status_int == 200
938 assert response.status_int == 200
936 assert_response = AssertResponse(response)
939 assert_response = AssertResponse(response)
937 assert_response.element_contains(
940 assert_response.element_contains(
938 '#changeset_compare_view_content .alert strong',
941 '#changeset_compare_view_content .alert strong',
939 'Missing commits')
942 'Missing commits')
940 assert_response.element_contains(
943 assert_response.element_contains(
941 '#changeset_compare_view_content .alert',
944 '#changeset_compare_view_content .alert',
942 'This pull request cannot be displayed, because one or more'
945 'This pull request cannot be displayed, because one or more'
943 ' commits no longer exist in the source repository.')
946 ' commits no longer exist in the source repository.')
944 assert_response.element_contains(
947 assert_response.element_contains(
945 '#update_commits',
948 '#update_commits',
946 'Update commits')
949 'Update commits')
947
950
948 def test_strip_commits_and_update(
951 def test_strip_commits_and_update(
949 self, backend, pr_util, csrf_token):
952 self, backend, pr_util, csrf_token):
950 commits = [
953 commits = [
951 {'message': 'initial-commit'},
954 {'message': 'initial-commit'},
952 {'message': 'old-feature'},
955 {'message': 'old-feature'},
953 {'message': 'new-feature', 'parents': ['old-feature']},
956 {'message': 'new-feature', 'parents': ['old-feature']},
954 ]
957 ]
955 pull_request = pr_util.create_pull_request(
958 pull_request = pr_util.create_pull_request(
956 commits, target_head='old-feature', source_head='new-feature',
959 commits, target_head='old-feature', source_head='new-feature',
957 revisions=['new-feature'], mergeable=True)
960 revisions=['new-feature'], mergeable=True)
958
961
959 vcs = pr_util.source_repository.scm_instance()
962 vcs = pr_util.source_repository.scm_instance()
960 if backend.alias == 'git':
963 if backend.alias == 'git':
961 vcs.strip(pr_util.commit_ids['new-feature'], branch_name='master')
964 vcs.strip(pr_util.commit_ids['new-feature'], branch_name='master')
962 else:
965 else:
963 vcs.strip(pr_util.commit_ids['new-feature'])
966 vcs.strip(pr_util.commit_ids['new-feature'])
964
967
965 response = self.app.post(
968 response = self.app.post(
966 route_path('pullrequest_update',
969 route_path('pullrequest_update',
967 repo_name=pull_request.target_repo.repo_name,
970 repo_name=pull_request.target_repo.repo_name,
968 pull_request_id=pull_request.pull_request_id),
971 pull_request_id=pull_request.pull_request_id),
969 params={'update_commits': 'true',
972 params={'update_commits': 'true',
970 'csrf_token': csrf_token})
973 'csrf_token': csrf_token})
971
974
972 assert response.status_int == 200
975 assert response.status_int == 200
973 assert response.body == 'true'
976 assert response.body == 'true'
974
977
975 # Make sure that after update, it won't raise 500 errors
978 # Make sure that after update, it won't raise 500 errors
976 response = self.app.get(route_path(
979 response = self.app.get(route_path(
977 'pullrequest_show',
980 'pullrequest_show',
978 repo_name=pr_util.target_repository.repo_name,
981 repo_name=pr_util.target_repository.repo_name,
979 pull_request_id=pull_request.pull_request_id))
982 pull_request_id=pull_request.pull_request_id))
980
983
981 assert response.status_int == 200
984 assert response.status_int == 200
982 assert_response = AssertResponse(response)
985 assert_response = AssertResponse(response)
983 assert_response.element_contains(
986 assert_response.element_contains(
984 '#changeset_compare_view_content .alert strong',
987 '#changeset_compare_view_content .alert strong',
985 'Missing commits')
988 'Missing commits')
986
989
987 def test_branch_is_a_link(self, pr_util):
990 def test_branch_is_a_link(self, pr_util):
988 pull_request = pr_util.create_pull_request()
991 pull_request = pr_util.create_pull_request()
989 pull_request.source_ref = 'branch:origin:1234567890abcdef'
992 pull_request.source_ref = 'branch:origin:1234567890abcdef'
990 pull_request.target_ref = 'branch:target:abcdef1234567890'
993 pull_request.target_ref = 'branch:target:abcdef1234567890'
991 Session().add(pull_request)
994 Session().add(pull_request)
992 Session().commit()
995 Session().commit()
993
996
994 response = self.app.get(route_path(
997 response = self.app.get(route_path(
995 'pullrequest_show',
998 'pullrequest_show',
996 repo_name=pull_request.target_repo.scm_instance().name,
999 repo_name=pull_request.target_repo.scm_instance().name,
997 pull_request_id=pull_request.pull_request_id))
1000 pull_request_id=pull_request.pull_request_id))
998 assert response.status_int == 200
1001 assert response.status_int == 200
999 assert_response = AssertResponse(response)
1002 assert_response = AssertResponse(response)
1000
1003
1001 origin = assert_response.get_element('.pr-origininfo .tag')
1004 origin = assert_response.get_element('.pr-origininfo .tag')
1002 origin_children = origin.getchildren()
1005 origin_children = origin.getchildren()
1003 assert len(origin_children) == 1
1006 assert len(origin_children) == 1
1004 target = assert_response.get_element('.pr-targetinfo .tag')
1007 target = assert_response.get_element('.pr-targetinfo .tag')
1005 target_children = target.getchildren()
1008 target_children = target.getchildren()
1006 assert len(target_children) == 1
1009 assert len(target_children) == 1
1007
1010
1008 expected_origin_link = route_path(
1011 expected_origin_link = route_path(
1009 'repo_changelog',
1012 'repo_changelog',
1010 repo_name=pull_request.source_repo.scm_instance().name,
1013 repo_name=pull_request.source_repo.scm_instance().name,
1011 params=dict(branch='origin'))
1014 params=dict(branch='origin'))
1012 expected_target_link = route_path(
1015 expected_target_link = route_path(
1013 'repo_changelog',
1016 'repo_changelog',
1014 repo_name=pull_request.target_repo.scm_instance().name,
1017 repo_name=pull_request.target_repo.scm_instance().name,
1015 params=dict(branch='target'))
1018 params=dict(branch='target'))
1016 assert origin_children[0].attrib['href'] == expected_origin_link
1019 assert origin_children[0].attrib['href'] == expected_origin_link
1017 assert origin_children[0].text == 'branch: origin'
1020 assert origin_children[0].text == 'branch: origin'
1018 assert target_children[0].attrib['href'] == expected_target_link
1021 assert target_children[0].attrib['href'] == expected_target_link
1019 assert target_children[0].text == 'branch: target'
1022 assert target_children[0].text == 'branch: target'
1020
1023
1021 def test_bookmark_is_not_a_link(self, pr_util):
1024 def test_bookmark_is_not_a_link(self, pr_util):
1022 pull_request = pr_util.create_pull_request()
1025 pull_request = pr_util.create_pull_request()
1023 pull_request.source_ref = 'bookmark:origin:1234567890abcdef'
1026 pull_request.source_ref = 'bookmark:origin:1234567890abcdef'
1024 pull_request.target_ref = 'bookmark:target:abcdef1234567890'
1027 pull_request.target_ref = 'bookmark:target:abcdef1234567890'
1025 Session().add(pull_request)
1028 Session().add(pull_request)
1026 Session().commit()
1029 Session().commit()
1027
1030
1028 response = self.app.get(route_path(
1031 response = self.app.get(route_path(
1029 'pullrequest_show',
1032 'pullrequest_show',
1030 repo_name=pull_request.target_repo.scm_instance().name,
1033 repo_name=pull_request.target_repo.scm_instance().name,
1031 pull_request_id=pull_request.pull_request_id))
1034 pull_request_id=pull_request.pull_request_id))
1032 assert response.status_int == 200
1035 assert response.status_int == 200
1033 assert_response = AssertResponse(response)
1036 assert_response = AssertResponse(response)
1034
1037
1035 origin = assert_response.get_element('.pr-origininfo .tag')
1038 origin = assert_response.get_element('.pr-origininfo .tag')
1036 assert origin.text.strip() == 'bookmark: origin'
1039 assert origin.text.strip() == 'bookmark: origin'
1037 assert origin.getchildren() == []
1040 assert origin.getchildren() == []
1038
1041
1039 target = assert_response.get_element('.pr-targetinfo .tag')
1042 target = assert_response.get_element('.pr-targetinfo .tag')
1040 assert target.text.strip() == 'bookmark: target'
1043 assert target.text.strip() == 'bookmark: target'
1041 assert target.getchildren() == []
1044 assert target.getchildren() == []
1042
1045
1043 def test_tag_is_not_a_link(self, pr_util):
1046 def test_tag_is_not_a_link(self, pr_util):
1044 pull_request = pr_util.create_pull_request()
1047 pull_request = pr_util.create_pull_request()
1045 pull_request.source_ref = 'tag:origin:1234567890abcdef'
1048 pull_request.source_ref = 'tag:origin:1234567890abcdef'
1046 pull_request.target_ref = 'tag:target:abcdef1234567890'
1049 pull_request.target_ref = 'tag:target:abcdef1234567890'
1047 Session().add(pull_request)
1050 Session().add(pull_request)
1048 Session().commit()
1051 Session().commit()
1049
1052
1050 response = self.app.get(route_path(
1053 response = self.app.get(route_path(
1051 'pullrequest_show',
1054 'pullrequest_show',
1052 repo_name=pull_request.target_repo.scm_instance().name,
1055 repo_name=pull_request.target_repo.scm_instance().name,
1053 pull_request_id=pull_request.pull_request_id))
1056 pull_request_id=pull_request.pull_request_id))
1054 assert response.status_int == 200
1057 assert response.status_int == 200
1055 assert_response = AssertResponse(response)
1058 assert_response = AssertResponse(response)
1056
1059
1057 origin = assert_response.get_element('.pr-origininfo .tag')
1060 origin = assert_response.get_element('.pr-origininfo .tag')
1058 assert origin.text.strip() == 'tag: origin'
1061 assert origin.text.strip() == 'tag: origin'
1059 assert origin.getchildren() == []
1062 assert origin.getchildren() == []
1060
1063
1061 target = assert_response.get_element('.pr-targetinfo .tag')
1064 target = assert_response.get_element('.pr-targetinfo .tag')
1062 assert target.text.strip() == 'tag: target'
1065 assert target.text.strip() == 'tag: target'
1063 assert target.getchildren() == []
1066 assert target.getchildren() == []
1064
1067
1065 @pytest.mark.parametrize('mergeable', [True, False])
1068 @pytest.mark.parametrize('mergeable', [True, False])
1066 def test_shadow_repository_link(
1069 def test_shadow_repository_link(
1067 self, mergeable, pr_util, http_host_only_stub):
1070 self, mergeable, pr_util, http_host_only_stub):
1068 """
1071 """
1069 Check that the pull request summary page displays a link to the shadow
1072 Check that the pull request summary page displays a link to the shadow
1070 repository if the pull request is mergeable. If it is not mergeable
1073 repository if the pull request is mergeable. If it is not mergeable
1071 the link should not be displayed.
1074 the link should not be displayed.
1072 """
1075 """
1073 pull_request = pr_util.create_pull_request(
1076 pull_request = pr_util.create_pull_request(
1074 mergeable=mergeable, enable_notifications=False)
1077 mergeable=mergeable, enable_notifications=False)
1075 target_repo = pull_request.target_repo.scm_instance()
1078 target_repo = pull_request.target_repo.scm_instance()
1076 pr_id = pull_request.pull_request_id
1079 pr_id = pull_request.pull_request_id
1077 shadow_url = '{host}/{repo}/pull-request/{pr_id}/repository'.format(
1080 shadow_url = '{host}/{repo}/pull-request/{pr_id}/repository'.format(
1078 host=http_host_only_stub, repo=target_repo.name, pr_id=pr_id)
1081 host=http_host_only_stub, repo=target_repo.name, pr_id=pr_id)
1079
1082
1080 response = self.app.get(route_path(
1083 response = self.app.get(route_path(
1081 'pullrequest_show',
1084 'pullrequest_show',
1082 repo_name=target_repo.name,
1085 repo_name=target_repo.name,
1083 pull_request_id=pr_id))
1086 pull_request_id=pr_id))
1084
1087
1085 assertr = AssertResponse(response)
1088 assertr = AssertResponse(response)
1086 if mergeable:
1089 if mergeable:
1087 assertr.element_value_contains('input.pr-mergeinfo', shadow_url)
1090 assertr.element_value_contains('input.pr-mergeinfo', shadow_url)
1088 assertr.element_value_contains('input.pr-mergeinfo ', 'pr-merge')
1091 assertr.element_value_contains('input.pr-mergeinfo ', 'pr-merge')
1089 else:
1092 else:
1090 assertr.no_element_exists('.pr-mergeinfo')
1093 assertr.no_element_exists('.pr-mergeinfo')
1091
1094
1092
1095
1093 @pytest.mark.usefixtures('app')
1096 @pytest.mark.usefixtures('app')
1094 @pytest.mark.backends("git", "hg")
1097 @pytest.mark.backends("git", "hg")
1095 class TestPullrequestsControllerDelete(object):
1098 class TestPullrequestsControllerDelete(object):
1096 def test_pull_request_delete_button_permissions_admin(
1099 def test_pull_request_delete_button_permissions_admin(
1097 self, autologin_user, user_admin, pr_util):
1100 self, autologin_user, user_admin, pr_util):
1098 pull_request = pr_util.create_pull_request(
1101 pull_request = pr_util.create_pull_request(
1099 author=user_admin.username, enable_notifications=False)
1102 author=user_admin.username, enable_notifications=False)
1100
1103
1101 response = self.app.get(route_path(
1104 response = self.app.get(route_path(
1102 'pullrequest_show',
1105 'pullrequest_show',
1103 repo_name=pull_request.target_repo.scm_instance().name,
1106 repo_name=pull_request.target_repo.scm_instance().name,
1104 pull_request_id=pull_request.pull_request_id))
1107 pull_request_id=pull_request.pull_request_id))
1105
1108
1106 response.mustcontain('id="delete_pullrequest"')
1109 response.mustcontain('id="delete_pullrequest"')
1107 response.mustcontain('Confirm to delete this pull request')
1110 response.mustcontain('Confirm to delete this pull request')
1108
1111
1109 def test_pull_request_delete_button_permissions_owner(
1112 def test_pull_request_delete_button_permissions_owner(
1110 self, autologin_regular_user, user_regular, pr_util):
1113 self, autologin_regular_user, user_regular, pr_util):
1111 pull_request = pr_util.create_pull_request(
1114 pull_request = pr_util.create_pull_request(
1112 author=user_regular.username, enable_notifications=False)
1115 author=user_regular.username, enable_notifications=False)
1113
1116
1114 response = self.app.get(route_path(
1117 response = self.app.get(route_path(
1115 'pullrequest_show',
1118 'pullrequest_show',
1116 repo_name=pull_request.target_repo.scm_instance().name,
1119 repo_name=pull_request.target_repo.scm_instance().name,
1117 pull_request_id=pull_request.pull_request_id))
1120 pull_request_id=pull_request.pull_request_id))
1118
1121
1119 response.mustcontain('id="delete_pullrequest"')
1122 response.mustcontain('id="delete_pullrequest"')
1120 response.mustcontain('Confirm to delete this pull request')
1123 response.mustcontain('Confirm to delete this pull request')
1121
1124
1122 def test_pull_request_delete_button_permissions_forbidden(
1125 def test_pull_request_delete_button_permissions_forbidden(
1123 self, autologin_regular_user, user_regular, user_admin, pr_util):
1126 self, autologin_regular_user, user_regular, user_admin, pr_util):
1124 pull_request = pr_util.create_pull_request(
1127 pull_request = pr_util.create_pull_request(
1125 author=user_admin.username, enable_notifications=False)
1128 author=user_admin.username, enable_notifications=False)
1126
1129
1127 response = self.app.get(route_path(
1130 response = self.app.get(route_path(
1128 'pullrequest_show',
1131 'pullrequest_show',
1129 repo_name=pull_request.target_repo.scm_instance().name,
1132 repo_name=pull_request.target_repo.scm_instance().name,
1130 pull_request_id=pull_request.pull_request_id))
1133 pull_request_id=pull_request.pull_request_id))
1131 response.mustcontain(no=['id="delete_pullrequest"'])
1134 response.mustcontain(no=['id="delete_pullrequest"'])
1132 response.mustcontain(no=['Confirm to delete this pull request'])
1135 response.mustcontain(no=['Confirm to delete this pull request'])
1133
1136
1134 def test_pull_request_delete_button_permissions_can_update_cannot_delete(
1137 def test_pull_request_delete_button_permissions_can_update_cannot_delete(
1135 self, autologin_regular_user, user_regular, user_admin, pr_util,
1138 self, autologin_regular_user, user_regular, user_admin, pr_util,
1136 user_util):
1139 user_util):
1137
1140
1138 pull_request = pr_util.create_pull_request(
1141 pull_request = pr_util.create_pull_request(
1139 author=user_admin.username, enable_notifications=False)
1142 author=user_admin.username, enable_notifications=False)
1140
1143
1141 user_util.grant_user_permission_to_repo(
1144 user_util.grant_user_permission_to_repo(
1142 pull_request.target_repo, user_regular,
1145 pull_request.target_repo, user_regular,
1143 'repository.write')
1146 'repository.write')
1144
1147
1145 response = self.app.get(route_path(
1148 response = self.app.get(route_path(
1146 'pullrequest_show',
1149 'pullrequest_show',
1147 repo_name=pull_request.target_repo.scm_instance().name,
1150 repo_name=pull_request.target_repo.scm_instance().name,
1148 pull_request_id=pull_request.pull_request_id))
1151 pull_request_id=pull_request.pull_request_id))
1149
1152
1150 response.mustcontain('id="open_edit_pullrequest"')
1153 response.mustcontain('id="open_edit_pullrequest"')
1151 response.mustcontain('id="delete_pullrequest"')
1154 response.mustcontain('id="delete_pullrequest"')
1152 response.mustcontain(no=['Confirm to delete this pull request'])
1155 response.mustcontain(no=['Confirm to delete this pull request'])
1153
1156
1154 def test_delete_comment_returns_404_if_comment_does_not_exist(
1157 def test_delete_comment_returns_404_if_comment_does_not_exist(
1155 self, autologin_user, pr_util, user_admin, csrf_token, xhr_header):
1158 self, autologin_user, pr_util, user_admin, csrf_token, xhr_header):
1156
1159
1157 pull_request = pr_util.create_pull_request(
1160 pull_request = pr_util.create_pull_request(
1158 author=user_admin.username, enable_notifications=False)
1161 author=user_admin.username, enable_notifications=False)
1159
1162
1160 self.app.post(
1163 self.app.post(
1161 route_path(
1164 route_path(
1162 'pullrequest_comment_delete',
1165 'pullrequest_comment_delete',
1163 repo_name=pull_request.target_repo.scm_instance().name,
1166 repo_name=pull_request.target_repo.scm_instance().name,
1164 pull_request_id=pull_request.pull_request_id,
1167 pull_request_id=pull_request.pull_request_id,
1165 comment_id=1024404),
1168 comment_id=1024404),
1166 extra_environ=xhr_header,
1169 extra_environ=xhr_header,
1167 params={'csrf_token': csrf_token},
1170 params={'csrf_token': csrf_token},
1168 status=404
1171 status=404
1169 )
1172 )
1170
1173
1171 def test_delete_comment(
1174 def test_delete_comment(
1172 self, autologin_user, pr_util, user_admin, csrf_token, xhr_header):
1175 self, autologin_user, pr_util, user_admin, csrf_token, xhr_header):
1173
1176
1174 pull_request = pr_util.create_pull_request(
1177 pull_request = pr_util.create_pull_request(
1175 author=user_admin.username, enable_notifications=False)
1178 author=user_admin.username, enable_notifications=False)
1176 comment = pr_util.create_comment()
1179 comment = pr_util.create_comment()
1177 comment_id = comment.comment_id
1180 comment_id = comment.comment_id
1178
1181
1179 response = self.app.post(
1182 response = self.app.post(
1180 route_path(
1183 route_path(
1181 'pullrequest_comment_delete',
1184 'pullrequest_comment_delete',
1182 repo_name=pull_request.target_repo.scm_instance().name,
1185 repo_name=pull_request.target_repo.scm_instance().name,
1183 pull_request_id=pull_request.pull_request_id,
1186 pull_request_id=pull_request.pull_request_id,
1184 comment_id=comment_id),
1187 comment_id=comment_id),
1185 extra_environ=xhr_header,
1188 extra_environ=xhr_header,
1186 params={'csrf_token': csrf_token},
1189 params={'csrf_token': csrf_token},
1187 status=200
1190 status=200
1188 )
1191 )
1189 assert response.body == 'true'
1192 assert response.body == 'true'
1190
1193
1191
1194
1192 def assert_pull_request_status(pull_request, expected_status):
1195 def assert_pull_request_status(pull_request, expected_status):
1193 status = ChangesetStatusModel().calculated_review_status(
1196 status = ChangesetStatusModel().calculated_review_status(
1194 pull_request=pull_request)
1197 pull_request=pull_request)
1195 assert status == expected_status
1198 assert status == expected_status
1196
1199
1197
1200
1198 @pytest.mark.parametrize('route', ['pullrequest_new', 'pullrequest_create'])
1201 @pytest.mark.parametrize('route', ['pullrequest_new', 'pullrequest_create'])
1199 @pytest.mark.usefixtures("autologin_user")
1202 @pytest.mark.usefixtures("autologin_user")
1200 def test_forbidde_to_repo_summary_for_svn_repositories(backend_svn, app, route):
1203 def test_forbidde_to_repo_summary_for_svn_repositories(backend_svn, app, route):
1201 response = app.get(
1204 response = app.get(
1202 route_path(route, repo_name=backend_svn.repo_name), status=404)
1205 route_path(route, repo_name=backend_svn.repo_name), status=404)
1203
1206
@@ -1,1316 +1,1324 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2018 RhodeCode GmbH
3 # Copyright (C) 2011-2018 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 import logging
21 import logging
22 import collections
22 import collections
23
23
24 import formencode
24 import formencode
25 import formencode.htmlfill
25 import formencode.htmlfill
26 import peppercorn
26 import peppercorn
27 from pyramid.httpexceptions import (
27 from pyramid.httpexceptions import (
28 HTTPFound, HTTPNotFound, HTTPForbidden, HTTPBadRequest)
28 HTTPFound, HTTPNotFound, HTTPForbidden, HTTPBadRequest)
29 from pyramid.view import view_config
29 from pyramid.view import view_config
30 from pyramid.renderers import render
30 from pyramid.renderers import render
31
31
32 from rhodecode import events
32 from rhodecode import events
33 from rhodecode.apps._base import RepoAppView, DataGridAppView
33 from rhodecode.apps._base import RepoAppView, DataGridAppView
34
34
35 from rhodecode.lib import helpers as h, diffs, codeblocks, channelstream
35 from rhodecode.lib import helpers as h, diffs, codeblocks, channelstream
36 from rhodecode.lib.base import vcs_operation_context
36 from rhodecode.lib.base import vcs_operation_context
37 from rhodecode.lib.diffs import load_cached_diff, cache_diff, diff_cache_exist
37 from rhodecode.lib.diffs import load_cached_diff, cache_diff, diff_cache_exist
38 from rhodecode.lib.ext_json import json
38 from rhodecode.lib.ext_json import json
39 from rhodecode.lib.auth import (
39 from rhodecode.lib.auth import (
40 LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator,
40 LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator,
41 NotAnonymous, CSRFRequired)
41 NotAnonymous, CSRFRequired)
42 from rhodecode.lib.utils2 import str2bool, safe_str, safe_unicode
42 from rhodecode.lib.utils2 import str2bool, safe_str, safe_unicode
43 from rhodecode.lib.vcs.backends.base import EmptyCommit, UpdateFailureReason
43 from rhodecode.lib.vcs.backends.base import EmptyCommit, UpdateFailureReason
44 from rhodecode.lib.vcs.exceptions import (CommitDoesNotExistError,
44 from rhodecode.lib.vcs.exceptions import (CommitDoesNotExistError,
45 RepositoryRequirementError, EmptyRepositoryError)
45 RepositoryRequirementError, EmptyRepositoryError)
46 from rhodecode.model.changeset_status import ChangesetStatusModel
46 from rhodecode.model.changeset_status import ChangesetStatusModel
47 from rhodecode.model.comment import CommentsModel
47 from rhodecode.model.comment import CommentsModel
48 from rhodecode.model.db import (func, or_, PullRequest, PullRequestVersion,
48 from rhodecode.model.db import (func, or_, PullRequest, PullRequestVersion,
49 ChangesetComment, ChangesetStatus, Repository)
49 ChangesetComment, ChangesetStatus, Repository)
50 from rhodecode.model.forms import PullRequestForm
50 from rhodecode.model.forms import PullRequestForm
51 from rhodecode.model.meta import Session
51 from rhodecode.model.meta import Session
52 from rhodecode.model.pull_request import PullRequestModel, MergeCheck
52 from rhodecode.model.pull_request import PullRequestModel, MergeCheck
53 from rhodecode.model.scm import ScmModel
53 from rhodecode.model.scm import ScmModel
54
54
55 log = logging.getLogger(__name__)
55 log = logging.getLogger(__name__)
56
56
57
57
58 class RepoPullRequestsView(RepoAppView, DataGridAppView):
58 class RepoPullRequestsView(RepoAppView, DataGridAppView):
59
59
60 def load_default_context(self):
60 def load_default_context(self):
61 c = self._get_local_tmpl_context(include_app_defaults=True)
61 c = self._get_local_tmpl_context(include_app_defaults=True)
62 c.REVIEW_STATUS_APPROVED = ChangesetStatus.STATUS_APPROVED
62 c.REVIEW_STATUS_APPROVED = ChangesetStatus.STATUS_APPROVED
63 c.REVIEW_STATUS_REJECTED = ChangesetStatus.STATUS_REJECTED
63 c.REVIEW_STATUS_REJECTED = ChangesetStatus.STATUS_REJECTED
64
64 # backward compat., we use for OLD PRs a plain renderer
65 c.renderer = 'plain'
65 return c
66 return c
66
67
67 def _get_pull_requests_list(
68 def _get_pull_requests_list(
68 self, repo_name, source, filter_type, opened_by, statuses):
69 self, repo_name, source, filter_type, opened_by, statuses):
69
70
70 draw, start, limit = self._extract_chunk(self.request)
71 draw, start, limit = self._extract_chunk(self.request)
71 search_q, order_by, order_dir = self._extract_ordering(self.request)
72 search_q, order_by, order_dir = self._extract_ordering(self.request)
72 _render = self.request.get_partial_renderer(
73 _render = self.request.get_partial_renderer(
73 'rhodecode:templates/data_table/_dt_elements.mako')
74 'rhodecode:templates/data_table/_dt_elements.mako')
74
75
75 # pagination
76 # pagination
76
77
77 if filter_type == 'awaiting_review':
78 if filter_type == 'awaiting_review':
78 pull_requests = PullRequestModel().get_awaiting_review(
79 pull_requests = PullRequestModel().get_awaiting_review(
79 repo_name, source=source, opened_by=opened_by,
80 repo_name, source=source, opened_by=opened_by,
80 statuses=statuses, offset=start, length=limit,
81 statuses=statuses, offset=start, length=limit,
81 order_by=order_by, order_dir=order_dir)
82 order_by=order_by, order_dir=order_dir)
82 pull_requests_total_count = PullRequestModel().count_awaiting_review(
83 pull_requests_total_count = PullRequestModel().count_awaiting_review(
83 repo_name, source=source, statuses=statuses,
84 repo_name, source=source, statuses=statuses,
84 opened_by=opened_by)
85 opened_by=opened_by)
85 elif filter_type == 'awaiting_my_review':
86 elif filter_type == 'awaiting_my_review':
86 pull_requests = PullRequestModel().get_awaiting_my_review(
87 pull_requests = PullRequestModel().get_awaiting_my_review(
87 repo_name, source=source, opened_by=opened_by,
88 repo_name, source=source, opened_by=opened_by,
88 user_id=self._rhodecode_user.user_id, statuses=statuses,
89 user_id=self._rhodecode_user.user_id, statuses=statuses,
89 offset=start, length=limit, order_by=order_by,
90 offset=start, length=limit, order_by=order_by,
90 order_dir=order_dir)
91 order_dir=order_dir)
91 pull_requests_total_count = PullRequestModel().count_awaiting_my_review(
92 pull_requests_total_count = PullRequestModel().count_awaiting_my_review(
92 repo_name, source=source, user_id=self._rhodecode_user.user_id,
93 repo_name, source=source, user_id=self._rhodecode_user.user_id,
93 statuses=statuses, opened_by=opened_by)
94 statuses=statuses, opened_by=opened_by)
94 else:
95 else:
95 pull_requests = PullRequestModel().get_all(
96 pull_requests = PullRequestModel().get_all(
96 repo_name, source=source, opened_by=opened_by,
97 repo_name, source=source, opened_by=opened_by,
97 statuses=statuses, offset=start, length=limit,
98 statuses=statuses, offset=start, length=limit,
98 order_by=order_by, order_dir=order_dir)
99 order_by=order_by, order_dir=order_dir)
99 pull_requests_total_count = PullRequestModel().count_all(
100 pull_requests_total_count = PullRequestModel().count_all(
100 repo_name, source=source, statuses=statuses,
101 repo_name, source=source, statuses=statuses,
101 opened_by=opened_by)
102 opened_by=opened_by)
102
103
103 data = []
104 data = []
104 comments_model = CommentsModel()
105 comments_model = CommentsModel()
105 for pr in pull_requests:
106 for pr in pull_requests:
106 comments = comments_model.get_all_comments(
107 comments = comments_model.get_all_comments(
107 self.db_repo.repo_id, pull_request=pr)
108 self.db_repo.repo_id, pull_request=pr)
108
109
109 data.append({
110 data.append({
110 'name': _render('pullrequest_name',
111 'name': _render('pullrequest_name',
111 pr.pull_request_id, pr.target_repo.repo_name),
112 pr.pull_request_id, pr.target_repo.repo_name),
112 'name_raw': pr.pull_request_id,
113 'name_raw': pr.pull_request_id,
113 'status': _render('pullrequest_status',
114 'status': _render('pullrequest_status',
114 pr.calculated_review_status()),
115 pr.calculated_review_status()),
115 'title': _render(
116 'title': _render(
116 'pullrequest_title', pr.title, pr.description),
117 'pullrequest_title', pr.title, pr.description),
117 'description': h.escape(pr.description),
118 'description': h.escape(pr.description),
118 'updated_on': _render('pullrequest_updated_on',
119 'updated_on': _render('pullrequest_updated_on',
119 h.datetime_to_time(pr.updated_on)),
120 h.datetime_to_time(pr.updated_on)),
120 'updated_on_raw': h.datetime_to_time(pr.updated_on),
121 'updated_on_raw': h.datetime_to_time(pr.updated_on),
121 'created_on': _render('pullrequest_updated_on',
122 'created_on': _render('pullrequest_updated_on',
122 h.datetime_to_time(pr.created_on)),
123 h.datetime_to_time(pr.created_on)),
123 'created_on_raw': h.datetime_to_time(pr.created_on),
124 'created_on_raw': h.datetime_to_time(pr.created_on),
124 'author': _render('pullrequest_author',
125 'author': _render('pullrequest_author',
125 pr.author.full_contact, ),
126 pr.author.full_contact, ),
126 'author_raw': pr.author.full_name,
127 'author_raw': pr.author.full_name,
127 'comments': _render('pullrequest_comments', len(comments)),
128 'comments': _render('pullrequest_comments', len(comments)),
128 'comments_raw': len(comments),
129 'comments_raw': len(comments),
129 'closed': pr.is_closed(),
130 'closed': pr.is_closed(),
130 })
131 })
131
132
132 data = ({
133 data = ({
133 'draw': draw,
134 'draw': draw,
134 'data': data,
135 'data': data,
135 'recordsTotal': pull_requests_total_count,
136 'recordsTotal': pull_requests_total_count,
136 'recordsFiltered': pull_requests_total_count,
137 'recordsFiltered': pull_requests_total_count,
137 })
138 })
138 return data
139 return data
139
140
140 @LoginRequired()
141 @LoginRequired()
141 @HasRepoPermissionAnyDecorator(
142 @HasRepoPermissionAnyDecorator(
142 'repository.read', 'repository.write', 'repository.admin')
143 'repository.read', 'repository.write', 'repository.admin')
143 @view_config(
144 @view_config(
144 route_name='pullrequest_show_all', request_method='GET',
145 route_name='pullrequest_show_all', request_method='GET',
145 renderer='rhodecode:templates/pullrequests/pullrequests.mako')
146 renderer='rhodecode:templates/pullrequests/pullrequests.mako')
146 def pull_request_list(self):
147 def pull_request_list(self):
147 c = self.load_default_context()
148 c = self.load_default_context()
148
149
149 req_get = self.request.GET
150 req_get = self.request.GET
150 c.source = str2bool(req_get.get('source'))
151 c.source = str2bool(req_get.get('source'))
151 c.closed = str2bool(req_get.get('closed'))
152 c.closed = str2bool(req_get.get('closed'))
152 c.my = str2bool(req_get.get('my'))
153 c.my = str2bool(req_get.get('my'))
153 c.awaiting_review = str2bool(req_get.get('awaiting_review'))
154 c.awaiting_review = str2bool(req_get.get('awaiting_review'))
154 c.awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
155 c.awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
155
156
156 c.active = 'open'
157 c.active = 'open'
157 if c.my:
158 if c.my:
158 c.active = 'my'
159 c.active = 'my'
159 if c.closed:
160 if c.closed:
160 c.active = 'closed'
161 c.active = 'closed'
161 if c.awaiting_review and not c.source:
162 if c.awaiting_review and not c.source:
162 c.active = 'awaiting'
163 c.active = 'awaiting'
163 if c.source and not c.awaiting_review:
164 if c.source and not c.awaiting_review:
164 c.active = 'source'
165 c.active = 'source'
165 if c.awaiting_my_review:
166 if c.awaiting_my_review:
166 c.active = 'awaiting_my'
167 c.active = 'awaiting_my'
167
168
168 return self._get_template_context(c)
169 return self._get_template_context(c)
169
170
170 @LoginRequired()
171 @LoginRequired()
171 @HasRepoPermissionAnyDecorator(
172 @HasRepoPermissionAnyDecorator(
172 'repository.read', 'repository.write', 'repository.admin')
173 'repository.read', 'repository.write', 'repository.admin')
173 @view_config(
174 @view_config(
174 route_name='pullrequest_show_all_data', request_method='GET',
175 route_name='pullrequest_show_all_data', request_method='GET',
175 renderer='json_ext', xhr=True)
176 renderer='json_ext', xhr=True)
176 def pull_request_list_data(self):
177 def pull_request_list_data(self):
177 self.load_default_context()
178 self.load_default_context()
178
179
179 # additional filters
180 # additional filters
180 req_get = self.request.GET
181 req_get = self.request.GET
181 source = str2bool(req_get.get('source'))
182 source = str2bool(req_get.get('source'))
182 closed = str2bool(req_get.get('closed'))
183 closed = str2bool(req_get.get('closed'))
183 my = str2bool(req_get.get('my'))
184 my = str2bool(req_get.get('my'))
184 awaiting_review = str2bool(req_get.get('awaiting_review'))
185 awaiting_review = str2bool(req_get.get('awaiting_review'))
185 awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
186 awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
186
187
187 filter_type = 'awaiting_review' if awaiting_review \
188 filter_type = 'awaiting_review' if awaiting_review \
188 else 'awaiting_my_review' if awaiting_my_review \
189 else 'awaiting_my_review' if awaiting_my_review \
189 else None
190 else None
190
191
191 opened_by = None
192 opened_by = None
192 if my:
193 if my:
193 opened_by = [self._rhodecode_user.user_id]
194 opened_by = [self._rhodecode_user.user_id]
194
195
195 statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN]
196 statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN]
196 if closed:
197 if closed:
197 statuses = [PullRequest.STATUS_CLOSED]
198 statuses = [PullRequest.STATUS_CLOSED]
198
199
199 data = self._get_pull_requests_list(
200 data = self._get_pull_requests_list(
200 repo_name=self.db_repo_name, source=source,
201 repo_name=self.db_repo_name, source=source,
201 filter_type=filter_type, opened_by=opened_by, statuses=statuses)
202 filter_type=filter_type, opened_by=opened_by, statuses=statuses)
202
203
203 return data
204 return data
204
205
205 def _is_diff_cache_enabled(self, target_repo):
206 def _is_diff_cache_enabled(self, target_repo):
206 caching_enabled = self._get_general_setting(
207 caching_enabled = self._get_general_setting(
207 target_repo, 'rhodecode_diff_cache')
208 target_repo, 'rhodecode_diff_cache')
208 log.debug('Diff caching enabled: %s', caching_enabled)
209 log.debug('Diff caching enabled: %s', caching_enabled)
209 return caching_enabled
210 return caching_enabled
210
211
211 def _get_diffset(self, source_repo_name, source_repo,
212 def _get_diffset(self, source_repo_name, source_repo,
212 source_ref_id, target_ref_id,
213 source_ref_id, target_ref_id,
213 target_commit, source_commit, diff_limit, file_limit,
214 target_commit, source_commit, diff_limit, file_limit,
214 fulldiff):
215 fulldiff):
215
216
216 vcs_diff = PullRequestModel().get_diff(
217 vcs_diff = PullRequestModel().get_diff(
217 source_repo, source_ref_id, target_ref_id)
218 source_repo, source_ref_id, target_ref_id)
218
219
219 diff_processor = diffs.DiffProcessor(
220 diff_processor = diffs.DiffProcessor(
220 vcs_diff, format='newdiff', diff_limit=diff_limit,
221 vcs_diff, format='newdiff', diff_limit=diff_limit,
221 file_limit=file_limit, show_full_diff=fulldiff)
222 file_limit=file_limit, show_full_diff=fulldiff)
222
223
223 _parsed = diff_processor.prepare()
224 _parsed = diff_processor.prepare()
224
225
225 diffset = codeblocks.DiffSet(
226 diffset = codeblocks.DiffSet(
226 repo_name=self.db_repo_name,
227 repo_name=self.db_repo_name,
227 source_repo_name=source_repo_name,
228 source_repo_name=source_repo_name,
228 source_node_getter=codeblocks.diffset_node_getter(target_commit),
229 source_node_getter=codeblocks.diffset_node_getter(target_commit),
229 target_node_getter=codeblocks.diffset_node_getter(source_commit),
230 target_node_getter=codeblocks.diffset_node_getter(source_commit),
230 )
231 )
231 diffset = self.path_filter.render_patchset_filtered(
232 diffset = self.path_filter.render_patchset_filtered(
232 diffset, _parsed, target_commit.raw_id, source_commit.raw_id)
233 diffset, _parsed, target_commit.raw_id, source_commit.raw_id)
233
234
234 return diffset
235 return diffset
235
236
236 @LoginRequired()
237 @LoginRequired()
237 @HasRepoPermissionAnyDecorator(
238 @HasRepoPermissionAnyDecorator(
238 'repository.read', 'repository.write', 'repository.admin')
239 'repository.read', 'repository.write', 'repository.admin')
239 @view_config(
240 @view_config(
240 route_name='pullrequest_show', request_method='GET',
241 route_name='pullrequest_show', request_method='GET',
241 renderer='rhodecode:templates/pullrequests/pullrequest_show.mako')
242 renderer='rhodecode:templates/pullrequests/pullrequest_show.mako')
242 def pull_request_show(self):
243 def pull_request_show(self):
243 pull_request_id = self.request.matchdict['pull_request_id']
244 pull_request_id = self.request.matchdict['pull_request_id']
244
245
245 c = self.load_default_context()
246 c = self.load_default_context()
246
247
247 version = self.request.GET.get('version')
248 version = self.request.GET.get('version')
248 from_version = self.request.GET.get('from_version') or version
249 from_version = self.request.GET.get('from_version') or version
249 merge_checks = self.request.GET.get('merge_checks')
250 merge_checks = self.request.GET.get('merge_checks')
250 c.fulldiff = str2bool(self.request.GET.get('fulldiff'))
251 c.fulldiff = str2bool(self.request.GET.get('fulldiff'))
251 force_refresh = str2bool(self.request.GET.get('force_refresh'))
252 force_refresh = str2bool(self.request.GET.get('force_refresh'))
252
253
253 (pull_request_latest,
254 (pull_request_latest,
254 pull_request_at_ver,
255 pull_request_at_ver,
255 pull_request_display_obj,
256 pull_request_display_obj,
256 at_version) = PullRequestModel().get_pr_version(
257 at_version) = PullRequestModel().get_pr_version(
257 pull_request_id, version=version)
258 pull_request_id, version=version)
258 pr_closed = pull_request_latest.is_closed()
259 pr_closed = pull_request_latest.is_closed()
259
260
260 if pr_closed and (version or from_version):
261 if pr_closed and (version or from_version):
261 # not allow to browse versions
262 # not allow to browse versions
262 raise HTTPFound(h.route_path(
263 raise HTTPFound(h.route_path(
263 'pullrequest_show', repo_name=self.db_repo_name,
264 'pullrequest_show', repo_name=self.db_repo_name,
264 pull_request_id=pull_request_id))
265 pull_request_id=pull_request_id))
265
266
266 versions = pull_request_display_obj.versions()
267 versions = pull_request_display_obj.versions()
267
268
268 c.at_version = at_version
269 c.at_version = at_version
269 c.at_version_num = (at_version
270 c.at_version_num = (at_version
270 if at_version and at_version != 'latest'
271 if at_version and at_version != 'latest'
271 else None)
272 else None)
272 c.at_version_pos = ChangesetComment.get_index_from_version(
273 c.at_version_pos = ChangesetComment.get_index_from_version(
273 c.at_version_num, versions)
274 c.at_version_num, versions)
274
275
275 (prev_pull_request_latest,
276 (prev_pull_request_latest,
276 prev_pull_request_at_ver,
277 prev_pull_request_at_ver,
277 prev_pull_request_display_obj,
278 prev_pull_request_display_obj,
278 prev_at_version) = PullRequestModel().get_pr_version(
279 prev_at_version) = PullRequestModel().get_pr_version(
279 pull_request_id, version=from_version)
280 pull_request_id, version=from_version)
280
281
281 c.from_version = prev_at_version
282 c.from_version = prev_at_version
282 c.from_version_num = (prev_at_version
283 c.from_version_num = (prev_at_version
283 if prev_at_version and prev_at_version != 'latest'
284 if prev_at_version and prev_at_version != 'latest'
284 else None)
285 else None)
285 c.from_version_pos = ChangesetComment.get_index_from_version(
286 c.from_version_pos = ChangesetComment.get_index_from_version(
286 c.from_version_num, versions)
287 c.from_version_num, versions)
287
288
288 # define if we're in COMPARE mode or VIEW at version mode
289 # define if we're in COMPARE mode or VIEW at version mode
289 compare = at_version != prev_at_version
290 compare = at_version != prev_at_version
290
291
291 # pull_requests repo_name we opened it against
292 # pull_requests repo_name we opened it against
292 # ie. target_repo must match
293 # ie. target_repo must match
293 if self.db_repo_name != pull_request_at_ver.target_repo.repo_name:
294 if self.db_repo_name != pull_request_at_ver.target_repo.repo_name:
294 raise HTTPNotFound()
295 raise HTTPNotFound()
295
296
296 c.shadow_clone_url = PullRequestModel().get_shadow_clone_url(
297 c.shadow_clone_url = PullRequestModel().get_shadow_clone_url(
297 pull_request_at_ver)
298 pull_request_at_ver)
298
299
299 c.pull_request = pull_request_display_obj
300 c.pull_request = pull_request_display_obj
301 c.renderer = pull_request_at_ver.description_renderer or c.renderer
300 c.pull_request_latest = pull_request_latest
302 c.pull_request_latest = pull_request_latest
301
303
302 if compare or (at_version and not at_version == 'latest'):
304 if compare or (at_version and not at_version == 'latest'):
303 c.allowed_to_change_status = False
305 c.allowed_to_change_status = False
304 c.allowed_to_update = False
306 c.allowed_to_update = False
305 c.allowed_to_merge = False
307 c.allowed_to_merge = False
306 c.allowed_to_delete = False
308 c.allowed_to_delete = False
307 c.allowed_to_comment = False
309 c.allowed_to_comment = False
308 c.allowed_to_close = False
310 c.allowed_to_close = False
309 else:
311 else:
310 can_change_status = PullRequestModel().check_user_change_status(
312 can_change_status = PullRequestModel().check_user_change_status(
311 pull_request_at_ver, self._rhodecode_user)
313 pull_request_at_ver, self._rhodecode_user)
312 c.allowed_to_change_status = can_change_status and not pr_closed
314 c.allowed_to_change_status = can_change_status and not pr_closed
313
315
314 c.allowed_to_update = PullRequestModel().check_user_update(
316 c.allowed_to_update = PullRequestModel().check_user_update(
315 pull_request_latest, self._rhodecode_user) and not pr_closed
317 pull_request_latest, self._rhodecode_user) and not pr_closed
316 c.allowed_to_merge = PullRequestModel().check_user_merge(
318 c.allowed_to_merge = PullRequestModel().check_user_merge(
317 pull_request_latest, self._rhodecode_user) and not pr_closed
319 pull_request_latest, self._rhodecode_user) and not pr_closed
318 c.allowed_to_delete = PullRequestModel().check_user_delete(
320 c.allowed_to_delete = PullRequestModel().check_user_delete(
319 pull_request_latest, self._rhodecode_user) and not pr_closed
321 pull_request_latest, self._rhodecode_user) and not pr_closed
320 c.allowed_to_comment = not pr_closed
322 c.allowed_to_comment = not pr_closed
321 c.allowed_to_close = c.allowed_to_merge and not pr_closed
323 c.allowed_to_close = c.allowed_to_merge and not pr_closed
322
324
323 c.forbid_adding_reviewers = False
325 c.forbid_adding_reviewers = False
324 c.forbid_author_to_review = False
326 c.forbid_author_to_review = False
325 c.forbid_commit_author_to_review = False
327 c.forbid_commit_author_to_review = False
326
328
327 if pull_request_latest.reviewer_data and \
329 if pull_request_latest.reviewer_data and \
328 'rules' in pull_request_latest.reviewer_data:
330 'rules' in pull_request_latest.reviewer_data:
329 rules = pull_request_latest.reviewer_data['rules'] or {}
331 rules = pull_request_latest.reviewer_data['rules'] or {}
330 try:
332 try:
331 c.forbid_adding_reviewers = rules.get(
333 c.forbid_adding_reviewers = rules.get(
332 'forbid_adding_reviewers')
334 'forbid_adding_reviewers')
333 c.forbid_author_to_review = rules.get(
335 c.forbid_author_to_review = rules.get(
334 'forbid_author_to_review')
336 'forbid_author_to_review')
335 c.forbid_commit_author_to_review = rules.get(
337 c.forbid_commit_author_to_review = rules.get(
336 'forbid_commit_author_to_review')
338 'forbid_commit_author_to_review')
337 except Exception:
339 except Exception:
338 pass
340 pass
339
341
340 # check merge capabilities
342 # check merge capabilities
341 _merge_check = MergeCheck.validate(
343 _merge_check = MergeCheck.validate(
342 pull_request_latest, user=self._rhodecode_user,
344 pull_request_latest, user=self._rhodecode_user,
343 translator=self.request.translate,
345 translator=self.request.translate,
344 force_shadow_repo_refresh=force_refresh)
346 force_shadow_repo_refresh=force_refresh)
345 c.pr_merge_errors = _merge_check.error_details
347 c.pr_merge_errors = _merge_check.error_details
346 c.pr_merge_possible = not _merge_check.failed
348 c.pr_merge_possible = not _merge_check.failed
347 c.pr_merge_message = _merge_check.merge_msg
349 c.pr_merge_message = _merge_check.merge_msg
348
350
349 c.pr_merge_info = MergeCheck.get_merge_conditions(
351 c.pr_merge_info = MergeCheck.get_merge_conditions(
350 pull_request_latest, translator=self.request.translate)
352 pull_request_latest, translator=self.request.translate)
351
353
352 c.pull_request_review_status = _merge_check.review_status
354 c.pull_request_review_status = _merge_check.review_status
353 if merge_checks:
355 if merge_checks:
354 self.request.override_renderer = \
356 self.request.override_renderer = \
355 'rhodecode:templates/pullrequests/pullrequest_merge_checks.mako'
357 'rhodecode:templates/pullrequests/pullrequest_merge_checks.mako'
356 return self._get_template_context(c)
358 return self._get_template_context(c)
357
359
358 comments_model = CommentsModel()
360 comments_model = CommentsModel()
359
361
360 # reviewers and statuses
362 # reviewers and statuses
361 c.pull_request_reviewers = pull_request_at_ver.reviewers_statuses()
363 c.pull_request_reviewers = pull_request_at_ver.reviewers_statuses()
362 allowed_reviewers = [x[0].user_id for x in c.pull_request_reviewers]
364 allowed_reviewers = [x[0].user_id for x in c.pull_request_reviewers]
363
365
364 # GENERAL COMMENTS with versions #
366 # GENERAL COMMENTS with versions #
365 q = comments_model._all_general_comments_of_pull_request(pull_request_latest)
367 q = comments_model._all_general_comments_of_pull_request(pull_request_latest)
366 q = q.order_by(ChangesetComment.comment_id.asc())
368 q = q.order_by(ChangesetComment.comment_id.asc())
367 general_comments = q
369 general_comments = q
368
370
369 # pick comments we want to render at current version
371 # pick comments we want to render at current version
370 c.comment_versions = comments_model.aggregate_comments(
372 c.comment_versions = comments_model.aggregate_comments(
371 general_comments, versions, c.at_version_num)
373 general_comments, versions, c.at_version_num)
372 c.comments = c.comment_versions[c.at_version_num]['until']
374 c.comments = c.comment_versions[c.at_version_num]['until']
373
375
374 # INLINE COMMENTS with versions #
376 # INLINE COMMENTS with versions #
375 q = comments_model._all_inline_comments_of_pull_request(pull_request_latest)
377 q = comments_model._all_inline_comments_of_pull_request(pull_request_latest)
376 q = q.order_by(ChangesetComment.comment_id.asc())
378 q = q.order_by(ChangesetComment.comment_id.asc())
377 inline_comments = q
379 inline_comments = q
378
380
379 c.inline_versions = comments_model.aggregate_comments(
381 c.inline_versions = comments_model.aggregate_comments(
380 inline_comments, versions, c.at_version_num, inline=True)
382 inline_comments, versions, c.at_version_num, inline=True)
381
383
382 # inject latest version
384 # inject latest version
383 latest_ver = PullRequest.get_pr_display_object(
385 latest_ver = PullRequest.get_pr_display_object(
384 pull_request_latest, pull_request_latest)
386 pull_request_latest, pull_request_latest)
385
387
386 c.versions = versions + [latest_ver]
388 c.versions = versions + [latest_ver]
387
389
388 # if we use version, then do not show later comments
390 # if we use version, then do not show later comments
389 # than current version
391 # than current version
390 display_inline_comments = collections.defaultdict(
392 display_inline_comments = collections.defaultdict(
391 lambda: collections.defaultdict(list))
393 lambda: collections.defaultdict(list))
392 for co in inline_comments:
394 for co in inline_comments:
393 if c.at_version_num:
395 if c.at_version_num:
394 # pick comments that are at least UPTO given version, so we
396 # pick comments that are at least UPTO given version, so we
395 # don't render comments for higher version
397 # don't render comments for higher version
396 should_render = co.pull_request_version_id and \
398 should_render = co.pull_request_version_id and \
397 co.pull_request_version_id <= c.at_version_num
399 co.pull_request_version_id <= c.at_version_num
398 else:
400 else:
399 # showing all, for 'latest'
401 # showing all, for 'latest'
400 should_render = True
402 should_render = True
401
403
402 if should_render:
404 if should_render:
403 display_inline_comments[co.f_path][co.line_no].append(co)
405 display_inline_comments[co.f_path][co.line_no].append(co)
404
406
405 # load diff data into template context, if we use compare mode then
407 # load diff data into template context, if we use compare mode then
406 # diff is calculated based on changes between versions of PR
408 # diff is calculated based on changes between versions of PR
407
409
408 source_repo = pull_request_at_ver.source_repo
410 source_repo = pull_request_at_ver.source_repo
409 source_ref_id = pull_request_at_ver.source_ref_parts.commit_id
411 source_ref_id = pull_request_at_ver.source_ref_parts.commit_id
410
412
411 target_repo = pull_request_at_ver.target_repo
413 target_repo = pull_request_at_ver.target_repo
412 target_ref_id = pull_request_at_ver.target_ref_parts.commit_id
414 target_ref_id = pull_request_at_ver.target_ref_parts.commit_id
413
415
414 if compare:
416 if compare:
415 # in compare switch the diff base to latest commit from prev version
417 # in compare switch the diff base to latest commit from prev version
416 target_ref_id = prev_pull_request_display_obj.revisions[0]
418 target_ref_id = prev_pull_request_display_obj.revisions[0]
417
419
418 # despite opening commits for bookmarks/branches/tags, we always
420 # despite opening commits for bookmarks/branches/tags, we always
419 # convert this to rev to prevent changes after bookmark or branch change
421 # convert this to rev to prevent changes after bookmark or branch change
420 c.source_ref_type = 'rev'
422 c.source_ref_type = 'rev'
421 c.source_ref = source_ref_id
423 c.source_ref = source_ref_id
422
424
423 c.target_ref_type = 'rev'
425 c.target_ref_type = 'rev'
424 c.target_ref = target_ref_id
426 c.target_ref = target_ref_id
425
427
426 c.source_repo = source_repo
428 c.source_repo = source_repo
427 c.target_repo = target_repo
429 c.target_repo = target_repo
428
430
429 c.commit_ranges = []
431 c.commit_ranges = []
430 source_commit = EmptyCommit()
432 source_commit = EmptyCommit()
431 target_commit = EmptyCommit()
433 target_commit = EmptyCommit()
432 c.missing_requirements = False
434 c.missing_requirements = False
433
435
434 source_scm = source_repo.scm_instance()
436 source_scm = source_repo.scm_instance()
435 target_scm = target_repo.scm_instance()
437 target_scm = target_repo.scm_instance()
436
438
437 shadow_scm = None
439 shadow_scm = None
438 try:
440 try:
439 shadow_scm = pull_request_latest.get_shadow_repo()
441 shadow_scm = pull_request_latest.get_shadow_repo()
440 except Exception:
442 except Exception:
441 log.debug('Failed to get shadow repo', exc_info=True)
443 log.debug('Failed to get shadow repo', exc_info=True)
442 # try first the existing source_repo, and then shadow
444 # try first the existing source_repo, and then shadow
443 # repo if we can obtain one
445 # repo if we can obtain one
444 commits_source_repo = source_scm or shadow_scm
446 commits_source_repo = source_scm or shadow_scm
445
447
446 c.commits_source_repo = commits_source_repo
448 c.commits_source_repo = commits_source_repo
447 c.ancestor = None # set it to None, to hide it from PR view
449 c.ancestor = None # set it to None, to hide it from PR view
448
450
449 # empty version means latest, so we keep this to prevent
451 # empty version means latest, so we keep this to prevent
450 # double caching
452 # double caching
451 version_normalized = version or 'latest'
453 version_normalized = version or 'latest'
452 from_version_normalized = from_version or 'latest'
454 from_version_normalized = from_version or 'latest'
453
455
454 cache_path = self.rhodecode_vcs_repo.get_create_shadow_cache_pr_path(
456 cache_path = self.rhodecode_vcs_repo.get_create_shadow_cache_pr_path(
455 target_repo)
457 target_repo)
456 cache_file_path = diff_cache_exist(
458 cache_file_path = diff_cache_exist(
457 cache_path, 'pull_request', pull_request_id, version_normalized,
459 cache_path, 'pull_request', pull_request_id, version_normalized,
458 from_version_normalized, source_ref_id, target_ref_id, c.fulldiff)
460 from_version_normalized, source_ref_id, target_ref_id, c.fulldiff)
459
461
460 caching_enabled = self._is_diff_cache_enabled(c.target_repo)
462 caching_enabled = self._is_diff_cache_enabled(c.target_repo)
461 force_recache = str2bool(self.request.GET.get('force_recache'))
463 force_recache = str2bool(self.request.GET.get('force_recache'))
462
464
463 cached_diff = None
465 cached_diff = None
464 if caching_enabled:
466 if caching_enabled:
465 cached_diff = load_cached_diff(cache_file_path)
467 cached_diff = load_cached_diff(cache_file_path)
466
468
467 has_proper_commit_cache = (
469 has_proper_commit_cache = (
468 cached_diff and cached_diff.get('commits')
470 cached_diff and cached_diff.get('commits')
469 and len(cached_diff.get('commits', [])) == 5
471 and len(cached_diff.get('commits', [])) == 5
470 and cached_diff.get('commits')[0]
472 and cached_diff.get('commits')[0]
471 and cached_diff.get('commits')[3])
473 and cached_diff.get('commits')[3])
472 if not force_recache and has_proper_commit_cache:
474 if not force_recache and has_proper_commit_cache:
473 diff_commit_cache = \
475 diff_commit_cache = \
474 (ancestor_commit, commit_cache, missing_requirements,
476 (ancestor_commit, commit_cache, missing_requirements,
475 source_commit, target_commit) = cached_diff['commits']
477 source_commit, target_commit) = cached_diff['commits']
476 else:
478 else:
477 diff_commit_cache = \
479 diff_commit_cache = \
478 (ancestor_commit, commit_cache, missing_requirements,
480 (ancestor_commit, commit_cache, missing_requirements,
479 source_commit, target_commit) = self.get_commits(
481 source_commit, target_commit) = self.get_commits(
480 commits_source_repo,
482 commits_source_repo,
481 pull_request_at_ver,
483 pull_request_at_ver,
482 source_commit,
484 source_commit,
483 source_ref_id,
485 source_ref_id,
484 source_scm,
486 source_scm,
485 target_commit,
487 target_commit,
486 target_ref_id,
488 target_ref_id,
487 target_scm)
489 target_scm)
488
490
489 # register our commit range
491 # register our commit range
490 for comm in commit_cache.values():
492 for comm in commit_cache.values():
491 c.commit_ranges.append(comm)
493 c.commit_ranges.append(comm)
492
494
493 c.missing_requirements = missing_requirements
495 c.missing_requirements = missing_requirements
494 c.ancestor_commit = ancestor_commit
496 c.ancestor_commit = ancestor_commit
495 c.statuses = source_repo.statuses(
497 c.statuses = source_repo.statuses(
496 [x.raw_id for x in c.commit_ranges])
498 [x.raw_id for x in c.commit_ranges])
497
499
498 # auto collapse if we have more than limit
500 # auto collapse if we have more than limit
499 collapse_limit = diffs.DiffProcessor._collapse_commits_over
501 collapse_limit = diffs.DiffProcessor._collapse_commits_over
500 c.collapse_all_commits = len(c.commit_ranges) > collapse_limit
502 c.collapse_all_commits = len(c.commit_ranges) > collapse_limit
501 c.compare_mode = compare
503 c.compare_mode = compare
502
504
503 # diff_limit is the old behavior, will cut off the whole diff
505 # diff_limit is the old behavior, will cut off the whole diff
504 # if the limit is applied otherwise will just hide the
506 # if the limit is applied otherwise will just hide the
505 # big files from the front-end
507 # big files from the front-end
506 diff_limit = c.visual.cut_off_limit_diff
508 diff_limit = c.visual.cut_off_limit_diff
507 file_limit = c.visual.cut_off_limit_file
509 file_limit = c.visual.cut_off_limit_file
508
510
509 c.missing_commits = False
511 c.missing_commits = False
510 if (c.missing_requirements
512 if (c.missing_requirements
511 or isinstance(source_commit, EmptyCommit)
513 or isinstance(source_commit, EmptyCommit)
512 or source_commit == target_commit):
514 or source_commit == target_commit):
513
515
514 c.missing_commits = True
516 c.missing_commits = True
515 else:
517 else:
516 c.inline_comments = display_inline_comments
518 c.inline_comments = display_inline_comments
517
519
518 has_proper_diff_cache = cached_diff and cached_diff.get('commits')
520 has_proper_diff_cache = cached_diff and cached_diff.get('commits')
519 if not force_recache and has_proper_diff_cache:
521 if not force_recache and has_proper_diff_cache:
520 c.diffset = cached_diff['diff']
522 c.diffset = cached_diff['diff']
521 (ancestor_commit, commit_cache, missing_requirements,
523 (ancestor_commit, commit_cache, missing_requirements,
522 source_commit, target_commit) = cached_diff['commits']
524 source_commit, target_commit) = cached_diff['commits']
523 else:
525 else:
524 c.diffset = self._get_diffset(
526 c.diffset = self._get_diffset(
525 c.source_repo.repo_name, commits_source_repo,
527 c.source_repo.repo_name, commits_source_repo,
526 source_ref_id, target_ref_id,
528 source_ref_id, target_ref_id,
527 target_commit, source_commit,
529 target_commit, source_commit,
528 diff_limit, file_limit, c.fulldiff)
530 diff_limit, file_limit, c.fulldiff)
529
531
530 # save cached diff
532 # save cached diff
531 if caching_enabled:
533 if caching_enabled:
532 cache_diff(cache_file_path, c.diffset, diff_commit_cache)
534 cache_diff(cache_file_path, c.diffset, diff_commit_cache)
533
535
534 c.limited_diff = c.diffset.limited_diff
536 c.limited_diff = c.diffset.limited_diff
535
537
536 # calculate removed files that are bound to comments
538 # calculate removed files that are bound to comments
537 comment_deleted_files = [
539 comment_deleted_files = [
538 fname for fname in display_inline_comments
540 fname for fname in display_inline_comments
539 if fname not in c.diffset.file_stats]
541 if fname not in c.diffset.file_stats]
540
542
541 c.deleted_files_comments = collections.defaultdict(dict)
543 c.deleted_files_comments = collections.defaultdict(dict)
542 for fname, per_line_comments in display_inline_comments.items():
544 for fname, per_line_comments in display_inline_comments.items():
543 if fname in comment_deleted_files:
545 if fname in comment_deleted_files:
544 c.deleted_files_comments[fname]['stats'] = 0
546 c.deleted_files_comments[fname]['stats'] = 0
545 c.deleted_files_comments[fname]['comments'] = list()
547 c.deleted_files_comments[fname]['comments'] = list()
546 for lno, comments in per_line_comments.items():
548 for lno, comments in per_line_comments.items():
547 c.deleted_files_comments[fname]['comments'].extend(
549 c.deleted_files_comments[fname]['comments'].extend(
548 comments)
550 comments)
549
551
550 # this is a hack to properly display links, when creating PR, the
552 # this is a hack to properly display links, when creating PR, the
551 # compare view and others uses different notation, and
553 # compare view and others uses different notation, and
552 # compare_commits.mako renders links based on the target_repo.
554 # compare_commits.mako renders links based on the target_repo.
553 # We need to swap that here to generate it properly on the html side
555 # We need to swap that here to generate it properly on the html side
554 c.target_repo = c.source_repo
556 c.target_repo = c.source_repo
555
557
556 c.commit_statuses = ChangesetStatus.STATUSES
558 c.commit_statuses = ChangesetStatus.STATUSES
557
559
558 c.show_version_changes = not pr_closed
560 c.show_version_changes = not pr_closed
559 if c.show_version_changes:
561 if c.show_version_changes:
560 cur_obj = pull_request_at_ver
562 cur_obj = pull_request_at_ver
561 prev_obj = prev_pull_request_at_ver
563 prev_obj = prev_pull_request_at_ver
562
564
563 old_commit_ids = prev_obj.revisions
565 old_commit_ids = prev_obj.revisions
564 new_commit_ids = cur_obj.revisions
566 new_commit_ids = cur_obj.revisions
565 commit_changes = PullRequestModel()._calculate_commit_id_changes(
567 commit_changes = PullRequestModel()._calculate_commit_id_changes(
566 old_commit_ids, new_commit_ids)
568 old_commit_ids, new_commit_ids)
567 c.commit_changes_summary = commit_changes
569 c.commit_changes_summary = commit_changes
568
570
569 # calculate the diff for commits between versions
571 # calculate the diff for commits between versions
570 c.commit_changes = []
572 c.commit_changes = []
571 mark = lambda cs, fw: list(
573 mark = lambda cs, fw: list(
572 h.itertools.izip_longest([], cs, fillvalue=fw))
574 h.itertools.izip_longest([], cs, fillvalue=fw))
573 for c_type, raw_id in mark(commit_changes.added, 'a') \
575 for c_type, raw_id in mark(commit_changes.added, 'a') \
574 + mark(commit_changes.removed, 'r') \
576 + mark(commit_changes.removed, 'r') \
575 + mark(commit_changes.common, 'c'):
577 + mark(commit_changes.common, 'c'):
576
578
577 if raw_id in commit_cache:
579 if raw_id in commit_cache:
578 commit = commit_cache[raw_id]
580 commit = commit_cache[raw_id]
579 else:
581 else:
580 try:
582 try:
581 commit = commits_source_repo.get_commit(raw_id)
583 commit = commits_source_repo.get_commit(raw_id)
582 except CommitDoesNotExistError:
584 except CommitDoesNotExistError:
583 # in case we fail extracting still use "dummy" commit
585 # in case we fail extracting still use "dummy" commit
584 # for display in commit diff
586 # for display in commit diff
585 commit = h.AttributeDict(
587 commit = h.AttributeDict(
586 {'raw_id': raw_id,
588 {'raw_id': raw_id,
587 'message': 'EMPTY or MISSING COMMIT'})
589 'message': 'EMPTY or MISSING COMMIT'})
588 c.commit_changes.append([c_type, commit])
590 c.commit_changes.append([c_type, commit])
589
591
590 # current user review statuses for each version
592 # current user review statuses for each version
591 c.review_versions = {}
593 c.review_versions = {}
592 if self._rhodecode_user.user_id in allowed_reviewers:
594 if self._rhodecode_user.user_id in allowed_reviewers:
593 for co in general_comments:
595 for co in general_comments:
594 if co.author.user_id == self._rhodecode_user.user_id:
596 if co.author.user_id == self._rhodecode_user.user_id:
595 status = co.status_change
597 status = co.status_change
596 if status:
598 if status:
597 _ver_pr = status[0].comment.pull_request_version_id
599 _ver_pr = status[0].comment.pull_request_version_id
598 c.review_versions[_ver_pr] = status[0]
600 c.review_versions[_ver_pr] = status[0]
599
601
600 return self._get_template_context(c)
602 return self._get_template_context(c)
601
603
602 def get_commits(
604 def get_commits(
603 self, commits_source_repo, pull_request_at_ver, source_commit,
605 self, commits_source_repo, pull_request_at_ver, source_commit,
604 source_ref_id, source_scm, target_commit, target_ref_id, target_scm):
606 source_ref_id, source_scm, target_commit, target_ref_id, target_scm):
605 commit_cache = collections.OrderedDict()
607 commit_cache = collections.OrderedDict()
606 missing_requirements = False
608 missing_requirements = False
607 try:
609 try:
608 pre_load = ["author", "branch", "date", "message"]
610 pre_load = ["author", "branch", "date", "message"]
609 show_revs = pull_request_at_ver.revisions
611 show_revs = pull_request_at_ver.revisions
610 for rev in show_revs:
612 for rev in show_revs:
611 comm = commits_source_repo.get_commit(
613 comm = commits_source_repo.get_commit(
612 commit_id=rev, pre_load=pre_load)
614 commit_id=rev, pre_load=pre_load)
613 commit_cache[comm.raw_id] = comm
615 commit_cache[comm.raw_id] = comm
614
616
615 # Order here matters, we first need to get target, and then
617 # Order here matters, we first need to get target, and then
616 # the source
618 # the source
617 target_commit = commits_source_repo.get_commit(
619 target_commit = commits_source_repo.get_commit(
618 commit_id=safe_str(target_ref_id))
620 commit_id=safe_str(target_ref_id))
619
621
620 source_commit = commits_source_repo.get_commit(
622 source_commit = commits_source_repo.get_commit(
621 commit_id=safe_str(source_ref_id))
623 commit_id=safe_str(source_ref_id))
622 except CommitDoesNotExistError:
624 except CommitDoesNotExistError:
623 log.warning(
625 log.warning(
624 'Failed to get commit from `{}` repo'.format(
626 'Failed to get commit from `{}` repo'.format(
625 commits_source_repo), exc_info=True)
627 commits_source_repo), exc_info=True)
626 except RepositoryRequirementError:
628 except RepositoryRequirementError:
627 log.warning(
629 log.warning(
628 'Failed to get all required data from repo', exc_info=True)
630 'Failed to get all required data from repo', exc_info=True)
629 missing_requirements = True
631 missing_requirements = True
630 ancestor_commit = None
632 ancestor_commit = None
631 try:
633 try:
632 ancestor_id = source_scm.get_common_ancestor(
634 ancestor_id = source_scm.get_common_ancestor(
633 source_commit.raw_id, target_commit.raw_id, target_scm)
635 source_commit.raw_id, target_commit.raw_id, target_scm)
634 ancestor_commit = source_scm.get_commit(ancestor_id)
636 ancestor_commit = source_scm.get_commit(ancestor_id)
635 except Exception:
637 except Exception:
636 ancestor_commit = None
638 ancestor_commit = None
637 return ancestor_commit, commit_cache, missing_requirements, source_commit, target_commit
639 return ancestor_commit, commit_cache, missing_requirements, source_commit, target_commit
638
640
639 def assure_not_empty_repo(self):
641 def assure_not_empty_repo(self):
640 _ = self.request.translate
642 _ = self.request.translate
641
643
642 try:
644 try:
643 self.db_repo.scm_instance().get_commit()
645 self.db_repo.scm_instance().get_commit()
644 except EmptyRepositoryError:
646 except EmptyRepositoryError:
645 h.flash(h.literal(_('There are no commits yet')),
647 h.flash(h.literal(_('There are no commits yet')),
646 category='warning')
648 category='warning')
647 raise HTTPFound(
649 raise HTTPFound(
648 h.route_path('repo_summary', repo_name=self.db_repo.repo_name))
650 h.route_path('repo_summary', repo_name=self.db_repo.repo_name))
649
651
650 @LoginRequired()
652 @LoginRequired()
651 @NotAnonymous()
653 @NotAnonymous()
652 @HasRepoPermissionAnyDecorator(
654 @HasRepoPermissionAnyDecorator(
653 'repository.read', 'repository.write', 'repository.admin')
655 'repository.read', 'repository.write', 'repository.admin')
654 @view_config(
656 @view_config(
655 route_name='pullrequest_new', request_method='GET',
657 route_name='pullrequest_new', request_method='GET',
656 renderer='rhodecode:templates/pullrequests/pullrequest.mako')
658 renderer='rhodecode:templates/pullrequests/pullrequest.mako')
657 def pull_request_new(self):
659 def pull_request_new(self):
658 _ = self.request.translate
660 _ = self.request.translate
659 c = self.load_default_context()
661 c = self.load_default_context()
660
662
661 self.assure_not_empty_repo()
663 self.assure_not_empty_repo()
662 source_repo = self.db_repo
664 source_repo = self.db_repo
663
665
664 commit_id = self.request.GET.get('commit')
666 commit_id = self.request.GET.get('commit')
665 branch_ref = self.request.GET.get('branch')
667 branch_ref = self.request.GET.get('branch')
666 bookmark_ref = self.request.GET.get('bookmark')
668 bookmark_ref = self.request.GET.get('bookmark')
667
669
668 try:
670 try:
669 source_repo_data = PullRequestModel().generate_repo_data(
671 source_repo_data = PullRequestModel().generate_repo_data(
670 source_repo, commit_id=commit_id,
672 source_repo, commit_id=commit_id,
671 branch=branch_ref, bookmark=bookmark_ref,
673 branch=branch_ref, bookmark=bookmark_ref,
672 translator=self.request.translate)
674 translator=self.request.translate)
673 except CommitDoesNotExistError as e:
675 except CommitDoesNotExistError as e:
674 log.exception(e)
676 log.exception(e)
675 h.flash(_('Commit does not exist'), 'error')
677 h.flash(_('Commit does not exist'), 'error')
676 raise HTTPFound(
678 raise HTTPFound(
677 h.route_path('pullrequest_new', repo_name=source_repo.repo_name))
679 h.route_path('pullrequest_new', repo_name=source_repo.repo_name))
678
680
679 default_target_repo = source_repo
681 default_target_repo = source_repo
680
682
681 if source_repo.parent:
683 if source_repo.parent:
682 parent_vcs_obj = source_repo.parent.scm_instance()
684 parent_vcs_obj = source_repo.parent.scm_instance()
683 if parent_vcs_obj and not parent_vcs_obj.is_empty():
685 if parent_vcs_obj and not parent_vcs_obj.is_empty():
684 # change default if we have a parent repo
686 # change default if we have a parent repo
685 default_target_repo = source_repo.parent
687 default_target_repo = source_repo.parent
686
688
687 target_repo_data = PullRequestModel().generate_repo_data(
689 target_repo_data = PullRequestModel().generate_repo_data(
688 default_target_repo, translator=self.request.translate)
690 default_target_repo, translator=self.request.translate)
689
691
690 selected_source_ref = source_repo_data['refs']['selected_ref']
692 selected_source_ref = source_repo_data['refs']['selected_ref']
691 title_source_ref = ''
693 title_source_ref = ''
692 if selected_source_ref:
694 if selected_source_ref:
693 title_source_ref = selected_source_ref.split(':', 2)[1]
695 title_source_ref = selected_source_ref.split(':', 2)[1]
694 c.default_title = PullRequestModel().generate_pullrequest_title(
696 c.default_title = PullRequestModel().generate_pullrequest_title(
695 source=source_repo.repo_name,
697 source=source_repo.repo_name,
696 source_ref=title_source_ref,
698 source_ref=title_source_ref,
697 target=default_target_repo.repo_name
699 target=default_target_repo.repo_name
698 )
700 )
699
701
700 c.default_repo_data = {
702 c.default_repo_data = {
701 'source_repo_name': source_repo.repo_name,
703 'source_repo_name': source_repo.repo_name,
702 'source_refs_json': json.dumps(source_repo_data),
704 'source_refs_json': json.dumps(source_repo_data),
703 'target_repo_name': default_target_repo.repo_name,
705 'target_repo_name': default_target_repo.repo_name,
704 'target_refs_json': json.dumps(target_repo_data),
706 'target_refs_json': json.dumps(target_repo_data),
705 }
707 }
706 c.default_source_ref = selected_source_ref
708 c.default_source_ref = selected_source_ref
707
709
708 return self._get_template_context(c)
710 return self._get_template_context(c)
709
711
710 @LoginRequired()
712 @LoginRequired()
711 @NotAnonymous()
713 @NotAnonymous()
712 @HasRepoPermissionAnyDecorator(
714 @HasRepoPermissionAnyDecorator(
713 'repository.read', 'repository.write', 'repository.admin')
715 'repository.read', 'repository.write', 'repository.admin')
714 @view_config(
716 @view_config(
715 route_name='pullrequest_repo_refs', request_method='GET',
717 route_name='pullrequest_repo_refs', request_method='GET',
716 renderer='json_ext', xhr=True)
718 renderer='json_ext', xhr=True)
717 def pull_request_repo_refs(self):
719 def pull_request_repo_refs(self):
718 self.load_default_context()
720 self.load_default_context()
719 target_repo_name = self.request.matchdict['target_repo_name']
721 target_repo_name = self.request.matchdict['target_repo_name']
720 repo = Repository.get_by_repo_name(target_repo_name)
722 repo = Repository.get_by_repo_name(target_repo_name)
721 if not repo:
723 if not repo:
722 raise HTTPNotFound()
724 raise HTTPNotFound()
723
725
724 target_perm = HasRepoPermissionAny(
726 target_perm = HasRepoPermissionAny(
725 'repository.read', 'repository.write', 'repository.admin')(
727 'repository.read', 'repository.write', 'repository.admin')(
726 target_repo_name)
728 target_repo_name)
727 if not target_perm:
729 if not target_perm:
728 raise HTTPNotFound()
730 raise HTTPNotFound()
729
731
730 return PullRequestModel().generate_repo_data(
732 return PullRequestModel().generate_repo_data(
731 repo, translator=self.request.translate)
733 repo, translator=self.request.translate)
732
734
733 @LoginRequired()
735 @LoginRequired()
734 @NotAnonymous()
736 @NotAnonymous()
735 @HasRepoPermissionAnyDecorator(
737 @HasRepoPermissionAnyDecorator(
736 'repository.read', 'repository.write', 'repository.admin')
738 'repository.read', 'repository.write', 'repository.admin')
737 @view_config(
739 @view_config(
738 route_name='pullrequest_repo_destinations', request_method='GET',
740 route_name='pullrequest_repo_destinations', request_method='GET',
739 renderer='json_ext', xhr=True)
741 renderer='json_ext', xhr=True)
740 def pull_request_repo_destinations(self):
742 def pull_request_repo_destinations(self):
741 _ = self.request.translate
743 _ = self.request.translate
742 filter_query = self.request.GET.get('query')
744 filter_query = self.request.GET.get('query')
743
745
744 query = Repository.query() \
746 query = Repository.query() \
745 .order_by(func.length(Repository.repo_name)) \
747 .order_by(func.length(Repository.repo_name)) \
746 .filter(
748 .filter(
747 or_(Repository.repo_name == self.db_repo.repo_name,
749 or_(Repository.repo_name == self.db_repo.repo_name,
748 Repository.fork_id == self.db_repo.repo_id))
750 Repository.fork_id == self.db_repo.repo_id))
749
751
750 if filter_query:
752 if filter_query:
751 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
753 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
752 query = query.filter(
754 query = query.filter(
753 Repository.repo_name.ilike(ilike_expression))
755 Repository.repo_name.ilike(ilike_expression))
754
756
755 add_parent = False
757 add_parent = False
756 if self.db_repo.parent:
758 if self.db_repo.parent:
757 if filter_query in self.db_repo.parent.repo_name:
759 if filter_query in self.db_repo.parent.repo_name:
758 parent_vcs_obj = self.db_repo.parent.scm_instance()
760 parent_vcs_obj = self.db_repo.parent.scm_instance()
759 if parent_vcs_obj and not parent_vcs_obj.is_empty():
761 if parent_vcs_obj and not parent_vcs_obj.is_empty():
760 add_parent = True
762 add_parent = True
761
763
762 limit = 20 - 1 if add_parent else 20
764 limit = 20 - 1 if add_parent else 20
763 all_repos = query.limit(limit).all()
765 all_repos = query.limit(limit).all()
764 if add_parent:
766 if add_parent:
765 all_repos += [self.db_repo.parent]
767 all_repos += [self.db_repo.parent]
766
768
767 repos = []
769 repos = []
768 for obj in ScmModel().get_repos(all_repos):
770 for obj in ScmModel().get_repos(all_repos):
769 repos.append({
771 repos.append({
770 'id': obj['name'],
772 'id': obj['name'],
771 'text': obj['name'],
773 'text': obj['name'],
772 'type': 'repo',
774 'type': 'repo',
773 'repo_id': obj['dbrepo']['repo_id'],
775 'repo_id': obj['dbrepo']['repo_id'],
774 'repo_type': obj['dbrepo']['repo_type'],
776 'repo_type': obj['dbrepo']['repo_type'],
775 'private': obj['dbrepo']['private'],
777 'private': obj['dbrepo']['private'],
776
778
777 })
779 })
778
780
779 data = {
781 data = {
780 'more': False,
782 'more': False,
781 'results': [{
783 'results': [{
782 'text': _('Repositories'),
784 'text': _('Repositories'),
783 'children': repos
785 'children': repos
784 }] if repos else []
786 }] if repos else []
785 }
787 }
786 return data
788 return data
787
789
788 @LoginRequired()
790 @LoginRequired()
789 @NotAnonymous()
791 @NotAnonymous()
790 @HasRepoPermissionAnyDecorator(
792 @HasRepoPermissionAnyDecorator(
791 'repository.read', 'repository.write', 'repository.admin')
793 'repository.read', 'repository.write', 'repository.admin')
792 @CSRFRequired()
794 @CSRFRequired()
793 @view_config(
795 @view_config(
794 route_name='pullrequest_create', request_method='POST',
796 route_name='pullrequest_create', request_method='POST',
795 renderer=None)
797 renderer=None)
796 def pull_request_create(self):
798 def pull_request_create(self):
797 _ = self.request.translate
799 _ = self.request.translate
798 self.assure_not_empty_repo()
800 self.assure_not_empty_repo()
799 self.load_default_context()
801 self.load_default_context()
800
802
801 controls = peppercorn.parse(self.request.POST.items())
803 controls = peppercorn.parse(self.request.POST.items())
802
804
803 try:
805 try:
804 form = PullRequestForm(
806 form = PullRequestForm(
805 self.request.translate, self.db_repo.repo_id)()
807 self.request.translate, self.db_repo.repo_id)()
806 _form = form.to_python(controls)
808 _form = form.to_python(controls)
807 except formencode.Invalid as errors:
809 except formencode.Invalid as errors:
808 if errors.error_dict.get('revisions'):
810 if errors.error_dict.get('revisions'):
809 msg = 'Revisions: %s' % errors.error_dict['revisions']
811 msg = 'Revisions: %s' % errors.error_dict['revisions']
810 elif errors.error_dict.get('pullrequest_title'):
812 elif errors.error_dict.get('pullrequest_title'):
811 msg = errors.error_dict.get('pullrequest_title')
813 msg = errors.error_dict.get('pullrequest_title')
812 else:
814 else:
813 msg = _('Error creating pull request: {}').format(errors)
815 msg = _('Error creating pull request: {}').format(errors)
814 log.exception(msg)
816 log.exception(msg)
815 h.flash(msg, 'error')
817 h.flash(msg, 'error')
816
818
817 # would rather just go back to form ...
819 # would rather just go back to form ...
818 raise HTTPFound(
820 raise HTTPFound(
819 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
821 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
820
822
821 source_repo = _form['source_repo']
823 source_repo = _form['source_repo']
822 source_ref = _form['source_ref']
824 source_ref = _form['source_ref']
823 target_repo = _form['target_repo']
825 target_repo = _form['target_repo']
824 target_ref = _form['target_ref']
826 target_ref = _form['target_ref']
825 commit_ids = _form['revisions'][::-1]
827 commit_ids = _form['revisions'][::-1]
826
828
827 # find the ancestor for this pr
829 # find the ancestor for this pr
828 source_db_repo = Repository.get_by_repo_name(_form['source_repo'])
830 source_db_repo = Repository.get_by_repo_name(_form['source_repo'])
829 target_db_repo = Repository.get_by_repo_name(_form['target_repo'])
831 target_db_repo = Repository.get_by_repo_name(_form['target_repo'])
830
832
831 # re-check permissions again here
833 # re-check permissions again here
832 # source_repo we must have read permissions
834 # source_repo we must have read permissions
833
835
834 source_perm = HasRepoPermissionAny(
836 source_perm = HasRepoPermissionAny(
835 'repository.read',
837 'repository.read',
836 'repository.write', 'repository.admin')(source_db_repo.repo_name)
838 'repository.write', 'repository.admin')(source_db_repo.repo_name)
837 if not source_perm:
839 if not source_perm:
838 msg = _('Not Enough permissions to source repo `{}`.'.format(
840 msg = _('Not Enough permissions to source repo `{}`.'.format(
839 source_db_repo.repo_name))
841 source_db_repo.repo_name))
840 h.flash(msg, category='error')
842 h.flash(msg, category='error')
841 # copy the args back to redirect
843 # copy the args back to redirect
842 org_query = self.request.GET.mixed()
844 org_query = self.request.GET.mixed()
843 raise HTTPFound(
845 raise HTTPFound(
844 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
846 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
845 _query=org_query))
847 _query=org_query))
846
848
847 # target repo we must have read permissions, and also later on
849 # target repo we must have read permissions, and also later on
848 # we want to check branch permissions here
850 # we want to check branch permissions here
849 target_perm = HasRepoPermissionAny(
851 target_perm = HasRepoPermissionAny(
850 'repository.read',
852 'repository.read',
851 'repository.write', 'repository.admin')(target_db_repo.repo_name)
853 'repository.write', 'repository.admin')(target_db_repo.repo_name)
852 if not target_perm:
854 if not target_perm:
853 msg = _('Not Enough permissions to target repo `{}`.'.format(
855 msg = _('Not Enough permissions to target repo `{}`.'.format(
854 target_db_repo.repo_name))
856 target_db_repo.repo_name))
855 h.flash(msg, category='error')
857 h.flash(msg, category='error')
856 # copy the args back to redirect
858 # copy the args back to redirect
857 org_query = self.request.GET.mixed()
859 org_query = self.request.GET.mixed()
858 raise HTTPFound(
860 raise HTTPFound(
859 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
861 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
860 _query=org_query))
862 _query=org_query))
861
863
862 source_scm = source_db_repo.scm_instance()
864 source_scm = source_db_repo.scm_instance()
863 target_scm = target_db_repo.scm_instance()
865 target_scm = target_db_repo.scm_instance()
864
866
865 source_commit = source_scm.get_commit(source_ref.split(':')[-1])
867 source_commit = source_scm.get_commit(source_ref.split(':')[-1])
866 target_commit = target_scm.get_commit(target_ref.split(':')[-1])
868 target_commit = target_scm.get_commit(target_ref.split(':')[-1])
867
869
868 ancestor = source_scm.get_common_ancestor(
870 ancestor = source_scm.get_common_ancestor(
869 source_commit.raw_id, target_commit.raw_id, target_scm)
871 source_commit.raw_id, target_commit.raw_id, target_scm)
870
872
871 # recalculate target ref based on ancestor
873 # recalculate target ref based on ancestor
872 target_ref_type, target_ref_name, __ = _form['target_ref'].split(':')
874 target_ref_type, target_ref_name, __ = _form['target_ref'].split(':')
873 target_ref = ':'.join((target_ref_type, target_ref_name, ancestor))
875 target_ref = ':'.join((target_ref_type, target_ref_name, ancestor))
874
876
875 get_default_reviewers_data, validate_default_reviewers = \
877 get_default_reviewers_data, validate_default_reviewers = \
876 PullRequestModel().get_reviewer_functions()
878 PullRequestModel().get_reviewer_functions()
877
879
878 # recalculate reviewers logic, to make sure we can validate this
880 # recalculate reviewers logic, to make sure we can validate this
879 reviewer_rules = get_default_reviewers_data(
881 reviewer_rules = get_default_reviewers_data(
880 self._rhodecode_db_user, source_db_repo,
882 self._rhodecode_db_user, source_db_repo,
881 source_commit, target_db_repo, target_commit)
883 source_commit, target_db_repo, target_commit)
882
884
883 given_reviewers = _form['review_members']
885 given_reviewers = _form['review_members']
884 reviewers = validate_default_reviewers(
886 reviewers = validate_default_reviewers(
885 given_reviewers, reviewer_rules)
887 given_reviewers, reviewer_rules)
886
888
887 pullrequest_title = _form['pullrequest_title']
889 pullrequest_title = _form['pullrequest_title']
888 title_source_ref = source_ref.split(':', 2)[1]
890 title_source_ref = source_ref.split(':', 2)[1]
889 if not pullrequest_title:
891 if not pullrequest_title:
890 pullrequest_title = PullRequestModel().generate_pullrequest_title(
892 pullrequest_title = PullRequestModel().generate_pullrequest_title(
891 source=source_repo,
893 source=source_repo,
892 source_ref=title_source_ref,
894 source_ref=title_source_ref,
893 target=target_repo
895 target=target_repo
894 )
896 )
895
897
896 description = _form['pullrequest_desc']
898 description = _form['pullrequest_desc']
899 description_renderer = _form['description_renderer']
897
900
898 try:
901 try:
899 pull_request = PullRequestModel().create(
902 pull_request = PullRequestModel().create(
900 created_by=self._rhodecode_user.user_id,
903 created_by=self._rhodecode_user.user_id,
901 source_repo=source_repo,
904 source_repo=source_repo,
902 source_ref=source_ref,
905 source_ref=source_ref,
903 target_repo=target_repo,
906 target_repo=target_repo,
904 target_ref=target_ref,
907 target_ref=target_ref,
905 revisions=commit_ids,
908 revisions=commit_ids,
906 reviewers=reviewers,
909 reviewers=reviewers,
907 title=pullrequest_title,
910 title=pullrequest_title,
908 description=description,
911 description=description,
912 description_renderer=description_renderer,
909 reviewer_data=reviewer_rules,
913 reviewer_data=reviewer_rules,
910 auth_user=self._rhodecode_user
914 auth_user=self._rhodecode_user
911 )
915 )
912 Session().commit()
916 Session().commit()
913
917
914 h.flash(_('Successfully opened new pull request'),
918 h.flash(_('Successfully opened new pull request'),
915 category='success')
919 category='success')
916 except Exception:
920 except Exception:
917 msg = _('Error occurred during creation of this pull request.')
921 msg = _('Error occurred during creation of this pull request.')
918 log.exception(msg)
922 log.exception(msg)
919 h.flash(msg, category='error')
923 h.flash(msg, category='error')
920
924
921 # copy the args back to redirect
925 # copy the args back to redirect
922 org_query = self.request.GET.mixed()
926 org_query = self.request.GET.mixed()
923 raise HTTPFound(
927 raise HTTPFound(
924 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
928 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
925 _query=org_query))
929 _query=org_query))
926
930
927 raise HTTPFound(
931 raise HTTPFound(
928 h.route_path('pullrequest_show', repo_name=target_repo,
932 h.route_path('pullrequest_show', repo_name=target_repo,
929 pull_request_id=pull_request.pull_request_id))
933 pull_request_id=pull_request.pull_request_id))
930
934
931 @LoginRequired()
935 @LoginRequired()
932 @NotAnonymous()
936 @NotAnonymous()
933 @HasRepoPermissionAnyDecorator(
937 @HasRepoPermissionAnyDecorator(
934 'repository.read', 'repository.write', 'repository.admin')
938 'repository.read', 'repository.write', 'repository.admin')
935 @CSRFRequired()
939 @CSRFRequired()
936 @view_config(
940 @view_config(
937 route_name='pullrequest_update', request_method='POST',
941 route_name='pullrequest_update', request_method='POST',
938 renderer='json_ext')
942 renderer='json_ext')
939 def pull_request_update(self):
943 def pull_request_update(self):
940 pull_request = PullRequest.get_or_404(
944 pull_request = PullRequest.get_or_404(
941 self.request.matchdict['pull_request_id'])
945 self.request.matchdict['pull_request_id'])
942 _ = self.request.translate
946 _ = self.request.translate
943
947
944 self.load_default_context()
948 self.load_default_context()
945
949
946 if pull_request.is_closed():
950 if pull_request.is_closed():
947 log.debug('update: forbidden because pull request is closed')
951 log.debug('update: forbidden because pull request is closed')
948 msg = _(u'Cannot update closed pull requests.')
952 msg = _(u'Cannot update closed pull requests.')
949 h.flash(msg, category='error')
953 h.flash(msg, category='error')
950 return True
954 return True
951
955
952 # only owner or admin can update it
956 # only owner or admin can update it
953 allowed_to_update = PullRequestModel().check_user_update(
957 allowed_to_update = PullRequestModel().check_user_update(
954 pull_request, self._rhodecode_user)
958 pull_request, self._rhodecode_user)
955 if allowed_to_update:
959 if allowed_to_update:
956 controls = peppercorn.parse(self.request.POST.items())
960 controls = peppercorn.parse(self.request.POST.items())
957
961
958 if 'review_members' in controls:
962 if 'review_members' in controls:
959 self._update_reviewers(
963 self._update_reviewers(
960 pull_request, controls['review_members'],
964 pull_request, controls['review_members'],
961 pull_request.reviewer_data)
965 pull_request.reviewer_data)
962 elif str2bool(self.request.POST.get('update_commits', 'false')):
966 elif str2bool(self.request.POST.get('update_commits', 'false')):
963 self._update_commits(pull_request)
967 self._update_commits(pull_request)
964 elif str2bool(self.request.POST.get('edit_pull_request', 'false')):
968 elif str2bool(self.request.POST.get('edit_pull_request', 'false')):
965 self._edit_pull_request(pull_request)
969 self._edit_pull_request(pull_request)
966 else:
970 else:
967 raise HTTPBadRequest()
971 raise HTTPBadRequest()
968 return True
972 return True
969 raise HTTPForbidden()
973 raise HTTPForbidden()
970
974
971 def _edit_pull_request(self, pull_request):
975 def _edit_pull_request(self, pull_request):
972 _ = self.request.translate
976 _ = self.request.translate
977
973 try:
978 try:
974 PullRequestModel().edit(
979 PullRequestModel().edit(
975 pull_request, self.request.POST.get('title'),
980 pull_request,
976 self.request.POST.get('description'), self._rhodecode_user)
981 self.request.POST.get('title'),
982 self.request.POST.get('description'),
983 self.request.POST.get('description_renderer'),
984 self._rhodecode_user)
977 except ValueError:
985 except ValueError:
978 msg = _(u'Cannot update closed pull requests.')
986 msg = _(u'Cannot update closed pull requests.')
979 h.flash(msg, category='error')
987 h.flash(msg, category='error')
980 return
988 return
981 else:
989 else:
982 Session().commit()
990 Session().commit()
983
991
984 msg = _(u'Pull request title & description updated.')
992 msg = _(u'Pull request title & description updated.')
985 h.flash(msg, category='success')
993 h.flash(msg, category='success')
986 return
994 return
987
995
988 def _update_commits(self, pull_request):
996 def _update_commits(self, pull_request):
989 _ = self.request.translate
997 _ = self.request.translate
990 resp = PullRequestModel().update_commits(pull_request)
998 resp = PullRequestModel().update_commits(pull_request)
991
999
992 if resp.executed:
1000 if resp.executed:
993
1001
994 if resp.target_changed and resp.source_changed:
1002 if resp.target_changed and resp.source_changed:
995 changed = 'target and source repositories'
1003 changed = 'target and source repositories'
996 elif resp.target_changed and not resp.source_changed:
1004 elif resp.target_changed and not resp.source_changed:
997 changed = 'target repository'
1005 changed = 'target repository'
998 elif not resp.target_changed and resp.source_changed:
1006 elif not resp.target_changed and resp.source_changed:
999 changed = 'source repository'
1007 changed = 'source repository'
1000 else:
1008 else:
1001 changed = 'nothing'
1009 changed = 'nothing'
1002
1010
1003 msg = _(
1011 msg = _(
1004 u'Pull request updated to "{source_commit_id}" with '
1012 u'Pull request updated to "{source_commit_id}" with '
1005 u'{count_added} added, {count_removed} removed commits. '
1013 u'{count_added} added, {count_removed} removed commits. '
1006 u'Source of changes: {change_source}')
1014 u'Source of changes: {change_source}')
1007 msg = msg.format(
1015 msg = msg.format(
1008 source_commit_id=pull_request.source_ref_parts.commit_id,
1016 source_commit_id=pull_request.source_ref_parts.commit_id,
1009 count_added=len(resp.changes.added),
1017 count_added=len(resp.changes.added),
1010 count_removed=len(resp.changes.removed),
1018 count_removed=len(resp.changes.removed),
1011 change_source=changed)
1019 change_source=changed)
1012 h.flash(msg, category='success')
1020 h.flash(msg, category='success')
1013
1021
1014 channel = '/repo${}$/pr/{}'.format(
1022 channel = '/repo${}$/pr/{}'.format(
1015 pull_request.target_repo.repo_name,
1023 pull_request.target_repo.repo_name,
1016 pull_request.pull_request_id)
1024 pull_request.pull_request_id)
1017 message = msg + (
1025 message = msg + (
1018 ' - <a onclick="window.location.reload()">'
1026 ' - <a onclick="window.location.reload()">'
1019 '<strong>{}</strong></a>'.format(_('Reload page')))
1027 '<strong>{}</strong></a>'.format(_('Reload page')))
1020 channelstream.post_message(
1028 channelstream.post_message(
1021 channel, message, self._rhodecode_user.username,
1029 channel, message, self._rhodecode_user.username,
1022 registry=self.request.registry)
1030 registry=self.request.registry)
1023 else:
1031 else:
1024 msg = PullRequestModel.UPDATE_STATUS_MESSAGES[resp.reason]
1032 msg = PullRequestModel.UPDATE_STATUS_MESSAGES[resp.reason]
1025 warning_reasons = [
1033 warning_reasons = [
1026 UpdateFailureReason.NO_CHANGE,
1034 UpdateFailureReason.NO_CHANGE,
1027 UpdateFailureReason.WRONG_REF_TYPE,
1035 UpdateFailureReason.WRONG_REF_TYPE,
1028 ]
1036 ]
1029 category = 'warning' if resp.reason in warning_reasons else 'error'
1037 category = 'warning' if resp.reason in warning_reasons else 'error'
1030 h.flash(msg, category=category)
1038 h.flash(msg, category=category)
1031
1039
1032 @LoginRequired()
1040 @LoginRequired()
1033 @NotAnonymous()
1041 @NotAnonymous()
1034 @HasRepoPermissionAnyDecorator(
1042 @HasRepoPermissionAnyDecorator(
1035 'repository.read', 'repository.write', 'repository.admin')
1043 'repository.read', 'repository.write', 'repository.admin')
1036 @CSRFRequired()
1044 @CSRFRequired()
1037 @view_config(
1045 @view_config(
1038 route_name='pullrequest_merge', request_method='POST',
1046 route_name='pullrequest_merge', request_method='POST',
1039 renderer='json_ext')
1047 renderer='json_ext')
1040 def pull_request_merge(self):
1048 def pull_request_merge(self):
1041 """
1049 """
1042 Merge will perform a server-side merge of the specified
1050 Merge will perform a server-side merge of the specified
1043 pull request, if the pull request is approved and mergeable.
1051 pull request, if the pull request is approved and mergeable.
1044 After successful merging, the pull request is automatically
1052 After successful merging, the pull request is automatically
1045 closed, with a relevant comment.
1053 closed, with a relevant comment.
1046 """
1054 """
1047 pull_request = PullRequest.get_or_404(
1055 pull_request = PullRequest.get_or_404(
1048 self.request.matchdict['pull_request_id'])
1056 self.request.matchdict['pull_request_id'])
1049
1057
1050 self.load_default_context()
1058 self.load_default_context()
1051 check = MergeCheck.validate(pull_request, self._rhodecode_db_user,
1059 check = MergeCheck.validate(pull_request, self._rhodecode_db_user,
1052 translator=self.request.translate)
1060 translator=self.request.translate)
1053 merge_possible = not check.failed
1061 merge_possible = not check.failed
1054
1062
1055 for err_type, error_msg in check.errors:
1063 for err_type, error_msg in check.errors:
1056 h.flash(error_msg, category=err_type)
1064 h.flash(error_msg, category=err_type)
1057
1065
1058 if merge_possible:
1066 if merge_possible:
1059 log.debug("Pre-conditions checked, trying to merge.")
1067 log.debug("Pre-conditions checked, trying to merge.")
1060 extras = vcs_operation_context(
1068 extras = vcs_operation_context(
1061 self.request.environ, repo_name=pull_request.target_repo.repo_name,
1069 self.request.environ, repo_name=pull_request.target_repo.repo_name,
1062 username=self._rhodecode_db_user.username, action='push',
1070 username=self._rhodecode_db_user.username, action='push',
1063 scm=pull_request.target_repo.repo_type)
1071 scm=pull_request.target_repo.repo_type)
1064 self._merge_pull_request(
1072 self._merge_pull_request(
1065 pull_request, self._rhodecode_db_user, extras)
1073 pull_request, self._rhodecode_db_user, extras)
1066 else:
1074 else:
1067 log.debug("Pre-conditions failed, NOT merging.")
1075 log.debug("Pre-conditions failed, NOT merging.")
1068
1076
1069 raise HTTPFound(
1077 raise HTTPFound(
1070 h.route_path('pullrequest_show',
1078 h.route_path('pullrequest_show',
1071 repo_name=pull_request.target_repo.repo_name,
1079 repo_name=pull_request.target_repo.repo_name,
1072 pull_request_id=pull_request.pull_request_id))
1080 pull_request_id=pull_request.pull_request_id))
1073
1081
1074 def _merge_pull_request(self, pull_request, user, extras):
1082 def _merge_pull_request(self, pull_request, user, extras):
1075 _ = self.request.translate
1083 _ = self.request.translate
1076 merge_resp = PullRequestModel().merge_repo(pull_request, user, extras=extras)
1084 merge_resp = PullRequestModel().merge_repo(pull_request, user, extras=extras)
1077
1085
1078 if merge_resp.executed:
1086 if merge_resp.executed:
1079 log.debug("The merge was successful, closing the pull request.")
1087 log.debug("The merge was successful, closing the pull request.")
1080 PullRequestModel().close_pull_request(
1088 PullRequestModel().close_pull_request(
1081 pull_request.pull_request_id, user)
1089 pull_request.pull_request_id, user)
1082 Session().commit()
1090 Session().commit()
1083 msg = _('Pull request was successfully merged and closed.')
1091 msg = _('Pull request was successfully merged and closed.')
1084 h.flash(msg, category='success')
1092 h.flash(msg, category='success')
1085 else:
1093 else:
1086 log.debug(
1094 log.debug(
1087 "The merge was not successful. Merge response: %s",
1095 "The merge was not successful. Merge response: %s",
1088 merge_resp)
1096 merge_resp)
1089 msg = PullRequestModel().merge_status_message(
1097 msg = PullRequestModel().merge_status_message(
1090 merge_resp.failure_reason)
1098 merge_resp.failure_reason)
1091 h.flash(msg, category='error')
1099 h.flash(msg, category='error')
1092
1100
1093 def _update_reviewers(self, pull_request, review_members, reviewer_rules):
1101 def _update_reviewers(self, pull_request, review_members, reviewer_rules):
1094 _ = self.request.translate
1102 _ = self.request.translate
1095 get_default_reviewers_data, validate_default_reviewers = \
1103 get_default_reviewers_data, validate_default_reviewers = \
1096 PullRequestModel().get_reviewer_functions()
1104 PullRequestModel().get_reviewer_functions()
1097
1105
1098 try:
1106 try:
1099 reviewers = validate_default_reviewers(review_members, reviewer_rules)
1107 reviewers = validate_default_reviewers(review_members, reviewer_rules)
1100 except ValueError as e:
1108 except ValueError as e:
1101 log.error('Reviewers Validation: {}'.format(e))
1109 log.error('Reviewers Validation: {}'.format(e))
1102 h.flash(e, category='error')
1110 h.flash(e, category='error')
1103 return
1111 return
1104
1112
1105 PullRequestModel().update_reviewers(
1113 PullRequestModel().update_reviewers(
1106 pull_request, reviewers, self._rhodecode_user)
1114 pull_request, reviewers, self._rhodecode_user)
1107 h.flash(_('Pull request reviewers updated.'), category='success')
1115 h.flash(_('Pull request reviewers updated.'), category='success')
1108 Session().commit()
1116 Session().commit()
1109
1117
1110 @LoginRequired()
1118 @LoginRequired()
1111 @NotAnonymous()
1119 @NotAnonymous()
1112 @HasRepoPermissionAnyDecorator(
1120 @HasRepoPermissionAnyDecorator(
1113 'repository.read', 'repository.write', 'repository.admin')
1121 'repository.read', 'repository.write', 'repository.admin')
1114 @CSRFRequired()
1122 @CSRFRequired()
1115 @view_config(
1123 @view_config(
1116 route_name='pullrequest_delete', request_method='POST',
1124 route_name='pullrequest_delete', request_method='POST',
1117 renderer='json_ext')
1125 renderer='json_ext')
1118 def pull_request_delete(self):
1126 def pull_request_delete(self):
1119 _ = self.request.translate
1127 _ = self.request.translate
1120
1128
1121 pull_request = PullRequest.get_or_404(
1129 pull_request = PullRequest.get_or_404(
1122 self.request.matchdict['pull_request_id'])
1130 self.request.matchdict['pull_request_id'])
1123 self.load_default_context()
1131 self.load_default_context()
1124
1132
1125 pr_closed = pull_request.is_closed()
1133 pr_closed = pull_request.is_closed()
1126 allowed_to_delete = PullRequestModel().check_user_delete(
1134 allowed_to_delete = PullRequestModel().check_user_delete(
1127 pull_request, self._rhodecode_user) and not pr_closed
1135 pull_request, self._rhodecode_user) and not pr_closed
1128
1136
1129 # only owner can delete it !
1137 # only owner can delete it !
1130 if allowed_to_delete:
1138 if allowed_to_delete:
1131 PullRequestModel().delete(pull_request, self._rhodecode_user)
1139 PullRequestModel().delete(pull_request, self._rhodecode_user)
1132 Session().commit()
1140 Session().commit()
1133 h.flash(_('Successfully deleted pull request'),
1141 h.flash(_('Successfully deleted pull request'),
1134 category='success')
1142 category='success')
1135 raise HTTPFound(h.route_path('pullrequest_show_all',
1143 raise HTTPFound(h.route_path('pullrequest_show_all',
1136 repo_name=self.db_repo_name))
1144 repo_name=self.db_repo_name))
1137
1145
1138 log.warning('user %s tried to delete pull request without access',
1146 log.warning('user %s tried to delete pull request without access',
1139 self._rhodecode_user)
1147 self._rhodecode_user)
1140 raise HTTPNotFound()
1148 raise HTTPNotFound()
1141
1149
1142 @LoginRequired()
1150 @LoginRequired()
1143 @NotAnonymous()
1151 @NotAnonymous()
1144 @HasRepoPermissionAnyDecorator(
1152 @HasRepoPermissionAnyDecorator(
1145 'repository.read', 'repository.write', 'repository.admin')
1153 'repository.read', 'repository.write', 'repository.admin')
1146 @CSRFRequired()
1154 @CSRFRequired()
1147 @view_config(
1155 @view_config(
1148 route_name='pullrequest_comment_create', request_method='POST',
1156 route_name='pullrequest_comment_create', request_method='POST',
1149 renderer='json_ext')
1157 renderer='json_ext')
1150 def pull_request_comment_create(self):
1158 def pull_request_comment_create(self):
1151 _ = self.request.translate
1159 _ = self.request.translate
1152
1160
1153 pull_request = PullRequest.get_or_404(
1161 pull_request = PullRequest.get_or_404(
1154 self.request.matchdict['pull_request_id'])
1162 self.request.matchdict['pull_request_id'])
1155 pull_request_id = pull_request.pull_request_id
1163 pull_request_id = pull_request.pull_request_id
1156
1164
1157 if pull_request.is_closed():
1165 if pull_request.is_closed():
1158 log.debug('comment: forbidden because pull request is closed')
1166 log.debug('comment: forbidden because pull request is closed')
1159 raise HTTPForbidden()
1167 raise HTTPForbidden()
1160
1168
1161 allowed_to_comment = PullRequestModel().check_user_comment(
1169 allowed_to_comment = PullRequestModel().check_user_comment(
1162 pull_request, self._rhodecode_user)
1170 pull_request, self._rhodecode_user)
1163 if not allowed_to_comment:
1171 if not allowed_to_comment:
1164 log.debug(
1172 log.debug(
1165 'comment: forbidden because pull request is from forbidden repo')
1173 'comment: forbidden because pull request is from forbidden repo')
1166 raise HTTPForbidden()
1174 raise HTTPForbidden()
1167
1175
1168 c = self.load_default_context()
1176 c = self.load_default_context()
1169
1177
1170 status = self.request.POST.get('changeset_status', None)
1178 status = self.request.POST.get('changeset_status', None)
1171 text = self.request.POST.get('text')
1179 text = self.request.POST.get('text')
1172 comment_type = self.request.POST.get('comment_type')
1180 comment_type = self.request.POST.get('comment_type')
1173 resolves_comment_id = self.request.POST.get('resolves_comment_id', None)
1181 resolves_comment_id = self.request.POST.get('resolves_comment_id', None)
1174 close_pull_request = self.request.POST.get('close_pull_request')
1182 close_pull_request = self.request.POST.get('close_pull_request')
1175
1183
1176 # the logic here should work like following, if we submit close
1184 # the logic here should work like following, if we submit close
1177 # pr comment, use `close_pull_request_with_comment` function
1185 # pr comment, use `close_pull_request_with_comment` function
1178 # else handle regular comment logic
1186 # else handle regular comment logic
1179
1187
1180 if close_pull_request:
1188 if close_pull_request:
1181 # only owner or admin or person with write permissions
1189 # only owner or admin or person with write permissions
1182 allowed_to_close = PullRequestModel().check_user_update(
1190 allowed_to_close = PullRequestModel().check_user_update(
1183 pull_request, self._rhodecode_user)
1191 pull_request, self._rhodecode_user)
1184 if not allowed_to_close:
1192 if not allowed_to_close:
1185 log.debug('comment: forbidden because not allowed to close '
1193 log.debug('comment: forbidden because not allowed to close '
1186 'pull request %s', pull_request_id)
1194 'pull request %s', pull_request_id)
1187 raise HTTPForbidden()
1195 raise HTTPForbidden()
1188 comment, status = PullRequestModel().close_pull_request_with_comment(
1196 comment, status = PullRequestModel().close_pull_request_with_comment(
1189 pull_request, self._rhodecode_user, self.db_repo, message=text)
1197 pull_request, self._rhodecode_user, self.db_repo, message=text)
1190 Session().flush()
1198 Session().flush()
1191 events.trigger(
1199 events.trigger(
1192 events.PullRequestCommentEvent(pull_request, comment))
1200 events.PullRequestCommentEvent(pull_request, comment))
1193
1201
1194 else:
1202 else:
1195 # regular comment case, could be inline, or one with status.
1203 # regular comment case, could be inline, or one with status.
1196 # for that one we check also permissions
1204 # for that one we check also permissions
1197
1205
1198 allowed_to_change_status = PullRequestModel().check_user_change_status(
1206 allowed_to_change_status = PullRequestModel().check_user_change_status(
1199 pull_request, self._rhodecode_user)
1207 pull_request, self._rhodecode_user)
1200
1208
1201 if status and allowed_to_change_status:
1209 if status and allowed_to_change_status:
1202 message = (_('Status change %(transition_icon)s %(status)s')
1210 message = (_('Status change %(transition_icon)s %(status)s')
1203 % {'transition_icon': '>',
1211 % {'transition_icon': '>',
1204 'status': ChangesetStatus.get_status_lbl(status)})
1212 'status': ChangesetStatus.get_status_lbl(status)})
1205 text = text or message
1213 text = text or message
1206
1214
1207 comment = CommentsModel().create(
1215 comment = CommentsModel().create(
1208 text=text,
1216 text=text,
1209 repo=self.db_repo.repo_id,
1217 repo=self.db_repo.repo_id,
1210 user=self._rhodecode_user.user_id,
1218 user=self._rhodecode_user.user_id,
1211 pull_request=pull_request,
1219 pull_request=pull_request,
1212 f_path=self.request.POST.get('f_path'),
1220 f_path=self.request.POST.get('f_path'),
1213 line_no=self.request.POST.get('line'),
1221 line_no=self.request.POST.get('line'),
1214 status_change=(ChangesetStatus.get_status_lbl(status)
1222 status_change=(ChangesetStatus.get_status_lbl(status)
1215 if status and allowed_to_change_status else None),
1223 if status and allowed_to_change_status else None),
1216 status_change_type=(status
1224 status_change_type=(status
1217 if status and allowed_to_change_status else None),
1225 if status and allowed_to_change_status else None),
1218 comment_type=comment_type,
1226 comment_type=comment_type,
1219 resolves_comment_id=resolves_comment_id,
1227 resolves_comment_id=resolves_comment_id,
1220 auth_user=self._rhodecode_user
1228 auth_user=self._rhodecode_user
1221 )
1229 )
1222
1230
1223 if allowed_to_change_status:
1231 if allowed_to_change_status:
1224 # calculate old status before we change it
1232 # calculate old status before we change it
1225 old_calculated_status = pull_request.calculated_review_status()
1233 old_calculated_status = pull_request.calculated_review_status()
1226
1234
1227 # get status if set !
1235 # get status if set !
1228 if status:
1236 if status:
1229 ChangesetStatusModel().set_status(
1237 ChangesetStatusModel().set_status(
1230 self.db_repo.repo_id,
1238 self.db_repo.repo_id,
1231 status,
1239 status,
1232 self._rhodecode_user.user_id,
1240 self._rhodecode_user.user_id,
1233 comment,
1241 comment,
1234 pull_request=pull_request
1242 pull_request=pull_request
1235 )
1243 )
1236
1244
1237 Session().flush()
1245 Session().flush()
1238 # this is somehow required to get access to some relationship
1246 # this is somehow required to get access to some relationship
1239 # loaded on comment
1247 # loaded on comment
1240 Session().refresh(comment)
1248 Session().refresh(comment)
1241
1249
1242 events.trigger(
1250 events.trigger(
1243 events.PullRequestCommentEvent(pull_request, comment))
1251 events.PullRequestCommentEvent(pull_request, comment))
1244
1252
1245 # we now calculate the status of pull request, and based on that
1253 # we now calculate the status of pull request, and based on that
1246 # calculation we set the commits status
1254 # calculation we set the commits status
1247 calculated_status = pull_request.calculated_review_status()
1255 calculated_status = pull_request.calculated_review_status()
1248 if old_calculated_status != calculated_status:
1256 if old_calculated_status != calculated_status:
1249 PullRequestModel()._trigger_pull_request_hook(
1257 PullRequestModel()._trigger_pull_request_hook(
1250 pull_request, self._rhodecode_user, 'review_status_change')
1258 pull_request, self._rhodecode_user, 'review_status_change')
1251
1259
1252 Session().commit()
1260 Session().commit()
1253
1261
1254 data = {
1262 data = {
1255 'target_id': h.safeid(h.safe_unicode(
1263 'target_id': h.safeid(h.safe_unicode(
1256 self.request.POST.get('f_path'))),
1264 self.request.POST.get('f_path'))),
1257 }
1265 }
1258 if comment:
1266 if comment:
1259 c.co = comment
1267 c.co = comment
1260 rendered_comment = render(
1268 rendered_comment = render(
1261 'rhodecode:templates/changeset/changeset_comment_block.mako',
1269 'rhodecode:templates/changeset/changeset_comment_block.mako',
1262 self._get_template_context(c), self.request)
1270 self._get_template_context(c), self.request)
1263
1271
1264 data.update(comment.get_dict())
1272 data.update(comment.get_dict())
1265 data.update({'rendered_text': rendered_comment})
1273 data.update({'rendered_text': rendered_comment})
1266
1274
1267 return data
1275 return data
1268
1276
1269 @LoginRequired()
1277 @LoginRequired()
1270 @NotAnonymous()
1278 @NotAnonymous()
1271 @HasRepoPermissionAnyDecorator(
1279 @HasRepoPermissionAnyDecorator(
1272 'repository.read', 'repository.write', 'repository.admin')
1280 'repository.read', 'repository.write', 'repository.admin')
1273 @CSRFRequired()
1281 @CSRFRequired()
1274 @view_config(
1282 @view_config(
1275 route_name='pullrequest_comment_delete', request_method='POST',
1283 route_name='pullrequest_comment_delete', request_method='POST',
1276 renderer='json_ext')
1284 renderer='json_ext')
1277 def pull_request_comment_delete(self):
1285 def pull_request_comment_delete(self):
1278 pull_request = PullRequest.get_or_404(
1286 pull_request = PullRequest.get_or_404(
1279 self.request.matchdict['pull_request_id'])
1287 self.request.matchdict['pull_request_id'])
1280
1288
1281 comment = ChangesetComment.get_or_404(
1289 comment = ChangesetComment.get_or_404(
1282 self.request.matchdict['comment_id'])
1290 self.request.matchdict['comment_id'])
1283 comment_id = comment.comment_id
1291 comment_id = comment.comment_id
1284
1292
1285 if pull_request.is_closed():
1293 if pull_request.is_closed():
1286 log.debug('comment: forbidden because pull request is closed')
1294 log.debug('comment: forbidden because pull request is closed')
1287 raise HTTPForbidden()
1295 raise HTTPForbidden()
1288
1296
1289 if not comment:
1297 if not comment:
1290 log.debug('Comment with id:%s not found, skipping', comment_id)
1298 log.debug('Comment with id:%s not found, skipping', comment_id)
1291 # comment already deleted in another call probably
1299 # comment already deleted in another call probably
1292 return True
1300 return True
1293
1301
1294 if comment.pull_request.is_closed():
1302 if comment.pull_request.is_closed():
1295 # don't allow deleting comments on closed pull request
1303 # don't allow deleting comments on closed pull request
1296 raise HTTPForbidden()
1304 raise HTTPForbidden()
1297
1305
1298 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1306 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1299 super_admin = h.HasPermissionAny('hg.admin')()
1307 super_admin = h.HasPermissionAny('hg.admin')()
1300 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1308 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1301 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1309 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1302 comment_repo_admin = is_repo_admin and is_repo_comment
1310 comment_repo_admin = is_repo_admin and is_repo_comment
1303
1311
1304 if super_admin or comment_owner or comment_repo_admin:
1312 if super_admin or comment_owner or comment_repo_admin:
1305 old_calculated_status = comment.pull_request.calculated_review_status()
1313 old_calculated_status = comment.pull_request.calculated_review_status()
1306 CommentsModel().delete(comment=comment, auth_user=self._rhodecode_user)
1314 CommentsModel().delete(comment=comment, auth_user=self._rhodecode_user)
1307 Session().commit()
1315 Session().commit()
1308 calculated_status = comment.pull_request.calculated_review_status()
1316 calculated_status = comment.pull_request.calculated_review_status()
1309 if old_calculated_status != calculated_status:
1317 if old_calculated_status != calculated_status:
1310 PullRequestModel()._trigger_pull_request_hook(
1318 PullRequestModel()._trigger_pull_request_hook(
1311 comment.pull_request, self._rhodecode_user, 'review_status_change')
1319 comment.pull_request, self._rhodecode_user, 'review_status_change')
1312 return True
1320 return True
1313 else:
1321 else:
1314 log.warning('No permissions for user %s to delete comment_id: %s',
1322 log.warning('No permissions for user %s to delete comment_id: %s',
1315 self._rhodecode_db_user, comment_id)
1323 self._rhodecode_db_user, comment_id)
1316 raise HTTPNotFound()
1324 raise HTTPNotFound()
@@ -1,2095 +1,2101 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2018 RhodeCode GmbH
3 # Copyright (C) 2010-2018 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 Helper functions
22 Helper functions
23
23
24 Consists of functions to typically be used within templates, but also
24 Consists of functions to typically be used within templates, but also
25 available to Controllers. This module is available to both as 'h'.
25 available to Controllers. This module is available to both as 'h'.
26 """
26 """
27
27
28 import os
28 import os
29 import random
29 import random
30 import hashlib
30 import hashlib
31 import StringIO
31 import StringIO
32 import textwrap
32 import textwrap
33 import urllib
33 import urllib
34 import math
34 import math
35 import logging
35 import logging
36 import re
36 import re
37 import urlparse
37 import urlparse
38 import time
38 import time
39 import string
39 import string
40 import hashlib
40 import hashlib
41 from collections import OrderedDict
41 from collections import OrderedDict
42
42
43 import pygments
43 import pygments
44 import itertools
44 import itertools
45 import fnmatch
45 import fnmatch
46
46
47 from datetime import datetime
47 from datetime import datetime
48 from functools import partial
48 from functools import partial
49 from pygments.formatters.html import HtmlFormatter
49 from pygments.formatters.html import HtmlFormatter
50 from pygments import highlight as code_highlight
50 from pygments import highlight as code_highlight
51 from pygments.lexers import (
51 from pygments.lexers import (
52 get_lexer_by_name, get_lexer_for_filename, get_lexer_for_mimetype)
52 get_lexer_by_name, get_lexer_for_filename, get_lexer_for_mimetype)
53
53
54 from pyramid.threadlocal import get_current_request
54 from pyramid.threadlocal import get_current_request
55
55
56 from webhelpers.html import literal, HTML, escape
56 from webhelpers.html import literal, HTML, escape
57 from webhelpers.html.tools import *
57 from webhelpers.html.tools import *
58 from webhelpers.html.builder import make_tag
58 from webhelpers.html.builder import make_tag
59 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \
59 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \
60 end_form, file, form as wh_form, hidden, image, javascript_link, link_to, \
60 end_form, file, form as wh_form, hidden, image, javascript_link, link_to, \
61 link_to_if, link_to_unless, ol, required_legend, select, stylesheet_link, \
61 link_to_if, link_to_unless, ol, required_legend, select, stylesheet_link, \
62 submit, text, password, textarea, title, ul, xml_declaration, radio
62 submit, text, password, textarea, title, ul, xml_declaration, radio
63 from webhelpers.html.tools import auto_link, button_to, highlight, \
63 from webhelpers.html.tools import auto_link, button_to, highlight, \
64 js_obfuscate, mail_to, strip_links, strip_tags, tag_re
64 js_obfuscate, mail_to, strip_links, strip_tags, tag_re
65 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
65 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
66 convert_misc_entities, lchop, plural, rchop, remove_formatting, \
66 convert_misc_entities, lchop, plural, rchop, remove_formatting, \
67 replace_whitespace, urlify, truncate, wrap_paragraphs
67 replace_whitespace, urlify, truncate, wrap_paragraphs
68 from webhelpers.date import time_ago_in_words
68 from webhelpers.date import time_ago_in_words
69 from webhelpers.paginate import Page as _Page
69 from webhelpers.paginate import Page as _Page
70 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \
70 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \
71 convert_boolean_attrs, NotGiven, _make_safe_id_component
71 convert_boolean_attrs, NotGiven, _make_safe_id_component
72 from webhelpers2.number import format_byte_size
72 from webhelpers2.number import format_byte_size
73
73
74 from rhodecode.lib.action_parser import action_parser
74 from rhodecode.lib.action_parser import action_parser
75 from rhodecode.lib.ext_json import json
75 from rhodecode.lib.ext_json import json
76 from rhodecode.lib.utils import repo_name_slug, get_custom_lexer
76 from rhodecode.lib.utils import repo_name_slug, get_custom_lexer
77 from rhodecode.lib.utils2 import str2bool, safe_unicode, safe_str, \
77 from rhodecode.lib.utils2 import str2bool, safe_unicode, safe_str, \
78 get_commit_safe, datetime_to_time, time_to_datetime, time_to_utcdatetime, \
78 get_commit_safe, datetime_to_time, time_to_datetime, time_to_utcdatetime, \
79 AttributeDict, safe_int, md5, md5_safe
79 AttributeDict, safe_int, md5, md5_safe
80 from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links
80 from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links
81 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
81 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
82 from rhodecode.lib.vcs.backends.base import BaseChangeset, EmptyCommit
82 from rhodecode.lib.vcs.backends.base import BaseChangeset, EmptyCommit
83 from rhodecode.config.conf import DATE_FORMAT, DATETIME_FORMAT
83 from rhodecode.config.conf import DATE_FORMAT, DATETIME_FORMAT
84 from rhodecode.model.changeset_status import ChangesetStatusModel
84 from rhodecode.model.changeset_status import ChangesetStatusModel
85 from rhodecode.model.db import Permission, User, Repository
85 from rhodecode.model.db import Permission, User, Repository
86 from rhodecode.model.repo_group import RepoGroupModel
86 from rhodecode.model.repo_group import RepoGroupModel
87 from rhodecode.model.settings import IssueTrackerSettingsModel
87 from rhodecode.model.settings import IssueTrackerSettingsModel
88
88
89 log = logging.getLogger(__name__)
89 log = logging.getLogger(__name__)
90
90
91
91
92 DEFAULT_USER = User.DEFAULT_USER
92 DEFAULT_USER = User.DEFAULT_USER
93 DEFAULT_USER_EMAIL = User.DEFAULT_USER_EMAIL
93 DEFAULT_USER_EMAIL = User.DEFAULT_USER_EMAIL
94
94
95
95
96 def asset(path, ver=None, **kwargs):
96 def asset(path, ver=None, **kwargs):
97 """
97 """
98 Helper to generate a static asset file path for rhodecode assets
98 Helper to generate a static asset file path for rhodecode assets
99
99
100 eg. h.asset('images/image.png', ver='3923')
100 eg. h.asset('images/image.png', ver='3923')
101
101
102 :param path: path of asset
102 :param path: path of asset
103 :param ver: optional version query param to append as ?ver=
103 :param ver: optional version query param to append as ?ver=
104 """
104 """
105 request = get_current_request()
105 request = get_current_request()
106 query = {}
106 query = {}
107 query.update(kwargs)
107 query.update(kwargs)
108 if ver:
108 if ver:
109 query = {'ver': ver}
109 query = {'ver': ver}
110 return request.static_path(
110 return request.static_path(
111 'rhodecode:public/{}'.format(path), _query=query)
111 'rhodecode:public/{}'.format(path), _query=query)
112
112
113
113
114 default_html_escape_table = {
114 default_html_escape_table = {
115 ord('&'): u'&amp;',
115 ord('&'): u'&amp;',
116 ord('<'): u'&lt;',
116 ord('<'): u'&lt;',
117 ord('>'): u'&gt;',
117 ord('>'): u'&gt;',
118 ord('"'): u'&quot;',
118 ord('"'): u'&quot;',
119 ord("'"): u'&#39;',
119 ord("'"): u'&#39;',
120 }
120 }
121
121
122
122
123 def html_escape(text, html_escape_table=default_html_escape_table):
123 def html_escape(text, html_escape_table=default_html_escape_table):
124 """Produce entities within text."""
124 """Produce entities within text."""
125 return text.translate(html_escape_table)
125 return text.translate(html_escape_table)
126
126
127
127
128 def chop_at_smart(s, sub, inclusive=False, suffix_if_chopped=None):
128 def chop_at_smart(s, sub, inclusive=False, suffix_if_chopped=None):
129 """
129 """
130 Truncate string ``s`` at the first occurrence of ``sub``.
130 Truncate string ``s`` at the first occurrence of ``sub``.
131
131
132 If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
132 If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
133 """
133 """
134 suffix_if_chopped = suffix_if_chopped or ''
134 suffix_if_chopped = suffix_if_chopped or ''
135 pos = s.find(sub)
135 pos = s.find(sub)
136 if pos == -1:
136 if pos == -1:
137 return s
137 return s
138
138
139 if inclusive:
139 if inclusive:
140 pos += len(sub)
140 pos += len(sub)
141
141
142 chopped = s[:pos]
142 chopped = s[:pos]
143 left = s[pos:].strip()
143 left = s[pos:].strip()
144
144
145 if left and suffix_if_chopped:
145 if left and suffix_if_chopped:
146 chopped += suffix_if_chopped
146 chopped += suffix_if_chopped
147
147
148 return chopped
148 return chopped
149
149
150
150
151 def shorter(text, size=20):
151 def shorter(text, size=20):
152 postfix = '...'
152 postfix = '...'
153 if len(text) > size:
153 if len(text) > size:
154 return text[:size - len(postfix)] + postfix
154 return text[:size - len(postfix)] + postfix
155 return text
155 return text
156
156
157
157
158 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
158 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
159 """
159 """
160 Reset button
160 Reset button
161 """
161 """
162 _set_input_attrs(attrs, type, name, value)
162 _set_input_attrs(attrs, type, name, value)
163 _set_id_attr(attrs, id, name)
163 _set_id_attr(attrs, id, name)
164 convert_boolean_attrs(attrs, ["disabled"])
164 convert_boolean_attrs(attrs, ["disabled"])
165 return HTML.input(**attrs)
165 return HTML.input(**attrs)
166
166
167 reset = _reset
167 reset = _reset
168 safeid = _make_safe_id_component
168 safeid = _make_safe_id_component
169
169
170
170
171 def branding(name, length=40):
171 def branding(name, length=40):
172 return truncate(name, length, indicator="")
172 return truncate(name, length, indicator="")
173
173
174
174
175 def FID(raw_id, path):
175 def FID(raw_id, path):
176 """
176 """
177 Creates a unique ID for filenode based on it's hash of path and commit
177 Creates a unique ID for filenode based on it's hash of path and commit
178 it's safe to use in urls
178 it's safe to use in urls
179
179
180 :param raw_id:
180 :param raw_id:
181 :param path:
181 :param path:
182 """
182 """
183
183
184 return 'c-%s-%s' % (short_id(raw_id), md5_safe(path)[:12])
184 return 'c-%s-%s' % (short_id(raw_id), md5_safe(path)[:12])
185
185
186
186
187 class _GetError(object):
187 class _GetError(object):
188 """Get error from form_errors, and represent it as span wrapped error
188 """Get error from form_errors, and represent it as span wrapped error
189 message
189 message
190
190
191 :param field_name: field to fetch errors for
191 :param field_name: field to fetch errors for
192 :param form_errors: form errors dict
192 :param form_errors: form errors dict
193 """
193 """
194
194
195 def __call__(self, field_name, form_errors):
195 def __call__(self, field_name, form_errors):
196 tmpl = """<span class="error_msg">%s</span>"""
196 tmpl = """<span class="error_msg">%s</span>"""
197 if form_errors and field_name in form_errors:
197 if form_errors and field_name in form_errors:
198 return literal(tmpl % form_errors.get(field_name))
198 return literal(tmpl % form_errors.get(field_name))
199
199
200 get_error = _GetError()
200 get_error = _GetError()
201
201
202
202
203 class _ToolTip(object):
203 class _ToolTip(object):
204
204
205 def __call__(self, tooltip_title, trim_at=50):
205 def __call__(self, tooltip_title, trim_at=50):
206 """
206 """
207 Special function just to wrap our text into nice formatted
207 Special function just to wrap our text into nice formatted
208 autowrapped text
208 autowrapped text
209
209
210 :param tooltip_title:
210 :param tooltip_title:
211 """
211 """
212 tooltip_title = escape(tooltip_title)
212 tooltip_title = escape(tooltip_title)
213 tooltip_title = tooltip_title.replace('<', '&lt;').replace('>', '&gt;')
213 tooltip_title = tooltip_title.replace('<', '&lt;').replace('>', '&gt;')
214 return tooltip_title
214 return tooltip_title
215 tooltip = _ToolTip()
215 tooltip = _ToolTip()
216
216
217
217
218 def files_breadcrumbs(repo_name, commit_id, file_path):
218 def files_breadcrumbs(repo_name, commit_id, file_path):
219 if isinstance(file_path, str):
219 if isinstance(file_path, str):
220 file_path = safe_unicode(file_path)
220 file_path = safe_unicode(file_path)
221
221
222 # TODO: johbo: Is this always a url like path, or is this operating
222 # TODO: johbo: Is this always a url like path, or is this operating
223 # system dependent?
223 # system dependent?
224 path_segments = file_path.split('/')
224 path_segments = file_path.split('/')
225
225
226 repo_name_html = escape(repo_name)
226 repo_name_html = escape(repo_name)
227 if len(path_segments) == 1 and path_segments[0] == '':
227 if len(path_segments) == 1 and path_segments[0] == '':
228 url_segments = [repo_name_html]
228 url_segments = [repo_name_html]
229 else:
229 else:
230 url_segments = [
230 url_segments = [
231 link_to(
231 link_to(
232 repo_name_html,
232 repo_name_html,
233 route_path(
233 route_path(
234 'repo_files',
234 'repo_files',
235 repo_name=repo_name,
235 repo_name=repo_name,
236 commit_id=commit_id,
236 commit_id=commit_id,
237 f_path=''),
237 f_path=''),
238 class_='pjax-link')]
238 class_='pjax-link')]
239
239
240 last_cnt = len(path_segments) - 1
240 last_cnt = len(path_segments) - 1
241 for cnt, segment in enumerate(path_segments):
241 for cnt, segment in enumerate(path_segments):
242 if not segment:
242 if not segment:
243 continue
243 continue
244 segment_html = escape(segment)
244 segment_html = escape(segment)
245
245
246 if cnt != last_cnt:
246 if cnt != last_cnt:
247 url_segments.append(
247 url_segments.append(
248 link_to(
248 link_to(
249 segment_html,
249 segment_html,
250 route_path(
250 route_path(
251 'repo_files',
251 'repo_files',
252 repo_name=repo_name,
252 repo_name=repo_name,
253 commit_id=commit_id,
253 commit_id=commit_id,
254 f_path='/'.join(path_segments[:cnt + 1])),
254 f_path='/'.join(path_segments[:cnt + 1])),
255 class_='pjax-link'))
255 class_='pjax-link'))
256 else:
256 else:
257 url_segments.append(segment_html)
257 url_segments.append(segment_html)
258
258
259 return literal('/'.join(url_segments))
259 return literal('/'.join(url_segments))
260
260
261
261
262 class CodeHtmlFormatter(HtmlFormatter):
262 class CodeHtmlFormatter(HtmlFormatter):
263 """
263 """
264 My code Html Formatter for source codes
264 My code Html Formatter for source codes
265 """
265 """
266
266
267 def wrap(self, source, outfile):
267 def wrap(self, source, outfile):
268 return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
268 return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
269
269
270 def _wrap_code(self, source):
270 def _wrap_code(self, source):
271 for cnt, it in enumerate(source):
271 for cnt, it in enumerate(source):
272 i, t = it
272 i, t = it
273 t = '<div id="L%s">%s</div>' % (cnt + 1, t)
273 t = '<div id="L%s">%s</div>' % (cnt + 1, t)
274 yield i, t
274 yield i, t
275
275
276 def _wrap_tablelinenos(self, inner):
276 def _wrap_tablelinenos(self, inner):
277 dummyoutfile = StringIO.StringIO()
277 dummyoutfile = StringIO.StringIO()
278 lncount = 0
278 lncount = 0
279 for t, line in inner:
279 for t, line in inner:
280 if t:
280 if t:
281 lncount += 1
281 lncount += 1
282 dummyoutfile.write(line)
282 dummyoutfile.write(line)
283
283
284 fl = self.linenostart
284 fl = self.linenostart
285 mw = len(str(lncount + fl - 1))
285 mw = len(str(lncount + fl - 1))
286 sp = self.linenospecial
286 sp = self.linenospecial
287 st = self.linenostep
287 st = self.linenostep
288 la = self.lineanchors
288 la = self.lineanchors
289 aln = self.anchorlinenos
289 aln = self.anchorlinenos
290 nocls = self.noclasses
290 nocls = self.noclasses
291 if sp:
291 if sp:
292 lines = []
292 lines = []
293
293
294 for i in range(fl, fl + lncount):
294 for i in range(fl, fl + lncount):
295 if i % st == 0:
295 if i % st == 0:
296 if i % sp == 0:
296 if i % sp == 0:
297 if aln:
297 if aln:
298 lines.append('<a href="#%s%d" class="special">%*d</a>' %
298 lines.append('<a href="#%s%d" class="special">%*d</a>' %
299 (la, i, mw, i))
299 (la, i, mw, i))
300 else:
300 else:
301 lines.append('<span class="special">%*d</span>' % (mw, i))
301 lines.append('<span class="special">%*d</span>' % (mw, i))
302 else:
302 else:
303 if aln:
303 if aln:
304 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
304 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
305 else:
305 else:
306 lines.append('%*d' % (mw, i))
306 lines.append('%*d' % (mw, i))
307 else:
307 else:
308 lines.append('')
308 lines.append('')
309 ls = '\n'.join(lines)
309 ls = '\n'.join(lines)
310 else:
310 else:
311 lines = []
311 lines = []
312 for i in range(fl, fl + lncount):
312 for i in range(fl, fl + lncount):
313 if i % st == 0:
313 if i % st == 0:
314 if aln:
314 if aln:
315 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
315 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
316 else:
316 else:
317 lines.append('%*d' % (mw, i))
317 lines.append('%*d' % (mw, i))
318 else:
318 else:
319 lines.append('')
319 lines.append('')
320 ls = '\n'.join(lines)
320 ls = '\n'.join(lines)
321
321
322 # in case you wonder about the seemingly redundant <div> here: since the
322 # in case you wonder about the seemingly redundant <div> here: since the
323 # content in the other cell also is wrapped in a div, some browsers in
323 # content in the other cell also is wrapped in a div, some browsers in
324 # some configurations seem to mess up the formatting...
324 # some configurations seem to mess up the formatting...
325 if nocls:
325 if nocls:
326 yield 0, ('<table class="%stable">' % self.cssclass +
326 yield 0, ('<table class="%stable">' % self.cssclass +
327 '<tr><td><div class="linenodiv" '
327 '<tr><td><div class="linenodiv" '
328 'style="background-color: #f0f0f0; padding-right: 10px">'
328 'style="background-color: #f0f0f0; padding-right: 10px">'
329 '<pre style="line-height: 125%">' +
329 '<pre style="line-height: 125%">' +
330 ls + '</pre></div></td><td id="hlcode" class="code">')
330 ls + '</pre></div></td><td id="hlcode" class="code">')
331 else:
331 else:
332 yield 0, ('<table class="%stable">' % self.cssclass +
332 yield 0, ('<table class="%stable">' % self.cssclass +
333 '<tr><td class="linenos"><div class="linenodiv"><pre>' +
333 '<tr><td class="linenos"><div class="linenodiv"><pre>' +
334 ls + '</pre></div></td><td id="hlcode" class="code">')
334 ls + '</pre></div></td><td id="hlcode" class="code">')
335 yield 0, dummyoutfile.getvalue()
335 yield 0, dummyoutfile.getvalue()
336 yield 0, '</td></tr></table>'
336 yield 0, '</td></tr></table>'
337
337
338
338
339 class SearchContentCodeHtmlFormatter(CodeHtmlFormatter):
339 class SearchContentCodeHtmlFormatter(CodeHtmlFormatter):
340 def __init__(self, **kw):
340 def __init__(self, **kw):
341 # only show these line numbers if set
341 # only show these line numbers if set
342 self.only_lines = kw.pop('only_line_numbers', [])
342 self.only_lines = kw.pop('only_line_numbers', [])
343 self.query_terms = kw.pop('query_terms', [])
343 self.query_terms = kw.pop('query_terms', [])
344 self.max_lines = kw.pop('max_lines', 5)
344 self.max_lines = kw.pop('max_lines', 5)
345 self.line_context = kw.pop('line_context', 3)
345 self.line_context = kw.pop('line_context', 3)
346 self.url = kw.pop('url', None)
346 self.url = kw.pop('url', None)
347
347
348 super(CodeHtmlFormatter, self).__init__(**kw)
348 super(CodeHtmlFormatter, self).__init__(**kw)
349
349
350 def _wrap_code(self, source):
350 def _wrap_code(self, source):
351 for cnt, it in enumerate(source):
351 for cnt, it in enumerate(source):
352 i, t = it
352 i, t = it
353 t = '<pre>%s</pre>' % t
353 t = '<pre>%s</pre>' % t
354 yield i, t
354 yield i, t
355
355
356 def _wrap_tablelinenos(self, inner):
356 def _wrap_tablelinenos(self, inner):
357 yield 0, '<table class="code-highlight %stable">' % self.cssclass
357 yield 0, '<table class="code-highlight %stable">' % self.cssclass
358
358
359 last_shown_line_number = 0
359 last_shown_line_number = 0
360 current_line_number = 1
360 current_line_number = 1
361
361
362 for t, line in inner:
362 for t, line in inner:
363 if not t:
363 if not t:
364 yield t, line
364 yield t, line
365 continue
365 continue
366
366
367 if current_line_number in self.only_lines:
367 if current_line_number in self.only_lines:
368 if last_shown_line_number + 1 != current_line_number:
368 if last_shown_line_number + 1 != current_line_number:
369 yield 0, '<tr>'
369 yield 0, '<tr>'
370 yield 0, '<td class="line">...</td>'
370 yield 0, '<td class="line">...</td>'
371 yield 0, '<td id="hlcode" class="code"></td>'
371 yield 0, '<td id="hlcode" class="code"></td>'
372 yield 0, '</tr>'
372 yield 0, '</tr>'
373
373
374 yield 0, '<tr>'
374 yield 0, '<tr>'
375 if self.url:
375 if self.url:
376 yield 0, '<td class="line"><a href="%s#L%i">%i</a></td>' % (
376 yield 0, '<td class="line"><a href="%s#L%i">%i</a></td>' % (
377 self.url, current_line_number, current_line_number)
377 self.url, current_line_number, current_line_number)
378 else:
378 else:
379 yield 0, '<td class="line"><a href="">%i</a></td>' % (
379 yield 0, '<td class="line"><a href="">%i</a></td>' % (
380 current_line_number)
380 current_line_number)
381 yield 0, '<td id="hlcode" class="code">' + line + '</td>'
381 yield 0, '<td id="hlcode" class="code">' + line + '</td>'
382 yield 0, '</tr>'
382 yield 0, '</tr>'
383
383
384 last_shown_line_number = current_line_number
384 last_shown_line_number = current_line_number
385
385
386 current_line_number += 1
386 current_line_number += 1
387
387
388
388
389 yield 0, '</table>'
389 yield 0, '</table>'
390
390
391
391
392 def extract_phrases(text_query):
392 def extract_phrases(text_query):
393 """
393 """
394 Extracts phrases from search term string making sure phrases
394 Extracts phrases from search term string making sure phrases
395 contained in double quotes are kept together - and discarding empty values
395 contained in double quotes are kept together - and discarding empty values
396 or fully whitespace values eg.
396 or fully whitespace values eg.
397
397
398 'some text "a phrase" more' => ['some', 'text', 'a phrase', 'more']
398 'some text "a phrase" more' => ['some', 'text', 'a phrase', 'more']
399
399
400 """
400 """
401
401
402 in_phrase = False
402 in_phrase = False
403 buf = ''
403 buf = ''
404 phrases = []
404 phrases = []
405 for char in text_query:
405 for char in text_query:
406 if in_phrase:
406 if in_phrase:
407 if char == '"': # end phrase
407 if char == '"': # end phrase
408 phrases.append(buf)
408 phrases.append(buf)
409 buf = ''
409 buf = ''
410 in_phrase = False
410 in_phrase = False
411 continue
411 continue
412 else:
412 else:
413 buf += char
413 buf += char
414 continue
414 continue
415 else:
415 else:
416 if char == '"': # start phrase
416 if char == '"': # start phrase
417 in_phrase = True
417 in_phrase = True
418 phrases.append(buf)
418 phrases.append(buf)
419 buf = ''
419 buf = ''
420 continue
420 continue
421 elif char == ' ':
421 elif char == ' ':
422 phrases.append(buf)
422 phrases.append(buf)
423 buf = ''
423 buf = ''
424 continue
424 continue
425 else:
425 else:
426 buf += char
426 buf += char
427
427
428 phrases.append(buf)
428 phrases.append(buf)
429 phrases = [phrase.strip() for phrase in phrases if phrase.strip()]
429 phrases = [phrase.strip() for phrase in phrases if phrase.strip()]
430 return phrases
430 return phrases
431
431
432
432
433 def get_matching_offsets(text, phrases):
433 def get_matching_offsets(text, phrases):
434 """
434 """
435 Returns a list of string offsets in `text` that the list of `terms` match
435 Returns a list of string offsets in `text` that the list of `terms` match
436
436
437 >>> get_matching_offsets('some text here', ['some', 'here'])
437 >>> get_matching_offsets('some text here', ['some', 'here'])
438 [(0, 4), (10, 14)]
438 [(0, 4), (10, 14)]
439
439
440 """
440 """
441 offsets = []
441 offsets = []
442 for phrase in phrases:
442 for phrase in phrases:
443 for match in re.finditer(phrase, text):
443 for match in re.finditer(phrase, text):
444 offsets.append((match.start(), match.end()))
444 offsets.append((match.start(), match.end()))
445
445
446 return offsets
446 return offsets
447
447
448
448
449 def normalize_text_for_matching(x):
449 def normalize_text_for_matching(x):
450 """
450 """
451 Replaces all non alnum characters to spaces and lower cases the string,
451 Replaces all non alnum characters to spaces and lower cases the string,
452 useful for comparing two text strings without punctuation
452 useful for comparing two text strings without punctuation
453 """
453 """
454 return re.sub(r'[^\w]', ' ', x.lower())
454 return re.sub(r'[^\w]', ' ', x.lower())
455
455
456
456
457 def get_matching_line_offsets(lines, terms):
457 def get_matching_line_offsets(lines, terms):
458 """ Return a set of `lines` indices (starting from 1) matching a
458 """ Return a set of `lines` indices (starting from 1) matching a
459 text search query, along with `context` lines above/below matching lines
459 text search query, along with `context` lines above/below matching lines
460
460
461 :param lines: list of strings representing lines
461 :param lines: list of strings representing lines
462 :param terms: search term string to match in lines eg. 'some text'
462 :param terms: search term string to match in lines eg. 'some text'
463 :param context: number of lines above/below a matching line to add to result
463 :param context: number of lines above/below a matching line to add to result
464 :param max_lines: cut off for lines of interest
464 :param max_lines: cut off for lines of interest
465 eg.
465 eg.
466
466
467 text = '''
467 text = '''
468 words words words
468 words words words
469 words words words
469 words words words
470 some text some
470 some text some
471 words words words
471 words words words
472 words words words
472 words words words
473 text here what
473 text here what
474 '''
474 '''
475 get_matching_line_offsets(text, 'text', context=1)
475 get_matching_line_offsets(text, 'text', context=1)
476 {3: [(5, 9)], 6: [(0, 4)]]
476 {3: [(5, 9)], 6: [(0, 4)]]
477
477
478 """
478 """
479 matching_lines = {}
479 matching_lines = {}
480 phrases = [normalize_text_for_matching(phrase)
480 phrases = [normalize_text_for_matching(phrase)
481 for phrase in extract_phrases(terms)]
481 for phrase in extract_phrases(terms)]
482
482
483 for line_index, line in enumerate(lines, start=1):
483 for line_index, line in enumerate(lines, start=1):
484 match_offsets = get_matching_offsets(
484 match_offsets = get_matching_offsets(
485 normalize_text_for_matching(line), phrases)
485 normalize_text_for_matching(line), phrases)
486 if match_offsets:
486 if match_offsets:
487 matching_lines[line_index] = match_offsets
487 matching_lines[line_index] = match_offsets
488
488
489 return matching_lines
489 return matching_lines
490
490
491
491
492 def hsv_to_rgb(h, s, v):
492 def hsv_to_rgb(h, s, v):
493 """ Convert hsv color values to rgb """
493 """ Convert hsv color values to rgb """
494
494
495 if s == 0.0:
495 if s == 0.0:
496 return v, v, v
496 return v, v, v
497 i = int(h * 6.0) # XXX assume int() truncates!
497 i = int(h * 6.0) # XXX assume int() truncates!
498 f = (h * 6.0) - i
498 f = (h * 6.0) - i
499 p = v * (1.0 - s)
499 p = v * (1.0 - s)
500 q = v * (1.0 - s * f)
500 q = v * (1.0 - s * f)
501 t = v * (1.0 - s * (1.0 - f))
501 t = v * (1.0 - s * (1.0 - f))
502 i = i % 6
502 i = i % 6
503 if i == 0:
503 if i == 0:
504 return v, t, p
504 return v, t, p
505 if i == 1:
505 if i == 1:
506 return q, v, p
506 return q, v, p
507 if i == 2:
507 if i == 2:
508 return p, v, t
508 return p, v, t
509 if i == 3:
509 if i == 3:
510 return p, q, v
510 return p, q, v
511 if i == 4:
511 if i == 4:
512 return t, p, v
512 return t, p, v
513 if i == 5:
513 if i == 5:
514 return v, p, q
514 return v, p, q
515
515
516
516
517 def unique_color_generator(n=10000, saturation=0.10, lightness=0.95):
517 def unique_color_generator(n=10000, saturation=0.10, lightness=0.95):
518 """
518 """
519 Generator for getting n of evenly distributed colors using
519 Generator for getting n of evenly distributed colors using
520 hsv color and golden ratio. It always return same order of colors
520 hsv color and golden ratio. It always return same order of colors
521
521
522 :param n: number of colors to generate
522 :param n: number of colors to generate
523 :param saturation: saturation of returned colors
523 :param saturation: saturation of returned colors
524 :param lightness: lightness of returned colors
524 :param lightness: lightness of returned colors
525 :returns: RGB tuple
525 :returns: RGB tuple
526 """
526 """
527
527
528 golden_ratio = 0.618033988749895
528 golden_ratio = 0.618033988749895
529 h = 0.22717784590367374
529 h = 0.22717784590367374
530
530
531 for _ in xrange(n):
531 for _ in xrange(n):
532 h += golden_ratio
532 h += golden_ratio
533 h %= 1
533 h %= 1
534 HSV_tuple = [h, saturation, lightness]
534 HSV_tuple = [h, saturation, lightness]
535 RGB_tuple = hsv_to_rgb(*HSV_tuple)
535 RGB_tuple = hsv_to_rgb(*HSV_tuple)
536 yield map(lambda x: str(int(x * 256)), RGB_tuple)
536 yield map(lambda x: str(int(x * 256)), RGB_tuple)
537
537
538
538
539 def color_hasher(n=10000, saturation=0.10, lightness=0.95):
539 def color_hasher(n=10000, saturation=0.10, lightness=0.95):
540 """
540 """
541 Returns a function which when called with an argument returns a unique
541 Returns a function which when called with an argument returns a unique
542 color for that argument, eg.
542 color for that argument, eg.
543
543
544 :param n: number of colors to generate
544 :param n: number of colors to generate
545 :param saturation: saturation of returned colors
545 :param saturation: saturation of returned colors
546 :param lightness: lightness of returned colors
546 :param lightness: lightness of returned colors
547 :returns: css RGB string
547 :returns: css RGB string
548
548
549 >>> color_hash = color_hasher()
549 >>> color_hash = color_hasher()
550 >>> color_hash('hello')
550 >>> color_hash('hello')
551 'rgb(34, 12, 59)'
551 'rgb(34, 12, 59)'
552 >>> color_hash('hello')
552 >>> color_hash('hello')
553 'rgb(34, 12, 59)'
553 'rgb(34, 12, 59)'
554 >>> color_hash('other')
554 >>> color_hash('other')
555 'rgb(90, 224, 159)'
555 'rgb(90, 224, 159)'
556 """
556 """
557
557
558 color_dict = {}
558 color_dict = {}
559 cgenerator = unique_color_generator(
559 cgenerator = unique_color_generator(
560 saturation=saturation, lightness=lightness)
560 saturation=saturation, lightness=lightness)
561
561
562 def get_color_string(thing):
562 def get_color_string(thing):
563 if thing in color_dict:
563 if thing in color_dict:
564 col = color_dict[thing]
564 col = color_dict[thing]
565 else:
565 else:
566 col = color_dict[thing] = cgenerator.next()
566 col = color_dict[thing] = cgenerator.next()
567 return "rgb(%s)" % (', '.join(col))
567 return "rgb(%s)" % (', '.join(col))
568
568
569 return get_color_string
569 return get_color_string
570
570
571
571
572 def get_lexer_safe(mimetype=None, filepath=None):
572 def get_lexer_safe(mimetype=None, filepath=None):
573 """
573 """
574 Tries to return a relevant pygments lexer using mimetype/filepath name,
574 Tries to return a relevant pygments lexer using mimetype/filepath name,
575 defaulting to plain text if none could be found
575 defaulting to plain text if none could be found
576 """
576 """
577 lexer = None
577 lexer = None
578 try:
578 try:
579 if mimetype:
579 if mimetype:
580 lexer = get_lexer_for_mimetype(mimetype)
580 lexer = get_lexer_for_mimetype(mimetype)
581 if not lexer:
581 if not lexer:
582 lexer = get_lexer_for_filename(filepath)
582 lexer = get_lexer_for_filename(filepath)
583 except pygments.util.ClassNotFound:
583 except pygments.util.ClassNotFound:
584 pass
584 pass
585
585
586 if not lexer:
586 if not lexer:
587 lexer = get_lexer_by_name('text')
587 lexer = get_lexer_by_name('text')
588
588
589 return lexer
589 return lexer
590
590
591
591
592 def get_lexer_for_filenode(filenode):
592 def get_lexer_for_filenode(filenode):
593 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
593 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
594 return lexer
594 return lexer
595
595
596
596
597 def pygmentize(filenode, **kwargs):
597 def pygmentize(filenode, **kwargs):
598 """
598 """
599 pygmentize function using pygments
599 pygmentize function using pygments
600
600
601 :param filenode:
601 :param filenode:
602 """
602 """
603 lexer = get_lexer_for_filenode(filenode)
603 lexer = get_lexer_for_filenode(filenode)
604 return literal(code_highlight(filenode.content, lexer,
604 return literal(code_highlight(filenode.content, lexer,
605 CodeHtmlFormatter(**kwargs)))
605 CodeHtmlFormatter(**kwargs)))
606
606
607
607
608 def is_following_repo(repo_name, user_id):
608 def is_following_repo(repo_name, user_id):
609 from rhodecode.model.scm import ScmModel
609 from rhodecode.model.scm import ScmModel
610 return ScmModel().is_following_repo(repo_name, user_id)
610 return ScmModel().is_following_repo(repo_name, user_id)
611
611
612
612
613 class _Message(object):
613 class _Message(object):
614 """A message returned by ``Flash.pop_messages()``.
614 """A message returned by ``Flash.pop_messages()``.
615
615
616 Converting the message to a string returns the message text. Instances
616 Converting the message to a string returns the message text. Instances
617 also have the following attributes:
617 also have the following attributes:
618
618
619 * ``message``: the message text.
619 * ``message``: the message text.
620 * ``category``: the category specified when the message was created.
620 * ``category``: the category specified when the message was created.
621 """
621 """
622
622
623 def __init__(self, category, message):
623 def __init__(self, category, message):
624 self.category = category
624 self.category = category
625 self.message = message
625 self.message = message
626
626
627 def __str__(self):
627 def __str__(self):
628 return self.message
628 return self.message
629
629
630 __unicode__ = __str__
630 __unicode__ = __str__
631
631
632 def __html__(self):
632 def __html__(self):
633 return escape(safe_unicode(self.message))
633 return escape(safe_unicode(self.message))
634
634
635
635
636 class Flash(object):
636 class Flash(object):
637 # List of allowed categories. If None, allow any category.
637 # List of allowed categories. If None, allow any category.
638 categories = ["warning", "notice", "error", "success"]
638 categories = ["warning", "notice", "error", "success"]
639
639
640 # Default category if none is specified.
640 # Default category if none is specified.
641 default_category = "notice"
641 default_category = "notice"
642
642
643 def __init__(self, session_key="flash", categories=None,
643 def __init__(self, session_key="flash", categories=None,
644 default_category=None):
644 default_category=None):
645 """
645 """
646 Instantiate a ``Flash`` object.
646 Instantiate a ``Flash`` object.
647
647
648 ``session_key`` is the key to save the messages under in the user's
648 ``session_key`` is the key to save the messages under in the user's
649 session.
649 session.
650
650
651 ``categories`` is an optional list which overrides the default list
651 ``categories`` is an optional list which overrides the default list
652 of categories.
652 of categories.
653
653
654 ``default_category`` overrides the default category used for messages
654 ``default_category`` overrides the default category used for messages
655 when none is specified.
655 when none is specified.
656 """
656 """
657 self.session_key = session_key
657 self.session_key = session_key
658 if categories is not None:
658 if categories is not None:
659 self.categories = categories
659 self.categories = categories
660 if default_category is not None:
660 if default_category is not None:
661 self.default_category = default_category
661 self.default_category = default_category
662 if self.categories and self.default_category not in self.categories:
662 if self.categories and self.default_category not in self.categories:
663 raise ValueError(
663 raise ValueError(
664 "unrecognized default category %r" % (self.default_category,))
664 "unrecognized default category %r" % (self.default_category,))
665
665
666 def pop_messages(self, session=None, request=None):
666 def pop_messages(self, session=None, request=None):
667 """
667 """
668 Return all accumulated messages and delete them from the session.
668 Return all accumulated messages and delete them from the session.
669
669
670 The return value is a list of ``Message`` objects.
670 The return value is a list of ``Message`` objects.
671 """
671 """
672 messages = []
672 messages = []
673
673
674 if not session:
674 if not session:
675 if not request:
675 if not request:
676 request = get_current_request()
676 request = get_current_request()
677 session = request.session
677 session = request.session
678
678
679 # Pop the 'old' pylons flash messages. They are tuples of the form
679 # Pop the 'old' pylons flash messages. They are tuples of the form
680 # (category, message)
680 # (category, message)
681 for cat, msg in session.pop(self.session_key, []):
681 for cat, msg in session.pop(self.session_key, []):
682 messages.append(_Message(cat, msg))
682 messages.append(_Message(cat, msg))
683
683
684 # Pop the 'new' pyramid flash messages for each category as list
684 # Pop the 'new' pyramid flash messages for each category as list
685 # of strings.
685 # of strings.
686 for cat in self.categories:
686 for cat in self.categories:
687 for msg in session.pop_flash(queue=cat):
687 for msg in session.pop_flash(queue=cat):
688 messages.append(_Message(cat, msg))
688 messages.append(_Message(cat, msg))
689 # Map messages from the default queue to the 'notice' category.
689 # Map messages from the default queue to the 'notice' category.
690 for msg in session.pop_flash():
690 for msg in session.pop_flash():
691 messages.append(_Message('notice', msg))
691 messages.append(_Message('notice', msg))
692
692
693 session.save()
693 session.save()
694 return messages
694 return messages
695
695
696 def json_alerts(self, session=None, request=None):
696 def json_alerts(self, session=None, request=None):
697 payloads = []
697 payloads = []
698 messages = flash.pop_messages(session=session, request=request)
698 messages = flash.pop_messages(session=session, request=request)
699 if messages:
699 if messages:
700 for message in messages:
700 for message in messages:
701 subdata = {}
701 subdata = {}
702 if hasattr(message.message, 'rsplit'):
702 if hasattr(message.message, 'rsplit'):
703 flash_data = message.message.rsplit('|DELIM|', 1)
703 flash_data = message.message.rsplit('|DELIM|', 1)
704 org_message = flash_data[0]
704 org_message = flash_data[0]
705 if len(flash_data) > 1:
705 if len(flash_data) > 1:
706 subdata = json.loads(flash_data[1])
706 subdata = json.loads(flash_data[1])
707 else:
707 else:
708 org_message = message.message
708 org_message = message.message
709 payloads.append({
709 payloads.append({
710 'message': {
710 'message': {
711 'message': u'{}'.format(org_message),
711 'message': u'{}'.format(org_message),
712 'level': message.category,
712 'level': message.category,
713 'force': True,
713 'force': True,
714 'subdata': subdata
714 'subdata': subdata
715 }
715 }
716 })
716 })
717 return json.dumps(payloads)
717 return json.dumps(payloads)
718
718
719 def __call__(self, message, category=None, ignore_duplicate=False,
719 def __call__(self, message, category=None, ignore_duplicate=False,
720 session=None, request=None):
720 session=None, request=None):
721
721
722 if not session:
722 if not session:
723 if not request:
723 if not request:
724 request = get_current_request()
724 request = get_current_request()
725 session = request.session
725 session = request.session
726
726
727 session.flash(
727 session.flash(
728 message, queue=category, allow_duplicate=not ignore_duplicate)
728 message, queue=category, allow_duplicate=not ignore_duplicate)
729
729
730
730
731 flash = Flash()
731 flash = Flash()
732
732
733 #==============================================================================
733 #==============================================================================
734 # SCM FILTERS available via h.
734 # SCM FILTERS available via h.
735 #==============================================================================
735 #==============================================================================
736 from rhodecode.lib.vcs.utils import author_name, author_email
736 from rhodecode.lib.vcs.utils import author_name, author_email
737 from rhodecode.lib.utils2 import credentials_filter, age as _age
737 from rhodecode.lib.utils2 import credentials_filter, age as _age
738 from rhodecode.model.db import User, ChangesetStatus
738 from rhodecode.model.db import User, ChangesetStatus
739
739
740 age = _age
740 age = _age
741 capitalize = lambda x: x.capitalize()
741 capitalize = lambda x: x.capitalize()
742 email = author_email
742 email = author_email
743 short_id = lambda x: x[:12]
743 short_id = lambda x: x[:12]
744 hide_credentials = lambda x: ''.join(credentials_filter(x))
744 hide_credentials = lambda x: ''.join(credentials_filter(x))
745
745
746
746
747 import pytz
747 import pytz
748 import tzlocal
748 import tzlocal
749 local_timezone = tzlocal.get_localzone()
749 local_timezone = tzlocal.get_localzone()
750
750
751
751
752 def age_component(datetime_iso, value=None, time_is_local=False):
752 def age_component(datetime_iso, value=None, time_is_local=False):
753 title = value or format_date(datetime_iso)
753 title = value or format_date(datetime_iso)
754 tzinfo = '+00:00'
754 tzinfo = '+00:00'
755
755
756 # detect if we have a timezone info, otherwise, add it
756 # detect if we have a timezone info, otherwise, add it
757 if time_is_local and isinstance(datetime_iso, datetime) and not datetime_iso.tzinfo:
757 if time_is_local and isinstance(datetime_iso, datetime) and not datetime_iso.tzinfo:
758 force_timezone = os.environ.get('RC_TIMEZONE', '')
758 force_timezone = os.environ.get('RC_TIMEZONE', '')
759 if force_timezone:
759 if force_timezone:
760 force_timezone = pytz.timezone(force_timezone)
760 force_timezone = pytz.timezone(force_timezone)
761 timezone = force_timezone or local_timezone
761 timezone = force_timezone or local_timezone
762 offset = timezone.localize(datetime_iso).strftime('%z')
762 offset = timezone.localize(datetime_iso).strftime('%z')
763 tzinfo = '{}:{}'.format(offset[:-2], offset[-2:])
763 tzinfo = '{}:{}'.format(offset[:-2], offset[-2:])
764
764
765 return literal(
765 return literal(
766 '<time class="timeago tooltip" '
766 '<time class="timeago tooltip" '
767 'title="{1}{2}" datetime="{0}{2}">{1}</time>'.format(
767 'title="{1}{2}" datetime="{0}{2}">{1}</time>'.format(
768 datetime_iso, title, tzinfo))
768 datetime_iso, title, tzinfo))
769
769
770
770
771 def _shorten_commit_id(commit_id):
771 def _shorten_commit_id(commit_id):
772 from rhodecode import CONFIG
772 from rhodecode import CONFIG
773 def_len = safe_int(CONFIG.get('rhodecode_show_sha_length', 12))
773 def_len = safe_int(CONFIG.get('rhodecode_show_sha_length', 12))
774 return commit_id[:def_len]
774 return commit_id[:def_len]
775
775
776
776
777 def show_id(commit):
777 def show_id(commit):
778 """
778 """
779 Configurable function that shows ID
779 Configurable function that shows ID
780 by default it's r123:fffeeefffeee
780 by default it's r123:fffeeefffeee
781
781
782 :param commit: commit instance
782 :param commit: commit instance
783 """
783 """
784 from rhodecode import CONFIG
784 from rhodecode import CONFIG
785 show_idx = str2bool(CONFIG.get('rhodecode_show_revision_number', True))
785 show_idx = str2bool(CONFIG.get('rhodecode_show_revision_number', True))
786
786
787 raw_id = _shorten_commit_id(commit.raw_id)
787 raw_id = _shorten_commit_id(commit.raw_id)
788 if show_idx:
788 if show_idx:
789 return 'r%s:%s' % (commit.idx, raw_id)
789 return 'r%s:%s' % (commit.idx, raw_id)
790 else:
790 else:
791 return '%s' % (raw_id, )
791 return '%s' % (raw_id, )
792
792
793
793
794 def format_date(date):
794 def format_date(date):
795 """
795 """
796 use a standardized formatting for dates used in RhodeCode
796 use a standardized formatting for dates used in RhodeCode
797
797
798 :param date: date/datetime object
798 :param date: date/datetime object
799 :return: formatted date
799 :return: formatted date
800 """
800 """
801
801
802 if date:
802 if date:
803 _fmt = "%a, %d %b %Y %H:%M:%S"
803 _fmt = "%a, %d %b %Y %H:%M:%S"
804 return safe_unicode(date.strftime(_fmt))
804 return safe_unicode(date.strftime(_fmt))
805
805
806 return u""
806 return u""
807
807
808
808
809 class _RepoChecker(object):
809 class _RepoChecker(object):
810
810
811 def __init__(self, backend_alias):
811 def __init__(self, backend_alias):
812 self._backend_alias = backend_alias
812 self._backend_alias = backend_alias
813
813
814 def __call__(self, repository):
814 def __call__(self, repository):
815 if hasattr(repository, 'alias'):
815 if hasattr(repository, 'alias'):
816 _type = repository.alias
816 _type = repository.alias
817 elif hasattr(repository, 'repo_type'):
817 elif hasattr(repository, 'repo_type'):
818 _type = repository.repo_type
818 _type = repository.repo_type
819 else:
819 else:
820 _type = repository
820 _type = repository
821 return _type == self._backend_alias
821 return _type == self._backend_alias
822
822
823 is_git = _RepoChecker('git')
823 is_git = _RepoChecker('git')
824 is_hg = _RepoChecker('hg')
824 is_hg = _RepoChecker('hg')
825 is_svn = _RepoChecker('svn')
825 is_svn = _RepoChecker('svn')
826
826
827
827
828 def get_repo_type_by_name(repo_name):
828 def get_repo_type_by_name(repo_name):
829 repo = Repository.get_by_repo_name(repo_name)
829 repo = Repository.get_by_repo_name(repo_name)
830 return repo.repo_type
830 return repo.repo_type
831
831
832
832
833 def is_svn_without_proxy(repository):
833 def is_svn_without_proxy(repository):
834 if is_svn(repository):
834 if is_svn(repository):
835 from rhodecode.model.settings import VcsSettingsModel
835 from rhodecode.model.settings import VcsSettingsModel
836 conf = VcsSettingsModel().get_ui_settings_as_config_obj()
836 conf = VcsSettingsModel().get_ui_settings_as_config_obj()
837 return not str2bool(conf.get('vcs_svn_proxy', 'http_requests_enabled'))
837 return not str2bool(conf.get('vcs_svn_proxy', 'http_requests_enabled'))
838 return False
838 return False
839
839
840
840
841 def discover_user(author):
841 def discover_user(author):
842 """
842 """
843 Tries to discover RhodeCode User based on the autho string. Author string
843 Tries to discover RhodeCode User based on the autho string. Author string
844 is typically `FirstName LastName <email@address.com>`
844 is typically `FirstName LastName <email@address.com>`
845 """
845 """
846
846
847 # if author is already an instance use it for extraction
847 # if author is already an instance use it for extraction
848 if isinstance(author, User):
848 if isinstance(author, User):
849 return author
849 return author
850
850
851 # Valid email in the attribute passed, see if they're in the system
851 # Valid email in the attribute passed, see if they're in the system
852 _email = author_email(author)
852 _email = author_email(author)
853 if _email != '':
853 if _email != '':
854 user = User.get_by_email(_email, case_insensitive=True, cache=True)
854 user = User.get_by_email(_email, case_insensitive=True, cache=True)
855 if user is not None:
855 if user is not None:
856 return user
856 return user
857
857
858 # Maybe it's a username, we try to extract it and fetch by username ?
858 # Maybe it's a username, we try to extract it and fetch by username ?
859 _author = author_name(author)
859 _author = author_name(author)
860 user = User.get_by_username(_author, case_insensitive=True, cache=True)
860 user = User.get_by_username(_author, case_insensitive=True, cache=True)
861 if user is not None:
861 if user is not None:
862 return user
862 return user
863
863
864 return None
864 return None
865
865
866
866
867 def email_or_none(author):
867 def email_or_none(author):
868 # extract email from the commit string
868 # extract email from the commit string
869 _email = author_email(author)
869 _email = author_email(author)
870
870
871 # If we have an email, use it, otherwise
871 # If we have an email, use it, otherwise
872 # see if it contains a username we can get an email from
872 # see if it contains a username we can get an email from
873 if _email != '':
873 if _email != '':
874 return _email
874 return _email
875 else:
875 else:
876 user = User.get_by_username(
876 user = User.get_by_username(
877 author_name(author), case_insensitive=True, cache=True)
877 author_name(author), case_insensitive=True, cache=True)
878
878
879 if user is not None:
879 if user is not None:
880 return user.email
880 return user.email
881
881
882 # No valid email, not a valid user in the system, none!
882 # No valid email, not a valid user in the system, none!
883 return None
883 return None
884
884
885
885
886 def link_to_user(author, length=0, **kwargs):
886 def link_to_user(author, length=0, **kwargs):
887 user = discover_user(author)
887 user = discover_user(author)
888 # user can be None, but if we have it already it means we can re-use it
888 # user can be None, but if we have it already it means we can re-use it
889 # in the person() function, so we save 1 intensive-query
889 # in the person() function, so we save 1 intensive-query
890 if user:
890 if user:
891 author = user
891 author = user
892
892
893 display_person = person(author, 'username_or_name_or_email')
893 display_person = person(author, 'username_or_name_or_email')
894 if length:
894 if length:
895 display_person = shorter(display_person, length)
895 display_person = shorter(display_person, length)
896
896
897 if user:
897 if user:
898 return link_to(
898 return link_to(
899 escape(display_person),
899 escape(display_person),
900 route_path('user_profile', username=user.username),
900 route_path('user_profile', username=user.username),
901 **kwargs)
901 **kwargs)
902 else:
902 else:
903 return escape(display_person)
903 return escape(display_person)
904
904
905
905
906 def link_to_group(users_group_name, **kwargs):
906 def link_to_group(users_group_name, **kwargs):
907 return link_to(
907 return link_to(
908 escape(users_group_name),
908 escape(users_group_name),
909 route_path('user_group_profile', user_group_name=users_group_name),
909 route_path('user_group_profile', user_group_name=users_group_name),
910 **kwargs)
910 **kwargs)
911
911
912
912
913 def person(author, show_attr="username_and_name"):
913 def person(author, show_attr="username_and_name"):
914 user = discover_user(author)
914 user = discover_user(author)
915 if user:
915 if user:
916 return getattr(user, show_attr)
916 return getattr(user, show_attr)
917 else:
917 else:
918 _author = author_name(author)
918 _author = author_name(author)
919 _email = email(author)
919 _email = email(author)
920 return _author or _email
920 return _author or _email
921
921
922
922
923 def author_string(email):
923 def author_string(email):
924 if email:
924 if email:
925 user = User.get_by_email(email, case_insensitive=True, cache=True)
925 user = User.get_by_email(email, case_insensitive=True, cache=True)
926 if user:
926 if user:
927 if user.first_name or user.last_name:
927 if user.first_name or user.last_name:
928 return '%s %s &lt;%s&gt;' % (
928 return '%s %s &lt;%s&gt;' % (
929 user.first_name, user.last_name, email)
929 user.first_name, user.last_name, email)
930 else:
930 else:
931 return email
931 return email
932 else:
932 else:
933 return email
933 return email
934 else:
934 else:
935 return None
935 return None
936
936
937
937
938 def person_by_id(id_, show_attr="username_and_name"):
938 def person_by_id(id_, show_attr="username_and_name"):
939 # attr to return from fetched user
939 # attr to return from fetched user
940 person_getter = lambda usr: getattr(usr, show_attr)
940 person_getter = lambda usr: getattr(usr, show_attr)
941
941
942 #maybe it's an ID ?
942 #maybe it's an ID ?
943 if str(id_).isdigit() or isinstance(id_, int):
943 if str(id_).isdigit() or isinstance(id_, int):
944 id_ = int(id_)
944 id_ = int(id_)
945 user = User.get(id_)
945 user = User.get(id_)
946 if user is not None:
946 if user is not None:
947 return person_getter(user)
947 return person_getter(user)
948 return id_
948 return id_
949
949
950
950
951 def gravatar_with_user(request, author, show_disabled=False):
951 def gravatar_with_user(request, author, show_disabled=False):
952 _render = request.get_partial_renderer(
952 _render = request.get_partial_renderer(
953 'rhodecode:templates/base/base.mako')
953 'rhodecode:templates/base/base.mako')
954 return _render('gravatar_with_user', author, show_disabled=show_disabled)
954 return _render('gravatar_with_user', author, show_disabled=show_disabled)
955
955
956
956
957 tags_paterns = OrderedDict((
957 tags_paterns = OrderedDict((
958 ('lang', (re.compile(r'\[(lang|language)\ \=\&gt;\ *([a-zA-Z\-\/\#\+\.]*)\]'),
958 ('lang', (re.compile(r'\[(lang|language)\ \=\&gt;\ *([a-zA-Z\-\/\#\+\.]*)\]'),
959 '<div class="metatag" tag="lang">\\2</div>')),
959 '<div class="metatag" tag="lang">\\2</div>')),
960
960
961 ('see', (re.compile(r'\[see\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]'),
961 ('see', (re.compile(r'\[see\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]'),
962 '<div class="metatag" tag="see">see: \\1 </div>')),
962 '<div class="metatag" tag="see">see: \\1 </div>')),
963
963
964 ('url', (re.compile(r'\[url\ \=\&gt;\ \[([a-zA-Z0-9\ \.\-\_]+)\]\((http://|https://|/)(.*?)\)\]'),
964 ('url', (re.compile(r'\[url\ \=\&gt;\ \[([a-zA-Z0-9\ \.\-\_]+)\]\((http://|https://|/)(.*?)\)\]'),
965 '<div class="metatag" tag="url"> <a href="\\2\\3">\\1</a> </div>')),
965 '<div class="metatag" tag="url"> <a href="\\2\\3">\\1</a> </div>')),
966
966
967 ('license', (re.compile(r'\[license\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]'),
967 ('license', (re.compile(r'\[license\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]'),
968 '<div class="metatag" tag="license"><a href="http:\/\/www.opensource.org/licenses/\\1">\\1</a></div>')),
968 '<div class="metatag" tag="license"><a href="http:\/\/www.opensource.org/licenses/\\1">\\1</a></div>')),
969
969
970 ('ref', (re.compile(r'\[(requires|recommends|conflicts|base)\ \=\&gt;\ *([a-zA-Z0-9\-\/]*)\]'),
970 ('ref', (re.compile(r'\[(requires|recommends|conflicts|base)\ \=\&gt;\ *([a-zA-Z0-9\-\/]*)\]'),
971 '<div class="metatag" tag="ref \\1">\\1: <a href="/\\2">\\2</a></div>')),
971 '<div class="metatag" tag="ref \\1">\\1: <a href="/\\2">\\2</a></div>')),
972
972
973 ('state', (re.compile(r'\[(stable|featured|stale|dead|dev|deprecated)\]'),
973 ('state', (re.compile(r'\[(stable|featured|stale|dead|dev|deprecated)\]'),
974 '<div class="metatag" tag="state \\1">\\1</div>')),
974 '<div class="metatag" tag="state \\1">\\1</div>')),
975
975
976 # label in grey
976 # label in grey
977 ('label', (re.compile(r'\[([a-z]+)\]'),
977 ('label', (re.compile(r'\[([a-z]+)\]'),
978 '<div class="metatag" tag="label">\\1</div>')),
978 '<div class="metatag" tag="label">\\1</div>')),
979
979
980 # generic catch all in grey
980 # generic catch all in grey
981 ('generic', (re.compile(r'\[([a-zA-Z0-9\.\-\_]+)\]'),
981 ('generic', (re.compile(r'\[([a-zA-Z0-9\.\-\_]+)\]'),
982 '<div class="metatag" tag="generic">\\1</div>')),
982 '<div class="metatag" tag="generic">\\1</div>')),
983 ))
983 ))
984
984
985
985
986 def extract_metatags(value):
986 def extract_metatags(value):
987 """
987 """
988 Extract supported meta-tags from given text value
988 Extract supported meta-tags from given text value
989 """
989 """
990 tags = []
990 tags = []
991 if not value:
991 if not value:
992 return tags, ''
992 return tags, ''
993
993
994 for key, val in tags_paterns.items():
994 for key, val in tags_paterns.items():
995 pat, replace_html = val
995 pat, replace_html = val
996 tags.extend([(key, x.group()) for x in pat.finditer(value)])
996 tags.extend([(key, x.group()) for x in pat.finditer(value)])
997 value = pat.sub('', value)
997 value = pat.sub('', value)
998
998
999 return tags, value
999 return tags, value
1000
1000
1001
1001
1002 def style_metatag(tag_type, value):
1002 def style_metatag(tag_type, value):
1003 """
1003 """
1004 converts tags from value into html equivalent
1004 converts tags from value into html equivalent
1005 """
1005 """
1006 if not value:
1006 if not value:
1007 return ''
1007 return ''
1008
1008
1009 html_value = value
1009 html_value = value
1010 tag_data = tags_paterns.get(tag_type)
1010 tag_data = tags_paterns.get(tag_type)
1011 if tag_data:
1011 if tag_data:
1012 pat, replace_html = tag_data
1012 pat, replace_html = tag_data
1013 # convert to plain `unicode` instead of a markup tag to be used in
1013 # convert to plain `unicode` instead of a markup tag to be used in
1014 # regex expressions. safe_unicode doesn't work here
1014 # regex expressions. safe_unicode doesn't work here
1015 html_value = pat.sub(replace_html, unicode(value))
1015 html_value = pat.sub(replace_html, unicode(value))
1016
1016
1017 return html_value
1017 return html_value
1018
1018
1019
1019
1020 def bool2icon(value):
1020 def bool2icon(value):
1021 """
1021 """
1022 Returns boolean value of a given value, represented as html element with
1022 Returns boolean value of a given value, represented as html element with
1023 classes that will represent icons
1023 classes that will represent icons
1024
1024
1025 :param value: given value to convert to html node
1025 :param value: given value to convert to html node
1026 """
1026 """
1027
1027
1028 if value: # does bool conversion
1028 if value: # does bool conversion
1029 return HTML.tag('i', class_="icon-true")
1029 return HTML.tag('i', class_="icon-true")
1030 else: # not true as bool
1030 else: # not true as bool
1031 return HTML.tag('i', class_="icon-false")
1031 return HTML.tag('i', class_="icon-false")
1032
1032
1033
1033
1034 #==============================================================================
1034 #==============================================================================
1035 # PERMS
1035 # PERMS
1036 #==============================================================================
1036 #==============================================================================
1037 from rhodecode.lib.auth import HasPermissionAny, HasPermissionAll, \
1037 from rhodecode.lib.auth import HasPermissionAny, HasPermissionAll, \
1038 HasRepoPermissionAny, HasRepoPermissionAll, HasRepoGroupPermissionAll, \
1038 HasRepoPermissionAny, HasRepoPermissionAll, HasRepoGroupPermissionAll, \
1039 HasRepoGroupPermissionAny, HasRepoPermissionAnyApi, get_csrf_token, \
1039 HasRepoGroupPermissionAny, HasRepoPermissionAnyApi, get_csrf_token, \
1040 csrf_token_key
1040 csrf_token_key
1041
1041
1042
1042
1043 #==============================================================================
1043 #==============================================================================
1044 # GRAVATAR URL
1044 # GRAVATAR URL
1045 #==============================================================================
1045 #==============================================================================
1046 class InitialsGravatar(object):
1046 class InitialsGravatar(object):
1047 def __init__(self, email_address, first_name, last_name, size=30,
1047 def __init__(self, email_address, first_name, last_name, size=30,
1048 background=None, text_color='#fff'):
1048 background=None, text_color='#fff'):
1049 self.size = size
1049 self.size = size
1050 self.first_name = first_name
1050 self.first_name = first_name
1051 self.last_name = last_name
1051 self.last_name = last_name
1052 self.email_address = email_address
1052 self.email_address = email_address
1053 self.background = background or self.str2color(email_address)
1053 self.background = background or self.str2color(email_address)
1054 self.text_color = text_color
1054 self.text_color = text_color
1055
1055
1056 def get_color_bank(self):
1056 def get_color_bank(self):
1057 """
1057 """
1058 returns a predefined list of colors that gravatars can use.
1058 returns a predefined list of colors that gravatars can use.
1059 Those are randomized distinct colors that guarantee readability and
1059 Those are randomized distinct colors that guarantee readability and
1060 uniqueness.
1060 uniqueness.
1061
1061
1062 generated with: http://phrogz.net/css/distinct-colors.html
1062 generated with: http://phrogz.net/css/distinct-colors.html
1063 """
1063 """
1064 return [
1064 return [
1065 '#bf3030', '#a67f53', '#00ff00', '#5989b3', '#392040', '#d90000',
1065 '#bf3030', '#a67f53', '#00ff00', '#5989b3', '#392040', '#d90000',
1066 '#402910', '#204020', '#79baf2', '#a700b3', '#bf6060', '#7f5320',
1066 '#402910', '#204020', '#79baf2', '#a700b3', '#bf6060', '#7f5320',
1067 '#008000', '#003059', '#ee00ff', '#ff0000', '#8c4b00', '#007300',
1067 '#008000', '#003059', '#ee00ff', '#ff0000', '#8c4b00', '#007300',
1068 '#005fb3', '#de73e6', '#ff4040', '#ffaa00', '#3df255', '#203140',
1068 '#005fb3', '#de73e6', '#ff4040', '#ffaa00', '#3df255', '#203140',
1069 '#47004d', '#591616', '#664400', '#59b365', '#0d2133', '#83008c',
1069 '#47004d', '#591616', '#664400', '#59b365', '#0d2133', '#83008c',
1070 '#592d2d', '#bf9f60', '#73e682', '#1d3f73', '#73006b', '#402020',
1070 '#592d2d', '#bf9f60', '#73e682', '#1d3f73', '#73006b', '#402020',
1071 '#b2862d', '#397341', '#597db3', '#e600d6', '#a60000', '#736039',
1071 '#b2862d', '#397341', '#597db3', '#e600d6', '#a60000', '#736039',
1072 '#00b318', '#79aaf2', '#330d30', '#ff8080', '#403010', '#16591f',
1072 '#00b318', '#79aaf2', '#330d30', '#ff8080', '#403010', '#16591f',
1073 '#002459', '#8c4688', '#e50000', '#ffbf40', '#00732e', '#102340',
1073 '#002459', '#8c4688', '#e50000', '#ffbf40', '#00732e', '#102340',
1074 '#bf60ac', '#8c4646', '#cc8800', '#00a642', '#1d3473', '#b32d98',
1074 '#bf60ac', '#8c4646', '#cc8800', '#00a642', '#1d3473', '#b32d98',
1075 '#660e00', '#ffd580', '#80ffb2', '#7391e6', '#733967', '#d97b6c',
1075 '#660e00', '#ffd580', '#80ffb2', '#7391e6', '#733967', '#d97b6c',
1076 '#8c5e00', '#59b389', '#3967e6', '#590047', '#73281d', '#665200',
1076 '#8c5e00', '#59b389', '#3967e6', '#590047', '#73281d', '#665200',
1077 '#00e67a', '#2d50b3', '#8c2377', '#734139', '#b2982d', '#16593a',
1077 '#00e67a', '#2d50b3', '#8c2377', '#734139', '#b2982d', '#16593a',
1078 '#001859', '#ff00aa', '#a65e53', '#ffcc00', '#0d3321', '#2d3959',
1078 '#001859', '#ff00aa', '#a65e53', '#ffcc00', '#0d3321', '#2d3959',
1079 '#731d56', '#401610', '#4c3d00', '#468c6c', '#002ca6', '#d936a3',
1079 '#731d56', '#401610', '#4c3d00', '#468c6c', '#002ca6', '#d936a3',
1080 '#d94c36', '#403920', '#36d9a3', '#0d1733', '#592d4a', '#993626',
1080 '#d94c36', '#403920', '#36d9a3', '#0d1733', '#592d4a', '#993626',
1081 '#cca300', '#00734d', '#46598c', '#8c005e', '#7f1100', '#8c7000',
1081 '#cca300', '#00734d', '#46598c', '#8c005e', '#7f1100', '#8c7000',
1082 '#00a66f', '#7382e6', '#b32d74', '#d9896c', '#ffe680', '#1d7362',
1082 '#00a66f', '#7382e6', '#b32d74', '#d9896c', '#ffe680', '#1d7362',
1083 '#364cd9', '#73003d', '#d93a00', '#998a4d', '#59b3a1', '#5965b3',
1083 '#364cd9', '#73003d', '#d93a00', '#998a4d', '#59b3a1', '#5965b3',
1084 '#e5007a', '#73341d', '#665f00', '#00b38f', '#0018b3', '#59163a',
1084 '#e5007a', '#73341d', '#665f00', '#00b38f', '#0018b3', '#59163a',
1085 '#b2502d', '#bfb960', '#00ffcc', '#23318c', '#a6537f', '#734939',
1085 '#b2502d', '#bfb960', '#00ffcc', '#23318c', '#a6537f', '#734939',
1086 '#b2a700', '#104036', '#3d3df2', '#402031', '#e56739', '#736f39',
1086 '#b2a700', '#104036', '#3d3df2', '#402031', '#e56739', '#736f39',
1087 '#79f2ea', '#000059', '#401029', '#4c1400', '#ffee00', '#005953',
1087 '#79f2ea', '#000059', '#401029', '#4c1400', '#ffee00', '#005953',
1088 '#101040', '#990052', '#402820', '#403d10', '#00ffee', '#0000d9',
1088 '#101040', '#990052', '#402820', '#403d10', '#00ffee', '#0000d9',
1089 '#ff80c4', '#a66953', '#eeff00', '#00ccbe', '#8080ff', '#e673a1',
1089 '#ff80c4', '#a66953', '#eeff00', '#00ccbe', '#8080ff', '#e673a1',
1090 '#a62c00', '#474d00', '#1a3331', '#46468c', '#733950', '#662900',
1090 '#a62c00', '#474d00', '#1a3331', '#46468c', '#733950', '#662900',
1091 '#858c23', '#238c85', '#0f0073', '#b20047', '#d9986c', '#becc00',
1091 '#858c23', '#238c85', '#0f0073', '#b20047', '#d9986c', '#becc00',
1092 '#396f73', '#281d73', '#ff0066', '#ff6600', '#dee673', '#59adb3',
1092 '#396f73', '#281d73', '#ff0066', '#ff6600', '#dee673', '#59adb3',
1093 '#6559b3', '#590024', '#b2622d', '#98b32d', '#36ced9', '#332d59',
1093 '#6559b3', '#590024', '#b2622d', '#98b32d', '#36ced9', '#332d59',
1094 '#40001a', '#733f1d', '#526600', '#005359', '#242040', '#bf6079',
1094 '#40001a', '#733f1d', '#526600', '#005359', '#242040', '#bf6079',
1095 '#735039', '#cef23d', '#007780', '#5630bf', '#66001b', '#b24700',
1095 '#735039', '#cef23d', '#007780', '#5630bf', '#66001b', '#b24700',
1096 '#acbf60', '#1d6273', '#25008c', '#731d34', '#a67453', '#50592d',
1096 '#acbf60', '#1d6273', '#25008c', '#731d34', '#a67453', '#50592d',
1097 '#00ccff', '#6600ff', '#ff0044', '#4c1f00', '#8a994d', '#79daf2',
1097 '#00ccff', '#6600ff', '#ff0044', '#4c1f00', '#8a994d', '#79daf2',
1098 '#a173e6', '#d93662', '#402310', '#aaff00', '#2d98b3', '#8c40ff',
1098 '#a173e6', '#d93662', '#402310', '#aaff00', '#2d98b3', '#8c40ff',
1099 '#592d39', '#ff8c40', '#354020', '#103640', '#1a0040', '#331a20',
1099 '#592d39', '#ff8c40', '#354020', '#103640', '#1a0040', '#331a20',
1100 '#331400', '#334d00', '#1d5673', '#583973', '#7f0022', '#4c3626',
1100 '#331400', '#334d00', '#1d5673', '#583973', '#7f0022', '#4c3626',
1101 '#88cc00', '#36a3d9', '#3d0073', '#d9364c', '#33241a', '#698c23',
1101 '#88cc00', '#36a3d9', '#3d0073', '#d9364c', '#33241a', '#698c23',
1102 '#5995b3', '#300059', '#e57382', '#7f3300', '#366600', '#00aaff',
1102 '#5995b3', '#300059', '#e57382', '#7f3300', '#366600', '#00aaff',
1103 '#3a1659', '#733941', '#663600', '#74b32d', '#003c59', '#7f53a6',
1103 '#3a1659', '#733941', '#663600', '#74b32d', '#003c59', '#7f53a6',
1104 '#73000f', '#ff8800', '#baf279', '#79caf2', '#291040', '#a6293a',
1104 '#73000f', '#ff8800', '#baf279', '#79caf2', '#291040', '#a6293a',
1105 '#b2742d', '#587339', '#0077b3', '#632699', '#400009', '#d9a66c',
1105 '#b2742d', '#587339', '#0077b3', '#632699', '#400009', '#d9a66c',
1106 '#294010', '#2d4a59', '#aa00ff', '#4c131b', '#b25f00', '#5ce600',
1106 '#294010', '#2d4a59', '#aa00ff', '#4c131b', '#b25f00', '#5ce600',
1107 '#267399', '#a336d9', '#990014', '#664e33', '#86bf60', '#0088ff',
1107 '#267399', '#a336d9', '#990014', '#664e33', '#86bf60', '#0088ff',
1108 '#7700b3', '#593a16', '#073300', '#1d4b73', '#ac60bf', '#e59539',
1108 '#7700b3', '#593a16', '#073300', '#1d4b73', '#ac60bf', '#e59539',
1109 '#4f8c46', '#368dd9', '#5c0073'
1109 '#4f8c46', '#368dd9', '#5c0073'
1110 ]
1110 ]
1111
1111
1112 def rgb_to_hex_color(self, rgb_tuple):
1112 def rgb_to_hex_color(self, rgb_tuple):
1113 """
1113 """
1114 Converts an rgb_tuple passed to an hex color.
1114 Converts an rgb_tuple passed to an hex color.
1115
1115
1116 :param rgb_tuple: tuple with 3 ints represents rgb color space
1116 :param rgb_tuple: tuple with 3 ints represents rgb color space
1117 """
1117 """
1118 return '#' + ("".join(map(chr, rgb_tuple)).encode('hex'))
1118 return '#' + ("".join(map(chr, rgb_tuple)).encode('hex'))
1119
1119
1120 def email_to_int_list(self, email_str):
1120 def email_to_int_list(self, email_str):
1121 """
1121 """
1122 Get every byte of the hex digest value of email and turn it to integer.
1122 Get every byte of the hex digest value of email and turn it to integer.
1123 It's going to be always between 0-255
1123 It's going to be always between 0-255
1124 """
1124 """
1125 digest = md5_safe(email_str.lower())
1125 digest = md5_safe(email_str.lower())
1126 return [int(digest[i * 2:i * 2 + 2], 16) for i in range(16)]
1126 return [int(digest[i * 2:i * 2 + 2], 16) for i in range(16)]
1127
1127
1128 def pick_color_bank_index(self, email_str, color_bank):
1128 def pick_color_bank_index(self, email_str, color_bank):
1129 return self.email_to_int_list(email_str)[0] % len(color_bank)
1129 return self.email_to_int_list(email_str)[0] % len(color_bank)
1130
1130
1131 def str2color(self, email_str):
1131 def str2color(self, email_str):
1132 """
1132 """
1133 Tries to map in a stable algorithm an email to color
1133 Tries to map in a stable algorithm an email to color
1134
1134
1135 :param email_str:
1135 :param email_str:
1136 """
1136 """
1137 color_bank = self.get_color_bank()
1137 color_bank = self.get_color_bank()
1138 # pick position (module it's length so we always find it in the
1138 # pick position (module it's length so we always find it in the
1139 # bank even if it's smaller than 256 values
1139 # bank even if it's smaller than 256 values
1140 pos = self.pick_color_bank_index(email_str, color_bank)
1140 pos = self.pick_color_bank_index(email_str, color_bank)
1141 return color_bank[pos]
1141 return color_bank[pos]
1142
1142
1143 def normalize_email(self, email_address):
1143 def normalize_email(self, email_address):
1144 import unicodedata
1144 import unicodedata
1145 # default host used to fill in the fake/missing email
1145 # default host used to fill in the fake/missing email
1146 default_host = u'localhost'
1146 default_host = u'localhost'
1147
1147
1148 if not email_address:
1148 if not email_address:
1149 email_address = u'%s@%s' % (User.DEFAULT_USER, default_host)
1149 email_address = u'%s@%s' % (User.DEFAULT_USER, default_host)
1150
1150
1151 email_address = safe_unicode(email_address)
1151 email_address = safe_unicode(email_address)
1152
1152
1153 if u'@' not in email_address:
1153 if u'@' not in email_address:
1154 email_address = u'%s@%s' % (email_address, default_host)
1154 email_address = u'%s@%s' % (email_address, default_host)
1155
1155
1156 if email_address.endswith(u'@'):
1156 if email_address.endswith(u'@'):
1157 email_address = u'%s%s' % (email_address, default_host)
1157 email_address = u'%s%s' % (email_address, default_host)
1158
1158
1159 email_address = unicodedata.normalize('NFKD', email_address)\
1159 email_address = unicodedata.normalize('NFKD', email_address)\
1160 .encode('ascii', 'ignore')
1160 .encode('ascii', 'ignore')
1161 return email_address
1161 return email_address
1162
1162
1163 def get_initials(self):
1163 def get_initials(self):
1164 """
1164 """
1165 Returns 2 letter initials calculated based on the input.
1165 Returns 2 letter initials calculated based on the input.
1166 The algorithm picks first given email address, and takes first letter
1166 The algorithm picks first given email address, and takes first letter
1167 of part before @, and then the first letter of server name. In case
1167 of part before @, and then the first letter of server name. In case
1168 the part before @ is in a format of `somestring.somestring2` it replaces
1168 the part before @ is in a format of `somestring.somestring2` it replaces
1169 the server letter with first letter of somestring2
1169 the server letter with first letter of somestring2
1170
1170
1171 In case function was initialized with both first and lastname, this
1171 In case function was initialized with both first and lastname, this
1172 overrides the extraction from email by first letter of the first and
1172 overrides the extraction from email by first letter of the first and
1173 last name. We add special logic to that functionality, In case Full name
1173 last name. We add special logic to that functionality, In case Full name
1174 is compound, like Guido Von Rossum, we use last part of the last name
1174 is compound, like Guido Von Rossum, we use last part of the last name
1175 (Von Rossum) picking `R`.
1175 (Von Rossum) picking `R`.
1176
1176
1177 Function also normalizes the non-ascii characters to they ascii
1177 Function also normalizes the non-ascii characters to they ascii
1178 representation, eg Δ„ => A
1178 representation, eg Δ„ => A
1179 """
1179 """
1180 import unicodedata
1180 import unicodedata
1181 # replace non-ascii to ascii
1181 # replace non-ascii to ascii
1182 first_name = unicodedata.normalize(
1182 first_name = unicodedata.normalize(
1183 'NFKD', safe_unicode(self.first_name)).encode('ascii', 'ignore')
1183 'NFKD', safe_unicode(self.first_name)).encode('ascii', 'ignore')
1184 last_name = unicodedata.normalize(
1184 last_name = unicodedata.normalize(
1185 'NFKD', safe_unicode(self.last_name)).encode('ascii', 'ignore')
1185 'NFKD', safe_unicode(self.last_name)).encode('ascii', 'ignore')
1186
1186
1187 # do NFKD encoding, and also make sure email has proper format
1187 # do NFKD encoding, and also make sure email has proper format
1188 email_address = self.normalize_email(self.email_address)
1188 email_address = self.normalize_email(self.email_address)
1189
1189
1190 # first push the email initials
1190 # first push the email initials
1191 prefix, server = email_address.split('@', 1)
1191 prefix, server = email_address.split('@', 1)
1192
1192
1193 # check if prefix is maybe a 'first_name.last_name' syntax
1193 # check if prefix is maybe a 'first_name.last_name' syntax
1194 _dot_split = prefix.rsplit('.', 1)
1194 _dot_split = prefix.rsplit('.', 1)
1195 if len(_dot_split) == 2 and _dot_split[1]:
1195 if len(_dot_split) == 2 and _dot_split[1]:
1196 initials = [_dot_split[0][0], _dot_split[1][0]]
1196 initials = [_dot_split[0][0], _dot_split[1][0]]
1197 else:
1197 else:
1198 initials = [prefix[0], server[0]]
1198 initials = [prefix[0], server[0]]
1199
1199
1200 # then try to replace either first_name or last_name
1200 # then try to replace either first_name or last_name
1201 fn_letter = (first_name or " ")[0].strip()
1201 fn_letter = (first_name or " ")[0].strip()
1202 ln_letter = (last_name.split(' ', 1)[-1] or " ")[0].strip()
1202 ln_letter = (last_name.split(' ', 1)[-1] or " ")[0].strip()
1203
1203
1204 if fn_letter:
1204 if fn_letter:
1205 initials[0] = fn_letter
1205 initials[0] = fn_letter
1206
1206
1207 if ln_letter:
1207 if ln_letter:
1208 initials[1] = ln_letter
1208 initials[1] = ln_letter
1209
1209
1210 return ''.join(initials).upper()
1210 return ''.join(initials).upper()
1211
1211
1212 def get_img_data_by_type(self, font_family, img_type):
1212 def get_img_data_by_type(self, font_family, img_type):
1213 default_user = """
1213 default_user = """
1214 <svg xmlns="http://www.w3.org/2000/svg"
1214 <svg xmlns="http://www.w3.org/2000/svg"
1215 version="1.1" x="0px" y="0px" width="{size}" height="{size}"
1215 version="1.1" x="0px" y="0px" width="{size}" height="{size}"
1216 viewBox="-15 -10 439.165 429.164"
1216 viewBox="-15 -10 439.165 429.164"
1217
1217
1218 xml:space="preserve"
1218 xml:space="preserve"
1219 style="background:{background};" >
1219 style="background:{background};" >
1220
1220
1221 <path d="M204.583,216.671c50.664,0,91.74-48.075,
1221 <path d="M204.583,216.671c50.664,0,91.74-48.075,
1222 91.74-107.378c0-82.237-41.074-107.377-91.74-107.377
1222 91.74-107.378c0-82.237-41.074-107.377-91.74-107.377
1223 c-50.668,0-91.74,25.14-91.74,107.377C112.844,
1223 c-50.668,0-91.74,25.14-91.74,107.377C112.844,
1224 168.596,153.916,216.671,
1224 168.596,153.916,216.671,
1225 204.583,216.671z" fill="{text_color}"/>
1225 204.583,216.671z" fill="{text_color}"/>
1226 <path d="M407.164,374.717L360.88,
1226 <path d="M407.164,374.717L360.88,
1227 270.454c-2.117-4.771-5.836-8.728-10.465-11.138l-71.83-37.392
1227 270.454c-2.117-4.771-5.836-8.728-10.465-11.138l-71.83-37.392
1228 c-1.584-0.823-3.502-0.663-4.926,0.415c-20.316,
1228 c-1.584-0.823-3.502-0.663-4.926,0.415c-20.316,
1229 15.366-44.203,23.488-69.076,23.488c-24.877,
1229 15.366-44.203,23.488-69.076,23.488c-24.877,
1230 0-48.762-8.122-69.078-23.488
1230 0-48.762-8.122-69.078-23.488
1231 c-1.428-1.078-3.346-1.238-4.93-0.415L58.75,
1231 c-1.428-1.078-3.346-1.238-4.93-0.415L58.75,
1232 259.316c-4.631,2.41-8.346,6.365-10.465,11.138L2.001,374.717
1232 259.316c-4.631,2.41-8.346,6.365-10.465,11.138L2.001,374.717
1233 c-3.191,7.188-2.537,15.412,1.75,22.005c4.285,
1233 c-3.191,7.188-2.537,15.412,1.75,22.005c4.285,
1234 6.592,11.537,10.526,19.4,10.526h362.861c7.863,0,15.117-3.936,
1234 6.592,11.537,10.526,19.4,10.526h362.861c7.863,0,15.117-3.936,
1235 19.402-10.527 C409.699,390.129,
1235 19.402-10.527 C409.699,390.129,
1236 410.355,381.902,407.164,374.717z" fill="{text_color}"/>
1236 410.355,381.902,407.164,374.717z" fill="{text_color}"/>
1237 </svg>""".format(
1237 </svg>""".format(
1238 size=self.size,
1238 size=self.size,
1239 background='#979797', # @grey4
1239 background='#979797', # @grey4
1240 text_color=self.text_color,
1240 text_color=self.text_color,
1241 font_family=font_family)
1241 font_family=font_family)
1242
1242
1243 return {
1243 return {
1244 "default_user": default_user
1244 "default_user": default_user
1245 }[img_type]
1245 }[img_type]
1246
1246
1247 def get_img_data(self, svg_type=None):
1247 def get_img_data(self, svg_type=None):
1248 """
1248 """
1249 generates the svg metadata for image
1249 generates the svg metadata for image
1250 """
1250 """
1251
1251
1252 font_family = ','.join([
1252 font_family = ','.join([
1253 'proximanovaregular',
1253 'proximanovaregular',
1254 'Proxima Nova Regular',
1254 'Proxima Nova Regular',
1255 'Proxima Nova',
1255 'Proxima Nova',
1256 'Arial',
1256 'Arial',
1257 'Lucida Grande',
1257 'Lucida Grande',
1258 'sans-serif'
1258 'sans-serif'
1259 ])
1259 ])
1260 if svg_type:
1260 if svg_type:
1261 return self.get_img_data_by_type(font_family, svg_type)
1261 return self.get_img_data_by_type(font_family, svg_type)
1262
1262
1263 initials = self.get_initials()
1263 initials = self.get_initials()
1264 img_data = """
1264 img_data = """
1265 <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none"
1265 <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none"
1266 width="{size}" height="{size}"
1266 width="{size}" height="{size}"
1267 style="width: 100%; height: 100%; background-color: {background}"
1267 style="width: 100%; height: 100%; background-color: {background}"
1268 viewBox="0 0 {size} {size}">
1268 viewBox="0 0 {size} {size}">
1269 <text text-anchor="middle" y="50%" x="50%" dy="0.35em"
1269 <text text-anchor="middle" y="50%" x="50%" dy="0.35em"
1270 pointer-events="auto" fill="{text_color}"
1270 pointer-events="auto" fill="{text_color}"
1271 font-family="{font_family}"
1271 font-family="{font_family}"
1272 style="font-weight: 400; font-size: {f_size}px;">{text}
1272 style="font-weight: 400; font-size: {f_size}px;">{text}
1273 </text>
1273 </text>
1274 </svg>""".format(
1274 </svg>""".format(
1275 size=self.size,
1275 size=self.size,
1276 f_size=self.size/1.85, # scale the text inside the box nicely
1276 f_size=self.size/1.85, # scale the text inside the box nicely
1277 background=self.background,
1277 background=self.background,
1278 text_color=self.text_color,
1278 text_color=self.text_color,
1279 text=initials.upper(),
1279 text=initials.upper(),
1280 font_family=font_family)
1280 font_family=font_family)
1281
1281
1282 return img_data
1282 return img_data
1283
1283
1284 def generate_svg(self, svg_type=None):
1284 def generate_svg(self, svg_type=None):
1285 img_data = self.get_img_data(svg_type)
1285 img_data = self.get_img_data(svg_type)
1286 return "data:image/svg+xml;base64,%s" % img_data.encode('base64')
1286 return "data:image/svg+xml;base64,%s" % img_data.encode('base64')
1287
1287
1288
1288
1289 def initials_gravatar(email_address, first_name, last_name, size=30):
1289 def initials_gravatar(email_address, first_name, last_name, size=30):
1290 svg_type = None
1290 svg_type = None
1291 if email_address == User.DEFAULT_USER_EMAIL:
1291 if email_address == User.DEFAULT_USER_EMAIL:
1292 svg_type = 'default_user'
1292 svg_type = 'default_user'
1293 klass = InitialsGravatar(email_address, first_name, last_name, size)
1293 klass = InitialsGravatar(email_address, first_name, last_name, size)
1294 return klass.generate_svg(svg_type=svg_type)
1294 return klass.generate_svg(svg_type=svg_type)
1295
1295
1296
1296
1297 def gravatar_url(email_address, size=30, request=None):
1297 def gravatar_url(email_address, size=30, request=None):
1298 request = get_current_request()
1298 request = get_current_request()
1299 _use_gravatar = request.call_context.visual.use_gravatar
1299 _use_gravatar = request.call_context.visual.use_gravatar
1300 _gravatar_url = request.call_context.visual.gravatar_url
1300 _gravatar_url = request.call_context.visual.gravatar_url
1301
1301
1302 _gravatar_url = _gravatar_url or User.DEFAULT_GRAVATAR_URL
1302 _gravatar_url = _gravatar_url or User.DEFAULT_GRAVATAR_URL
1303
1303
1304 email_address = email_address or User.DEFAULT_USER_EMAIL
1304 email_address = email_address or User.DEFAULT_USER_EMAIL
1305 if isinstance(email_address, unicode):
1305 if isinstance(email_address, unicode):
1306 # hashlib crashes on unicode items
1306 # hashlib crashes on unicode items
1307 email_address = safe_str(email_address)
1307 email_address = safe_str(email_address)
1308
1308
1309 # empty email or default user
1309 # empty email or default user
1310 if not email_address or email_address == User.DEFAULT_USER_EMAIL:
1310 if not email_address or email_address == User.DEFAULT_USER_EMAIL:
1311 return initials_gravatar(User.DEFAULT_USER_EMAIL, '', '', size=size)
1311 return initials_gravatar(User.DEFAULT_USER_EMAIL, '', '', size=size)
1312
1312
1313 if _use_gravatar:
1313 if _use_gravatar:
1314 # TODO: Disuse pyramid thread locals. Think about another solution to
1314 # TODO: Disuse pyramid thread locals. Think about another solution to
1315 # get the host and schema here.
1315 # get the host and schema here.
1316 request = get_current_request()
1316 request = get_current_request()
1317 tmpl = safe_str(_gravatar_url)
1317 tmpl = safe_str(_gravatar_url)
1318 tmpl = tmpl.replace('{email}', email_address)\
1318 tmpl = tmpl.replace('{email}', email_address)\
1319 .replace('{md5email}', md5_safe(email_address.lower())) \
1319 .replace('{md5email}', md5_safe(email_address.lower())) \
1320 .replace('{netloc}', request.host)\
1320 .replace('{netloc}', request.host)\
1321 .replace('{scheme}', request.scheme)\
1321 .replace('{scheme}', request.scheme)\
1322 .replace('{size}', safe_str(size))
1322 .replace('{size}', safe_str(size))
1323 return tmpl
1323 return tmpl
1324 else:
1324 else:
1325 return initials_gravatar(email_address, '', '', size=size)
1325 return initials_gravatar(email_address, '', '', size=size)
1326
1326
1327
1327
1328 class Page(_Page):
1328 class Page(_Page):
1329 """
1329 """
1330 Custom pager to match rendering style with paginator
1330 Custom pager to match rendering style with paginator
1331 """
1331 """
1332
1332
1333 def _get_pos(self, cur_page, max_page, items):
1333 def _get_pos(self, cur_page, max_page, items):
1334 edge = (items / 2) + 1
1334 edge = (items / 2) + 1
1335 if (cur_page <= edge):
1335 if (cur_page <= edge):
1336 radius = max(items / 2, items - cur_page)
1336 radius = max(items / 2, items - cur_page)
1337 elif (max_page - cur_page) < edge:
1337 elif (max_page - cur_page) < edge:
1338 radius = (items - 1) - (max_page - cur_page)
1338 radius = (items - 1) - (max_page - cur_page)
1339 else:
1339 else:
1340 radius = items / 2
1340 radius = items / 2
1341
1341
1342 left = max(1, (cur_page - (radius)))
1342 left = max(1, (cur_page - (radius)))
1343 right = min(max_page, cur_page + (radius))
1343 right = min(max_page, cur_page + (radius))
1344 return left, cur_page, right
1344 return left, cur_page, right
1345
1345
1346 def _range(self, regexp_match):
1346 def _range(self, regexp_match):
1347 """
1347 """
1348 Return range of linked pages (e.g. '1 2 [3] 4 5 6 7 8').
1348 Return range of linked pages (e.g. '1 2 [3] 4 5 6 7 8').
1349
1349
1350 Arguments:
1350 Arguments:
1351
1351
1352 regexp_match
1352 regexp_match
1353 A "re" (regular expressions) match object containing the
1353 A "re" (regular expressions) match object containing the
1354 radius of linked pages around the current page in
1354 radius of linked pages around the current page in
1355 regexp_match.group(1) as a string
1355 regexp_match.group(1) as a string
1356
1356
1357 This function is supposed to be called as a callable in
1357 This function is supposed to be called as a callable in
1358 re.sub.
1358 re.sub.
1359
1359
1360 """
1360 """
1361 radius = int(regexp_match.group(1))
1361 radius = int(regexp_match.group(1))
1362
1362
1363 # Compute the first and last page number within the radius
1363 # Compute the first and last page number within the radius
1364 # e.g. '1 .. 5 6 [7] 8 9 .. 12'
1364 # e.g. '1 .. 5 6 [7] 8 9 .. 12'
1365 # -> leftmost_page = 5
1365 # -> leftmost_page = 5
1366 # -> rightmost_page = 9
1366 # -> rightmost_page = 9
1367 leftmost_page, _cur, rightmost_page = self._get_pos(self.page,
1367 leftmost_page, _cur, rightmost_page = self._get_pos(self.page,
1368 self.last_page,
1368 self.last_page,
1369 (radius * 2) + 1)
1369 (radius * 2) + 1)
1370 nav_items = []
1370 nav_items = []
1371
1371
1372 # Create a link to the first page (unless we are on the first page
1372 # Create a link to the first page (unless we are on the first page
1373 # or there would be no need to insert '..' spacers)
1373 # or there would be no need to insert '..' spacers)
1374 if self.page != self.first_page and self.first_page < leftmost_page:
1374 if self.page != self.first_page and self.first_page < leftmost_page:
1375 nav_items.append(self._pagerlink(self.first_page, self.first_page))
1375 nav_items.append(self._pagerlink(self.first_page, self.first_page))
1376
1376
1377 # Insert dots if there are pages between the first page
1377 # Insert dots if there are pages between the first page
1378 # and the currently displayed page range
1378 # and the currently displayed page range
1379 if leftmost_page - self.first_page > 1:
1379 if leftmost_page - self.first_page > 1:
1380 # Wrap in a SPAN tag if nolink_attr is set
1380 # Wrap in a SPAN tag if nolink_attr is set
1381 text = '..'
1381 text = '..'
1382 if self.dotdot_attr:
1382 if self.dotdot_attr:
1383 text = HTML.span(c=text, **self.dotdot_attr)
1383 text = HTML.span(c=text, **self.dotdot_attr)
1384 nav_items.append(text)
1384 nav_items.append(text)
1385
1385
1386 for thispage in xrange(leftmost_page, rightmost_page + 1):
1386 for thispage in xrange(leftmost_page, rightmost_page + 1):
1387 # Hilight the current page number and do not use a link
1387 # Hilight the current page number and do not use a link
1388 if thispage == self.page:
1388 if thispage == self.page:
1389 text = '%s' % (thispage,)
1389 text = '%s' % (thispage,)
1390 # Wrap in a SPAN tag if nolink_attr is set
1390 # Wrap in a SPAN tag if nolink_attr is set
1391 if self.curpage_attr:
1391 if self.curpage_attr:
1392 text = HTML.span(c=text, **self.curpage_attr)
1392 text = HTML.span(c=text, **self.curpage_attr)
1393 nav_items.append(text)
1393 nav_items.append(text)
1394 # Otherwise create just a link to that page
1394 # Otherwise create just a link to that page
1395 else:
1395 else:
1396 text = '%s' % (thispage,)
1396 text = '%s' % (thispage,)
1397 nav_items.append(self._pagerlink(thispage, text))
1397 nav_items.append(self._pagerlink(thispage, text))
1398
1398
1399 # Insert dots if there are pages between the displayed
1399 # Insert dots if there are pages between the displayed
1400 # page numbers and the end of the page range
1400 # page numbers and the end of the page range
1401 if self.last_page - rightmost_page > 1:
1401 if self.last_page - rightmost_page > 1:
1402 text = '..'
1402 text = '..'
1403 # Wrap in a SPAN tag if nolink_attr is set
1403 # Wrap in a SPAN tag if nolink_attr is set
1404 if self.dotdot_attr:
1404 if self.dotdot_attr:
1405 text = HTML.span(c=text, **self.dotdot_attr)
1405 text = HTML.span(c=text, **self.dotdot_attr)
1406 nav_items.append(text)
1406 nav_items.append(text)
1407
1407
1408 # Create a link to the very last page (unless we are on the last
1408 # Create a link to the very last page (unless we are on the last
1409 # page or there would be no need to insert '..' spacers)
1409 # page or there would be no need to insert '..' spacers)
1410 if self.page != self.last_page and rightmost_page < self.last_page:
1410 if self.page != self.last_page and rightmost_page < self.last_page:
1411 nav_items.append(self._pagerlink(self.last_page, self.last_page))
1411 nav_items.append(self._pagerlink(self.last_page, self.last_page))
1412
1412
1413 ## prerender links
1413 ## prerender links
1414 #_page_link = url.current()
1414 #_page_link = url.current()
1415 #nav_items.append(literal('<link rel="prerender" href="%s?page=%s">' % (_page_link, str(int(self.page)+1))))
1415 #nav_items.append(literal('<link rel="prerender" href="%s?page=%s">' % (_page_link, str(int(self.page)+1))))
1416 #nav_items.append(literal('<link rel="prefetch" href="%s?page=%s">' % (_page_link, str(int(self.page)+1))))
1416 #nav_items.append(literal('<link rel="prefetch" href="%s?page=%s">' % (_page_link, str(int(self.page)+1))))
1417 return self.separator.join(nav_items)
1417 return self.separator.join(nav_items)
1418
1418
1419 def pager(self, format='~2~', page_param='page', partial_param='partial',
1419 def pager(self, format='~2~', page_param='page', partial_param='partial',
1420 show_if_single_page=False, separator=' ', onclick=None,
1420 show_if_single_page=False, separator=' ', onclick=None,
1421 symbol_first='<<', symbol_last='>>',
1421 symbol_first='<<', symbol_last='>>',
1422 symbol_previous='<', symbol_next='>',
1422 symbol_previous='<', symbol_next='>',
1423 link_attr={'class': 'pager_link', 'rel': 'prerender'},
1423 link_attr={'class': 'pager_link', 'rel': 'prerender'},
1424 curpage_attr={'class': 'pager_curpage'},
1424 curpage_attr={'class': 'pager_curpage'},
1425 dotdot_attr={'class': 'pager_dotdot'}, **kwargs):
1425 dotdot_attr={'class': 'pager_dotdot'}, **kwargs):
1426
1426
1427 self.curpage_attr = curpage_attr
1427 self.curpage_attr = curpage_attr
1428 self.separator = separator
1428 self.separator = separator
1429 self.pager_kwargs = kwargs
1429 self.pager_kwargs = kwargs
1430 self.page_param = page_param
1430 self.page_param = page_param
1431 self.partial_param = partial_param
1431 self.partial_param = partial_param
1432 self.onclick = onclick
1432 self.onclick = onclick
1433 self.link_attr = link_attr
1433 self.link_attr = link_attr
1434 self.dotdot_attr = dotdot_attr
1434 self.dotdot_attr = dotdot_attr
1435
1435
1436 # Don't show navigator if there is no more than one page
1436 # Don't show navigator if there is no more than one page
1437 if self.page_count == 0 or (self.page_count == 1 and not show_if_single_page):
1437 if self.page_count == 0 or (self.page_count == 1 and not show_if_single_page):
1438 return ''
1438 return ''
1439
1439
1440 from string import Template
1440 from string import Template
1441 # Replace ~...~ in token format by range of pages
1441 # Replace ~...~ in token format by range of pages
1442 result = re.sub(r'~(\d+)~', self._range, format)
1442 result = re.sub(r'~(\d+)~', self._range, format)
1443
1443
1444 # Interpolate '%' variables
1444 # Interpolate '%' variables
1445 result = Template(result).safe_substitute({
1445 result = Template(result).safe_substitute({
1446 'first_page': self.first_page,
1446 'first_page': self.first_page,
1447 'last_page': self.last_page,
1447 'last_page': self.last_page,
1448 'page': self.page,
1448 'page': self.page,
1449 'page_count': self.page_count,
1449 'page_count': self.page_count,
1450 'items_per_page': self.items_per_page,
1450 'items_per_page': self.items_per_page,
1451 'first_item': self.first_item,
1451 'first_item': self.first_item,
1452 'last_item': self.last_item,
1452 'last_item': self.last_item,
1453 'item_count': self.item_count,
1453 'item_count': self.item_count,
1454 'link_first': self.page > self.first_page and \
1454 'link_first': self.page > self.first_page and \
1455 self._pagerlink(self.first_page, symbol_first) or '',
1455 self._pagerlink(self.first_page, symbol_first) or '',
1456 'link_last': self.page < self.last_page and \
1456 'link_last': self.page < self.last_page and \
1457 self._pagerlink(self.last_page, symbol_last) or '',
1457 self._pagerlink(self.last_page, symbol_last) or '',
1458 'link_previous': self.previous_page and \
1458 'link_previous': self.previous_page and \
1459 self._pagerlink(self.previous_page, symbol_previous) \
1459 self._pagerlink(self.previous_page, symbol_previous) \
1460 or HTML.span(symbol_previous, class_="pg-previous disabled"),
1460 or HTML.span(symbol_previous, class_="pg-previous disabled"),
1461 'link_next': self.next_page and \
1461 'link_next': self.next_page and \
1462 self._pagerlink(self.next_page, symbol_next) \
1462 self._pagerlink(self.next_page, symbol_next) \
1463 or HTML.span(symbol_next, class_="pg-next disabled")
1463 or HTML.span(symbol_next, class_="pg-next disabled")
1464 })
1464 })
1465
1465
1466 return literal(result)
1466 return literal(result)
1467
1467
1468
1468
1469 #==============================================================================
1469 #==============================================================================
1470 # REPO PAGER, PAGER FOR REPOSITORY
1470 # REPO PAGER, PAGER FOR REPOSITORY
1471 #==============================================================================
1471 #==============================================================================
1472 class RepoPage(Page):
1472 class RepoPage(Page):
1473
1473
1474 def __init__(self, collection, page=1, items_per_page=20,
1474 def __init__(self, collection, page=1, items_per_page=20,
1475 item_count=None, url=None, **kwargs):
1475 item_count=None, url=None, **kwargs):
1476
1476
1477 """Create a "RepoPage" instance. special pager for paging
1477 """Create a "RepoPage" instance. special pager for paging
1478 repository
1478 repository
1479 """
1479 """
1480 self._url_generator = url
1480 self._url_generator = url
1481
1481
1482 # Safe the kwargs class-wide so they can be used in the pager() method
1482 # Safe the kwargs class-wide so they can be used in the pager() method
1483 self.kwargs = kwargs
1483 self.kwargs = kwargs
1484
1484
1485 # Save a reference to the collection
1485 # Save a reference to the collection
1486 self.original_collection = collection
1486 self.original_collection = collection
1487
1487
1488 self.collection = collection
1488 self.collection = collection
1489
1489
1490 # The self.page is the number of the current page.
1490 # The self.page is the number of the current page.
1491 # The first page has the number 1!
1491 # The first page has the number 1!
1492 try:
1492 try:
1493 self.page = int(page) # make it int() if we get it as a string
1493 self.page = int(page) # make it int() if we get it as a string
1494 except (ValueError, TypeError):
1494 except (ValueError, TypeError):
1495 self.page = 1
1495 self.page = 1
1496
1496
1497 self.items_per_page = items_per_page
1497 self.items_per_page = items_per_page
1498
1498
1499 # Unless the user tells us how many items the collections has
1499 # Unless the user tells us how many items the collections has
1500 # we calculate that ourselves.
1500 # we calculate that ourselves.
1501 if item_count is not None:
1501 if item_count is not None:
1502 self.item_count = item_count
1502 self.item_count = item_count
1503 else:
1503 else:
1504 self.item_count = len(self.collection)
1504 self.item_count = len(self.collection)
1505
1505
1506 # Compute the number of the first and last available page
1506 # Compute the number of the first and last available page
1507 if self.item_count > 0:
1507 if self.item_count > 0:
1508 self.first_page = 1
1508 self.first_page = 1
1509 self.page_count = int(math.ceil(float(self.item_count) /
1509 self.page_count = int(math.ceil(float(self.item_count) /
1510 self.items_per_page))
1510 self.items_per_page))
1511 self.last_page = self.first_page + self.page_count - 1
1511 self.last_page = self.first_page + self.page_count - 1
1512
1512
1513 # Make sure that the requested page number is the range of
1513 # Make sure that the requested page number is the range of
1514 # valid pages
1514 # valid pages
1515 if self.page > self.last_page:
1515 if self.page > self.last_page:
1516 self.page = self.last_page
1516 self.page = self.last_page
1517 elif self.page < self.first_page:
1517 elif self.page < self.first_page:
1518 self.page = self.first_page
1518 self.page = self.first_page
1519
1519
1520 # Note: the number of items on this page can be less than
1520 # Note: the number of items on this page can be less than
1521 # items_per_page if the last page is not full
1521 # items_per_page if the last page is not full
1522 self.first_item = max(0, (self.item_count) - (self.page *
1522 self.first_item = max(0, (self.item_count) - (self.page *
1523 items_per_page))
1523 items_per_page))
1524 self.last_item = ((self.item_count - 1) - items_per_page *
1524 self.last_item = ((self.item_count - 1) - items_per_page *
1525 (self.page - 1))
1525 (self.page - 1))
1526
1526
1527 self.items = list(self.collection[self.first_item:self.last_item + 1])
1527 self.items = list(self.collection[self.first_item:self.last_item + 1])
1528
1528
1529 # Links to previous and next page
1529 # Links to previous and next page
1530 if self.page > self.first_page:
1530 if self.page > self.first_page:
1531 self.previous_page = self.page - 1
1531 self.previous_page = self.page - 1
1532 else:
1532 else:
1533 self.previous_page = None
1533 self.previous_page = None
1534
1534
1535 if self.page < self.last_page:
1535 if self.page < self.last_page:
1536 self.next_page = self.page + 1
1536 self.next_page = self.page + 1
1537 else:
1537 else:
1538 self.next_page = None
1538 self.next_page = None
1539
1539
1540 # No items available
1540 # No items available
1541 else:
1541 else:
1542 self.first_page = None
1542 self.first_page = None
1543 self.page_count = 0
1543 self.page_count = 0
1544 self.last_page = None
1544 self.last_page = None
1545 self.first_item = None
1545 self.first_item = None
1546 self.last_item = None
1546 self.last_item = None
1547 self.previous_page = None
1547 self.previous_page = None
1548 self.next_page = None
1548 self.next_page = None
1549 self.items = []
1549 self.items = []
1550
1550
1551 # This is a subclass of the 'list' type. Initialise the list now.
1551 # This is a subclass of the 'list' type. Initialise the list now.
1552 list.__init__(self, reversed(self.items))
1552 list.__init__(self, reversed(self.items))
1553
1553
1554
1554
1555 def breadcrumb_repo_link(repo):
1555 def breadcrumb_repo_link(repo):
1556 """
1556 """
1557 Makes a breadcrumbs path link to repo
1557 Makes a breadcrumbs path link to repo
1558
1558
1559 ex::
1559 ex::
1560 group >> subgroup >> repo
1560 group >> subgroup >> repo
1561
1561
1562 :param repo: a Repository instance
1562 :param repo: a Repository instance
1563 """
1563 """
1564
1564
1565 path = [
1565 path = [
1566 link_to(group.name, route_path('repo_group_home', repo_group_name=group.group_name))
1566 link_to(group.name, route_path('repo_group_home', repo_group_name=group.group_name))
1567 for group in repo.groups_with_parents
1567 for group in repo.groups_with_parents
1568 ] + [
1568 ] + [
1569 link_to(repo.just_name, route_path('repo_summary', repo_name=repo.repo_name))
1569 link_to(repo.just_name, route_path('repo_summary', repo_name=repo.repo_name))
1570 ]
1570 ]
1571
1571
1572 return literal(' &raquo; '.join(path))
1572 return literal(' &raquo; '.join(path))
1573
1573
1574
1574
1575 def format_byte_size_binary(file_size):
1575 def format_byte_size_binary(file_size):
1576 """
1576 """
1577 Formats file/folder sizes to standard.
1577 Formats file/folder sizes to standard.
1578 """
1578 """
1579 if file_size is None:
1579 if file_size is None:
1580 file_size = 0
1580 file_size = 0
1581
1581
1582 formatted_size = format_byte_size(file_size, binary=True)
1582 formatted_size = format_byte_size(file_size, binary=True)
1583 return formatted_size
1583 return formatted_size
1584
1584
1585
1585
1586 def urlify_text(text_, safe=True):
1586 def urlify_text(text_, safe=True):
1587 """
1587 """
1588 Extrac urls from text and make html links out of them
1588 Extrac urls from text and make html links out of them
1589
1589
1590 :param text_:
1590 :param text_:
1591 """
1591 """
1592
1592
1593 url_pat = re.compile(r'''(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@#.&+]'''
1593 url_pat = re.compile(r'''(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@#.&+]'''
1594 '''|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)''')
1594 '''|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)''')
1595
1595
1596 def url_func(match_obj):
1596 def url_func(match_obj):
1597 url_full = match_obj.groups()[0]
1597 url_full = match_obj.groups()[0]
1598 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
1598 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
1599 _newtext = url_pat.sub(url_func, text_)
1599 _newtext = url_pat.sub(url_func, text_)
1600 if safe:
1600 if safe:
1601 return literal(_newtext)
1601 return literal(_newtext)
1602 return _newtext
1602 return _newtext
1603
1603
1604
1604
1605 def urlify_commits(text_, repository):
1605 def urlify_commits(text_, repository):
1606 """
1606 """
1607 Extract commit ids from text and make link from them
1607 Extract commit ids from text and make link from them
1608
1608
1609 :param text_:
1609 :param text_:
1610 :param repository: repo name to build the URL with
1610 :param repository: repo name to build the URL with
1611 """
1611 """
1612
1612
1613 URL_PAT = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)')
1613 URL_PAT = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)')
1614
1614
1615 def url_func(match_obj):
1615 def url_func(match_obj):
1616 commit_id = match_obj.groups()[1]
1616 commit_id = match_obj.groups()[1]
1617 pref = match_obj.groups()[0]
1617 pref = match_obj.groups()[0]
1618 suf = match_obj.groups()[2]
1618 suf = match_obj.groups()[2]
1619
1619
1620 tmpl = (
1620 tmpl = (
1621 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1621 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1622 '%(commit_id)s</a>%(suf)s'
1622 '%(commit_id)s</a>%(suf)s'
1623 )
1623 )
1624 return tmpl % {
1624 return tmpl % {
1625 'pref': pref,
1625 'pref': pref,
1626 'cls': 'revision-link',
1626 'cls': 'revision-link',
1627 'url': route_url('repo_commit', repo_name=repository,
1627 'url': route_url('repo_commit', repo_name=repository,
1628 commit_id=commit_id),
1628 commit_id=commit_id),
1629 'commit_id': commit_id,
1629 'commit_id': commit_id,
1630 'suf': suf
1630 'suf': suf
1631 }
1631 }
1632
1632
1633 newtext = URL_PAT.sub(url_func, text_)
1633 newtext = URL_PAT.sub(url_func, text_)
1634
1634
1635 return newtext
1635 return newtext
1636
1636
1637
1637
1638 def _process_url_func(match_obj, repo_name, uid, entry,
1638 def _process_url_func(match_obj, repo_name, uid, entry,
1639 return_raw_data=False, link_format='html'):
1639 return_raw_data=False, link_format='html'):
1640 pref = ''
1640 pref = ''
1641 if match_obj.group().startswith(' '):
1641 if match_obj.group().startswith(' '):
1642 pref = ' '
1642 pref = ' '
1643
1643
1644 issue_id = ''.join(match_obj.groups())
1644 issue_id = ''.join(match_obj.groups())
1645
1645
1646 if link_format == 'html':
1646 if link_format == 'html':
1647 tmpl = (
1647 tmpl = (
1648 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1648 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1649 '%(issue-prefix)s%(id-repr)s'
1649 '%(issue-prefix)s%(id-repr)s'
1650 '</a>')
1650 '</a>')
1651 elif link_format == 'rst':
1651 elif link_format == 'rst':
1652 tmpl = '`%(issue-prefix)s%(id-repr)s <%(url)s>`_'
1652 tmpl = '`%(issue-prefix)s%(id-repr)s <%(url)s>`_'
1653 elif link_format == 'markdown':
1653 elif link_format == 'markdown':
1654 tmpl = '[%(issue-prefix)s%(id-repr)s](%(url)s)'
1654 tmpl = '[%(issue-prefix)s%(id-repr)s](%(url)s)'
1655 else:
1655 else:
1656 raise ValueError('Bad link_format:{}'.format(link_format))
1656 raise ValueError('Bad link_format:{}'.format(link_format))
1657
1657
1658 (repo_name_cleaned,
1658 (repo_name_cleaned,
1659 parent_group_name) = RepoGroupModel().\
1659 parent_group_name) = RepoGroupModel().\
1660 _get_group_name_and_parent(repo_name)
1660 _get_group_name_and_parent(repo_name)
1661
1661
1662 # variables replacement
1662 # variables replacement
1663 named_vars = {
1663 named_vars = {
1664 'id': issue_id,
1664 'id': issue_id,
1665 'repo': repo_name,
1665 'repo': repo_name,
1666 'repo_name': repo_name_cleaned,
1666 'repo_name': repo_name_cleaned,
1667 'group_name': parent_group_name
1667 'group_name': parent_group_name
1668 }
1668 }
1669 # named regex variables
1669 # named regex variables
1670 named_vars.update(match_obj.groupdict())
1670 named_vars.update(match_obj.groupdict())
1671 _url = string.Template(entry['url']).safe_substitute(**named_vars)
1671 _url = string.Template(entry['url']).safe_substitute(**named_vars)
1672
1672
1673 data = {
1673 data = {
1674 'pref': pref,
1674 'pref': pref,
1675 'cls': 'issue-tracker-link',
1675 'cls': 'issue-tracker-link',
1676 'url': _url,
1676 'url': _url,
1677 'id-repr': issue_id,
1677 'id-repr': issue_id,
1678 'issue-prefix': entry['pref'],
1678 'issue-prefix': entry['pref'],
1679 'serv': entry['url'],
1679 'serv': entry['url'],
1680 }
1680 }
1681 if return_raw_data:
1681 if return_raw_data:
1682 return {
1682 return {
1683 'id': issue_id,
1683 'id': issue_id,
1684 'url': _url
1684 'url': _url
1685 }
1685 }
1686 return tmpl % data
1686 return tmpl % data
1687
1687
1688
1688
1689 def get_active_pattern_entries(repo_name):
1689 def get_active_pattern_entries(repo_name):
1690 repo = None
1690 repo = None
1691 if repo_name:
1691 if repo_name:
1692 # Retrieving repo_name to avoid invalid repo_name to explode on
1692 # Retrieving repo_name to avoid invalid repo_name to explode on
1693 # IssueTrackerSettingsModel but still passing invalid name further down
1693 # IssueTrackerSettingsModel but still passing invalid name further down
1694 repo = Repository.get_by_repo_name(repo_name, cache=True)
1694 repo = Repository.get_by_repo_name(repo_name, cache=True)
1695
1695
1696 settings_model = IssueTrackerSettingsModel(repo=repo)
1696 settings_model = IssueTrackerSettingsModel(repo=repo)
1697 active_entries = settings_model.get_settings(cache=True)
1697 active_entries = settings_model.get_settings(cache=True)
1698 return active_entries
1698 return active_entries
1699
1699
1700
1700
1701 def process_patterns(text_string, repo_name, link_format='html',
1701 def process_patterns(text_string, repo_name, link_format='html',
1702 active_entries=None):
1702 active_entries=None):
1703
1703
1704 allowed_formats = ['html', 'rst', 'markdown']
1704 allowed_formats = ['html', 'rst', 'markdown']
1705 if link_format not in allowed_formats:
1705 if link_format not in allowed_formats:
1706 raise ValueError('Link format can be only one of:{} got {}'.format(
1706 raise ValueError('Link format can be only one of:{} got {}'.format(
1707 allowed_formats, link_format))
1707 allowed_formats, link_format))
1708
1708
1709 active_entries = active_entries or get_active_pattern_entries(repo_name)
1709 active_entries = active_entries or get_active_pattern_entries(repo_name)
1710 issues_data = []
1710 issues_data = []
1711 newtext = text_string
1711 newtext = text_string
1712
1712
1713 for uid, entry in active_entries.items():
1713 for uid, entry in active_entries.items():
1714 log.debug('found issue tracker entry with uid %s' % (uid,))
1714 log.debug('found issue tracker entry with uid %s' % (uid,))
1715
1715
1716 if not (entry['pat'] and entry['url']):
1716 if not (entry['pat'] and entry['url']):
1717 log.debug('skipping due to missing data')
1717 log.debug('skipping due to missing data')
1718 continue
1718 continue
1719
1719
1720 log.debug('issue tracker entry: uid: `%s` PAT:%s URL:%s PREFIX:%s'
1720 log.debug('issue tracker entry: uid: `%s` PAT:%s URL:%s PREFIX:%s'
1721 % (uid, entry['pat'], entry['url'], entry['pref']))
1721 % (uid, entry['pat'], entry['url'], entry['pref']))
1722
1722
1723 try:
1723 try:
1724 pattern = re.compile(r'%s' % entry['pat'])
1724 pattern = re.compile(r'%s' % entry['pat'])
1725 except re.error:
1725 except re.error:
1726 log.exception(
1726 log.exception(
1727 'issue tracker pattern: `%s` failed to compile',
1727 'issue tracker pattern: `%s` failed to compile',
1728 entry['pat'])
1728 entry['pat'])
1729 continue
1729 continue
1730
1730
1731 data_func = partial(
1731 data_func = partial(
1732 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
1732 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
1733 return_raw_data=True)
1733 return_raw_data=True)
1734
1734
1735 for match_obj in pattern.finditer(text_string):
1735 for match_obj in pattern.finditer(text_string):
1736 issues_data.append(data_func(match_obj))
1736 issues_data.append(data_func(match_obj))
1737
1737
1738 url_func = partial(
1738 url_func = partial(
1739 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
1739 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
1740 link_format=link_format)
1740 link_format=link_format)
1741
1741
1742 newtext = pattern.sub(url_func, newtext)
1742 newtext = pattern.sub(url_func, newtext)
1743 log.debug('processed prefix:uid `%s`' % (uid,))
1743 log.debug('processed prefix:uid `%s`' % (uid,))
1744
1744
1745 return newtext, issues_data
1745 return newtext, issues_data
1746
1746
1747
1747
1748 def urlify_commit_message(commit_text, repository=None,
1748 def urlify_commit_message(commit_text, repository=None,
1749 active_pattern_entries=None):
1749 active_pattern_entries=None):
1750 """
1750 """
1751 Parses given text message and makes proper links.
1751 Parses given text message and makes proper links.
1752 issues are linked to given issue-server, and rest is a commit link
1752 issues are linked to given issue-server, and rest is a commit link
1753
1753
1754 :param commit_text:
1754 :param commit_text:
1755 :param repository:
1755 :param repository:
1756 """
1756 """
1757 def escaper(string):
1757 def escaper(string):
1758 return string.replace('<', '&lt;').replace('>', '&gt;')
1758 return string.replace('<', '&lt;').replace('>', '&gt;')
1759
1759
1760 newtext = escaper(commit_text)
1760 newtext = escaper(commit_text)
1761
1761
1762 # extract http/https links and make them real urls
1762 # extract http/https links and make them real urls
1763 newtext = urlify_text(newtext, safe=False)
1763 newtext = urlify_text(newtext, safe=False)
1764
1764
1765 # urlify commits - extract commit ids and make link out of them, if we have
1765 # urlify commits - extract commit ids and make link out of them, if we have
1766 # the scope of repository present.
1766 # the scope of repository present.
1767 if repository:
1767 if repository:
1768 newtext = urlify_commits(newtext, repository)
1768 newtext = urlify_commits(newtext, repository)
1769
1769
1770 # process issue tracker patterns
1770 # process issue tracker patterns
1771 newtext, issues = process_patterns(newtext, repository or '',
1771 newtext, issues = process_patterns(newtext, repository or '',
1772 active_entries=active_pattern_entries)
1772 active_entries=active_pattern_entries)
1773
1773
1774 return literal(newtext)
1774 return literal(newtext)
1775
1775
1776
1776
1777 def render_binary(repo_name, file_obj):
1777 def render_binary(repo_name, file_obj):
1778 """
1778 """
1779 Choose how to render a binary file
1779 Choose how to render a binary file
1780 """
1780 """
1781 filename = file_obj.name
1781 filename = file_obj.name
1782
1782
1783 # images
1783 # images
1784 for ext in ['*.png', '*.jpg', '*.ico', '*.gif']:
1784 for ext in ['*.png', '*.jpg', '*.ico', '*.gif']:
1785 if fnmatch.fnmatch(filename, pat=ext):
1785 if fnmatch.fnmatch(filename, pat=ext):
1786 alt = filename
1786 alt = filename
1787 src = route_path(
1787 src = route_path(
1788 'repo_file_raw', repo_name=repo_name,
1788 'repo_file_raw', repo_name=repo_name,
1789 commit_id=file_obj.commit.raw_id, f_path=file_obj.path)
1789 commit_id=file_obj.commit.raw_id, f_path=file_obj.path)
1790 return literal('<img class="rendered-binary" alt="{}" src="{}">'.format(alt, src))
1790 return literal('<img class="rendered-binary" alt="{}" src="{}">'.format(alt, src))
1791
1791
1792
1792
1793 def renderer_from_filename(filename, exclude=None):
1793 def renderer_from_filename(filename, exclude=None):
1794 """
1794 """
1795 choose a renderer based on filename, this works only for text based files
1795 choose a renderer based on filename, this works only for text based files
1796 """
1796 """
1797
1797
1798 # ipython
1798 # ipython
1799 for ext in ['*.ipynb']:
1799 for ext in ['*.ipynb']:
1800 if fnmatch.fnmatch(filename, pat=ext):
1800 if fnmatch.fnmatch(filename, pat=ext):
1801 return 'jupyter'
1801 return 'jupyter'
1802
1802
1803 is_markup = MarkupRenderer.renderer_from_filename(filename, exclude=exclude)
1803 is_markup = MarkupRenderer.renderer_from_filename(filename, exclude=exclude)
1804 if is_markup:
1804 if is_markup:
1805 return is_markup
1805 return is_markup
1806 return None
1806 return None
1807
1807
1808
1808
1809 def render(source, renderer='rst', mentions=False, relative_urls=None,
1809 def render(source, renderer='rst', mentions=False, relative_urls=None,
1810 repo_name=None):
1810 repo_name=None):
1811
1811
1812 def maybe_convert_relative_links(html_source):
1812 def maybe_convert_relative_links(html_source):
1813 if relative_urls:
1813 if relative_urls:
1814 return relative_links(html_source, relative_urls)
1814 return relative_links(html_source, relative_urls)
1815 return html_source
1815 return html_source
1816
1816
1817 if renderer == 'rst':
1817 if renderer == 'plain':
1818 return literal(
1819 MarkupRenderer.plain(source, leading_newline=False))
1820
1821 elif renderer == 'rst':
1818 if repo_name:
1822 if repo_name:
1819 # process patterns on comments if we pass in repo name
1823 # process patterns on comments if we pass in repo name
1820 source, issues = process_patterns(
1824 source, issues = process_patterns(
1821 source, repo_name, link_format='rst')
1825 source, repo_name, link_format='rst')
1822
1826
1823 return literal(
1827 return literal(
1824 '<div class="rst-block">%s</div>' %
1828 '<div class="rst-block">%s</div>' %
1825 maybe_convert_relative_links(
1829 maybe_convert_relative_links(
1826 MarkupRenderer.rst(source, mentions=mentions)))
1830 MarkupRenderer.rst(source, mentions=mentions)))
1831
1827 elif renderer == 'markdown':
1832 elif renderer == 'markdown':
1828 if repo_name:
1833 if repo_name:
1829 # process patterns on comments if we pass in repo name
1834 # process patterns on comments if we pass in repo name
1830 source, issues = process_patterns(
1835 source, issues = process_patterns(
1831 source, repo_name, link_format='markdown')
1836 source, repo_name, link_format='markdown')
1832
1837
1833 return literal(
1838 return literal(
1834 '<div class="markdown-block">%s</div>' %
1839 '<div class="markdown-block">%s</div>' %
1835 maybe_convert_relative_links(
1840 maybe_convert_relative_links(
1836 MarkupRenderer.markdown(source, flavored=True,
1841 MarkupRenderer.markdown(source, flavored=True,
1837 mentions=mentions)))
1842 mentions=mentions)))
1843
1838 elif renderer == 'jupyter':
1844 elif renderer == 'jupyter':
1839 return literal(
1845 return literal(
1840 '<div class="ipynb">%s</div>' %
1846 '<div class="ipynb">%s</div>' %
1841 maybe_convert_relative_links(
1847 maybe_convert_relative_links(
1842 MarkupRenderer.jupyter(source)))
1848 MarkupRenderer.jupyter(source)))
1843
1849
1844 # None means just show the file-source
1850 # None means just show the file-source
1845 return None
1851 return None
1846
1852
1847
1853
1848 def commit_status(repo, commit_id):
1854 def commit_status(repo, commit_id):
1849 return ChangesetStatusModel().get_status(repo, commit_id)
1855 return ChangesetStatusModel().get_status(repo, commit_id)
1850
1856
1851
1857
1852 def commit_status_lbl(commit_status):
1858 def commit_status_lbl(commit_status):
1853 return dict(ChangesetStatus.STATUSES).get(commit_status)
1859 return dict(ChangesetStatus.STATUSES).get(commit_status)
1854
1860
1855
1861
1856 def commit_time(repo_name, commit_id):
1862 def commit_time(repo_name, commit_id):
1857 repo = Repository.get_by_repo_name(repo_name)
1863 repo = Repository.get_by_repo_name(repo_name)
1858 commit = repo.get_commit(commit_id=commit_id)
1864 commit = repo.get_commit(commit_id=commit_id)
1859 return commit.date
1865 return commit.date
1860
1866
1861
1867
1862 def get_permission_name(key):
1868 def get_permission_name(key):
1863 return dict(Permission.PERMS).get(key)
1869 return dict(Permission.PERMS).get(key)
1864
1870
1865
1871
1866 def journal_filter_help(request):
1872 def journal_filter_help(request):
1867 _ = request.translate
1873 _ = request.translate
1868 from rhodecode.lib.audit_logger import ACTIONS
1874 from rhodecode.lib.audit_logger import ACTIONS
1869 actions = '\n'.join(textwrap.wrap(', '.join(sorted(ACTIONS.keys())), 80))
1875 actions = '\n'.join(textwrap.wrap(', '.join(sorted(ACTIONS.keys())), 80))
1870
1876
1871 return _(
1877 return _(
1872 'Example filter terms:\n' +
1878 'Example filter terms:\n' +
1873 ' repository:vcs\n' +
1879 ' repository:vcs\n' +
1874 ' username:marcin\n' +
1880 ' username:marcin\n' +
1875 ' username:(NOT marcin)\n' +
1881 ' username:(NOT marcin)\n' +
1876 ' action:*push*\n' +
1882 ' action:*push*\n' +
1877 ' ip:127.0.0.1\n' +
1883 ' ip:127.0.0.1\n' +
1878 ' date:20120101\n' +
1884 ' date:20120101\n' +
1879 ' date:[20120101100000 TO 20120102]\n' +
1885 ' date:[20120101100000 TO 20120102]\n' +
1880 '\n' +
1886 '\n' +
1881 'Actions: {actions}\n' +
1887 'Actions: {actions}\n' +
1882 '\n' +
1888 '\n' +
1883 'Generate wildcards using \'*\' character:\n' +
1889 'Generate wildcards using \'*\' character:\n' +
1884 ' "repository:vcs*" - search everything starting with \'vcs\'\n' +
1890 ' "repository:vcs*" - search everything starting with \'vcs\'\n' +
1885 ' "repository:*vcs*" - search for repository containing \'vcs\'\n' +
1891 ' "repository:*vcs*" - search for repository containing \'vcs\'\n' +
1886 '\n' +
1892 '\n' +
1887 'Optional AND / OR operators in queries\n' +
1893 'Optional AND / OR operators in queries\n' +
1888 ' "repository:vcs OR repository:test"\n' +
1894 ' "repository:vcs OR repository:test"\n' +
1889 ' "username:test AND repository:test*"\n'
1895 ' "username:test AND repository:test*"\n'
1890 ).format(actions=actions)
1896 ).format(actions=actions)
1891
1897
1892
1898
1893 def search_filter_help(searcher, request):
1899 def search_filter_help(searcher, request):
1894 _ = request.translate
1900 _ = request.translate
1895
1901
1896 terms = ''
1902 terms = ''
1897 return _(
1903 return _(
1898 'Example filter terms for `{searcher}` search:\n' +
1904 'Example filter terms for `{searcher}` search:\n' +
1899 '{terms}\n' +
1905 '{terms}\n' +
1900 'Generate wildcards using \'*\' character:\n' +
1906 'Generate wildcards using \'*\' character:\n' +
1901 ' "repo_name:vcs*" - search everything starting with \'vcs\'\n' +
1907 ' "repo_name:vcs*" - search everything starting with \'vcs\'\n' +
1902 ' "repo_name:*vcs*" - search for repository containing \'vcs\'\n' +
1908 ' "repo_name:*vcs*" - search for repository containing \'vcs\'\n' +
1903 '\n' +
1909 '\n' +
1904 'Optional AND / OR operators in queries\n' +
1910 'Optional AND / OR operators in queries\n' +
1905 ' "repo_name:vcs OR repo_name:test"\n' +
1911 ' "repo_name:vcs OR repo_name:test"\n' +
1906 ' "owner:test AND repo_name:test*"\n' +
1912 ' "owner:test AND repo_name:test*"\n' +
1907 'More: {search_doc}'
1913 'More: {search_doc}'
1908 ).format(searcher=searcher.name,
1914 ).format(searcher=searcher.name,
1909 terms=terms, search_doc=searcher.query_lang_doc)
1915 terms=terms, search_doc=searcher.query_lang_doc)
1910
1916
1911
1917
1912 def not_mapped_error(repo_name):
1918 def not_mapped_error(repo_name):
1913 from rhodecode.translation import _
1919 from rhodecode.translation import _
1914 flash(_('%s repository is not mapped to db perhaps'
1920 flash(_('%s repository is not mapped to db perhaps'
1915 ' it was created or renamed from the filesystem'
1921 ' it was created or renamed from the filesystem'
1916 ' please run the application again'
1922 ' please run the application again'
1917 ' in order to rescan repositories') % repo_name, category='error')
1923 ' in order to rescan repositories') % repo_name, category='error')
1918
1924
1919
1925
1920 def ip_range(ip_addr):
1926 def ip_range(ip_addr):
1921 from rhodecode.model.db import UserIpMap
1927 from rhodecode.model.db import UserIpMap
1922 s, e = UserIpMap._get_ip_range(ip_addr)
1928 s, e = UserIpMap._get_ip_range(ip_addr)
1923 return '%s - %s' % (s, e)
1929 return '%s - %s' % (s, e)
1924
1930
1925
1931
1926 def form(url, method='post', needs_csrf_token=True, **attrs):
1932 def form(url, method='post', needs_csrf_token=True, **attrs):
1927 """Wrapper around webhelpers.tags.form to prevent CSRF attacks."""
1933 """Wrapper around webhelpers.tags.form to prevent CSRF attacks."""
1928 if method.lower() != 'get' and needs_csrf_token:
1934 if method.lower() != 'get' and needs_csrf_token:
1929 raise Exception(
1935 raise Exception(
1930 'Forms to POST/PUT/DELETE endpoints should have (in general) a ' +
1936 'Forms to POST/PUT/DELETE endpoints should have (in general) a ' +
1931 'CSRF token. If the endpoint does not require such token you can ' +
1937 'CSRF token. If the endpoint does not require such token you can ' +
1932 'explicitly set the parameter needs_csrf_token to false.')
1938 'explicitly set the parameter needs_csrf_token to false.')
1933
1939
1934 return wh_form(url, method=method, **attrs)
1940 return wh_form(url, method=method, **attrs)
1935
1941
1936
1942
1937 def secure_form(form_url, method="POST", multipart=False, **attrs):
1943 def secure_form(form_url, method="POST", multipart=False, **attrs):
1938 """Start a form tag that points the action to an url. This
1944 """Start a form tag that points the action to an url. This
1939 form tag will also include the hidden field containing
1945 form tag will also include the hidden field containing
1940 the auth token.
1946 the auth token.
1941
1947
1942 The url options should be given either as a string, or as a
1948 The url options should be given either as a string, or as a
1943 ``url()`` function. The method for the form defaults to POST.
1949 ``url()`` function. The method for the form defaults to POST.
1944
1950
1945 Options:
1951 Options:
1946
1952
1947 ``multipart``
1953 ``multipart``
1948 If set to True, the enctype is set to "multipart/form-data".
1954 If set to True, the enctype is set to "multipart/form-data".
1949 ``method``
1955 ``method``
1950 The method to use when submitting the form, usually either
1956 The method to use when submitting the form, usually either
1951 "GET" or "POST". If "PUT", "DELETE", or another verb is used, a
1957 "GET" or "POST". If "PUT", "DELETE", or another verb is used, a
1952 hidden input with name _method is added to simulate the verb
1958 hidden input with name _method is added to simulate the verb
1953 over POST.
1959 over POST.
1954
1960
1955 """
1961 """
1956 from webhelpers.pylonslib.secure_form import insecure_form
1962 from webhelpers.pylonslib.secure_form import insecure_form
1957
1963
1958 if 'request' in attrs:
1964 if 'request' in attrs:
1959 session = attrs['request'].session
1965 session = attrs['request'].session
1960 del attrs['request']
1966 del attrs['request']
1961 else:
1967 else:
1962 raise ValueError(
1968 raise ValueError(
1963 'Calling this form requires request= to be passed as argument')
1969 'Calling this form requires request= to be passed as argument')
1964
1970
1965 form = insecure_form(form_url, method, multipart, **attrs)
1971 form = insecure_form(form_url, method, multipart, **attrs)
1966 token = literal(
1972 token = literal(
1967 '<input type="hidden" id="{}" name="{}" value="{}">'.format(
1973 '<input type="hidden" id="{}" name="{}" value="{}">'.format(
1968 csrf_token_key, csrf_token_key, get_csrf_token(session)))
1974 csrf_token_key, csrf_token_key, get_csrf_token(session)))
1969
1975
1970 return literal("%s\n%s" % (form, token))
1976 return literal("%s\n%s" % (form, token))
1971
1977
1972
1978
1973 def dropdownmenu(name, selected, options, enable_filter=False, **attrs):
1979 def dropdownmenu(name, selected, options, enable_filter=False, **attrs):
1974 select_html = select(name, selected, options, **attrs)
1980 select_html = select(name, selected, options, **attrs)
1975 select2 = """
1981 select2 = """
1976 <script>
1982 <script>
1977 $(document).ready(function() {
1983 $(document).ready(function() {
1978 $('#%s').select2({
1984 $('#%s').select2({
1979 containerCssClass: 'drop-menu',
1985 containerCssClass: 'drop-menu',
1980 dropdownCssClass: 'drop-menu-dropdown',
1986 dropdownCssClass: 'drop-menu-dropdown',
1981 dropdownAutoWidth: true%s
1987 dropdownAutoWidth: true%s
1982 });
1988 });
1983 });
1989 });
1984 </script>
1990 </script>
1985 """
1991 """
1986 filter_option = """,
1992 filter_option = """,
1987 minimumResultsForSearch: -1
1993 minimumResultsForSearch: -1
1988 """
1994 """
1989 input_id = attrs.get('id') or name
1995 input_id = attrs.get('id') or name
1990 filter_enabled = "" if enable_filter else filter_option
1996 filter_enabled = "" if enable_filter else filter_option
1991 select_script = literal(select2 % (input_id, filter_enabled))
1997 select_script = literal(select2 % (input_id, filter_enabled))
1992
1998
1993 return literal(select_html+select_script)
1999 return literal(select_html+select_script)
1994
2000
1995
2001
1996 def get_visual_attr(tmpl_context_var, attr_name):
2002 def get_visual_attr(tmpl_context_var, attr_name):
1997 """
2003 """
1998 A safe way to get a variable from visual variable of template context
2004 A safe way to get a variable from visual variable of template context
1999
2005
2000 :param tmpl_context_var: instance of tmpl_context, usually present as `c`
2006 :param tmpl_context_var: instance of tmpl_context, usually present as `c`
2001 :param attr_name: name of the attribute we fetch from the c.visual
2007 :param attr_name: name of the attribute we fetch from the c.visual
2002 """
2008 """
2003 visual = getattr(tmpl_context_var, 'visual', None)
2009 visual = getattr(tmpl_context_var, 'visual', None)
2004 if not visual:
2010 if not visual:
2005 return
2011 return
2006 else:
2012 else:
2007 return getattr(visual, attr_name, None)
2013 return getattr(visual, attr_name, None)
2008
2014
2009
2015
2010 def get_last_path_part(file_node):
2016 def get_last_path_part(file_node):
2011 if not file_node.path:
2017 if not file_node.path:
2012 return u''
2018 return u''
2013
2019
2014 path = safe_unicode(file_node.path.split('/')[-1])
2020 path = safe_unicode(file_node.path.split('/')[-1])
2015 return u'../' + path
2021 return u'../' + path
2016
2022
2017
2023
2018 def route_url(*args, **kwargs):
2024 def route_url(*args, **kwargs):
2019 """
2025 """
2020 Wrapper around pyramids `route_url` (fully qualified url) function.
2026 Wrapper around pyramids `route_url` (fully qualified url) function.
2021 """
2027 """
2022 req = get_current_request()
2028 req = get_current_request()
2023 return req.route_url(*args, **kwargs)
2029 return req.route_url(*args, **kwargs)
2024
2030
2025
2031
2026 def route_path(*args, **kwargs):
2032 def route_path(*args, **kwargs):
2027 """
2033 """
2028 Wrapper around pyramids `route_path` function.
2034 Wrapper around pyramids `route_path` function.
2029 """
2035 """
2030 req = get_current_request()
2036 req = get_current_request()
2031 return req.route_path(*args, **kwargs)
2037 return req.route_path(*args, **kwargs)
2032
2038
2033
2039
2034 def route_path_or_none(*args, **kwargs):
2040 def route_path_or_none(*args, **kwargs):
2035 try:
2041 try:
2036 return route_path(*args, **kwargs)
2042 return route_path(*args, **kwargs)
2037 except KeyError:
2043 except KeyError:
2038 return None
2044 return None
2039
2045
2040
2046
2041 def current_route_path(request, **kw):
2047 def current_route_path(request, **kw):
2042 new_args = request.GET.mixed()
2048 new_args = request.GET.mixed()
2043 new_args.update(kw)
2049 new_args.update(kw)
2044 return request.current_route_path(_query=new_args)
2050 return request.current_route_path(_query=new_args)
2045
2051
2046
2052
2047 def api_call_example(method, args):
2053 def api_call_example(method, args):
2048 """
2054 """
2049 Generates an API call example via CURL
2055 Generates an API call example via CURL
2050 """
2056 """
2051 args_json = json.dumps(OrderedDict([
2057 args_json = json.dumps(OrderedDict([
2052 ('id', 1),
2058 ('id', 1),
2053 ('auth_token', 'SECRET'),
2059 ('auth_token', 'SECRET'),
2054 ('method', method),
2060 ('method', method),
2055 ('args', args)
2061 ('args', args)
2056 ]))
2062 ]))
2057 return literal(
2063 return literal(
2058 "curl {api_url} -X POST -H 'content-type:text/plain' --data-binary '{data}'"
2064 "curl {api_url} -X POST -H 'content-type:text/plain' --data-binary '{data}'"
2059 "<br/><br/>SECRET can be found in <a href=\"{token_url}\">auth-tokens</a> page, "
2065 "<br/><br/>SECRET can be found in <a href=\"{token_url}\">auth-tokens</a> page, "
2060 "and needs to be of `api calls` role."
2066 "and needs to be of `api calls` role."
2061 .format(
2067 .format(
2062 api_url=route_url('apiv2'),
2068 api_url=route_url('apiv2'),
2063 token_url=route_url('my_account_auth_tokens'),
2069 token_url=route_url('my_account_auth_tokens'),
2064 data=args_json))
2070 data=args_json))
2065
2071
2066
2072
2067 def notification_description(notification, request):
2073 def notification_description(notification, request):
2068 """
2074 """
2069 Generate notification human readable description based on notification type
2075 Generate notification human readable description based on notification type
2070 """
2076 """
2071 from rhodecode.model.notification import NotificationModel
2077 from rhodecode.model.notification import NotificationModel
2072 return NotificationModel().make_description(
2078 return NotificationModel().make_description(
2073 notification, translate=request.translate)
2079 notification, translate=request.translate)
2074
2080
2075
2081
2076 def go_import_header(request, db_repo=None):
2082 def go_import_header(request, db_repo=None):
2077 """
2083 """
2078 Creates a header for go-import functionality in Go Lang
2084 Creates a header for go-import functionality in Go Lang
2079 """
2085 """
2080
2086
2081 if not db_repo:
2087 if not db_repo:
2082 return
2088 return
2083 if 'go-get' not in request.GET:
2089 if 'go-get' not in request.GET:
2084 return
2090 return
2085
2091
2086 clone_url = db_repo.clone_url()
2092 clone_url = db_repo.clone_url()
2087 prefix = re.split(r'^https?:\/\/', clone_url)[-1]
2093 prefix = re.split(r'^https?:\/\/', clone_url)[-1]
2088 # we have a repo and go-get flag,
2094 # we have a repo and go-get flag,
2089 return literal('<meta name="go-import" content="{} {} {}">'.format(
2095 return literal('<meta name="go-import" content="{} {} {}">'.format(
2090 prefix, db_repo.repo_type, clone_url))
2096 prefix, db_repo.repo_type, clone_url))
2091
2097
2092
2098
2093 def reviewer_as_json(*args, **kwargs):
2099 def reviewer_as_json(*args, **kwargs):
2094 from rhodecode.apps.repository.utils import reviewer_as_json as _reviewer_as_json
2100 from rhodecode.apps.repository.utils import reviewer_as_json as _reviewer_as_json
2095 return _reviewer_as_json(*args, **kwargs)
2101 return _reviewer_as_json(*args, **kwargs)
@@ -1,515 +1,519 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2018 RhodeCode GmbH
3 # Copyright (C) 2011-2018 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 Renderer for markup languages with ability to parse using rst or markdown
23 Renderer for markup languages with ability to parse using rst or markdown
24 """
24 """
25
25
26 import re
26 import re
27 import os
27 import os
28 import lxml
28 import lxml
29 import logging
29 import logging
30 import urlparse
30 import urlparse
31 import bleach
31 import bleach
32
32
33 from mako.lookup import TemplateLookup
33 from mako.lookup import TemplateLookup
34 from mako.template import Template as MakoTemplate
34 from mako.template import Template as MakoTemplate
35
35
36 from docutils.core import publish_parts
36 from docutils.core import publish_parts
37 from docutils.parsers.rst import directives
37 from docutils.parsers.rst import directives
38 from docutils import writers
38 from docutils import writers
39 from docutils.writers import html4css1
39 from docutils.writers import html4css1
40 import markdown
40 import markdown
41
41
42 from rhodecode.lib.markdown_ext import GithubFlavoredMarkdownExtension
42 from rhodecode.lib.markdown_ext import GithubFlavoredMarkdownExtension
43 from rhodecode.lib.utils2 import (
43 from rhodecode.lib.utils2 import (
44 safe_str, safe_unicode, md5_safe, MENTIONS_REGEX)
44 safe_str, safe_unicode, md5_safe, MENTIONS_REGEX)
45
45
46 log = logging.getLogger(__name__)
46 log = logging.getLogger(__name__)
47
47
48 # default renderer used to generate automated comments
48 # default renderer used to generate automated comments
49 DEFAULT_COMMENTS_RENDERER = 'rst'
49 DEFAULT_COMMENTS_RENDERER = 'rst'
50
50
51
51
52 class CustomHTMLTranslator(writers.html4css1.HTMLTranslator):
52 class CustomHTMLTranslator(writers.html4css1.HTMLTranslator):
53 """
53 """
54 Custom HTML Translator used for sandboxing potential
54 Custom HTML Translator used for sandboxing potential
55 JS injections in ref links
55 JS injections in ref links
56 """
56 """
57
57
58 def visit_reference(self, node):
58 def visit_reference(self, node):
59 if 'refuri' in node.attributes:
59 if 'refuri' in node.attributes:
60 refuri = node['refuri']
60 refuri = node['refuri']
61 if ':' in refuri:
61 if ':' in refuri:
62 prefix, link = refuri.lstrip().split(':', 1)
62 prefix, link = refuri.lstrip().split(':', 1)
63 if prefix == 'javascript':
63 if prefix == 'javascript':
64 # we don't allow javascript type of refs...
64 # we don't allow javascript type of refs...
65 node['refuri'] = 'javascript:alert("SandBoxedJavascript")'
65 node['refuri'] = 'javascript:alert("SandBoxedJavascript")'
66
66
67 # old style class requires this...
67 # old style class requires this...
68 return html4css1.HTMLTranslator.visit_reference(self, node)
68 return html4css1.HTMLTranslator.visit_reference(self, node)
69
69
70
70
71 class RhodeCodeWriter(writers.html4css1.Writer):
71 class RhodeCodeWriter(writers.html4css1.Writer):
72 def __init__(self):
72 def __init__(self):
73 writers.Writer.__init__(self)
73 writers.Writer.__init__(self)
74 self.translator_class = CustomHTMLTranslator
74 self.translator_class = CustomHTMLTranslator
75
75
76
76
77 def relative_links(html_source, server_paths):
77 def relative_links(html_source, server_paths):
78 if not html_source:
78 if not html_source:
79 return html_source
79 return html_source
80
80
81 try:
81 try:
82 from lxml.html import fromstring
82 from lxml.html import fromstring
83 from lxml.html import tostring
83 from lxml.html import tostring
84 except ImportError:
84 except ImportError:
85 log.exception('Failed to import lxml')
85 log.exception('Failed to import lxml')
86 return html_source
86 return html_source
87
87
88 try:
88 try:
89 doc = lxml.html.fromstring(html_source)
89 doc = lxml.html.fromstring(html_source)
90 except Exception:
90 except Exception:
91 return html_source
91 return html_source
92
92
93 for el in doc.cssselect('img, video'):
93 for el in doc.cssselect('img, video'):
94 src = el.attrib.get('src')
94 src = el.attrib.get('src')
95 if src:
95 if src:
96 el.attrib['src'] = relative_path(src, server_paths['raw'])
96 el.attrib['src'] = relative_path(src, server_paths['raw'])
97
97
98 for el in doc.cssselect('a:not(.gfm)'):
98 for el in doc.cssselect('a:not(.gfm)'):
99 src = el.attrib.get('href')
99 src = el.attrib.get('href')
100 if src:
100 if src:
101 raw_mode = el.attrib['href'].endswith('?raw=1')
101 raw_mode = el.attrib['href'].endswith('?raw=1')
102 if raw_mode:
102 if raw_mode:
103 el.attrib['href'] = relative_path(src, server_paths['raw'])
103 el.attrib['href'] = relative_path(src, server_paths['raw'])
104 else:
104 else:
105 el.attrib['href'] = relative_path(src, server_paths['standard'])
105 el.attrib['href'] = relative_path(src, server_paths['standard'])
106
106
107 return lxml.html.tostring(doc)
107 return lxml.html.tostring(doc)
108
108
109
109
110 def relative_path(path, request_path, is_repo_file=None):
110 def relative_path(path, request_path, is_repo_file=None):
111 """
111 """
112 relative link support, path is a rel path, and request_path is current
112 relative link support, path is a rel path, and request_path is current
113 server path (not absolute)
113 server path (not absolute)
114
114
115 e.g.
115 e.g.
116
116
117 path = '../logo.png'
117 path = '../logo.png'
118 request_path= '/repo/files/path/file.md'
118 request_path= '/repo/files/path/file.md'
119 produces: '/repo/files/logo.png'
119 produces: '/repo/files/logo.png'
120 """
120 """
121 # TODO(marcink): unicode/str support ?
121 # TODO(marcink): unicode/str support ?
122 # maybe=> safe_unicode(urllib.quote(safe_str(final_path), '/:'))
122 # maybe=> safe_unicode(urllib.quote(safe_str(final_path), '/:'))
123
123
124 def dummy_check(p):
124 def dummy_check(p):
125 return True # assume default is a valid file path
125 return True # assume default is a valid file path
126
126
127 is_repo_file = is_repo_file or dummy_check
127 is_repo_file = is_repo_file or dummy_check
128 if not path:
128 if not path:
129 return request_path
129 return request_path
130
130
131 path = safe_unicode(path)
131 path = safe_unicode(path)
132 request_path = safe_unicode(request_path)
132 request_path = safe_unicode(request_path)
133
133
134 if path.startswith((u'data:', u'javascript:', u'#', u':')):
134 if path.startswith((u'data:', u'javascript:', u'#', u':')):
135 # skip data, anchor, invalid links
135 # skip data, anchor, invalid links
136 return path
136 return path
137
137
138 is_absolute = bool(urlparse.urlparse(path).netloc)
138 is_absolute = bool(urlparse.urlparse(path).netloc)
139 if is_absolute:
139 if is_absolute:
140 return path
140 return path
141
141
142 if not request_path:
142 if not request_path:
143 return path
143 return path
144
144
145 if path.startswith(u'/'):
145 if path.startswith(u'/'):
146 path = path[1:]
146 path = path[1:]
147
147
148 if path.startswith(u'./'):
148 if path.startswith(u'./'):
149 path = path[2:]
149 path = path[2:]
150
150
151 parts = request_path.split('/')
151 parts = request_path.split('/')
152 # compute how deep we need to traverse the request_path
152 # compute how deep we need to traverse the request_path
153 depth = 0
153 depth = 0
154
154
155 if is_repo_file(request_path):
155 if is_repo_file(request_path):
156 # if request path is a VALID file, we use a relative path with
156 # if request path is a VALID file, we use a relative path with
157 # one level up
157 # one level up
158 depth += 1
158 depth += 1
159
159
160 while path.startswith(u'../'):
160 while path.startswith(u'../'):
161 depth += 1
161 depth += 1
162 path = path[3:]
162 path = path[3:]
163
163
164 if depth > 0:
164 if depth > 0:
165 parts = parts[:-depth]
165 parts = parts[:-depth]
166
166
167 parts.append(path)
167 parts.append(path)
168 final_path = u'/'.join(parts).lstrip(u'/')
168 final_path = u'/'.join(parts).lstrip(u'/')
169
169
170 return u'/' + final_path
170 return u'/' + final_path
171
171
172
172
173 class MarkupRenderer(object):
173 class MarkupRenderer(object):
174 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
174 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
175
175
176 MARKDOWN_PAT = re.compile(r'\.(md|mkdn?|mdown|markdown)$', re.IGNORECASE)
176 MARKDOWN_PAT = re.compile(r'\.(md|mkdn?|mdown|markdown)$', re.IGNORECASE)
177 RST_PAT = re.compile(r'\.re?st$', re.IGNORECASE)
177 RST_PAT = re.compile(r'\.re?st$', re.IGNORECASE)
178 JUPYTER_PAT = re.compile(r'\.(ipynb)$', re.IGNORECASE)
178 JUPYTER_PAT = re.compile(r'\.(ipynb)$', re.IGNORECASE)
179 PLAIN_PAT = re.compile(r'^readme$', re.IGNORECASE)
179 PLAIN_PAT = re.compile(r'^readme$', re.IGNORECASE)
180
180
181 URL_PAT = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]'
181 URL_PAT = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]'
182 r'|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
182 r'|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
183
183
184 extensions = ['codehilite', 'extra', 'def_list', 'sane_lists']
184 extensions = ['codehilite', 'extra', 'def_list', 'sane_lists']
185 output_format = 'html4'
185 output_format = 'html4'
186 markdown_renderer = markdown.Markdown(
186 markdown_renderer = markdown.Markdown(
187 extensions, enable_attributes=False, output_format=output_format)
187 extensions, enable_attributes=False, output_format=output_format)
188
188
189 markdown_renderer_flavored = markdown.Markdown(
189 markdown_renderer_flavored = markdown.Markdown(
190 extensions + [GithubFlavoredMarkdownExtension()],
190 extensions + [GithubFlavoredMarkdownExtension()],
191 enable_attributes=False, output_format=output_format)
191 enable_attributes=False, output_format=output_format)
192
192
193 # extension together with weights. Lower is first means we control how
193 # extension together with weights. Lower is first means we control how
194 # extensions are attached to readme names with those.
194 # extensions are attached to readme names with those.
195 PLAIN_EXTS = [
195 PLAIN_EXTS = [
196 # prefer no extension
196 # prefer no extension
197 ('', 0), # special case that renders READMES names without extension
197 ('', 0), # special case that renders READMES names without extension
198 ('.text', 2), ('.TEXT', 2),
198 ('.text', 2), ('.TEXT', 2),
199 ('.txt', 3), ('.TXT', 3)
199 ('.txt', 3), ('.TXT', 3)
200 ]
200 ]
201
201
202 RST_EXTS = [
202 RST_EXTS = [
203 ('.rst', 1), ('.rest', 1),
203 ('.rst', 1), ('.rest', 1),
204 ('.RST', 2), ('.REST', 2)
204 ('.RST', 2), ('.REST', 2)
205 ]
205 ]
206
206
207 MARKDOWN_EXTS = [
207 MARKDOWN_EXTS = [
208 ('.md', 1), ('.MD', 1),
208 ('.md', 1), ('.MD', 1),
209 ('.mkdn', 2), ('.MKDN', 2),
209 ('.mkdn', 2), ('.MKDN', 2),
210 ('.mdown', 3), ('.MDOWN', 3),
210 ('.mdown', 3), ('.MDOWN', 3),
211 ('.markdown', 4), ('.MARKDOWN', 4)
211 ('.markdown', 4), ('.MARKDOWN', 4)
212 ]
212 ]
213
213
214 def _detect_renderer(self, source, filename=None):
214 def _detect_renderer(self, source, filename=None):
215 """
215 """
216 runs detection of what renderer should be used for generating html
216 runs detection of what renderer should be used for generating html
217 from a markup language
217 from a markup language
218
218
219 filename can be also explicitly a renderer name
219 filename can be also explicitly a renderer name
220
220
221 :param source:
221 :param source:
222 :param filename:
222 :param filename:
223 """
223 """
224
224
225 if MarkupRenderer.MARKDOWN_PAT.findall(filename):
225 if MarkupRenderer.MARKDOWN_PAT.findall(filename):
226 detected_renderer = 'markdown'
226 detected_renderer = 'markdown'
227 elif MarkupRenderer.RST_PAT.findall(filename):
227 elif MarkupRenderer.RST_PAT.findall(filename):
228 detected_renderer = 'rst'
228 detected_renderer = 'rst'
229 elif MarkupRenderer.JUPYTER_PAT.findall(filename):
229 elif MarkupRenderer.JUPYTER_PAT.findall(filename):
230 detected_renderer = 'jupyter'
230 detected_renderer = 'jupyter'
231 elif MarkupRenderer.PLAIN_PAT.findall(filename):
231 elif MarkupRenderer.PLAIN_PAT.findall(filename):
232 detected_renderer = 'plain'
232 detected_renderer = 'plain'
233 else:
233 else:
234 detected_renderer = 'plain'
234 detected_renderer = 'plain'
235
235
236 return getattr(MarkupRenderer, detected_renderer)
236 return getattr(MarkupRenderer, detected_renderer)
237
237
238 @classmethod
238 @classmethod
239 def bleach_clean(cls, text):
239 def bleach_clean(cls, text):
240 from .bleach_whitelist import markdown_attrs, markdown_tags
240 from .bleach_whitelist import markdown_attrs, markdown_tags
241 allowed_tags = markdown_tags
241 allowed_tags = markdown_tags
242 allowed_attrs = markdown_attrs
242 allowed_attrs = markdown_attrs
243 return bleach.clean(text, tags=allowed_tags, attributes=allowed_attrs)
243 return bleach.clean(text, tags=allowed_tags, attributes=allowed_attrs)
244
244
245 @classmethod
245 @classmethod
246 def renderer_from_filename(cls, filename, exclude):
246 def renderer_from_filename(cls, filename, exclude):
247 """
247 """
248 Detect renderer markdown/rst from filename and optionally use exclude
248 Detect renderer markdown/rst from filename and optionally use exclude
249 list to remove some options. This is mostly used in helpers.
249 list to remove some options. This is mostly used in helpers.
250 Returns None when no renderer can be detected.
250 Returns None when no renderer can be detected.
251 """
251 """
252 def _filter(elements):
252 def _filter(elements):
253 if isinstance(exclude, (list, tuple)):
253 if isinstance(exclude, (list, tuple)):
254 return [x for x in elements if x not in exclude]
254 return [x for x in elements if x not in exclude]
255 return elements
255 return elements
256
256
257 if filename.endswith(
257 if filename.endswith(
258 tuple(_filter([x[0] for x in cls.MARKDOWN_EXTS if x[0]]))):
258 tuple(_filter([x[0] for x in cls.MARKDOWN_EXTS if x[0]]))):
259 return 'markdown'
259 return 'markdown'
260 if filename.endswith(tuple(_filter([x[0] for x in cls.RST_EXTS if x[0]]))):
260 if filename.endswith(tuple(_filter([x[0] for x in cls.RST_EXTS if x[0]]))):
261 return 'rst'
261 return 'rst'
262
262
263 return None
263 return None
264
264
265 def render(self, source, filename=None):
265 def render(self, source, filename=None):
266 """
266 """
267 Renders a given filename using detected renderer
267 Renders a given filename using detected renderer
268 it detects renderers based on file extension or mimetype.
268 it detects renderers based on file extension or mimetype.
269 At last it will just do a simple html replacing new lines with <br/>
269 At last it will just do a simple html replacing new lines with <br/>
270
270
271 :param file_name:
271 :param file_name:
272 :param source:
272 :param source:
273 """
273 """
274
274
275 renderer = self._detect_renderer(source, filename)
275 renderer = self._detect_renderer(source, filename)
276 readme_data = renderer(source)
276 readme_data = renderer(source)
277 return readme_data
277 return readme_data
278
278
279 @classmethod
279 @classmethod
280 def _flavored_markdown(cls, text):
280 def _flavored_markdown(cls, text):
281 """
281 """
282 Github style flavored markdown
282 Github style flavored markdown
283
283
284 :param text:
284 :param text:
285 """
285 """
286
286
287 # Extract pre blocks.
287 # Extract pre blocks.
288 extractions = {}
288 extractions = {}
289
289
290 def pre_extraction_callback(matchobj):
290 def pre_extraction_callback(matchobj):
291 digest = md5_safe(matchobj.group(0))
291 digest = md5_safe(matchobj.group(0))
292 extractions[digest] = matchobj.group(0)
292 extractions[digest] = matchobj.group(0)
293 return "{gfm-extraction-%s}" % digest
293 return "{gfm-extraction-%s}" % digest
294 pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL)
294 pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL)
295 text = re.sub(pattern, pre_extraction_callback, text)
295 text = re.sub(pattern, pre_extraction_callback, text)
296
296
297 # Prevent foo_bar_baz from ending up with an italic word in the middle.
297 # Prevent foo_bar_baz from ending up with an italic word in the middle.
298 def italic_callback(matchobj):
298 def italic_callback(matchobj):
299 s = matchobj.group(0)
299 s = matchobj.group(0)
300 if list(s).count('_') >= 2:
300 if list(s).count('_') >= 2:
301 return s.replace('_', r'\_')
301 return s.replace('_', r'\_')
302 return s
302 return s
303 text = re.sub(r'^(?! {4}|\t)\w+_\w+_\w[\w_]*', italic_callback, text)
303 text = re.sub(r'^(?! {4}|\t)\w+_\w+_\w[\w_]*', italic_callback, text)
304
304
305 # Insert pre block extractions.
305 # Insert pre block extractions.
306 def pre_insert_callback(matchobj):
306 def pre_insert_callback(matchobj):
307 return '\n\n' + extractions[matchobj.group(1)]
307 return '\n\n' + extractions[matchobj.group(1)]
308 text = re.sub(r'\{gfm-extraction-([0-9a-f]{32})\}',
308 text = re.sub(r'\{gfm-extraction-([0-9a-f]{32})\}',
309 pre_insert_callback, text)
309 pre_insert_callback, text)
310
310
311 return text
311 return text
312
312
313 @classmethod
313 @classmethod
314 def urlify_text(cls, text):
314 def urlify_text(cls, text):
315 def url_func(match_obj):
315 def url_func(match_obj):
316 url_full = match_obj.groups()[0]
316 url_full = match_obj.groups()[0]
317 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
317 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
318
318
319 return cls.URL_PAT.sub(url_func, text)
319 return cls.URL_PAT.sub(url_func, text)
320
320
321 @classmethod
321 @classmethod
322 def plain(cls, source, universal_newline=True):
322 def plain(cls, source, universal_newline=True, leading_newline=True):
323 source = safe_unicode(source)
323 source = safe_unicode(source)
324 if universal_newline:
324 if universal_newline:
325 newline = '\n'
325 newline = '\n'
326 source = newline.join(source.splitlines())
326 source = newline.join(source.splitlines())
327
327
328 source = cls.urlify_text(source)
328 rendered_source = cls.urlify_text(source)
329 return '<br />' + source.replace("\n", '<br />')
329 source = ''
330 if leading_newline:
331 source += '<br />'
332 source += rendered_source.replace("\n", '<br />')
333 return source
330
334
331 @classmethod
335 @classmethod
332 def markdown(cls, source, safe=True, flavored=True, mentions=False,
336 def markdown(cls, source, safe=True, flavored=True, mentions=False,
333 clean_html=True):
337 clean_html=True):
334 """
338 """
335 returns markdown rendered code cleaned by the bleach library
339 returns markdown rendered code cleaned by the bleach library
336 """
340 """
337
341
338 if flavored:
342 if flavored:
339 markdown_renderer = cls.markdown_renderer_flavored
343 markdown_renderer = cls.markdown_renderer_flavored
340 else:
344 else:
341 markdown_renderer = cls.markdown_renderer
345 markdown_renderer = cls.markdown_renderer
342
346
343 if mentions:
347 if mentions:
344 mention_pat = re.compile(MENTIONS_REGEX)
348 mention_pat = re.compile(MENTIONS_REGEX)
345
349
346 def wrapp(match_obj):
350 def wrapp(match_obj):
347 uname = match_obj.groups()[0]
351 uname = match_obj.groups()[0]
348 return ' **@%(uname)s** ' % {'uname': uname}
352 return ' **@%(uname)s** ' % {'uname': uname}
349 mention_hl = mention_pat.sub(wrapp, source).strip()
353 mention_hl = mention_pat.sub(wrapp, source).strip()
350 # we extracted mentions render with this using Mentions false
354 # we extracted mentions render with this using Mentions false
351 return cls.markdown(mention_hl, safe=safe, flavored=flavored,
355 return cls.markdown(mention_hl, safe=safe, flavored=flavored,
352 mentions=False)
356 mentions=False)
353
357
354 source = safe_unicode(source)
358 source = safe_unicode(source)
355
359
356 try:
360 try:
357 if flavored:
361 if flavored:
358 source = cls._flavored_markdown(source)
362 source = cls._flavored_markdown(source)
359 rendered = markdown_renderer.convert(source)
363 rendered = markdown_renderer.convert(source)
360 if clean_html:
364 if clean_html:
361 rendered = cls.bleach_clean(rendered)
365 rendered = cls.bleach_clean(rendered)
362 return rendered
366 return rendered
363 except Exception:
367 except Exception:
364 log.exception('Error when rendering Markdown')
368 log.exception('Error when rendering Markdown')
365 if safe:
369 if safe:
366 log.debug('Fallback to render in plain mode')
370 log.debug('Fallback to render in plain mode')
367 return cls.plain(source)
371 return cls.plain(source)
368 else:
372 else:
369 raise
373 raise
370
374
371 @classmethod
375 @classmethod
372 def rst(cls, source, safe=True, mentions=False, clean_html=False):
376 def rst(cls, source, safe=True, mentions=False, clean_html=False):
373 if mentions:
377 if mentions:
374 mention_pat = re.compile(MENTIONS_REGEX)
378 mention_pat = re.compile(MENTIONS_REGEX)
375
379
376 def wrapp(match_obj):
380 def wrapp(match_obj):
377 uname = match_obj.groups()[0]
381 uname = match_obj.groups()[0]
378 return ' **@%(uname)s** ' % {'uname': uname}
382 return ' **@%(uname)s** ' % {'uname': uname}
379 mention_hl = mention_pat.sub(wrapp, source).strip()
383 mention_hl = mention_pat.sub(wrapp, source).strip()
380 # we extracted mentions render with this using Mentions false
384 # we extracted mentions render with this using Mentions false
381 return cls.rst(mention_hl, safe=safe, mentions=False)
385 return cls.rst(mention_hl, safe=safe, mentions=False)
382
386
383 source = safe_unicode(source)
387 source = safe_unicode(source)
384 try:
388 try:
385 docutils_settings = dict(
389 docutils_settings = dict(
386 [(alias, None) for alias in
390 [(alias, None) for alias in
387 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
391 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
388
392
389 docutils_settings.update({
393 docutils_settings.update({
390 'input_encoding': 'unicode', 'report_level': 4})
394 'input_encoding': 'unicode', 'report_level': 4})
391
395
392 for k, v in docutils_settings.iteritems():
396 for k, v in docutils_settings.iteritems():
393 directives.register_directive(k, v)
397 directives.register_directive(k, v)
394
398
395 parts = publish_parts(source=source,
399 parts = publish_parts(source=source,
396 writer=RhodeCodeWriter(),
400 writer=RhodeCodeWriter(),
397 settings_overrides=docutils_settings)
401 settings_overrides=docutils_settings)
398 rendered = parts["fragment"]
402 rendered = parts["fragment"]
399 if clean_html:
403 if clean_html:
400 rendered = cls.bleach_clean(rendered)
404 rendered = cls.bleach_clean(rendered)
401 return parts['html_title'] + rendered
405 return parts['html_title'] + rendered
402 except Exception:
406 except Exception:
403 log.exception('Error when rendering RST')
407 log.exception('Error when rendering RST')
404 if safe:
408 if safe:
405 log.debug('Fallbacking to render in plain mode')
409 log.debug('Fallbacking to render in plain mode')
406 return cls.plain(source)
410 return cls.plain(source)
407 else:
411 else:
408 raise
412 raise
409
413
410 @classmethod
414 @classmethod
411 def jupyter(cls, source, safe=True):
415 def jupyter(cls, source, safe=True):
412 from rhodecode.lib import helpers
416 from rhodecode.lib import helpers
413
417
414 from traitlets.config import Config
418 from traitlets.config import Config
415 import nbformat
419 import nbformat
416 from nbconvert import HTMLExporter
420 from nbconvert import HTMLExporter
417 from nbconvert.preprocessors import Preprocessor
421 from nbconvert.preprocessors import Preprocessor
418
422
419 class CustomHTMLExporter(HTMLExporter):
423 class CustomHTMLExporter(HTMLExporter):
420 def _template_file_default(self):
424 def _template_file_default(self):
421 return 'basic'
425 return 'basic'
422
426
423 class Sandbox(Preprocessor):
427 class Sandbox(Preprocessor):
424
428
425 def preprocess(self, nb, resources):
429 def preprocess(self, nb, resources):
426 sandbox_text = 'SandBoxed(IPython.core.display.Javascript object)'
430 sandbox_text = 'SandBoxed(IPython.core.display.Javascript object)'
427 for cell in nb['cells']:
431 for cell in nb['cells']:
428 if safe and 'outputs' in cell:
432 if safe and 'outputs' in cell:
429 for cell_output in cell['outputs']:
433 for cell_output in cell['outputs']:
430 if 'data' in cell_output:
434 if 'data' in cell_output:
431 if 'application/javascript' in cell_output['data']:
435 if 'application/javascript' in cell_output['data']:
432 cell_output['data']['text/plain'] = sandbox_text
436 cell_output['data']['text/plain'] = sandbox_text
433 cell_output['data'].pop('application/javascript', None)
437 cell_output['data'].pop('application/javascript', None)
434 return nb, resources
438 return nb, resources
435
439
436 def _sanitize_resources(resources):
440 def _sanitize_resources(resources):
437 """
441 """
438 Skip/sanitize some of the CSS generated and included in jupyter
442 Skip/sanitize some of the CSS generated and included in jupyter
439 so it doesn't messes up UI so much
443 so it doesn't messes up UI so much
440 """
444 """
441
445
442 # TODO(marcink): probably we should replace this with whole custom
446 # TODO(marcink): probably we should replace this with whole custom
443 # CSS set that doesn't screw up, but jupyter generated html has some
447 # CSS set that doesn't screw up, but jupyter generated html has some
444 # special markers, so it requires Custom HTML exporter template with
448 # special markers, so it requires Custom HTML exporter template with
445 # _default_template_path_default, to achieve that
449 # _default_template_path_default, to achieve that
446
450
447 # strip the reset CSS
451 # strip the reset CSS
448 resources[0] = resources[0][resources[0].find('/*! Source'):]
452 resources[0] = resources[0][resources[0].find('/*! Source'):]
449 return resources
453 return resources
450
454
451 def as_html(notebook):
455 def as_html(notebook):
452 conf = Config()
456 conf = Config()
453 conf.CustomHTMLExporter.preprocessors = [Sandbox]
457 conf.CustomHTMLExporter.preprocessors = [Sandbox]
454 html_exporter = CustomHTMLExporter(config=conf)
458 html_exporter = CustomHTMLExporter(config=conf)
455
459
456 (body, resources) = html_exporter.from_notebook_node(notebook)
460 (body, resources) = html_exporter.from_notebook_node(notebook)
457 header = '<!-- ## IPYTHON NOTEBOOK RENDERING ## -->'
461 header = '<!-- ## IPYTHON NOTEBOOK RENDERING ## -->'
458 js = MakoTemplate(r'''
462 js = MakoTemplate(r'''
459 <!-- Load mathjax -->
463 <!-- Load mathjax -->
460 <!-- MathJax configuration -->
464 <!-- MathJax configuration -->
461 <script type="text/x-mathjax-config">
465 <script type="text/x-mathjax-config">
462 MathJax.Hub.Config({
466 MathJax.Hub.Config({
463 jax: ["input/TeX","output/HTML-CSS", "output/PreviewHTML"],
467 jax: ["input/TeX","output/HTML-CSS", "output/PreviewHTML"],
464 extensions: ["tex2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "[Contrib]/a11y/accessibility-menu.js"],
468 extensions: ["tex2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "[Contrib]/a11y/accessibility-menu.js"],
465 TeX: {
469 TeX: {
466 extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
470 extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
467 },
471 },
468 tex2jax: {
472 tex2jax: {
469 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
473 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
470 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
474 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
471 processEscapes: true,
475 processEscapes: true,
472 processEnvironments: true
476 processEnvironments: true
473 },
477 },
474 // Center justify equations in code and markdown cells. Elsewhere
478 // Center justify equations in code and markdown cells. Elsewhere
475 // we use CSS to left justify single line equations in code cells.
479 // we use CSS to left justify single line equations in code cells.
476 displayAlign: 'center',
480 displayAlign: 'center',
477 "HTML-CSS": {
481 "HTML-CSS": {
478 styles: {'.MathJax_Display': {"margin": 0}},
482 styles: {'.MathJax_Display': {"margin": 0}},
479 linebreaks: { automatic: true },
483 linebreaks: { automatic: true },
480 availableFonts: ["STIX", "TeX"]
484 availableFonts: ["STIX", "TeX"]
481 },
485 },
482 showMathMenu: false
486 showMathMenu: false
483 });
487 });
484 </script>
488 </script>
485 <!-- End of mathjax configuration -->
489 <!-- End of mathjax configuration -->
486 <script src="${h.asset('js/src/math_jax/MathJax.js')}"></script>
490 <script src="${h.asset('js/src/math_jax/MathJax.js')}"></script>
487 ''').render(h=helpers)
491 ''').render(h=helpers)
488
492
489 css = '<style>{}</style>'.format(
493 css = '<style>{}</style>'.format(
490 ''.join(_sanitize_resources(resources['inlining']['css'])))
494 ''.join(_sanitize_resources(resources['inlining']['css'])))
491
495
492 body = '\n'.join([header, css, js, body])
496 body = '\n'.join([header, css, js, body])
493 return body, resources
497 return body, resources
494
498
495 notebook = nbformat.reads(source, as_version=4)
499 notebook = nbformat.reads(source, as_version=4)
496 (body, resources) = as_html(notebook)
500 (body, resources) = as_html(notebook)
497 return body
501 return body
498
502
499
503
500 class RstTemplateRenderer(object):
504 class RstTemplateRenderer(object):
501
505
502 def __init__(self):
506 def __init__(self):
503 base = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
507 base = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
504 rst_template_dirs = [os.path.join(base, 'templates', 'rst_templates')]
508 rst_template_dirs = [os.path.join(base, 'templates', 'rst_templates')]
505 self.template_store = TemplateLookup(
509 self.template_store = TemplateLookup(
506 directories=rst_template_dirs,
510 directories=rst_template_dirs,
507 input_encoding='utf-8',
511 input_encoding='utf-8',
508 imports=['from rhodecode.lib import helpers as h'])
512 imports=['from rhodecode.lib import helpers as h'])
509
513
510 def _get_template(self, templatename):
514 def _get_template(self, templatename):
511 return self.template_store.get_template(templatename)
515 return self.template_store.get_template(templatename)
512
516
513 def render(self, template_name, **kwargs):
517 def render(self, template_name, **kwargs):
514 template = self._get_template(template_name)
518 template = self._get_template(template_name)
515 return template.render(**kwargs)
519 return template.render(**kwargs)
@@ -1,4538 +1,4540 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2018 RhodeCode GmbH
3 # Copyright (C) 2010-2018 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 Database Models for RhodeCode Enterprise
22 Database Models for RhodeCode Enterprise
23 """
23 """
24
24
25 import re
25 import re
26 import os
26 import os
27 import time
27 import time
28 import hashlib
28 import hashlib
29 import logging
29 import logging
30 import datetime
30 import datetime
31 import warnings
31 import warnings
32 import ipaddress
32 import ipaddress
33 import functools
33 import functools
34 import traceback
34 import traceback
35 import collections
35 import collections
36
36
37 from sqlalchemy import (
37 from sqlalchemy import (
38 or_, and_, not_, func, TypeDecorator, event,
38 or_, and_, not_, func, TypeDecorator, event,
39 Index, Sequence, UniqueConstraint, ForeignKey, CheckConstraint, Column,
39 Index, Sequence, UniqueConstraint, ForeignKey, CheckConstraint, Column,
40 Boolean, String, Unicode, UnicodeText, DateTime, Integer, LargeBinary,
40 Boolean, String, Unicode, UnicodeText, DateTime, Integer, LargeBinary,
41 Text, Float, PickleType)
41 Text, Float, PickleType)
42 from sqlalchemy.sql.expression import true, false
42 from sqlalchemy.sql.expression import true, false
43 from sqlalchemy.sql.functions import coalesce, count # noqa
43 from sqlalchemy.sql.functions import coalesce, count # noqa
44 from sqlalchemy.orm import (
44 from sqlalchemy.orm import (
45 relationship, joinedload, class_mapper, validates, aliased)
45 relationship, joinedload, class_mapper, validates, aliased)
46 from sqlalchemy.ext.declarative import declared_attr
46 from sqlalchemy.ext.declarative import declared_attr
47 from sqlalchemy.ext.hybrid import hybrid_property
47 from sqlalchemy.ext.hybrid import hybrid_property
48 from sqlalchemy.exc import IntegrityError # noqa
48 from sqlalchemy.exc import IntegrityError # noqa
49 from sqlalchemy.dialects.mysql import LONGTEXT
49 from sqlalchemy.dialects.mysql import LONGTEXT
50 from beaker.cache import cache_region
50 from beaker.cache import cache_region
51 from zope.cachedescriptors.property import Lazy as LazyProperty
51 from zope.cachedescriptors.property import Lazy as LazyProperty
52
52
53 from pyramid.threadlocal import get_current_request
53 from pyramid.threadlocal import get_current_request
54
54
55 from rhodecode.translation import _
55 from rhodecode.translation import _
56 from rhodecode.lib.vcs import get_vcs_instance
56 from rhodecode.lib.vcs import get_vcs_instance
57 from rhodecode.lib.vcs.backends.base import EmptyCommit, Reference
57 from rhodecode.lib.vcs.backends.base import EmptyCommit, Reference
58 from rhodecode.lib.utils2 import (
58 from rhodecode.lib.utils2 import (
59 str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe,
59 str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe,
60 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict,
60 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict,
61 glob2re, StrictAttributeDict, cleaned_uri)
61 glob2re, StrictAttributeDict, cleaned_uri)
62 from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \
62 from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \
63 JsonRaw
63 JsonRaw
64 from rhodecode.lib.ext_json import json
64 from rhodecode.lib.ext_json import json
65 from rhodecode.lib.caching_query import FromCache
65 from rhodecode.lib.caching_query import FromCache
66 from rhodecode.lib.encrypt import AESCipher
66 from rhodecode.lib.encrypt import AESCipher
67
67
68 from rhodecode.model.meta import Base, Session
68 from rhodecode.model.meta import Base, Session
69
69
70 URL_SEP = '/'
70 URL_SEP = '/'
71 log = logging.getLogger(__name__)
71 log = logging.getLogger(__name__)
72
72
73 # =============================================================================
73 # =============================================================================
74 # BASE CLASSES
74 # BASE CLASSES
75 # =============================================================================
75 # =============================================================================
76
76
77 # this is propagated from .ini file rhodecode.encrypted_values.secret or
77 # this is propagated from .ini file rhodecode.encrypted_values.secret or
78 # beaker.session.secret if first is not set.
78 # beaker.session.secret if first is not set.
79 # and initialized at environment.py
79 # and initialized at environment.py
80 ENCRYPTION_KEY = None
80 ENCRYPTION_KEY = None
81
81
82 # used to sort permissions by types, '#' used here is not allowed to be in
82 # used to sort permissions by types, '#' used here is not allowed to be in
83 # usernames, and it's very early in sorted string.printable table.
83 # usernames, and it's very early in sorted string.printable table.
84 PERMISSION_TYPE_SORT = {
84 PERMISSION_TYPE_SORT = {
85 'admin': '####',
85 'admin': '####',
86 'write': '###',
86 'write': '###',
87 'read': '##',
87 'read': '##',
88 'none': '#',
88 'none': '#',
89 }
89 }
90
90
91
91
92 def display_user_sort(obj):
92 def display_user_sort(obj):
93 """
93 """
94 Sort function used to sort permissions in .permissions() function of
94 Sort function used to sort permissions in .permissions() function of
95 Repository, RepoGroup, UserGroup. Also it put the default user in front
95 Repository, RepoGroup, UserGroup. Also it put the default user in front
96 of all other resources
96 of all other resources
97 """
97 """
98
98
99 if obj.username == User.DEFAULT_USER:
99 if obj.username == User.DEFAULT_USER:
100 return '#####'
100 return '#####'
101 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
101 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
102 return prefix + obj.username
102 return prefix + obj.username
103
103
104
104
105 def display_user_group_sort(obj):
105 def display_user_group_sort(obj):
106 """
106 """
107 Sort function used to sort permissions in .permissions() function of
107 Sort function used to sort permissions in .permissions() function of
108 Repository, RepoGroup, UserGroup. Also it put the default user in front
108 Repository, RepoGroup, UserGroup. Also it put the default user in front
109 of all other resources
109 of all other resources
110 """
110 """
111
111
112 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
112 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
113 return prefix + obj.users_group_name
113 return prefix + obj.users_group_name
114
114
115
115
116 def _hash_key(k):
116 def _hash_key(k):
117 return sha1_safe(k)
117 return sha1_safe(k)
118
118
119
119
120 def in_filter_generator(qry, items, limit=500):
120 def in_filter_generator(qry, items, limit=500):
121 """
121 """
122 Splits IN() into multiple with OR
122 Splits IN() into multiple with OR
123 e.g.::
123 e.g.::
124 cnt = Repository.query().filter(
124 cnt = Repository.query().filter(
125 or_(
125 or_(
126 *in_filter_generator(Repository.repo_id, range(100000))
126 *in_filter_generator(Repository.repo_id, range(100000))
127 )).count()
127 )).count()
128 """
128 """
129 if not items:
129 if not items:
130 # empty list will cause empty query which might cause security issues
130 # empty list will cause empty query which might cause security issues
131 # this can lead to hidden unpleasant results
131 # this can lead to hidden unpleasant results
132 items = [-1]
132 items = [-1]
133
133
134 parts = []
134 parts = []
135 for chunk in xrange(0, len(items), limit):
135 for chunk in xrange(0, len(items), limit):
136 parts.append(
136 parts.append(
137 qry.in_(items[chunk: chunk + limit])
137 qry.in_(items[chunk: chunk + limit])
138 )
138 )
139
139
140 return parts
140 return parts
141
141
142
142
143 base_table_args = {
143 base_table_args = {
144 'extend_existing': True,
144 'extend_existing': True,
145 'mysql_engine': 'InnoDB',
145 'mysql_engine': 'InnoDB',
146 'mysql_charset': 'utf8',
146 'mysql_charset': 'utf8',
147 'sqlite_autoincrement': True
147 'sqlite_autoincrement': True
148 }
148 }
149
149
150
150
151 class EncryptedTextValue(TypeDecorator):
151 class EncryptedTextValue(TypeDecorator):
152 """
152 """
153 Special column for encrypted long text data, use like::
153 Special column for encrypted long text data, use like::
154
154
155 value = Column("encrypted_value", EncryptedValue(), nullable=False)
155 value = Column("encrypted_value", EncryptedValue(), nullable=False)
156
156
157 This column is intelligent so if value is in unencrypted form it return
157 This column is intelligent so if value is in unencrypted form it return
158 unencrypted form, but on save it always encrypts
158 unencrypted form, but on save it always encrypts
159 """
159 """
160 impl = Text
160 impl = Text
161
161
162 def process_bind_param(self, value, dialect):
162 def process_bind_param(self, value, dialect):
163 if not value:
163 if not value:
164 return value
164 return value
165 if value.startswith('enc$aes$') or value.startswith('enc$aes_hmac$'):
165 if value.startswith('enc$aes$') or value.startswith('enc$aes_hmac$'):
166 # protect against double encrypting if someone manually starts
166 # protect against double encrypting if someone manually starts
167 # doing
167 # doing
168 raise ValueError('value needs to be in unencrypted format, ie. '
168 raise ValueError('value needs to be in unencrypted format, ie. '
169 'not starting with enc$aes')
169 'not starting with enc$aes')
170 return 'enc$aes_hmac$%s' % AESCipher(
170 return 'enc$aes_hmac$%s' % AESCipher(
171 ENCRYPTION_KEY, hmac=True).encrypt(value)
171 ENCRYPTION_KEY, hmac=True).encrypt(value)
172
172
173 def process_result_value(self, value, dialect):
173 def process_result_value(self, value, dialect):
174 import rhodecode
174 import rhodecode
175
175
176 if not value:
176 if not value:
177 return value
177 return value
178
178
179 parts = value.split('$', 3)
179 parts = value.split('$', 3)
180 if not len(parts) == 3:
180 if not len(parts) == 3:
181 # probably not encrypted values
181 # probably not encrypted values
182 return value
182 return value
183 else:
183 else:
184 if parts[0] != 'enc':
184 if parts[0] != 'enc':
185 # parts ok but without our header ?
185 # parts ok but without our header ?
186 return value
186 return value
187 enc_strict_mode = str2bool(rhodecode.CONFIG.get(
187 enc_strict_mode = str2bool(rhodecode.CONFIG.get(
188 'rhodecode.encrypted_values.strict') or True)
188 'rhodecode.encrypted_values.strict') or True)
189 # at that stage we know it's our encryption
189 # at that stage we know it's our encryption
190 if parts[1] == 'aes':
190 if parts[1] == 'aes':
191 decrypted_data = AESCipher(ENCRYPTION_KEY).decrypt(parts[2])
191 decrypted_data = AESCipher(ENCRYPTION_KEY).decrypt(parts[2])
192 elif parts[1] == 'aes_hmac':
192 elif parts[1] == 'aes_hmac':
193 decrypted_data = AESCipher(
193 decrypted_data = AESCipher(
194 ENCRYPTION_KEY, hmac=True,
194 ENCRYPTION_KEY, hmac=True,
195 strict_verification=enc_strict_mode).decrypt(parts[2])
195 strict_verification=enc_strict_mode).decrypt(parts[2])
196 else:
196 else:
197 raise ValueError(
197 raise ValueError(
198 'Encryption type part is wrong, must be `aes` '
198 'Encryption type part is wrong, must be `aes` '
199 'or `aes_hmac`, got `%s` instead' % (parts[1]))
199 'or `aes_hmac`, got `%s` instead' % (parts[1]))
200 return decrypted_data
200 return decrypted_data
201
201
202
202
203 class BaseModel(object):
203 class BaseModel(object):
204 """
204 """
205 Base Model for all classes
205 Base Model for all classes
206 """
206 """
207
207
208 @classmethod
208 @classmethod
209 def _get_keys(cls):
209 def _get_keys(cls):
210 """return column names for this model """
210 """return column names for this model """
211 return class_mapper(cls).c.keys()
211 return class_mapper(cls).c.keys()
212
212
213 def get_dict(self):
213 def get_dict(self):
214 """
214 """
215 return dict with keys and values corresponding
215 return dict with keys and values corresponding
216 to this model data """
216 to this model data """
217
217
218 d = {}
218 d = {}
219 for k in self._get_keys():
219 for k in self._get_keys():
220 d[k] = getattr(self, k)
220 d[k] = getattr(self, k)
221
221
222 # also use __json__() if present to get additional fields
222 # also use __json__() if present to get additional fields
223 _json_attr = getattr(self, '__json__', None)
223 _json_attr = getattr(self, '__json__', None)
224 if _json_attr:
224 if _json_attr:
225 # update with attributes from __json__
225 # update with attributes from __json__
226 if callable(_json_attr):
226 if callable(_json_attr):
227 _json_attr = _json_attr()
227 _json_attr = _json_attr()
228 for k, val in _json_attr.iteritems():
228 for k, val in _json_attr.iteritems():
229 d[k] = val
229 d[k] = val
230 return d
230 return d
231
231
232 def get_appstruct(self):
232 def get_appstruct(self):
233 """return list with keys and values tuples corresponding
233 """return list with keys and values tuples corresponding
234 to this model data """
234 to this model data """
235
235
236 lst = []
236 lst = []
237 for k in self._get_keys():
237 for k in self._get_keys():
238 lst.append((k, getattr(self, k),))
238 lst.append((k, getattr(self, k),))
239 return lst
239 return lst
240
240
241 def populate_obj(self, populate_dict):
241 def populate_obj(self, populate_dict):
242 """populate model with data from given populate_dict"""
242 """populate model with data from given populate_dict"""
243
243
244 for k in self._get_keys():
244 for k in self._get_keys():
245 if k in populate_dict:
245 if k in populate_dict:
246 setattr(self, k, populate_dict[k])
246 setattr(self, k, populate_dict[k])
247
247
248 @classmethod
248 @classmethod
249 def query(cls):
249 def query(cls):
250 return Session().query(cls)
250 return Session().query(cls)
251
251
252 @classmethod
252 @classmethod
253 def get(cls, id_):
253 def get(cls, id_):
254 if id_:
254 if id_:
255 return cls.query().get(id_)
255 return cls.query().get(id_)
256
256
257 @classmethod
257 @classmethod
258 def get_or_404(cls, id_):
258 def get_or_404(cls, id_):
259 from pyramid.httpexceptions import HTTPNotFound
259 from pyramid.httpexceptions import HTTPNotFound
260
260
261 try:
261 try:
262 id_ = int(id_)
262 id_ = int(id_)
263 except (TypeError, ValueError):
263 except (TypeError, ValueError):
264 raise HTTPNotFound()
264 raise HTTPNotFound()
265
265
266 res = cls.query().get(id_)
266 res = cls.query().get(id_)
267 if not res:
267 if not res:
268 raise HTTPNotFound()
268 raise HTTPNotFound()
269 return res
269 return res
270
270
271 @classmethod
271 @classmethod
272 def getAll(cls):
272 def getAll(cls):
273 # deprecated and left for backward compatibility
273 # deprecated and left for backward compatibility
274 return cls.get_all()
274 return cls.get_all()
275
275
276 @classmethod
276 @classmethod
277 def get_all(cls):
277 def get_all(cls):
278 return cls.query().all()
278 return cls.query().all()
279
279
280 @classmethod
280 @classmethod
281 def delete(cls, id_):
281 def delete(cls, id_):
282 obj = cls.query().get(id_)
282 obj = cls.query().get(id_)
283 Session().delete(obj)
283 Session().delete(obj)
284
284
285 @classmethod
285 @classmethod
286 def identity_cache(cls, session, attr_name, value):
286 def identity_cache(cls, session, attr_name, value):
287 exist_in_session = []
287 exist_in_session = []
288 for (item_cls, pkey), instance in session.identity_map.items():
288 for (item_cls, pkey), instance in session.identity_map.items():
289 if cls == item_cls and getattr(instance, attr_name) == value:
289 if cls == item_cls and getattr(instance, attr_name) == value:
290 exist_in_session.append(instance)
290 exist_in_session.append(instance)
291 if exist_in_session:
291 if exist_in_session:
292 if len(exist_in_session) == 1:
292 if len(exist_in_session) == 1:
293 return exist_in_session[0]
293 return exist_in_session[0]
294 log.exception(
294 log.exception(
295 'multiple objects with attr %s and '
295 'multiple objects with attr %s and '
296 'value %s found with same name: %r',
296 'value %s found with same name: %r',
297 attr_name, value, exist_in_session)
297 attr_name, value, exist_in_session)
298
298
299 def __repr__(self):
299 def __repr__(self):
300 if hasattr(self, '__unicode__'):
300 if hasattr(self, '__unicode__'):
301 # python repr needs to return str
301 # python repr needs to return str
302 try:
302 try:
303 return safe_str(self.__unicode__())
303 return safe_str(self.__unicode__())
304 except UnicodeDecodeError:
304 except UnicodeDecodeError:
305 pass
305 pass
306 return '<DB:%s>' % (self.__class__.__name__)
306 return '<DB:%s>' % (self.__class__.__name__)
307
307
308
308
309 class RhodeCodeSetting(Base, BaseModel):
309 class RhodeCodeSetting(Base, BaseModel):
310 __tablename__ = 'rhodecode_settings'
310 __tablename__ = 'rhodecode_settings'
311 __table_args__ = (
311 __table_args__ = (
312 UniqueConstraint('app_settings_name'),
312 UniqueConstraint('app_settings_name'),
313 base_table_args
313 base_table_args
314 )
314 )
315
315
316 SETTINGS_TYPES = {
316 SETTINGS_TYPES = {
317 'str': safe_str,
317 'str': safe_str,
318 'int': safe_int,
318 'int': safe_int,
319 'unicode': safe_unicode,
319 'unicode': safe_unicode,
320 'bool': str2bool,
320 'bool': str2bool,
321 'list': functools.partial(aslist, sep=',')
321 'list': functools.partial(aslist, sep=',')
322 }
322 }
323 DEFAULT_UPDATE_URL = 'https://rhodecode.com/api/v1/info/versions'
323 DEFAULT_UPDATE_URL = 'https://rhodecode.com/api/v1/info/versions'
324 GLOBAL_CONF_KEY = 'app_settings'
324 GLOBAL_CONF_KEY = 'app_settings'
325
325
326 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
326 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
327 app_settings_name = Column("app_settings_name", String(255), nullable=True, unique=None, default=None)
327 app_settings_name = Column("app_settings_name", String(255), nullable=True, unique=None, default=None)
328 _app_settings_value = Column("app_settings_value", String(4096), nullable=True, unique=None, default=None)
328 _app_settings_value = Column("app_settings_value", String(4096), nullable=True, unique=None, default=None)
329 _app_settings_type = Column("app_settings_type", String(255), nullable=True, unique=None, default=None)
329 _app_settings_type = Column("app_settings_type", String(255), nullable=True, unique=None, default=None)
330
330
331 def __init__(self, key='', val='', type='unicode'):
331 def __init__(self, key='', val='', type='unicode'):
332 self.app_settings_name = key
332 self.app_settings_name = key
333 self.app_settings_type = type
333 self.app_settings_type = type
334 self.app_settings_value = val
334 self.app_settings_value = val
335
335
336 @validates('_app_settings_value')
336 @validates('_app_settings_value')
337 def validate_settings_value(self, key, val):
337 def validate_settings_value(self, key, val):
338 assert type(val) == unicode
338 assert type(val) == unicode
339 return val
339 return val
340
340
341 @hybrid_property
341 @hybrid_property
342 def app_settings_value(self):
342 def app_settings_value(self):
343 v = self._app_settings_value
343 v = self._app_settings_value
344 _type = self.app_settings_type
344 _type = self.app_settings_type
345 if _type:
345 if _type:
346 _type = self.app_settings_type.split('.')[0]
346 _type = self.app_settings_type.split('.')[0]
347 # decode the encrypted value
347 # decode the encrypted value
348 if 'encrypted' in self.app_settings_type:
348 if 'encrypted' in self.app_settings_type:
349 cipher = EncryptedTextValue()
349 cipher = EncryptedTextValue()
350 v = safe_unicode(cipher.process_result_value(v, None))
350 v = safe_unicode(cipher.process_result_value(v, None))
351
351
352 converter = self.SETTINGS_TYPES.get(_type) or \
352 converter = self.SETTINGS_TYPES.get(_type) or \
353 self.SETTINGS_TYPES['unicode']
353 self.SETTINGS_TYPES['unicode']
354 return converter(v)
354 return converter(v)
355
355
356 @app_settings_value.setter
356 @app_settings_value.setter
357 def app_settings_value(self, val):
357 def app_settings_value(self, val):
358 """
358 """
359 Setter that will always make sure we use unicode in app_settings_value
359 Setter that will always make sure we use unicode in app_settings_value
360
360
361 :param val:
361 :param val:
362 """
362 """
363 val = safe_unicode(val)
363 val = safe_unicode(val)
364 # encode the encrypted value
364 # encode the encrypted value
365 if 'encrypted' in self.app_settings_type:
365 if 'encrypted' in self.app_settings_type:
366 cipher = EncryptedTextValue()
366 cipher = EncryptedTextValue()
367 val = safe_unicode(cipher.process_bind_param(val, None))
367 val = safe_unicode(cipher.process_bind_param(val, None))
368 self._app_settings_value = val
368 self._app_settings_value = val
369
369
370 @hybrid_property
370 @hybrid_property
371 def app_settings_type(self):
371 def app_settings_type(self):
372 return self._app_settings_type
372 return self._app_settings_type
373
373
374 @app_settings_type.setter
374 @app_settings_type.setter
375 def app_settings_type(self, val):
375 def app_settings_type(self, val):
376 if val.split('.')[0] not in self.SETTINGS_TYPES:
376 if val.split('.')[0] not in self.SETTINGS_TYPES:
377 raise Exception('type must be one of %s got %s'
377 raise Exception('type must be one of %s got %s'
378 % (self.SETTINGS_TYPES.keys(), val))
378 % (self.SETTINGS_TYPES.keys(), val))
379 self._app_settings_type = val
379 self._app_settings_type = val
380
380
381 def __unicode__(self):
381 def __unicode__(self):
382 return u"<%s('%s:%s[%s]')>" % (
382 return u"<%s('%s:%s[%s]')>" % (
383 self.__class__.__name__,
383 self.__class__.__name__,
384 self.app_settings_name, self.app_settings_value,
384 self.app_settings_name, self.app_settings_value,
385 self.app_settings_type
385 self.app_settings_type
386 )
386 )
387
387
388
388
389 class RhodeCodeUi(Base, BaseModel):
389 class RhodeCodeUi(Base, BaseModel):
390 __tablename__ = 'rhodecode_ui'
390 __tablename__ = 'rhodecode_ui'
391 __table_args__ = (
391 __table_args__ = (
392 UniqueConstraint('ui_key'),
392 UniqueConstraint('ui_key'),
393 base_table_args
393 base_table_args
394 )
394 )
395
395
396 HOOK_REPO_SIZE = 'changegroup.repo_size'
396 HOOK_REPO_SIZE = 'changegroup.repo_size'
397 # HG
397 # HG
398 HOOK_PRE_PULL = 'preoutgoing.pre_pull'
398 HOOK_PRE_PULL = 'preoutgoing.pre_pull'
399 HOOK_PULL = 'outgoing.pull_logger'
399 HOOK_PULL = 'outgoing.pull_logger'
400 HOOK_PRE_PUSH = 'prechangegroup.pre_push'
400 HOOK_PRE_PUSH = 'prechangegroup.pre_push'
401 HOOK_PRETX_PUSH = 'pretxnchangegroup.pre_push'
401 HOOK_PRETX_PUSH = 'pretxnchangegroup.pre_push'
402 HOOK_PUSH = 'changegroup.push_logger'
402 HOOK_PUSH = 'changegroup.push_logger'
403 HOOK_PUSH_KEY = 'pushkey.key_push'
403 HOOK_PUSH_KEY = 'pushkey.key_push'
404
404
405 # TODO: johbo: Unify way how hooks are configured for git and hg,
405 # TODO: johbo: Unify way how hooks are configured for git and hg,
406 # git part is currently hardcoded.
406 # git part is currently hardcoded.
407
407
408 # SVN PATTERNS
408 # SVN PATTERNS
409 SVN_BRANCH_ID = 'vcs_svn_branch'
409 SVN_BRANCH_ID = 'vcs_svn_branch'
410 SVN_TAG_ID = 'vcs_svn_tag'
410 SVN_TAG_ID = 'vcs_svn_tag'
411
411
412 ui_id = Column(
412 ui_id = Column(
413 "ui_id", Integer(), nullable=False, unique=True, default=None,
413 "ui_id", Integer(), nullable=False, unique=True, default=None,
414 primary_key=True)
414 primary_key=True)
415 ui_section = Column(
415 ui_section = Column(
416 "ui_section", String(255), nullable=True, unique=None, default=None)
416 "ui_section", String(255), nullable=True, unique=None, default=None)
417 ui_key = Column(
417 ui_key = Column(
418 "ui_key", String(255), nullable=True, unique=None, default=None)
418 "ui_key", String(255), nullable=True, unique=None, default=None)
419 ui_value = Column(
419 ui_value = Column(
420 "ui_value", String(255), nullable=True, unique=None, default=None)
420 "ui_value", String(255), nullable=True, unique=None, default=None)
421 ui_active = Column(
421 ui_active = Column(
422 "ui_active", Boolean(), nullable=True, unique=None, default=True)
422 "ui_active", Boolean(), nullable=True, unique=None, default=True)
423
423
424 def __repr__(self):
424 def __repr__(self):
425 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
425 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
426 self.ui_key, self.ui_value)
426 self.ui_key, self.ui_value)
427
427
428
428
429 class RepoRhodeCodeSetting(Base, BaseModel):
429 class RepoRhodeCodeSetting(Base, BaseModel):
430 __tablename__ = 'repo_rhodecode_settings'
430 __tablename__ = 'repo_rhodecode_settings'
431 __table_args__ = (
431 __table_args__ = (
432 UniqueConstraint(
432 UniqueConstraint(
433 'app_settings_name', 'repository_id',
433 'app_settings_name', 'repository_id',
434 name='uq_repo_rhodecode_setting_name_repo_id'),
434 name='uq_repo_rhodecode_setting_name_repo_id'),
435 base_table_args
435 base_table_args
436 )
436 )
437
437
438 repository_id = Column(
438 repository_id = Column(
439 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
439 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
440 nullable=False)
440 nullable=False)
441 app_settings_id = Column(
441 app_settings_id = Column(
442 "app_settings_id", Integer(), nullable=False, unique=True,
442 "app_settings_id", Integer(), nullable=False, unique=True,
443 default=None, primary_key=True)
443 default=None, primary_key=True)
444 app_settings_name = Column(
444 app_settings_name = Column(
445 "app_settings_name", String(255), nullable=True, unique=None,
445 "app_settings_name", String(255), nullable=True, unique=None,
446 default=None)
446 default=None)
447 _app_settings_value = Column(
447 _app_settings_value = Column(
448 "app_settings_value", String(4096), nullable=True, unique=None,
448 "app_settings_value", String(4096), nullable=True, unique=None,
449 default=None)
449 default=None)
450 _app_settings_type = Column(
450 _app_settings_type = Column(
451 "app_settings_type", String(255), nullable=True, unique=None,
451 "app_settings_type", String(255), nullable=True, unique=None,
452 default=None)
452 default=None)
453
453
454 repository = relationship('Repository')
454 repository = relationship('Repository')
455
455
456 def __init__(self, repository_id, key='', val='', type='unicode'):
456 def __init__(self, repository_id, key='', val='', type='unicode'):
457 self.repository_id = repository_id
457 self.repository_id = repository_id
458 self.app_settings_name = key
458 self.app_settings_name = key
459 self.app_settings_type = type
459 self.app_settings_type = type
460 self.app_settings_value = val
460 self.app_settings_value = val
461
461
462 @validates('_app_settings_value')
462 @validates('_app_settings_value')
463 def validate_settings_value(self, key, val):
463 def validate_settings_value(self, key, val):
464 assert type(val) == unicode
464 assert type(val) == unicode
465 return val
465 return val
466
466
467 @hybrid_property
467 @hybrid_property
468 def app_settings_value(self):
468 def app_settings_value(self):
469 v = self._app_settings_value
469 v = self._app_settings_value
470 type_ = self.app_settings_type
470 type_ = self.app_settings_type
471 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
471 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
472 converter = SETTINGS_TYPES.get(type_) or SETTINGS_TYPES['unicode']
472 converter = SETTINGS_TYPES.get(type_) or SETTINGS_TYPES['unicode']
473 return converter(v)
473 return converter(v)
474
474
475 @app_settings_value.setter
475 @app_settings_value.setter
476 def app_settings_value(self, val):
476 def app_settings_value(self, val):
477 """
477 """
478 Setter that will always make sure we use unicode in app_settings_value
478 Setter that will always make sure we use unicode in app_settings_value
479
479
480 :param val:
480 :param val:
481 """
481 """
482 self._app_settings_value = safe_unicode(val)
482 self._app_settings_value = safe_unicode(val)
483
483
484 @hybrid_property
484 @hybrid_property
485 def app_settings_type(self):
485 def app_settings_type(self):
486 return self._app_settings_type
486 return self._app_settings_type
487
487
488 @app_settings_type.setter
488 @app_settings_type.setter
489 def app_settings_type(self, val):
489 def app_settings_type(self, val):
490 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
490 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
491 if val not in SETTINGS_TYPES:
491 if val not in SETTINGS_TYPES:
492 raise Exception('type must be one of %s got %s'
492 raise Exception('type must be one of %s got %s'
493 % (SETTINGS_TYPES.keys(), val))
493 % (SETTINGS_TYPES.keys(), val))
494 self._app_settings_type = val
494 self._app_settings_type = val
495
495
496 def __unicode__(self):
496 def __unicode__(self):
497 return u"<%s('%s:%s:%s[%s]')>" % (
497 return u"<%s('%s:%s:%s[%s]')>" % (
498 self.__class__.__name__, self.repository.repo_name,
498 self.__class__.__name__, self.repository.repo_name,
499 self.app_settings_name, self.app_settings_value,
499 self.app_settings_name, self.app_settings_value,
500 self.app_settings_type
500 self.app_settings_type
501 )
501 )
502
502
503
503
504 class RepoRhodeCodeUi(Base, BaseModel):
504 class RepoRhodeCodeUi(Base, BaseModel):
505 __tablename__ = 'repo_rhodecode_ui'
505 __tablename__ = 'repo_rhodecode_ui'
506 __table_args__ = (
506 __table_args__ = (
507 UniqueConstraint(
507 UniqueConstraint(
508 'repository_id', 'ui_section', 'ui_key',
508 'repository_id', 'ui_section', 'ui_key',
509 name='uq_repo_rhodecode_ui_repository_id_section_key'),
509 name='uq_repo_rhodecode_ui_repository_id_section_key'),
510 base_table_args
510 base_table_args
511 )
511 )
512
512
513 repository_id = Column(
513 repository_id = Column(
514 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
514 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
515 nullable=False)
515 nullable=False)
516 ui_id = Column(
516 ui_id = Column(
517 "ui_id", Integer(), nullable=False, unique=True, default=None,
517 "ui_id", Integer(), nullable=False, unique=True, default=None,
518 primary_key=True)
518 primary_key=True)
519 ui_section = Column(
519 ui_section = Column(
520 "ui_section", String(255), nullable=True, unique=None, default=None)
520 "ui_section", String(255), nullable=True, unique=None, default=None)
521 ui_key = Column(
521 ui_key = Column(
522 "ui_key", String(255), nullable=True, unique=None, default=None)
522 "ui_key", String(255), nullable=True, unique=None, default=None)
523 ui_value = Column(
523 ui_value = Column(
524 "ui_value", String(255), nullable=True, unique=None, default=None)
524 "ui_value", String(255), nullable=True, unique=None, default=None)
525 ui_active = Column(
525 ui_active = Column(
526 "ui_active", Boolean(), nullable=True, unique=None, default=True)
526 "ui_active", Boolean(), nullable=True, unique=None, default=True)
527
527
528 repository = relationship('Repository')
528 repository = relationship('Repository')
529
529
530 def __repr__(self):
530 def __repr__(self):
531 return '<%s[%s:%s]%s=>%s]>' % (
531 return '<%s[%s:%s]%s=>%s]>' % (
532 self.__class__.__name__, self.repository.repo_name,
532 self.__class__.__name__, self.repository.repo_name,
533 self.ui_section, self.ui_key, self.ui_value)
533 self.ui_section, self.ui_key, self.ui_value)
534
534
535
535
536 class User(Base, BaseModel):
536 class User(Base, BaseModel):
537 __tablename__ = 'users'
537 __tablename__ = 'users'
538 __table_args__ = (
538 __table_args__ = (
539 UniqueConstraint('username'), UniqueConstraint('email'),
539 UniqueConstraint('username'), UniqueConstraint('email'),
540 Index('u_username_idx', 'username'),
540 Index('u_username_idx', 'username'),
541 Index('u_email_idx', 'email'),
541 Index('u_email_idx', 'email'),
542 base_table_args
542 base_table_args
543 )
543 )
544
544
545 DEFAULT_USER = 'default'
545 DEFAULT_USER = 'default'
546 DEFAULT_USER_EMAIL = 'anonymous@rhodecode.org'
546 DEFAULT_USER_EMAIL = 'anonymous@rhodecode.org'
547 DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
547 DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
548
548
549 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
549 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
550 username = Column("username", String(255), nullable=True, unique=None, default=None)
550 username = Column("username", String(255), nullable=True, unique=None, default=None)
551 password = Column("password", String(255), nullable=True, unique=None, default=None)
551 password = Column("password", String(255), nullable=True, unique=None, default=None)
552 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
552 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
553 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
553 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
554 name = Column("firstname", String(255), nullable=True, unique=None, default=None)
554 name = Column("firstname", String(255), nullable=True, unique=None, default=None)
555 lastname = Column("lastname", String(255), nullable=True, unique=None, default=None)
555 lastname = Column("lastname", String(255), nullable=True, unique=None, default=None)
556 _email = Column("email", String(255), nullable=True, unique=None, default=None)
556 _email = Column("email", String(255), nullable=True, unique=None, default=None)
557 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
557 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
558 last_activity = Column('last_activity', DateTime(timezone=False), nullable=True, unique=None, default=None)
558 last_activity = Column('last_activity', DateTime(timezone=False), nullable=True, unique=None, default=None)
559
559
560 extern_type = Column("extern_type", String(255), nullable=True, unique=None, default=None)
560 extern_type = Column("extern_type", String(255), nullable=True, unique=None, default=None)
561 extern_name = Column("extern_name", String(255), nullable=True, unique=None, default=None)
561 extern_name = Column("extern_name", String(255), nullable=True, unique=None, default=None)
562 _api_key = Column("api_key", String(255), nullable=True, unique=None, default=None)
562 _api_key = Column("api_key", String(255), nullable=True, unique=None, default=None)
563 inherit_default_permissions = Column("inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
563 inherit_default_permissions = Column("inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
564 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
564 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
565 _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data
565 _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data
566
566
567 user_log = relationship('UserLog')
567 user_log = relationship('UserLog')
568 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
568 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
569
569
570 repositories = relationship('Repository')
570 repositories = relationship('Repository')
571 repository_groups = relationship('RepoGroup')
571 repository_groups = relationship('RepoGroup')
572 user_groups = relationship('UserGroup')
572 user_groups = relationship('UserGroup')
573
573
574 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
574 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
575 followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all')
575 followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all')
576
576
577 repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all')
577 repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all')
578 repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all')
578 repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all')
579 user_group_to_perm = relationship('UserUserGroupToPerm', primaryjoin='UserUserGroupToPerm.user_id==User.user_id', cascade='all')
579 user_group_to_perm = relationship('UserUserGroupToPerm', primaryjoin='UserUserGroupToPerm.user_id==User.user_id', cascade='all')
580
580
581 group_member = relationship('UserGroupMember', cascade='all')
581 group_member = relationship('UserGroupMember', cascade='all')
582
582
583 notifications = relationship('UserNotification', cascade='all')
583 notifications = relationship('UserNotification', cascade='all')
584 # notifications assigned to this user
584 # notifications assigned to this user
585 user_created_notifications = relationship('Notification', cascade='all')
585 user_created_notifications = relationship('Notification', cascade='all')
586 # comments created by this user
586 # comments created by this user
587 user_comments = relationship('ChangesetComment', cascade='all')
587 user_comments = relationship('ChangesetComment', cascade='all')
588 # user profile extra info
588 # user profile extra info
589 user_emails = relationship('UserEmailMap', cascade='all')
589 user_emails = relationship('UserEmailMap', cascade='all')
590 user_ip_map = relationship('UserIpMap', cascade='all')
590 user_ip_map = relationship('UserIpMap', cascade='all')
591 user_auth_tokens = relationship('UserApiKeys', cascade='all')
591 user_auth_tokens = relationship('UserApiKeys', cascade='all')
592 user_ssh_keys = relationship('UserSshKeys', cascade='all')
592 user_ssh_keys = relationship('UserSshKeys', cascade='all')
593
593
594 # gists
594 # gists
595 user_gists = relationship('Gist', cascade='all')
595 user_gists = relationship('Gist', cascade='all')
596 # user pull requests
596 # user pull requests
597 user_pull_requests = relationship('PullRequest', cascade='all')
597 user_pull_requests = relationship('PullRequest', cascade='all')
598 # external identities
598 # external identities
599 extenal_identities = relationship(
599 extenal_identities = relationship(
600 'ExternalIdentity',
600 'ExternalIdentity',
601 primaryjoin="User.user_id==ExternalIdentity.local_user_id",
601 primaryjoin="User.user_id==ExternalIdentity.local_user_id",
602 cascade='all')
602 cascade='all')
603 # review rules
603 # review rules
604 user_review_rules = relationship('RepoReviewRuleUser', cascade='all')
604 user_review_rules = relationship('RepoReviewRuleUser', cascade='all')
605
605
606 def __unicode__(self):
606 def __unicode__(self):
607 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
607 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
608 self.user_id, self.username)
608 self.user_id, self.username)
609
609
610 @hybrid_property
610 @hybrid_property
611 def email(self):
611 def email(self):
612 return self._email
612 return self._email
613
613
614 @email.setter
614 @email.setter
615 def email(self, val):
615 def email(self, val):
616 self._email = val.lower() if val else None
616 self._email = val.lower() if val else None
617
617
618 @hybrid_property
618 @hybrid_property
619 def first_name(self):
619 def first_name(self):
620 from rhodecode.lib import helpers as h
620 from rhodecode.lib import helpers as h
621 if self.name:
621 if self.name:
622 return h.escape(self.name)
622 return h.escape(self.name)
623 return self.name
623 return self.name
624
624
625 @hybrid_property
625 @hybrid_property
626 def last_name(self):
626 def last_name(self):
627 from rhodecode.lib import helpers as h
627 from rhodecode.lib import helpers as h
628 if self.lastname:
628 if self.lastname:
629 return h.escape(self.lastname)
629 return h.escape(self.lastname)
630 return self.lastname
630 return self.lastname
631
631
632 @hybrid_property
632 @hybrid_property
633 def api_key(self):
633 def api_key(self):
634 """
634 """
635 Fetch if exist an auth-token with role ALL connected to this user
635 Fetch if exist an auth-token with role ALL connected to this user
636 """
636 """
637 user_auth_token = UserApiKeys.query()\
637 user_auth_token = UserApiKeys.query()\
638 .filter(UserApiKeys.user_id == self.user_id)\
638 .filter(UserApiKeys.user_id == self.user_id)\
639 .filter(or_(UserApiKeys.expires == -1,
639 .filter(or_(UserApiKeys.expires == -1,
640 UserApiKeys.expires >= time.time()))\
640 UserApiKeys.expires >= time.time()))\
641 .filter(UserApiKeys.role == UserApiKeys.ROLE_ALL).first()
641 .filter(UserApiKeys.role == UserApiKeys.ROLE_ALL).first()
642 if user_auth_token:
642 if user_auth_token:
643 user_auth_token = user_auth_token.api_key
643 user_auth_token = user_auth_token.api_key
644
644
645 return user_auth_token
645 return user_auth_token
646
646
647 @api_key.setter
647 @api_key.setter
648 def api_key(self, val):
648 def api_key(self, val):
649 # don't allow to set API key this is deprecated for now
649 # don't allow to set API key this is deprecated for now
650 self._api_key = None
650 self._api_key = None
651
651
652 @property
652 @property
653 def reviewer_pull_requests(self):
653 def reviewer_pull_requests(self):
654 return PullRequestReviewers.query() \
654 return PullRequestReviewers.query() \
655 .options(joinedload(PullRequestReviewers.pull_request)) \
655 .options(joinedload(PullRequestReviewers.pull_request)) \
656 .filter(PullRequestReviewers.user_id == self.user_id) \
656 .filter(PullRequestReviewers.user_id == self.user_id) \
657 .all()
657 .all()
658
658
659 @property
659 @property
660 def firstname(self):
660 def firstname(self):
661 # alias for future
661 # alias for future
662 return self.name
662 return self.name
663
663
664 @property
664 @property
665 def emails(self):
665 def emails(self):
666 other = UserEmailMap.query()\
666 other = UserEmailMap.query()\
667 .filter(UserEmailMap.user == self) \
667 .filter(UserEmailMap.user == self) \
668 .order_by(UserEmailMap.email_id.asc()) \
668 .order_by(UserEmailMap.email_id.asc()) \
669 .all()
669 .all()
670 return [self.email] + [x.email for x in other]
670 return [self.email] + [x.email for x in other]
671
671
672 @property
672 @property
673 def auth_tokens(self):
673 def auth_tokens(self):
674 auth_tokens = self.get_auth_tokens()
674 auth_tokens = self.get_auth_tokens()
675 return [x.api_key for x in auth_tokens]
675 return [x.api_key for x in auth_tokens]
676
676
677 def get_auth_tokens(self):
677 def get_auth_tokens(self):
678 return UserApiKeys.query()\
678 return UserApiKeys.query()\
679 .filter(UserApiKeys.user == self)\
679 .filter(UserApiKeys.user == self)\
680 .order_by(UserApiKeys.user_api_key_id.asc())\
680 .order_by(UserApiKeys.user_api_key_id.asc())\
681 .all()
681 .all()
682
682
683 @LazyProperty
683 @LazyProperty
684 def feed_token(self):
684 def feed_token(self):
685 return self.get_feed_token()
685 return self.get_feed_token()
686
686
687 def get_feed_token(self, cache=True):
687 def get_feed_token(self, cache=True):
688 feed_tokens = UserApiKeys.query()\
688 feed_tokens = UserApiKeys.query()\
689 .filter(UserApiKeys.user == self)\
689 .filter(UserApiKeys.user == self)\
690 .filter(UserApiKeys.role == UserApiKeys.ROLE_FEED)
690 .filter(UserApiKeys.role == UserApiKeys.ROLE_FEED)
691 if cache:
691 if cache:
692 feed_tokens = feed_tokens.options(
692 feed_tokens = feed_tokens.options(
693 FromCache("sql_cache_short", "get_user_feed_token_%s" % self.user_id))
693 FromCache("sql_cache_short", "get_user_feed_token_%s" % self.user_id))
694
694
695 feed_tokens = feed_tokens.all()
695 feed_tokens = feed_tokens.all()
696 if feed_tokens:
696 if feed_tokens:
697 return feed_tokens[0].api_key
697 return feed_tokens[0].api_key
698 return 'NO_FEED_TOKEN_AVAILABLE'
698 return 'NO_FEED_TOKEN_AVAILABLE'
699
699
700 @classmethod
700 @classmethod
701 def get(cls, user_id, cache=False):
701 def get(cls, user_id, cache=False):
702 if not user_id:
702 if not user_id:
703 return
703 return
704
704
705 user = cls.query()
705 user = cls.query()
706 if cache:
706 if cache:
707 user = user.options(
707 user = user.options(
708 FromCache("sql_cache_short", "get_users_%s" % user_id))
708 FromCache("sql_cache_short", "get_users_%s" % user_id))
709 return user.get(user_id)
709 return user.get(user_id)
710
710
711 @classmethod
711 @classmethod
712 def extra_valid_auth_tokens(cls, user, role=None):
712 def extra_valid_auth_tokens(cls, user, role=None):
713 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
713 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
714 .filter(or_(UserApiKeys.expires == -1,
714 .filter(or_(UserApiKeys.expires == -1,
715 UserApiKeys.expires >= time.time()))
715 UserApiKeys.expires >= time.time()))
716 if role:
716 if role:
717 tokens = tokens.filter(or_(UserApiKeys.role == role,
717 tokens = tokens.filter(or_(UserApiKeys.role == role,
718 UserApiKeys.role == UserApiKeys.ROLE_ALL))
718 UserApiKeys.role == UserApiKeys.ROLE_ALL))
719 return tokens.all()
719 return tokens.all()
720
720
721 def authenticate_by_token(self, auth_token, roles=None, scope_repo_id=None):
721 def authenticate_by_token(self, auth_token, roles=None, scope_repo_id=None):
722 from rhodecode.lib import auth
722 from rhodecode.lib import auth
723
723
724 log.debug('Trying to authenticate user: %s via auth-token, '
724 log.debug('Trying to authenticate user: %s via auth-token, '
725 'and roles: %s', self, roles)
725 'and roles: %s', self, roles)
726
726
727 if not auth_token:
727 if not auth_token:
728 return False
728 return False
729
729
730 crypto_backend = auth.crypto_backend()
730 crypto_backend = auth.crypto_backend()
731
731
732 roles = (roles or []) + [UserApiKeys.ROLE_ALL]
732 roles = (roles or []) + [UserApiKeys.ROLE_ALL]
733 tokens_q = UserApiKeys.query()\
733 tokens_q = UserApiKeys.query()\
734 .filter(UserApiKeys.user_id == self.user_id)\
734 .filter(UserApiKeys.user_id == self.user_id)\
735 .filter(or_(UserApiKeys.expires == -1,
735 .filter(or_(UserApiKeys.expires == -1,
736 UserApiKeys.expires >= time.time()))
736 UserApiKeys.expires >= time.time()))
737
737
738 tokens_q = tokens_q.filter(UserApiKeys.role.in_(roles))
738 tokens_q = tokens_q.filter(UserApiKeys.role.in_(roles))
739
739
740 plain_tokens = []
740 plain_tokens = []
741 hash_tokens = []
741 hash_tokens = []
742
742
743 for token in tokens_q.all():
743 for token in tokens_q.all():
744 # verify scope first
744 # verify scope first
745 if token.repo_id:
745 if token.repo_id:
746 # token has a scope, we need to verify it
746 # token has a scope, we need to verify it
747 if scope_repo_id != token.repo_id:
747 if scope_repo_id != token.repo_id:
748 log.debug(
748 log.debug(
749 'Scope mismatch: token has a set repo scope: %s, '
749 'Scope mismatch: token has a set repo scope: %s, '
750 'and calling scope is:%s, skipping further checks',
750 'and calling scope is:%s, skipping further checks',
751 token.repo, scope_repo_id)
751 token.repo, scope_repo_id)
752 # token has a scope, and it doesn't match, skip token
752 # token has a scope, and it doesn't match, skip token
753 continue
753 continue
754
754
755 if token.api_key.startswith(crypto_backend.ENC_PREF):
755 if token.api_key.startswith(crypto_backend.ENC_PREF):
756 hash_tokens.append(token.api_key)
756 hash_tokens.append(token.api_key)
757 else:
757 else:
758 plain_tokens.append(token.api_key)
758 plain_tokens.append(token.api_key)
759
759
760 is_plain_match = auth_token in plain_tokens
760 is_plain_match = auth_token in plain_tokens
761 if is_plain_match:
761 if is_plain_match:
762 return True
762 return True
763
763
764 for hashed in hash_tokens:
764 for hashed in hash_tokens:
765 # TODO(marcink): this is expensive to calculate, but most secure
765 # TODO(marcink): this is expensive to calculate, but most secure
766 match = crypto_backend.hash_check(auth_token, hashed)
766 match = crypto_backend.hash_check(auth_token, hashed)
767 if match:
767 if match:
768 return True
768 return True
769
769
770 return False
770 return False
771
771
772 @property
772 @property
773 def ip_addresses(self):
773 def ip_addresses(self):
774 ret = UserIpMap.query().filter(UserIpMap.user == self).all()
774 ret = UserIpMap.query().filter(UserIpMap.user == self).all()
775 return [x.ip_addr for x in ret]
775 return [x.ip_addr for x in ret]
776
776
777 @property
777 @property
778 def username_and_name(self):
778 def username_and_name(self):
779 return '%s (%s %s)' % (self.username, self.first_name, self.last_name)
779 return '%s (%s %s)' % (self.username, self.first_name, self.last_name)
780
780
781 @property
781 @property
782 def username_or_name_or_email(self):
782 def username_or_name_or_email(self):
783 full_name = self.full_name if self.full_name is not ' ' else None
783 full_name = self.full_name if self.full_name is not ' ' else None
784 return self.username or full_name or self.email
784 return self.username or full_name or self.email
785
785
786 @property
786 @property
787 def full_name(self):
787 def full_name(self):
788 return '%s %s' % (self.first_name, self.last_name)
788 return '%s %s' % (self.first_name, self.last_name)
789
789
790 @property
790 @property
791 def full_name_or_username(self):
791 def full_name_or_username(self):
792 return ('%s %s' % (self.first_name, self.last_name)
792 return ('%s %s' % (self.first_name, self.last_name)
793 if (self.first_name and self.last_name) else self.username)
793 if (self.first_name and self.last_name) else self.username)
794
794
795 @property
795 @property
796 def full_contact(self):
796 def full_contact(self):
797 return '%s %s <%s>' % (self.first_name, self.last_name, self.email)
797 return '%s %s <%s>' % (self.first_name, self.last_name, self.email)
798
798
799 @property
799 @property
800 def short_contact(self):
800 def short_contact(self):
801 return '%s %s' % (self.first_name, self.last_name)
801 return '%s %s' % (self.first_name, self.last_name)
802
802
803 @property
803 @property
804 def is_admin(self):
804 def is_admin(self):
805 return self.admin
805 return self.admin
806
806
807 def AuthUser(self, **kwargs):
807 def AuthUser(self, **kwargs):
808 """
808 """
809 Returns instance of AuthUser for this user
809 Returns instance of AuthUser for this user
810 """
810 """
811 from rhodecode.lib.auth import AuthUser
811 from rhodecode.lib.auth import AuthUser
812 return AuthUser(user_id=self.user_id, username=self.username, **kwargs)
812 return AuthUser(user_id=self.user_id, username=self.username, **kwargs)
813
813
814 @hybrid_property
814 @hybrid_property
815 def user_data(self):
815 def user_data(self):
816 if not self._user_data:
816 if not self._user_data:
817 return {}
817 return {}
818
818
819 try:
819 try:
820 return json.loads(self._user_data)
820 return json.loads(self._user_data)
821 except TypeError:
821 except TypeError:
822 return {}
822 return {}
823
823
824 @user_data.setter
824 @user_data.setter
825 def user_data(self, val):
825 def user_data(self, val):
826 if not isinstance(val, dict):
826 if not isinstance(val, dict):
827 raise Exception('user_data must be dict, got %s' % type(val))
827 raise Exception('user_data must be dict, got %s' % type(val))
828 try:
828 try:
829 self._user_data = json.dumps(val)
829 self._user_data = json.dumps(val)
830 except Exception:
830 except Exception:
831 log.error(traceback.format_exc())
831 log.error(traceback.format_exc())
832
832
833 @classmethod
833 @classmethod
834 def get_by_username(cls, username, case_insensitive=False,
834 def get_by_username(cls, username, case_insensitive=False,
835 cache=False, identity_cache=False):
835 cache=False, identity_cache=False):
836 session = Session()
836 session = Session()
837
837
838 if case_insensitive:
838 if case_insensitive:
839 q = cls.query().filter(
839 q = cls.query().filter(
840 func.lower(cls.username) == func.lower(username))
840 func.lower(cls.username) == func.lower(username))
841 else:
841 else:
842 q = cls.query().filter(cls.username == username)
842 q = cls.query().filter(cls.username == username)
843
843
844 if cache:
844 if cache:
845 if identity_cache:
845 if identity_cache:
846 val = cls.identity_cache(session, 'username', username)
846 val = cls.identity_cache(session, 'username', username)
847 if val:
847 if val:
848 return val
848 return val
849 else:
849 else:
850 cache_key = "get_user_by_name_%s" % _hash_key(username)
850 cache_key = "get_user_by_name_%s" % _hash_key(username)
851 q = q.options(
851 q = q.options(
852 FromCache("sql_cache_short", cache_key))
852 FromCache("sql_cache_short", cache_key))
853
853
854 return q.scalar()
854 return q.scalar()
855
855
856 @classmethod
856 @classmethod
857 def get_by_auth_token(cls, auth_token, cache=False):
857 def get_by_auth_token(cls, auth_token, cache=False):
858 q = UserApiKeys.query()\
858 q = UserApiKeys.query()\
859 .filter(UserApiKeys.api_key == auth_token)\
859 .filter(UserApiKeys.api_key == auth_token)\
860 .filter(or_(UserApiKeys.expires == -1,
860 .filter(or_(UserApiKeys.expires == -1,
861 UserApiKeys.expires >= time.time()))
861 UserApiKeys.expires >= time.time()))
862 if cache:
862 if cache:
863 q = q.options(
863 q = q.options(
864 FromCache("sql_cache_short", "get_auth_token_%s" % auth_token))
864 FromCache("sql_cache_short", "get_auth_token_%s" % auth_token))
865
865
866 match = q.first()
866 match = q.first()
867 if match:
867 if match:
868 return match.user
868 return match.user
869
869
870 @classmethod
870 @classmethod
871 def get_by_email(cls, email, case_insensitive=False, cache=False):
871 def get_by_email(cls, email, case_insensitive=False, cache=False):
872
872
873 if case_insensitive:
873 if case_insensitive:
874 q = cls.query().filter(func.lower(cls.email) == func.lower(email))
874 q = cls.query().filter(func.lower(cls.email) == func.lower(email))
875
875
876 else:
876 else:
877 q = cls.query().filter(cls.email == email)
877 q = cls.query().filter(cls.email == email)
878
878
879 email_key = _hash_key(email)
879 email_key = _hash_key(email)
880 if cache:
880 if cache:
881 q = q.options(
881 q = q.options(
882 FromCache("sql_cache_short", "get_email_key_%s" % email_key))
882 FromCache("sql_cache_short", "get_email_key_%s" % email_key))
883
883
884 ret = q.scalar()
884 ret = q.scalar()
885 if ret is None:
885 if ret is None:
886 q = UserEmailMap.query()
886 q = UserEmailMap.query()
887 # try fetching in alternate email map
887 # try fetching in alternate email map
888 if case_insensitive:
888 if case_insensitive:
889 q = q.filter(func.lower(UserEmailMap.email) == func.lower(email))
889 q = q.filter(func.lower(UserEmailMap.email) == func.lower(email))
890 else:
890 else:
891 q = q.filter(UserEmailMap.email == email)
891 q = q.filter(UserEmailMap.email == email)
892 q = q.options(joinedload(UserEmailMap.user))
892 q = q.options(joinedload(UserEmailMap.user))
893 if cache:
893 if cache:
894 q = q.options(
894 q = q.options(
895 FromCache("sql_cache_short", "get_email_map_key_%s" % email_key))
895 FromCache("sql_cache_short", "get_email_map_key_%s" % email_key))
896 ret = getattr(q.scalar(), 'user', None)
896 ret = getattr(q.scalar(), 'user', None)
897
897
898 return ret
898 return ret
899
899
900 @classmethod
900 @classmethod
901 def get_from_cs_author(cls, author):
901 def get_from_cs_author(cls, author):
902 """
902 """
903 Tries to get User objects out of commit author string
903 Tries to get User objects out of commit author string
904
904
905 :param author:
905 :param author:
906 """
906 """
907 from rhodecode.lib.helpers import email, author_name
907 from rhodecode.lib.helpers import email, author_name
908 # Valid email in the attribute passed, see if they're in the system
908 # Valid email in the attribute passed, see if they're in the system
909 _email = email(author)
909 _email = email(author)
910 if _email:
910 if _email:
911 user = cls.get_by_email(_email, case_insensitive=True)
911 user = cls.get_by_email(_email, case_insensitive=True)
912 if user:
912 if user:
913 return user
913 return user
914 # Maybe we can match by username?
914 # Maybe we can match by username?
915 _author = author_name(author)
915 _author = author_name(author)
916 user = cls.get_by_username(_author, case_insensitive=True)
916 user = cls.get_by_username(_author, case_insensitive=True)
917 if user:
917 if user:
918 return user
918 return user
919
919
920 def update_userdata(self, **kwargs):
920 def update_userdata(self, **kwargs):
921 usr = self
921 usr = self
922 old = usr.user_data
922 old = usr.user_data
923 old.update(**kwargs)
923 old.update(**kwargs)
924 usr.user_data = old
924 usr.user_data = old
925 Session().add(usr)
925 Session().add(usr)
926 log.debug('updated userdata with ', kwargs)
926 log.debug('updated userdata with ', kwargs)
927
927
928 def update_lastlogin(self):
928 def update_lastlogin(self):
929 """Update user lastlogin"""
929 """Update user lastlogin"""
930 self.last_login = datetime.datetime.now()
930 self.last_login = datetime.datetime.now()
931 Session().add(self)
931 Session().add(self)
932 log.debug('updated user %s lastlogin', self.username)
932 log.debug('updated user %s lastlogin', self.username)
933
933
934 def update_lastactivity(self):
934 def update_lastactivity(self):
935 """Update user lastactivity"""
935 """Update user lastactivity"""
936 self.last_activity = datetime.datetime.now()
936 self.last_activity = datetime.datetime.now()
937 Session().add(self)
937 Session().add(self)
938 log.debug('updated user `%s` last activity', self.username)
938 log.debug('updated user `%s` last activity', self.username)
939
939
940 def update_password(self, new_password):
940 def update_password(self, new_password):
941 from rhodecode.lib.auth import get_crypt_password
941 from rhodecode.lib.auth import get_crypt_password
942
942
943 self.password = get_crypt_password(new_password)
943 self.password = get_crypt_password(new_password)
944 Session().add(self)
944 Session().add(self)
945
945
946 @classmethod
946 @classmethod
947 def get_first_super_admin(cls):
947 def get_first_super_admin(cls):
948 user = User.query().filter(User.admin == true()).first()
948 user = User.query().filter(User.admin == true()).first()
949 if user is None:
949 if user is None:
950 raise Exception('FATAL: Missing administrative account!')
950 raise Exception('FATAL: Missing administrative account!')
951 return user
951 return user
952
952
953 @classmethod
953 @classmethod
954 def get_all_super_admins(cls):
954 def get_all_super_admins(cls):
955 """
955 """
956 Returns all admin accounts sorted by username
956 Returns all admin accounts sorted by username
957 """
957 """
958 return User.query().filter(User.admin == true())\
958 return User.query().filter(User.admin == true())\
959 .order_by(User.username.asc()).all()
959 .order_by(User.username.asc()).all()
960
960
961 @classmethod
961 @classmethod
962 def get_default_user(cls, cache=False, refresh=False):
962 def get_default_user(cls, cache=False, refresh=False):
963 user = User.get_by_username(User.DEFAULT_USER, cache=cache)
963 user = User.get_by_username(User.DEFAULT_USER, cache=cache)
964 if user is None:
964 if user is None:
965 raise Exception('FATAL: Missing default account!')
965 raise Exception('FATAL: Missing default account!')
966 if refresh:
966 if refresh:
967 # The default user might be based on outdated state which
967 # The default user might be based on outdated state which
968 # has been loaded from the cache.
968 # has been loaded from the cache.
969 # A call to refresh() ensures that the
969 # A call to refresh() ensures that the
970 # latest state from the database is used.
970 # latest state from the database is used.
971 Session().refresh(user)
971 Session().refresh(user)
972 return user
972 return user
973
973
974 def _get_default_perms(self, user, suffix=''):
974 def _get_default_perms(self, user, suffix=''):
975 from rhodecode.model.permission import PermissionModel
975 from rhodecode.model.permission import PermissionModel
976 return PermissionModel().get_default_perms(user.user_perms, suffix)
976 return PermissionModel().get_default_perms(user.user_perms, suffix)
977
977
978 def get_default_perms(self, suffix=''):
978 def get_default_perms(self, suffix=''):
979 return self._get_default_perms(self, suffix)
979 return self._get_default_perms(self, suffix)
980
980
981 def get_api_data(self, include_secrets=False, details='full'):
981 def get_api_data(self, include_secrets=False, details='full'):
982 """
982 """
983 Common function for generating user related data for API
983 Common function for generating user related data for API
984
984
985 :param include_secrets: By default secrets in the API data will be replaced
985 :param include_secrets: By default secrets in the API data will be replaced
986 by a placeholder value to prevent exposing this data by accident. In case
986 by a placeholder value to prevent exposing this data by accident. In case
987 this data shall be exposed, set this flag to ``True``.
987 this data shall be exposed, set this flag to ``True``.
988
988
989 :param details: details can be 'basic|full' basic gives only a subset of
989 :param details: details can be 'basic|full' basic gives only a subset of
990 the available user information that includes user_id, name and emails.
990 the available user information that includes user_id, name and emails.
991 """
991 """
992 user = self
992 user = self
993 user_data = self.user_data
993 user_data = self.user_data
994 data = {
994 data = {
995 'user_id': user.user_id,
995 'user_id': user.user_id,
996 'username': user.username,
996 'username': user.username,
997 'firstname': user.name,
997 'firstname': user.name,
998 'lastname': user.lastname,
998 'lastname': user.lastname,
999 'email': user.email,
999 'email': user.email,
1000 'emails': user.emails,
1000 'emails': user.emails,
1001 }
1001 }
1002 if details == 'basic':
1002 if details == 'basic':
1003 return data
1003 return data
1004
1004
1005 auth_token_length = 40
1005 auth_token_length = 40
1006 auth_token_replacement = '*' * auth_token_length
1006 auth_token_replacement = '*' * auth_token_length
1007
1007
1008 extras = {
1008 extras = {
1009 'auth_tokens': [auth_token_replacement],
1009 'auth_tokens': [auth_token_replacement],
1010 'active': user.active,
1010 'active': user.active,
1011 'admin': user.admin,
1011 'admin': user.admin,
1012 'extern_type': user.extern_type,
1012 'extern_type': user.extern_type,
1013 'extern_name': user.extern_name,
1013 'extern_name': user.extern_name,
1014 'last_login': user.last_login,
1014 'last_login': user.last_login,
1015 'last_activity': user.last_activity,
1015 'last_activity': user.last_activity,
1016 'ip_addresses': user.ip_addresses,
1016 'ip_addresses': user.ip_addresses,
1017 'language': user_data.get('language')
1017 'language': user_data.get('language')
1018 }
1018 }
1019 data.update(extras)
1019 data.update(extras)
1020
1020
1021 if include_secrets:
1021 if include_secrets:
1022 data['auth_tokens'] = user.auth_tokens
1022 data['auth_tokens'] = user.auth_tokens
1023 return data
1023 return data
1024
1024
1025 def __json__(self):
1025 def __json__(self):
1026 data = {
1026 data = {
1027 'full_name': self.full_name,
1027 'full_name': self.full_name,
1028 'full_name_or_username': self.full_name_or_username,
1028 'full_name_or_username': self.full_name_or_username,
1029 'short_contact': self.short_contact,
1029 'short_contact': self.short_contact,
1030 'full_contact': self.full_contact,
1030 'full_contact': self.full_contact,
1031 }
1031 }
1032 data.update(self.get_api_data())
1032 data.update(self.get_api_data())
1033 return data
1033 return data
1034
1034
1035
1035
1036 class UserApiKeys(Base, BaseModel):
1036 class UserApiKeys(Base, BaseModel):
1037 __tablename__ = 'user_api_keys'
1037 __tablename__ = 'user_api_keys'
1038 __table_args__ = (
1038 __table_args__ = (
1039 Index('uak_api_key_idx', 'api_key', unique=True),
1039 Index('uak_api_key_idx', 'api_key', unique=True),
1040 Index('uak_api_key_expires_idx', 'api_key', 'expires'),
1040 Index('uak_api_key_expires_idx', 'api_key', 'expires'),
1041 base_table_args
1041 base_table_args
1042 )
1042 )
1043 __mapper_args__ = {}
1043 __mapper_args__ = {}
1044
1044
1045 # ApiKey role
1045 # ApiKey role
1046 ROLE_ALL = 'token_role_all'
1046 ROLE_ALL = 'token_role_all'
1047 ROLE_HTTP = 'token_role_http'
1047 ROLE_HTTP = 'token_role_http'
1048 ROLE_VCS = 'token_role_vcs'
1048 ROLE_VCS = 'token_role_vcs'
1049 ROLE_API = 'token_role_api'
1049 ROLE_API = 'token_role_api'
1050 ROLE_FEED = 'token_role_feed'
1050 ROLE_FEED = 'token_role_feed'
1051 ROLE_PASSWORD_RESET = 'token_password_reset'
1051 ROLE_PASSWORD_RESET = 'token_password_reset'
1052
1052
1053 ROLES = [ROLE_ALL, ROLE_HTTP, ROLE_VCS, ROLE_API, ROLE_FEED]
1053 ROLES = [ROLE_ALL, ROLE_HTTP, ROLE_VCS, ROLE_API, ROLE_FEED]
1054
1054
1055 user_api_key_id = Column("user_api_key_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1055 user_api_key_id = Column("user_api_key_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1056 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1056 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1057 api_key = Column("api_key", String(255), nullable=False, unique=True)
1057 api_key = Column("api_key", String(255), nullable=False, unique=True)
1058 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1058 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1059 expires = Column('expires', Float(53), nullable=False)
1059 expires = Column('expires', Float(53), nullable=False)
1060 role = Column('role', String(255), nullable=True)
1060 role = Column('role', String(255), nullable=True)
1061 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1061 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1062
1062
1063 # scope columns
1063 # scope columns
1064 repo_id = Column(
1064 repo_id = Column(
1065 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
1065 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
1066 nullable=True, unique=None, default=None)
1066 nullable=True, unique=None, default=None)
1067 repo = relationship('Repository', lazy='joined')
1067 repo = relationship('Repository', lazy='joined')
1068
1068
1069 repo_group_id = Column(
1069 repo_group_id = Column(
1070 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
1070 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
1071 nullable=True, unique=None, default=None)
1071 nullable=True, unique=None, default=None)
1072 repo_group = relationship('RepoGroup', lazy='joined')
1072 repo_group = relationship('RepoGroup', lazy='joined')
1073
1073
1074 user = relationship('User', lazy='joined')
1074 user = relationship('User', lazy='joined')
1075
1075
1076 def __unicode__(self):
1076 def __unicode__(self):
1077 return u"<%s('%s')>" % (self.__class__.__name__, self.role)
1077 return u"<%s('%s')>" % (self.__class__.__name__, self.role)
1078
1078
1079 def __json__(self):
1079 def __json__(self):
1080 data = {
1080 data = {
1081 'auth_token': self.api_key,
1081 'auth_token': self.api_key,
1082 'role': self.role,
1082 'role': self.role,
1083 'scope': self.scope_humanized,
1083 'scope': self.scope_humanized,
1084 'expired': self.expired
1084 'expired': self.expired
1085 }
1085 }
1086 return data
1086 return data
1087
1087
1088 def get_api_data(self, include_secrets=False):
1088 def get_api_data(self, include_secrets=False):
1089 data = self.__json__()
1089 data = self.__json__()
1090 if include_secrets:
1090 if include_secrets:
1091 return data
1091 return data
1092 else:
1092 else:
1093 data['auth_token'] = self.token_obfuscated
1093 data['auth_token'] = self.token_obfuscated
1094 return data
1094 return data
1095
1095
1096 @hybrid_property
1096 @hybrid_property
1097 def description_safe(self):
1097 def description_safe(self):
1098 from rhodecode.lib import helpers as h
1098 from rhodecode.lib import helpers as h
1099 return h.escape(self.description)
1099 return h.escape(self.description)
1100
1100
1101 @property
1101 @property
1102 def expired(self):
1102 def expired(self):
1103 if self.expires == -1:
1103 if self.expires == -1:
1104 return False
1104 return False
1105 return time.time() > self.expires
1105 return time.time() > self.expires
1106
1106
1107 @classmethod
1107 @classmethod
1108 def _get_role_name(cls, role):
1108 def _get_role_name(cls, role):
1109 return {
1109 return {
1110 cls.ROLE_ALL: _('all'),
1110 cls.ROLE_ALL: _('all'),
1111 cls.ROLE_HTTP: _('http/web interface'),
1111 cls.ROLE_HTTP: _('http/web interface'),
1112 cls.ROLE_VCS: _('vcs (git/hg/svn protocol)'),
1112 cls.ROLE_VCS: _('vcs (git/hg/svn protocol)'),
1113 cls.ROLE_API: _('api calls'),
1113 cls.ROLE_API: _('api calls'),
1114 cls.ROLE_FEED: _('feed access'),
1114 cls.ROLE_FEED: _('feed access'),
1115 }.get(role, role)
1115 }.get(role, role)
1116
1116
1117 @property
1117 @property
1118 def role_humanized(self):
1118 def role_humanized(self):
1119 return self._get_role_name(self.role)
1119 return self._get_role_name(self.role)
1120
1120
1121 def _get_scope(self):
1121 def _get_scope(self):
1122 if self.repo:
1122 if self.repo:
1123 return repr(self.repo)
1123 return repr(self.repo)
1124 if self.repo_group:
1124 if self.repo_group:
1125 return repr(self.repo_group) + ' (recursive)'
1125 return repr(self.repo_group) + ' (recursive)'
1126 return 'global'
1126 return 'global'
1127
1127
1128 @property
1128 @property
1129 def scope_humanized(self):
1129 def scope_humanized(self):
1130 return self._get_scope()
1130 return self._get_scope()
1131
1131
1132 @property
1132 @property
1133 def token_obfuscated(self):
1133 def token_obfuscated(self):
1134 if self.api_key:
1134 if self.api_key:
1135 return self.api_key[:4] + "****"
1135 return self.api_key[:4] + "****"
1136
1136
1137
1137
1138 class UserEmailMap(Base, BaseModel):
1138 class UserEmailMap(Base, BaseModel):
1139 __tablename__ = 'user_email_map'
1139 __tablename__ = 'user_email_map'
1140 __table_args__ = (
1140 __table_args__ = (
1141 Index('uem_email_idx', 'email'),
1141 Index('uem_email_idx', 'email'),
1142 UniqueConstraint('email'),
1142 UniqueConstraint('email'),
1143 base_table_args
1143 base_table_args
1144 )
1144 )
1145 __mapper_args__ = {}
1145 __mapper_args__ = {}
1146
1146
1147 email_id = Column("email_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1147 email_id = Column("email_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1148 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1148 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1149 _email = Column("email", String(255), nullable=True, unique=False, default=None)
1149 _email = Column("email", String(255), nullable=True, unique=False, default=None)
1150 user = relationship('User', lazy='joined')
1150 user = relationship('User', lazy='joined')
1151
1151
1152 @validates('_email')
1152 @validates('_email')
1153 def validate_email(self, key, email):
1153 def validate_email(self, key, email):
1154 # check if this email is not main one
1154 # check if this email is not main one
1155 main_email = Session().query(User).filter(User.email == email).scalar()
1155 main_email = Session().query(User).filter(User.email == email).scalar()
1156 if main_email is not None:
1156 if main_email is not None:
1157 raise AttributeError('email %s is present is user table' % email)
1157 raise AttributeError('email %s is present is user table' % email)
1158 return email
1158 return email
1159
1159
1160 @hybrid_property
1160 @hybrid_property
1161 def email(self):
1161 def email(self):
1162 return self._email
1162 return self._email
1163
1163
1164 @email.setter
1164 @email.setter
1165 def email(self, val):
1165 def email(self, val):
1166 self._email = val.lower() if val else None
1166 self._email = val.lower() if val else None
1167
1167
1168
1168
1169 class UserIpMap(Base, BaseModel):
1169 class UserIpMap(Base, BaseModel):
1170 __tablename__ = 'user_ip_map'
1170 __tablename__ = 'user_ip_map'
1171 __table_args__ = (
1171 __table_args__ = (
1172 UniqueConstraint('user_id', 'ip_addr'),
1172 UniqueConstraint('user_id', 'ip_addr'),
1173 base_table_args
1173 base_table_args
1174 )
1174 )
1175 __mapper_args__ = {}
1175 __mapper_args__ = {}
1176
1176
1177 ip_id = Column("ip_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1177 ip_id = Column("ip_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1178 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1178 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1179 ip_addr = Column("ip_addr", String(255), nullable=True, unique=False, default=None)
1179 ip_addr = Column("ip_addr", String(255), nullable=True, unique=False, default=None)
1180 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
1180 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
1181 description = Column("description", String(10000), nullable=True, unique=None, default=None)
1181 description = Column("description", String(10000), nullable=True, unique=None, default=None)
1182 user = relationship('User', lazy='joined')
1182 user = relationship('User', lazy='joined')
1183
1183
1184 @hybrid_property
1184 @hybrid_property
1185 def description_safe(self):
1185 def description_safe(self):
1186 from rhodecode.lib import helpers as h
1186 from rhodecode.lib import helpers as h
1187 return h.escape(self.description)
1187 return h.escape(self.description)
1188
1188
1189 @classmethod
1189 @classmethod
1190 def _get_ip_range(cls, ip_addr):
1190 def _get_ip_range(cls, ip_addr):
1191 net = ipaddress.ip_network(safe_unicode(ip_addr), strict=False)
1191 net = ipaddress.ip_network(safe_unicode(ip_addr), strict=False)
1192 return [str(net.network_address), str(net.broadcast_address)]
1192 return [str(net.network_address), str(net.broadcast_address)]
1193
1193
1194 def __json__(self):
1194 def __json__(self):
1195 return {
1195 return {
1196 'ip_addr': self.ip_addr,
1196 'ip_addr': self.ip_addr,
1197 'ip_range': self._get_ip_range(self.ip_addr),
1197 'ip_range': self._get_ip_range(self.ip_addr),
1198 }
1198 }
1199
1199
1200 def __unicode__(self):
1200 def __unicode__(self):
1201 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
1201 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
1202 self.user_id, self.ip_addr)
1202 self.user_id, self.ip_addr)
1203
1203
1204
1204
1205 class UserSshKeys(Base, BaseModel):
1205 class UserSshKeys(Base, BaseModel):
1206 __tablename__ = 'user_ssh_keys'
1206 __tablename__ = 'user_ssh_keys'
1207 __table_args__ = (
1207 __table_args__ = (
1208 Index('usk_ssh_key_fingerprint_idx', 'ssh_key_fingerprint'),
1208 Index('usk_ssh_key_fingerprint_idx', 'ssh_key_fingerprint'),
1209
1209
1210 UniqueConstraint('ssh_key_fingerprint'),
1210 UniqueConstraint('ssh_key_fingerprint'),
1211
1211
1212 base_table_args
1212 base_table_args
1213 )
1213 )
1214 __mapper_args__ = {}
1214 __mapper_args__ = {}
1215
1215
1216 ssh_key_id = Column('ssh_key_id', Integer(), nullable=False, unique=True, default=None, primary_key=True)
1216 ssh_key_id = Column('ssh_key_id', Integer(), nullable=False, unique=True, default=None, primary_key=True)
1217 ssh_key_data = Column('ssh_key_data', String(10240), nullable=False, unique=None, default=None)
1217 ssh_key_data = Column('ssh_key_data', String(10240), nullable=False, unique=None, default=None)
1218 ssh_key_fingerprint = Column('ssh_key_fingerprint', String(255), nullable=False, unique=None, default=None)
1218 ssh_key_fingerprint = Column('ssh_key_fingerprint', String(255), nullable=False, unique=None, default=None)
1219
1219
1220 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1220 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1221
1221
1222 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1222 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1223 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True, default=None)
1223 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True, default=None)
1224 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1224 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1225
1225
1226 user = relationship('User', lazy='joined')
1226 user = relationship('User', lazy='joined')
1227
1227
1228 def __json__(self):
1228 def __json__(self):
1229 data = {
1229 data = {
1230 'ssh_fingerprint': self.ssh_key_fingerprint,
1230 'ssh_fingerprint': self.ssh_key_fingerprint,
1231 'description': self.description,
1231 'description': self.description,
1232 'created_on': self.created_on
1232 'created_on': self.created_on
1233 }
1233 }
1234 return data
1234 return data
1235
1235
1236 def get_api_data(self):
1236 def get_api_data(self):
1237 data = self.__json__()
1237 data = self.__json__()
1238 return data
1238 return data
1239
1239
1240
1240
1241 class UserLog(Base, BaseModel):
1241 class UserLog(Base, BaseModel):
1242 __tablename__ = 'user_logs'
1242 __tablename__ = 'user_logs'
1243 __table_args__ = (
1243 __table_args__ = (
1244 base_table_args,
1244 base_table_args,
1245 )
1245 )
1246
1246
1247 VERSION_1 = 'v1'
1247 VERSION_1 = 'v1'
1248 VERSION_2 = 'v2'
1248 VERSION_2 = 'v2'
1249 VERSIONS = [VERSION_1, VERSION_2]
1249 VERSIONS = [VERSION_1, VERSION_2]
1250
1250
1251 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1251 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1252 user_id = Column("user_id", Integer(), ForeignKey('users.user_id',ondelete='SET NULL'), nullable=True, unique=None, default=None)
1252 user_id = Column("user_id", Integer(), ForeignKey('users.user_id',ondelete='SET NULL'), nullable=True, unique=None, default=None)
1253 username = Column("username", String(255), nullable=True, unique=None, default=None)
1253 username = Column("username", String(255), nullable=True, unique=None, default=None)
1254 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id', ondelete='SET NULL'), nullable=True, unique=None, default=None)
1254 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id', ondelete='SET NULL'), nullable=True, unique=None, default=None)
1255 repository_name = Column("repository_name", String(255), nullable=True, unique=None, default=None)
1255 repository_name = Column("repository_name", String(255), nullable=True, unique=None, default=None)
1256 user_ip = Column("user_ip", String(255), nullable=True, unique=None, default=None)
1256 user_ip = Column("user_ip", String(255), nullable=True, unique=None, default=None)
1257 action = Column("action", Text().with_variant(Text(1200000), 'mysql'), nullable=True, unique=None, default=None)
1257 action = Column("action", Text().with_variant(Text(1200000), 'mysql'), nullable=True, unique=None, default=None)
1258 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
1258 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
1259
1259
1260 version = Column("version", String(255), nullable=True, default=VERSION_1)
1260 version = Column("version", String(255), nullable=True, default=VERSION_1)
1261 user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1261 user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1262 action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1262 action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1263
1263
1264 def __unicode__(self):
1264 def __unicode__(self):
1265 return u"<%s('id:%s:%s')>" % (
1265 return u"<%s('id:%s:%s')>" % (
1266 self.__class__.__name__, self.repository_name, self.action)
1266 self.__class__.__name__, self.repository_name, self.action)
1267
1267
1268 def __json__(self):
1268 def __json__(self):
1269 return {
1269 return {
1270 'user_id': self.user_id,
1270 'user_id': self.user_id,
1271 'username': self.username,
1271 'username': self.username,
1272 'repository_id': self.repository_id,
1272 'repository_id': self.repository_id,
1273 'repository_name': self.repository_name,
1273 'repository_name': self.repository_name,
1274 'user_ip': self.user_ip,
1274 'user_ip': self.user_ip,
1275 'action_date': self.action_date,
1275 'action_date': self.action_date,
1276 'action': self.action,
1276 'action': self.action,
1277 }
1277 }
1278
1278
1279 @hybrid_property
1279 @hybrid_property
1280 def entry_id(self):
1280 def entry_id(self):
1281 return self.user_log_id
1281 return self.user_log_id
1282
1282
1283 @property
1283 @property
1284 def action_as_day(self):
1284 def action_as_day(self):
1285 return datetime.date(*self.action_date.timetuple()[:3])
1285 return datetime.date(*self.action_date.timetuple()[:3])
1286
1286
1287 user = relationship('User')
1287 user = relationship('User')
1288 repository = relationship('Repository', cascade='')
1288 repository = relationship('Repository', cascade='')
1289
1289
1290
1290
1291 class UserGroup(Base, BaseModel):
1291 class UserGroup(Base, BaseModel):
1292 __tablename__ = 'users_groups'
1292 __tablename__ = 'users_groups'
1293 __table_args__ = (
1293 __table_args__ = (
1294 base_table_args,
1294 base_table_args,
1295 )
1295 )
1296
1296
1297 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1297 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1298 users_group_name = Column("users_group_name", String(255), nullable=False, unique=True, default=None)
1298 users_group_name = Column("users_group_name", String(255), nullable=False, unique=True, default=None)
1299 user_group_description = Column("user_group_description", String(10000), nullable=True, unique=None, default=None)
1299 user_group_description = Column("user_group_description", String(10000), nullable=True, unique=None, default=None)
1300 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
1300 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
1301 inherit_default_permissions = Column("users_group_inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
1301 inherit_default_permissions = Column("users_group_inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
1302 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
1302 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
1303 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1303 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1304 _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data
1304 _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data
1305
1305
1306 members = relationship('UserGroupMember', cascade="all, delete, delete-orphan", lazy="joined")
1306 members = relationship('UserGroupMember', cascade="all, delete, delete-orphan", lazy="joined")
1307 users_group_to_perm = relationship('UserGroupToPerm', cascade='all')
1307 users_group_to_perm = relationship('UserGroupToPerm', cascade='all')
1308 users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1308 users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1309 users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
1309 users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
1310 user_user_group_to_perm = relationship('UserUserGroupToPerm', cascade='all')
1310 user_user_group_to_perm = relationship('UserUserGroupToPerm', cascade='all')
1311 user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all')
1311 user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all')
1312
1312
1313 user_group_review_rules = relationship('RepoReviewRuleUserGroup', cascade='all')
1313 user_group_review_rules = relationship('RepoReviewRuleUserGroup', cascade='all')
1314 user = relationship('User', primaryjoin="User.user_id==UserGroup.user_id")
1314 user = relationship('User', primaryjoin="User.user_id==UserGroup.user_id")
1315
1315
1316 @classmethod
1316 @classmethod
1317 def _load_group_data(cls, column):
1317 def _load_group_data(cls, column):
1318 if not column:
1318 if not column:
1319 return {}
1319 return {}
1320
1320
1321 try:
1321 try:
1322 return json.loads(column) or {}
1322 return json.loads(column) or {}
1323 except TypeError:
1323 except TypeError:
1324 return {}
1324 return {}
1325
1325
1326 @hybrid_property
1326 @hybrid_property
1327 def description_safe(self):
1327 def description_safe(self):
1328 from rhodecode.lib import helpers as h
1328 from rhodecode.lib import helpers as h
1329 return h.escape(self.user_group_description)
1329 return h.escape(self.user_group_description)
1330
1330
1331 @hybrid_property
1331 @hybrid_property
1332 def group_data(self):
1332 def group_data(self):
1333 return self._load_group_data(self._group_data)
1333 return self._load_group_data(self._group_data)
1334
1334
1335 @group_data.expression
1335 @group_data.expression
1336 def group_data(self, **kwargs):
1336 def group_data(self, **kwargs):
1337 return self._group_data
1337 return self._group_data
1338
1338
1339 @group_data.setter
1339 @group_data.setter
1340 def group_data(self, val):
1340 def group_data(self, val):
1341 try:
1341 try:
1342 self._group_data = json.dumps(val)
1342 self._group_data = json.dumps(val)
1343 except Exception:
1343 except Exception:
1344 log.error(traceback.format_exc())
1344 log.error(traceback.format_exc())
1345
1345
1346 @classmethod
1346 @classmethod
1347 def _load_sync(cls, group_data):
1347 def _load_sync(cls, group_data):
1348 if group_data:
1348 if group_data:
1349 return group_data.get('extern_type')
1349 return group_data.get('extern_type')
1350
1350
1351 @property
1351 @property
1352 def sync(self):
1352 def sync(self):
1353 return self._load_sync(self.group_data)
1353 return self._load_sync(self.group_data)
1354
1354
1355 def __unicode__(self):
1355 def __unicode__(self):
1356 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1356 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1357 self.users_group_id,
1357 self.users_group_id,
1358 self.users_group_name)
1358 self.users_group_name)
1359
1359
1360 @classmethod
1360 @classmethod
1361 def get_by_group_name(cls, group_name, cache=False,
1361 def get_by_group_name(cls, group_name, cache=False,
1362 case_insensitive=False):
1362 case_insensitive=False):
1363 if case_insensitive:
1363 if case_insensitive:
1364 q = cls.query().filter(func.lower(cls.users_group_name) ==
1364 q = cls.query().filter(func.lower(cls.users_group_name) ==
1365 func.lower(group_name))
1365 func.lower(group_name))
1366
1366
1367 else:
1367 else:
1368 q = cls.query().filter(cls.users_group_name == group_name)
1368 q = cls.query().filter(cls.users_group_name == group_name)
1369 if cache:
1369 if cache:
1370 q = q.options(
1370 q = q.options(
1371 FromCache("sql_cache_short", "get_group_%s" % _hash_key(group_name)))
1371 FromCache("sql_cache_short", "get_group_%s" % _hash_key(group_name)))
1372 return q.scalar()
1372 return q.scalar()
1373
1373
1374 @classmethod
1374 @classmethod
1375 def get(cls, user_group_id, cache=False):
1375 def get(cls, user_group_id, cache=False):
1376 if not user_group_id:
1376 if not user_group_id:
1377 return
1377 return
1378
1378
1379 user_group = cls.query()
1379 user_group = cls.query()
1380 if cache:
1380 if cache:
1381 user_group = user_group.options(
1381 user_group = user_group.options(
1382 FromCache("sql_cache_short", "get_users_group_%s" % user_group_id))
1382 FromCache("sql_cache_short", "get_users_group_%s" % user_group_id))
1383 return user_group.get(user_group_id)
1383 return user_group.get(user_group_id)
1384
1384
1385 def permissions(self, with_admins=True, with_owner=True):
1385 def permissions(self, with_admins=True, with_owner=True):
1386 q = UserUserGroupToPerm.query().filter(UserUserGroupToPerm.user_group == self)
1386 q = UserUserGroupToPerm.query().filter(UserUserGroupToPerm.user_group == self)
1387 q = q.options(joinedload(UserUserGroupToPerm.user_group),
1387 q = q.options(joinedload(UserUserGroupToPerm.user_group),
1388 joinedload(UserUserGroupToPerm.user),
1388 joinedload(UserUserGroupToPerm.user),
1389 joinedload(UserUserGroupToPerm.permission),)
1389 joinedload(UserUserGroupToPerm.permission),)
1390
1390
1391 # get owners and admins and permissions. We do a trick of re-writing
1391 # get owners and admins and permissions. We do a trick of re-writing
1392 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1392 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1393 # has a global reference and changing one object propagates to all
1393 # has a global reference and changing one object propagates to all
1394 # others. This means if admin is also an owner admin_row that change
1394 # others. This means if admin is also an owner admin_row that change
1395 # would propagate to both objects
1395 # would propagate to both objects
1396 perm_rows = []
1396 perm_rows = []
1397 for _usr in q.all():
1397 for _usr in q.all():
1398 usr = AttributeDict(_usr.user.get_dict())
1398 usr = AttributeDict(_usr.user.get_dict())
1399 usr.permission = _usr.permission.permission_name
1399 usr.permission = _usr.permission.permission_name
1400 perm_rows.append(usr)
1400 perm_rows.append(usr)
1401
1401
1402 # filter the perm rows by 'default' first and then sort them by
1402 # filter the perm rows by 'default' first and then sort them by
1403 # admin,write,read,none permissions sorted again alphabetically in
1403 # admin,write,read,none permissions sorted again alphabetically in
1404 # each group
1404 # each group
1405 perm_rows = sorted(perm_rows, key=display_user_sort)
1405 perm_rows = sorted(perm_rows, key=display_user_sort)
1406
1406
1407 _admin_perm = 'usergroup.admin'
1407 _admin_perm = 'usergroup.admin'
1408 owner_row = []
1408 owner_row = []
1409 if with_owner:
1409 if with_owner:
1410 usr = AttributeDict(self.user.get_dict())
1410 usr = AttributeDict(self.user.get_dict())
1411 usr.owner_row = True
1411 usr.owner_row = True
1412 usr.permission = _admin_perm
1412 usr.permission = _admin_perm
1413 owner_row.append(usr)
1413 owner_row.append(usr)
1414
1414
1415 super_admin_rows = []
1415 super_admin_rows = []
1416 if with_admins:
1416 if with_admins:
1417 for usr in User.get_all_super_admins():
1417 for usr in User.get_all_super_admins():
1418 # if this admin is also owner, don't double the record
1418 # if this admin is also owner, don't double the record
1419 if usr.user_id == owner_row[0].user_id:
1419 if usr.user_id == owner_row[0].user_id:
1420 owner_row[0].admin_row = True
1420 owner_row[0].admin_row = True
1421 else:
1421 else:
1422 usr = AttributeDict(usr.get_dict())
1422 usr = AttributeDict(usr.get_dict())
1423 usr.admin_row = True
1423 usr.admin_row = True
1424 usr.permission = _admin_perm
1424 usr.permission = _admin_perm
1425 super_admin_rows.append(usr)
1425 super_admin_rows.append(usr)
1426
1426
1427 return super_admin_rows + owner_row + perm_rows
1427 return super_admin_rows + owner_row + perm_rows
1428
1428
1429 def permission_user_groups(self):
1429 def permission_user_groups(self):
1430 q = UserGroupUserGroupToPerm.query().filter(UserGroupUserGroupToPerm.target_user_group == self)
1430 q = UserGroupUserGroupToPerm.query().filter(UserGroupUserGroupToPerm.target_user_group == self)
1431 q = q.options(joinedload(UserGroupUserGroupToPerm.user_group),
1431 q = q.options(joinedload(UserGroupUserGroupToPerm.user_group),
1432 joinedload(UserGroupUserGroupToPerm.target_user_group),
1432 joinedload(UserGroupUserGroupToPerm.target_user_group),
1433 joinedload(UserGroupUserGroupToPerm.permission),)
1433 joinedload(UserGroupUserGroupToPerm.permission),)
1434
1434
1435 perm_rows = []
1435 perm_rows = []
1436 for _user_group in q.all():
1436 for _user_group in q.all():
1437 usr = AttributeDict(_user_group.user_group.get_dict())
1437 usr = AttributeDict(_user_group.user_group.get_dict())
1438 usr.permission = _user_group.permission.permission_name
1438 usr.permission = _user_group.permission.permission_name
1439 perm_rows.append(usr)
1439 perm_rows.append(usr)
1440
1440
1441 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1441 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1442 return perm_rows
1442 return perm_rows
1443
1443
1444 def _get_default_perms(self, user_group, suffix=''):
1444 def _get_default_perms(self, user_group, suffix=''):
1445 from rhodecode.model.permission import PermissionModel
1445 from rhodecode.model.permission import PermissionModel
1446 return PermissionModel().get_default_perms(user_group.users_group_to_perm, suffix)
1446 return PermissionModel().get_default_perms(user_group.users_group_to_perm, suffix)
1447
1447
1448 def get_default_perms(self, suffix=''):
1448 def get_default_perms(self, suffix=''):
1449 return self._get_default_perms(self, suffix)
1449 return self._get_default_perms(self, suffix)
1450
1450
1451 def get_api_data(self, with_group_members=True, include_secrets=False):
1451 def get_api_data(self, with_group_members=True, include_secrets=False):
1452 """
1452 """
1453 :param include_secrets: See :meth:`User.get_api_data`, this parameter is
1453 :param include_secrets: See :meth:`User.get_api_data`, this parameter is
1454 basically forwarded.
1454 basically forwarded.
1455
1455
1456 """
1456 """
1457 user_group = self
1457 user_group = self
1458 data = {
1458 data = {
1459 'users_group_id': user_group.users_group_id,
1459 'users_group_id': user_group.users_group_id,
1460 'group_name': user_group.users_group_name,
1460 'group_name': user_group.users_group_name,
1461 'group_description': user_group.user_group_description,
1461 'group_description': user_group.user_group_description,
1462 'active': user_group.users_group_active,
1462 'active': user_group.users_group_active,
1463 'owner': user_group.user.username,
1463 'owner': user_group.user.username,
1464 'sync': user_group.sync,
1464 'sync': user_group.sync,
1465 'owner_email': user_group.user.email,
1465 'owner_email': user_group.user.email,
1466 }
1466 }
1467
1467
1468 if with_group_members:
1468 if with_group_members:
1469 users = []
1469 users = []
1470 for user in user_group.members:
1470 for user in user_group.members:
1471 user = user.user
1471 user = user.user
1472 users.append(user.get_api_data(include_secrets=include_secrets))
1472 users.append(user.get_api_data(include_secrets=include_secrets))
1473 data['users'] = users
1473 data['users'] = users
1474
1474
1475 return data
1475 return data
1476
1476
1477
1477
1478 class UserGroupMember(Base, BaseModel):
1478 class UserGroupMember(Base, BaseModel):
1479 __tablename__ = 'users_groups_members'
1479 __tablename__ = 'users_groups_members'
1480 __table_args__ = (
1480 __table_args__ = (
1481 base_table_args,
1481 base_table_args,
1482 )
1482 )
1483
1483
1484 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1484 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1485 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
1485 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
1486 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
1486 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
1487
1487
1488 user = relationship('User', lazy='joined')
1488 user = relationship('User', lazy='joined')
1489 users_group = relationship('UserGroup')
1489 users_group = relationship('UserGroup')
1490
1490
1491 def __init__(self, gr_id='', u_id=''):
1491 def __init__(self, gr_id='', u_id=''):
1492 self.users_group_id = gr_id
1492 self.users_group_id = gr_id
1493 self.user_id = u_id
1493 self.user_id = u_id
1494
1494
1495
1495
1496 class RepositoryField(Base, BaseModel):
1496 class RepositoryField(Base, BaseModel):
1497 __tablename__ = 'repositories_fields'
1497 __tablename__ = 'repositories_fields'
1498 __table_args__ = (
1498 __table_args__ = (
1499 UniqueConstraint('repository_id', 'field_key'), # no-multi field
1499 UniqueConstraint('repository_id', 'field_key'), # no-multi field
1500 base_table_args,
1500 base_table_args,
1501 )
1501 )
1502
1502
1503 PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields
1503 PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields
1504
1504
1505 repo_field_id = Column("repo_field_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1505 repo_field_id = Column("repo_field_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1506 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
1506 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
1507 field_key = Column("field_key", String(250))
1507 field_key = Column("field_key", String(250))
1508 field_label = Column("field_label", String(1024), nullable=False)
1508 field_label = Column("field_label", String(1024), nullable=False)
1509 field_value = Column("field_value", String(10000), nullable=False)
1509 field_value = Column("field_value", String(10000), nullable=False)
1510 field_desc = Column("field_desc", String(1024), nullable=False)
1510 field_desc = Column("field_desc", String(1024), nullable=False)
1511 field_type = Column("field_type", String(255), nullable=False, unique=None)
1511 field_type = Column("field_type", String(255), nullable=False, unique=None)
1512 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1512 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1513
1513
1514 repository = relationship('Repository')
1514 repository = relationship('Repository')
1515
1515
1516 @property
1516 @property
1517 def field_key_prefixed(self):
1517 def field_key_prefixed(self):
1518 return 'ex_%s' % self.field_key
1518 return 'ex_%s' % self.field_key
1519
1519
1520 @classmethod
1520 @classmethod
1521 def un_prefix_key(cls, key):
1521 def un_prefix_key(cls, key):
1522 if key.startswith(cls.PREFIX):
1522 if key.startswith(cls.PREFIX):
1523 return key[len(cls.PREFIX):]
1523 return key[len(cls.PREFIX):]
1524 return key
1524 return key
1525
1525
1526 @classmethod
1526 @classmethod
1527 def get_by_key_name(cls, key, repo):
1527 def get_by_key_name(cls, key, repo):
1528 row = cls.query()\
1528 row = cls.query()\
1529 .filter(cls.repository == repo)\
1529 .filter(cls.repository == repo)\
1530 .filter(cls.field_key == key).scalar()
1530 .filter(cls.field_key == key).scalar()
1531 return row
1531 return row
1532
1532
1533
1533
1534 class Repository(Base, BaseModel):
1534 class Repository(Base, BaseModel):
1535 __tablename__ = 'repositories'
1535 __tablename__ = 'repositories'
1536 __table_args__ = (
1536 __table_args__ = (
1537 Index('r_repo_name_idx', 'repo_name', mysql_length=255),
1537 Index('r_repo_name_idx', 'repo_name', mysql_length=255),
1538 base_table_args,
1538 base_table_args,
1539 )
1539 )
1540 DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}'
1540 DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}'
1541 DEFAULT_CLONE_URI_ID = '{scheme}://{user}@{netloc}/_{repoid}'
1541 DEFAULT_CLONE_URI_ID = '{scheme}://{user}@{netloc}/_{repoid}'
1542 DEFAULT_CLONE_URI_SSH = 'ssh://{sys_user}@{hostname}/{repo}'
1542 DEFAULT_CLONE_URI_SSH = 'ssh://{sys_user}@{hostname}/{repo}'
1543
1543
1544 STATE_CREATED = 'repo_state_created'
1544 STATE_CREATED = 'repo_state_created'
1545 STATE_PENDING = 'repo_state_pending'
1545 STATE_PENDING = 'repo_state_pending'
1546 STATE_ERROR = 'repo_state_error'
1546 STATE_ERROR = 'repo_state_error'
1547
1547
1548 LOCK_AUTOMATIC = 'lock_auto'
1548 LOCK_AUTOMATIC = 'lock_auto'
1549 LOCK_API = 'lock_api'
1549 LOCK_API = 'lock_api'
1550 LOCK_WEB = 'lock_web'
1550 LOCK_WEB = 'lock_web'
1551 LOCK_PULL = 'lock_pull'
1551 LOCK_PULL = 'lock_pull'
1552
1552
1553 NAME_SEP = URL_SEP
1553 NAME_SEP = URL_SEP
1554
1554
1555 repo_id = Column(
1555 repo_id = Column(
1556 "repo_id", Integer(), nullable=False, unique=True, default=None,
1556 "repo_id", Integer(), nullable=False, unique=True, default=None,
1557 primary_key=True)
1557 primary_key=True)
1558 _repo_name = Column(
1558 _repo_name = Column(
1559 "repo_name", Text(), nullable=False, default=None)
1559 "repo_name", Text(), nullable=False, default=None)
1560 _repo_name_hash = Column(
1560 _repo_name_hash = Column(
1561 "repo_name_hash", String(255), nullable=False, unique=True)
1561 "repo_name_hash", String(255), nullable=False, unique=True)
1562 repo_state = Column("repo_state", String(255), nullable=True)
1562 repo_state = Column("repo_state", String(255), nullable=True)
1563
1563
1564 clone_uri = Column(
1564 clone_uri = Column(
1565 "clone_uri", EncryptedTextValue(), nullable=True, unique=False,
1565 "clone_uri", EncryptedTextValue(), nullable=True, unique=False,
1566 default=None)
1566 default=None)
1567 push_uri = Column(
1567 push_uri = Column(
1568 "push_uri", EncryptedTextValue(), nullable=True, unique=False,
1568 "push_uri", EncryptedTextValue(), nullable=True, unique=False,
1569 default=None)
1569 default=None)
1570 repo_type = Column(
1570 repo_type = Column(
1571 "repo_type", String(255), nullable=False, unique=False, default=None)
1571 "repo_type", String(255), nullable=False, unique=False, default=None)
1572 user_id = Column(
1572 user_id = Column(
1573 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
1573 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
1574 unique=False, default=None)
1574 unique=False, default=None)
1575 private = Column(
1575 private = Column(
1576 "private", Boolean(), nullable=True, unique=None, default=None)
1576 "private", Boolean(), nullable=True, unique=None, default=None)
1577 enable_statistics = Column(
1577 enable_statistics = Column(
1578 "statistics", Boolean(), nullable=True, unique=None, default=True)
1578 "statistics", Boolean(), nullable=True, unique=None, default=True)
1579 enable_downloads = Column(
1579 enable_downloads = Column(
1580 "downloads", Boolean(), nullable=True, unique=None, default=True)
1580 "downloads", Boolean(), nullable=True, unique=None, default=True)
1581 description = Column(
1581 description = Column(
1582 "description", String(10000), nullable=True, unique=None, default=None)
1582 "description", String(10000), nullable=True, unique=None, default=None)
1583 created_on = Column(
1583 created_on = Column(
1584 'created_on', DateTime(timezone=False), nullable=True, unique=None,
1584 'created_on', DateTime(timezone=False), nullable=True, unique=None,
1585 default=datetime.datetime.now)
1585 default=datetime.datetime.now)
1586 updated_on = Column(
1586 updated_on = Column(
1587 'updated_on', DateTime(timezone=False), nullable=True, unique=None,
1587 'updated_on', DateTime(timezone=False), nullable=True, unique=None,
1588 default=datetime.datetime.now)
1588 default=datetime.datetime.now)
1589 _landing_revision = Column(
1589 _landing_revision = Column(
1590 "landing_revision", String(255), nullable=False, unique=False,
1590 "landing_revision", String(255), nullable=False, unique=False,
1591 default=None)
1591 default=None)
1592 enable_locking = Column(
1592 enable_locking = Column(
1593 "enable_locking", Boolean(), nullable=False, unique=None,
1593 "enable_locking", Boolean(), nullable=False, unique=None,
1594 default=False)
1594 default=False)
1595 _locked = Column(
1595 _locked = Column(
1596 "locked", String(255), nullable=True, unique=False, default=None)
1596 "locked", String(255), nullable=True, unique=False, default=None)
1597 _changeset_cache = Column(
1597 _changeset_cache = Column(
1598 "changeset_cache", LargeBinary(), nullable=True) # JSON data
1598 "changeset_cache", LargeBinary(), nullable=True) # JSON data
1599
1599
1600 fork_id = Column(
1600 fork_id = Column(
1601 "fork_id", Integer(), ForeignKey('repositories.repo_id'),
1601 "fork_id", Integer(), ForeignKey('repositories.repo_id'),
1602 nullable=True, unique=False, default=None)
1602 nullable=True, unique=False, default=None)
1603 group_id = Column(
1603 group_id = Column(
1604 "group_id", Integer(), ForeignKey('groups.group_id'), nullable=True,
1604 "group_id", Integer(), ForeignKey('groups.group_id'), nullable=True,
1605 unique=False, default=None)
1605 unique=False, default=None)
1606
1606
1607 user = relationship('User', lazy='joined')
1607 user = relationship('User', lazy='joined')
1608 fork = relationship('Repository', remote_side=repo_id, lazy='joined')
1608 fork = relationship('Repository', remote_side=repo_id, lazy='joined')
1609 group = relationship('RepoGroup', lazy='joined')
1609 group = relationship('RepoGroup', lazy='joined')
1610 repo_to_perm = relationship(
1610 repo_to_perm = relationship(
1611 'UserRepoToPerm', cascade='all',
1611 'UserRepoToPerm', cascade='all',
1612 order_by='UserRepoToPerm.repo_to_perm_id')
1612 order_by='UserRepoToPerm.repo_to_perm_id')
1613 users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1613 users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1614 stats = relationship('Statistics', cascade='all', uselist=False)
1614 stats = relationship('Statistics', cascade='all', uselist=False)
1615
1615
1616 followers = relationship(
1616 followers = relationship(
1617 'UserFollowing',
1617 'UserFollowing',
1618 primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id',
1618 primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id',
1619 cascade='all')
1619 cascade='all')
1620 extra_fields = relationship(
1620 extra_fields = relationship(
1621 'RepositoryField', cascade="all, delete, delete-orphan")
1621 'RepositoryField', cascade="all, delete, delete-orphan")
1622 logs = relationship('UserLog')
1622 logs = relationship('UserLog')
1623 comments = relationship(
1623 comments = relationship(
1624 'ChangesetComment', cascade="all, delete, delete-orphan")
1624 'ChangesetComment', cascade="all, delete, delete-orphan")
1625 pull_requests_source = relationship(
1625 pull_requests_source = relationship(
1626 'PullRequest',
1626 'PullRequest',
1627 primaryjoin='PullRequest.source_repo_id==Repository.repo_id',
1627 primaryjoin='PullRequest.source_repo_id==Repository.repo_id',
1628 cascade="all, delete, delete-orphan")
1628 cascade="all, delete, delete-orphan")
1629 pull_requests_target = relationship(
1629 pull_requests_target = relationship(
1630 'PullRequest',
1630 'PullRequest',
1631 primaryjoin='PullRequest.target_repo_id==Repository.repo_id',
1631 primaryjoin='PullRequest.target_repo_id==Repository.repo_id',
1632 cascade="all, delete, delete-orphan")
1632 cascade="all, delete, delete-orphan")
1633 ui = relationship('RepoRhodeCodeUi', cascade="all")
1633 ui = relationship('RepoRhodeCodeUi', cascade="all")
1634 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1634 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1635 integrations = relationship('Integration',
1635 integrations = relationship('Integration',
1636 cascade="all, delete, delete-orphan")
1636 cascade="all, delete, delete-orphan")
1637
1637
1638 scoped_tokens = relationship('UserApiKeys', cascade="all")
1638 scoped_tokens = relationship('UserApiKeys', cascade="all")
1639
1639
1640 def __unicode__(self):
1640 def __unicode__(self):
1641 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1641 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1642 safe_unicode(self.repo_name))
1642 safe_unicode(self.repo_name))
1643
1643
1644 @hybrid_property
1644 @hybrid_property
1645 def description_safe(self):
1645 def description_safe(self):
1646 from rhodecode.lib import helpers as h
1646 from rhodecode.lib import helpers as h
1647 return h.escape(self.description)
1647 return h.escape(self.description)
1648
1648
1649 @hybrid_property
1649 @hybrid_property
1650 def landing_rev(self):
1650 def landing_rev(self):
1651 # always should return [rev_type, rev]
1651 # always should return [rev_type, rev]
1652 if self._landing_revision:
1652 if self._landing_revision:
1653 _rev_info = self._landing_revision.split(':')
1653 _rev_info = self._landing_revision.split(':')
1654 if len(_rev_info) < 2:
1654 if len(_rev_info) < 2:
1655 _rev_info.insert(0, 'rev')
1655 _rev_info.insert(0, 'rev')
1656 return [_rev_info[0], _rev_info[1]]
1656 return [_rev_info[0], _rev_info[1]]
1657 return [None, None]
1657 return [None, None]
1658
1658
1659 @landing_rev.setter
1659 @landing_rev.setter
1660 def landing_rev(self, val):
1660 def landing_rev(self, val):
1661 if ':' not in val:
1661 if ':' not in val:
1662 raise ValueError('value must be delimited with `:` and consist '
1662 raise ValueError('value must be delimited with `:` and consist '
1663 'of <rev_type>:<rev>, got %s instead' % val)
1663 'of <rev_type>:<rev>, got %s instead' % val)
1664 self._landing_revision = val
1664 self._landing_revision = val
1665
1665
1666 @hybrid_property
1666 @hybrid_property
1667 def locked(self):
1667 def locked(self):
1668 if self._locked:
1668 if self._locked:
1669 user_id, timelocked, reason = self._locked.split(':')
1669 user_id, timelocked, reason = self._locked.split(':')
1670 lock_values = int(user_id), timelocked, reason
1670 lock_values = int(user_id), timelocked, reason
1671 else:
1671 else:
1672 lock_values = [None, None, None]
1672 lock_values = [None, None, None]
1673 return lock_values
1673 return lock_values
1674
1674
1675 @locked.setter
1675 @locked.setter
1676 def locked(self, val):
1676 def locked(self, val):
1677 if val and isinstance(val, (list, tuple)):
1677 if val and isinstance(val, (list, tuple)):
1678 self._locked = ':'.join(map(str, val))
1678 self._locked = ':'.join(map(str, val))
1679 else:
1679 else:
1680 self._locked = None
1680 self._locked = None
1681
1681
1682 @hybrid_property
1682 @hybrid_property
1683 def changeset_cache(self):
1683 def changeset_cache(self):
1684 from rhodecode.lib.vcs.backends.base import EmptyCommit
1684 from rhodecode.lib.vcs.backends.base import EmptyCommit
1685 dummy = EmptyCommit().__json__()
1685 dummy = EmptyCommit().__json__()
1686 if not self._changeset_cache:
1686 if not self._changeset_cache:
1687 return dummy
1687 return dummy
1688 try:
1688 try:
1689 return json.loads(self._changeset_cache)
1689 return json.loads(self._changeset_cache)
1690 except TypeError:
1690 except TypeError:
1691 return dummy
1691 return dummy
1692 except Exception:
1692 except Exception:
1693 log.error(traceback.format_exc())
1693 log.error(traceback.format_exc())
1694 return dummy
1694 return dummy
1695
1695
1696 @changeset_cache.setter
1696 @changeset_cache.setter
1697 def changeset_cache(self, val):
1697 def changeset_cache(self, val):
1698 try:
1698 try:
1699 self._changeset_cache = json.dumps(val)
1699 self._changeset_cache = json.dumps(val)
1700 except Exception:
1700 except Exception:
1701 log.error(traceback.format_exc())
1701 log.error(traceback.format_exc())
1702
1702
1703 @hybrid_property
1703 @hybrid_property
1704 def repo_name(self):
1704 def repo_name(self):
1705 return self._repo_name
1705 return self._repo_name
1706
1706
1707 @repo_name.setter
1707 @repo_name.setter
1708 def repo_name(self, value):
1708 def repo_name(self, value):
1709 self._repo_name = value
1709 self._repo_name = value
1710 self._repo_name_hash = hashlib.sha1(safe_str(value)).hexdigest()
1710 self._repo_name_hash = hashlib.sha1(safe_str(value)).hexdigest()
1711
1711
1712 @classmethod
1712 @classmethod
1713 def normalize_repo_name(cls, repo_name):
1713 def normalize_repo_name(cls, repo_name):
1714 """
1714 """
1715 Normalizes os specific repo_name to the format internally stored inside
1715 Normalizes os specific repo_name to the format internally stored inside
1716 database using URL_SEP
1716 database using URL_SEP
1717
1717
1718 :param cls:
1718 :param cls:
1719 :param repo_name:
1719 :param repo_name:
1720 """
1720 """
1721 return cls.NAME_SEP.join(repo_name.split(os.sep))
1721 return cls.NAME_SEP.join(repo_name.split(os.sep))
1722
1722
1723 @classmethod
1723 @classmethod
1724 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1724 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1725 session = Session()
1725 session = Session()
1726 q = session.query(cls).filter(cls.repo_name == repo_name)
1726 q = session.query(cls).filter(cls.repo_name == repo_name)
1727
1727
1728 if cache:
1728 if cache:
1729 if identity_cache:
1729 if identity_cache:
1730 val = cls.identity_cache(session, 'repo_name', repo_name)
1730 val = cls.identity_cache(session, 'repo_name', repo_name)
1731 if val:
1731 if val:
1732 return val
1732 return val
1733 else:
1733 else:
1734 cache_key = "get_repo_by_name_%s" % _hash_key(repo_name)
1734 cache_key = "get_repo_by_name_%s" % _hash_key(repo_name)
1735 q = q.options(
1735 q = q.options(
1736 FromCache("sql_cache_short", cache_key))
1736 FromCache("sql_cache_short", cache_key))
1737
1737
1738 return q.scalar()
1738 return q.scalar()
1739
1739
1740 @classmethod
1740 @classmethod
1741 def get_by_id_or_repo_name(cls, repoid):
1741 def get_by_id_or_repo_name(cls, repoid):
1742 if isinstance(repoid, (int, long)):
1742 if isinstance(repoid, (int, long)):
1743 try:
1743 try:
1744 repo = cls.get(repoid)
1744 repo = cls.get(repoid)
1745 except ValueError:
1745 except ValueError:
1746 repo = None
1746 repo = None
1747 else:
1747 else:
1748 repo = cls.get_by_repo_name(repoid)
1748 repo = cls.get_by_repo_name(repoid)
1749 return repo
1749 return repo
1750
1750
1751 @classmethod
1751 @classmethod
1752 def get_by_full_path(cls, repo_full_path):
1752 def get_by_full_path(cls, repo_full_path):
1753 repo_name = repo_full_path.split(cls.base_path(), 1)[-1]
1753 repo_name = repo_full_path.split(cls.base_path(), 1)[-1]
1754 repo_name = cls.normalize_repo_name(repo_name)
1754 repo_name = cls.normalize_repo_name(repo_name)
1755 return cls.get_by_repo_name(repo_name.strip(URL_SEP))
1755 return cls.get_by_repo_name(repo_name.strip(URL_SEP))
1756
1756
1757 @classmethod
1757 @classmethod
1758 def get_repo_forks(cls, repo_id):
1758 def get_repo_forks(cls, repo_id):
1759 return cls.query().filter(Repository.fork_id == repo_id)
1759 return cls.query().filter(Repository.fork_id == repo_id)
1760
1760
1761 @classmethod
1761 @classmethod
1762 def base_path(cls):
1762 def base_path(cls):
1763 """
1763 """
1764 Returns base path when all repos are stored
1764 Returns base path when all repos are stored
1765
1765
1766 :param cls:
1766 :param cls:
1767 """
1767 """
1768 q = Session().query(RhodeCodeUi)\
1768 q = Session().query(RhodeCodeUi)\
1769 .filter(RhodeCodeUi.ui_key == cls.NAME_SEP)
1769 .filter(RhodeCodeUi.ui_key == cls.NAME_SEP)
1770 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1770 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1771 return q.one().ui_value
1771 return q.one().ui_value
1772
1772
1773 @classmethod
1773 @classmethod
1774 def get_all_repos(cls, user_id=Optional(None), group_id=Optional(None),
1774 def get_all_repos(cls, user_id=Optional(None), group_id=Optional(None),
1775 case_insensitive=True):
1775 case_insensitive=True):
1776 q = Repository.query()
1776 q = Repository.query()
1777
1777
1778 if not isinstance(user_id, Optional):
1778 if not isinstance(user_id, Optional):
1779 q = q.filter(Repository.user_id == user_id)
1779 q = q.filter(Repository.user_id == user_id)
1780
1780
1781 if not isinstance(group_id, Optional):
1781 if not isinstance(group_id, Optional):
1782 q = q.filter(Repository.group_id == group_id)
1782 q = q.filter(Repository.group_id == group_id)
1783
1783
1784 if case_insensitive:
1784 if case_insensitive:
1785 q = q.order_by(func.lower(Repository.repo_name))
1785 q = q.order_by(func.lower(Repository.repo_name))
1786 else:
1786 else:
1787 q = q.order_by(Repository.repo_name)
1787 q = q.order_by(Repository.repo_name)
1788 return q.all()
1788 return q.all()
1789
1789
1790 @property
1790 @property
1791 def forks(self):
1791 def forks(self):
1792 """
1792 """
1793 Return forks of this repo
1793 Return forks of this repo
1794 """
1794 """
1795 return Repository.get_repo_forks(self.repo_id)
1795 return Repository.get_repo_forks(self.repo_id)
1796
1796
1797 @property
1797 @property
1798 def parent(self):
1798 def parent(self):
1799 """
1799 """
1800 Returns fork parent
1800 Returns fork parent
1801 """
1801 """
1802 return self.fork
1802 return self.fork
1803
1803
1804 @property
1804 @property
1805 def just_name(self):
1805 def just_name(self):
1806 return self.repo_name.split(self.NAME_SEP)[-1]
1806 return self.repo_name.split(self.NAME_SEP)[-1]
1807
1807
1808 @property
1808 @property
1809 def groups_with_parents(self):
1809 def groups_with_parents(self):
1810 groups = []
1810 groups = []
1811 if self.group is None:
1811 if self.group is None:
1812 return groups
1812 return groups
1813
1813
1814 cur_gr = self.group
1814 cur_gr = self.group
1815 groups.insert(0, cur_gr)
1815 groups.insert(0, cur_gr)
1816 while 1:
1816 while 1:
1817 gr = getattr(cur_gr, 'parent_group', None)
1817 gr = getattr(cur_gr, 'parent_group', None)
1818 cur_gr = cur_gr.parent_group
1818 cur_gr = cur_gr.parent_group
1819 if gr is None:
1819 if gr is None:
1820 break
1820 break
1821 groups.insert(0, gr)
1821 groups.insert(0, gr)
1822
1822
1823 return groups
1823 return groups
1824
1824
1825 @property
1825 @property
1826 def groups_and_repo(self):
1826 def groups_and_repo(self):
1827 return self.groups_with_parents, self
1827 return self.groups_with_parents, self
1828
1828
1829 @LazyProperty
1829 @LazyProperty
1830 def repo_path(self):
1830 def repo_path(self):
1831 """
1831 """
1832 Returns base full path for that repository means where it actually
1832 Returns base full path for that repository means where it actually
1833 exists on a filesystem
1833 exists on a filesystem
1834 """
1834 """
1835 q = Session().query(RhodeCodeUi).filter(
1835 q = Session().query(RhodeCodeUi).filter(
1836 RhodeCodeUi.ui_key == self.NAME_SEP)
1836 RhodeCodeUi.ui_key == self.NAME_SEP)
1837 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1837 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1838 return q.one().ui_value
1838 return q.one().ui_value
1839
1839
1840 @property
1840 @property
1841 def repo_full_path(self):
1841 def repo_full_path(self):
1842 p = [self.repo_path]
1842 p = [self.repo_path]
1843 # we need to split the name by / since this is how we store the
1843 # we need to split the name by / since this is how we store the
1844 # names in the database, but that eventually needs to be converted
1844 # names in the database, but that eventually needs to be converted
1845 # into a valid system path
1845 # into a valid system path
1846 p += self.repo_name.split(self.NAME_SEP)
1846 p += self.repo_name.split(self.NAME_SEP)
1847 return os.path.join(*map(safe_unicode, p))
1847 return os.path.join(*map(safe_unicode, p))
1848
1848
1849 @property
1849 @property
1850 def cache_keys(self):
1850 def cache_keys(self):
1851 """
1851 """
1852 Returns associated cache keys for that repo
1852 Returns associated cache keys for that repo
1853 """
1853 """
1854 return CacheKey.query()\
1854 return CacheKey.query()\
1855 .filter(CacheKey.cache_args == self.repo_name)\
1855 .filter(CacheKey.cache_args == self.repo_name)\
1856 .order_by(CacheKey.cache_key)\
1856 .order_by(CacheKey.cache_key)\
1857 .all()
1857 .all()
1858
1858
1859 @property
1859 @property
1860 def cached_diffs_relative_dir(self):
1860 def cached_diffs_relative_dir(self):
1861 """
1861 """
1862 Return a relative to the repository store path of cached diffs
1862 Return a relative to the repository store path of cached diffs
1863 used for safe display for users, who shouldn't know the absolute store
1863 used for safe display for users, who shouldn't know the absolute store
1864 path
1864 path
1865 """
1865 """
1866 return os.path.join(
1866 return os.path.join(
1867 os.path.dirname(self.repo_name),
1867 os.path.dirname(self.repo_name),
1868 self.cached_diffs_dir.split(os.path.sep)[-1])
1868 self.cached_diffs_dir.split(os.path.sep)[-1])
1869
1869
1870 @property
1870 @property
1871 def cached_diffs_dir(self):
1871 def cached_diffs_dir(self):
1872 path = self.repo_full_path
1872 path = self.repo_full_path
1873 return os.path.join(
1873 return os.path.join(
1874 os.path.dirname(path),
1874 os.path.dirname(path),
1875 '.__shadow_diff_cache_repo_{}'.format(self.repo_id))
1875 '.__shadow_diff_cache_repo_{}'.format(self.repo_id))
1876
1876
1877 def cached_diffs(self):
1877 def cached_diffs(self):
1878 diff_cache_dir = self.cached_diffs_dir
1878 diff_cache_dir = self.cached_diffs_dir
1879 if os.path.isdir(diff_cache_dir):
1879 if os.path.isdir(diff_cache_dir):
1880 return os.listdir(diff_cache_dir)
1880 return os.listdir(diff_cache_dir)
1881 return []
1881 return []
1882
1882
1883 def shadow_repos(self):
1883 def shadow_repos(self):
1884 shadow_repos_pattern = '.__shadow_repo_{}'.format(self.repo_id)
1884 shadow_repos_pattern = '.__shadow_repo_{}'.format(self.repo_id)
1885 return [
1885 return [
1886 x for x in os.listdir(os.path.dirname(self.repo_full_path))
1886 x for x in os.listdir(os.path.dirname(self.repo_full_path))
1887 if x.startswith(shadow_repos_pattern)]
1887 if x.startswith(shadow_repos_pattern)]
1888
1888
1889 def get_new_name(self, repo_name):
1889 def get_new_name(self, repo_name):
1890 """
1890 """
1891 returns new full repository name based on assigned group and new new
1891 returns new full repository name based on assigned group and new new
1892
1892
1893 :param group_name:
1893 :param group_name:
1894 """
1894 """
1895 path_prefix = self.group.full_path_splitted if self.group else []
1895 path_prefix = self.group.full_path_splitted if self.group else []
1896 return self.NAME_SEP.join(path_prefix + [repo_name])
1896 return self.NAME_SEP.join(path_prefix + [repo_name])
1897
1897
1898 @property
1898 @property
1899 def _config(self):
1899 def _config(self):
1900 """
1900 """
1901 Returns db based config object.
1901 Returns db based config object.
1902 """
1902 """
1903 from rhodecode.lib.utils import make_db_config
1903 from rhodecode.lib.utils import make_db_config
1904 return make_db_config(clear_session=False, repo=self)
1904 return make_db_config(clear_session=False, repo=self)
1905
1905
1906 def permissions(self, with_admins=True, with_owner=True):
1906 def permissions(self, with_admins=True, with_owner=True):
1907 q = UserRepoToPerm.query().filter(UserRepoToPerm.repository == self)
1907 q = UserRepoToPerm.query().filter(UserRepoToPerm.repository == self)
1908 q = q.options(joinedload(UserRepoToPerm.repository),
1908 q = q.options(joinedload(UserRepoToPerm.repository),
1909 joinedload(UserRepoToPerm.user),
1909 joinedload(UserRepoToPerm.user),
1910 joinedload(UserRepoToPerm.permission),)
1910 joinedload(UserRepoToPerm.permission),)
1911
1911
1912 # get owners and admins and permissions. We do a trick of re-writing
1912 # get owners and admins and permissions. We do a trick of re-writing
1913 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1913 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1914 # has a global reference and changing one object propagates to all
1914 # has a global reference and changing one object propagates to all
1915 # others. This means if admin is also an owner admin_row that change
1915 # others. This means if admin is also an owner admin_row that change
1916 # would propagate to both objects
1916 # would propagate to both objects
1917 perm_rows = []
1917 perm_rows = []
1918 for _usr in q.all():
1918 for _usr in q.all():
1919 usr = AttributeDict(_usr.user.get_dict())
1919 usr = AttributeDict(_usr.user.get_dict())
1920 usr.permission = _usr.permission.permission_name
1920 usr.permission = _usr.permission.permission_name
1921 perm_rows.append(usr)
1921 perm_rows.append(usr)
1922
1922
1923 # filter the perm rows by 'default' first and then sort them by
1923 # filter the perm rows by 'default' first and then sort them by
1924 # admin,write,read,none permissions sorted again alphabetically in
1924 # admin,write,read,none permissions sorted again alphabetically in
1925 # each group
1925 # each group
1926 perm_rows = sorted(perm_rows, key=display_user_sort)
1926 perm_rows = sorted(perm_rows, key=display_user_sort)
1927
1927
1928 _admin_perm = 'repository.admin'
1928 _admin_perm = 'repository.admin'
1929 owner_row = []
1929 owner_row = []
1930 if with_owner:
1930 if with_owner:
1931 usr = AttributeDict(self.user.get_dict())
1931 usr = AttributeDict(self.user.get_dict())
1932 usr.owner_row = True
1932 usr.owner_row = True
1933 usr.permission = _admin_perm
1933 usr.permission = _admin_perm
1934 owner_row.append(usr)
1934 owner_row.append(usr)
1935
1935
1936 super_admin_rows = []
1936 super_admin_rows = []
1937 if with_admins:
1937 if with_admins:
1938 for usr in User.get_all_super_admins():
1938 for usr in User.get_all_super_admins():
1939 # if this admin is also owner, don't double the record
1939 # if this admin is also owner, don't double the record
1940 if usr.user_id == owner_row[0].user_id:
1940 if usr.user_id == owner_row[0].user_id:
1941 owner_row[0].admin_row = True
1941 owner_row[0].admin_row = True
1942 else:
1942 else:
1943 usr = AttributeDict(usr.get_dict())
1943 usr = AttributeDict(usr.get_dict())
1944 usr.admin_row = True
1944 usr.admin_row = True
1945 usr.permission = _admin_perm
1945 usr.permission = _admin_perm
1946 super_admin_rows.append(usr)
1946 super_admin_rows.append(usr)
1947
1947
1948 return super_admin_rows + owner_row + perm_rows
1948 return super_admin_rows + owner_row + perm_rows
1949
1949
1950 def permission_user_groups(self):
1950 def permission_user_groups(self):
1951 q = UserGroupRepoToPerm.query().filter(
1951 q = UserGroupRepoToPerm.query().filter(
1952 UserGroupRepoToPerm.repository == self)
1952 UserGroupRepoToPerm.repository == self)
1953 q = q.options(joinedload(UserGroupRepoToPerm.repository),
1953 q = q.options(joinedload(UserGroupRepoToPerm.repository),
1954 joinedload(UserGroupRepoToPerm.users_group),
1954 joinedload(UserGroupRepoToPerm.users_group),
1955 joinedload(UserGroupRepoToPerm.permission),)
1955 joinedload(UserGroupRepoToPerm.permission),)
1956
1956
1957 perm_rows = []
1957 perm_rows = []
1958 for _user_group in q.all():
1958 for _user_group in q.all():
1959 usr = AttributeDict(_user_group.users_group.get_dict())
1959 usr = AttributeDict(_user_group.users_group.get_dict())
1960 usr.permission = _user_group.permission.permission_name
1960 usr.permission = _user_group.permission.permission_name
1961 perm_rows.append(usr)
1961 perm_rows.append(usr)
1962
1962
1963 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1963 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1964 return perm_rows
1964 return perm_rows
1965
1965
1966 def get_api_data(self, include_secrets=False):
1966 def get_api_data(self, include_secrets=False):
1967 """
1967 """
1968 Common function for generating repo api data
1968 Common function for generating repo api data
1969
1969
1970 :param include_secrets: See :meth:`User.get_api_data`.
1970 :param include_secrets: See :meth:`User.get_api_data`.
1971
1971
1972 """
1972 """
1973 # TODO: mikhail: Here there is an anti-pattern, we probably need to
1973 # TODO: mikhail: Here there is an anti-pattern, we probably need to
1974 # move this methods on models level.
1974 # move this methods on models level.
1975 from rhodecode.model.settings import SettingsModel
1975 from rhodecode.model.settings import SettingsModel
1976 from rhodecode.model.repo import RepoModel
1976 from rhodecode.model.repo import RepoModel
1977
1977
1978 repo = self
1978 repo = self
1979 _user_id, _time, _reason = self.locked
1979 _user_id, _time, _reason = self.locked
1980
1980
1981 data = {
1981 data = {
1982 'repo_id': repo.repo_id,
1982 'repo_id': repo.repo_id,
1983 'repo_name': repo.repo_name,
1983 'repo_name': repo.repo_name,
1984 'repo_type': repo.repo_type,
1984 'repo_type': repo.repo_type,
1985 'clone_uri': repo.clone_uri or '',
1985 'clone_uri': repo.clone_uri or '',
1986 'push_uri': repo.push_uri or '',
1986 'push_uri': repo.push_uri or '',
1987 'url': RepoModel().get_url(self),
1987 'url': RepoModel().get_url(self),
1988 'private': repo.private,
1988 'private': repo.private,
1989 'created_on': repo.created_on,
1989 'created_on': repo.created_on,
1990 'description': repo.description_safe,
1990 'description': repo.description_safe,
1991 'landing_rev': repo.landing_rev,
1991 'landing_rev': repo.landing_rev,
1992 'owner': repo.user.username,
1992 'owner': repo.user.username,
1993 'fork_of': repo.fork.repo_name if repo.fork else None,
1993 'fork_of': repo.fork.repo_name if repo.fork else None,
1994 'fork_of_id': repo.fork.repo_id if repo.fork else None,
1994 'fork_of_id': repo.fork.repo_id if repo.fork else None,
1995 'enable_statistics': repo.enable_statistics,
1995 'enable_statistics': repo.enable_statistics,
1996 'enable_locking': repo.enable_locking,
1996 'enable_locking': repo.enable_locking,
1997 'enable_downloads': repo.enable_downloads,
1997 'enable_downloads': repo.enable_downloads,
1998 'last_changeset': repo.changeset_cache,
1998 'last_changeset': repo.changeset_cache,
1999 'locked_by': User.get(_user_id).get_api_data(
1999 'locked_by': User.get(_user_id).get_api_data(
2000 include_secrets=include_secrets) if _user_id else None,
2000 include_secrets=include_secrets) if _user_id else None,
2001 'locked_date': time_to_datetime(_time) if _time else None,
2001 'locked_date': time_to_datetime(_time) if _time else None,
2002 'lock_reason': _reason if _reason else None,
2002 'lock_reason': _reason if _reason else None,
2003 }
2003 }
2004
2004
2005 # TODO: mikhail: should be per-repo settings here
2005 # TODO: mikhail: should be per-repo settings here
2006 rc_config = SettingsModel().get_all_settings()
2006 rc_config = SettingsModel().get_all_settings()
2007 repository_fields = str2bool(
2007 repository_fields = str2bool(
2008 rc_config.get('rhodecode_repository_fields'))
2008 rc_config.get('rhodecode_repository_fields'))
2009 if repository_fields:
2009 if repository_fields:
2010 for f in self.extra_fields:
2010 for f in self.extra_fields:
2011 data[f.field_key_prefixed] = f.field_value
2011 data[f.field_key_prefixed] = f.field_value
2012
2012
2013 return data
2013 return data
2014
2014
2015 @classmethod
2015 @classmethod
2016 def lock(cls, repo, user_id, lock_time=None, lock_reason=None):
2016 def lock(cls, repo, user_id, lock_time=None, lock_reason=None):
2017 if not lock_time:
2017 if not lock_time:
2018 lock_time = time.time()
2018 lock_time = time.time()
2019 if not lock_reason:
2019 if not lock_reason:
2020 lock_reason = cls.LOCK_AUTOMATIC
2020 lock_reason = cls.LOCK_AUTOMATIC
2021 repo.locked = [user_id, lock_time, lock_reason]
2021 repo.locked = [user_id, lock_time, lock_reason]
2022 Session().add(repo)
2022 Session().add(repo)
2023 Session().commit()
2023 Session().commit()
2024
2024
2025 @classmethod
2025 @classmethod
2026 def unlock(cls, repo):
2026 def unlock(cls, repo):
2027 repo.locked = None
2027 repo.locked = None
2028 Session().add(repo)
2028 Session().add(repo)
2029 Session().commit()
2029 Session().commit()
2030
2030
2031 @classmethod
2031 @classmethod
2032 def getlock(cls, repo):
2032 def getlock(cls, repo):
2033 return repo.locked
2033 return repo.locked
2034
2034
2035 def is_user_lock(self, user_id):
2035 def is_user_lock(self, user_id):
2036 if self.lock[0]:
2036 if self.lock[0]:
2037 lock_user_id = safe_int(self.lock[0])
2037 lock_user_id = safe_int(self.lock[0])
2038 user_id = safe_int(user_id)
2038 user_id = safe_int(user_id)
2039 # both are ints, and they are equal
2039 # both are ints, and they are equal
2040 return all([lock_user_id, user_id]) and lock_user_id == user_id
2040 return all([lock_user_id, user_id]) and lock_user_id == user_id
2041
2041
2042 return False
2042 return False
2043
2043
2044 def get_locking_state(self, action, user_id, only_when_enabled=True):
2044 def get_locking_state(self, action, user_id, only_when_enabled=True):
2045 """
2045 """
2046 Checks locking on this repository, if locking is enabled and lock is
2046 Checks locking on this repository, if locking is enabled and lock is
2047 present returns a tuple of make_lock, locked, locked_by.
2047 present returns a tuple of make_lock, locked, locked_by.
2048 make_lock can have 3 states None (do nothing) True, make lock
2048 make_lock can have 3 states None (do nothing) True, make lock
2049 False release lock, This value is later propagated to hooks, which
2049 False release lock, This value is later propagated to hooks, which
2050 do the locking. Think about this as signals passed to hooks what to do.
2050 do the locking. Think about this as signals passed to hooks what to do.
2051
2051
2052 """
2052 """
2053 # TODO: johbo: This is part of the business logic and should be moved
2053 # TODO: johbo: This is part of the business logic and should be moved
2054 # into the RepositoryModel.
2054 # into the RepositoryModel.
2055
2055
2056 if action not in ('push', 'pull'):
2056 if action not in ('push', 'pull'):
2057 raise ValueError("Invalid action value: %s" % repr(action))
2057 raise ValueError("Invalid action value: %s" % repr(action))
2058
2058
2059 # defines if locked error should be thrown to user
2059 # defines if locked error should be thrown to user
2060 currently_locked = False
2060 currently_locked = False
2061 # defines if new lock should be made, tri-state
2061 # defines if new lock should be made, tri-state
2062 make_lock = None
2062 make_lock = None
2063 repo = self
2063 repo = self
2064 user = User.get(user_id)
2064 user = User.get(user_id)
2065
2065
2066 lock_info = repo.locked
2066 lock_info = repo.locked
2067
2067
2068 if repo and (repo.enable_locking or not only_when_enabled):
2068 if repo and (repo.enable_locking or not only_when_enabled):
2069 if action == 'push':
2069 if action == 'push':
2070 # check if it's already locked !, if it is compare users
2070 # check if it's already locked !, if it is compare users
2071 locked_by_user_id = lock_info[0]
2071 locked_by_user_id = lock_info[0]
2072 if user.user_id == locked_by_user_id:
2072 if user.user_id == locked_by_user_id:
2073 log.debug(
2073 log.debug(
2074 'Got `push` action from user %s, now unlocking', user)
2074 'Got `push` action from user %s, now unlocking', user)
2075 # unlock if we have push from user who locked
2075 # unlock if we have push from user who locked
2076 make_lock = False
2076 make_lock = False
2077 else:
2077 else:
2078 # we're not the same user who locked, ban with
2078 # we're not the same user who locked, ban with
2079 # code defined in settings (default is 423 HTTP Locked) !
2079 # code defined in settings (default is 423 HTTP Locked) !
2080 log.debug('Repo %s is currently locked by %s', repo, user)
2080 log.debug('Repo %s is currently locked by %s', repo, user)
2081 currently_locked = True
2081 currently_locked = True
2082 elif action == 'pull':
2082 elif action == 'pull':
2083 # [0] user [1] date
2083 # [0] user [1] date
2084 if lock_info[0] and lock_info[1]:
2084 if lock_info[0] and lock_info[1]:
2085 log.debug('Repo %s is currently locked by %s', repo, user)
2085 log.debug('Repo %s is currently locked by %s', repo, user)
2086 currently_locked = True
2086 currently_locked = True
2087 else:
2087 else:
2088 log.debug('Setting lock on repo %s by %s', repo, user)
2088 log.debug('Setting lock on repo %s by %s', repo, user)
2089 make_lock = True
2089 make_lock = True
2090
2090
2091 else:
2091 else:
2092 log.debug('Repository %s do not have locking enabled', repo)
2092 log.debug('Repository %s do not have locking enabled', repo)
2093
2093
2094 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
2094 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
2095 make_lock, currently_locked, lock_info)
2095 make_lock, currently_locked, lock_info)
2096
2096
2097 from rhodecode.lib.auth import HasRepoPermissionAny
2097 from rhodecode.lib.auth import HasRepoPermissionAny
2098 perm_check = HasRepoPermissionAny('repository.write', 'repository.admin')
2098 perm_check = HasRepoPermissionAny('repository.write', 'repository.admin')
2099 if make_lock and not perm_check(repo_name=repo.repo_name, user=user):
2099 if make_lock and not perm_check(repo_name=repo.repo_name, user=user):
2100 # if we don't have at least write permission we cannot make a lock
2100 # if we don't have at least write permission we cannot make a lock
2101 log.debug('lock state reset back to FALSE due to lack '
2101 log.debug('lock state reset back to FALSE due to lack '
2102 'of at least read permission')
2102 'of at least read permission')
2103 make_lock = False
2103 make_lock = False
2104
2104
2105 return make_lock, currently_locked, lock_info
2105 return make_lock, currently_locked, lock_info
2106
2106
2107 @property
2107 @property
2108 def last_db_change(self):
2108 def last_db_change(self):
2109 return self.updated_on
2109 return self.updated_on
2110
2110
2111 @property
2111 @property
2112 def clone_uri_hidden(self):
2112 def clone_uri_hidden(self):
2113 clone_uri = self.clone_uri
2113 clone_uri = self.clone_uri
2114 if clone_uri:
2114 if clone_uri:
2115 import urlobject
2115 import urlobject
2116 url_obj = urlobject.URLObject(cleaned_uri(clone_uri))
2116 url_obj = urlobject.URLObject(cleaned_uri(clone_uri))
2117 if url_obj.password:
2117 if url_obj.password:
2118 clone_uri = url_obj.with_password('*****')
2118 clone_uri = url_obj.with_password('*****')
2119 return clone_uri
2119 return clone_uri
2120
2120
2121 @property
2121 @property
2122 def push_uri_hidden(self):
2122 def push_uri_hidden(self):
2123 push_uri = self.push_uri
2123 push_uri = self.push_uri
2124 if push_uri:
2124 if push_uri:
2125 import urlobject
2125 import urlobject
2126 url_obj = urlobject.URLObject(cleaned_uri(push_uri))
2126 url_obj = urlobject.URLObject(cleaned_uri(push_uri))
2127 if url_obj.password:
2127 if url_obj.password:
2128 push_uri = url_obj.with_password('*****')
2128 push_uri = url_obj.with_password('*****')
2129 return push_uri
2129 return push_uri
2130
2130
2131 def clone_url(self, **override):
2131 def clone_url(self, **override):
2132 from rhodecode.model.settings import SettingsModel
2132 from rhodecode.model.settings import SettingsModel
2133
2133
2134 uri_tmpl = None
2134 uri_tmpl = None
2135 if 'with_id' in override:
2135 if 'with_id' in override:
2136 uri_tmpl = self.DEFAULT_CLONE_URI_ID
2136 uri_tmpl = self.DEFAULT_CLONE_URI_ID
2137 del override['with_id']
2137 del override['with_id']
2138
2138
2139 if 'uri_tmpl' in override:
2139 if 'uri_tmpl' in override:
2140 uri_tmpl = override['uri_tmpl']
2140 uri_tmpl = override['uri_tmpl']
2141 del override['uri_tmpl']
2141 del override['uri_tmpl']
2142
2142
2143 ssh = False
2143 ssh = False
2144 if 'ssh' in override:
2144 if 'ssh' in override:
2145 ssh = True
2145 ssh = True
2146 del override['ssh']
2146 del override['ssh']
2147
2147
2148 # we didn't override our tmpl from **overrides
2148 # we didn't override our tmpl from **overrides
2149 if not uri_tmpl:
2149 if not uri_tmpl:
2150 rc_config = SettingsModel().get_all_settings(cache=True)
2150 rc_config = SettingsModel().get_all_settings(cache=True)
2151 if ssh:
2151 if ssh:
2152 uri_tmpl = rc_config.get(
2152 uri_tmpl = rc_config.get(
2153 'rhodecode_clone_uri_ssh_tmpl') or self.DEFAULT_CLONE_URI_SSH
2153 'rhodecode_clone_uri_ssh_tmpl') or self.DEFAULT_CLONE_URI_SSH
2154 else:
2154 else:
2155 uri_tmpl = rc_config.get(
2155 uri_tmpl = rc_config.get(
2156 'rhodecode_clone_uri_tmpl') or self.DEFAULT_CLONE_URI
2156 'rhodecode_clone_uri_tmpl') or self.DEFAULT_CLONE_URI
2157
2157
2158 request = get_current_request()
2158 request = get_current_request()
2159 return get_clone_url(request=request,
2159 return get_clone_url(request=request,
2160 uri_tmpl=uri_tmpl,
2160 uri_tmpl=uri_tmpl,
2161 repo_name=self.repo_name,
2161 repo_name=self.repo_name,
2162 repo_id=self.repo_id, **override)
2162 repo_id=self.repo_id, **override)
2163
2163
2164 def set_state(self, state):
2164 def set_state(self, state):
2165 self.repo_state = state
2165 self.repo_state = state
2166 Session().add(self)
2166 Session().add(self)
2167 #==========================================================================
2167 #==========================================================================
2168 # SCM PROPERTIES
2168 # SCM PROPERTIES
2169 #==========================================================================
2169 #==========================================================================
2170
2170
2171 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None):
2171 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None):
2172 return get_commit_safe(
2172 return get_commit_safe(
2173 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load)
2173 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load)
2174
2174
2175 def get_changeset(self, rev=None, pre_load=None):
2175 def get_changeset(self, rev=None, pre_load=None):
2176 warnings.warn("Use get_commit", DeprecationWarning)
2176 warnings.warn("Use get_commit", DeprecationWarning)
2177 commit_id = None
2177 commit_id = None
2178 commit_idx = None
2178 commit_idx = None
2179 if isinstance(rev, basestring):
2179 if isinstance(rev, basestring):
2180 commit_id = rev
2180 commit_id = rev
2181 else:
2181 else:
2182 commit_idx = rev
2182 commit_idx = rev
2183 return self.get_commit(commit_id=commit_id, commit_idx=commit_idx,
2183 return self.get_commit(commit_id=commit_id, commit_idx=commit_idx,
2184 pre_load=pre_load)
2184 pre_load=pre_load)
2185
2185
2186 def get_landing_commit(self):
2186 def get_landing_commit(self):
2187 """
2187 """
2188 Returns landing commit, or if that doesn't exist returns the tip
2188 Returns landing commit, or if that doesn't exist returns the tip
2189 """
2189 """
2190 _rev_type, _rev = self.landing_rev
2190 _rev_type, _rev = self.landing_rev
2191 commit = self.get_commit(_rev)
2191 commit = self.get_commit(_rev)
2192 if isinstance(commit, EmptyCommit):
2192 if isinstance(commit, EmptyCommit):
2193 return self.get_commit()
2193 return self.get_commit()
2194 return commit
2194 return commit
2195
2195
2196 def update_commit_cache(self, cs_cache=None, config=None):
2196 def update_commit_cache(self, cs_cache=None, config=None):
2197 """
2197 """
2198 Update cache of last changeset for repository, keys should be::
2198 Update cache of last changeset for repository, keys should be::
2199
2199
2200 short_id
2200 short_id
2201 raw_id
2201 raw_id
2202 revision
2202 revision
2203 parents
2203 parents
2204 message
2204 message
2205 date
2205 date
2206 author
2206 author
2207
2207
2208 :param cs_cache:
2208 :param cs_cache:
2209 """
2209 """
2210 from rhodecode.lib.vcs.backends.base import BaseChangeset
2210 from rhodecode.lib.vcs.backends.base import BaseChangeset
2211 if cs_cache is None:
2211 if cs_cache is None:
2212 # use no-cache version here
2212 # use no-cache version here
2213 scm_repo = self.scm_instance(cache=False, config=config)
2213 scm_repo = self.scm_instance(cache=False, config=config)
2214 if scm_repo:
2214 if scm_repo:
2215 cs_cache = scm_repo.get_commit(
2215 cs_cache = scm_repo.get_commit(
2216 pre_load=["author", "date", "message", "parents"])
2216 pre_load=["author", "date", "message", "parents"])
2217 else:
2217 else:
2218 cs_cache = EmptyCommit()
2218 cs_cache = EmptyCommit()
2219
2219
2220 if isinstance(cs_cache, BaseChangeset):
2220 if isinstance(cs_cache, BaseChangeset):
2221 cs_cache = cs_cache.__json__()
2221 cs_cache = cs_cache.__json__()
2222
2222
2223 def is_outdated(new_cs_cache):
2223 def is_outdated(new_cs_cache):
2224 if (new_cs_cache['raw_id'] != self.changeset_cache['raw_id'] or
2224 if (new_cs_cache['raw_id'] != self.changeset_cache['raw_id'] or
2225 new_cs_cache['revision'] != self.changeset_cache['revision']):
2225 new_cs_cache['revision'] != self.changeset_cache['revision']):
2226 return True
2226 return True
2227 return False
2227 return False
2228
2228
2229 # check if we have maybe already latest cached revision
2229 # check if we have maybe already latest cached revision
2230 if is_outdated(cs_cache) or not self.changeset_cache:
2230 if is_outdated(cs_cache) or not self.changeset_cache:
2231 _default = datetime.datetime.utcnow()
2231 _default = datetime.datetime.utcnow()
2232 last_change = cs_cache.get('date') or _default
2232 last_change = cs_cache.get('date') or _default
2233 if self.updated_on and self.updated_on > last_change:
2233 if self.updated_on and self.updated_on > last_change:
2234 # we check if last update is newer than the new value
2234 # we check if last update is newer than the new value
2235 # if yes, we use the current timestamp instead. Imagine you get
2235 # if yes, we use the current timestamp instead. Imagine you get
2236 # old commit pushed 1y ago, we'd set last update 1y to ago.
2236 # old commit pushed 1y ago, we'd set last update 1y to ago.
2237 last_change = _default
2237 last_change = _default
2238 log.debug('updated repo %s with new cs cache %s',
2238 log.debug('updated repo %s with new cs cache %s',
2239 self.repo_name, cs_cache)
2239 self.repo_name, cs_cache)
2240 self.updated_on = last_change
2240 self.updated_on = last_change
2241 self.changeset_cache = cs_cache
2241 self.changeset_cache = cs_cache
2242 Session().add(self)
2242 Session().add(self)
2243 Session().commit()
2243 Session().commit()
2244 else:
2244 else:
2245 log.debug('Skipping update_commit_cache for repo:`%s` '
2245 log.debug('Skipping update_commit_cache for repo:`%s` '
2246 'commit already with latest changes', self.repo_name)
2246 'commit already with latest changes', self.repo_name)
2247
2247
2248 @property
2248 @property
2249 def tip(self):
2249 def tip(self):
2250 return self.get_commit('tip')
2250 return self.get_commit('tip')
2251
2251
2252 @property
2252 @property
2253 def author(self):
2253 def author(self):
2254 return self.tip.author
2254 return self.tip.author
2255
2255
2256 @property
2256 @property
2257 def last_change(self):
2257 def last_change(self):
2258 return self.scm_instance().last_change
2258 return self.scm_instance().last_change
2259
2259
2260 def get_comments(self, revisions=None):
2260 def get_comments(self, revisions=None):
2261 """
2261 """
2262 Returns comments for this repository grouped by revisions
2262 Returns comments for this repository grouped by revisions
2263
2263
2264 :param revisions: filter query by revisions only
2264 :param revisions: filter query by revisions only
2265 """
2265 """
2266 cmts = ChangesetComment.query()\
2266 cmts = ChangesetComment.query()\
2267 .filter(ChangesetComment.repo == self)
2267 .filter(ChangesetComment.repo == self)
2268 if revisions:
2268 if revisions:
2269 cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
2269 cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
2270 grouped = collections.defaultdict(list)
2270 grouped = collections.defaultdict(list)
2271 for cmt in cmts.all():
2271 for cmt in cmts.all():
2272 grouped[cmt.revision].append(cmt)
2272 grouped[cmt.revision].append(cmt)
2273 return grouped
2273 return grouped
2274
2274
2275 def statuses(self, revisions=None):
2275 def statuses(self, revisions=None):
2276 """
2276 """
2277 Returns statuses for this repository
2277 Returns statuses for this repository
2278
2278
2279 :param revisions: list of revisions to get statuses for
2279 :param revisions: list of revisions to get statuses for
2280 """
2280 """
2281 statuses = ChangesetStatus.query()\
2281 statuses = ChangesetStatus.query()\
2282 .filter(ChangesetStatus.repo == self)\
2282 .filter(ChangesetStatus.repo == self)\
2283 .filter(ChangesetStatus.version == 0)
2283 .filter(ChangesetStatus.version == 0)
2284
2284
2285 if revisions:
2285 if revisions:
2286 # Try doing the filtering in chunks to avoid hitting limits
2286 # Try doing the filtering in chunks to avoid hitting limits
2287 size = 500
2287 size = 500
2288 status_results = []
2288 status_results = []
2289 for chunk in xrange(0, len(revisions), size):
2289 for chunk in xrange(0, len(revisions), size):
2290 status_results += statuses.filter(
2290 status_results += statuses.filter(
2291 ChangesetStatus.revision.in_(
2291 ChangesetStatus.revision.in_(
2292 revisions[chunk: chunk+size])
2292 revisions[chunk: chunk+size])
2293 ).all()
2293 ).all()
2294 else:
2294 else:
2295 status_results = statuses.all()
2295 status_results = statuses.all()
2296
2296
2297 grouped = {}
2297 grouped = {}
2298
2298
2299 # maybe we have open new pullrequest without a status?
2299 # maybe we have open new pullrequest without a status?
2300 stat = ChangesetStatus.STATUS_UNDER_REVIEW
2300 stat = ChangesetStatus.STATUS_UNDER_REVIEW
2301 status_lbl = ChangesetStatus.get_status_lbl(stat)
2301 status_lbl = ChangesetStatus.get_status_lbl(stat)
2302 for pr in PullRequest.query().filter(PullRequest.source_repo == self).all():
2302 for pr in PullRequest.query().filter(PullRequest.source_repo == self).all():
2303 for rev in pr.revisions:
2303 for rev in pr.revisions:
2304 pr_id = pr.pull_request_id
2304 pr_id = pr.pull_request_id
2305 pr_repo = pr.target_repo.repo_name
2305 pr_repo = pr.target_repo.repo_name
2306 grouped[rev] = [stat, status_lbl, pr_id, pr_repo]
2306 grouped[rev] = [stat, status_lbl, pr_id, pr_repo]
2307
2307
2308 for stat in status_results:
2308 for stat in status_results:
2309 pr_id = pr_repo = None
2309 pr_id = pr_repo = None
2310 if stat.pull_request:
2310 if stat.pull_request:
2311 pr_id = stat.pull_request.pull_request_id
2311 pr_id = stat.pull_request.pull_request_id
2312 pr_repo = stat.pull_request.target_repo.repo_name
2312 pr_repo = stat.pull_request.target_repo.repo_name
2313 grouped[stat.revision] = [str(stat.status), stat.status_lbl,
2313 grouped[stat.revision] = [str(stat.status), stat.status_lbl,
2314 pr_id, pr_repo]
2314 pr_id, pr_repo]
2315 return grouped
2315 return grouped
2316
2316
2317 # ==========================================================================
2317 # ==========================================================================
2318 # SCM CACHE INSTANCE
2318 # SCM CACHE INSTANCE
2319 # ==========================================================================
2319 # ==========================================================================
2320
2320
2321 def scm_instance(self, **kwargs):
2321 def scm_instance(self, **kwargs):
2322 import rhodecode
2322 import rhodecode
2323
2323
2324 # Passing a config will not hit the cache currently only used
2324 # Passing a config will not hit the cache currently only used
2325 # for repo2dbmapper
2325 # for repo2dbmapper
2326 config = kwargs.pop('config', None)
2326 config = kwargs.pop('config', None)
2327 cache = kwargs.pop('cache', None)
2327 cache = kwargs.pop('cache', None)
2328 full_cache = str2bool(rhodecode.CONFIG.get('vcs_full_cache'))
2328 full_cache = str2bool(rhodecode.CONFIG.get('vcs_full_cache'))
2329 # if cache is NOT defined use default global, else we have a full
2329 # if cache is NOT defined use default global, else we have a full
2330 # control over cache behaviour
2330 # control over cache behaviour
2331 if cache is None and full_cache and not config:
2331 if cache is None and full_cache and not config:
2332 return self._get_instance_cached()
2332 return self._get_instance_cached()
2333 return self._get_instance(cache=bool(cache), config=config)
2333 return self._get_instance(cache=bool(cache), config=config)
2334
2334
2335 def _get_instance_cached(self):
2335 def _get_instance_cached(self):
2336 @cache_region('long_term')
2336 @cache_region('long_term')
2337 def _get_repo(cache_key):
2337 def _get_repo(cache_key):
2338 return self._get_instance()
2338 return self._get_instance()
2339
2339
2340 invalidator_context = CacheKey.repo_context_cache(
2340 invalidator_context = CacheKey.repo_context_cache(
2341 _get_repo, self.repo_name, None, thread_scoped=True)
2341 _get_repo, self.repo_name, None, thread_scoped=True)
2342
2342
2343 with invalidator_context as context:
2343 with invalidator_context as context:
2344 context.invalidate()
2344 context.invalidate()
2345 repo = context.compute()
2345 repo = context.compute()
2346
2346
2347 return repo
2347 return repo
2348
2348
2349 def _get_instance(self, cache=True, config=None):
2349 def _get_instance(self, cache=True, config=None):
2350 config = config or self._config
2350 config = config or self._config
2351 custom_wire = {
2351 custom_wire = {
2352 'cache': cache # controls the vcs.remote cache
2352 'cache': cache # controls the vcs.remote cache
2353 }
2353 }
2354 repo = get_vcs_instance(
2354 repo = get_vcs_instance(
2355 repo_path=safe_str(self.repo_full_path),
2355 repo_path=safe_str(self.repo_full_path),
2356 config=config,
2356 config=config,
2357 with_wire=custom_wire,
2357 with_wire=custom_wire,
2358 create=False,
2358 create=False,
2359 _vcs_alias=self.repo_type)
2359 _vcs_alias=self.repo_type)
2360
2360
2361 return repo
2361 return repo
2362
2362
2363 def __json__(self):
2363 def __json__(self):
2364 return {'landing_rev': self.landing_rev}
2364 return {'landing_rev': self.landing_rev}
2365
2365
2366 def get_dict(self):
2366 def get_dict(self):
2367
2367
2368 # Since we transformed `repo_name` to a hybrid property, we need to
2368 # Since we transformed `repo_name` to a hybrid property, we need to
2369 # keep compatibility with the code which uses `repo_name` field.
2369 # keep compatibility with the code which uses `repo_name` field.
2370
2370
2371 result = super(Repository, self).get_dict()
2371 result = super(Repository, self).get_dict()
2372 result['repo_name'] = result.pop('_repo_name', None)
2372 result['repo_name'] = result.pop('_repo_name', None)
2373 return result
2373 return result
2374
2374
2375
2375
2376 class RepoGroup(Base, BaseModel):
2376 class RepoGroup(Base, BaseModel):
2377 __tablename__ = 'groups'
2377 __tablename__ = 'groups'
2378 __table_args__ = (
2378 __table_args__ = (
2379 UniqueConstraint('group_name', 'group_parent_id'),
2379 UniqueConstraint('group_name', 'group_parent_id'),
2380 CheckConstraint('group_id != group_parent_id'),
2380 CheckConstraint('group_id != group_parent_id'),
2381 base_table_args,
2381 base_table_args,
2382 )
2382 )
2383 __mapper_args__ = {'order_by': 'group_name'}
2383 __mapper_args__ = {'order_by': 'group_name'}
2384
2384
2385 CHOICES_SEPARATOR = '/' # used to generate select2 choices for nested groups
2385 CHOICES_SEPARATOR = '/' # used to generate select2 choices for nested groups
2386
2386
2387 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2387 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2388 group_name = Column("group_name", String(255), nullable=False, unique=True, default=None)
2388 group_name = Column("group_name", String(255), nullable=False, unique=True, default=None)
2389 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
2389 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
2390 group_description = Column("group_description", String(10000), nullable=True, unique=None, default=None)
2390 group_description = Column("group_description", String(10000), nullable=True, unique=None, default=None)
2391 enable_locking = Column("enable_locking", Boolean(), nullable=False, unique=None, default=False)
2391 enable_locking = Column("enable_locking", Boolean(), nullable=False, unique=None, default=False)
2392 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
2392 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
2393 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2393 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2394 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
2394 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
2395 personal = Column('personal', Boolean(), nullable=True, unique=None, default=None)
2395 personal = Column('personal', Boolean(), nullable=True, unique=None, default=None)
2396
2396
2397 repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
2397 repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
2398 users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
2398 users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
2399 parent_group = relationship('RepoGroup', remote_side=group_id)
2399 parent_group = relationship('RepoGroup', remote_side=group_id)
2400 user = relationship('User')
2400 user = relationship('User')
2401 integrations = relationship('Integration',
2401 integrations = relationship('Integration',
2402 cascade="all, delete, delete-orphan")
2402 cascade="all, delete, delete-orphan")
2403
2403
2404 def __init__(self, group_name='', parent_group=None):
2404 def __init__(self, group_name='', parent_group=None):
2405 self.group_name = group_name
2405 self.group_name = group_name
2406 self.parent_group = parent_group
2406 self.parent_group = parent_group
2407
2407
2408 def __unicode__(self):
2408 def __unicode__(self):
2409 return u"<%s('id:%s:%s')>" % (
2409 return u"<%s('id:%s:%s')>" % (
2410 self.__class__.__name__, self.group_id, self.group_name)
2410 self.__class__.__name__, self.group_id, self.group_name)
2411
2411
2412 @hybrid_property
2412 @hybrid_property
2413 def description_safe(self):
2413 def description_safe(self):
2414 from rhodecode.lib import helpers as h
2414 from rhodecode.lib import helpers as h
2415 return h.escape(self.group_description)
2415 return h.escape(self.group_description)
2416
2416
2417 @classmethod
2417 @classmethod
2418 def _generate_choice(cls, repo_group):
2418 def _generate_choice(cls, repo_group):
2419 from webhelpers.html import literal as _literal
2419 from webhelpers.html import literal as _literal
2420 _name = lambda k: _literal(cls.CHOICES_SEPARATOR.join(k))
2420 _name = lambda k: _literal(cls.CHOICES_SEPARATOR.join(k))
2421 return repo_group.group_id, _name(repo_group.full_path_splitted)
2421 return repo_group.group_id, _name(repo_group.full_path_splitted)
2422
2422
2423 @classmethod
2423 @classmethod
2424 def groups_choices(cls, groups=None, show_empty_group=True):
2424 def groups_choices(cls, groups=None, show_empty_group=True):
2425 if not groups:
2425 if not groups:
2426 groups = cls.query().all()
2426 groups = cls.query().all()
2427
2427
2428 repo_groups = []
2428 repo_groups = []
2429 if show_empty_group:
2429 if show_empty_group:
2430 repo_groups = [(-1, u'-- %s --' % _('No parent'))]
2430 repo_groups = [(-1, u'-- %s --' % _('No parent'))]
2431
2431
2432 repo_groups.extend([cls._generate_choice(x) for x in groups])
2432 repo_groups.extend([cls._generate_choice(x) for x in groups])
2433
2433
2434 repo_groups = sorted(
2434 repo_groups = sorted(
2435 repo_groups, key=lambda t: t[1].split(cls.CHOICES_SEPARATOR)[0])
2435 repo_groups, key=lambda t: t[1].split(cls.CHOICES_SEPARATOR)[0])
2436 return repo_groups
2436 return repo_groups
2437
2437
2438 @classmethod
2438 @classmethod
2439 def url_sep(cls):
2439 def url_sep(cls):
2440 return URL_SEP
2440 return URL_SEP
2441
2441
2442 @classmethod
2442 @classmethod
2443 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2443 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2444 if case_insensitive:
2444 if case_insensitive:
2445 gr = cls.query().filter(func.lower(cls.group_name)
2445 gr = cls.query().filter(func.lower(cls.group_name)
2446 == func.lower(group_name))
2446 == func.lower(group_name))
2447 else:
2447 else:
2448 gr = cls.query().filter(cls.group_name == group_name)
2448 gr = cls.query().filter(cls.group_name == group_name)
2449 if cache:
2449 if cache:
2450 name_key = _hash_key(group_name)
2450 name_key = _hash_key(group_name)
2451 gr = gr.options(
2451 gr = gr.options(
2452 FromCache("sql_cache_short", "get_group_%s" % name_key))
2452 FromCache("sql_cache_short", "get_group_%s" % name_key))
2453 return gr.scalar()
2453 return gr.scalar()
2454
2454
2455 @classmethod
2455 @classmethod
2456 def get_user_personal_repo_group(cls, user_id):
2456 def get_user_personal_repo_group(cls, user_id):
2457 user = User.get(user_id)
2457 user = User.get(user_id)
2458 if user.username == User.DEFAULT_USER:
2458 if user.username == User.DEFAULT_USER:
2459 return None
2459 return None
2460
2460
2461 return cls.query()\
2461 return cls.query()\
2462 .filter(cls.personal == true()) \
2462 .filter(cls.personal == true()) \
2463 .filter(cls.user == user).scalar()
2463 .filter(cls.user == user).scalar()
2464
2464
2465 @classmethod
2465 @classmethod
2466 def get_all_repo_groups(cls, user_id=Optional(None), group_id=Optional(None),
2466 def get_all_repo_groups(cls, user_id=Optional(None), group_id=Optional(None),
2467 case_insensitive=True):
2467 case_insensitive=True):
2468 q = RepoGroup.query()
2468 q = RepoGroup.query()
2469
2469
2470 if not isinstance(user_id, Optional):
2470 if not isinstance(user_id, Optional):
2471 q = q.filter(RepoGroup.user_id == user_id)
2471 q = q.filter(RepoGroup.user_id == user_id)
2472
2472
2473 if not isinstance(group_id, Optional):
2473 if not isinstance(group_id, Optional):
2474 q = q.filter(RepoGroup.group_parent_id == group_id)
2474 q = q.filter(RepoGroup.group_parent_id == group_id)
2475
2475
2476 if case_insensitive:
2476 if case_insensitive:
2477 q = q.order_by(func.lower(RepoGroup.group_name))
2477 q = q.order_by(func.lower(RepoGroup.group_name))
2478 else:
2478 else:
2479 q = q.order_by(RepoGroup.group_name)
2479 q = q.order_by(RepoGroup.group_name)
2480 return q.all()
2480 return q.all()
2481
2481
2482 @property
2482 @property
2483 def parents(self):
2483 def parents(self):
2484 parents_recursion_limit = 10
2484 parents_recursion_limit = 10
2485 groups = []
2485 groups = []
2486 if self.parent_group is None:
2486 if self.parent_group is None:
2487 return groups
2487 return groups
2488 cur_gr = self.parent_group
2488 cur_gr = self.parent_group
2489 groups.insert(0, cur_gr)
2489 groups.insert(0, cur_gr)
2490 cnt = 0
2490 cnt = 0
2491 while 1:
2491 while 1:
2492 cnt += 1
2492 cnt += 1
2493 gr = getattr(cur_gr, 'parent_group', None)
2493 gr = getattr(cur_gr, 'parent_group', None)
2494 cur_gr = cur_gr.parent_group
2494 cur_gr = cur_gr.parent_group
2495 if gr is None:
2495 if gr is None:
2496 break
2496 break
2497 if cnt == parents_recursion_limit:
2497 if cnt == parents_recursion_limit:
2498 # this will prevent accidental infinit loops
2498 # this will prevent accidental infinit loops
2499 log.error(('more than %s parents found for group %s, stopping '
2499 log.error(('more than %s parents found for group %s, stopping '
2500 'recursive parent fetching' % (parents_recursion_limit, self)))
2500 'recursive parent fetching' % (parents_recursion_limit, self)))
2501 break
2501 break
2502
2502
2503 groups.insert(0, gr)
2503 groups.insert(0, gr)
2504 return groups
2504 return groups
2505
2505
2506 @property
2506 @property
2507 def last_db_change(self):
2507 def last_db_change(self):
2508 return self.updated_on
2508 return self.updated_on
2509
2509
2510 @property
2510 @property
2511 def children(self):
2511 def children(self):
2512 return RepoGroup.query().filter(RepoGroup.parent_group == self)
2512 return RepoGroup.query().filter(RepoGroup.parent_group == self)
2513
2513
2514 @property
2514 @property
2515 def name(self):
2515 def name(self):
2516 return self.group_name.split(RepoGroup.url_sep())[-1]
2516 return self.group_name.split(RepoGroup.url_sep())[-1]
2517
2517
2518 @property
2518 @property
2519 def full_path(self):
2519 def full_path(self):
2520 return self.group_name
2520 return self.group_name
2521
2521
2522 @property
2522 @property
2523 def full_path_splitted(self):
2523 def full_path_splitted(self):
2524 return self.group_name.split(RepoGroup.url_sep())
2524 return self.group_name.split(RepoGroup.url_sep())
2525
2525
2526 @property
2526 @property
2527 def repositories(self):
2527 def repositories(self):
2528 return Repository.query()\
2528 return Repository.query()\
2529 .filter(Repository.group == self)\
2529 .filter(Repository.group == self)\
2530 .order_by(Repository.repo_name)
2530 .order_by(Repository.repo_name)
2531
2531
2532 @property
2532 @property
2533 def repositories_recursive_count(self):
2533 def repositories_recursive_count(self):
2534 cnt = self.repositories.count()
2534 cnt = self.repositories.count()
2535
2535
2536 def children_count(group):
2536 def children_count(group):
2537 cnt = 0
2537 cnt = 0
2538 for child in group.children:
2538 for child in group.children:
2539 cnt += child.repositories.count()
2539 cnt += child.repositories.count()
2540 cnt += children_count(child)
2540 cnt += children_count(child)
2541 return cnt
2541 return cnt
2542
2542
2543 return cnt + children_count(self)
2543 return cnt + children_count(self)
2544
2544
2545 def _recursive_objects(self, include_repos=True):
2545 def _recursive_objects(self, include_repos=True):
2546 all_ = []
2546 all_ = []
2547
2547
2548 def _get_members(root_gr):
2548 def _get_members(root_gr):
2549 if include_repos:
2549 if include_repos:
2550 for r in root_gr.repositories:
2550 for r in root_gr.repositories:
2551 all_.append(r)
2551 all_.append(r)
2552 childs = root_gr.children.all()
2552 childs = root_gr.children.all()
2553 if childs:
2553 if childs:
2554 for gr in childs:
2554 for gr in childs:
2555 all_.append(gr)
2555 all_.append(gr)
2556 _get_members(gr)
2556 _get_members(gr)
2557
2557
2558 _get_members(self)
2558 _get_members(self)
2559 return [self] + all_
2559 return [self] + all_
2560
2560
2561 def recursive_groups_and_repos(self):
2561 def recursive_groups_and_repos(self):
2562 """
2562 """
2563 Recursive return all groups, with repositories in those groups
2563 Recursive return all groups, with repositories in those groups
2564 """
2564 """
2565 return self._recursive_objects()
2565 return self._recursive_objects()
2566
2566
2567 def recursive_groups(self):
2567 def recursive_groups(self):
2568 """
2568 """
2569 Returns all children groups for this group including children of children
2569 Returns all children groups for this group including children of children
2570 """
2570 """
2571 return self._recursive_objects(include_repos=False)
2571 return self._recursive_objects(include_repos=False)
2572
2572
2573 def get_new_name(self, group_name):
2573 def get_new_name(self, group_name):
2574 """
2574 """
2575 returns new full group name based on parent and new name
2575 returns new full group name based on parent and new name
2576
2576
2577 :param group_name:
2577 :param group_name:
2578 """
2578 """
2579 path_prefix = (self.parent_group.full_path_splitted if
2579 path_prefix = (self.parent_group.full_path_splitted if
2580 self.parent_group else [])
2580 self.parent_group else [])
2581 return RepoGroup.url_sep().join(path_prefix + [group_name])
2581 return RepoGroup.url_sep().join(path_prefix + [group_name])
2582
2582
2583 def permissions(self, with_admins=True, with_owner=True):
2583 def permissions(self, with_admins=True, with_owner=True):
2584 q = UserRepoGroupToPerm.query().filter(UserRepoGroupToPerm.group == self)
2584 q = UserRepoGroupToPerm.query().filter(UserRepoGroupToPerm.group == self)
2585 q = q.options(joinedload(UserRepoGroupToPerm.group),
2585 q = q.options(joinedload(UserRepoGroupToPerm.group),
2586 joinedload(UserRepoGroupToPerm.user),
2586 joinedload(UserRepoGroupToPerm.user),
2587 joinedload(UserRepoGroupToPerm.permission),)
2587 joinedload(UserRepoGroupToPerm.permission),)
2588
2588
2589 # get owners and admins and permissions. We do a trick of re-writing
2589 # get owners and admins and permissions. We do a trick of re-writing
2590 # objects from sqlalchemy to named-tuples due to sqlalchemy session
2590 # objects from sqlalchemy to named-tuples due to sqlalchemy session
2591 # has a global reference and changing one object propagates to all
2591 # has a global reference and changing one object propagates to all
2592 # others. This means if admin is also an owner admin_row that change
2592 # others. This means if admin is also an owner admin_row that change
2593 # would propagate to both objects
2593 # would propagate to both objects
2594 perm_rows = []
2594 perm_rows = []
2595 for _usr in q.all():
2595 for _usr in q.all():
2596 usr = AttributeDict(_usr.user.get_dict())
2596 usr = AttributeDict(_usr.user.get_dict())
2597 usr.permission = _usr.permission.permission_name
2597 usr.permission = _usr.permission.permission_name
2598 perm_rows.append(usr)
2598 perm_rows.append(usr)
2599
2599
2600 # filter the perm rows by 'default' first and then sort them by
2600 # filter the perm rows by 'default' first and then sort them by
2601 # admin,write,read,none permissions sorted again alphabetically in
2601 # admin,write,read,none permissions sorted again alphabetically in
2602 # each group
2602 # each group
2603 perm_rows = sorted(perm_rows, key=display_user_sort)
2603 perm_rows = sorted(perm_rows, key=display_user_sort)
2604
2604
2605 _admin_perm = 'group.admin'
2605 _admin_perm = 'group.admin'
2606 owner_row = []
2606 owner_row = []
2607 if with_owner:
2607 if with_owner:
2608 usr = AttributeDict(self.user.get_dict())
2608 usr = AttributeDict(self.user.get_dict())
2609 usr.owner_row = True
2609 usr.owner_row = True
2610 usr.permission = _admin_perm
2610 usr.permission = _admin_perm
2611 owner_row.append(usr)
2611 owner_row.append(usr)
2612
2612
2613 super_admin_rows = []
2613 super_admin_rows = []
2614 if with_admins:
2614 if with_admins:
2615 for usr in User.get_all_super_admins():
2615 for usr in User.get_all_super_admins():
2616 # if this admin is also owner, don't double the record
2616 # if this admin is also owner, don't double the record
2617 if usr.user_id == owner_row[0].user_id:
2617 if usr.user_id == owner_row[0].user_id:
2618 owner_row[0].admin_row = True
2618 owner_row[0].admin_row = True
2619 else:
2619 else:
2620 usr = AttributeDict(usr.get_dict())
2620 usr = AttributeDict(usr.get_dict())
2621 usr.admin_row = True
2621 usr.admin_row = True
2622 usr.permission = _admin_perm
2622 usr.permission = _admin_perm
2623 super_admin_rows.append(usr)
2623 super_admin_rows.append(usr)
2624
2624
2625 return super_admin_rows + owner_row + perm_rows
2625 return super_admin_rows + owner_row + perm_rows
2626
2626
2627 def permission_user_groups(self):
2627 def permission_user_groups(self):
2628 q = UserGroupRepoGroupToPerm.query().filter(UserGroupRepoGroupToPerm.group == self)
2628 q = UserGroupRepoGroupToPerm.query().filter(UserGroupRepoGroupToPerm.group == self)
2629 q = q.options(joinedload(UserGroupRepoGroupToPerm.group),
2629 q = q.options(joinedload(UserGroupRepoGroupToPerm.group),
2630 joinedload(UserGroupRepoGroupToPerm.users_group),
2630 joinedload(UserGroupRepoGroupToPerm.users_group),
2631 joinedload(UserGroupRepoGroupToPerm.permission),)
2631 joinedload(UserGroupRepoGroupToPerm.permission),)
2632
2632
2633 perm_rows = []
2633 perm_rows = []
2634 for _user_group in q.all():
2634 for _user_group in q.all():
2635 usr = AttributeDict(_user_group.users_group.get_dict())
2635 usr = AttributeDict(_user_group.users_group.get_dict())
2636 usr.permission = _user_group.permission.permission_name
2636 usr.permission = _user_group.permission.permission_name
2637 perm_rows.append(usr)
2637 perm_rows.append(usr)
2638
2638
2639 perm_rows = sorted(perm_rows, key=display_user_group_sort)
2639 perm_rows = sorted(perm_rows, key=display_user_group_sort)
2640 return perm_rows
2640 return perm_rows
2641
2641
2642 def get_api_data(self):
2642 def get_api_data(self):
2643 """
2643 """
2644 Common function for generating api data
2644 Common function for generating api data
2645
2645
2646 """
2646 """
2647 group = self
2647 group = self
2648 data = {
2648 data = {
2649 'group_id': group.group_id,
2649 'group_id': group.group_id,
2650 'group_name': group.group_name,
2650 'group_name': group.group_name,
2651 'group_description': group.description_safe,
2651 'group_description': group.description_safe,
2652 'parent_group': group.parent_group.group_name if group.parent_group else None,
2652 'parent_group': group.parent_group.group_name if group.parent_group else None,
2653 'repositories': [x.repo_name for x in group.repositories],
2653 'repositories': [x.repo_name for x in group.repositories],
2654 'owner': group.user.username,
2654 'owner': group.user.username,
2655 }
2655 }
2656 return data
2656 return data
2657
2657
2658
2658
2659 class Permission(Base, BaseModel):
2659 class Permission(Base, BaseModel):
2660 __tablename__ = 'permissions'
2660 __tablename__ = 'permissions'
2661 __table_args__ = (
2661 __table_args__ = (
2662 Index('p_perm_name_idx', 'permission_name'),
2662 Index('p_perm_name_idx', 'permission_name'),
2663 base_table_args,
2663 base_table_args,
2664 )
2664 )
2665
2665
2666 PERMS = [
2666 PERMS = [
2667 ('hg.admin', _('RhodeCode Super Administrator')),
2667 ('hg.admin', _('RhodeCode Super Administrator')),
2668
2668
2669 ('repository.none', _('Repository no access')),
2669 ('repository.none', _('Repository no access')),
2670 ('repository.read', _('Repository read access')),
2670 ('repository.read', _('Repository read access')),
2671 ('repository.write', _('Repository write access')),
2671 ('repository.write', _('Repository write access')),
2672 ('repository.admin', _('Repository admin access')),
2672 ('repository.admin', _('Repository admin access')),
2673
2673
2674 ('group.none', _('Repository group no access')),
2674 ('group.none', _('Repository group no access')),
2675 ('group.read', _('Repository group read access')),
2675 ('group.read', _('Repository group read access')),
2676 ('group.write', _('Repository group write access')),
2676 ('group.write', _('Repository group write access')),
2677 ('group.admin', _('Repository group admin access')),
2677 ('group.admin', _('Repository group admin access')),
2678
2678
2679 ('usergroup.none', _('User group no access')),
2679 ('usergroup.none', _('User group no access')),
2680 ('usergroup.read', _('User group read access')),
2680 ('usergroup.read', _('User group read access')),
2681 ('usergroup.write', _('User group write access')),
2681 ('usergroup.write', _('User group write access')),
2682 ('usergroup.admin', _('User group admin access')),
2682 ('usergroup.admin', _('User group admin access')),
2683
2683
2684 ('hg.repogroup.create.false', _('Repository Group creation disabled')),
2684 ('hg.repogroup.create.false', _('Repository Group creation disabled')),
2685 ('hg.repogroup.create.true', _('Repository Group creation enabled')),
2685 ('hg.repogroup.create.true', _('Repository Group creation enabled')),
2686
2686
2687 ('hg.usergroup.create.false', _('User Group creation disabled')),
2687 ('hg.usergroup.create.false', _('User Group creation disabled')),
2688 ('hg.usergroup.create.true', _('User Group creation enabled')),
2688 ('hg.usergroup.create.true', _('User Group creation enabled')),
2689
2689
2690 ('hg.create.none', _('Repository creation disabled')),
2690 ('hg.create.none', _('Repository creation disabled')),
2691 ('hg.create.repository', _('Repository creation enabled')),
2691 ('hg.create.repository', _('Repository creation enabled')),
2692 ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')),
2692 ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')),
2693 ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')),
2693 ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')),
2694
2694
2695 ('hg.fork.none', _('Repository forking disabled')),
2695 ('hg.fork.none', _('Repository forking disabled')),
2696 ('hg.fork.repository', _('Repository forking enabled')),
2696 ('hg.fork.repository', _('Repository forking enabled')),
2697
2697
2698 ('hg.register.none', _('Registration disabled')),
2698 ('hg.register.none', _('Registration disabled')),
2699 ('hg.register.manual_activate', _('User Registration with manual account activation')),
2699 ('hg.register.manual_activate', _('User Registration with manual account activation')),
2700 ('hg.register.auto_activate', _('User Registration with automatic account activation')),
2700 ('hg.register.auto_activate', _('User Registration with automatic account activation')),
2701
2701
2702 ('hg.password_reset.enabled', _('Password reset enabled')),
2702 ('hg.password_reset.enabled', _('Password reset enabled')),
2703 ('hg.password_reset.hidden', _('Password reset hidden')),
2703 ('hg.password_reset.hidden', _('Password reset hidden')),
2704 ('hg.password_reset.disabled', _('Password reset disabled')),
2704 ('hg.password_reset.disabled', _('Password reset disabled')),
2705
2705
2706 ('hg.extern_activate.manual', _('Manual activation of external account')),
2706 ('hg.extern_activate.manual', _('Manual activation of external account')),
2707 ('hg.extern_activate.auto', _('Automatic activation of external account')),
2707 ('hg.extern_activate.auto', _('Automatic activation of external account')),
2708
2708
2709 ('hg.inherit_default_perms.false', _('Inherit object permissions from default user disabled')),
2709 ('hg.inherit_default_perms.false', _('Inherit object permissions from default user disabled')),
2710 ('hg.inherit_default_perms.true', _('Inherit object permissions from default user enabled')),
2710 ('hg.inherit_default_perms.true', _('Inherit object permissions from default user enabled')),
2711 ]
2711 ]
2712
2712
2713 # definition of system default permissions for DEFAULT user
2713 # definition of system default permissions for DEFAULT user
2714 DEFAULT_USER_PERMISSIONS = [
2714 DEFAULT_USER_PERMISSIONS = [
2715 'repository.read',
2715 'repository.read',
2716 'group.read',
2716 'group.read',
2717 'usergroup.read',
2717 'usergroup.read',
2718 'hg.create.repository',
2718 'hg.create.repository',
2719 'hg.repogroup.create.false',
2719 'hg.repogroup.create.false',
2720 'hg.usergroup.create.false',
2720 'hg.usergroup.create.false',
2721 'hg.create.write_on_repogroup.true',
2721 'hg.create.write_on_repogroup.true',
2722 'hg.fork.repository',
2722 'hg.fork.repository',
2723 'hg.register.manual_activate',
2723 'hg.register.manual_activate',
2724 'hg.password_reset.enabled',
2724 'hg.password_reset.enabled',
2725 'hg.extern_activate.auto',
2725 'hg.extern_activate.auto',
2726 'hg.inherit_default_perms.true',
2726 'hg.inherit_default_perms.true',
2727 ]
2727 ]
2728
2728
2729 # defines which permissions are more important higher the more important
2729 # defines which permissions are more important higher the more important
2730 # Weight defines which permissions are more important.
2730 # Weight defines which permissions are more important.
2731 # The higher number the more important.
2731 # The higher number the more important.
2732 PERM_WEIGHTS = {
2732 PERM_WEIGHTS = {
2733 'repository.none': 0,
2733 'repository.none': 0,
2734 'repository.read': 1,
2734 'repository.read': 1,
2735 'repository.write': 3,
2735 'repository.write': 3,
2736 'repository.admin': 4,
2736 'repository.admin': 4,
2737
2737
2738 'group.none': 0,
2738 'group.none': 0,
2739 'group.read': 1,
2739 'group.read': 1,
2740 'group.write': 3,
2740 'group.write': 3,
2741 'group.admin': 4,
2741 'group.admin': 4,
2742
2742
2743 'usergroup.none': 0,
2743 'usergroup.none': 0,
2744 'usergroup.read': 1,
2744 'usergroup.read': 1,
2745 'usergroup.write': 3,
2745 'usergroup.write': 3,
2746 'usergroup.admin': 4,
2746 'usergroup.admin': 4,
2747
2747
2748 'hg.repogroup.create.false': 0,
2748 'hg.repogroup.create.false': 0,
2749 'hg.repogroup.create.true': 1,
2749 'hg.repogroup.create.true': 1,
2750
2750
2751 'hg.usergroup.create.false': 0,
2751 'hg.usergroup.create.false': 0,
2752 'hg.usergroup.create.true': 1,
2752 'hg.usergroup.create.true': 1,
2753
2753
2754 'hg.fork.none': 0,
2754 'hg.fork.none': 0,
2755 'hg.fork.repository': 1,
2755 'hg.fork.repository': 1,
2756 'hg.create.none': 0,
2756 'hg.create.none': 0,
2757 'hg.create.repository': 1
2757 'hg.create.repository': 1
2758 }
2758 }
2759
2759
2760 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2760 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2761 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
2761 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
2762 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
2762 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
2763
2763
2764 def __unicode__(self):
2764 def __unicode__(self):
2765 return u"<%s('%s:%s')>" % (
2765 return u"<%s('%s:%s')>" % (
2766 self.__class__.__name__, self.permission_id, self.permission_name
2766 self.__class__.__name__, self.permission_id, self.permission_name
2767 )
2767 )
2768
2768
2769 @classmethod
2769 @classmethod
2770 def get_by_key(cls, key):
2770 def get_by_key(cls, key):
2771 return cls.query().filter(cls.permission_name == key).scalar()
2771 return cls.query().filter(cls.permission_name == key).scalar()
2772
2772
2773 @classmethod
2773 @classmethod
2774 def get_default_repo_perms(cls, user_id, repo_id=None):
2774 def get_default_repo_perms(cls, user_id, repo_id=None):
2775 q = Session().query(UserRepoToPerm, Repository, Permission)\
2775 q = Session().query(UserRepoToPerm, Repository, Permission)\
2776 .join((Permission, UserRepoToPerm.permission_id == Permission.permission_id))\
2776 .join((Permission, UserRepoToPerm.permission_id == Permission.permission_id))\
2777 .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
2777 .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
2778 .filter(UserRepoToPerm.user_id == user_id)
2778 .filter(UserRepoToPerm.user_id == user_id)
2779 if repo_id:
2779 if repo_id:
2780 q = q.filter(UserRepoToPerm.repository_id == repo_id)
2780 q = q.filter(UserRepoToPerm.repository_id == repo_id)
2781 return q.all()
2781 return q.all()
2782
2782
2783 @classmethod
2783 @classmethod
2784 def get_default_repo_perms_from_user_group(cls, user_id, repo_id=None):
2784 def get_default_repo_perms_from_user_group(cls, user_id, repo_id=None):
2785 q = Session().query(UserGroupRepoToPerm, Repository, Permission)\
2785 q = Session().query(UserGroupRepoToPerm, Repository, Permission)\
2786 .join(
2786 .join(
2787 Permission,
2787 Permission,
2788 UserGroupRepoToPerm.permission_id == Permission.permission_id)\
2788 UserGroupRepoToPerm.permission_id == Permission.permission_id)\
2789 .join(
2789 .join(
2790 Repository,
2790 Repository,
2791 UserGroupRepoToPerm.repository_id == Repository.repo_id)\
2791 UserGroupRepoToPerm.repository_id == Repository.repo_id)\
2792 .join(
2792 .join(
2793 UserGroup,
2793 UserGroup,
2794 UserGroupRepoToPerm.users_group_id ==
2794 UserGroupRepoToPerm.users_group_id ==
2795 UserGroup.users_group_id)\
2795 UserGroup.users_group_id)\
2796 .join(
2796 .join(
2797 UserGroupMember,
2797 UserGroupMember,
2798 UserGroupRepoToPerm.users_group_id ==
2798 UserGroupRepoToPerm.users_group_id ==
2799 UserGroupMember.users_group_id)\
2799 UserGroupMember.users_group_id)\
2800 .filter(
2800 .filter(
2801 UserGroupMember.user_id == user_id,
2801 UserGroupMember.user_id == user_id,
2802 UserGroup.users_group_active == true())
2802 UserGroup.users_group_active == true())
2803 if repo_id:
2803 if repo_id:
2804 q = q.filter(UserGroupRepoToPerm.repository_id == repo_id)
2804 q = q.filter(UserGroupRepoToPerm.repository_id == repo_id)
2805 return q.all()
2805 return q.all()
2806
2806
2807 @classmethod
2807 @classmethod
2808 def get_default_group_perms(cls, user_id, repo_group_id=None):
2808 def get_default_group_perms(cls, user_id, repo_group_id=None):
2809 q = Session().query(UserRepoGroupToPerm, RepoGroup, Permission)\
2809 q = Session().query(UserRepoGroupToPerm, RepoGroup, Permission)\
2810 .join((Permission, UserRepoGroupToPerm.permission_id == Permission.permission_id))\
2810 .join((Permission, UserRepoGroupToPerm.permission_id == Permission.permission_id))\
2811 .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id))\
2811 .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id))\
2812 .filter(UserRepoGroupToPerm.user_id == user_id)
2812 .filter(UserRepoGroupToPerm.user_id == user_id)
2813 if repo_group_id:
2813 if repo_group_id:
2814 q = q.filter(UserRepoGroupToPerm.group_id == repo_group_id)
2814 q = q.filter(UserRepoGroupToPerm.group_id == repo_group_id)
2815 return q.all()
2815 return q.all()
2816
2816
2817 @classmethod
2817 @classmethod
2818 def get_default_group_perms_from_user_group(
2818 def get_default_group_perms_from_user_group(
2819 cls, user_id, repo_group_id=None):
2819 cls, user_id, repo_group_id=None):
2820 q = Session().query(UserGroupRepoGroupToPerm, RepoGroup, Permission)\
2820 q = Session().query(UserGroupRepoGroupToPerm, RepoGroup, Permission)\
2821 .join(
2821 .join(
2822 Permission,
2822 Permission,
2823 UserGroupRepoGroupToPerm.permission_id ==
2823 UserGroupRepoGroupToPerm.permission_id ==
2824 Permission.permission_id)\
2824 Permission.permission_id)\
2825 .join(
2825 .join(
2826 RepoGroup,
2826 RepoGroup,
2827 UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id)\
2827 UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id)\
2828 .join(
2828 .join(
2829 UserGroup,
2829 UserGroup,
2830 UserGroupRepoGroupToPerm.users_group_id ==
2830 UserGroupRepoGroupToPerm.users_group_id ==
2831 UserGroup.users_group_id)\
2831 UserGroup.users_group_id)\
2832 .join(
2832 .join(
2833 UserGroupMember,
2833 UserGroupMember,
2834 UserGroupRepoGroupToPerm.users_group_id ==
2834 UserGroupRepoGroupToPerm.users_group_id ==
2835 UserGroupMember.users_group_id)\
2835 UserGroupMember.users_group_id)\
2836 .filter(
2836 .filter(
2837 UserGroupMember.user_id == user_id,
2837 UserGroupMember.user_id == user_id,
2838 UserGroup.users_group_active == true())
2838 UserGroup.users_group_active == true())
2839 if repo_group_id:
2839 if repo_group_id:
2840 q = q.filter(UserGroupRepoGroupToPerm.group_id == repo_group_id)
2840 q = q.filter(UserGroupRepoGroupToPerm.group_id == repo_group_id)
2841 return q.all()
2841 return q.all()
2842
2842
2843 @classmethod
2843 @classmethod
2844 def get_default_user_group_perms(cls, user_id, user_group_id=None):
2844 def get_default_user_group_perms(cls, user_id, user_group_id=None):
2845 q = Session().query(UserUserGroupToPerm, UserGroup, Permission)\
2845 q = Session().query(UserUserGroupToPerm, UserGroup, Permission)\
2846 .join((Permission, UserUserGroupToPerm.permission_id == Permission.permission_id))\
2846 .join((Permission, UserUserGroupToPerm.permission_id == Permission.permission_id))\
2847 .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id))\
2847 .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id))\
2848 .filter(UserUserGroupToPerm.user_id == user_id)
2848 .filter(UserUserGroupToPerm.user_id == user_id)
2849 if user_group_id:
2849 if user_group_id:
2850 q = q.filter(UserUserGroupToPerm.user_group_id == user_group_id)
2850 q = q.filter(UserUserGroupToPerm.user_group_id == user_group_id)
2851 return q.all()
2851 return q.all()
2852
2852
2853 @classmethod
2853 @classmethod
2854 def get_default_user_group_perms_from_user_group(
2854 def get_default_user_group_perms_from_user_group(
2855 cls, user_id, user_group_id=None):
2855 cls, user_id, user_group_id=None):
2856 TargetUserGroup = aliased(UserGroup, name='target_user_group')
2856 TargetUserGroup = aliased(UserGroup, name='target_user_group')
2857 q = Session().query(UserGroupUserGroupToPerm, UserGroup, Permission)\
2857 q = Session().query(UserGroupUserGroupToPerm, UserGroup, Permission)\
2858 .join(
2858 .join(
2859 Permission,
2859 Permission,
2860 UserGroupUserGroupToPerm.permission_id ==
2860 UserGroupUserGroupToPerm.permission_id ==
2861 Permission.permission_id)\
2861 Permission.permission_id)\
2862 .join(
2862 .join(
2863 TargetUserGroup,
2863 TargetUserGroup,
2864 UserGroupUserGroupToPerm.target_user_group_id ==
2864 UserGroupUserGroupToPerm.target_user_group_id ==
2865 TargetUserGroup.users_group_id)\
2865 TargetUserGroup.users_group_id)\
2866 .join(
2866 .join(
2867 UserGroup,
2867 UserGroup,
2868 UserGroupUserGroupToPerm.user_group_id ==
2868 UserGroupUserGroupToPerm.user_group_id ==
2869 UserGroup.users_group_id)\
2869 UserGroup.users_group_id)\
2870 .join(
2870 .join(
2871 UserGroupMember,
2871 UserGroupMember,
2872 UserGroupUserGroupToPerm.user_group_id ==
2872 UserGroupUserGroupToPerm.user_group_id ==
2873 UserGroupMember.users_group_id)\
2873 UserGroupMember.users_group_id)\
2874 .filter(
2874 .filter(
2875 UserGroupMember.user_id == user_id,
2875 UserGroupMember.user_id == user_id,
2876 UserGroup.users_group_active == true())
2876 UserGroup.users_group_active == true())
2877 if user_group_id:
2877 if user_group_id:
2878 q = q.filter(
2878 q = q.filter(
2879 UserGroupUserGroupToPerm.user_group_id == user_group_id)
2879 UserGroupUserGroupToPerm.user_group_id == user_group_id)
2880
2880
2881 return q.all()
2881 return q.all()
2882
2882
2883
2883
2884 class UserRepoToPerm(Base, BaseModel):
2884 class UserRepoToPerm(Base, BaseModel):
2885 __tablename__ = 'repo_to_perm'
2885 __tablename__ = 'repo_to_perm'
2886 __table_args__ = (
2886 __table_args__ = (
2887 UniqueConstraint('user_id', 'repository_id', 'permission_id'),
2887 UniqueConstraint('user_id', 'repository_id', 'permission_id'),
2888 base_table_args
2888 base_table_args
2889 )
2889 )
2890
2890
2891 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2891 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2892 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2892 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2893 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2893 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2894 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2894 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2895
2895
2896 user = relationship('User')
2896 user = relationship('User')
2897 repository = relationship('Repository')
2897 repository = relationship('Repository')
2898 permission = relationship('Permission')
2898 permission = relationship('Permission')
2899
2899
2900 @classmethod
2900 @classmethod
2901 def create(cls, user, repository, permission):
2901 def create(cls, user, repository, permission):
2902 n = cls()
2902 n = cls()
2903 n.user = user
2903 n.user = user
2904 n.repository = repository
2904 n.repository = repository
2905 n.permission = permission
2905 n.permission = permission
2906 Session().add(n)
2906 Session().add(n)
2907 return n
2907 return n
2908
2908
2909 def __unicode__(self):
2909 def __unicode__(self):
2910 return u'<%s => %s >' % (self.user, self.repository)
2910 return u'<%s => %s >' % (self.user, self.repository)
2911
2911
2912
2912
2913 class UserUserGroupToPerm(Base, BaseModel):
2913 class UserUserGroupToPerm(Base, BaseModel):
2914 __tablename__ = 'user_user_group_to_perm'
2914 __tablename__ = 'user_user_group_to_perm'
2915 __table_args__ = (
2915 __table_args__ = (
2916 UniqueConstraint('user_id', 'user_group_id', 'permission_id'),
2916 UniqueConstraint('user_id', 'user_group_id', 'permission_id'),
2917 base_table_args
2917 base_table_args
2918 )
2918 )
2919
2919
2920 user_user_group_to_perm_id = Column("user_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2920 user_user_group_to_perm_id = Column("user_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2921 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2921 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2922 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2922 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2923 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2923 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2924
2924
2925 user = relationship('User')
2925 user = relationship('User')
2926 user_group = relationship('UserGroup')
2926 user_group = relationship('UserGroup')
2927 permission = relationship('Permission')
2927 permission = relationship('Permission')
2928
2928
2929 @classmethod
2929 @classmethod
2930 def create(cls, user, user_group, permission):
2930 def create(cls, user, user_group, permission):
2931 n = cls()
2931 n = cls()
2932 n.user = user
2932 n.user = user
2933 n.user_group = user_group
2933 n.user_group = user_group
2934 n.permission = permission
2934 n.permission = permission
2935 Session().add(n)
2935 Session().add(n)
2936 return n
2936 return n
2937
2937
2938 def __unicode__(self):
2938 def __unicode__(self):
2939 return u'<%s => %s >' % (self.user, self.user_group)
2939 return u'<%s => %s >' % (self.user, self.user_group)
2940
2940
2941
2941
2942 class UserToPerm(Base, BaseModel):
2942 class UserToPerm(Base, BaseModel):
2943 __tablename__ = 'user_to_perm'
2943 __tablename__ = 'user_to_perm'
2944 __table_args__ = (
2944 __table_args__ = (
2945 UniqueConstraint('user_id', 'permission_id'),
2945 UniqueConstraint('user_id', 'permission_id'),
2946 base_table_args
2946 base_table_args
2947 )
2947 )
2948
2948
2949 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2949 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2950 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2950 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2951 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2951 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2952
2952
2953 user = relationship('User')
2953 user = relationship('User')
2954 permission = relationship('Permission', lazy='joined')
2954 permission = relationship('Permission', lazy='joined')
2955
2955
2956 def __unicode__(self):
2956 def __unicode__(self):
2957 return u'<%s => %s >' % (self.user, self.permission)
2957 return u'<%s => %s >' % (self.user, self.permission)
2958
2958
2959
2959
2960 class UserGroupRepoToPerm(Base, BaseModel):
2960 class UserGroupRepoToPerm(Base, BaseModel):
2961 __tablename__ = 'users_group_repo_to_perm'
2961 __tablename__ = 'users_group_repo_to_perm'
2962 __table_args__ = (
2962 __table_args__ = (
2963 UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
2963 UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
2964 base_table_args
2964 base_table_args
2965 )
2965 )
2966
2966
2967 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2967 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2968 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2968 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2969 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2969 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2970 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2970 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2971
2971
2972 users_group = relationship('UserGroup')
2972 users_group = relationship('UserGroup')
2973 permission = relationship('Permission')
2973 permission = relationship('Permission')
2974 repository = relationship('Repository')
2974 repository = relationship('Repository')
2975
2975
2976 @classmethod
2976 @classmethod
2977 def create(cls, users_group, repository, permission):
2977 def create(cls, users_group, repository, permission):
2978 n = cls()
2978 n = cls()
2979 n.users_group = users_group
2979 n.users_group = users_group
2980 n.repository = repository
2980 n.repository = repository
2981 n.permission = permission
2981 n.permission = permission
2982 Session().add(n)
2982 Session().add(n)
2983 return n
2983 return n
2984
2984
2985 def __unicode__(self):
2985 def __unicode__(self):
2986 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
2986 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
2987
2987
2988
2988
2989 class UserGroupUserGroupToPerm(Base, BaseModel):
2989 class UserGroupUserGroupToPerm(Base, BaseModel):
2990 __tablename__ = 'user_group_user_group_to_perm'
2990 __tablename__ = 'user_group_user_group_to_perm'
2991 __table_args__ = (
2991 __table_args__ = (
2992 UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'),
2992 UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'),
2993 CheckConstraint('target_user_group_id != user_group_id'),
2993 CheckConstraint('target_user_group_id != user_group_id'),
2994 base_table_args
2994 base_table_args
2995 )
2995 )
2996
2996
2997 user_group_user_group_to_perm_id = Column("user_group_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2997 user_group_user_group_to_perm_id = Column("user_group_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2998 target_user_group_id = Column("target_user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2998 target_user_group_id = Column("target_user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2999 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2999 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3000 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3000 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3001
3001
3002 target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id')
3002 target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id')
3003 user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id')
3003 user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id')
3004 permission = relationship('Permission')
3004 permission = relationship('Permission')
3005
3005
3006 @classmethod
3006 @classmethod
3007 def create(cls, target_user_group, user_group, permission):
3007 def create(cls, target_user_group, user_group, permission):
3008 n = cls()
3008 n = cls()
3009 n.target_user_group = target_user_group
3009 n.target_user_group = target_user_group
3010 n.user_group = user_group
3010 n.user_group = user_group
3011 n.permission = permission
3011 n.permission = permission
3012 Session().add(n)
3012 Session().add(n)
3013 return n
3013 return n
3014
3014
3015 def __unicode__(self):
3015 def __unicode__(self):
3016 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3016 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3017
3017
3018
3018
3019 class UserGroupToPerm(Base, BaseModel):
3019 class UserGroupToPerm(Base, BaseModel):
3020 __tablename__ = 'users_group_to_perm'
3020 __tablename__ = 'users_group_to_perm'
3021 __table_args__ = (
3021 __table_args__ = (
3022 UniqueConstraint('users_group_id', 'permission_id',),
3022 UniqueConstraint('users_group_id', 'permission_id',),
3023 base_table_args
3023 base_table_args
3024 )
3024 )
3025
3025
3026 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3026 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3027 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3027 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3028 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3028 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3029
3029
3030 users_group = relationship('UserGroup')
3030 users_group = relationship('UserGroup')
3031 permission = relationship('Permission')
3031 permission = relationship('Permission')
3032
3032
3033
3033
3034 class UserRepoGroupToPerm(Base, BaseModel):
3034 class UserRepoGroupToPerm(Base, BaseModel):
3035 __tablename__ = 'user_repo_group_to_perm'
3035 __tablename__ = 'user_repo_group_to_perm'
3036 __table_args__ = (
3036 __table_args__ = (
3037 UniqueConstraint('user_id', 'group_id', 'permission_id'),
3037 UniqueConstraint('user_id', 'group_id', 'permission_id'),
3038 base_table_args
3038 base_table_args
3039 )
3039 )
3040
3040
3041 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3041 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3042 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3042 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3043 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3043 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3044 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3044 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3045
3045
3046 user = relationship('User')
3046 user = relationship('User')
3047 group = relationship('RepoGroup')
3047 group = relationship('RepoGroup')
3048 permission = relationship('Permission')
3048 permission = relationship('Permission')
3049
3049
3050 @classmethod
3050 @classmethod
3051 def create(cls, user, repository_group, permission):
3051 def create(cls, user, repository_group, permission):
3052 n = cls()
3052 n = cls()
3053 n.user = user
3053 n.user = user
3054 n.group = repository_group
3054 n.group = repository_group
3055 n.permission = permission
3055 n.permission = permission
3056 Session().add(n)
3056 Session().add(n)
3057 return n
3057 return n
3058
3058
3059
3059
3060 class UserGroupRepoGroupToPerm(Base, BaseModel):
3060 class UserGroupRepoGroupToPerm(Base, BaseModel):
3061 __tablename__ = 'users_group_repo_group_to_perm'
3061 __tablename__ = 'users_group_repo_group_to_perm'
3062 __table_args__ = (
3062 __table_args__ = (
3063 UniqueConstraint('users_group_id', 'group_id'),
3063 UniqueConstraint('users_group_id', 'group_id'),
3064 base_table_args
3064 base_table_args
3065 )
3065 )
3066
3066
3067 users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3067 users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3068 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3068 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3069 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3069 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3070 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3070 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3071
3071
3072 users_group = relationship('UserGroup')
3072 users_group = relationship('UserGroup')
3073 permission = relationship('Permission')
3073 permission = relationship('Permission')
3074 group = relationship('RepoGroup')
3074 group = relationship('RepoGroup')
3075
3075
3076 @classmethod
3076 @classmethod
3077 def create(cls, user_group, repository_group, permission):
3077 def create(cls, user_group, repository_group, permission):
3078 n = cls()
3078 n = cls()
3079 n.users_group = user_group
3079 n.users_group = user_group
3080 n.group = repository_group
3080 n.group = repository_group
3081 n.permission = permission
3081 n.permission = permission
3082 Session().add(n)
3082 Session().add(n)
3083 return n
3083 return n
3084
3084
3085 def __unicode__(self):
3085 def __unicode__(self):
3086 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3086 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3087
3087
3088
3088
3089 class Statistics(Base, BaseModel):
3089 class Statistics(Base, BaseModel):
3090 __tablename__ = 'statistics'
3090 __tablename__ = 'statistics'
3091 __table_args__ = (
3091 __table_args__ = (
3092 base_table_args
3092 base_table_args
3093 )
3093 )
3094
3094
3095 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3095 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3096 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
3096 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
3097 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
3097 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
3098 commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
3098 commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
3099 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
3099 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
3100 languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
3100 languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
3101
3101
3102 repository = relationship('Repository', single_parent=True)
3102 repository = relationship('Repository', single_parent=True)
3103
3103
3104
3104
3105 class UserFollowing(Base, BaseModel):
3105 class UserFollowing(Base, BaseModel):
3106 __tablename__ = 'user_followings'
3106 __tablename__ = 'user_followings'
3107 __table_args__ = (
3107 __table_args__ = (
3108 UniqueConstraint('user_id', 'follows_repository_id'),
3108 UniqueConstraint('user_id', 'follows_repository_id'),
3109 UniqueConstraint('user_id', 'follows_user_id'),
3109 UniqueConstraint('user_id', 'follows_user_id'),
3110 base_table_args
3110 base_table_args
3111 )
3111 )
3112
3112
3113 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3113 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3114 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3114 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3115 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
3115 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
3116 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
3116 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
3117 follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
3117 follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
3118
3118
3119 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
3119 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
3120
3120
3121 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
3121 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
3122 follows_repository = relationship('Repository', order_by='Repository.repo_name')
3122 follows_repository = relationship('Repository', order_by='Repository.repo_name')
3123
3123
3124 @classmethod
3124 @classmethod
3125 def get_repo_followers(cls, repo_id):
3125 def get_repo_followers(cls, repo_id):
3126 return cls.query().filter(cls.follows_repo_id == repo_id)
3126 return cls.query().filter(cls.follows_repo_id == repo_id)
3127
3127
3128
3128
3129 class CacheKey(Base, BaseModel):
3129 class CacheKey(Base, BaseModel):
3130 __tablename__ = 'cache_invalidation'
3130 __tablename__ = 'cache_invalidation'
3131 __table_args__ = (
3131 __table_args__ = (
3132 UniqueConstraint('cache_key'),
3132 UniqueConstraint('cache_key'),
3133 Index('key_idx', 'cache_key'),
3133 Index('key_idx', 'cache_key'),
3134 base_table_args,
3134 base_table_args,
3135 )
3135 )
3136
3136
3137 CACHE_TYPE_ATOM = 'ATOM'
3137 CACHE_TYPE_ATOM = 'ATOM'
3138 CACHE_TYPE_RSS = 'RSS'
3138 CACHE_TYPE_RSS = 'RSS'
3139 CACHE_TYPE_README = 'README'
3139 CACHE_TYPE_README = 'README'
3140
3140
3141 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3141 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3142 cache_key = Column("cache_key", String(255), nullable=True, unique=None, default=None)
3142 cache_key = Column("cache_key", String(255), nullable=True, unique=None, default=None)
3143 cache_args = Column("cache_args", String(255), nullable=True, unique=None, default=None)
3143 cache_args = Column("cache_args", String(255), nullable=True, unique=None, default=None)
3144 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
3144 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
3145
3145
3146 def __init__(self, cache_key, cache_args=''):
3146 def __init__(self, cache_key, cache_args=''):
3147 self.cache_key = cache_key
3147 self.cache_key = cache_key
3148 self.cache_args = cache_args
3148 self.cache_args = cache_args
3149 self.cache_active = False
3149 self.cache_active = False
3150
3150
3151 def __unicode__(self):
3151 def __unicode__(self):
3152 return u"<%s('%s:%s[%s]')>" % (
3152 return u"<%s('%s:%s[%s]')>" % (
3153 self.__class__.__name__,
3153 self.__class__.__name__,
3154 self.cache_id, self.cache_key, self.cache_active)
3154 self.cache_id, self.cache_key, self.cache_active)
3155
3155
3156 def _cache_key_partition(self):
3156 def _cache_key_partition(self):
3157 prefix, repo_name, suffix = self.cache_key.partition(self.cache_args)
3157 prefix, repo_name, suffix = self.cache_key.partition(self.cache_args)
3158 return prefix, repo_name, suffix
3158 return prefix, repo_name, suffix
3159
3159
3160 def get_prefix(self):
3160 def get_prefix(self):
3161 """
3161 """
3162 Try to extract prefix from existing cache key. The key could consist
3162 Try to extract prefix from existing cache key. The key could consist
3163 of prefix, repo_name, suffix
3163 of prefix, repo_name, suffix
3164 """
3164 """
3165 # this returns prefix, repo_name, suffix
3165 # this returns prefix, repo_name, suffix
3166 return self._cache_key_partition()[0]
3166 return self._cache_key_partition()[0]
3167
3167
3168 def get_suffix(self):
3168 def get_suffix(self):
3169 """
3169 """
3170 get suffix that might have been used in _get_cache_key to
3170 get suffix that might have been used in _get_cache_key to
3171 generate self.cache_key. Only used for informational purposes
3171 generate self.cache_key. Only used for informational purposes
3172 in repo_edit.mako.
3172 in repo_edit.mako.
3173 """
3173 """
3174 # prefix, repo_name, suffix
3174 # prefix, repo_name, suffix
3175 return self._cache_key_partition()[2]
3175 return self._cache_key_partition()[2]
3176
3176
3177 @classmethod
3177 @classmethod
3178 def delete_all_cache(cls):
3178 def delete_all_cache(cls):
3179 """
3179 """
3180 Delete all cache keys from database.
3180 Delete all cache keys from database.
3181 Should only be run when all instances are down and all entries
3181 Should only be run when all instances are down and all entries
3182 thus stale.
3182 thus stale.
3183 """
3183 """
3184 cls.query().delete()
3184 cls.query().delete()
3185 Session().commit()
3185 Session().commit()
3186
3186
3187 @classmethod
3187 @classmethod
3188 def get_cache_key(cls, repo_name, cache_type):
3188 def get_cache_key(cls, repo_name, cache_type):
3189 """
3189 """
3190
3190
3191 Generate a cache key for this process of RhodeCode instance.
3191 Generate a cache key for this process of RhodeCode instance.
3192 Prefix most likely will be process id or maybe explicitly set
3192 Prefix most likely will be process id or maybe explicitly set
3193 instance_id from .ini file.
3193 instance_id from .ini file.
3194 """
3194 """
3195 import rhodecode
3195 import rhodecode
3196 prefix = safe_unicode(rhodecode.CONFIG.get('instance_id') or '')
3196 prefix = safe_unicode(rhodecode.CONFIG.get('instance_id') or '')
3197
3197
3198 repo_as_unicode = safe_unicode(repo_name)
3198 repo_as_unicode = safe_unicode(repo_name)
3199 key = u'{}_{}'.format(repo_as_unicode, cache_type) \
3199 key = u'{}_{}'.format(repo_as_unicode, cache_type) \
3200 if cache_type else repo_as_unicode
3200 if cache_type else repo_as_unicode
3201
3201
3202 return u'{}{}'.format(prefix, key)
3202 return u'{}{}'.format(prefix, key)
3203
3203
3204 @classmethod
3204 @classmethod
3205 def set_invalidate(cls, repo_name, delete=False):
3205 def set_invalidate(cls, repo_name, delete=False):
3206 """
3206 """
3207 Mark all caches of a repo as invalid in the database.
3207 Mark all caches of a repo as invalid in the database.
3208 """
3208 """
3209
3209
3210 try:
3210 try:
3211 qry = Session().query(cls).filter(cls.cache_args == repo_name)
3211 qry = Session().query(cls).filter(cls.cache_args == repo_name)
3212 if delete:
3212 if delete:
3213 log.debug('cache objects deleted for repo %s',
3213 log.debug('cache objects deleted for repo %s',
3214 safe_str(repo_name))
3214 safe_str(repo_name))
3215 qry.delete()
3215 qry.delete()
3216 else:
3216 else:
3217 log.debug('cache objects marked as invalid for repo %s',
3217 log.debug('cache objects marked as invalid for repo %s',
3218 safe_str(repo_name))
3218 safe_str(repo_name))
3219 qry.update({"cache_active": False})
3219 qry.update({"cache_active": False})
3220
3220
3221 Session().commit()
3221 Session().commit()
3222 except Exception:
3222 except Exception:
3223 log.exception(
3223 log.exception(
3224 'Cache key invalidation failed for repository %s',
3224 'Cache key invalidation failed for repository %s',
3225 safe_str(repo_name))
3225 safe_str(repo_name))
3226 Session().rollback()
3226 Session().rollback()
3227
3227
3228 @classmethod
3228 @classmethod
3229 def get_active_cache(cls, cache_key):
3229 def get_active_cache(cls, cache_key):
3230 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
3230 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
3231 if inv_obj:
3231 if inv_obj:
3232 return inv_obj
3232 return inv_obj
3233 return None
3233 return None
3234
3234
3235 @classmethod
3235 @classmethod
3236 def repo_context_cache(cls, compute_func, repo_name, cache_type,
3236 def repo_context_cache(cls, compute_func, repo_name, cache_type,
3237 thread_scoped=False):
3237 thread_scoped=False):
3238 """
3238 """
3239 @cache_region('long_term')
3239 @cache_region('long_term')
3240 def _heavy_calculation(cache_key):
3240 def _heavy_calculation(cache_key):
3241 return 'result'
3241 return 'result'
3242
3242
3243 cache_context = CacheKey.repo_context_cache(
3243 cache_context = CacheKey.repo_context_cache(
3244 _heavy_calculation, repo_name, cache_type)
3244 _heavy_calculation, repo_name, cache_type)
3245
3245
3246 with cache_context as context:
3246 with cache_context as context:
3247 context.invalidate()
3247 context.invalidate()
3248 computed = context.compute()
3248 computed = context.compute()
3249
3249
3250 assert computed == 'result'
3250 assert computed == 'result'
3251 """
3251 """
3252 from rhodecode.lib import caches
3252 from rhodecode.lib import caches
3253 return caches.InvalidationContext(
3253 return caches.InvalidationContext(
3254 compute_func, repo_name, cache_type, thread_scoped=thread_scoped)
3254 compute_func, repo_name, cache_type, thread_scoped=thread_scoped)
3255
3255
3256
3256
3257 class ChangesetComment(Base, BaseModel):
3257 class ChangesetComment(Base, BaseModel):
3258 __tablename__ = 'changeset_comments'
3258 __tablename__ = 'changeset_comments'
3259 __table_args__ = (
3259 __table_args__ = (
3260 Index('cc_revision_idx', 'revision'),
3260 Index('cc_revision_idx', 'revision'),
3261 base_table_args,
3261 base_table_args,
3262 )
3262 )
3263
3263
3264 COMMENT_OUTDATED = u'comment_outdated'
3264 COMMENT_OUTDATED = u'comment_outdated'
3265 COMMENT_TYPE_NOTE = u'note'
3265 COMMENT_TYPE_NOTE = u'note'
3266 COMMENT_TYPE_TODO = u'todo'
3266 COMMENT_TYPE_TODO = u'todo'
3267 COMMENT_TYPES = [COMMENT_TYPE_NOTE, COMMENT_TYPE_TODO]
3267 COMMENT_TYPES = [COMMENT_TYPE_NOTE, COMMENT_TYPE_TODO]
3268
3268
3269 comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
3269 comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
3270 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3270 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3271 revision = Column('revision', String(40), nullable=True)
3271 revision = Column('revision', String(40), nullable=True)
3272 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3272 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3273 pull_request_version_id = Column("pull_request_version_id", Integer(), ForeignKey('pull_request_versions.pull_request_version_id'), nullable=True)
3273 pull_request_version_id = Column("pull_request_version_id", Integer(), ForeignKey('pull_request_versions.pull_request_version_id'), nullable=True)
3274 line_no = Column('line_no', Unicode(10), nullable=True)
3274 line_no = Column('line_no', Unicode(10), nullable=True)
3275 hl_lines = Column('hl_lines', Unicode(512), nullable=True)
3275 hl_lines = Column('hl_lines', Unicode(512), nullable=True)
3276 f_path = Column('f_path', Unicode(1000), nullable=True)
3276 f_path = Column('f_path', Unicode(1000), nullable=True)
3277 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3277 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3278 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3278 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3279 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3279 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3280 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3280 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3281 renderer = Column('renderer', Unicode(64), nullable=True)
3281 renderer = Column('renderer', Unicode(64), nullable=True)
3282 display_state = Column('display_state', Unicode(128), nullable=True)
3282 display_state = Column('display_state', Unicode(128), nullable=True)
3283
3283
3284 comment_type = Column('comment_type', Unicode(128), nullable=True, default=COMMENT_TYPE_NOTE)
3284 comment_type = Column('comment_type', Unicode(128), nullable=True, default=COMMENT_TYPE_NOTE)
3285 resolved_comment_id = Column('resolved_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=True)
3285 resolved_comment_id = Column('resolved_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=True)
3286 resolved_comment = relationship('ChangesetComment', remote_side=comment_id, backref='resolved_by')
3286 resolved_comment = relationship('ChangesetComment', remote_side=comment_id, backref='resolved_by')
3287 author = relationship('User', lazy='joined')
3287 author = relationship('User', lazy='joined')
3288 repo = relationship('Repository')
3288 repo = relationship('Repository')
3289 status_change = relationship('ChangesetStatus', cascade="all, delete, delete-orphan", lazy='joined')
3289 status_change = relationship('ChangesetStatus', cascade="all, delete, delete-orphan", lazy='joined')
3290 pull_request = relationship('PullRequest', lazy='joined')
3290 pull_request = relationship('PullRequest', lazy='joined')
3291 pull_request_version = relationship('PullRequestVersion')
3291 pull_request_version = relationship('PullRequestVersion')
3292
3292
3293 @classmethod
3293 @classmethod
3294 def get_users(cls, revision=None, pull_request_id=None):
3294 def get_users(cls, revision=None, pull_request_id=None):
3295 """
3295 """
3296 Returns user associated with this ChangesetComment. ie those
3296 Returns user associated with this ChangesetComment. ie those
3297 who actually commented
3297 who actually commented
3298
3298
3299 :param cls:
3299 :param cls:
3300 :param revision:
3300 :param revision:
3301 """
3301 """
3302 q = Session().query(User)\
3302 q = Session().query(User)\
3303 .join(ChangesetComment.author)
3303 .join(ChangesetComment.author)
3304 if revision:
3304 if revision:
3305 q = q.filter(cls.revision == revision)
3305 q = q.filter(cls.revision == revision)
3306 elif pull_request_id:
3306 elif pull_request_id:
3307 q = q.filter(cls.pull_request_id == pull_request_id)
3307 q = q.filter(cls.pull_request_id == pull_request_id)
3308 return q.all()
3308 return q.all()
3309
3309
3310 @classmethod
3310 @classmethod
3311 def get_index_from_version(cls, pr_version, versions):
3311 def get_index_from_version(cls, pr_version, versions):
3312 num_versions = [x.pull_request_version_id for x in versions]
3312 num_versions = [x.pull_request_version_id for x in versions]
3313 try:
3313 try:
3314 return num_versions.index(pr_version) +1
3314 return num_versions.index(pr_version) +1
3315 except (IndexError, ValueError):
3315 except (IndexError, ValueError):
3316 return
3316 return
3317
3317
3318 @property
3318 @property
3319 def outdated(self):
3319 def outdated(self):
3320 return self.display_state == self.COMMENT_OUTDATED
3320 return self.display_state == self.COMMENT_OUTDATED
3321
3321
3322 def outdated_at_version(self, version):
3322 def outdated_at_version(self, version):
3323 """
3323 """
3324 Checks if comment is outdated for given pull request version
3324 Checks if comment is outdated for given pull request version
3325 """
3325 """
3326 return self.outdated and self.pull_request_version_id != version
3326 return self.outdated and self.pull_request_version_id != version
3327
3327
3328 def older_than_version(self, version):
3328 def older_than_version(self, version):
3329 """
3329 """
3330 Checks if comment is made from previous version than given
3330 Checks if comment is made from previous version than given
3331 """
3331 """
3332 if version is None:
3332 if version is None:
3333 return self.pull_request_version_id is not None
3333 return self.pull_request_version_id is not None
3334
3334
3335 return self.pull_request_version_id < version
3335 return self.pull_request_version_id < version
3336
3336
3337 @property
3337 @property
3338 def resolved(self):
3338 def resolved(self):
3339 return self.resolved_by[0] if self.resolved_by else None
3339 return self.resolved_by[0] if self.resolved_by else None
3340
3340
3341 @property
3341 @property
3342 def is_todo(self):
3342 def is_todo(self):
3343 return self.comment_type == self.COMMENT_TYPE_TODO
3343 return self.comment_type == self.COMMENT_TYPE_TODO
3344
3344
3345 @property
3345 @property
3346 def is_inline(self):
3346 def is_inline(self):
3347 return self.line_no and self.f_path
3347 return self.line_no and self.f_path
3348
3348
3349 def get_index_version(self, versions):
3349 def get_index_version(self, versions):
3350 return self.get_index_from_version(
3350 return self.get_index_from_version(
3351 self.pull_request_version_id, versions)
3351 self.pull_request_version_id, versions)
3352
3352
3353 def __repr__(self):
3353 def __repr__(self):
3354 if self.comment_id:
3354 if self.comment_id:
3355 return '<DB:Comment #%s>' % self.comment_id
3355 return '<DB:Comment #%s>' % self.comment_id
3356 else:
3356 else:
3357 return '<DB:Comment at %#x>' % id(self)
3357 return '<DB:Comment at %#x>' % id(self)
3358
3358
3359 def get_api_data(self):
3359 def get_api_data(self):
3360 comment = self
3360 comment = self
3361 data = {
3361 data = {
3362 'comment_id': comment.comment_id,
3362 'comment_id': comment.comment_id,
3363 'comment_type': comment.comment_type,
3363 'comment_type': comment.comment_type,
3364 'comment_text': comment.text,
3364 'comment_text': comment.text,
3365 'comment_status': comment.status_change,
3365 'comment_status': comment.status_change,
3366 'comment_f_path': comment.f_path,
3366 'comment_f_path': comment.f_path,
3367 'comment_lineno': comment.line_no,
3367 'comment_lineno': comment.line_no,
3368 'comment_author': comment.author,
3368 'comment_author': comment.author,
3369 'comment_created_on': comment.created_on
3369 'comment_created_on': comment.created_on
3370 }
3370 }
3371 return data
3371 return data
3372
3372
3373 def __json__(self):
3373 def __json__(self):
3374 data = dict()
3374 data = dict()
3375 data.update(self.get_api_data())
3375 data.update(self.get_api_data())
3376 return data
3376 return data
3377
3377
3378
3378
3379 class ChangesetStatus(Base, BaseModel):
3379 class ChangesetStatus(Base, BaseModel):
3380 __tablename__ = 'changeset_statuses'
3380 __tablename__ = 'changeset_statuses'
3381 __table_args__ = (
3381 __table_args__ = (
3382 Index('cs_revision_idx', 'revision'),
3382 Index('cs_revision_idx', 'revision'),
3383 Index('cs_version_idx', 'version'),
3383 Index('cs_version_idx', 'version'),
3384 UniqueConstraint('repo_id', 'revision', 'version'),
3384 UniqueConstraint('repo_id', 'revision', 'version'),
3385 base_table_args
3385 base_table_args
3386 )
3386 )
3387
3387
3388 STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed'
3388 STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed'
3389 STATUS_APPROVED = 'approved'
3389 STATUS_APPROVED = 'approved'
3390 STATUS_REJECTED = 'rejected'
3390 STATUS_REJECTED = 'rejected'
3391 STATUS_UNDER_REVIEW = 'under_review'
3391 STATUS_UNDER_REVIEW = 'under_review'
3392
3392
3393 STATUSES = [
3393 STATUSES = [
3394 (STATUS_NOT_REVIEWED, _("Not Reviewed")), # (no icon) and default
3394 (STATUS_NOT_REVIEWED, _("Not Reviewed")), # (no icon) and default
3395 (STATUS_APPROVED, _("Approved")),
3395 (STATUS_APPROVED, _("Approved")),
3396 (STATUS_REJECTED, _("Rejected")),
3396 (STATUS_REJECTED, _("Rejected")),
3397 (STATUS_UNDER_REVIEW, _("Under Review")),
3397 (STATUS_UNDER_REVIEW, _("Under Review")),
3398 ]
3398 ]
3399
3399
3400 changeset_status_id = Column('changeset_status_id', Integer(), nullable=False, primary_key=True)
3400 changeset_status_id = Column('changeset_status_id', Integer(), nullable=False, primary_key=True)
3401 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3401 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3402 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None)
3402 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None)
3403 revision = Column('revision', String(40), nullable=False)
3403 revision = Column('revision', String(40), nullable=False)
3404 status = Column('status', String(128), nullable=False, default=DEFAULT)
3404 status = Column('status', String(128), nullable=False, default=DEFAULT)
3405 changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'))
3405 changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'))
3406 modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now)
3406 modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now)
3407 version = Column('version', Integer(), nullable=False, default=0)
3407 version = Column('version', Integer(), nullable=False, default=0)
3408 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3408 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3409
3409
3410 author = relationship('User', lazy='joined')
3410 author = relationship('User', lazy='joined')
3411 repo = relationship('Repository')
3411 repo = relationship('Repository')
3412 comment = relationship('ChangesetComment', lazy='joined')
3412 comment = relationship('ChangesetComment', lazy='joined')
3413 pull_request = relationship('PullRequest', lazy='joined')
3413 pull_request = relationship('PullRequest', lazy='joined')
3414
3414
3415 def __unicode__(self):
3415 def __unicode__(self):
3416 return u"<%s('%s[v%s]:%s')>" % (
3416 return u"<%s('%s[v%s]:%s')>" % (
3417 self.__class__.__name__,
3417 self.__class__.__name__,
3418 self.status, self.version, self.author
3418 self.status, self.version, self.author
3419 )
3419 )
3420
3420
3421 @classmethod
3421 @classmethod
3422 def get_status_lbl(cls, value):
3422 def get_status_lbl(cls, value):
3423 return dict(cls.STATUSES).get(value)
3423 return dict(cls.STATUSES).get(value)
3424
3424
3425 @property
3425 @property
3426 def status_lbl(self):
3426 def status_lbl(self):
3427 return ChangesetStatus.get_status_lbl(self.status)
3427 return ChangesetStatus.get_status_lbl(self.status)
3428
3428
3429 def get_api_data(self):
3429 def get_api_data(self):
3430 status = self
3430 status = self
3431 data = {
3431 data = {
3432 'status_id': status.changeset_status_id,
3432 'status_id': status.changeset_status_id,
3433 'status': status.status,
3433 'status': status.status,
3434 }
3434 }
3435 return data
3435 return data
3436
3436
3437 def __json__(self):
3437 def __json__(self):
3438 data = dict()
3438 data = dict()
3439 data.update(self.get_api_data())
3439 data.update(self.get_api_data())
3440 return data
3440 return data
3441
3441
3442
3442
3443 class _PullRequestBase(BaseModel):
3443 class _PullRequestBase(BaseModel):
3444 """
3444 """
3445 Common attributes of pull request and version entries.
3445 Common attributes of pull request and version entries.
3446 """
3446 """
3447
3447
3448 # .status values
3448 # .status values
3449 STATUS_NEW = u'new'
3449 STATUS_NEW = u'new'
3450 STATUS_OPEN = u'open'
3450 STATUS_OPEN = u'open'
3451 STATUS_CLOSED = u'closed'
3451 STATUS_CLOSED = u'closed'
3452
3452
3453 title = Column('title', Unicode(255), nullable=True)
3453 title = Column('title', Unicode(255), nullable=True)
3454 description = Column(
3454 description = Column(
3455 'description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'),
3455 'description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'),
3456 nullable=True)
3456 nullable=True)
3457 description_renderer = Column('description_renderer', Unicode(64), nullable=True)
3458
3457 # new/open/closed status of pull request (not approve/reject/etc)
3459 # new/open/closed status of pull request (not approve/reject/etc)
3458 status = Column('status', Unicode(255), nullable=False, default=STATUS_NEW)
3460 status = Column('status', Unicode(255), nullable=False, default=STATUS_NEW)
3459 created_on = Column(
3461 created_on = Column(
3460 'created_on', DateTime(timezone=False), nullable=False,
3462 'created_on', DateTime(timezone=False), nullable=False,
3461 default=datetime.datetime.now)
3463 default=datetime.datetime.now)
3462 updated_on = Column(
3464 updated_on = Column(
3463 'updated_on', DateTime(timezone=False), nullable=False,
3465 'updated_on', DateTime(timezone=False), nullable=False,
3464 default=datetime.datetime.now)
3466 default=datetime.datetime.now)
3465
3467
3466 @declared_attr
3468 @declared_attr
3467 def user_id(cls):
3469 def user_id(cls):
3468 return Column(
3470 return Column(
3469 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
3471 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
3470 unique=None)
3472 unique=None)
3471
3473
3472 # 500 revisions max
3474 # 500 revisions max
3473 _revisions = Column(
3475 _revisions = Column(
3474 'revisions', UnicodeText().with_variant(UnicodeText(20500), 'mysql'))
3476 'revisions', UnicodeText().with_variant(UnicodeText(20500), 'mysql'))
3475
3477
3476 @declared_attr
3478 @declared_attr
3477 def source_repo_id(cls):
3479 def source_repo_id(cls):
3478 # TODO: dan: rename column to source_repo_id
3480 # TODO: dan: rename column to source_repo_id
3479 return Column(
3481 return Column(
3480 'org_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3482 'org_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3481 nullable=False)
3483 nullable=False)
3482
3484
3483 source_ref = Column('org_ref', Unicode(255), nullable=False)
3485 source_ref = Column('org_ref', Unicode(255), nullable=False)
3484
3486
3485 @declared_attr
3487 @declared_attr
3486 def target_repo_id(cls):
3488 def target_repo_id(cls):
3487 # TODO: dan: rename column to target_repo_id
3489 # TODO: dan: rename column to target_repo_id
3488 return Column(
3490 return Column(
3489 'other_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3491 'other_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3490 nullable=False)
3492 nullable=False)
3491
3493
3492 target_ref = Column('other_ref', Unicode(255), nullable=False)
3494 target_ref = Column('other_ref', Unicode(255), nullable=False)
3493 _shadow_merge_ref = Column('shadow_merge_ref', Unicode(255), nullable=True)
3495 _shadow_merge_ref = Column('shadow_merge_ref', Unicode(255), nullable=True)
3494
3496
3495 # TODO: dan: rename column to last_merge_source_rev
3497 # TODO: dan: rename column to last_merge_source_rev
3496 _last_merge_source_rev = Column(
3498 _last_merge_source_rev = Column(
3497 'last_merge_org_rev', String(40), nullable=True)
3499 'last_merge_org_rev', String(40), nullable=True)
3498 # TODO: dan: rename column to last_merge_target_rev
3500 # TODO: dan: rename column to last_merge_target_rev
3499 _last_merge_target_rev = Column(
3501 _last_merge_target_rev = Column(
3500 'last_merge_other_rev', String(40), nullable=True)
3502 'last_merge_other_rev', String(40), nullable=True)
3501 _last_merge_status = Column('merge_status', Integer(), nullable=True)
3503 _last_merge_status = Column('merge_status', Integer(), nullable=True)
3502 merge_rev = Column('merge_rev', String(40), nullable=True)
3504 merge_rev = Column('merge_rev', String(40), nullable=True)
3503
3505
3504 reviewer_data = Column(
3506 reviewer_data = Column(
3505 'reviewer_data_json', MutationObj.as_mutable(
3507 'reviewer_data_json', MutationObj.as_mutable(
3506 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
3508 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
3507
3509
3508 @property
3510 @property
3509 def reviewer_data_json(self):
3511 def reviewer_data_json(self):
3510 return json.dumps(self.reviewer_data)
3512 return json.dumps(self.reviewer_data)
3511
3513
3512 @hybrid_property
3514 @hybrid_property
3513 def description_safe(self):
3515 def description_safe(self):
3514 from rhodecode.lib import helpers as h
3516 from rhodecode.lib import helpers as h
3515 return h.escape(self.description)
3517 return h.escape(self.description)
3516
3518
3517 @hybrid_property
3519 @hybrid_property
3518 def revisions(self):
3520 def revisions(self):
3519 return self._revisions.split(':') if self._revisions else []
3521 return self._revisions.split(':') if self._revisions else []
3520
3522
3521 @revisions.setter
3523 @revisions.setter
3522 def revisions(self, val):
3524 def revisions(self, val):
3523 self._revisions = ':'.join(val)
3525 self._revisions = ':'.join(val)
3524
3526
3525 @hybrid_property
3527 @hybrid_property
3526 def last_merge_status(self):
3528 def last_merge_status(self):
3527 return safe_int(self._last_merge_status)
3529 return safe_int(self._last_merge_status)
3528
3530
3529 @last_merge_status.setter
3531 @last_merge_status.setter
3530 def last_merge_status(self, val):
3532 def last_merge_status(self, val):
3531 self._last_merge_status = val
3533 self._last_merge_status = val
3532
3534
3533 @declared_attr
3535 @declared_attr
3534 def author(cls):
3536 def author(cls):
3535 return relationship('User', lazy='joined')
3537 return relationship('User', lazy='joined')
3536
3538
3537 @declared_attr
3539 @declared_attr
3538 def source_repo(cls):
3540 def source_repo(cls):
3539 return relationship(
3541 return relationship(
3540 'Repository',
3542 'Repository',
3541 primaryjoin='%s.source_repo_id==Repository.repo_id' % cls.__name__)
3543 primaryjoin='%s.source_repo_id==Repository.repo_id' % cls.__name__)
3542
3544
3543 @property
3545 @property
3544 def source_ref_parts(self):
3546 def source_ref_parts(self):
3545 return self.unicode_to_reference(self.source_ref)
3547 return self.unicode_to_reference(self.source_ref)
3546
3548
3547 @declared_attr
3549 @declared_attr
3548 def target_repo(cls):
3550 def target_repo(cls):
3549 return relationship(
3551 return relationship(
3550 'Repository',
3552 'Repository',
3551 primaryjoin='%s.target_repo_id==Repository.repo_id' % cls.__name__)
3553 primaryjoin='%s.target_repo_id==Repository.repo_id' % cls.__name__)
3552
3554
3553 @property
3555 @property
3554 def target_ref_parts(self):
3556 def target_ref_parts(self):
3555 return self.unicode_to_reference(self.target_ref)
3557 return self.unicode_to_reference(self.target_ref)
3556
3558
3557 @property
3559 @property
3558 def shadow_merge_ref(self):
3560 def shadow_merge_ref(self):
3559 return self.unicode_to_reference(self._shadow_merge_ref)
3561 return self.unicode_to_reference(self._shadow_merge_ref)
3560
3562
3561 @shadow_merge_ref.setter
3563 @shadow_merge_ref.setter
3562 def shadow_merge_ref(self, ref):
3564 def shadow_merge_ref(self, ref):
3563 self._shadow_merge_ref = self.reference_to_unicode(ref)
3565 self._shadow_merge_ref = self.reference_to_unicode(ref)
3564
3566
3565 def unicode_to_reference(self, raw):
3567 def unicode_to_reference(self, raw):
3566 """
3568 """
3567 Convert a unicode (or string) to a reference object.
3569 Convert a unicode (or string) to a reference object.
3568 If unicode evaluates to False it returns None.
3570 If unicode evaluates to False it returns None.
3569 """
3571 """
3570 if raw:
3572 if raw:
3571 refs = raw.split(':')
3573 refs = raw.split(':')
3572 return Reference(*refs)
3574 return Reference(*refs)
3573 else:
3575 else:
3574 return None
3576 return None
3575
3577
3576 def reference_to_unicode(self, ref):
3578 def reference_to_unicode(self, ref):
3577 """
3579 """
3578 Convert a reference object to unicode.
3580 Convert a reference object to unicode.
3579 If reference is None it returns None.
3581 If reference is None it returns None.
3580 """
3582 """
3581 if ref:
3583 if ref:
3582 return u':'.join(ref)
3584 return u':'.join(ref)
3583 else:
3585 else:
3584 return None
3586 return None
3585
3587
3586 def get_api_data(self, with_merge_state=True):
3588 def get_api_data(self, with_merge_state=True):
3587 from rhodecode.model.pull_request import PullRequestModel
3589 from rhodecode.model.pull_request import PullRequestModel
3588
3590
3589 pull_request = self
3591 pull_request = self
3590 if with_merge_state:
3592 if with_merge_state:
3591 merge_status = PullRequestModel().merge_status(pull_request)
3593 merge_status = PullRequestModel().merge_status(pull_request)
3592 merge_state = {
3594 merge_state = {
3593 'status': merge_status[0],
3595 'status': merge_status[0],
3594 'message': safe_unicode(merge_status[1]),
3596 'message': safe_unicode(merge_status[1]),
3595 }
3597 }
3596 else:
3598 else:
3597 merge_state = {'status': 'not_available',
3599 merge_state = {'status': 'not_available',
3598 'message': 'not_available'}
3600 'message': 'not_available'}
3599
3601
3600 merge_data = {
3602 merge_data = {
3601 'clone_url': PullRequestModel().get_shadow_clone_url(pull_request),
3603 'clone_url': PullRequestModel().get_shadow_clone_url(pull_request),
3602 'reference': (
3604 'reference': (
3603 pull_request.shadow_merge_ref._asdict()
3605 pull_request.shadow_merge_ref._asdict()
3604 if pull_request.shadow_merge_ref else None),
3606 if pull_request.shadow_merge_ref else None),
3605 }
3607 }
3606
3608
3607 data = {
3609 data = {
3608 'pull_request_id': pull_request.pull_request_id,
3610 'pull_request_id': pull_request.pull_request_id,
3609 'url': PullRequestModel().get_url(pull_request),
3611 'url': PullRequestModel().get_url(pull_request),
3610 'title': pull_request.title,
3612 'title': pull_request.title,
3611 'description': pull_request.description,
3613 'description': pull_request.description,
3612 'status': pull_request.status,
3614 'status': pull_request.status,
3613 'created_on': pull_request.created_on,
3615 'created_on': pull_request.created_on,
3614 'updated_on': pull_request.updated_on,
3616 'updated_on': pull_request.updated_on,
3615 'commit_ids': pull_request.revisions,
3617 'commit_ids': pull_request.revisions,
3616 'review_status': pull_request.calculated_review_status(),
3618 'review_status': pull_request.calculated_review_status(),
3617 'mergeable': merge_state,
3619 'mergeable': merge_state,
3618 'source': {
3620 'source': {
3619 'clone_url': pull_request.source_repo.clone_url(),
3621 'clone_url': pull_request.source_repo.clone_url(),
3620 'repository': pull_request.source_repo.repo_name,
3622 'repository': pull_request.source_repo.repo_name,
3621 'reference': {
3623 'reference': {
3622 'name': pull_request.source_ref_parts.name,
3624 'name': pull_request.source_ref_parts.name,
3623 'type': pull_request.source_ref_parts.type,
3625 'type': pull_request.source_ref_parts.type,
3624 'commit_id': pull_request.source_ref_parts.commit_id,
3626 'commit_id': pull_request.source_ref_parts.commit_id,
3625 },
3627 },
3626 },
3628 },
3627 'target': {
3629 'target': {
3628 'clone_url': pull_request.target_repo.clone_url(),
3630 'clone_url': pull_request.target_repo.clone_url(),
3629 'repository': pull_request.target_repo.repo_name,
3631 'repository': pull_request.target_repo.repo_name,
3630 'reference': {
3632 'reference': {
3631 'name': pull_request.target_ref_parts.name,
3633 'name': pull_request.target_ref_parts.name,
3632 'type': pull_request.target_ref_parts.type,
3634 'type': pull_request.target_ref_parts.type,
3633 'commit_id': pull_request.target_ref_parts.commit_id,
3635 'commit_id': pull_request.target_ref_parts.commit_id,
3634 },
3636 },
3635 },
3637 },
3636 'merge': merge_data,
3638 'merge': merge_data,
3637 'author': pull_request.author.get_api_data(include_secrets=False,
3639 'author': pull_request.author.get_api_data(include_secrets=False,
3638 details='basic'),
3640 details='basic'),
3639 'reviewers': [
3641 'reviewers': [
3640 {
3642 {
3641 'user': reviewer.get_api_data(include_secrets=False,
3643 'user': reviewer.get_api_data(include_secrets=False,
3642 details='basic'),
3644 details='basic'),
3643 'reasons': reasons,
3645 'reasons': reasons,
3644 'review_status': st[0][1].status if st else 'not_reviewed',
3646 'review_status': st[0][1].status if st else 'not_reviewed',
3645 }
3647 }
3646 for obj, reviewer, reasons, mandatory, st in
3648 for obj, reviewer, reasons, mandatory, st in
3647 pull_request.reviewers_statuses()
3649 pull_request.reviewers_statuses()
3648 ]
3650 ]
3649 }
3651 }
3650
3652
3651 return data
3653 return data
3652
3654
3653
3655
3654 class PullRequest(Base, _PullRequestBase):
3656 class PullRequest(Base, _PullRequestBase):
3655 __tablename__ = 'pull_requests'
3657 __tablename__ = 'pull_requests'
3656 __table_args__ = (
3658 __table_args__ = (
3657 base_table_args,
3659 base_table_args,
3658 )
3660 )
3659
3661
3660 pull_request_id = Column(
3662 pull_request_id = Column(
3661 'pull_request_id', Integer(), nullable=False, primary_key=True)
3663 'pull_request_id', Integer(), nullable=False, primary_key=True)
3662
3664
3663 def __repr__(self):
3665 def __repr__(self):
3664 if self.pull_request_id:
3666 if self.pull_request_id:
3665 return '<DB:PullRequest #%s>' % self.pull_request_id
3667 return '<DB:PullRequest #%s>' % self.pull_request_id
3666 else:
3668 else:
3667 return '<DB:PullRequest at %#x>' % id(self)
3669 return '<DB:PullRequest at %#x>' % id(self)
3668
3670
3669 reviewers = relationship('PullRequestReviewers',
3671 reviewers = relationship('PullRequestReviewers',
3670 cascade="all, delete, delete-orphan")
3672 cascade="all, delete, delete-orphan")
3671 statuses = relationship('ChangesetStatus',
3673 statuses = relationship('ChangesetStatus',
3672 cascade="all, delete, delete-orphan")
3674 cascade="all, delete, delete-orphan")
3673 comments = relationship('ChangesetComment',
3675 comments = relationship('ChangesetComment',
3674 cascade="all, delete, delete-orphan")
3676 cascade="all, delete, delete-orphan")
3675 versions = relationship('PullRequestVersion',
3677 versions = relationship('PullRequestVersion',
3676 cascade="all, delete, delete-orphan",
3678 cascade="all, delete, delete-orphan",
3677 lazy='dynamic')
3679 lazy='dynamic')
3678
3680
3679 @classmethod
3681 @classmethod
3680 def get_pr_display_object(cls, pull_request_obj, org_pull_request_obj,
3682 def get_pr_display_object(cls, pull_request_obj, org_pull_request_obj,
3681 internal_methods=None):
3683 internal_methods=None):
3682
3684
3683 class PullRequestDisplay(object):
3685 class PullRequestDisplay(object):
3684 """
3686 """
3685 Special object wrapper for showing PullRequest data via Versions
3687 Special object wrapper for showing PullRequest data via Versions
3686 It mimics PR object as close as possible. This is read only object
3688 It mimics PR object as close as possible. This is read only object
3687 just for display
3689 just for display
3688 """
3690 """
3689
3691
3690 def __init__(self, attrs, internal=None):
3692 def __init__(self, attrs, internal=None):
3691 self.attrs = attrs
3693 self.attrs = attrs
3692 # internal have priority over the given ones via attrs
3694 # internal have priority over the given ones via attrs
3693 self.internal = internal or ['versions']
3695 self.internal = internal or ['versions']
3694
3696
3695 def __getattr__(self, item):
3697 def __getattr__(self, item):
3696 if item in self.internal:
3698 if item in self.internal:
3697 return getattr(self, item)
3699 return getattr(self, item)
3698 try:
3700 try:
3699 return self.attrs[item]
3701 return self.attrs[item]
3700 except KeyError:
3702 except KeyError:
3701 raise AttributeError(
3703 raise AttributeError(
3702 '%s object has no attribute %s' % (self, item))
3704 '%s object has no attribute %s' % (self, item))
3703
3705
3704 def __repr__(self):
3706 def __repr__(self):
3705 return '<DB:PullRequestDisplay #%s>' % self.attrs.get('pull_request_id')
3707 return '<DB:PullRequestDisplay #%s>' % self.attrs.get('pull_request_id')
3706
3708
3707 def versions(self):
3709 def versions(self):
3708 return pull_request_obj.versions.order_by(
3710 return pull_request_obj.versions.order_by(
3709 PullRequestVersion.pull_request_version_id).all()
3711 PullRequestVersion.pull_request_version_id).all()
3710
3712
3711 def is_closed(self):
3713 def is_closed(self):
3712 return pull_request_obj.is_closed()
3714 return pull_request_obj.is_closed()
3713
3715
3714 @property
3716 @property
3715 def pull_request_version_id(self):
3717 def pull_request_version_id(self):
3716 return getattr(pull_request_obj, 'pull_request_version_id', None)
3718 return getattr(pull_request_obj, 'pull_request_version_id', None)
3717
3719
3718 attrs = StrictAttributeDict(pull_request_obj.get_api_data())
3720 attrs = StrictAttributeDict(pull_request_obj.get_api_data())
3719
3721
3720 attrs.author = StrictAttributeDict(
3722 attrs.author = StrictAttributeDict(
3721 pull_request_obj.author.get_api_data())
3723 pull_request_obj.author.get_api_data())
3722 if pull_request_obj.target_repo:
3724 if pull_request_obj.target_repo:
3723 attrs.target_repo = StrictAttributeDict(
3725 attrs.target_repo = StrictAttributeDict(
3724 pull_request_obj.target_repo.get_api_data())
3726 pull_request_obj.target_repo.get_api_data())
3725 attrs.target_repo.clone_url = pull_request_obj.target_repo.clone_url
3727 attrs.target_repo.clone_url = pull_request_obj.target_repo.clone_url
3726
3728
3727 if pull_request_obj.source_repo:
3729 if pull_request_obj.source_repo:
3728 attrs.source_repo = StrictAttributeDict(
3730 attrs.source_repo = StrictAttributeDict(
3729 pull_request_obj.source_repo.get_api_data())
3731 pull_request_obj.source_repo.get_api_data())
3730 attrs.source_repo.clone_url = pull_request_obj.source_repo.clone_url
3732 attrs.source_repo.clone_url = pull_request_obj.source_repo.clone_url
3731
3733
3732 attrs.source_ref_parts = pull_request_obj.source_ref_parts
3734 attrs.source_ref_parts = pull_request_obj.source_ref_parts
3733 attrs.target_ref_parts = pull_request_obj.target_ref_parts
3735 attrs.target_ref_parts = pull_request_obj.target_ref_parts
3734 attrs.revisions = pull_request_obj.revisions
3736 attrs.revisions = pull_request_obj.revisions
3735
3737
3736 attrs.shadow_merge_ref = org_pull_request_obj.shadow_merge_ref
3738 attrs.shadow_merge_ref = org_pull_request_obj.shadow_merge_ref
3737 attrs.reviewer_data = org_pull_request_obj.reviewer_data
3739 attrs.reviewer_data = org_pull_request_obj.reviewer_data
3738 attrs.reviewer_data_json = org_pull_request_obj.reviewer_data_json
3740 attrs.reviewer_data_json = org_pull_request_obj.reviewer_data_json
3739
3741
3740 return PullRequestDisplay(attrs, internal=internal_methods)
3742 return PullRequestDisplay(attrs, internal=internal_methods)
3741
3743
3742 def is_closed(self):
3744 def is_closed(self):
3743 return self.status == self.STATUS_CLOSED
3745 return self.status == self.STATUS_CLOSED
3744
3746
3745 def __json__(self):
3747 def __json__(self):
3746 return {
3748 return {
3747 'revisions': self.revisions,
3749 'revisions': self.revisions,
3748 }
3750 }
3749
3751
3750 def calculated_review_status(self):
3752 def calculated_review_status(self):
3751 from rhodecode.model.changeset_status import ChangesetStatusModel
3753 from rhodecode.model.changeset_status import ChangesetStatusModel
3752 return ChangesetStatusModel().calculated_review_status(self)
3754 return ChangesetStatusModel().calculated_review_status(self)
3753
3755
3754 def reviewers_statuses(self):
3756 def reviewers_statuses(self):
3755 from rhodecode.model.changeset_status import ChangesetStatusModel
3757 from rhodecode.model.changeset_status import ChangesetStatusModel
3756 return ChangesetStatusModel().reviewers_statuses(self)
3758 return ChangesetStatusModel().reviewers_statuses(self)
3757
3759
3758 @property
3760 @property
3759 def workspace_id(self):
3761 def workspace_id(self):
3760 from rhodecode.model.pull_request import PullRequestModel
3762 from rhodecode.model.pull_request import PullRequestModel
3761 return PullRequestModel()._workspace_id(self)
3763 return PullRequestModel()._workspace_id(self)
3762
3764
3763 def get_shadow_repo(self):
3765 def get_shadow_repo(self):
3764 workspace_id = self.workspace_id
3766 workspace_id = self.workspace_id
3765 vcs_obj = self.target_repo.scm_instance()
3767 vcs_obj = self.target_repo.scm_instance()
3766 shadow_repository_path = vcs_obj._get_shadow_repository_path(
3768 shadow_repository_path = vcs_obj._get_shadow_repository_path(
3767 self.target_repo.repo_id, workspace_id)
3769 self.target_repo.repo_id, workspace_id)
3768 if os.path.isdir(shadow_repository_path):
3770 if os.path.isdir(shadow_repository_path):
3769 return vcs_obj._get_shadow_instance(shadow_repository_path)
3771 return vcs_obj._get_shadow_instance(shadow_repository_path)
3770
3772
3771
3773
3772 class PullRequestVersion(Base, _PullRequestBase):
3774 class PullRequestVersion(Base, _PullRequestBase):
3773 __tablename__ = 'pull_request_versions'
3775 __tablename__ = 'pull_request_versions'
3774 __table_args__ = (
3776 __table_args__ = (
3775 base_table_args,
3777 base_table_args,
3776 )
3778 )
3777
3779
3778 pull_request_version_id = Column(
3780 pull_request_version_id = Column(
3779 'pull_request_version_id', Integer(), nullable=False, primary_key=True)
3781 'pull_request_version_id', Integer(), nullable=False, primary_key=True)
3780 pull_request_id = Column(
3782 pull_request_id = Column(
3781 'pull_request_id', Integer(),
3783 'pull_request_id', Integer(),
3782 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3784 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3783 pull_request = relationship('PullRequest')
3785 pull_request = relationship('PullRequest')
3784
3786
3785 def __repr__(self):
3787 def __repr__(self):
3786 if self.pull_request_version_id:
3788 if self.pull_request_version_id:
3787 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
3789 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
3788 else:
3790 else:
3789 return '<DB:PullRequestVersion at %#x>' % id(self)
3791 return '<DB:PullRequestVersion at %#x>' % id(self)
3790
3792
3791 @property
3793 @property
3792 def reviewers(self):
3794 def reviewers(self):
3793 return self.pull_request.reviewers
3795 return self.pull_request.reviewers
3794
3796
3795 @property
3797 @property
3796 def versions(self):
3798 def versions(self):
3797 return self.pull_request.versions
3799 return self.pull_request.versions
3798
3800
3799 def is_closed(self):
3801 def is_closed(self):
3800 # calculate from original
3802 # calculate from original
3801 return self.pull_request.status == self.STATUS_CLOSED
3803 return self.pull_request.status == self.STATUS_CLOSED
3802
3804
3803 def calculated_review_status(self):
3805 def calculated_review_status(self):
3804 return self.pull_request.calculated_review_status()
3806 return self.pull_request.calculated_review_status()
3805
3807
3806 def reviewers_statuses(self):
3808 def reviewers_statuses(self):
3807 return self.pull_request.reviewers_statuses()
3809 return self.pull_request.reviewers_statuses()
3808
3810
3809
3811
3810 class PullRequestReviewers(Base, BaseModel):
3812 class PullRequestReviewers(Base, BaseModel):
3811 __tablename__ = 'pull_request_reviewers'
3813 __tablename__ = 'pull_request_reviewers'
3812 __table_args__ = (
3814 __table_args__ = (
3813 base_table_args,
3815 base_table_args,
3814 )
3816 )
3815
3817
3816 @hybrid_property
3818 @hybrid_property
3817 def reasons(self):
3819 def reasons(self):
3818 if not self._reasons:
3820 if not self._reasons:
3819 return []
3821 return []
3820 return self._reasons
3822 return self._reasons
3821
3823
3822 @reasons.setter
3824 @reasons.setter
3823 def reasons(self, val):
3825 def reasons(self, val):
3824 val = val or []
3826 val = val or []
3825 if any(not isinstance(x, basestring) for x in val):
3827 if any(not isinstance(x, basestring) for x in val):
3826 raise Exception('invalid reasons type, must be list of strings')
3828 raise Exception('invalid reasons type, must be list of strings')
3827 self._reasons = val
3829 self._reasons = val
3828
3830
3829 pull_requests_reviewers_id = Column(
3831 pull_requests_reviewers_id = Column(
3830 'pull_requests_reviewers_id', Integer(), nullable=False,
3832 'pull_requests_reviewers_id', Integer(), nullable=False,
3831 primary_key=True)
3833 primary_key=True)
3832 pull_request_id = Column(
3834 pull_request_id = Column(
3833 "pull_request_id", Integer(),
3835 "pull_request_id", Integer(),
3834 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3836 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3835 user_id = Column(
3837 user_id = Column(
3836 "user_id", Integer(), ForeignKey('users.user_id'), nullable=True)
3838 "user_id", Integer(), ForeignKey('users.user_id'), nullable=True)
3837 _reasons = Column(
3839 _reasons = Column(
3838 'reason', MutationList.as_mutable(
3840 'reason', MutationList.as_mutable(
3839 JsonType('list', dialect_map=dict(mysql=UnicodeText(16384)))))
3841 JsonType('list', dialect_map=dict(mysql=UnicodeText(16384)))))
3840
3842
3841 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
3843 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
3842 user = relationship('User')
3844 user = relationship('User')
3843 pull_request = relationship('PullRequest')
3845 pull_request = relationship('PullRequest')
3844
3846
3845 rule_data = Column(
3847 rule_data = Column(
3846 'rule_data_json',
3848 'rule_data_json',
3847 JsonType(dialect_map=dict(mysql=UnicodeText(16384))))
3849 JsonType(dialect_map=dict(mysql=UnicodeText(16384))))
3848
3850
3849 def rule_user_group_data(self):
3851 def rule_user_group_data(self):
3850 """
3852 """
3851 Returns the voting user group rule data for this reviewer
3853 Returns the voting user group rule data for this reviewer
3852 """
3854 """
3853
3855
3854 if self.rule_data and 'vote_rule' in self.rule_data:
3856 if self.rule_data and 'vote_rule' in self.rule_data:
3855 user_group_data = {}
3857 user_group_data = {}
3856 if 'rule_user_group_entry_id' in self.rule_data:
3858 if 'rule_user_group_entry_id' in self.rule_data:
3857 # means a group with voting rules !
3859 # means a group with voting rules !
3858 user_group_data['id'] = self.rule_data['rule_user_group_entry_id']
3860 user_group_data['id'] = self.rule_data['rule_user_group_entry_id']
3859 user_group_data['name'] = self.rule_data['rule_name']
3861 user_group_data['name'] = self.rule_data['rule_name']
3860 user_group_data['vote_rule'] = self.rule_data['vote_rule']
3862 user_group_data['vote_rule'] = self.rule_data['vote_rule']
3861
3863
3862 return user_group_data
3864 return user_group_data
3863
3865
3864 def __unicode__(self):
3866 def __unicode__(self):
3865 return u"<%s('id:%s')>" % (self.__class__.__name__,
3867 return u"<%s('id:%s')>" % (self.__class__.__name__,
3866 self.pull_requests_reviewers_id)
3868 self.pull_requests_reviewers_id)
3867
3869
3868
3870
3869 class Notification(Base, BaseModel):
3871 class Notification(Base, BaseModel):
3870 __tablename__ = 'notifications'
3872 __tablename__ = 'notifications'
3871 __table_args__ = (
3873 __table_args__ = (
3872 Index('notification_type_idx', 'type'),
3874 Index('notification_type_idx', 'type'),
3873 base_table_args,
3875 base_table_args,
3874 )
3876 )
3875
3877
3876 TYPE_CHANGESET_COMMENT = u'cs_comment'
3878 TYPE_CHANGESET_COMMENT = u'cs_comment'
3877 TYPE_MESSAGE = u'message'
3879 TYPE_MESSAGE = u'message'
3878 TYPE_MENTION = u'mention'
3880 TYPE_MENTION = u'mention'
3879 TYPE_REGISTRATION = u'registration'
3881 TYPE_REGISTRATION = u'registration'
3880 TYPE_PULL_REQUEST = u'pull_request'
3882 TYPE_PULL_REQUEST = u'pull_request'
3881 TYPE_PULL_REQUEST_COMMENT = u'pull_request_comment'
3883 TYPE_PULL_REQUEST_COMMENT = u'pull_request_comment'
3882
3884
3883 notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
3885 notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
3884 subject = Column('subject', Unicode(512), nullable=True)
3886 subject = Column('subject', Unicode(512), nullable=True)
3885 body = Column('body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
3887 body = Column('body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
3886 created_by = Column("created_by", Integer(), ForeignKey('users.user_id'), nullable=True)
3888 created_by = Column("created_by", Integer(), ForeignKey('users.user_id'), nullable=True)
3887 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3889 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3888 type_ = Column('type', Unicode(255))
3890 type_ = Column('type', Unicode(255))
3889
3891
3890 created_by_user = relationship('User')
3892 created_by_user = relationship('User')
3891 notifications_to_users = relationship('UserNotification', lazy='joined',
3893 notifications_to_users = relationship('UserNotification', lazy='joined',
3892 cascade="all, delete, delete-orphan")
3894 cascade="all, delete, delete-orphan")
3893
3895
3894 @property
3896 @property
3895 def recipients(self):
3897 def recipients(self):
3896 return [x.user for x in UserNotification.query()\
3898 return [x.user for x in UserNotification.query()\
3897 .filter(UserNotification.notification == self)\
3899 .filter(UserNotification.notification == self)\
3898 .order_by(UserNotification.user_id.asc()).all()]
3900 .order_by(UserNotification.user_id.asc()).all()]
3899
3901
3900 @classmethod
3902 @classmethod
3901 def create(cls, created_by, subject, body, recipients, type_=None):
3903 def create(cls, created_by, subject, body, recipients, type_=None):
3902 if type_ is None:
3904 if type_ is None:
3903 type_ = Notification.TYPE_MESSAGE
3905 type_ = Notification.TYPE_MESSAGE
3904
3906
3905 notification = cls()
3907 notification = cls()
3906 notification.created_by_user = created_by
3908 notification.created_by_user = created_by
3907 notification.subject = subject
3909 notification.subject = subject
3908 notification.body = body
3910 notification.body = body
3909 notification.type_ = type_
3911 notification.type_ = type_
3910 notification.created_on = datetime.datetime.now()
3912 notification.created_on = datetime.datetime.now()
3911
3913
3912 # For each recipient link the created notification to his account
3914 # For each recipient link the created notification to his account
3913 for u in recipients:
3915 for u in recipients:
3914 assoc = UserNotification()
3916 assoc = UserNotification()
3915 assoc.user_id = u.user_id
3917 assoc.user_id = u.user_id
3916 assoc.notification = notification
3918 assoc.notification = notification
3917
3919
3918 # if created_by is inside recipients mark his notification
3920 # if created_by is inside recipients mark his notification
3919 # as read
3921 # as read
3920 if u.user_id == created_by.user_id:
3922 if u.user_id == created_by.user_id:
3921 assoc.read = True
3923 assoc.read = True
3922 Session().add(assoc)
3924 Session().add(assoc)
3923
3925
3924 Session().add(notification)
3926 Session().add(notification)
3925
3927
3926 return notification
3928 return notification
3927
3929
3928
3930
3929 class UserNotification(Base, BaseModel):
3931 class UserNotification(Base, BaseModel):
3930 __tablename__ = 'user_to_notification'
3932 __tablename__ = 'user_to_notification'
3931 __table_args__ = (
3933 __table_args__ = (
3932 UniqueConstraint('user_id', 'notification_id'),
3934 UniqueConstraint('user_id', 'notification_id'),
3933 base_table_args
3935 base_table_args
3934 )
3936 )
3935
3937
3936 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
3938 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
3937 notification_id = Column("notification_id", Integer(), ForeignKey('notifications.notification_id'), primary_key=True)
3939 notification_id = Column("notification_id", Integer(), ForeignKey('notifications.notification_id'), primary_key=True)
3938 read = Column('read', Boolean, default=False)
3940 read = Column('read', Boolean, default=False)
3939 sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
3941 sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
3940
3942
3941 user = relationship('User', lazy="joined")
3943 user = relationship('User', lazy="joined")
3942 notification = relationship('Notification', lazy="joined",
3944 notification = relationship('Notification', lazy="joined",
3943 order_by=lambda: Notification.created_on.desc(),)
3945 order_by=lambda: Notification.created_on.desc(),)
3944
3946
3945 def mark_as_read(self):
3947 def mark_as_read(self):
3946 self.read = True
3948 self.read = True
3947 Session().add(self)
3949 Session().add(self)
3948
3950
3949
3951
3950 class Gist(Base, BaseModel):
3952 class Gist(Base, BaseModel):
3951 __tablename__ = 'gists'
3953 __tablename__ = 'gists'
3952 __table_args__ = (
3954 __table_args__ = (
3953 Index('g_gist_access_id_idx', 'gist_access_id'),
3955 Index('g_gist_access_id_idx', 'gist_access_id'),
3954 Index('g_created_on_idx', 'created_on'),
3956 Index('g_created_on_idx', 'created_on'),
3955 base_table_args
3957 base_table_args
3956 )
3958 )
3957
3959
3958 GIST_PUBLIC = u'public'
3960 GIST_PUBLIC = u'public'
3959 GIST_PRIVATE = u'private'
3961 GIST_PRIVATE = u'private'
3960 DEFAULT_FILENAME = u'gistfile1.txt'
3962 DEFAULT_FILENAME = u'gistfile1.txt'
3961
3963
3962 ACL_LEVEL_PUBLIC = u'acl_public'
3964 ACL_LEVEL_PUBLIC = u'acl_public'
3963 ACL_LEVEL_PRIVATE = u'acl_private'
3965 ACL_LEVEL_PRIVATE = u'acl_private'
3964
3966
3965 gist_id = Column('gist_id', Integer(), primary_key=True)
3967 gist_id = Column('gist_id', Integer(), primary_key=True)
3966 gist_access_id = Column('gist_access_id', Unicode(250))
3968 gist_access_id = Column('gist_access_id', Unicode(250))
3967 gist_description = Column('gist_description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
3969 gist_description = Column('gist_description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
3968 gist_owner = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True)
3970 gist_owner = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True)
3969 gist_expires = Column('gist_expires', Float(53), nullable=False)
3971 gist_expires = Column('gist_expires', Float(53), nullable=False)
3970 gist_type = Column('gist_type', Unicode(128), nullable=False)
3972 gist_type = Column('gist_type', Unicode(128), nullable=False)
3971 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3973 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3972 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3974 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3973 acl_level = Column('acl_level', Unicode(128), nullable=True)
3975 acl_level = Column('acl_level', Unicode(128), nullable=True)
3974
3976
3975 owner = relationship('User')
3977 owner = relationship('User')
3976
3978
3977 def __repr__(self):
3979 def __repr__(self):
3978 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
3980 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
3979
3981
3980 @hybrid_property
3982 @hybrid_property
3981 def description_safe(self):
3983 def description_safe(self):
3982 from rhodecode.lib import helpers as h
3984 from rhodecode.lib import helpers as h
3983 return h.escape(self.gist_description)
3985 return h.escape(self.gist_description)
3984
3986
3985 @classmethod
3987 @classmethod
3986 def get_or_404(cls, id_):
3988 def get_or_404(cls, id_):
3987 from pyramid.httpexceptions import HTTPNotFound
3989 from pyramid.httpexceptions import HTTPNotFound
3988
3990
3989 res = cls.query().filter(cls.gist_access_id == id_).scalar()
3991 res = cls.query().filter(cls.gist_access_id == id_).scalar()
3990 if not res:
3992 if not res:
3991 raise HTTPNotFound()
3993 raise HTTPNotFound()
3992 return res
3994 return res
3993
3995
3994 @classmethod
3996 @classmethod
3995 def get_by_access_id(cls, gist_access_id):
3997 def get_by_access_id(cls, gist_access_id):
3996 return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
3998 return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
3997
3999
3998 def gist_url(self):
4000 def gist_url(self):
3999 from rhodecode.model.gist import GistModel
4001 from rhodecode.model.gist import GistModel
4000 return GistModel().get_url(self)
4002 return GistModel().get_url(self)
4001
4003
4002 @classmethod
4004 @classmethod
4003 def base_path(cls):
4005 def base_path(cls):
4004 """
4006 """
4005 Returns base path when all gists are stored
4007 Returns base path when all gists are stored
4006
4008
4007 :param cls:
4009 :param cls:
4008 """
4010 """
4009 from rhodecode.model.gist import GIST_STORE_LOC
4011 from rhodecode.model.gist import GIST_STORE_LOC
4010 q = Session().query(RhodeCodeUi)\
4012 q = Session().query(RhodeCodeUi)\
4011 .filter(RhodeCodeUi.ui_key == URL_SEP)
4013 .filter(RhodeCodeUi.ui_key == URL_SEP)
4012 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
4014 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
4013 return os.path.join(q.one().ui_value, GIST_STORE_LOC)
4015 return os.path.join(q.one().ui_value, GIST_STORE_LOC)
4014
4016
4015 def get_api_data(self):
4017 def get_api_data(self):
4016 """
4018 """
4017 Common function for generating gist related data for API
4019 Common function for generating gist related data for API
4018 """
4020 """
4019 gist = self
4021 gist = self
4020 data = {
4022 data = {
4021 'gist_id': gist.gist_id,
4023 'gist_id': gist.gist_id,
4022 'type': gist.gist_type,
4024 'type': gist.gist_type,
4023 'access_id': gist.gist_access_id,
4025 'access_id': gist.gist_access_id,
4024 'description': gist.gist_description,
4026 'description': gist.gist_description,
4025 'url': gist.gist_url(),
4027 'url': gist.gist_url(),
4026 'expires': gist.gist_expires,
4028 'expires': gist.gist_expires,
4027 'created_on': gist.created_on,
4029 'created_on': gist.created_on,
4028 'modified_at': gist.modified_at,
4030 'modified_at': gist.modified_at,
4029 'content': None,
4031 'content': None,
4030 'acl_level': gist.acl_level,
4032 'acl_level': gist.acl_level,
4031 }
4033 }
4032 return data
4034 return data
4033
4035
4034 def __json__(self):
4036 def __json__(self):
4035 data = dict(
4037 data = dict(
4036 )
4038 )
4037 data.update(self.get_api_data())
4039 data.update(self.get_api_data())
4038 return data
4040 return data
4039 # SCM functions
4041 # SCM functions
4040
4042
4041 def scm_instance(self, **kwargs):
4043 def scm_instance(self, **kwargs):
4042 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
4044 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
4043 return get_vcs_instance(
4045 return get_vcs_instance(
4044 repo_path=safe_str(full_repo_path), create=False)
4046 repo_path=safe_str(full_repo_path), create=False)
4045
4047
4046
4048
4047 class ExternalIdentity(Base, BaseModel):
4049 class ExternalIdentity(Base, BaseModel):
4048 __tablename__ = 'external_identities'
4050 __tablename__ = 'external_identities'
4049 __table_args__ = (
4051 __table_args__ = (
4050 Index('local_user_id_idx', 'local_user_id'),
4052 Index('local_user_id_idx', 'local_user_id'),
4051 Index('external_id_idx', 'external_id'),
4053 Index('external_id_idx', 'external_id'),
4052 base_table_args
4054 base_table_args
4053 )
4055 )
4054
4056
4055 external_id = Column('external_id', Unicode(255), default=u'',
4057 external_id = Column('external_id', Unicode(255), default=u'',
4056 primary_key=True)
4058 primary_key=True)
4057 external_username = Column('external_username', Unicode(1024), default=u'')
4059 external_username = Column('external_username', Unicode(1024), default=u'')
4058 local_user_id = Column('local_user_id', Integer(),
4060 local_user_id = Column('local_user_id', Integer(),
4059 ForeignKey('users.user_id'), primary_key=True)
4061 ForeignKey('users.user_id'), primary_key=True)
4060 provider_name = Column('provider_name', Unicode(255), default=u'',
4062 provider_name = Column('provider_name', Unicode(255), default=u'',
4061 primary_key=True)
4063 primary_key=True)
4062 access_token = Column('access_token', String(1024), default=u'')
4064 access_token = Column('access_token', String(1024), default=u'')
4063 alt_token = Column('alt_token', String(1024), default=u'')
4065 alt_token = Column('alt_token', String(1024), default=u'')
4064 token_secret = Column('token_secret', String(1024), default=u'')
4066 token_secret = Column('token_secret', String(1024), default=u'')
4065
4067
4066 @classmethod
4068 @classmethod
4067 def by_external_id_and_provider(cls, external_id, provider_name,
4069 def by_external_id_and_provider(cls, external_id, provider_name,
4068 local_user_id=None):
4070 local_user_id=None):
4069 """
4071 """
4070 Returns ExternalIdentity instance based on search params
4072 Returns ExternalIdentity instance based on search params
4071
4073
4072 :param external_id:
4074 :param external_id:
4073 :param provider_name:
4075 :param provider_name:
4074 :return: ExternalIdentity
4076 :return: ExternalIdentity
4075 """
4077 """
4076 query = cls.query()
4078 query = cls.query()
4077 query = query.filter(cls.external_id == external_id)
4079 query = query.filter(cls.external_id == external_id)
4078 query = query.filter(cls.provider_name == provider_name)
4080 query = query.filter(cls.provider_name == provider_name)
4079 if local_user_id:
4081 if local_user_id:
4080 query = query.filter(cls.local_user_id == local_user_id)
4082 query = query.filter(cls.local_user_id == local_user_id)
4081 return query.first()
4083 return query.first()
4082
4084
4083 @classmethod
4085 @classmethod
4084 def user_by_external_id_and_provider(cls, external_id, provider_name):
4086 def user_by_external_id_and_provider(cls, external_id, provider_name):
4085 """
4087 """
4086 Returns User instance based on search params
4088 Returns User instance based on search params
4087
4089
4088 :param external_id:
4090 :param external_id:
4089 :param provider_name:
4091 :param provider_name:
4090 :return: User
4092 :return: User
4091 """
4093 """
4092 query = User.query()
4094 query = User.query()
4093 query = query.filter(cls.external_id == external_id)
4095 query = query.filter(cls.external_id == external_id)
4094 query = query.filter(cls.provider_name == provider_name)
4096 query = query.filter(cls.provider_name == provider_name)
4095 query = query.filter(User.user_id == cls.local_user_id)
4097 query = query.filter(User.user_id == cls.local_user_id)
4096 return query.first()
4098 return query.first()
4097
4099
4098 @classmethod
4100 @classmethod
4099 def by_local_user_id(cls, local_user_id):
4101 def by_local_user_id(cls, local_user_id):
4100 """
4102 """
4101 Returns all tokens for user
4103 Returns all tokens for user
4102
4104
4103 :param local_user_id:
4105 :param local_user_id:
4104 :return: ExternalIdentity
4106 :return: ExternalIdentity
4105 """
4107 """
4106 query = cls.query()
4108 query = cls.query()
4107 query = query.filter(cls.local_user_id == local_user_id)
4109 query = query.filter(cls.local_user_id == local_user_id)
4108 return query
4110 return query
4109
4111
4110
4112
4111 class Integration(Base, BaseModel):
4113 class Integration(Base, BaseModel):
4112 __tablename__ = 'integrations'
4114 __tablename__ = 'integrations'
4113 __table_args__ = (
4115 __table_args__ = (
4114 base_table_args
4116 base_table_args
4115 )
4117 )
4116
4118
4117 integration_id = Column('integration_id', Integer(), primary_key=True)
4119 integration_id = Column('integration_id', Integer(), primary_key=True)
4118 integration_type = Column('integration_type', String(255))
4120 integration_type = Column('integration_type', String(255))
4119 enabled = Column('enabled', Boolean(), nullable=False)
4121 enabled = Column('enabled', Boolean(), nullable=False)
4120 name = Column('name', String(255), nullable=False)
4122 name = Column('name', String(255), nullable=False)
4121 child_repos_only = Column('child_repos_only', Boolean(), nullable=False,
4123 child_repos_only = Column('child_repos_only', Boolean(), nullable=False,
4122 default=False)
4124 default=False)
4123
4125
4124 settings = Column(
4126 settings = Column(
4125 'settings_json', MutationObj.as_mutable(
4127 'settings_json', MutationObj.as_mutable(
4126 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4128 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4127 repo_id = Column(
4129 repo_id = Column(
4128 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
4130 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
4129 nullable=True, unique=None, default=None)
4131 nullable=True, unique=None, default=None)
4130 repo = relationship('Repository', lazy='joined')
4132 repo = relationship('Repository', lazy='joined')
4131
4133
4132 repo_group_id = Column(
4134 repo_group_id = Column(
4133 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
4135 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
4134 nullable=True, unique=None, default=None)
4136 nullable=True, unique=None, default=None)
4135 repo_group = relationship('RepoGroup', lazy='joined')
4137 repo_group = relationship('RepoGroup', lazy='joined')
4136
4138
4137 @property
4139 @property
4138 def scope(self):
4140 def scope(self):
4139 if self.repo:
4141 if self.repo:
4140 return repr(self.repo)
4142 return repr(self.repo)
4141 if self.repo_group:
4143 if self.repo_group:
4142 if self.child_repos_only:
4144 if self.child_repos_only:
4143 return repr(self.repo_group) + ' (child repos only)'
4145 return repr(self.repo_group) + ' (child repos only)'
4144 else:
4146 else:
4145 return repr(self.repo_group) + ' (recursive)'
4147 return repr(self.repo_group) + ' (recursive)'
4146 if self.child_repos_only:
4148 if self.child_repos_only:
4147 return 'root_repos'
4149 return 'root_repos'
4148 return 'global'
4150 return 'global'
4149
4151
4150 def __repr__(self):
4152 def __repr__(self):
4151 return '<Integration(%r, %r)>' % (self.integration_type, self.scope)
4153 return '<Integration(%r, %r)>' % (self.integration_type, self.scope)
4152
4154
4153
4155
4154 class RepoReviewRuleUser(Base, BaseModel):
4156 class RepoReviewRuleUser(Base, BaseModel):
4155 __tablename__ = 'repo_review_rules_users'
4157 __tablename__ = 'repo_review_rules_users'
4156 __table_args__ = (
4158 __table_args__ = (
4157 base_table_args
4159 base_table_args
4158 )
4160 )
4159
4161
4160 repo_review_rule_user_id = Column('repo_review_rule_user_id', Integer(), primary_key=True)
4162 repo_review_rule_user_id = Column('repo_review_rule_user_id', Integer(), primary_key=True)
4161 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4163 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4162 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False)
4164 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False)
4163 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4165 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4164 user = relationship('User')
4166 user = relationship('User')
4165
4167
4166 def rule_data(self):
4168 def rule_data(self):
4167 return {
4169 return {
4168 'mandatory': self.mandatory
4170 'mandatory': self.mandatory
4169 }
4171 }
4170
4172
4171
4173
4172 class RepoReviewRuleUserGroup(Base, BaseModel):
4174 class RepoReviewRuleUserGroup(Base, BaseModel):
4173 __tablename__ = 'repo_review_rules_users_groups'
4175 __tablename__ = 'repo_review_rules_users_groups'
4174 __table_args__ = (
4176 __table_args__ = (
4175 base_table_args
4177 base_table_args
4176 )
4178 )
4177
4179
4178 VOTE_RULE_ALL = -1
4180 VOTE_RULE_ALL = -1
4179
4181
4180 repo_review_rule_users_group_id = Column('repo_review_rule_users_group_id', Integer(), primary_key=True)
4182 repo_review_rule_users_group_id = Column('repo_review_rule_users_group_id', Integer(), primary_key=True)
4181 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4183 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4182 users_group_id = Column("users_group_id", Integer(),ForeignKey('users_groups.users_group_id'), nullable=False)
4184 users_group_id = Column("users_group_id", Integer(),ForeignKey('users_groups.users_group_id'), nullable=False)
4183 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4185 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4184 vote_rule = Column("vote_rule", Integer(), nullable=True, default=VOTE_RULE_ALL)
4186 vote_rule = Column("vote_rule", Integer(), nullable=True, default=VOTE_RULE_ALL)
4185 users_group = relationship('UserGroup')
4187 users_group = relationship('UserGroup')
4186
4188
4187 def rule_data(self):
4189 def rule_data(self):
4188 return {
4190 return {
4189 'mandatory': self.mandatory,
4191 'mandatory': self.mandatory,
4190 'vote_rule': self.vote_rule
4192 'vote_rule': self.vote_rule
4191 }
4193 }
4192
4194
4193 @property
4195 @property
4194 def vote_rule_label(self):
4196 def vote_rule_label(self):
4195 if not self.vote_rule or self.vote_rule == self.VOTE_RULE_ALL:
4197 if not self.vote_rule or self.vote_rule == self.VOTE_RULE_ALL:
4196 return 'all must vote'
4198 return 'all must vote'
4197 else:
4199 else:
4198 return 'min. vote {}'.format(self.vote_rule)
4200 return 'min. vote {}'.format(self.vote_rule)
4199
4201
4200
4202
4201 class RepoReviewRule(Base, BaseModel):
4203 class RepoReviewRule(Base, BaseModel):
4202 __tablename__ = 'repo_review_rules'
4204 __tablename__ = 'repo_review_rules'
4203 __table_args__ = (
4205 __table_args__ = (
4204 base_table_args
4206 base_table_args
4205 )
4207 )
4206
4208
4207 repo_review_rule_id = Column(
4209 repo_review_rule_id = Column(
4208 'repo_review_rule_id', Integer(), primary_key=True)
4210 'repo_review_rule_id', Integer(), primary_key=True)
4209 repo_id = Column(
4211 repo_id = Column(
4210 "repo_id", Integer(), ForeignKey('repositories.repo_id'))
4212 "repo_id", Integer(), ForeignKey('repositories.repo_id'))
4211 repo = relationship('Repository', backref='review_rules')
4213 repo = relationship('Repository', backref='review_rules')
4212
4214
4213 review_rule_name = Column('review_rule_name', String(255))
4215 review_rule_name = Column('review_rule_name', String(255))
4214 _branch_pattern = Column("branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4216 _branch_pattern = Column("branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4215 _target_branch_pattern = Column("target_branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4217 _target_branch_pattern = Column("target_branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4216 _file_pattern = Column("file_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4218 _file_pattern = Column("file_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4217
4219
4218 use_authors_for_review = Column("use_authors_for_review", Boolean(), nullable=False, default=False)
4220 use_authors_for_review = Column("use_authors_for_review", Boolean(), nullable=False, default=False)
4219 forbid_author_to_review = Column("forbid_author_to_review", Boolean(), nullable=False, default=False)
4221 forbid_author_to_review = Column("forbid_author_to_review", Boolean(), nullable=False, default=False)
4220 forbid_commit_author_to_review = Column("forbid_commit_author_to_review", Boolean(), nullable=False, default=False)
4222 forbid_commit_author_to_review = Column("forbid_commit_author_to_review", Boolean(), nullable=False, default=False)
4221 forbid_adding_reviewers = Column("forbid_adding_reviewers", Boolean(), nullable=False, default=False)
4223 forbid_adding_reviewers = Column("forbid_adding_reviewers", Boolean(), nullable=False, default=False)
4222
4224
4223 rule_users = relationship('RepoReviewRuleUser')
4225 rule_users = relationship('RepoReviewRuleUser')
4224 rule_user_groups = relationship('RepoReviewRuleUserGroup')
4226 rule_user_groups = relationship('RepoReviewRuleUserGroup')
4225
4227
4226 def _validate_pattern(self, value):
4228 def _validate_pattern(self, value):
4227 re.compile('^' + glob2re(value) + '$')
4229 re.compile('^' + glob2re(value) + '$')
4228
4230
4229 @hybrid_property
4231 @hybrid_property
4230 def source_branch_pattern(self):
4232 def source_branch_pattern(self):
4231 return self._branch_pattern or '*'
4233 return self._branch_pattern or '*'
4232
4234
4233 @source_branch_pattern.setter
4235 @source_branch_pattern.setter
4234 def source_branch_pattern(self, value):
4236 def source_branch_pattern(self, value):
4235 self._validate_pattern(value)
4237 self._validate_pattern(value)
4236 self._branch_pattern = value or '*'
4238 self._branch_pattern = value or '*'
4237
4239
4238 @hybrid_property
4240 @hybrid_property
4239 def target_branch_pattern(self):
4241 def target_branch_pattern(self):
4240 return self._target_branch_pattern or '*'
4242 return self._target_branch_pattern or '*'
4241
4243
4242 @target_branch_pattern.setter
4244 @target_branch_pattern.setter
4243 def target_branch_pattern(self, value):
4245 def target_branch_pattern(self, value):
4244 self._validate_pattern(value)
4246 self._validate_pattern(value)
4245 self._target_branch_pattern = value or '*'
4247 self._target_branch_pattern = value or '*'
4246
4248
4247 @hybrid_property
4249 @hybrid_property
4248 def file_pattern(self):
4250 def file_pattern(self):
4249 return self._file_pattern or '*'
4251 return self._file_pattern or '*'
4250
4252
4251 @file_pattern.setter
4253 @file_pattern.setter
4252 def file_pattern(self, value):
4254 def file_pattern(self, value):
4253 self._validate_pattern(value)
4255 self._validate_pattern(value)
4254 self._file_pattern = value or '*'
4256 self._file_pattern = value or '*'
4255
4257
4256 def matches(self, source_branch, target_branch, files_changed):
4258 def matches(self, source_branch, target_branch, files_changed):
4257 """
4259 """
4258 Check if this review rule matches a branch/files in a pull request
4260 Check if this review rule matches a branch/files in a pull request
4259
4261
4260 :param source_branch: source branch name for the commit
4262 :param source_branch: source branch name for the commit
4261 :param target_branch: target branch name for the commit
4263 :param target_branch: target branch name for the commit
4262 :param files_changed: list of file paths changed in the pull request
4264 :param files_changed: list of file paths changed in the pull request
4263 """
4265 """
4264
4266
4265 source_branch = source_branch or ''
4267 source_branch = source_branch or ''
4266 target_branch = target_branch or ''
4268 target_branch = target_branch or ''
4267 files_changed = files_changed or []
4269 files_changed = files_changed or []
4268
4270
4269 branch_matches = True
4271 branch_matches = True
4270 if source_branch or target_branch:
4272 if source_branch or target_branch:
4271 if self.source_branch_pattern == '*':
4273 if self.source_branch_pattern == '*':
4272 source_branch_match = True
4274 source_branch_match = True
4273 else:
4275 else:
4274 if self.source_branch_pattern.startswith('re:'):
4276 if self.source_branch_pattern.startswith('re:'):
4275 source_pattern = self.source_branch_pattern[3:]
4277 source_pattern = self.source_branch_pattern[3:]
4276 else:
4278 else:
4277 source_pattern = '^' + glob2re(self.source_branch_pattern) + '$'
4279 source_pattern = '^' + glob2re(self.source_branch_pattern) + '$'
4278 source_branch_regex = re.compile(source_pattern)
4280 source_branch_regex = re.compile(source_pattern)
4279 source_branch_match = bool(source_branch_regex.search(source_branch))
4281 source_branch_match = bool(source_branch_regex.search(source_branch))
4280 if self.target_branch_pattern == '*':
4282 if self.target_branch_pattern == '*':
4281 target_branch_match = True
4283 target_branch_match = True
4282 else:
4284 else:
4283 if self.target_branch_pattern.startswith('re:'):
4285 if self.target_branch_pattern.startswith('re:'):
4284 target_pattern = self.target_branch_pattern[3:]
4286 target_pattern = self.target_branch_pattern[3:]
4285 else:
4287 else:
4286 target_pattern = '^' + glob2re(self.target_branch_pattern) + '$'
4288 target_pattern = '^' + glob2re(self.target_branch_pattern) + '$'
4287 target_branch_regex = re.compile(target_pattern)
4289 target_branch_regex = re.compile(target_pattern)
4288 target_branch_match = bool(target_branch_regex.search(target_branch))
4290 target_branch_match = bool(target_branch_regex.search(target_branch))
4289
4291
4290 branch_matches = source_branch_match and target_branch_match
4292 branch_matches = source_branch_match and target_branch_match
4291
4293
4292 files_matches = True
4294 files_matches = True
4293 if self.file_pattern != '*':
4295 if self.file_pattern != '*':
4294 files_matches = False
4296 files_matches = False
4295 if self.file_pattern.startswith('re:'):
4297 if self.file_pattern.startswith('re:'):
4296 file_pattern = self.file_pattern[3:]
4298 file_pattern = self.file_pattern[3:]
4297 else:
4299 else:
4298 file_pattern = glob2re(self.file_pattern)
4300 file_pattern = glob2re(self.file_pattern)
4299 file_regex = re.compile(file_pattern)
4301 file_regex = re.compile(file_pattern)
4300 for filename in files_changed:
4302 for filename in files_changed:
4301 if file_regex.search(filename):
4303 if file_regex.search(filename):
4302 files_matches = True
4304 files_matches = True
4303 break
4305 break
4304
4306
4305 return branch_matches and files_matches
4307 return branch_matches and files_matches
4306
4308
4307 @property
4309 @property
4308 def review_users(self):
4310 def review_users(self):
4309 """ Returns the users which this rule applies to """
4311 """ Returns the users which this rule applies to """
4310
4312
4311 users = collections.OrderedDict()
4313 users = collections.OrderedDict()
4312
4314
4313 for rule_user in self.rule_users:
4315 for rule_user in self.rule_users:
4314 if rule_user.user.active:
4316 if rule_user.user.active:
4315 if rule_user.user not in users:
4317 if rule_user.user not in users:
4316 users[rule_user.user.username] = {
4318 users[rule_user.user.username] = {
4317 'user': rule_user.user,
4319 'user': rule_user.user,
4318 'source': 'user',
4320 'source': 'user',
4319 'source_data': {},
4321 'source_data': {},
4320 'data': rule_user.rule_data()
4322 'data': rule_user.rule_data()
4321 }
4323 }
4322
4324
4323 for rule_user_group in self.rule_user_groups:
4325 for rule_user_group in self.rule_user_groups:
4324 source_data = {
4326 source_data = {
4325 'user_group_id': rule_user_group.users_group.users_group_id,
4327 'user_group_id': rule_user_group.users_group.users_group_id,
4326 'name': rule_user_group.users_group.users_group_name,
4328 'name': rule_user_group.users_group.users_group_name,
4327 'members': len(rule_user_group.users_group.members)
4329 'members': len(rule_user_group.users_group.members)
4328 }
4330 }
4329 for member in rule_user_group.users_group.members:
4331 for member in rule_user_group.users_group.members:
4330 if member.user.active:
4332 if member.user.active:
4331 key = member.user.username
4333 key = member.user.username
4332 if key in users:
4334 if key in users:
4333 # skip this member as we have him already
4335 # skip this member as we have him already
4334 # this prevents from override the "first" matched
4336 # this prevents from override the "first" matched
4335 # users with duplicates in multiple groups
4337 # users with duplicates in multiple groups
4336 continue
4338 continue
4337
4339
4338 users[key] = {
4340 users[key] = {
4339 'user': member.user,
4341 'user': member.user,
4340 'source': 'user_group',
4342 'source': 'user_group',
4341 'source_data': source_data,
4343 'source_data': source_data,
4342 'data': rule_user_group.rule_data()
4344 'data': rule_user_group.rule_data()
4343 }
4345 }
4344
4346
4345 return users
4347 return users
4346
4348
4347 def user_group_vote_rule(self):
4349 def user_group_vote_rule(self):
4348 rules = []
4350 rules = []
4349 if self.rule_user_groups:
4351 if self.rule_user_groups:
4350 for user_group in self.rule_user_groups:
4352 for user_group in self.rule_user_groups:
4351 rules.append(user_group)
4353 rules.append(user_group)
4352 return rules
4354 return rules
4353
4355
4354 def __repr__(self):
4356 def __repr__(self):
4355 return '<RepoReviewerRule(id=%r, repo=%r)>' % (
4357 return '<RepoReviewerRule(id=%r, repo=%r)>' % (
4356 self.repo_review_rule_id, self.repo)
4358 self.repo_review_rule_id, self.repo)
4357
4359
4358
4360
4359 class ScheduleEntry(Base, BaseModel):
4361 class ScheduleEntry(Base, BaseModel):
4360 __tablename__ = 'schedule_entries'
4362 __tablename__ = 'schedule_entries'
4361 __table_args__ = (
4363 __table_args__ = (
4362 UniqueConstraint('schedule_name', name='s_schedule_name_idx'),
4364 UniqueConstraint('schedule_name', name='s_schedule_name_idx'),
4363 UniqueConstraint('task_uid', name='s_task_uid_idx'),
4365 UniqueConstraint('task_uid', name='s_task_uid_idx'),
4364 base_table_args,
4366 base_table_args,
4365 )
4367 )
4366
4368
4367 schedule_types = ['crontab', 'timedelta', 'integer']
4369 schedule_types = ['crontab', 'timedelta', 'integer']
4368 schedule_entry_id = Column('schedule_entry_id', Integer(), primary_key=True)
4370 schedule_entry_id = Column('schedule_entry_id', Integer(), primary_key=True)
4369
4371
4370 schedule_name = Column("schedule_name", String(255), nullable=False, unique=None, default=None)
4372 schedule_name = Column("schedule_name", String(255), nullable=False, unique=None, default=None)
4371 schedule_description = Column("schedule_description", String(10000), nullable=True, unique=None, default=None)
4373 schedule_description = Column("schedule_description", String(10000), nullable=True, unique=None, default=None)
4372 schedule_enabled = Column("schedule_enabled", Boolean(), nullable=False, unique=None, default=True)
4374 schedule_enabled = Column("schedule_enabled", Boolean(), nullable=False, unique=None, default=True)
4373
4375
4374 _schedule_type = Column("schedule_type", String(255), nullable=False, unique=None, default=None)
4376 _schedule_type = Column("schedule_type", String(255), nullable=False, unique=None, default=None)
4375 schedule_definition = Column('schedule_definition_json', MutationObj.as_mutable(JsonType(default=lambda: "", dialect_map=dict(mysql=LONGTEXT()))))
4377 schedule_definition = Column('schedule_definition_json', MutationObj.as_mutable(JsonType(default=lambda: "", dialect_map=dict(mysql=LONGTEXT()))))
4376
4378
4377 schedule_last_run = Column('schedule_last_run', DateTime(timezone=False), nullable=True, unique=None, default=None)
4379 schedule_last_run = Column('schedule_last_run', DateTime(timezone=False), nullable=True, unique=None, default=None)
4378 schedule_total_run_count = Column('schedule_total_run_count', Integer(), nullable=True, unique=None, default=0)
4380 schedule_total_run_count = Column('schedule_total_run_count', Integer(), nullable=True, unique=None, default=0)
4379
4381
4380 # task
4382 # task
4381 task_uid = Column("task_uid", String(255), nullable=False, unique=None, default=None)
4383 task_uid = Column("task_uid", String(255), nullable=False, unique=None, default=None)
4382 task_dot_notation = Column("task_dot_notation", String(4096), nullable=False, unique=None, default=None)
4384 task_dot_notation = Column("task_dot_notation", String(4096), nullable=False, unique=None, default=None)
4383 task_args = Column('task_args_json', MutationObj.as_mutable(JsonType(default=list, dialect_map=dict(mysql=LONGTEXT()))))
4385 task_args = Column('task_args_json', MutationObj.as_mutable(JsonType(default=list, dialect_map=dict(mysql=LONGTEXT()))))
4384 task_kwargs = Column('task_kwargs_json', MutationObj.as_mutable(JsonType(default=dict, dialect_map=dict(mysql=LONGTEXT()))))
4386 task_kwargs = Column('task_kwargs_json', MutationObj.as_mutable(JsonType(default=dict, dialect_map=dict(mysql=LONGTEXT()))))
4385
4387
4386 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4388 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4387 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=None)
4389 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=None)
4388
4390
4389 @hybrid_property
4391 @hybrid_property
4390 def schedule_type(self):
4392 def schedule_type(self):
4391 return self._schedule_type
4393 return self._schedule_type
4392
4394
4393 @schedule_type.setter
4395 @schedule_type.setter
4394 def schedule_type(self, val):
4396 def schedule_type(self, val):
4395 if val not in self.schedule_types:
4397 if val not in self.schedule_types:
4396 raise ValueError('Value must be on of `{}` and got `{}`'.format(
4398 raise ValueError('Value must be on of `{}` and got `{}`'.format(
4397 val, self.schedule_type))
4399 val, self.schedule_type))
4398
4400
4399 self._schedule_type = val
4401 self._schedule_type = val
4400
4402
4401 @classmethod
4403 @classmethod
4402 def get_uid(cls, obj):
4404 def get_uid(cls, obj):
4403 args = obj.task_args
4405 args = obj.task_args
4404 kwargs = obj.task_kwargs
4406 kwargs = obj.task_kwargs
4405 if isinstance(args, JsonRaw):
4407 if isinstance(args, JsonRaw):
4406 try:
4408 try:
4407 args = json.loads(args)
4409 args = json.loads(args)
4408 except ValueError:
4410 except ValueError:
4409 args = tuple()
4411 args = tuple()
4410
4412
4411 if isinstance(kwargs, JsonRaw):
4413 if isinstance(kwargs, JsonRaw):
4412 try:
4414 try:
4413 kwargs = json.loads(kwargs)
4415 kwargs = json.loads(kwargs)
4414 except ValueError:
4416 except ValueError:
4415 kwargs = dict()
4417 kwargs = dict()
4416
4418
4417 dot_notation = obj.task_dot_notation
4419 dot_notation = obj.task_dot_notation
4418 val = '.'.join(map(safe_str, [
4420 val = '.'.join(map(safe_str, [
4419 sorted(dot_notation), args, sorted(kwargs.items())]))
4421 sorted(dot_notation), args, sorted(kwargs.items())]))
4420 return hashlib.sha1(val).hexdigest()
4422 return hashlib.sha1(val).hexdigest()
4421
4423
4422 @classmethod
4424 @classmethod
4423 def get_by_schedule_name(cls, schedule_name):
4425 def get_by_schedule_name(cls, schedule_name):
4424 return cls.query().filter(cls.schedule_name == schedule_name).scalar()
4426 return cls.query().filter(cls.schedule_name == schedule_name).scalar()
4425
4427
4426 @classmethod
4428 @classmethod
4427 def get_by_schedule_id(cls, schedule_id):
4429 def get_by_schedule_id(cls, schedule_id):
4428 return cls.query().filter(cls.schedule_entry_id == schedule_id).scalar()
4430 return cls.query().filter(cls.schedule_entry_id == schedule_id).scalar()
4429
4431
4430 @property
4432 @property
4431 def task(self):
4433 def task(self):
4432 return self.task_dot_notation
4434 return self.task_dot_notation
4433
4435
4434 @property
4436 @property
4435 def schedule(self):
4437 def schedule(self):
4436 from rhodecode.lib.celerylib.utils import raw_2_schedule
4438 from rhodecode.lib.celerylib.utils import raw_2_schedule
4437 schedule = raw_2_schedule(self.schedule_definition, self.schedule_type)
4439 schedule = raw_2_schedule(self.schedule_definition, self.schedule_type)
4438 return schedule
4440 return schedule
4439
4441
4440 @property
4442 @property
4441 def args(self):
4443 def args(self):
4442 try:
4444 try:
4443 return list(self.task_args or [])
4445 return list(self.task_args or [])
4444 except ValueError:
4446 except ValueError:
4445 return list()
4447 return list()
4446
4448
4447 @property
4449 @property
4448 def kwargs(self):
4450 def kwargs(self):
4449 try:
4451 try:
4450 return dict(self.task_kwargs or {})
4452 return dict(self.task_kwargs or {})
4451 except ValueError:
4453 except ValueError:
4452 return dict()
4454 return dict()
4453
4455
4454 def _as_raw(self, val):
4456 def _as_raw(self, val):
4455 if hasattr(val, 'de_coerce'):
4457 if hasattr(val, 'de_coerce'):
4456 val = val.de_coerce()
4458 val = val.de_coerce()
4457 if val:
4459 if val:
4458 val = json.dumps(val)
4460 val = json.dumps(val)
4459
4461
4460 return val
4462 return val
4461
4463
4462 @property
4464 @property
4463 def schedule_definition_raw(self):
4465 def schedule_definition_raw(self):
4464 return self._as_raw(self.schedule_definition)
4466 return self._as_raw(self.schedule_definition)
4465
4467
4466 @property
4468 @property
4467 def args_raw(self):
4469 def args_raw(self):
4468 return self._as_raw(self.task_args)
4470 return self._as_raw(self.task_args)
4469
4471
4470 @property
4472 @property
4471 def kwargs_raw(self):
4473 def kwargs_raw(self):
4472 return self._as_raw(self.task_kwargs)
4474 return self._as_raw(self.task_kwargs)
4473
4475
4474 def __repr__(self):
4476 def __repr__(self):
4475 return '<DB:ScheduleEntry({}:{})>'.format(
4477 return '<DB:ScheduleEntry({}:{})>'.format(
4476 self.schedule_entry_id, self.schedule_name)
4478 self.schedule_entry_id, self.schedule_name)
4477
4479
4478
4480
4479 @event.listens_for(ScheduleEntry, 'before_update')
4481 @event.listens_for(ScheduleEntry, 'before_update')
4480 def update_task_uid(mapper, connection, target):
4482 def update_task_uid(mapper, connection, target):
4481 target.task_uid = ScheduleEntry.get_uid(target)
4483 target.task_uid = ScheduleEntry.get_uid(target)
4482
4484
4483
4485
4484 @event.listens_for(ScheduleEntry, 'before_insert')
4486 @event.listens_for(ScheduleEntry, 'before_insert')
4485 def set_task_uid(mapper, connection, target):
4487 def set_task_uid(mapper, connection, target):
4486 target.task_uid = ScheduleEntry.get_uid(target)
4488 target.task_uid = ScheduleEntry.get_uid(target)
4487
4489
4488
4490
4489 class DbMigrateVersion(Base, BaseModel):
4491 class DbMigrateVersion(Base, BaseModel):
4490 __tablename__ = 'db_migrate_version'
4492 __tablename__ = 'db_migrate_version'
4491 __table_args__ = (
4493 __table_args__ = (
4492 base_table_args,
4494 base_table_args,
4493 )
4495 )
4494
4496
4495 repository_id = Column('repository_id', String(250), primary_key=True)
4497 repository_id = Column('repository_id', String(250), primary_key=True)
4496 repository_path = Column('repository_path', Text)
4498 repository_path = Column('repository_path', Text)
4497 version = Column('version', Integer)
4499 version = Column('version', Integer)
4498
4500
4499 @classmethod
4501 @classmethod
4500 def set_version(cls, version):
4502 def set_version(cls, version):
4501 """
4503 """
4502 Helper for forcing a different version, usually for debugging purposes via ishell.
4504 Helper for forcing a different version, usually for debugging purposes via ishell.
4503 """
4505 """
4504 ver = DbMigrateVersion.query().first()
4506 ver = DbMigrateVersion.query().first()
4505 ver.version = version
4507 ver.version = version
4506 Session().commit()
4508 Session().commit()
4507
4509
4508
4510
4509 class DbSession(Base, BaseModel):
4511 class DbSession(Base, BaseModel):
4510 __tablename__ = 'db_session'
4512 __tablename__ = 'db_session'
4511 __table_args__ = (
4513 __table_args__ = (
4512 base_table_args,
4514 base_table_args,
4513 )
4515 )
4514
4516
4515 def __repr__(self):
4517 def __repr__(self):
4516 return '<DB:DbSession({})>'.format(self.id)
4518 return '<DB:DbSession({})>'.format(self.id)
4517
4519
4518 id = Column('id', Integer())
4520 id = Column('id', Integer())
4519 namespace = Column('namespace', String(255), primary_key=True)
4521 namespace = Column('namespace', String(255), primary_key=True)
4520 accessed = Column('accessed', DateTime, nullable=False)
4522 accessed = Column('accessed', DateTime, nullable=False)
4521 created = Column('created', DateTime, nullable=False)
4523 created = Column('created', DateTime, nullable=False)
4522 data = Column('data', PickleType, nullable=False)
4524 data = Column('data', PickleType, nullable=False)
4523
4525
4524
4526
4525 class BeakerCache(Base, BaseModel):
4527 class BeakerCache(Base, BaseModel):
4526 __tablename__ = 'beaker_cache'
4528 __tablename__ = 'beaker_cache'
4527 __table_args__ = (
4529 __table_args__ = (
4528 base_table_args,
4530 base_table_args,
4529 )
4531 )
4530
4532
4531 def __repr__(self):
4533 def __repr__(self):
4532 return '<DB:DbSession({})>'.format(self.id)
4534 return '<DB:DbSession({})>'.format(self.id)
4533
4535
4534 id = Column('id', Integer())
4536 id = Column('id', Integer())
4535 namespace = Column('namespace', String(255), primary_key=True)
4537 namespace = Column('namespace', String(255), primary_key=True)
4536 accessed = Column('accessed', DateTime, nullable=False)
4538 accessed = Column('accessed', DateTime, nullable=False)
4537 created = Column('created', DateTime, nullable=False)
4539 created = Column('created', DateTime, nullable=False)
4538 data = Column('data', PickleType, nullable=False)
4540 data = Column('data', PickleType, nullable=False)
@@ -1,620 +1,621 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2018 RhodeCode GmbH
3 # Copyright (C) 2010-2018 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 this is forms validation classes
22 this is forms validation classes
23 http://formencode.org/module-formencode.validators.html
23 http://formencode.org/module-formencode.validators.html
24 for list off all availible validators
24 for list off all availible validators
25
25
26 we can create our own validators
26 we can create our own validators
27
27
28 The table below outlines the options which can be used in a schema in addition to the validators themselves
28 The table below outlines the options which can be used in a schema in addition to the validators themselves
29 pre_validators [] These validators will be applied before the schema
29 pre_validators [] These validators will be applied before the schema
30 chained_validators [] These validators will be applied after the schema
30 chained_validators [] These validators will be applied after the schema
31 allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present
31 allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present
32 filter_extra_fields False If True, then keys that aren't associated with a validator are removed
32 filter_extra_fields False If True, then keys that aren't associated with a validator are removed
33 if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value.
33 if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value.
34 ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already
34 ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already
35
35
36
36
37 <name> = formencode.validators.<name of validator>
37 <name> = formencode.validators.<name of validator>
38 <name> must equal form name
38 <name> must equal form name
39 list=[1,2,3,4,5]
39 list=[1,2,3,4,5]
40 for SELECT use formencode.All(OneOf(list), Int())
40 for SELECT use formencode.All(OneOf(list), Int())
41
41
42 """
42 """
43
43
44 import deform
44 import deform
45 import logging
45 import logging
46 import formencode
46 import formencode
47
47
48 from pkg_resources import resource_filename
48 from pkg_resources import resource_filename
49 from formencode import All, Pipe
49 from formencode import All, Pipe
50
50
51 from pyramid.threadlocal import get_current_request
51 from pyramid.threadlocal import get_current_request
52
52
53 from rhodecode import BACKENDS
53 from rhodecode import BACKENDS
54 from rhodecode.lib import helpers
54 from rhodecode.lib import helpers
55 from rhodecode.model import validators as v
55 from rhodecode.model import validators as v
56
56
57 log = logging.getLogger(__name__)
57 log = logging.getLogger(__name__)
58
58
59
59
60 deform_templates = resource_filename('deform', 'templates')
60 deform_templates = resource_filename('deform', 'templates')
61 rhodecode_templates = resource_filename('rhodecode', 'templates/forms')
61 rhodecode_templates = resource_filename('rhodecode', 'templates/forms')
62 search_path = (rhodecode_templates, deform_templates)
62 search_path = (rhodecode_templates, deform_templates)
63
63
64
64
65 class RhodecodeFormZPTRendererFactory(deform.ZPTRendererFactory):
65 class RhodecodeFormZPTRendererFactory(deform.ZPTRendererFactory):
66 """ Subclass of ZPTRendererFactory to add rhodecode context variables """
66 """ Subclass of ZPTRendererFactory to add rhodecode context variables """
67 def __call__(self, template_name, **kw):
67 def __call__(self, template_name, **kw):
68 kw['h'] = helpers
68 kw['h'] = helpers
69 kw['request'] = get_current_request()
69 kw['request'] = get_current_request()
70 return self.load(template_name)(**kw)
70 return self.load(template_name)(**kw)
71
71
72
72
73 form_renderer = RhodecodeFormZPTRendererFactory(search_path)
73 form_renderer = RhodecodeFormZPTRendererFactory(search_path)
74 deform.Form.set_default_renderer(form_renderer)
74 deform.Form.set_default_renderer(form_renderer)
75
75
76
76
77 def LoginForm(localizer):
77 def LoginForm(localizer):
78 _ = localizer
78 _ = localizer
79
79
80 class _LoginForm(formencode.Schema):
80 class _LoginForm(formencode.Schema):
81 allow_extra_fields = True
81 allow_extra_fields = True
82 filter_extra_fields = True
82 filter_extra_fields = True
83 username = v.UnicodeString(
83 username = v.UnicodeString(
84 strip=True,
84 strip=True,
85 min=1,
85 min=1,
86 not_empty=True,
86 not_empty=True,
87 messages={
87 messages={
88 'empty': _(u'Please enter a login'),
88 'empty': _(u'Please enter a login'),
89 'tooShort': _(u'Enter a value %(min)i characters long or more')
89 'tooShort': _(u'Enter a value %(min)i characters long or more')
90 }
90 }
91 )
91 )
92
92
93 password = v.UnicodeString(
93 password = v.UnicodeString(
94 strip=False,
94 strip=False,
95 min=3,
95 min=3,
96 max=72,
96 max=72,
97 not_empty=True,
97 not_empty=True,
98 messages={
98 messages={
99 'empty': _(u'Please enter a password'),
99 'empty': _(u'Please enter a password'),
100 'tooShort': _(u'Enter %(min)i characters or more')}
100 'tooShort': _(u'Enter %(min)i characters or more')}
101 )
101 )
102
102
103 remember = v.StringBoolean(if_missing=False)
103 remember = v.StringBoolean(if_missing=False)
104
104
105 chained_validators = [v.ValidAuth(localizer)]
105 chained_validators = [v.ValidAuth(localizer)]
106 return _LoginForm
106 return _LoginForm
107
107
108
108
109 def UserForm(localizer, edit=False, available_languages=None, old_data=None):
109 def UserForm(localizer, edit=False, available_languages=None, old_data=None):
110 old_data = old_data or {}
110 old_data = old_data or {}
111 available_languages = available_languages or []
111 available_languages = available_languages or []
112 _ = localizer
112 _ = localizer
113
113
114 class _UserForm(formencode.Schema):
114 class _UserForm(formencode.Schema):
115 allow_extra_fields = True
115 allow_extra_fields = True
116 filter_extra_fields = True
116 filter_extra_fields = True
117 username = All(v.UnicodeString(strip=True, min=1, not_empty=True),
117 username = All(v.UnicodeString(strip=True, min=1, not_empty=True),
118 v.ValidUsername(localizer, edit, old_data))
118 v.ValidUsername(localizer, edit, old_data))
119 if edit:
119 if edit:
120 new_password = All(
120 new_password = All(
121 v.ValidPassword(localizer),
121 v.ValidPassword(localizer),
122 v.UnicodeString(strip=False, min=6, max=72, not_empty=False)
122 v.UnicodeString(strip=False, min=6, max=72, not_empty=False)
123 )
123 )
124 password_confirmation = All(
124 password_confirmation = All(
125 v.ValidPassword(localizer),
125 v.ValidPassword(localizer),
126 v.UnicodeString(strip=False, min=6, max=72, not_empty=False),
126 v.UnicodeString(strip=False, min=6, max=72, not_empty=False),
127 )
127 )
128 admin = v.StringBoolean(if_missing=False)
128 admin = v.StringBoolean(if_missing=False)
129 else:
129 else:
130 password = All(
130 password = All(
131 v.ValidPassword(localizer),
131 v.ValidPassword(localizer),
132 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
132 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
133 )
133 )
134 password_confirmation = All(
134 password_confirmation = All(
135 v.ValidPassword(localizer),
135 v.ValidPassword(localizer),
136 v.UnicodeString(strip=False, min=6, max=72, not_empty=False)
136 v.UnicodeString(strip=False, min=6, max=72, not_empty=False)
137 )
137 )
138
138
139 password_change = v.StringBoolean(if_missing=False)
139 password_change = v.StringBoolean(if_missing=False)
140 create_repo_group = v.StringBoolean(if_missing=False)
140 create_repo_group = v.StringBoolean(if_missing=False)
141
141
142 active = v.StringBoolean(if_missing=False)
142 active = v.StringBoolean(if_missing=False)
143 firstname = v.UnicodeString(strip=True, min=1, not_empty=False)
143 firstname = v.UnicodeString(strip=True, min=1, not_empty=False)
144 lastname = v.UnicodeString(strip=True, min=1, not_empty=False)
144 lastname = v.UnicodeString(strip=True, min=1, not_empty=False)
145 email = All(v.UniqSystemEmail(localizer, old_data), v.Email(not_empty=True))
145 email = All(v.UniqSystemEmail(localizer, old_data), v.Email(not_empty=True))
146 extern_name = v.UnicodeString(strip=True)
146 extern_name = v.UnicodeString(strip=True)
147 extern_type = v.UnicodeString(strip=True)
147 extern_type = v.UnicodeString(strip=True)
148 language = v.OneOf(available_languages, hideList=False,
148 language = v.OneOf(available_languages, hideList=False,
149 testValueList=True, if_missing=None)
149 testValueList=True, if_missing=None)
150 chained_validators = [v.ValidPasswordsMatch(localizer)]
150 chained_validators = [v.ValidPasswordsMatch(localizer)]
151 return _UserForm
151 return _UserForm
152
152
153
153
154 def UserGroupForm(localizer, edit=False, old_data=None, allow_disabled=False):
154 def UserGroupForm(localizer, edit=False, old_data=None, allow_disabled=False):
155 old_data = old_data or {}
155 old_data = old_data or {}
156 _ = localizer
156 _ = localizer
157
157
158 class _UserGroupForm(formencode.Schema):
158 class _UserGroupForm(formencode.Schema):
159 allow_extra_fields = True
159 allow_extra_fields = True
160 filter_extra_fields = True
160 filter_extra_fields = True
161
161
162 users_group_name = All(
162 users_group_name = All(
163 v.UnicodeString(strip=True, min=1, not_empty=True),
163 v.UnicodeString(strip=True, min=1, not_empty=True),
164 v.ValidUserGroup(localizer, edit, old_data)
164 v.ValidUserGroup(localizer, edit, old_data)
165 )
165 )
166 user_group_description = v.UnicodeString(strip=True, min=1,
166 user_group_description = v.UnicodeString(strip=True, min=1,
167 not_empty=False)
167 not_empty=False)
168
168
169 users_group_active = v.StringBoolean(if_missing=False)
169 users_group_active = v.StringBoolean(if_missing=False)
170
170
171 if edit:
171 if edit:
172 # this is user group owner
172 # this is user group owner
173 user = All(
173 user = All(
174 v.UnicodeString(not_empty=True),
174 v.UnicodeString(not_empty=True),
175 v.ValidRepoUser(localizer, allow_disabled))
175 v.ValidRepoUser(localizer, allow_disabled))
176 return _UserGroupForm
176 return _UserGroupForm
177
177
178
178
179 def RepoGroupForm(localizer, edit=False, old_data=None, available_groups=None,
179 def RepoGroupForm(localizer, edit=False, old_data=None, available_groups=None,
180 can_create_in_root=False, allow_disabled=False):
180 can_create_in_root=False, allow_disabled=False):
181 _ = localizer
181 _ = localizer
182 old_data = old_data or {}
182 old_data = old_data or {}
183 available_groups = available_groups or []
183 available_groups = available_groups or []
184
184
185 class _RepoGroupForm(formencode.Schema):
185 class _RepoGroupForm(formencode.Schema):
186 allow_extra_fields = True
186 allow_extra_fields = True
187 filter_extra_fields = False
187 filter_extra_fields = False
188
188
189 group_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
189 group_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
190 v.SlugifyName(localizer),)
190 v.SlugifyName(localizer),)
191 group_description = v.UnicodeString(strip=True, min=1,
191 group_description = v.UnicodeString(strip=True, min=1,
192 not_empty=False)
192 not_empty=False)
193 group_copy_permissions = v.StringBoolean(if_missing=False)
193 group_copy_permissions = v.StringBoolean(if_missing=False)
194
194
195 group_parent_id = v.OneOf(available_groups, hideList=False,
195 group_parent_id = v.OneOf(available_groups, hideList=False,
196 testValueList=True, not_empty=True)
196 testValueList=True, not_empty=True)
197 enable_locking = v.StringBoolean(if_missing=False)
197 enable_locking = v.StringBoolean(if_missing=False)
198 chained_validators = [
198 chained_validators = [
199 v.ValidRepoGroup(localizer, edit, old_data, can_create_in_root)]
199 v.ValidRepoGroup(localizer, edit, old_data, can_create_in_root)]
200
200
201 if edit:
201 if edit:
202 # this is repo group owner
202 # this is repo group owner
203 user = All(
203 user = All(
204 v.UnicodeString(not_empty=True),
204 v.UnicodeString(not_empty=True),
205 v.ValidRepoUser(localizer, allow_disabled))
205 v.ValidRepoUser(localizer, allow_disabled))
206 return _RepoGroupForm
206 return _RepoGroupForm
207
207
208
208
209 def RegisterForm(localizer, edit=False, old_data=None):
209 def RegisterForm(localizer, edit=False, old_data=None):
210 _ = localizer
210 _ = localizer
211 old_data = old_data or {}
211 old_data = old_data or {}
212
212
213 class _RegisterForm(formencode.Schema):
213 class _RegisterForm(formencode.Schema):
214 allow_extra_fields = True
214 allow_extra_fields = True
215 filter_extra_fields = True
215 filter_extra_fields = True
216 username = All(
216 username = All(
217 v.ValidUsername(localizer, edit, old_data),
217 v.ValidUsername(localizer, edit, old_data),
218 v.UnicodeString(strip=True, min=1, not_empty=True)
218 v.UnicodeString(strip=True, min=1, not_empty=True)
219 )
219 )
220 password = All(
220 password = All(
221 v.ValidPassword(localizer),
221 v.ValidPassword(localizer),
222 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
222 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
223 )
223 )
224 password_confirmation = All(
224 password_confirmation = All(
225 v.ValidPassword(localizer),
225 v.ValidPassword(localizer),
226 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
226 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
227 )
227 )
228 active = v.StringBoolean(if_missing=False)
228 active = v.StringBoolean(if_missing=False)
229 firstname = v.UnicodeString(strip=True, min=1, not_empty=False)
229 firstname = v.UnicodeString(strip=True, min=1, not_empty=False)
230 lastname = v.UnicodeString(strip=True, min=1, not_empty=False)
230 lastname = v.UnicodeString(strip=True, min=1, not_empty=False)
231 email = All(v.UniqSystemEmail(localizer, old_data), v.Email(not_empty=True))
231 email = All(v.UniqSystemEmail(localizer, old_data), v.Email(not_empty=True))
232
232
233 chained_validators = [v.ValidPasswordsMatch(localizer)]
233 chained_validators = [v.ValidPasswordsMatch(localizer)]
234 return _RegisterForm
234 return _RegisterForm
235
235
236
236
237 def PasswordResetForm(localizer):
237 def PasswordResetForm(localizer):
238 _ = localizer
238 _ = localizer
239
239
240 class _PasswordResetForm(formencode.Schema):
240 class _PasswordResetForm(formencode.Schema):
241 allow_extra_fields = True
241 allow_extra_fields = True
242 filter_extra_fields = True
242 filter_extra_fields = True
243 email = All(v.ValidSystemEmail(localizer), v.Email(not_empty=True))
243 email = All(v.ValidSystemEmail(localizer), v.Email(not_empty=True))
244 return _PasswordResetForm
244 return _PasswordResetForm
245
245
246
246
247 def RepoForm(localizer, edit=False, old_data=None, repo_groups=None,
247 def RepoForm(localizer, edit=False, old_data=None, repo_groups=None,
248 landing_revs=None, allow_disabled=False):
248 landing_revs=None, allow_disabled=False):
249 _ = localizer
249 _ = localizer
250 old_data = old_data or {}
250 old_data = old_data or {}
251 repo_groups = repo_groups or []
251 repo_groups = repo_groups or []
252 landing_revs = landing_revs or []
252 landing_revs = landing_revs or []
253 supported_backends = BACKENDS.keys()
253 supported_backends = BACKENDS.keys()
254
254
255 class _RepoForm(formencode.Schema):
255 class _RepoForm(formencode.Schema):
256 allow_extra_fields = True
256 allow_extra_fields = True
257 filter_extra_fields = False
257 filter_extra_fields = False
258 repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
258 repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
259 v.SlugifyName(localizer), v.CannotHaveGitSuffix(localizer))
259 v.SlugifyName(localizer), v.CannotHaveGitSuffix(localizer))
260 repo_group = All(v.CanWriteGroup(localizer, old_data),
260 repo_group = All(v.CanWriteGroup(localizer, old_data),
261 v.OneOf(repo_groups, hideList=True))
261 v.OneOf(repo_groups, hideList=True))
262 repo_type = v.OneOf(supported_backends, required=False,
262 repo_type = v.OneOf(supported_backends, required=False,
263 if_missing=old_data.get('repo_type'))
263 if_missing=old_data.get('repo_type'))
264 repo_description = v.UnicodeString(strip=True, min=1, not_empty=False)
264 repo_description = v.UnicodeString(strip=True, min=1, not_empty=False)
265 repo_private = v.StringBoolean(if_missing=False)
265 repo_private = v.StringBoolean(if_missing=False)
266 repo_landing_rev = v.OneOf(landing_revs, hideList=True)
266 repo_landing_rev = v.OneOf(landing_revs, hideList=True)
267 repo_copy_permissions = v.StringBoolean(if_missing=False)
267 repo_copy_permissions = v.StringBoolean(if_missing=False)
268 clone_uri = All(v.UnicodeString(strip=True, min=1, not_empty=False))
268 clone_uri = All(v.UnicodeString(strip=True, min=1, not_empty=False))
269
269
270 repo_enable_statistics = v.StringBoolean(if_missing=False)
270 repo_enable_statistics = v.StringBoolean(if_missing=False)
271 repo_enable_downloads = v.StringBoolean(if_missing=False)
271 repo_enable_downloads = v.StringBoolean(if_missing=False)
272 repo_enable_locking = v.StringBoolean(if_missing=False)
272 repo_enable_locking = v.StringBoolean(if_missing=False)
273
273
274 if edit:
274 if edit:
275 # this is repo owner
275 # this is repo owner
276 user = All(
276 user = All(
277 v.UnicodeString(not_empty=True),
277 v.UnicodeString(not_empty=True),
278 v.ValidRepoUser(localizer, allow_disabled))
278 v.ValidRepoUser(localizer, allow_disabled))
279 clone_uri_change = v.UnicodeString(
279 clone_uri_change = v.UnicodeString(
280 not_empty=False, if_missing=v.Missing)
280 not_empty=False, if_missing=v.Missing)
281
281
282 chained_validators = [v.ValidCloneUri(localizer),
282 chained_validators = [v.ValidCloneUri(localizer),
283 v.ValidRepoName(localizer, edit, old_data)]
283 v.ValidRepoName(localizer, edit, old_data)]
284 return _RepoForm
284 return _RepoForm
285
285
286
286
287 def RepoPermsForm(localizer):
287 def RepoPermsForm(localizer):
288 _ = localizer
288 _ = localizer
289
289
290 class _RepoPermsForm(formencode.Schema):
290 class _RepoPermsForm(formencode.Schema):
291 allow_extra_fields = True
291 allow_extra_fields = True
292 filter_extra_fields = False
292 filter_extra_fields = False
293 chained_validators = [v.ValidPerms(localizer, type_='repo')]
293 chained_validators = [v.ValidPerms(localizer, type_='repo')]
294 return _RepoPermsForm
294 return _RepoPermsForm
295
295
296
296
297 def RepoGroupPermsForm(localizer, valid_recursive_choices):
297 def RepoGroupPermsForm(localizer, valid_recursive_choices):
298 _ = localizer
298 _ = localizer
299
299
300 class _RepoGroupPermsForm(formencode.Schema):
300 class _RepoGroupPermsForm(formencode.Schema):
301 allow_extra_fields = True
301 allow_extra_fields = True
302 filter_extra_fields = False
302 filter_extra_fields = False
303 recursive = v.OneOf(valid_recursive_choices)
303 recursive = v.OneOf(valid_recursive_choices)
304 chained_validators = [v.ValidPerms(localizer, type_='repo_group')]
304 chained_validators = [v.ValidPerms(localizer, type_='repo_group')]
305 return _RepoGroupPermsForm
305 return _RepoGroupPermsForm
306
306
307
307
308 def UserGroupPermsForm(localizer):
308 def UserGroupPermsForm(localizer):
309 _ = localizer
309 _ = localizer
310
310
311 class _UserPermsForm(formencode.Schema):
311 class _UserPermsForm(formencode.Schema):
312 allow_extra_fields = True
312 allow_extra_fields = True
313 filter_extra_fields = False
313 filter_extra_fields = False
314 chained_validators = [v.ValidPerms(localizer, type_='user_group')]
314 chained_validators = [v.ValidPerms(localizer, type_='user_group')]
315 return _UserPermsForm
315 return _UserPermsForm
316
316
317
317
318 def RepoFieldForm(localizer):
318 def RepoFieldForm(localizer):
319 _ = localizer
319 _ = localizer
320
320
321 class _RepoFieldForm(formencode.Schema):
321 class _RepoFieldForm(formencode.Schema):
322 filter_extra_fields = True
322 filter_extra_fields = True
323 allow_extra_fields = True
323 allow_extra_fields = True
324
324
325 new_field_key = All(v.FieldKey(localizer),
325 new_field_key = All(v.FieldKey(localizer),
326 v.UnicodeString(strip=True, min=3, not_empty=True))
326 v.UnicodeString(strip=True, min=3, not_empty=True))
327 new_field_value = v.UnicodeString(not_empty=False, if_missing=u'')
327 new_field_value = v.UnicodeString(not_empty=False, if_missing=u'')
328 new_field_type = v.OneOf(['str', 'unicode', 'list', 'tuple'],
328 new_field_type = v.OneOf(['str', 'unicode', 'list', 'tuple'],
329 if_missing='str')
329 if_missing='str')
330 new_field_label = v.UnicodeString(not_empty=False)
330 new_field_label = v.UnicodeString(not_empty=False)
331 new_field_desc = v.UnicodeString(not_empty=False)
331 new_field_desc = v.UnicodeString(not_empty=False)
332 return _RepoFieldForm
332 return _RepoFieldForm
333
333
334
334
335 def RepoForkForm(localizer, edit=False, old_data=None,
335 def RepoForkForm(localizer, edit=False, old_data=None,
336 supported_backends=BACKENDS.keys(), repo_groups=None,
336 supported_backends=BACKENDS.keys(), repo_groups=None,
337 landing_revs=None):
337 landing_revs=None):
338 _ = localizer
338 _ = localizer
339 old_data = old_data or {}
339 old_data = old_data or {}
340 repo_groups = repo_groups or []
340 repo_groups = repo_groups or []
341 landing_revs = landing_revs or []
341 landing_revs = landing_revs or []
342
342
343 class _RepoForkForm(formencode.Schema):
343 class _RepoForkForm(formencode.Schema):
344 allow_extra_fields = True
344 allow_extra_fields = True
345 filter_extra_fields = False
345 filter_extra_fields = False
346 repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
346 repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
347 v.SlugifyName(localizer))
347 v.SlugifyName(localizer))
348 repo_group = All(v.CanWriteGroup(localizer, ),
348 repo_group = All(v.CanWriteGroup(localizer, ),
349 v.OneOf(repo_groups, hideList=True))
349 v.OneOf(repo_groups, hideList=True))
350 repo_type = All(v.ValidForkType(localizer, old_data), v.OneOf(supported_backends))
350 repo_type = All(v.ValidForkType(localizer, old_data), v.OneOf(supported_backends))
351 description = v.UnicodeString(strip=True, min=1, not_empty=True)
351 description = v.UnicodeString(strip=True, min=1, not_empty=True)
352 private = v.StringBoolean(if_missing=False)
352 private = v.StringBoolean(if_missing=False)
353 copy_permissions = v.StringBoolean(if_missing=False)
353 copy_permissions = v.StringBoolean(if_missing=False)
354 fork_parent_id = v.UnicodeString()
354 fork_parent_id = v.UnicodeString()
355 chained_validators = [v.ValidForkName(localizer, edit, old_data)]
355 chained_validators = [v.ValidForkName(localizer, edit, old_data)]
356 landing_rev = v.OneOf(landing_revs, hideList=True)
356 landing_rev = v.OneOf(landing_revs, hideList=True)
357 return _RepoForkForm
357 return _RepoForkForm
358
358
359
359
360 def ApplicationSettingsForm(localizer):
360 def ApplicationSettingsForm(localizer):
361 _ = localizer
361 _ = localizer
362
362
363 class _ApplicationSettingsForm(formencode.Schema):
363 class _ApplicationSettingsForm(formencode.Schema):
364 allow_extra_fields = True
364 allow_extra_fields = True
365 filter_extra_fields = False
365 filter_extra_fields = False
366 rhodecode_title = v.UnicodeString(strip=True, max=40, not_empty=False)
366 rhodecode_title = v.UnicodeString(strip=True, max=40, not_empty=False)
367 rhodecode_realm = v.UnicodeString(strip=True, min=1, not_empty=True)
367 rhodecode_realm = v.UnicodeString(strip=True, min=1, not_empty=True)
368 rhodecode_pre_code = v.UnicodeString(strip=True, min=1, not_empty=False)
368 rhodecode_pre_code = v.UnicodeString(strip=True, min=1, not_empty=False)
369 rhodecode_post_code = v.UnicodeString(strip=True, min=1, not_empty=False)
369 rhodecode_post_code = v.UnicodeString(strip=True, min=1, not_empty=False)
370 rhodecode_captcha_public_key = v.UnicodeString(strip=True, min=1, not_empty=False)
370 rhodecode_captcha_public_key = v.UnicodeString(strip=True, min=1, not_empty=False)
371 rhodecode_captcha_private_key = v.UnicodeString(strip=True, min=1, not_empty=False)
371 rhodecode_captcha_private_key = v.UnicodeString(strip=True, min=1, not_empty=False)
372 rhodecode_create_personal_repo_group = v.StringBoolean(if_missing=False)
372 rhodecode_create_personal_repo_group = v.StringBoolean(if_missing=False)
373 rhodecode_personal_repo_group_pattern = v.UnicodeString(strip=True, min=1, not_empty=False)
373 rhodecode_personal_repo_group_pattern = v.UnicodeString(strip=True, min=1, not_empty=False)
374 return _ApplicationSettingsForm
374 return _ApplicationSettingsForm
375
375
376
376
377 def ApplicationVisualisationForm(localizer):
377 def ApplicationVisualisationForm(localizer):
378 from rhodecode.model.db import Repository
378 from rhodecode.model.db import Repository
379 _ = localizer
379 _ = localizer
380
380
381 class _ApplicationVisualisationForm(formencode.Schema):
381 class _ApplicationVisualisationForm(formencode.Schema):
382 allow_extra_fields = True
382 allow_extra_fields = True
383 filter_extra_fields = False
383 filter_extra_fields = False
384 rhodecode_show_public_icon = v.StringBoolean(if_missing=False)
384 rhodecode_show_public_icon = v.StringBoolean(if_missing=False)
385 rhodecode_show_private_icon = v.StringBoolean(if_missing=False)
385 rhodecode_show_private_icon = v.StringBoolean(if_missing=False)
386 rhodecode_stylify_metatags = v.StringBoolean(if_missing=False)
386 rhodecode_stylify_metatags = v.StringBoolean(if_missing=False)
387
387
388 rhodecode_repository_fields = v.StringBoolean(if_missing=False)
388 rhodecode_repository_fields = v.StringBoolean(if_missing=False)
389 rhodecode_lightweight_journal = v.StringBoolean(if_missing=False)
389 rhodecode_lightweight_journal = v.StringBoolean(if_missing=False)
390 rhodecode_dashboard_items = v.Int(min=5, not_empty=True)
390 rhodecode_dashboard_items = v.Int(min=5, not_empty=True)
391 rhodecode_admin_grid_items = v.Int(min=5, not_empty=True)
391 rhodecode_admin_grid_items = v.Int(min=5, not_empty=True)
392 rhodecode_show_version = v.StringBoolean(if_missing=False)
392 rhodecode_show_version = v.StringBoolean(if_missing=False)
393 rhodecode_use_gravatar = v.StringBoolean(if_missing=False)
393 rhodecode_use_gravatar = v.StringBoolean(if_missing=False)
394 rhodecode_markup_renderer = v.OneOf(['markdown', 'rst'])
394 rhodecode_markup_renderer = v.OneOf(['markdown', 'rst'])
395 rhodecode_gravatar_url = v.UnicodeString(min=3)
395 rhodecode_gravatar_url = v.UnicodeString(min=3)
396 rhodecode_clone_uri_tmpl = v.UnicodeString(not_empty=False, if_empty=Repository.DEFAULT_CLONE_URI)
396 rhodecode_clone_uri_tmpl = v.UnicodeString(not_empty=False, if_empty=Repository.DEFAULT_CLONE_URI)
397 rhodecode_clone_uri_ssh_tmpl = v.UnicodeString(not_empty=False, if_empty=Repository.DEFAULT_CLONE_URI_SSH)
397 rhodecode_clone_uri_ssh_tmpl = v.UnicodeString(not_empty=False, if_empty=Repository.DEFAULT_CLONE_URI_SSH)
398 rhodecode_support_url = v.UnicodeString()
398 rhodecode_support_url = v.UnicodeString()
399 rhodecode_show_revision_number = v.StringBoolean(if_missing=False)
399 rhodecode_show_revision_number = v.StringBoolean(if_missing=False)
400 rhodecode_show_sha_length = v.Int(min=4, not_empty=True)
400 rhodecode_show_sha_length = v.Int(min=4, not_empty=True)
401 return _ApplicationVisualisationForm
401 return _ApplicationVisualisationForm
402
402
403
403
404 class _BaseVcsSettingsForm(formencode.Schema):
404 class _BaseVcsSettingsForm(formencode.Schema):
405
405
406 allow_extra_fields = True
406 allow_extra_fields = True
407 filter_extra_fields = False
407 filter_extra_fields = False
408 hooks_changegroup_repo_size = v.StringBoolean(if_missing=False)
408 hooks_changegroup_repo_size = v.StringBoolean(if_missing=False)
409 hooks_changegroup_push_logger = v.StringBoolean(if_missing=False)
409 hooks_changegroup_push_logger = v.StringBoolean(if_missing=False)
410 hooks_outgoing_pull_logger = v.StringBoolean(if_missing=False)
410 hooks_outgoing_pull_logger = v.StringBoolean(if_missing=False)
411
411
412 # PR/Code-review
412 # PR/Code-review
413 rhodecode_pr_merge_enabled = v.StringBoolean(if_missing=False)
413 rhodecode_pr_merge_enabled = v.StringBoolean(if_missing=False)
414 rhodecode_use_outdated_comments = v.StringBoolean(if_missing=False)
414 rhodecode_use_outdated_comments = v.StringBoolean(if_missing=False)
415
415
416 # hg
416 # hg
417 extensions_largefiles = v.StringBoolean(if_missing=False)
417 extensions_largefiles = v.StringBoolean(if_missing=False)
418 extensions_evolve = v.StringBoolean(if_missing=False)
418 extensions_evolve = v.StringBoolean(if_missing=False)
419 phases_publish = v.StringBoolean(if_missing=False)
419 phases_publish = v.StringBoolean(if_missing=False)
420
420
421 rhodecode_hg_use_rebase_for_merging = v.StringBoolean(if_missing=False)
421 rhodecode_hg_use_rebase_for_merging = v.StringBoolean(if_missing=False)
422 rhodecode_hg_close_branch_before_merging = v.StringBoolean(if_missing=False)
422 rhodecode_hg_close_branch_before_merging = v.StringBoolean(if_missing=False)
423
423
424 # git
424 # git
425 vcs_git_lfs_enabled = v.StringBoolean(if_missing=False)
425 vcs_git_lfs_enabled = v.StringBoolean(if_missing=False)
426 rhodecode_git_use_rebase_for_merging = v.StringBoolean(if_missing=False)
426 rhodecode_git_use_rebase_for_merging = v.StringBoolean(if_missing=False)
427 rhodecode_git_close_branch_before_merging = v.StringBoolean(if_missing=False)
427 rhodecode_git_close_branch_before_merging = v.StringBoolean(if_missing=False)
428
428
429 # svn
429 # svn
430 vcs_svn_proxy_http_requests_enabled = v.StringBoolean(if_missing=False)
430 vcs_svn_proxy_http_requests_enabled = v.StringBoolean(if_missing=False)
431 vcs_svn_proxy_http_server_url = v.UnicodeString(strip=True, if_missing=None)
431 vcs_svn_proxy_http_server_url = v.UnicodeString(strip=True, if_missing=None)
432
432
433 # cache
433 # cache
434 rhodecode_diff_cache = v.StringBoolean(if_missing=False)
434 rhodecode_diff_cache = v.StringBoolean(if_missing=False)
435
435
436
436
437 def ApplicationUiSettingsForm(localizer):
437 def ApplicationUiSettingsForm(localizer):
438 _ = localizer
438 _ = localizer
439
439
440 class _ApplicationUiSettingsForm(_BaseVcsSettingsForm):
440 class _ApplicationUiSettingsForm(_BaseVcsSettingsForm):
441 web_push_ssl = v.StringBoolean(if_missing=False)
441 web_push_ssl = v.StringBoolean(if_missing=False)
442 paths_root_path = All(
442 paths_root_path = All(
443 v.ValidPath(localizer),
443 v.ValidPath(localizer),
444 v.UnicodeString(strip=True, min=1, not_empty=True)
444 v.UnicodeString(strip=True, min=1, not_empty=True)
445 )
445 )
446 largefiles_usercache = All(
446 largefiles_usercache = All(
447 v.ValidPath(localizer),
447 v.ValidPath(localizer),
448 v.UnicodeString(strip=True, min=2, not_empty=True))
448 v.UnicodeString(strip=True, min=2, not_empty=True))
449 vcs_git_lfs_store_location = All(
449 vcs_git_lfs_store_location = All(
450 v.ValidPath(localizer),
450 v.ValidPath(localizer),
451 v.UnicodeString(strip=True, min=2, not_empty=True))
451 v.UnicodeString(strip=True, min=2, not_empty=True))
452 extensions_hgsubversion = v.StringBoolean(if_missing=False)
452 extensions_hgsubversion = v.StringBoolean(if_missing=False)
453 extensions_hggit = v.StringBoolean(if_missing=False)
453 extensions_hggit = v.StringBoolean(if_missing=False)
454 new_svn_branch = v.ValidSvnPattern(localizer, section='vcs_svn_branch')
454 new_svn_branch = v.ValidSvnPattern(localizer, section='vcs_svn_branch')
455 new_svn_tag = v.ValidSvnPattern(localizer, section='vcs_svn_tag')
455 new_svn_tag = v.ValidSvnPattern(localizer, section='vcs_svn_tag')
456 return _ApplicationUiSettingsForm
456 return _ApplicationUiSettingsForm
457
457
458
458
459 def RepoVcsSettingsForm(localizer, repo_name):
459 def RepoVcsSettingsForm(localizer, repo_name):
460 _ = localizer
460 _ = localizer
461
461
462 class _RepoVcsSettingsForm(_BaseVcsSettingsForm):
462 class _RepoVcsSettingsForm(_BaseVcsSettingsForm):
463 inherit_global_settings = v.StringBoolean(if_missing=False)
463 inherit_global_settings = v.StringBoolean(if_missing=False)
464 new_svn_branch = v.ValidSvnPattern(localizer,
464 new_svn_branch = v.ValidSvnPattern(localizer,
465 section='vcs_svn_branch', repo_name=repo_name)
465 section='vcs_svn_branch', repo_name=repo_name)
466 new_svn_tag = v.ValidSvnPattern(localizer,
466 new_svn_tag = v.ValidSvnPattern(localizer,
467 section='vcs_svn_tag', repo_name=repo_name)
467 section='vcs_svn_tag', repo_name=repo_name)
468 return _RepoVcsSettingsForm
468 return _RepoVcsSettingsForm
469
469
470
470
471 def LabsSettingsForm(localizer):
471 def LabsSettingsForm(localizer):
472 _ = localizer
472 _ = localizer
473
473
474 class _LabSettingsForm(formencode.Schema):
474 class _LabSettingsForm(formencode.Schema):
475 allow_extra_fields = True
475 allow_extra_fields = True
476 filter_extra_fields = False
476 filter_extra_fields = False
477 return _LabSettingsForm
477 return _LabSettingsForm
478
478
479
479
480 def ApplicationPermissionsForm(
480 def ApplicationPermissionsForm(
481 localizer, register_choices, password_reset_choices,
481 localizer, register_choices, password_reset_choices,
482 extern_activate_choices):
482 extern_activate_choices):
483 _ = localizer
483 _ = localizer
484
484
485 class _DefaultPermissionsForm(formencode.Schema):
485 class _DefaultPermissionsForm(formencode.Schema):
486 allow_extra_fields = True
486 allow_extra_fields = True
487 filter_extra_fields = True
487 filter_extra_fields = True
488
488
489 anonymous = v.StringBoolean(if_missing=False)
489 anonymous = v.StringBoolean(if_missing=False)
490 default_register = v.OneOf(register_choices)
490 default_register = v.OneOf(register_choices)
491 default_register_message = v.UnicodeString()
491 default_register_message = v.UnicodeString()
492 default_password_reset = v.OneOf(password_reset_choices)
492 default_password_reset = v.OneOf(password_reset_choices)
493 default_extern_activate = v.OneOf(extern_activate_choices)
493 default_extern_activate = v.OneOf(extern_activate_choices)
494 return _DefaultPermissionsForm
494 return _DefaultPermissionsForm
495
495
496
496
497 def ObjectPermissionsForm(localizer, repo_perms_choices, group_perms_choices,
497 def ObjectPermissionsForm(localizer, repo_perms_choices, group_perms_choices,
498 user_group_perms_choices):
498 user_group_perms_choices):
499 _ = localizer
499 _ = localizer
500
500
501 class _ObjectPermissionsForm(formencode.Schema):
501 class _ObjectPermissionsForm(formencode.Schema):
502 allow_extra_fields = True
502 allow_extra_fields = True
503 filter_extra_fields = True
503 filter_extra_fields = True
504 overwrite_default_repo = v.StringBoolean(if_missing=False)
504 overwrite_default_repo = v.StringBoolean(if_missing=False)
505 overwrite_default_group = v.StringBoolean(if_missing=False)
505 overwrite_default_group = v.StringBoolean(if_missing=False)
506 overwrite_default_user_group = v.StringBoolean(if_missing=False)
506 overwrite_default_user_group = v.StringBoolean(if_missing=False)
507 default_repo_perm = v.OneOf(repo_perms_choices)
507 default_repo_perm = v.OneOf(repo_perms_choices)
508 default_group_perm = v.OneOf(group_perms_choices)
508 default_group_perm = v.OneOf(group_perms_choices)
509 default_user_group_perm = v.OneOf(user_group_perms_choices)
509 default_user_group_perm = v.OneOf(user_group_perms_choices)
510 return _ObjectPermissionsForm
510 return _ObjectPermissionsForm
511
511
512
512
513 def UserPermissionsForm(localizer, create_choices, create_on_write_choices,
513 def UserPermissionsForm(localizer, create_choices, create_on_write_choices,
514 repo_group_create_choices, user_group_create_choices,
514 repo_group_create_choices, user_group_create_choices,
515 fork_choices, inherit_default_permissions_choices):
515 fork_choices, inherit_default_permissions_choices):
516 _ = localizer
516 _ = localizer
517
517
518 class _DefaultPermissionsForm(formencode.Schema):
518 class _DefaultPermissionsForm(formencode.Schema):
519 allow_extra_fields = True
519 allow_extra_fields = True
520 filter_extra_fields = True
520 filter_extra_fields = True
521
521
522 anonymous = v.StringBoolean(if_missing=False)
522 anonymous = v.StringBoolean(if_missing=False)
523
523
524 default_repo_create = v.OneOf(create_choices)
524 default_repo_create = v.OneOf(create_choices)
525 default_repo_create_on_write = v.OneOf(create_on_write_choices)
525 default_repo_create_on_write = v.OneOf(create_on_write_choices)
526 default_user_group_create = v.OneOf(user_group_create_choices)
526 default_user_group_create = v.OneOf(user_group_create_choices)
527 default_repo_group_create = v.OneOf(repo_group_create_choices)
527 default_repo_group_create = v.OneOf(repo_group_create_choices)
528 default_fork_create = v.OneOf(fork_choices)
528 default_fork_create = v.OneOf(fork_choices)
529 default_inherit_default_permissions = v.OneOf(inherit_default_permissions_choices)
529 default_inherit_default_permissions = v.OneOf(inherit_default_permissions_choices)
530 return _DefaultPermissionsForm
530 return _DefaultPermissionsForm
531
531
532
532
533 def UserIndividualPermissionsForm(localizer):
533 def UserIndividualPermissionsForm(localizer):
534 _ = localizer
534 _ = localizer
535
535
536 class _DefaultPermissionsForm(formencode.Schema):
536 class _DefaultPermissionsForm(formencode.Schema):
537 allow_extra_fields = True
537 allow_extra_fields = True
538 filter_extra_fields = True
538 filter_extra_fields = True
539
539
540 inherit_default_permissions = v.StringBoolean(if_missing=False)
540 inherit_default_permissions = v.StringBoolean(if_missing=False)
541 return _DefaultPermissionsForm
541 return _DefaultPermissionsForm
542
542
543
543
544 def DefaultsForm(localizer, edit=False, old_data=None, supported_backends=BACKENDS.keys()):
544 def DefaultsForm(localizer, edit=False, old_data=None, supported_backends=BACKENDS.keys()):
545 _ = localizer
545 _ = localizer
546 old_data = old_data or {}
546 old_data = old_data or {}
547
547
548 class _DefaultsForm(formencode.Schema):
548 class _DefaultsForm(formencode.Schema):
549 allow_extra_fields = True
549 allow_extra_fields = True
550 filter_extra_fields = True
550 filter_extra_fields = True
551 default_repo_type = v.OneOf(supported_backends)
551 default_repo_type = v.OneOf(supported_backends)
552 default_repo_private = v.StringBoolean(if_missing=False)
552 default_repo_private = v.StringBoolean(if_missing=False)
553 default_repo_enable_statistics = v.StringBoolean(if_missing=False)
553 default_repo_enable_statistics = v.StringBoolean(if_missing=False)
554 default_repo_enable_downloads = v.StringBoolean(if_missing=False)
554 default_repo_enable_downloads = v.StringBoolean(if_missing=False)
555 default_repo_enable_locking = v.StringBoolean(if_missing=False)
555 default_repo_enable_locking = v.StringBoolean(if_missing=False)
556 return _DefaultsForm
556 return _DefaultsForm
557
557
558
558
559 def AuthSettingsForm(localizer):
559 def AuthSettingsForm(localizer):
560 _ = localizer
560 _ = localizer
561
561
562 class _AuthSettingsForm(formencode.Schema):
562 class _AuthSettingsForm(formencode.Schema):
563 allow_extra_fields = True
563 allow_extra_fields = True
564 filter_extra_fields = True
564 filter_extra_fields = True
565 auth_plugins = All(v.ValidAuthPlugins(localizer),
565 auth_plugins = All(v.ValidAuthPlugins(localizer),
566 v.UniqueListFromString(localizer)(not_empty=True))
566 v.UniqueListFromString(localizer)(not_empty=True))
567 return _AuthSettingsForm
567 return _AuthSettingsForm
568
568
569
569
570 def UserExtraEmailForm(localizer):
570 def UserExtraEmailForm(localizer):
571 _ = localizer
571 _ = localizer
572
572
573 class _UserExtraEmailForm(formencode.Schema):
573 class _UserExtraEmailForm(formencode.Schema):
574 email = All(v.UniqSystemEmail(localizer), v.Email(not_empty=True))
574 email = All(v.UniqSystemEmail(localizer), v.Email(not_empty=True))
575 return _UserExtraEmailForm
575 return _UserExtraEmailForm
576
576
577
577
578 def UserExtraIpForm(localizer):
578 def UserExtraIpForm(localizer):
579 _ = localizer
579 _ = localizer
580
580
581 class _UserExtraIpForm(formencode.Schema):
581 class _UserExtraIpForm(formencode.Schema):
582 ip = v.ValidIp(localizer)(not_empty=True)
582 ip = v.ValidIp(localizer)(not_empty=True)
583 return _UserExtraIpForm
583 return _UserExtraIpForm
584
584
585
585
586 def PullRequestForm(localizer, repo_id):
586 def PullRequestForm(localizer, repo_id):
587 _ = localizer
587 _ = localizer
588
588
589 class ReviewerForm(formencode.Schema):
589 class ReviewerForm(formencode.Schema):
590 user_id = v.Int(not_empty=True)
590 user_id = v.Int(not_empty=True)
591 reasons = All()
591 reasons = All()
592 rules = All(v.UniqueList(localizer, convert=int)())
592 rules = All(v.UniqueList(localizer, convert=int)())
593 mandatory = v.StringBoolean()
593 mandatory = v.StringBoolean()
594
594
595 class _PullRequestForm(formencode.Schema):
595 class _PullRequestForm(formencode.Schema):
596 allow_extra_fields = True
596 allow_extra_fields = True
597 filter_extra_fields = True
597 filter_extra_fields = True
598
598
599 common_ancestor = v.UnicodeString(strip=True, required=True)
599 common_ancestor = v.UnicodeString(strip=True, required=True)
600 source_repo = v.UnicodeString(strip=True, required=True)
600 source_repo = v.UnicodeString(strip=True, required=True)
601 source_ref = v.UnicodeString(strip=True, required=True)
601 source_ref = v.UnicodeString(strip=True, required=True)
602 target_repo = v.UnicodeString(strip=True, required=True)
602 target_repo = v.UnicodeString(strip=True, required=True)
603 target_ref = v.UnicodeString(strip=True, required=True)
603 target_ref = v.UnicodeString(strip=True, required=True)
604 revisions = All(#v.NotReviewedRevisions(localizer, repo_id)(),
604 revisions = All(#v.NotReviewedRevisions(localizer, repo_id)(),
605 v.UniqueList(localizer)(not_empty=True))
605 v.UniqueList(localizer)(not_empty=True))
606 review_members = formencode.ForEach(ReviewerForm())
606 review_members = formencode.ForEach(ReviewerForm())
607 pullrequest_title = v.UnicodeString(strip=True, required=True, min=3, max=255)
607 pullrequest_title = v.UnicodeString(strip=True, required=True, min=3, max=255)
608 pullrequest_desc = v.UnicodeString(strip=True, required=False)
608 pullrequest_desc = v.UnicodeString(strip=True, required=False)
609 description_renderer = v.UnicodeString(strip=True, required=False)
609
610
610 return _PullRequestForm
611 return _PullRequestForm
611
612
612
613
613 def IssueTrackerPatternsForm(localizer):
614 def IssueTrackerPatternsForm(localizer):
614 _ = localizer
615 _ = localizer
615
616
616 class _IssueTrackerPatternsForm(formencode.Schema):
617 class _IssueTrackerPatternsForm(formencode.Schema):
617 allow_extra_fields = True
618 allow_extra_fields = True
618 filter_extra_fields = False
619 filter_extra_fields = False
619 chained_validators = [v.ValidPattern(localizer)]
620 chained_validators = [v.ValidPattern(localizer)]
620 return _IssueTrackerPatternsForm
621 return _IssueTrackerPatternsForm
@@ -1,1701 +1,1704 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2012-2018 RhodeCode GmbH
3 # Copyright (C) 2012-2018 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 pull request model for RhodeCode
23 pull request model for RhodeCode
24 """
24 """
25
25
26
26
27 import json
27 import json
28 import logging
28 import logging
29 import datetime
29 import datetime
30 import urllib
30 import urllib
31 import collections
31 import collections
32
32
33 from pyramid.threadlocal import get_current_request
33 from pyramid.threadlocal import get_current_request
34
34
35 from rhodecode import events
35 from rhodecode import events
36 from rhodecode.translation import lazy_ugettext#, _
36 from rhodecode.translation import lazy_ugettext#, _
37 from rhodecode.lib import helpers as h, hooks_utils, diffs
37 from rhodecode.lib import helpers as h, hooks_utils, diffs
38 from rhodecode.lib import audit_logger
38 from rhodecode.lib import audit_logger
39 from rhodecode.lib.compat import OrderedDict
39 from rhodecode.lib.compat import OrderedDict
40 from rhodecode.lib.hooks_daemon import prepare_callback_daemon
40 from rhodecode.lib.hooks_daemon import prepare_callback_daemon
41 from rhodecode.lib.markup_renderer import (
41 from rhodecode.lib.markup_renderer import (
42 DEFAULT_COMMENTS_RENDERER, RstTemplateRenderer)
42 DEFAULT_COMMENTS_RENDERER, RstTemplateRenderer)
43 from rhodecode.lib.utils2 import safe_unicode, safe_str, md5_safe
43 from rhodecode.lib.utils2 import safe_unicode, safe_str, md5_safe
44 from rhodecode.lib.vcs.backends.base import (
44 from rhodecode.lib.vcs.backends.base import (
45 Reference, MergeResponse, MergeFailureReason, UpdateFailureReason)
45 Reference, MergeResponse, MergeFailureReason, UpdateFailureReason)
46 from rhodecode.lib.vcs.conf import settings as vcs_settings
46 from rhodecode.lib.vcs.conf import settings as vcs_settings
47 from rhodecode.lib.vcs.exceptions import (
47 from rhodecode.lib.vcs.exceptions import (
48 CommitDoesNotExistError, EmptyRepositoryError)
48 CommitDoesNotExistError, EmptyRepositoryError)
49 from rhodecode.model import BaseModel
49 from rhodecode.model import BaseModel
50 from rhodecode.model.changeset_status import ChangesetStatusModel
50 from rhodecode.model.changeset_status import ChangesetStatusModel
51 from rhodecode.model.comment import CommentsModel
51 from rhodecode.model.comment import CommentsModel
52 from rhodecode.model.db import (
52 from rhodecode.model.db import (
53 or_, PullRequest, PullRequestReviewers, ChangesetStatus,
53 or_, PullRequest, PullRequestReviewers, ChangesetStatus,
54 PullRequestVersion, ChangesetComment, Repository, RepoReviewRule)
54 PullRequestVersion, ChangesetComment, Repository, RepoReviewRule)
55 from rhodecode.model.meta import Session
55 from rhodecode.model.meta import Session
56 from rhodecode.model.notification import NotificationModel, \
56 from rhodecode.model.notification import NotificationModel, \
57 EmailNotificationModel
57 EmailNotificationModel
58 from rhodecode.model.scm import ScmModel
58 from rhodecode.model.scm import ScmModel
59 from rhodecode.model.settings import VcsSettingsModel
59 from rhodecode.model.settings import VcsSettingsModel
60
60
61
61
62 log = logging.getLogger(__name__)
62 log = logging.getLogger(__name__)
63
63
64
64
65 # Data structure to hold the response data when updating commits during a pull
65 # Data structure to hold the response data when updating commits during a pull
66 # request update.
66 # request update.
67 UpdateResponse = collections.namedtuple('UpdateResponse', [
67 UpdateResponse = collections.namedtuple('UpdateResponse', [
68 'executed', 'reason', 'new', 'old', 'changes',
68 'executed', 'reason', 'new', 'old', 'changes',
69 'source_changed', 'target_changed'])
69 'source_changed', 'target_changed'])
70
70
71
71
72 class PullRequestModel(BaseModel):
72 class PullRequestModel(BaseModel):
73
73
74 cls = PullRequest
74 cls = PullRequest
75
75
76 DIFF_CONTEXT = 3
76 DIFF_CONTEXT = 3
77
77
78 MERGE_STATUS_MESSAGES = {
78 MERGE_STATUS_MESSAGES = {
79 MergeFailureReason.NONE: lazy_ugettext(
79 MergeFailureReason.NONE: lazy_ugettext(
80 'This pull request can be automatically merged.'),
80 'This pull request can be automatically merged.'),
81 MergeFailureReason.UNKNOWN: lazy_ugettext(
81 MergeFailureReason.UNKNOWN: lazy_ugettext(
82 'This pull request cannot be merged because of an unhandled'
82 'This pull request cannot be merged because of an unhandled'
83 ' exception.'),
83 ' exception.'),
84 MergeFailureReason.MERGE_FAILED: lazy_ugettext(
84 MergeFailureReason.MERGE_FAILED: lazy_ugettext(
85 'This pull request cannot be merged because of merge conflicts.'),
85 'This pull request cannot be merged because of merge conflicts.'),
86 MergeFailureReason.PUSH_FAILED: lazy_ugettext(
86 MergeFailureReason.PUSH_FAILED: lazy_ugettext(
87 'This pull request could not be merged because push to target'
87 'This pull request could not be merged because push to target'
88 ' failed.'),
88 ' failed.'),
89 MergeFailureReason.TARGET_IS_NOT_HEAD: lazy_ugettext(
89 MergeFailureReason.TARGET_IS_NOT_HEAD: lazy_ugettext(
90 'This pull request cannot be merged because the target is not a'
90 'This pull request cannot be merged because the target is not a'
91 ' head.'),
91 ' head.'),
92 MergeFailureReason.HG_SOURCE_HAS_MORE_BRANCHES: lazy_ugettext(
92 MergeFailureReason.HG_SOURCE_HAS_MORE_BRANCHES: lazy_ugettext(
93 'This pull request cannot be merged because the source contains'
93 'This pull request cannot be merged because the source contains'
94 ' more branches than the target.'),
94 ' more branches than the target.'),
95 MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS: lazy_ugettext(
95 MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS: lazy_ugettext(
96 'This pull request cannot be merged because the target has'
96 'This pull request cannot be merged because the target has'
97 ' multiple heads.'),
97 ' multiple heads.'),
98 MergeFailureReason.TARGET_IS_LOCKED: lazy_ugettext(
98 MergeFailureReason.TARGET_IS_LOCKED: lazy_ugettext(
99 'This pull request cannot be merged because the target repository'
99 'This pull request cannot be merged because the target repository'
100 ' is locked.'),
100 ' is locked.'),
101 MergeFailureReason._DEPRECATED_MISSING_COMMIT: lazy_ugettext(
101 MergeFailureReason._DEPRECATED_MISSING_COMMIT: lazy_ugettext(
102 'This pull request cannot be merged because the target or the '
102 'This pull request cannot be merged because the target or the '
103 'source reference is missing.'),
103 'source reference is missing.'),
104 MergeFailureReason.MISSING_TARGET_REF: lazy_ugettext(
104 MergeFailureReason.MISSING_TARGET_REF: lazy_ugettext(
105 'This pull request cannot be merged because the target '
105 'This pull request cannot be merged because the target '
106 'reference is missing.'),
106 'reference is missing.'),
107 MergeFailureReason.MISSING_SOURCE_REF: lazy_ugettext(
107 MergeFailureReason.MISSING_SOURCE_REF: lazy_ugettext(
108 'This pull request cannot be merged because the source '
108 'This pull request cannot be merged because the source '
109 'reference is missing.'),
109 'reference is missing.'),
110 MergeFailureReason.SUBREPO_MERGE_FAILED: lazy_ugettext(
110 MergeFailureReason.SUBREPO_MERGE_FAILED: lazy_ugettext(
111 'This pull request cannot be merged because of conflicts related '
111 'This pull request cannot be merged because of conflicts related '
112 'to sub repositories.'),
112 'to sub repositories.'),
113 }
113 }
114
114
115 UPDATE_STATUS_MESSAGES = {
115 UPDATE_STATUS_MESSAGES = {
116 UpdateFailureReason.NONE: lazy_ugettext(
116 UpdateFailureReason.NONE: lazy_ugettext(
117 'Pull request update successful.'),
117 'Pull request update successful.'),
118 UpdateFailureReason.UNKNOWN: lazy_ugettext(
118 UpdateFailureReason.UNKNOWN: lazy_ugettext(
119 'Pull request update failed because of an unknown error.'),
119 'Pull request update failed because of an unknown error.'),
120 UpdateFailureReason.NO_CHANGE: lazy_ugettext(
120 UpdateFailureReason.NO_CHANGE: lazy_ugettext(
121 'No update needed because the source and target have not changed.'),
121 'No update needed because the source and target have not changed.'),
122 UpdateFailureReason.WRONG_REF_TYPE: lazy_ugettext(
122 UpdateFailureReason.WRONG_REF_TYPE: lazy_ugettext(
123 'Pull request cannot be updated because the reference type is '
123 'Pull request cannot be updated because the reference type is '
124 'not supported for an update. Only Branch, Tag or Bookmark is allowed.'),
124 'not supported for an update. Only Branch, Tag or Bookmark is allowed.'),
125 UpdateFailureReason.MISSING_TARGET_REF: lazy_ugettext(
125 UpdateFailureReason.MISSING_TARGET_REF: lazy_ugettext(
126 'This pull request cannot be updated because the target '
126 'This pull request cannot be updated because the target '
127 'reference is missing.'),
127 'reference is missing.'),
128 UpdateFailureReason.MISSING_SOURCE_REF: lazy_ugettext(
128 UpdateFailureReason.MISSING_SOURCE_REF: lazy_ugettext(
129 'This pull request cannot be updated because the source '
129 'This pull request cannot be updated because the source '
130 'reference is missing.'),
130 'reference is missing.'),
131 }
131 }
132
132
133 def __get_pull_request(self, pull_request):
133 def __get_pull_request(self, pull_request):
134 return self._get_instance((
134 return self._get_instance((
135 PullRequest, PullRequestVersion), pull_request)
135 PullRequest, PullRequestVersion), pull_request)
136
136
137 def _check_perms(self, perms, pull_request, user, api=False):
137 def _check_perms(self, perms, pull_request, user, api=False):
138 if not api:
138 if not api:
139 return h.HasRepoPermissionAny(*perms)(
139 return h.HasRepoPermissionAny(*perms)(
140 user=user, repo_name=pull_request.target_repo.repo_name)
140 user=user, repo_name=pull_request.target_repo.repo_name)
141 else:
141 else:
142 return h.HasRepoPermissionAnyApi(*perms)(
142 return h.HasRepoPermissionAnyApi(*perms)(
143 user=user, repo_name=pull_request.target_repo.repo_name)
143 user=user, repo_name=pull_request.target_repo.repo_name)
144
144
145 def check_user_read(self, pull_request, user, api=False):
145 def check_user_read(self, pull_request, user, api=False):
146 _perms = ('repository.admin', 'repository.write', 'repository.read',)
146 _perms = ('repository.admin', 'repository.write', 'repository.read',)
147 return self._check_perms(_perms, pull_request, user, api)
147 return self._check_perms(_perms, pull_request, user, api)
148
148
149 def check_user_merge(self, pull_request, user, api=False):
149 def check_user_merge(self, pull_request, user, api=False):
150 _perms = ('repository.admin', 'repository.write', 'hg.admin',)
150 _perms = ('repository.admin', 'repository.write', 'hg.admin',)
151 return self._check_perms(_perms, pull_request, user, api)
151 return self._check_perms(_perms, pull_request, user, api)
152
152
153 def check_user_update(self, pull_request, user, api=False):
153 def check_user_update(self, pull_request, user, api=False):
154 owner = user.user_id == pull_request.user_id
154 owner = user.user_id == pull_request.user_id
155 return self.check_user_merge(pull_request, user, api) or owner
155 return self.check_user_merge(pull_request, user, api) or owner
156
156
157 def check_user_delete(self, pull_request, user):
157 def check_user_delete(self, pull_request, user):
158 owner = user.user_id == pull_request.user_id
158 owner = user.user_id == pull_request.user_id
159 _perms = ('repository.admin',)
159 _perms = ('repository.admin',)
160 return self._check_perms(_perms, pull_request, user) or owner
160 return self._check_perms(_perms, pull_request, user) or owner
161
161
162 def check_user_change_status(self, pull_request, user, api=False):
162 def check_user_change_status(self, pull_request, user, api=False):
163 reviewer = user.user_id in [x.user_id for x in
163 reviewer = user.user_id in [x.user_id for x in
164 pull_request.reviewers]
164 pull_request.reviewers]
165 return self.check_user_update(pull_request, user, api) or reviewer
165 return self.check_user_update(pull_request, user, api) or reviewer
166
166
167 def check_user_comment(self, pull_request, user):
167 def check_user_comment(self, pull_request, user):
168 owner = user.user_id == pull_request.user_id
168 owner = user.user_id == pull_request.user_id
169 return self.check_user_read(pull_request, user) or owner
169 return self.check_user_read(pull_request, user) or owner
170
170
171 def get(self, pull_request):
171 def get(self, pull_request):
172 return self.__get_pull_request(pull_request)
172 return self.__get_pull_request(pull_request)
173
173
174 def _prepare_get_all_query(self, repo_name, source=False, statuses=None,
174 def _prepare_get_all_query(self, repo_name, source=False, statuses=None,
175 opened_by=None, order_by=None,
175 opened_by=None, order_by=None,
176 order_dir='desc'):
176 order_dir='desc'):
177 repo = None
177 repo = None
178 if repo_name:
178 if repo_name:
179 repo = self._get_repo(repo_name)
179 repo = self._get_repo(repo_name)
180
180
181 q = PullRequest.query()
181 q = PullRequest.query()
182
182
183 # source or target
183 # source or target
184 if repo and source:
184 if repo and source:
185 q = q.filter(PullRequest.source_repo == repo)
185 q = q.filter(PullRequest.source_repo == repo)
186 elif repo:
186 elif repo:
187 q = q.filter(PullRequest.target_repo == repo)
187 q = q.filter(PullRequest.target_repo == repo)
188
188
189 # closed,opened
189 # closed,opened
190 if statuses:
190 if statuses:
191 q = q.filter(PullRequest.status.in_(statuses))
191 q = q.filter(PullRequest.status.in_(statuses))
192
192
193 # opened by filter
193 # opened by filter
194 if opened_by:
194 if opened_by:
195 q = q.filter(PullRequest.user_id.in_(opened_by))
195 q = q.filter(PullRequest.user_id.in_(opened_by))
196
196
197 if order_by:
197 if order_by:
198 order_map = {
198 order_map = {
199 'name_raw': PullRequest.pull_request_id,
199 'name_raw': PullRequest.pull_request_id,
200 'title': PullRequest.title,
200 'title': PullRequest.title,
201 'updated_on_raw': PullRequest.updated_on,
201 'updated_on_raw': PullRequest.updated_on,
202 'target_repo': PullRequest.target_repo_id
202 'target_repo': PullRequest.target_repo_id
203 }
203 }
204 if order_dir == 'asc':
204 if order_dir == 'asc':
205 q = q.order_by(order_map[order_by].asc())
205 q = q.order_by(order_map[order_by].asc())
206 else:
206 else:
207 q = q.order_by(order_map[order_by].desc())
207 q = q.order_by(order_map[order_by].desc())
208
208
209 return q
209 return q
210
210
211 def count_all(self, repo_name, source=False, statuses=None,
211 def count_all(self, repo_name, source=False, statuses=None,
212 opened_by=None):
212 opened_by=None):
213 """
213 """
214 Count the number of pull requests for a specific repository.
214 Count the number of pull requests for a specific repository.
215
215
216 :param repo_name: target or source repo
216 :param repo_name: target or source repo
217 :param source: boolean flag to specify if repo_name refers to source
217 :param source: boolean flag to specify if repo_name refers to source
218 :param statuses: list of pull request statuses
218 :param statuses: list of pull request statuses
219 :param opened_by: author user of the pull request
219 :param opened_by: author user of the pull request
220 :returns: int number of pull requests
220 :returns: int number of pull requests
221 """
221 """
222 q = self._prepare_get_all_query(
222 q = self._prepare_get_all_query(
223 repo_name, source=source, statuses=statuses, opened_by=opened_by)
223 repo_name, source=source, statuses=statuses, opened_by=opened_by)
224
224
225 return q.count()
225 return q.count()
226
226
227 def get_all(self, repo_name, source=False, statuses=None, opened_by=None,
227 def get_all(self, repo_name, source=False, statuses=None, opened_by=None,
228 offset=0, length=None, order_by=None, order_dir='desc'):
228 offset=0, length=None, order_by=None, order_dir='desc'):
229 """
229 """
230 Get all pull requests for a specific repository.
230 Get all pull requests for a specific repository.
231
231
232 :param repo_name: target or source repo
232 :param repo_name: target or source repo
233 :param source: boolean flag to specify if repo_name refers to source
233 :param source: boolean flag to specify if repo_name refers to source
234 :param statuses: list of pull request statuses
234 :param statuses: list of pull request statuses
235 :param opened_by: author user of the pull request
235 :param opened_by: author user of the pull request
236 :param offset: pagination offset
236 :param offset: pagination offset
237 :param length: length of returned list
237 :param length: length of returned list
238 :param order_by: order of the returned list
238 :param order_by: order of the returned list
239 :param order_dir: 'asc' or 'desc' ordering direction
239 :param order_dir: 'asc' or 'desc' ordering direction
240 :returns: list of pull requests
240 :returns: list of pull requests
241 """
241 """
242 q = self._prepare_get_all_query(
242 q = self._prepare_get_all_query(
243 repo_name, source=source, statuses=statuses, opened_by=opened_by,
243 repo_name, source=source, statuses=statuses, opened_by=opened_by,
244 order_by=order_by, order_dir=order_dir)
244 order_by=order_by, order_dir=order_dir)
245
245
246 if length:
246 if length:
247 pull_requests = q.limit(length).offset(offset).all()
247 pull_requests = q.limit(length).offset(offset).all()
248 else:
248 else:
249 pull_requests = q.all()
249 pull_requests = q.all()
250
250
251 return pull_requests
251 return pull_requests
252
252
253 def count_awaiting_review(self, repo_name, source=False, statuses=None,
253 def count_awaiting_review(self, repo_name, source=False, statuses=None,
254 opened_by=None):
254 opened_by=None):
255 """
255 """
256 Count the number of pull requests for a specific repository that are
256 Count the number of pull requests for a specific repository that are
257 awaiting review.
257 awaiting review.
258
258
259 :param repo_name: target or source repo
259 :param repo_name: target or source repo
260 :param source: boolean flag to specify if repo_name refers to source
260 :param source: boolean flag to specify if repo_name refers to source
261 :param statuses: list of pull request statuses
261 :param statuses: list of pull request statuses
262 :param opened_by: author user of the pull request
262 :param opened_by: author user of the pull request
263 :returns: int number of pull requests
263 :returns: int number of pull requests
264 """
264 """
265 pull_requests = self.get_awaiting_review(
265 pull_requests = self.get_awaiting_review(
266 repo_name, source=source, statuses=statuses, opened_by=opened_by)
266 repo_name, source=source, statuses=statuses, opened_by=opened_by)
267
267
268 return len(pull_requests)
268 return len(pull_requests)
269
269
270 def get_awaiting_review(self, repo_name, source=False, statuses=None,
270 def get_awaiting_review(self, repo_name, source=False, statuses=None,
271 opened_by=None, offset=0, length=None,
271 opened_by=None, offset=0, length=None,
272 order_by=None, order_dir='desc'):
272 order_by=None, order_dir='desc'):
273 """
273 """
274 Get all pull requests for a specific repository that are awaiting
274 Get all pull requests for a specific repository that are awaiting
275 review.
275 review.
276
276
277 :param repo_name: target or source repo
277 :param repo_name: target or source repo
278 :param source: boolean flag to specify if repo_name refers to source
278 :param source: boolean flag to specify if repo_name refers to source
279 :param statuses: list of pull request statuses
279 :param statuses: list of pull request statuses
280 :param opened_by: author user of the pull request
280 :param opened_by: author user of the pull request
281 :param offset: pagination offset
281 :param offset: pagination offset
282 :param length: length of returned list
282 :param length: length of returned list
283 :param order_by: order of the returned list
283 :param order_by: order of the returned list
284 :param order_dir: 'asc' or 'desc' ordering direction
284 :param order_dir: 'asc' or 'desc' ordering direction
285 :returns: list of pull requests
285 :returns: list of pull requests
286 """
286 """
287 pull_requests = self.get_all(
287 pull_requests = self.get_all(
288 repo_name, source=source, statuses=statuses, opened_by=opened_by,
288 repo_name, source=source, statuses=statuses, opened_by=opened_by,
289 order_by=order_by, order_dir=order_dir)
289 order_by=order_by, order_dir=order_dir)
290
290
291 _filtered_pull_requests = []
291 _filtered_pull_requests = []
292 for pr in pull_requests:
292 for pr in pull_requests:
293 status = pr.calculated_review_status()
293 status = pr.calculated_review_status()
294 if status in [ChangesetStatus.STATUS_NOT_REVIEWED,
294 if status in [ChangesetStatus.STATUS_NOT_REVIEWED,
295 ChangesetStatus.STATUS_UNDER_REVIEW]:
295 ChangesetStatus.STATUS_UNDER_REVIEW]:
296 _filtered_pull_requests.append(pr)
296 _filtered_pull_requests.append(pr)
297 if length:
297 if length:
298 return _filtered_pull_requests[offset:offset+length]
298 return _filtered_pull_requests[offset:offset+length]
299 else:
299 else:
300 return _filtered_pull_requests
300 return _filtered_pull_requests
301
301
302 def count_awaiting_my_review(self, repo_name, source=False, statuses=None,
302 def count_awaiting_my_review(self, repo_name, source=False, statuses=None,
303 opened_by=None, user_id=None):
303 opened_by=None, user_id=None):
304 """
304 """
305 Count the number of pull requests for a specific repository that are
305 Count the number of pull requests for a specific repository that are
306 awaiting review from a specific user.
306 awaiting review from a specific user.
307
307
308 :param repo_name: target or source repo
308 :param repo_name: target or source repo
309 :param source: boolean flag to specify if repo_name refers to source
309 :param source: boolean flag to specify if repo_name refers to source
310 :param statuses: list of pull request statuses
310 :param statuses: list of pull request statuses
311 :param opened_by: author user of the pull request
311 :param opened_by: author user of the pull request
312 :param user_id: reviewer user of the pull request
312 :param user_id: reviewer user of the pull request
313 :returns: int number of pull requests
313 :returns: int number of pull requests
314 """
314 """
315 pull_requests = self.get_awaiting_my_review(
315 pull_requests = self.get_awaiting_my_review(
316 repo_name, source=source, statuses=statuses, opened_by=opened_by,
316 repo_name, source=source, statuses=statuses, opened_by=opened_by,
317 user_id=user_id)
317 user_id=user_id)
318
318
319 return len(pull_requests)
319 return len(pull_requests)
320
320
321 def get_awaiting_my_review(self, repo_name, source=False, statuses=None,
321 def get_awaiting_my_review(self, repo_name, source=False, statuses=None,
322 opened_by=None, user_id=None, offset=0,
322 opened_by=None, user_id=None, offset=0,
323 length=None, order_by=None, order_dir='desc'):
323 length=None, order_by=None, order_dir='desc'):
324 """
324 """
325 Get all pull requests for a specific repository that are awaiting
325 Get all pull requests for a specific repository that are awaiting
326 review from a specific user.
326 review from a specific user.
327
327
328 :param repo_name: target or source repo
328 :param repo_name: target or source repo
329 :param source: boolean flag to specify if repo_name refers to source
329 :param source: boolean flag to specify if repo_name refers to source
330 :param statuses: list of pull request statuses
330 :param statuses: list of pull request statuses
331 :param opened_by: author user of the pull request
331 :param opened_by: author user of the pull request
332 :param user_id: reviewer user of the pull request
332 :param user_id: reviewer user of the pull request
333 :param offset: pagination offset
333 :param offset: pagination offset
334 :param length: length of returned list
334 :param length: length of returned list
335 :param order_by: order of the returned list
335 :param order_by: order of the returned list
336 :param order_dir: 'asc' or 'desc' ordering direction
336 :param order_dir: 'asc' or 'desc' ordering direction
337 :returns: list of pull requests
337 :returns: list of pull requests
338 """
338 """
339 pull_requests = self.get_all(
339 pull_requests = self.get_all(
340 repo_name, source=source, statuses=statuses, opened_by=opened_by,
340 repo_name, source=source, statuses=statuses, opened_by=opened_by,
341 order_by=order_by, order_dir=order_dir)
341 order_by=order_by, order_dir=order_dir)
342
342
343 _my = PullRequestModel().get_not_reviewed(user_id)
343 _my = PullRequestModel().get_not_reviewed(user_id)
344 my_participation = []
344 my_participation = []
345 for pr in pull_requests:
345 for pr in pull_requests:
346 if pr in _my:
346 if pr in _my:
347 my_participation.append(pr)
347 my_participation.append(pr)
348 _filtered_pull_requests = my_participation
348 _filtered_pull_requests = my_participation
349 if length:
349 if length:
350 return _filtered_pull_requests[offset:offset+length]
350 return _filtered_pull_requests[offset:offset+length]
351 else:
351 else:
352 return _filtered_pull_requests
352 return _filtered_pull_requests
353
353
354 def get_not_reviewed(self, user_id):
354 def get_not_reviewed(self, user_id):
355 return [
355 return [
356 x.pull_request for x in PullRequestReviewers.query().filter(
356 x.pull_request for x in PullRequestReviewers.query().filter(
357 PullRequestReviewers.user_id == user_id).all()
357 PullRequestReviewers.user_id == user_id).all()
358 ]
358 ]
359
359
360 def _prepare_participating_query(self, user_id=None, statuses=None,
360 def _prepare_participating_query(self, user_id=None, statuses=None,
361 order_by=None, order_dir='desc'):
361 order_by=None, order_dir='desc'):
362 q = PullRequest.query()
362 q = PullRequest.query()
363 if user_id:
363 if user_id:
364 reviewers_subquery = Session().query(
364 reviewers_subquery = Session().query(
365 PullRequestReviewers.pull_request_id).filter(
365 PullRequestReviewers.pull_request_id).filter(
366 PullRequestReviewers.user_id == user_id).subquery()
366 PullRequestReviewers.user_id == user_id).subquery()
367 user_filter = or_(
367 user_filter = or_(
368 PullRequest.user_id == user_id,
368 PullRequest.user_id == user_id,
369 PullRequest.pull_request_id.in_(reviewers_subquery)
369 PullRequest.pull_request_id.in_(reviewers_subquery)
370 )
370 )
371 q = PullRequest.query().filter(user_filter)
371 q = PullRequest.query().filter(user_filter)
372
372
373 # closed,opened
373 # closed,opened
374 if statuses:
374 if statuses:
375 q = q.filter(PullRequest.status.in_(statuses))
375 q = q.filter(PullRequest.status.in_(statuses))
376
376
377 if order_by:
377 if order_by:
378 order_map = {
378 order_map = {
379 'name_raw': PullRequest.pull_request_id,
379 'name_raw': PullRequest.pull_request_id,
380 'title': PullRequest.title,
380 'title': PullRequest.title,
381 'updated_on_raw': PullRequest.updated_on,
381 'updated_on_raw': PullRequest.updated_on,
382 'target_repo': PullRequest.target_repo_id
382 'target_repo': PullRequest.target_repo_id
383 }
383 }
384 if order_dir == 'asc':
384 if order_dir == 'asc':
385 q = q.order_by(order_map[order_by].asc())
385 q = q.order_by(order_map[order_by].asc())
386 else:
386 else:
387 q = q.order_by(order_map[order_by].desc())
387 q = q.order_by(order_map[order_by].desc())
388
388
389 return q
389 return q
390
390
391 def count_im_participating_in(self, user_id=None, statuses=None):
391 def count_im_participating_in(self, user_id=None, statuses=None):
392 q = self._prepare_participating_query(user_id, statuses=statuses)
392 q = self._prepare_participating_query(user_id, statuses=statuses)
393 return q.count()
393 return q.count()
394
394
395 def get_im_participating_in(
395 def get_im_participating_in(
396 self, user_id=None, statuses=None, offset=0,
396 self, user_id=None, statuses=None, offset=0,
397 length=None, order_by=None, order_dir='desc'):
397 length=None, order_by=None, order_dir='desc'):
398 """
398 """
399 Get all Pull requests that i'm participating in, or i have opened
399 Get all Pull requests that i'm participating in, or i have opened
400 """
400 """
401
401
402 q = self._prepare_participating_query(
402 q = self._prepare_participating_query(
403 user_id, statuses=statuses, order_by=order_by,
403 user_id, statuses=statuses, order_by=order_by,
404 order_dir=order_dir)
404 order_dir=order_dir)
405
405
406 if length:
406 if length:
407 pull_requests = q.limit(length).offset(offset).all()
407 pull_requests = q.limit(length).offset(offset).all()
408 else:
408 else:
409 pull_requests = q.all()
409 pull_requests = q.all()
410
410
411 return pull_requests
411 return pull_requests
412
412
413 def get_versions(self, pull_request):
413 def get_versions(self, pull_request):
414 """
414 """
415 returns version of pull request sorted by ID descending
415 returns version of pull request sorted by ID descending
416 """
416 """
417 return PullRequestVersion.query()\
417 return PullRequestVersion.query()\
418 .filter(PullRequestVersion.pull_request == pull_request)\
418 .filter(PullRequestVersion.pull_request == pull_request)\
419 .order_by(PullRequestVersion.pull_request_version_id.asc())\
419 .order_by(PullRequestVersion.pull_request_version_id.asc())\
420 .all()
420 .all()
421
421
422 def get_pr_version(self, pull_request_id, version=None):
422 def get_pr_version(self, pull_request_id, version=None):
423 at_version = None
423 at_version = None
424
424
425 if version and version == 'latest':
425 if version and version == 'latest':
426 pull_request_ver = PullRequest.get(pull_request_id)
426 pull_request_ver = PullRequest.get(pull_request_id)
427 pull_request_obj = pull_request_ver
427 pull_request_obj = pull_request_ver
428 _org_pull_request_obj = pull_request_obj
428 _org_pull_request_obj = pull_request_obj
429 at_version = 'latest'
429 at_version = 'latest'
430 elif version:
430 elif version:
431 pull_request_ver = PullRequestVersion.get_or_404(version)
431 pull_request_ver = PullRequestVersion.get_or_404(version)
432 pull_request_obj = pull_request_ver
432 pull_request_obj = pull_request_ver
433 _org_pull_request_obj = pull_request_ver.pull_request
433 _org_pull_request_obj = pull_request_ver.pull_request
434 at_version = pull_request_ver.pull_request_version_id
434 at_version = pull_request_ver.pull_request_version_id
435 else:
435 else:
436 _org_pull_request_obj = pull_request_obj = PullRequest.get_or_404(
436 _org_pull_request_obj = pull_request_obj = PullRequest.get_or_404(
437 pull_request_id)
437 pull_request_id)
438
438
439 pull_request_display_obj = PullRequest.get_pr_display_object(
439 pull_request_display_obj = PullRequest.get_pr_display_object(
440 pull_request_obj, _org_pull_request_obj)
440 pull_request_obj, _org_pull_request_obj)
441
441
442 return _org_pull_request_obj, pull_request_obj, \
442 return _org_pull_request_obj, pull_request_obj, \
443 pull_request_display_obj, at_version
443 pull_request_display_obj, at_version
444
444
445 def create(self, created_by, source_repo, source_ref, target_repo,
445 def create(self, created_by, source_repo, source_ref, target_repo,
446 target_ref, revisions, reviewers, title, description=None,
446 target_ref, revisions, reviewers, title, description=None,
447 description_renderer=None,
447 reviewer_data=None, translator=None, auth_user=None):
448 reviewer_data=None, translator=None, auth_user=None):
448 translator = translator or get_current_request().translate
449 translator = translator or get_current_request().translate
449
450
450 created_by_user = self._get_user(created_by)
451 created_by_user = self._get_user(created_by)
451 auth_user = auth_user or created_by_user
452 auth_user = auth_user or created_by_user
452 source_repo = self._get_repo(source_repo)
453 source_repo = self._get_repo(source_repo)
453 target_repo = self._get_repo(target_repo)
454 target_repo = self._get_repo(target_repo)
454
455
455 pull_request = PullRequest()
456 pull_request = PullRequest()
456 pull_request.source_repo = source_repo
457 pull_request.source_repo = source_repo
457 pull_request.source_ref = source_ref
458 pull_request.source_ref = source_ref
458 pull_request.target_repo = target_repo
459 pull_request.target_repo = target_repo
459 pull_request.target_ref = target_ref
460 pull_request.target_ref = target_ref
460 pull_request.revisions = revisions
461 pull_request.revisions = revisions
461 pull_request.title = title
462 pull_request.title = title
462 pull_request.description = description
463 pull_request.description = description
464 pull_request.description_renderer = description_renderer
463 pull_request.author = created_by_user
465 pull_request.author = created_by_user
464 pull_request.reviewer_data = reviewer_data
466 pull_request.reviewer_data = reviewer_data
465
467
466 Session().add(pull_request)
468 Session().add(pull_request)
467 Session().flush()
469 Session().flush()
468
470
469 reviewer_ids = set()
471 reviewer_ids = set()
470 # members / reviewers
472 # members / reviewers
471 for reviewer_object in reviewers:
473 for reviewer_object in reviewers:
472 user_id, reasons, mandatory, rules = reviewer_object
474 user_id, reasons, mandatory, rules = reviewer_object
473 user = self._get_user(user_id)
475 user = self._get_user(user_id)
474
476
475 # skip duplicates
477 # skip duplicates
476 if user.user_id in reviewer_ids:
478 if user.user_id in reviewer_ids:
477 continue
479 continue
478
480
479 reviewer_ids.add(user.user_id)
481 reviewer_ids.add(user.user_id)
480
482
481 reviewer = PullRequestReviewers()
483 reviewer = PullRequestReviewers()
482 reviewer.user = user
484 reviewer.user = user
483 reviewer.pull_request = pull_request
485 reviewer.pull_request = pull_request
484 reviewer.reasons = reasons
486 reviewer.reasons = reasons
485 reviewer.mandatory = mandatory
487 reviewer.mandatory = mandatory
486
488
487 # NOTE(marcink): pick only first rule for now
489 # NOTE(marcink): pick only first rule for now
488 rule_id = list(rules)[0] if rules else None
490 rule_id = list(rules)[0] if rules else None
489 rule = RepoReviewRule.get(rule_id) if rule_id else None
491 rule = RepoReviewRule.get(rule_id) if rule_id else None
490 if rule:
492 if rule:
491 review_group = rule.user_group_vote_rule()
493 review_group = rule.user_group_vote_rule()
492 if review_group:
494 if review_group:
493 # NOTE(marcink):
495 # NOTE(marcink):
494 # again, can be that user is member of more,
496 # again, can be that user is member of more,
495 # but we pick the first same, as default reviewers algo
497 # but we pick the first same, as default reviewers algo
496 review_group = review_group[0]
498 review_group = review_group[0]
497
499
498 rule_data = {
500 rule_data = {
499 'rule_name':
501 'rule_name':
500 rule.review_rule_name,
502 rule.review_rule_name,
501 'rule_user_group_entry_id':
503 'rule_user_group_entry_id':
502 review_group.repo_review_rule_users_group_id,
504 review_group.repo_review_rule_users_group_id,
503 'rule_user_group_name':
505 'rule_user_group_name':
504 review_group.users_group.users_group_name,
506 review_group.users_group.users_group_name,
505 'rule_user_group_members':
507 'rule_user_group_members':
506 [x.user.username for x in review_group.users_group.members],
508 [x.user.username for x in review_group.users_group.members],
507 }
509 }
508 # e.g {'vote_rule': -1, 'mandatory': True}
510 # e.g {'vote_rule': -1, 'mandatory': True}
509 rule_data.update(review_group.rule_data())
511 rule_data.update(review_group.rule_data())
510
512
511 reviewer.rule_data = rule_data
513 reviewer.rule_data = rule_data
512
514
513 Session().add(reviewer)
515 Session().add(reviewer)
514 Session().flush()
516 Session().flush()
515
517
516 # Set approval status to "Under Review" for all commits which are
518 # Set approval status to "Under Review" for all commits which are
517 # part of this pull request.
519 # part of this pull request.
518 ChangesetStatusModel().set_status(
520 ChangesetStatusModel().set_status(
519 repo=target_repo,
521 repo=target_repo,
520 status=ChangesetStatus.STATUS_UNDER_REVIEW,
522 status=ChangesetStatus.STATUS_UNDER_REVIEW,
521 user=created_by_user,
523 user=created_by_user,
522 pull_request=pull_request
524 pull_request=pull_request
523 )
525 )
524 # we commit early at this point. This has to do with a fact
526 # we commit early at this point. This has to do with a fact
525 # that before queries do some row-locking. And because of that
527 # that before queries do some row-locking. And because of that
526 # we need to commit and finish transation before below validate call
528 # we need to commit and finish transation before below validate call
527 # that for large repos could be long resulting in long row locks
529 # that for large repos could be long resulting in long row locks
528 Session().commit()
530 Session().commit()
529
531
530 # prepare workspace, and run initial merge simulation
532 # prepare workspace, and run initial merge simulation
531 MergeCheck.validate(
533 MergeCheck.validate(
532 pull_request, user=created_by_user, translator=translator)
534 pull_request, user=created_by_user, translator=translator)
533
535
534 self.notify_reviewers(pull_request, reviewer_ids)
536 self.notify_reviewers(pull_request, reviewer_ids)
535 self._trigger_pull_request_hook(
537 self._trigger_pull_request_hook(
536 pull_request, created_by_user, 'create')
538 pull_request, created_by_user, 'create')
537
539
538 creation_data = pull_request.get_api_data(with_merge_state=False)
540 creation_data = pull_request.get_api_data(with_merge_state=False)
539 self._log_audit_action(
541 self._log_audit_action(
540 'repo.pull_request.create', {'data': creation_data},
542 'repo.pull_request.create', {'data': creation_data},
541 auth_user, pull_request)
543 auth_user, pull_request)
542
544
543 return pull_request
545 return pull_request
544
546
545 def _trigger_pull_request_hook(self, pull_request, user, action):
547 def _trigger_pull_request_hook(self, pull_request, user, action):
546 pull_request = self.__get_pull_request(pull_request)
548 pull_request = self.__get_pull_request(pull_request)
547 target_scm = pull_request.target_repo.scm_instance()
549 target_scm = pull_request.target_repo.scm_instance()
548 if action == 'create':
550 if action == 'create':
549 trigger_hook = hooks_utils.trigger_log_create_pull_request_hook
551 trigger_hook = hooks_utils.trigger_log_create_pull_request_hook
550 elif action == 'merge':
552 elif action == 'merge':
551 trigger_hook = hooks_utils.trigger_log_merge_pull_request_hook
553 trigger_hook = hooks_utils.trigger_log_merge_pull_request_hook
552 elif action == 'close':
554 elif action == 'close':
553 trigger_hook = hooks_utils.trigger_log_close_pull_request_hook
555 trigger_hook = hooks_utils.trigger_log_close_pull_request_hook
554 elif action == 'review_status_change':
556 elif action == 'review_status_change':
555 trigger_hook = hooks_utils.trigger_log_review_pull_request_hook
557 trigger_hook = hooks_utils.trigger_log_review_pull_request_hook
556 elif action == 'update':
558 elif action == 'update':
557 trigger_hook = hooks_utils.trigger_log_update_pull_request_hook
559 trigger_hook = hooks_utils.trigger_log_update_pull_request_hook
558 else:
560 else:
559 return
561 return
560
562
561 trigger_hook(
563 trigger_hook(
562 username=user.username,
564 username=user.username,
563 repo_name=pull_request.target_repo.repo_name,
565 repo_name=pull_request.target_repo.repo_name,
564 repo_alias=target_scm.alias,
566 repo_alias=target_scm.alias,
565 pull_request=pull_request)
567 pull_request=pull_request)
566
568
567 def _get_commit_ids(self, pull_request):
569 def _get_commit_ids(self, pull_request):
568 """
570 """
569 Return the commit ids of the merged pull request.
571 Return the commit ids of the merged pull request.
570
572
571 This method is not dealing correctly yet with the lack of autoupdates
573 This method is not dealing correctly yet with the lack of autoupdates
572 nor with the implicit target updates.
574 nor with the implicit target updates.
573 For example: if a commit in the source repo is already in the target it
575 For example: if a commit in the source repo is already in the target it
574 will be reported anyways.
576 will be reported anyways.
575 """
577 """
576 merge_rev = pull_request.merge_rev
578 merge_rev = pull_request.merge_rev
577 if merge_rev is None:
579 if merge_rev is None:
578 raise ValueError('This pull request was not merged yet')
580 raise ValueError('This pull request was not merged yet')
579
581
580 commit_ids = list(pull_request.revisions)
582 commit_ids = list(pull_request.revisions)
581 if merge_rev not in commit_ids:
583 if merge_rev not in commit_ids:
582 commit_ids.append(merge_rev)
584 commit_ids.append(merge_rev)
583
585
584 return commit_ids
586 return commit_ids
585
587
586 def merge_repo(self, pull_request, user, extras):
588 def merge_repo(self, pull_request, user, extras):
587 log.debug("Merging pull request %s", pull_request.pull_request_id)
589 log.debug("Merging pull request %s", pull_request.pull_request_id)
588 merge_state = self._merge_pull_request(pull_request, user, extras)
590 merge_state = self._merge_pull_request(pull_request, user, extras)
589 if merge_state.executed:
591 if merge_state.executed:
590 log.debug(
592 log.debug(
591 "Merge was successful, updating the pull request comments.")
593 "Merge was successful, updating the pull request comments.")
592 self._comment_and_close_pr(pull_request, user, merge_state)
594 self._comment_and_close_pr(pull_request, user, merge_state)
593
595
594 self._log_audit_action(
596 self._log_audit_action(
595 'repo.pull_request.merge',
597 'repo.pull_request.merge',
596 {'merge_state': merge_state.__dict__},
598 {'merge_state': merge_state.__dict__},
597 user, pull_request)
599 user, pull_request)
598
600
599 else:
601 else:
600 log.warn("Merge failed, not updating the pull request.")
602 log.warn("Merge failed, not updating the pull request.")
601 return merge_state
603 return merge_state
602
604
603 def _merge_pull_request(self, pull_request, user, extras, merge_msg=None):
605 def _merge_pull_request(self, pull_request, user, extras, merge_msg=None):
604 target_vcs = pull_request.target_repo.scm_instance()
606 target_vcs = pull_request.target_repo.scm_instance()
605 source_vcs = pull_request.source_repo.scm_instance()
607 source_vcs = pull_request.source_repo.scm_instance()
606 target_ref = self._refresh_reference(
608 target_ref = self._refresh_reference(
607 pull_request.target_ref_parts, target_vcs)
609 pull_request.target_ref_parts, target_vcs)
608
610
609 message = merge_msg or (
611 message = merge_msg or (
610 'Merge pull request #%(pr_id)s from '
612 'Merge pull request #%(pr_id)s from '
611 '%(source_repo)s %(source_ref_name)s\n\n %(pr_title)s') % {
613 '%(source_repo)s %(source_ref_name)s\n\n %(pr_title)s') % {
612 'pr_id': pull_request.pull_request_id,
614 'pr_id': pull_request.pull_request_id,
613 'source_repo': source_vcs.name,
615 'source_repo': source_vcs.name,
614 'source_ref_name': pull_request.source_ref_parts.name,
616 'source_ref_name': pull_request.source_ref_parts.name,
615 'pr_title': pull_request.title
617 'pr_title': pull_request.title
616 }
618 }
617
619
618 workspace_id = self._workspace_id(pull_request)
620 workspace_id = self._workspace_id(pull_request)
619 repo_id = pull_request.target_repo.repo_id
621 repo_id = pull_request.target_repo.repo_id
620 use_rebase = self._use_rebase_for_merging(pull_request)
622 use_rebase = self._use_rebase_for_merging(pull_request)
621 close_branch = self._close_branch_before_merging(pull_request)
623 close_branch = self._close_branch_before_merging(pull_request)
622
624
623 callback_daemon, extras = prepare_callback_daemon(
625 callback_daemon, extras = prepare_callback_daemon(
624 extras, protocol=vcs_settings.HOOKS_PROTOCOL,
626 extras, protocol=vcs_settings.HOOKS_PROTOCOL,
625 host=vcs_settings.HOOKS_HOST,
627 host=vcs_settings.HOOKS_HOST,
626 use_direct_calls=vcs_settings.HOOKS_DIRECT_CALLS)
628 use_direct_calls=vcs_settings.HOOKS_DIRECT_CALLS)
627
629
628 with callback_daemon:
630 with callback_daemon:
629 # TODO: johbo: Implement a clean way to run a config_override
631 # TODO: johbo: Implement a clean way to run a config_override
630 # for a single call.
632 # for a single call.
631 target_vcs.config.set(
633 target_vcs.config.set(
632 'rhodecode', 'RC_SCM_DATA', json.dumps(extras))
634 'rhodecode', 'RC_SCM_DATA', json.dumps(extras))
633 merge_state = target_vcs.merge(
635 merge_state = target_vcs.merge(
634 repo_id, workspace_id, target_ref, source_vcs,
636 repo_id, workspace_id, target_ref, source_vcs,
635 pull_request.source_ref_parts,
637 pull_request.source_ref_parts,
636 user_name=user.username, user_email=user.email,
638 user_name=user.username, user_email=user.email,
637 message=message, use_rebase=use_rebase,
639 message=message, use_rebase=use_rebase,
638 close_branch=close_branch)
640 close_branch=close_branch)
639 return merge_state
641 return merge_state
640
642
641 def _comment_and_close_pr(self, pull_request, user, merge_state, close_msg=None):
643 def _comment_and_close_pr(self, pull_request, user, merge_state, close_msg=None):
642 pull_request.merge_rev = merge_state.merge_ref.commit_id
644 pull_request.merge_rev = merge_state.merge_ref.commit_id
643 pull_request.updated_on = datetime.datetime.now()
645 pull_request.updated_on = datetime.datetime.now()
644 close_msg = close_msg or 'Pull request merged and closed'
646 close_msg = close_msg or 'Pull request merged and closed'
645
647
646 CommentsModel().create(
648 CommentsModel().create(
647 text=safe_unicode(close_msg),
649 text=safe_unicode(close_msg),
648 repo=pull_request.target_repo.repo_id,
650 repo=pull_request.target_repo.repo_id,
649 user=user.user_id,
651 user=user.user_id,
650 pull_request=pull_request.pull_request_id,
652 pull_request=pull_request.pull_request_id,
651 f_path=None,
653 f_path=None,
652 line_no=None,
654 line_no=None,
653 closing_pr=True
655 closing_pr=True
654 )
656 )
655
657
656 Session().add(pull_request)
658 Session().add(pull_request)
657 Session().flush()
659 Session().flush()
658 # TODO: paris: replace invalidation with less radical solution
660 # TODO: paris: replace invalidation with less radical solution
659 ScmModel().mark_for_invalidation(
661 ScmModel().mark_for_invalidation(
660 pull_request.target_repo.repo_name)
662 pull_request.target_repo.repo_name)
661 self._trigger_pull_request_hook(pull_request, user, 'merge')
663 self._trigger_pull_request_hook(pull_request, user, 'merge')
662
664
663 def has_valid_update_type(self, pull_request):
665 def has_valid_update_type(self, pull_request):
664 source_ref_type = pull_request.source_ref_parts.type
666 source_ref_type = pull_request.source_ref_parts.type
665 return source_ref_type in ['book', 'branch', 'tag']
667 return source_ref_type in ['book', 'branch', 'tag']
666
668
667 def update_commits(self, pull_request):
669 def update_commits(self, pull_request):
668 """
670 """
669 Get the updated list of commits for the pull request
671 Get the updated list of commits for the pull request
670 and return the new pull request version and the list
672 and return the new pull request version and the list
671 of commits processed by this update action
673 of commits processed by this update action
672 """
674 """
673 pull_request = self.__get_pull_request(pull_request)
675 pull_request = self.__get_pull_request(pull_request)
674 source_ref_type = pull_request.source_ref_parts.type
676 source_ref_type = pull_request.source_ref_parts.type
675 source_ref_name = pull_request.source_ref_parts.name
677 source_ref_name = pull_request.source_ref_parts.name
676 source_ref_id = pull_request.source_ref_parts.commit_id
678 source_ref_id = pull_request.source_ref_parts.commit_id
677
679
678 target_ref_type = pull_request.target_ref_parts.type
680 target_ref_type = pull_request.target_ref_parts.type
679 target_ref_name = pull_request.target_ref_parts.name
681 target_ref_name = pull_request.target_ref_parts.name
680 target_ref_id = pull_request.target_ref_parts.commit_id
682 target_ref_id = pull_request.target_ref_parts.commit_id
681
683
682 if not self.has_valid_update_type(pull_request):
684 if not self.has_valid_update_type(pull_request):
683 log.debug(
685 log.debug(
684 "Skipping update of pull request %s due to ref type: %s",
686 "Skipping update of pull request %s due to ref type: %s",
685 pull_request, source_ref_type)
687 pull_request, source_ref_type)
686 return UpdateResponse(
688 return UpdateResponse(
687 executed=False,
689 executed=False,
688 reason=UpdateFailureReason.WRONG_REF_TYPE,
690 reason=UpdateFailureReason.WRONG_REF_TYPE,
689 old=pull_request, new=None, changes=None,
691 old=pull_request, new=None, changes=None,
690 source_changed=False, target_changed=False)
692 source_changed=False, target_changed=False)
691
693
692 # source repo
694 # source repo
693 source_repo = pull_request.source_repo.scm_instance()
695 source_repo = pull_request.source_repo.scm_instance()
694 try:
696 try:
695 source_commit = source_repo.get_commit(commit_id=source_ref_name)
697 source_commit = source_repo.get_commit(commit_id=source_ref_name)
696 except CommitDoesNotExistError:
698 except CommitDoesNotExistError:
697 return UpdateResponse(
699 return UpdateResponse(
698 executed=False,
700 executed=False,
699 reason=UpdateFailureReason.MISSING_SOURCE_REF,
701 reason=UpdateFailureReason.MISSING_SOURCE_REF,
700 old=pull_request, new=None, changes=None,
702 old=pull_request, new=None, changes=None,
701 source_changed=False, target_changed=False)
703 source_changed=False, target_changed=False)
702
704
703 source_changed = source_ref_id != source_commit.raw_id
705 source_changed = source_ref_id != source_commit.raw_id
704
706
705 # target repo
707 # target repo
706 target_repo = pull_request.target_repo.scm_instance()
708 target_repo = pull_request.target_repo.scm_instance()
707 try:
709 try:
708 target_commit = target_repo.get_commit(commit_id=target_ref_name)
710 target_commit = target_repo.get_commit(commit_id=target_ref_name)
709 except CommitDoesNotExistError:
711 except CommitDoesNotExistError:
710 return UpdateResponse(
712 return UpdateResponse(
711 executed=False,
713 executed=False,
712 reason=UpdateFailureReason.MISSING_TARGET_REF,
714 reason=UpdateFailureReason.MISSING_TARGET_REF,
713 old=pull_request, new=None, changes=None,
715 old=pull_request, new=None, changes=None,
714 source_changed=False, target_changed=False)
716 source_changed=False, target_changed=False)
715 target_changed = target_ref_id != target_commit.raw_id
717 target_changed = target_ref_id != target_commit.raw_id
716
718
717 if not (source_changed or target_changed):
719 if not (source_changed or target_changed):
718 log.debug("Nothing changed in pull request %s", pull_request)
720 log.debug("Nothing changed in pull request %s", pull_request)
719 return UpdateResponse(
721 return UpdateResponse(
720 executed=False,
722 executed=False,
721 reason=UpdateFailureReason.NO_CHANGE,
723 reason=UpdateFailureReason.NO_CHANGE,
722 old=pull_request, new=None, changes=None,
724 old=pull_request, new=None, changes=None,
723 source_changed=target_changed, target_changed=source_changed)
725 source_changed=target_changed, target_changed=source_changed)
724
726
725 change_in_found = 'target repo' if target_changed else 'source repo'
727 change_in_found = 'target repo' if target_changed else 'source repo'
726 log.debug('Updating pull request because of change in %s detected',
728 log.debug('Updating pull request because of change in %s detected',
727 change_in_found)
729 change_in_found)
728
730
729 # Finally there is a need for an update, in case of source change
731 # Finally there is a need for an update, in case of source change
730 # we create a new version, else just an update
732 # we create a new version, else just an update
731 if source_changed:
733 if source_changed:
732 pull_request_version = self._create_version_from_snapshot(pull_request)
734 pull_request_version = self._create_version_from_snapshot(pull_request)
733 self._link_comments_to_version(pull_request_version)
735 self._link_comments_to_version(pull_request_version)
734 else:
736 else:
735 try:
737 try:
736 ver = pull_request.versions[-1]
738 ver = pull_request.versions[-1]
737 except IndexError:
739 except IndexError:
738 ver = None
740 ver = None
739
741
740 pull_request.pull_request_version_id = \
742 pull_request.pull_request_version_id = \
741 ver.pull_request_version_id if ver else None
743 ver.pull_request_version_id if ver else None
742 pull_request_version = pull_request
744 pull_request_version = pull_request
743
745
744 try:
746 try:
745 if target_ref_type in ('tag', 'branch', 'book'):
747 if target_ref_type in ('tag', 'branch', 'book'):
746 target_commit = target_repo.get_commit(target_ref_name)
748 target_commit = target_repo.get_commit(target_ref_name)
747 else:
749 else:
748 target_commit = target_repo.get_commit(target_ref_id)
750 target_commit = target_repo.get_commit(target_ref_id)
749 except CommitDoesNotExistError:
751 except CommitDoesNotExistError:
750 return UpdateResponse(
752 return UpdateResponse(
751 executed=False,
753 executed=False,
752 reason=UpdateFailureReason.MISSING_TARGET_REF,
754 reason=UpdateFailureReason.MISSING_TARGET_REF,
753 old=pull_request, new=None, changes=None,
755 old=pull_request, new=None, changes=None,
754 source_changed=source_changed, target_changed=target_changed)
756 source_changed=source_changed, target_changed=target_changed)
755
757
756 # re-compute commit ids
758 # re-compute commit ids
757 old_commit_ids = pull_request.revisions
759 old_commit_ids = pull_request.revisions
758 pre_load = ["author", "branch", "date", "message"]
760 pre_load = ["author", "branch", "date", "message"]
759 commit_ranges = target_repo.compare(
761 commit_ranges = target_repo.compare(
760 target_commit.raw_id, source_commit.raw_id, source_repo, merge=True,
762 target_commit.raw_id, source_commit.raw_id, source_repo, merge=True,
761 pre_load=pre_load)
763 pre_load=pre_load)
762
764
763 ancestor = target_repo.get_common_ancestor(
765 ancestor = target_repo.get_common_ancestor(
764 target_commit.raw_id, source_commit.raw_id, source_repo)
766 target_commit.raw_id, source_commit.raw_id, source_repo)
765
767
766 pull_request.source_ref = '%s:%s:%s' % (
768 pull_request.source_ref = '%s:%s:%s' % (
767 source_ref_type, source_ref_name, source_commit.raw_id)
769 source_ref_type, source_ref_name, source_commit.raw_id)
768 pull_request.target_ref = '%s:%s:%s' % (
770 pull_request.target_ref = '%s:%s:%s' % (
769 target_ref_type, target_ref_name, ancestor)
771 target_ref_type, target_ref_name, ancestor)
770
772
771 pull_request.revisions = [
773 pull_request.revisions = [
772 commit.raw_id for commit in reversed(commit_ranges)]
774 commit.raw_id for commit in reversed(commit_ranges)]
773 pull_request.updated_on = datetime.datetime.now()
775 pull_request.updated_on = datetime.datetime.now()
774 Session().add(pull_request)
776 Session().add(pull_request)
775 new_commit_ids = pull_request.revisions
777 new_commit_ids = pull_request.revisions
776
778
777 old_diff_data, new_diff_data = self._generate_update_diffs(
779 old_diff_data, new_diff_data = self._generate_update_diffs(
778 pull_request, pull_request_version)
780 pull_request, pull_request_version)
779
781
780 # calculate commit and file changes
782 # calculate commit and file changes
781 changes = self._calculate_commit_id_changes(
783 changes = self._calculate_commit_id_changes(
782 old_commit_ids, new_commit_ids)
784 old_commit_ids, new_commit_ids)
783 file_changes = self._calculate_file_changes(
785 file_changes = self._calculate_file_changes(
784 old_diff_data, new_diff_data)
786 old_diff_data, new_diff_data)
785
787
786 # set comments as outdated if DIFFS changed
788 # set comments as outdated if DIFFS changed
787 CommentsModel().outdate_comments(
789 CommentsModel().outdate_comments(
788 pull_request, old_diff_data=old_diff_data,
790 pull_request, old_diff_data=old_diff_data,
789 new_diff_data=new_diff_data)
791 new_diff_data=new_diff_data)
790
792
791 commit_changes = (changes.added or changes.removed)
793 commit_changes = (changes.added or changes.removed)
792 file_node_changes = (
794 file_node_changes = (
793 file_changes.added or file_changes.modified or file_changes.removed)
795 file_changes.added or file_changes.modified or file_changes.removed)
794 pr_has_changes = commit_changes or file_node_changes
796 pr_has_changes = commit_changes or file_node_changes
795
797
796 # Add an automatic comment to the pull request, in case
798 # Add an automatic comment to the pull request, in case
797 # anything has changed
799 # anything has changed
798 if pr_has_changes:
800 if pr_has_changes:
799 update_comment = CommentsModel().create(
801 update_comment = CommentsModel().create(
800 text=self._render_update_message(changes, file_changes),
802 text=self._render_update_message(changes, file_changes),
801 repo=pull_request.target_repo,
803 repo=pull_request.target_repo,
802 user=pull_request.author,
804 user=pull_request.author,
803 pull_request=pull_request,
805 pull_request=pull_request,
804 send_email=False, renderer=DEFAULT_COMMENTS_RENDERER)
806 send_email=False, renderer=DEFAULT_COMMENTS_RENDERER)
805
807
806 # Update status to "Under Review" for added commits
808 # Update status to "Under Review" for added commits
807 for commit_id in changes.added:
809 for commit_id in changes.added:
808 ChangesetStatusModel().set_status(
810 ChangesetStatusModel().set_status(
809 repo=pull_request.source_repo,
811 repo=pull_request.source_repo,
810 status=ChangesetStatus.STATUS_UNDER_REVIEW,
812 status=ChangesetStatus.STATUS_UNDER_REVIEW,
811 comment=update_comment,
813 comment=update_comment,
812 user=pull_request.author,
814 user=pull_request.author,
813 pull_request=pull_request,
815 pull_request=pull_request,
814 revision=commit_id)
816 revision=commit_id)
815
817
816 log.debug(
818 log.debug(
817 'Updated pull request %s, added_ids: %s, common_ids: %s, '
819 'Updated pull request %s, added_ids: %s, common_ids: %s, '
818 'removed_ids: %s', pull_request.pull_request_id,
820 'removed_ids: %s', pull_request.pull_request_id,
819 changes.added, changes.common, changes.removed)
821 changes.added, changes.common, changes.removed)
820 log.debug(
822 log.debug(
821 'Updated pull request with the following file changes: %s',
823 'Updated pull request with the following file changes: %s',
822 file_changes)
824 file_changes)
823
825
824 log.info(
826 log.info(
825 "Updated pull request %s from commit %s to commit %s, "
827 "Updated pull request %s from commit %s to commit %s, "
826 "stored new version %s of this pull request.",
828 "stored new version %s of this pull request.",
827 pull_request.pull_request_id, source_ref_id,
829 pull_request.pull_request_id, source_ref_id,
828 pull_request.source_ref_parts.commit_id,
830 pull_request.source_ref_parts.commit_id,
829 pull_request_version.pull_request_version_id)
831 pull_request_version.pull_request_version_id)
830 Session().commit()
832 Session().commit()
831 self._trigger_pull_request_hook(
833 self._trigger_pull_request_hook(
832 pull_request, pull_request.author, 'update')
834 pull_request, pull_request.author, 'update')
833
835
834 return UpdateResponse(
836 return UpdateResponse(
835 executed=True, reason=UpdateFailureReason.NONE,
837 executed=True, reason=UpdateFailureReason.NONE,
836 old=pull_request, new=pull_request_version, changes=changes,
838 old=pull_request, new=pull_request_version, changes=changes,
837 source_changed=source_changed, target_changed=target_changed)
839 source_changed=source_changed, target_changed=target_changed)
838
840
839 def _create_version_from_snapshot(self, pull_request):
841 def _create_version_from_snapshot(self, pull_request):
840 version = PullRequestVersion()
842 version = PullRequestVersion()
841 version.title = pull_request.title
843 version.title = pull_request.title
842 version.description = pull_request.description
844 version.description = pull_request.description
843 version.status = pull_request.status
845 version.status = pull_request.status
844 version.created_on = datetime.datetime.now()
846 version.created_on = datetime.datetime.now()
845 version.updated_on = pull_request.updated_on
847 version.updated_on = pull_request.updated_on
846 version.user_id = pull_request.user_id
848 version.user_id = pull_request.user_id
847 version.source_repo = pull_request.source_repo
849 version.source_repo = pull_request.source_repo
848 version.source_ref = pull_request.source_ref
850 version.source_ref = pull_request.source_ref
849 version.target_repo = pull_request.target_repo
851 version.target_repo = pull_request.target_repo
850 version.target_ref = pull_request.target_ref
852 version.target_ref = pull_request.target_ref
851
853
852 version._last_merge_source_rev = pull_request._last_merge_source_rev
854 version._last_merge_source_rev = pull_request._last_merge_source_rev
853 version._last_merge_target_rev = pull_request._last_merge_target_rev
855 version._last_merge_target_rev = pull_request._last_merge_target_rev
854 version.last_merge_status = pull_request.last_merge_status
856 version.last_merge_status = pull_request.last_merge_status
855 version.shadow_merge_ref = pull_request.shadow_merge_ref
857 version.shadow_merge_ref = pull_request.shadow_merge_ref
856 version.merge_rev = pull_request.merge_rev
858 version.merge_rev = pull_request.merge_rev
857 version.reviewer_data = pull_request.reviewer_data
859 version.reviewer_data = pull_request.reviewer_data
858
860
859 version.revisions = pull_request.revisions
861 version.revisions = pull_request.revisions
860 version.pull_request = pull_request
862 version.pull_request = pull_request
861 Session().add(version)
863 Session().add(version)
862 Session().flush()
864 Session().flush()
863
865
864 return version
866 return version
865
867
866 def _generate_update_diffs(self, pull_request, pull_request_version):
868 def _generate_update_diffs(self, pull_request, pull_request_version):
867
869
868 diff_context = (
870 diff_context = (
869 self.DIFF_CONTEXT +
871 self.DIFF_CONTEXT +
870 CommentsModel.needed_extra_diff_context())
872 CommentsModel.needed_extra_diff_context())
871
873
872 source_repo = pull_request_version.source_repo
874 source_repo = pull_request_version.source_repo
873 source_ref_id = pull_request_version.source_ref_parts.commit_id
875 source_ref_id = pull_request_version.source_ref_parts.commit_id
874 target_ref_id = pull_request_version.target_ref_parts.commit_id
876 target_ref_id = pull_request_version.target_ref_parts.commit_id
875 old_diff = self._get_diff_from_pr_or_version(
877 old_diff = self._get_diff_from_pr_or_version(
876 source_repo, source_ref_id, target_ref_id, context=diff_context)
878 source_repo, source_ref_id, target_ref_id, context=diff_context)
877
879
878 source_repo = pull_request.source_repo
880 source_repo = pull_request.source_repo
879 source_ref_id = pull_request.source_ref_parts.commit_id
881 source_ref_id = pull_request.source_ref_parts.commit_id
880 target_ref_id = pull_request.target_ref_parts.commit_id
882 target_ref_id = pull_request.target_ref_parts.commit_id
881
883
882 new_diff = self._get_diff_from_pr_or_version(
884 new_diff = self._get_diff_from_pr_or_version(
883 source_repo, source_ref_id, target_ref_id, context=diff_context)
885 source_repo, source_ref_id, target_ref_id, context=diff_context)
884
886
885 old_diff_data = diffs.DiffProcessor(old_diff)
887 old_diff_data = diffs.DiffProcessor(old_diff)
886 old_diff_data.prepare()
888 old_diff_data.prepare()
887 new_diff_data = diffs.DiffProcessor(new_diff)
889 new_diff_data = diffs.DiffProcessor(new_diff)
888 new_diff_data.prepare()
890 new_diff_data.prepare()
889
891
890 return old_diff_data, new_diff_data
892 return old_diff_data, new_diff_data
891
893
892 def _link_comments_to_version(self, pull_request_version):
894 def _link_comments_to_version(self, pull_request_version):
893 """
895 """
894 Link all unlinked comments of this pull request to the given version.
896 Link all unlinked comments of this pull request to the given version.
895
897
896 :param pull_request_version: The `PullRequestVersion` to which
898 :param pull_request_version: The `PullRequestVersion` to which
897 the comments shall be linked.
899 the comments shall be linked.
898
900
899 """
901 """
900 pull_request = pull_request_version.pull_request
902 pull_request = pull_request_version.pull_request
901 comments = ChangesetComment.query()\
903 comments = ChangesetComment.query()\
902 .filter(
904 .filter(
903 # TODO: johbo: Should we query for the repo at all here?
905 # TODO: johbo: Should we query for the repo at all here?
904 # Pending decision on how comments of PRs are to be related
906 # Pending decision on how comments of PRs are to be related
905 # to either the source repo, the target repo or no repo at all.
907 # to either the source repo, the target repo or no repo at all.
906 ChangesetComment.repo_id == pull_request.target_repo.repo_id,
908 ChangesetComment.repo_id == pull_request.target_repo.repo_id,
907 ChangesetComment.pull_request == pull_request,
909 ChangesetComment.pull_request == pull_request,
908 ChangesetComment.pull_request_version == None)\
910 ChangesetComment.pull_request_version == None)\
909 .order_by(ChangesetComment.comment_id.asc())
911 .order_by(ChangesetComment.comment_id.asc())
910
912
911 # TODO: johbo: Find out why this breaks if it is done in a bulk
913 # TODO: johbo: Find out why this breaks if it is done in a bulk
912 # operation.
914 # operation.
913 for comment in comments:
915 for comment in comments:
914 comment.pull_request_version_id = (
916 comment.pull_request_version_id = (
915 pull_request_version.pull_request_version_id)
917 pull_request_version.pull_request_version_id)
916 Session().add(comment)
918 Session().add(comment)
917
919
918 def _calculate_commit_id_changes(self, old_ids, new_ids):
920 def _calculate_commit_id_changes(self, old_ids, new_ids):
919 added = [x for x in new_ids if x not in old_ids]
921 added = [x for x in new_ids if x not in old_ids]
920 common = [x for x in new_ids if x in old_ids]
922 common = [x for x in new_ids if x in old_ids]
921 removed = [x for x in old_ids if x not in new_ids]
923 removed = [x for x in old_ids if x not in new_ids]
922 total = new_ids
924 total = new_ids
923 return ChangeTuple(added, common, removed, total)
925 return ChangeTuple(added, common, removed, total)
924
926
925 def _calculate_file_changes(self, old_diff_data, new_diff_data):
927 def _calculate_file_changes(self, old_diff_data, new_diff_data):
926
928
927 old_files = OrderedDict()
929 old_files = OrderedDict()
928 for diff_data in old_diff_data.parsed_diff:
930 for diff_data in old_diff_data.parsed_diff:
929 old_files[diff_data['filename']] = md5_safe(diff_data['raw_diff'])
931 old_files[diff_data['filename']] = md5_safe(diff_data['raw_diff'])
930
932
931 added_files = []
933 added_files = []
932 modified_files = []
934 modified_files = []
933 removed_files = []
935 removed_files = []
934 for diff_data in new_diff_data.parsed_diff:
936 for diff_data in new_diff_data.parsed_diff:
935 new_filename = diff_data['filename']
937 new_filename = diff_data['filename']
936 new_hash = md5_safe(diff_data['raw_diff'])
938 new_hash = md5_safe(diff_data['raw_diff'])
937
939
938 old_hash = old_files.get(new_filename)
940 old_hash = old_files.get(new_filename)
939 if not old_hash:
941 if not old_hash:
940 # file is not present in old diff, means it's added
942 # file is not present in old diff, means it's added
941 added_files.append(new_filename)
943 added_files.append(new_filename)
942 else:
944 else:
943 if new_hash != old_hash:
945 if new_hash != old_hash:
944 modified_files.append(new_filename)
946 modified_files.append(new_filename)
945 # now remove a file from old, since we have seen it already
947 # now remove a file from old, since we have seen it already
946 del old_files[new_filename]
948 del old_files[new_filename]
947
949
948 # removed files is when there are present in old, but not in NEW,
950 # removed files is when there are present in old, but not in NEW,
949 # since we remove old files that are present in new diff, left-overs
951 # since we remove old files that are present in new diff, left-overs
950 # if any should be the removed files
952 # if any should be the removed files
951 removed_files.extend(old_files.keys())
953 removed_files.extend(old_files.keys())
952
954
953 return FileChangeTuple(added_files, modified_files, removed_files)
955 return FileChangeTuple(added_files, modified_files, removed_files)
954
956
955 def _render_update_message(self, changes, file_changes):
957 def _render_update_message(self, changes, file_changes):
956 """
958 """
957 render the message using DEFAULT_COMMENTS_RENDERER (RST renderer),
959 render the message using DEFAULT_COMMENTS_RENDERER (RST renderer),
958 so it's always looking the same disregarding on which default
960 so it's always looking the same disregarding on which default
959 renderer system is using.
961 renderer system is using.
960
962
961 :param changes: changes named tuple
963 :param changes: changes named tuple
962 :param file_changes: file changes named tuple
964 :param file_changes: file changes named tuple
963
965
964 """
966 """
965 new_status = ChangesetStatus.get_status_lbl(
967 new_status = ChangesetStatus.get_status_lbl(
966 ChangesetStatus.STATUS_UNDER_REVIEW)
968 ChangesetStatus.STATUS_UNDER_REVIEW)
967
969
968 changed_files = (
970 changed_files = (
969 file_changes.added + file_changes.modified + file_changes.removed)
971 file_changes.added + file_changes.modified + file_changes.removed)
970
972
971 params = {
973 params = {
972 'under_review_label': new_status,
974 'under_review_label': new_status,
973 'added_commits': changes.added,
975 'added_commits': changes.added,
974 'removed_commits': changes.removed,
976 'removed_commits': changes.removed,
975 'changed_files': changed_files,
977 'changed_files': changed_files,
976 'added_files': file_changes.added,
978 'added_files': file_changes.added,
977 'modified_files': file_changes.modified,
979 'modified_files': file_changes.modified,
978 'removed_files': file_changes.removed,
980 'removed_files': file_changes.removed,
979 }
981 }
980 renderer = RstTemplateRenderer()
982 renderer = RstTemplateRenderer()
981 return renderer.render('pull_request_update.mako', **params)
983 return renderer.render('pull_request_update.mako', **params)
982
984
983 def edit(self, pull_request, title, description, user):
985 def edit(self, pull_request, title, description, description_renderer, user):
984 pull_request = self.__get_pull_request(pull_request)
986 pull_request = self.__get_pull_request(pull_request)
985 old_data = pull_request.get_api_data(with_merge_state=False)
987 old_data = pull_request.get_api_data(with_merge_state=False)
986 if pull_request.is_closed():
988 if pull_request.is_closed():
987 raise ValueError('This pull request is closed')
989 raise ValueError('This pull request is closed')
988 if title:
990 if title:
989 pull_request.title = title
991 pull_request.title = title
990 pull_request.description = description
992 pull_request.description = description
991 pull_request.updated_on = datetime.datetime.now()
993 pull_request.updated_on = datetime.datetime.now()
994 pull_request.description_renderer = description_renderer
992 Session().add(pull_request)
995 Session().add(pull_request)
993 self._log_audit_action(
996 self._log_audit_action(
994 'repo.pull_request.edit', {'old_data': old_data},
997 'repo.pull_request.edit', {'old_data': old_data},
995 user, pull_request)
998 user, pull_request)
996
999
997 def update_reviewers(self, pull_request, reviewer_data, user):
1000 def update_reviewers(self, pull_request, reviewer_data, user):
998 """
1001 """
999 Update the reviewers in the pull request
1002 Update the reviewers in the pull request
1000
1003
1001 :param pull_request: the pr to update
1004 :param pull_request: the pr to update
1002 :param reviewer_data: list of tuples
1005 :param reviewer_data: list of tuples
1003 [(user, ['reason1', 'reason2'], mandatory_flag, [rules])]
1006 [(user, ['reason1', 'reason2'], mandatory_flag, [rules])]
1004 """
1007 """
1005 pull_request = self.__get_pull_request(pull_request)
1008 pull_request = self.__get_pull_request(pull_request)
1006 if pull_request.is_closed():
1009 if pull_request.is_closed():
1007 raise ValueError('This pull request is closed')
1010 raise ValueError('This pull request is closed')
1008
1011
1009 reviewers = {}
1012 reviewers = {}
1010 for user_id, reasons, mandatory, rules in reviewer_data:
1013 for user_id, reasons, mandatory, rules in reviewer_data:
1011 if isinstance(user_id, (int, basestring)):
1014 if isinstance(user_id, (int, basestring)):
1012 user_id = self._get_user(user_id).user_id
1015 user_id = self._get_user(user_id).user_id
1013 reviewers[user_id] = {
1016 reviewers[user_id] = {
1014 'reasons': reasons, 'mandatory': mandatory}
1017 'reasons': reasons, 'mandatory': mandatory}
1015
1018
1016 reviewers_ids = set(reviewers.keys())
1019 reviewers_ids = set(reviewers.keys())
1017 current_reviewers = PullRequestReviewers.query()\
1020 current_reviewers = PullRequestReviewers.query()\
1018 .filter(PullRequestReviewers.pull_request ==
1021 .filter(PullRequestReviewers.pull_request ==
1019 pull_request).all()
1022 pull_request).all()
1020 current_reviewers_ids = set([x.user.user_id for x in current_reviewers])
1023 current_reviewers_ids = set([x.user.user_id for x in current_reviewers])
1021
1024
1022 ids_to_add = reviewers_ids.difference(current_reviewers_ids)
1025 ids_to_add = reviewers_ids.difference(current_reviewers_ids)
1023 ids_to_remove = current_reviewers_ids.difference(reviewers_ids)
1026 ids_to_remove = current_reviewers_ids.difference(reviewers_ids)
1024
1027
1025 log.debug("Adding %s reviewers", ids_to_add)
1028 log.debug("Adding %s reviewers", ids_to_add)
1026 log.debug("Removing %s reviewers", ids_to_remove)
1029 log.debug("Removing %s reviewers", ids_to_remove)
1027 changed = False
1030 changed = False
1028 for uid in ids_to_add:
1031 for uid in ids_to_add:
1029 changed = True
1032 changed = True
1030 _usr = self._get_user(uid)
1033 _usr = self._get_user(uid)
1031 reviewer = PullRequestReviewers()
1034 reviewer = PullRequestReviewers()
1032 reviewer.user = _usr
1035 reviewer.user = _usr
1033 reviewer.pull_request = pull_request
1036 reviewer.pull_request = pull_request
1034 reviewer.reasons = reviewers[uid]['reasons']
1037 reviewer.reasons = reviewers[uid]['reasons']
1035 # NOTE(marcink): mandatory shouldn't be changed now
1038 # NOTE(marcink): mandatory shouldn't be changed now
1036 # reviewer.mandatory = reviewers[uid]['reasons']
1039 # reviewer.mandatory = reviewers[uid]['reasons']
1037 Session().add(reviewer)
1040 Session().add(reviewer)
1038 self._log_audit_action(
1041 self._log_audit_action(
1039 'repo.pull_request.reviewer.add', {'data': reviewer.get_dict()},
1042 'repo.pull_request.reviewer.add', {'data': reviewer.get_dict()},
1040 user, pull_request)
1043 user, pull_request)
1041
1044
1042 for uid in ids_to_remove:
1045 for uid in ids_to_remove:
1043 changed = True
1046 changed = True
1044 reviewers = PullRequestReviewers.query()\
1047 reviewers = PullRequestReviewers.query()\
1045 .filter(PullRequestReviewers.user_id == uid,
1048 .filter(PullRequestReviewers.user_id == uid,
1046 PullRequestReviewers.pull_request == pull_request)\
1049 PullRequestReviewers.pull_request == pull_request)\
1047 .all()
1050 .all()
1048 # use .all() in case we accidentally added the same person twice
1051 # use .all() in case we accidentally added the same person twice
1049 # this CAN happen due to the lack of DB checks
1052 # this CAN happen due to the lack of DB checks
1050 for obj in reviewers:
1053 for obj in reviewers:
1051 old_data = obj.get_dict()
1054 old_data = obj.get_dict()
1052 Session().delete(obj)
1055 Session().delete(obj)
1053 self._log_audit_action(
1056 self._log_audit_action(
1054 'repo.pull_request.reviewer.delete',
1057 'repo.pull_request.reviewer.delete',
1055 {'old_data': old_data}, user, pull_request)
1058 {'old_data': old_data}, user, pull_request)
1056
1059
1057 if changed:
1060 if changed:
1058 pull_request.updated_on = datetime.datetime.now()
1061 pull_request.updated_on = datetime.datetime.now()
1059 Session().add(pull_request)
1062 Session().add(pull_request)
1060
1063
1061 self.notify_reviewers(pull_request, ids_to_add)
1064 self.notify_reviewers(pull_request, ids_to_add)
1062 return ids_to_add, ids_to_remove
1065 return ids_to_add, ids_to_remove
1063
1066
1064 def get_url(self, pull_request, request=None, permalink=False):
1067 def get_url(self, pull_request, request=None, permalink=False):
1065 if not request:
1068 if not request:
1066 request = get_current_request()
1069 request = get_current_request()
1067
1070
1068 if permalink:
1071 if permalink:
1069 return request.route_url(
1072 return request.route_url(
1070 'pull_requests_global',
1073 'pull_requests_global',
1071 pull_request_id=pull_request.pull_request_id,)
1074 pull_request_id=pull_request.pull_request_id,)
1072 else:
1075 else:
1073 return request.route_url('pullrequest_show',
1076 return request.route_url('pullrequest_show',
1074 repo_name=safe_str(pull_request.target_repo.repo_name),
1077 repo_name=safe_str(pull_request.target_repo.repo_name),
1075 pull_request_id=pull_request.pull_request_id,)
1078 pull_request_id=pull_request.pull_request_id,)
1076
1079
1077 def get_shadow_clone_url(self, pull_request, request=None):
1080 def get_shadow_clone_url(self, pull_request, request=None):
1078 """
1081 """
1079 Returns qualified url pointing to the shadow repository. If this pull
1082 Returns qualified url pointing to the shadow repository. If this pull
1080 request is closed there is no shadow repository and ``None`` will be
1083 request is closed there is no shadow repository and ``None`` will be
1081 returned.
1084 returned.
1082 """
1085 """
1083 if pull_request.is_closed():
1086 if pull_request.is_closed():
1084 return None
1087 return None
1085 else:
1088 else:
1086 pr_url = urllib.unquote(self.get_url(pull_request, request=request))
1089 pr_url = urllib.unquote(self.get_url(pull_request, request=request))
1087 return safe_unicode('{pr_url}/repository'.format(pr_url=pr_url))
1090 return safe_unicode('{pr_url}/repository'.format(pr_url=pr_url))
1088
1091
1089 def notify_reviewers(self, pull_request, reviewers_ids):
1092 def notify_reviewers(self, pull_request, reviewers_ids):
1090 # notification to reviewers
1093 # notification to reviewers
1091 if not reviewers_ids:
1094 if not reviewers_ids:
1092 return
1095 return
1093
1096
1094 pull_request_obj = pull_request
1097 pull_request_obj = pull_request
1095 # get the current participants of this pull request
1098 # get the current participants of this pull request
1096 recipients = reviewers_ids
1099 recipients = reviewers_ids
1097 notification_type = EmailNotificationModel.TYPE_PULL_REQUEST
1100 notification_type = EmailNotificationModel.TYPE_PULL_REQUEST
1098
1101
1099 pr_source_repo = pull_request_obj.source_repo
1102 pr_source_repo = pull_request_obj.source_repo
1100 pr_target_repo = pull_request_obj.target_repo
1103 pr_target_repo = pull_request_obj.target_repo
1101
1104
1102 pr_url = h.route_url('pullrequest_show',
1105 pr_url = h.route_url('pullrequest_show',
1103 repo_name=pr_target_repo.repo_name,
1106 repo_name=pr_target_repo.repo_name,
1104 pull_request_id=pull_request_obj.pull_request_id,)
1107 pull_request_id=pull_request_obj.pull_request_id,)
1105
1108
1106 # set some variables for email notification
1109 # set some variables for email notification
1107 pr_target_repo_url = h.route_url(
1110 pr_target_repo_url = h.route_url(
1108 'repo_summary', repo_name=pr_target_repo.repo_name)
1111 'repo_summary', repo_name=pr_target_repo.repo_name)
1109
1112
1110 pr_source_repo_url = h.route_url(
1113 pr_source_repo_url = h.route_url(
1111 'repo_summary', repo_name=pr_source_repo.repo_name)
1114 'repo_summary', repo_name=pr_source_repo.repo_name)
1112
1115
1113 # pull request specifics
1116 # pull request specifics
1114 pull_request_commits = [
1117 pull_request_commits = [
1115 (x.raw_id, x.message)
1118 (x.raw_id, x.message)
1116 for x in map(pr_source_repo.get_commit, pull_request.revisions)]
1119 for x in map(pr_source_repo.get_commit, pull_request.revisions)]
1117
1120
1118 kwargs = {
1121 kwargs = {
1119 'user': pull_request.author,
1122 'user': pull_request.author,
1120 'pull_request': pull_request_obj,
1123 'pull_request': pull_request_obj,
1121 'pull_request_commits': pull_request_commits,
1124 'pull_request_commits': pull_request_commits,
1122
1125
1123 'pull_request_target_repo': pr_target_repo,
1126 'pull_request_target_repo': pr_target_repo,
1124 'pull_request_target_repo_url': pr_target_repo_url,
1127 'pull_request_target_repo_url': pr_target_repo_url,
1125
1128
1126 'pull_request_source_repo': pr_source_repo,
1129 'pull_request_source_repo': pr_source_repo,
1127 'pull_request_source_repo_url': pr_source_repo_url,
1130 'pull_request_source_repo_url': pr_source_repo_url,
1128
1131
1129 'pull_request_url': pr_url,
1132 'pull_request_url': pr_url,
1130 }
1133 }
1131
1134
1132 # pre-generate the subject for notification itself
1135 # pre-generate the subject for notification itself
1133 (subject,
1136 (subject,
1134 _h, _e, # we don't care about those
1137 _h, _e, # we don't care about those
1135 body_plaintext) = EmailNotificationModel().render_email(
1138 body_plaintext) = EmailNotificationModel().render_email(
1136 notification_type, **kwargs)
1139 notification_type, **kwargs)
1137
1140
1138 # create notification objects, and emails
1141 # create notification objects, and emails
1139 NotificationModel().create(
1142 NotificationModel().create(
1140 created_by=pull_request.author,
1143 created_by=pull_request.author,
1141 notification_subject=subject,
1144 notification_subject=subject,
1142 notification_body=body_plaintext,
1145 notification_body=body_plaintext,
1143 notification_type=notification_type,
1146 notification_type=notification_type,
1144 recipients=recipients,
1147 recipients=recipients,
1145 email_kwargs=kwargs,
1148 email_kwargs=kwargs,
1146 )
1149 )
1147
1150
1148 def delete(self, pull_request, user):
1151 def delete(self, pull_request, user):
1149 pull_request = self.__get_pull_request(pull_request)
1152 pull_request = self.__get_pull_request(pull_request)
1150 old_data = pull_request.get_api_data(with_merge_state=False)
1153 old_data = pull_request.get_api_data(with_merge_state=False)
1151 self._cleanup_merge_workspace(pull_request)
1154 self._cleanup_merge_workspace(pull_request)
1152 self._log_audit_action(
1155 self._log_audit_action(
1153 'repo.pull_request.delete', {'old_data': old_data},
1156 'repo.pull_request.delete', {'old_data': old_data},
1154 user, pull_request)
1157 user, pull_request)
1155 Session().delete(pull_request)
1158 Session().delete(pull_request)
1156
1159
1157 def close_pull_request(self, pull_request, user):
1160 def close_pull_request(self, pull_request, user):
1158 pull_request = self.__get_pull_request(pull_request)
1161 pull_request = self.__get_pull_request(pull_request)
1159 self._cleanup_merge_workspace(pull_request)
1162 self._cleanup_merge_workspace(pull_request)
1160 pull_request.status = PullRequest.STATUS_CLOSED
1163 pull_request.status = PullRequest.STATUS_CLOSED
1161 pull_request.updated_on = datetime.datetime.now()
1164 pull_request.updated_on = datetime.datetime.now()
1162 Session().add(pull_request)
1165 Session().add(pull_request)
1163 self._trigger_pull_request_hook(
1166 self._trigger_pull_request_hook(
1164 pull_request, pull_request.author, 'close')
1167 pull_request, pull_request.author, 'close')
1165
1168
1166 pr_data = pull_request.get_api_data(with_merge_state=False)
1169 pr_data = pull_request.get_api_data(with_merge_state=False)
1167 self._log_audit_action(
1170 self._log_audit_action(
1168 'repo.pull_request.close', {'data': pr_data}, user, pull_request)
1171 'repo.pull_request.close', {'data': pr_data}, user, pull_request)
1169
1172
1170 def close_pull_request_with_comment(
1173 def close_pull_request_with_comment(
1171 self, pull_request, user, repo, message=None):
1174 self, pull_request, user, repo, message=None):
1172
1175
1173 pull_request_review_status = pull_request.calculated_review_status()
1176 pull_request_review_status = pull_request.calculated_review_status()
1174
1177
1175 if pull_request_review_status == ChangesetStatus.STATUS_APPROVED:
1178 if pull_request_review_status == ChangesetStatus.STATUS_APPROVED:
1176 # approved only if we have voting consent
1179 # approved only if we have voting consent
1177 status = ChangesetStatus.STATUS_APPROVED
1180 status = ChangesetStatus.STATUS_APPROVED
1178 else:
1181 else:
1179 status = ChangesetStatus.STATUS_REJECTED
1182 status = ChangesetStatus.STATUS_REJECTED
1180 status_lbl = ChangesetStatus.get_status_lbl(status)
1183 status_lbl = ChangesetStatus.get_status_lbl(status)
1181
1184
1182 default_message = (
1185 default_message = (
1183 'Closing with status change {transition_icon} {status}.'
1186 'Closing with status change {transition_icon} {status}.'
1184 ).format(transition_icon='>', status=status_lbl)
1187 ).format(transition_icon='>', status=status_lbl)
1185 text = message or default_message
1188 text = message or default_message
1186
1189
1187 # create a comment, and link it to new status
1190 # create a comment, and link it to new status
1188 comment = CommentsModel().create(
1191 comment = CommentsModel().create(
1189 text=text,
1192 text=text,
1190 repo=repo.repo_id,
1193 repo=repo.repo_id,
1191 user=user.user_id,
1194 user=user.user_id,
1192 pull_request=pull_request.pull_request_id,
1195 pull_request=pull_request.pull_request_id,
1193 status_change=status_lbl,
1196 status_change=status_lbl,
1194 status_change_type=status,
1197 status_change_type=status,
1195 closing_pr=True
1198 closing_pr=True
1196 )
1199 )
1197
1200
1198 # calculate old status before we change it
1201 # calculate old status before we change it
1199 old_calculated_status = pull_request.calculated_review_status()
1202 old_calculated_status = pull_request.calculated_review_status()
1200 ChangesetStatusModel().set_status(
1203 ChangesetStatusModel().set_status(
1201 repo.repo_id,
1204 repo.repo_id,
1202 status,
1205 status,
1203 user.user_id,
1206 user.user_id,
1204 comment=comment,
1207 comment=comment,
1205 pull_request=pull_request.pull_request_id
1208 pull_request=pull_request.pull_request_id
1206 )
1209 )
1207
1210
1208 Session().flush()
1211 Session().flush()
1209 events.trigger(events.PullRequestCommentEvent(pull_request, comment))
1212 events.trigger(events.PullRequestCommentEvent(pull_request, comment))
1210 # we now calculate the status of pull request again, and based on that
1213 # we now calculate the status of pull request again, and based on that
1211 # calculation trigger status change. This might happen in cases
1214 # calculation trigger status change. This might happen in cases
1212 # that non-reviewer admin closes a pr, which means his vote doesn't
1215 # that non-reviewer admin closes a pr, which means his vote doesn't
1213 # change the status, while if he's a reviewer this might change it.
1216 # change the status, while if he's a reviewer this might change it.
1214 calculated_status = pull_request.calculated_review_status()
1217 calculated_status = pull_request.calculated_review_status()
1215 if old_calculated_status != calculated_status:
1218 if old_calculated_status != calculated_status:
1216 self._trigger_pull_request_hook(
1219 self._trigger_pull_request_hook(
1217 pull_request, user, 'review_status_change')
1220 pull_request, user, 'review_status_change')
1218
1221
1219 # finally close the PR
1222 # finally close the PR
1220 PullRequestModel().close_pull_request(
1223 PullRequestModel().close_pull_request(
1221 pull_request.pull_request_id, user)
1224 pull_request.pull_request_id, user)
1222
1225
1223 return comment, status
1226 return comment, status
1224
1227
1225 def merge_status(self, pull_request, translator=None,
1228 def merge_status(self, pull_request, translator=None,
1226 force_shadow_repo_refresh=False):
1229 force_shadow_repo_refresh=False):
1227 _ = translator or get_current_request().translate
1230 _ = translator or get_current_request().translate
1228
1231
1229 if not self._is_merge_enabled(pull_request):
1232 if not self._is_merge_enabled(pull_request):
1230 return False, _('Server-side pull request merging is disabled.')
1233 return False, _('Server-side pull request merging is disabled.')
1231 if pull_request.is_closed():
1234 if pull_request.is_closed():
1232 return False, _('This pull request is closed.')
1235 return False, _('This pull request is closed.')
1233 merge_possible, msg = self._check_repo_requirements(
1236 merge_possible, msg = self._check_repo_requirements(
1234 target=pull_request.target_repo, source=pull_request.source_repo,
1237 target=pull_request.target_repo, source=pull_request.source_repo,
1235 translator=_)
1238 translator=_)
1236 if not merge_possible:
1239 if not merge_possible:
1237 return merge_possible, msg
1240 return merge_possible, msg
1238
1241
1239 try:
1242 try:
1240 resp = self._try_merge(
1243 resp = self._try_merge(
1241 pull_request,
1244 pull_request,
1242 force_shadow_repo_refresh=force_shadow_repo_refresh)
1245 force_shadow_repo_refresh=force_shadow_repo_refresh)
1243 log.debug("Merge response: %s", resp)
1246 log.debug("Merge response: %s", resp)
1244 status = resp.possible, self.merge_status_message(
1247 status = resp.possible, self.merge_status_message(
1245 resp.failure_reason)
1248 resp.failure_reason)
1246 except NotImplementedError:
1249 except NotImplementedError:
1247 status = False, _('Pull request merging is not supported.')
1250 status = False, _('Pull request merging is not supported.')
1248
1251
1249 return status
1252 return status
1250
1253
1251 def _check_repo_requirements(self, target, source, translator):
1254 def _check_repo_requirements(self, target, source, translator):
1252 """
1255 """
1253 Check if `target` and `source` have compatible requirements.
1256 Check if `target` and `source` have compatible requirements.
1254
1257
1255 Currently this is just checking for largefiles.
1258 Currently this is just checking for largefiles.
1256 """
1259 """
1257 _ = translator
1260 _ = translator
1258 target_has_largefiles = self._has_largefiles(target)
1261 target_has_largefiles = self._has_largefiles(target)
1259 source_has_largefiles = self._has_largefiles(source)
1262 source_has_largefiles = self._has_largefiles(source)
1260 merge_possible = True
1263 merge_possible = True
1261 message = u''
1264 message = u''
1262
1265
1263 if target_has_largefiles != source_has_largefiles:
1266 if target_has_largefiles != source_has_largefiles:
1264 merge_possible = False
1267 merge_possible = False
1265 if source_has_largefiles:
1268 if source_has_largefiles:
1266 message = _(
1269 message = _(
1267 'Target repository large files support is disabled.')
1270 'Target repository large files support is disabled.')
1268 else:
1271 else:
1269 message = _(
1272 message = _(
1270 'Source repository large files support is disabled.')
1273 'Source repository large files support is disabled.')
1271
1274
1272 return merge_possible, message
1275 return merge_possible, message
1273
1276
1274 def _has_largefiles(self, repo):
1277 def _has_largefiles(self, repo):
1275 largefiles_ui = VcsSettingsModel(repo=repo).get_ui_settings(
1278 largefiles_ui = VcsSettingsModel(repo=repo).get_ui_settings(
1276 'extensions', 'largefiles')
1279 'extensions', 'largefiles')
1277 return largefiles_ui and largefiles_ui[0].active
1280 return largefiles_ui and largefiles_ui[0].active
1278
1281
1279 def _try_merge(self, pull_request, force_shadow_repo_refresh=False):
1282 def _try_merge(self, pull_request, force_shadow_repo_refresh=False):
1280 """
1283 """
1281 Try to merge the pull request and return the merge status.
1284 Try to merge the pull request and return the merge status.
1282 """
1285 """
1283 log.debug(
1286 log.debug(
1284 "Trying out if the pull request %s can be merged. Force_refresh=%s",
1287 "Trying out if the pull request %s can be merged. Force_refresh=%s",
1285 pull_request.pull_request_id, force_shadow_repo_refresh)
1288 pull_request.pull_request_id, force_shadow_repo_refresh)
1286 target_vcs = pull_request.target_repo.scm_instance()
1289 target_vcs = pull_request.target_repo.scm_instance()
1287
1290
1288 # Refresh the target reference.
1291 # Refresh the target reference.
1289 try:
1292 try:
1290 target_ref = self._refresh_reference(
1293 target_ref = self._refresh_reference(
1291 pull_request.target_ref_parts, target_vcs)
1294 pull_request.target_ref_parts, target_vcs)
1292 except CommitDoesNotExistError:
1295 except CommitDoesNotExistError:
1293 merge_state = MergeResponse(
1296 merge_state = MergeResponse(
1294 False, False, None, MergeFailureReason.MISSING_TARGET_REF)
1297 False, False, None, MergeFailureReason.MISSING_TARGET_REF)
1295 return merge_state
1298 return merge_state
1296
1299
1297 target_locked = pull_request.target_repo.locked
1300 target_locked = pull_request.target_repo.locked
1298 if target_locked and target_locked[0]:
1301 if target_locked and target_locked[0]:
1299 log.debug("The target repository is locked.")
1302 log.debug("The target repository is locked.")
1300 merge_state = MergeResponse(
1303 merge_state = MergeResponse(
1301 False, False, None, MergeFailureReason.TARGET_IS_LOCKED)
1304 False, False, None, MergeFailureReason.TARGET_IS_LOCKED)
1302 elif force_shadow_repo_refresh or self._needs_merge_state_refresh(
1305 elif force_shadow_repo_refresh or self._needs_merge_state_refresh(
1303 pull_request, target_ref):
1306 pull_request, target_ref):
1304 log.debug("Refreshing the merge status of the repository.")
1307 log.debug("Refreshing the merge status of the repository.")
1305 merge_state = self._refresh_merge_state(
1308 merge_state = self._refresh_merge_state(
1306 pull_request, target_vcs, target_ref)
1309 pull_request, target_vcs, target_ref)
1307 else:
1310 else:
1308 possible = pull_request.\
1311 possible = pull_request.\
1309 last_merge_status == MergeFailureReason.NONE
1312 last_merge_status == MergeFailureReason.NONE
1310 merge_state = MergeResponse(
1313 merge_state = MergeResponse(
1311 possible, False, None, pull_request.last_merge_status)
1314 possible, False, None, pull_request.last_merge_status)
1312
1315
1313 return merge_state
1316 return merge_state
1314
1317
1315 def _refresh_reference(self, reference, vcs_repository):
1318 def _refresh_reference(self, reference, vcs_repository):
1316 if reference.type in ('branch', 'book'):
1319 if reference.type in ('branch', 'book'):
1317 name_or_id = reference.name
1320 name_or_id = reference.name
1318 else:
1321 else:
1319 name_or_id = reference.commit_id
1322 name_or_id = reference.commit_id
1320 refreshed_commit = vcs_repository.get_commit(name_or_id)
1323 refreshed_commit = vcs_repository.get_commit(name_or_id)
1321 refreshed_reference = Reference(
1324 refreshed_reference = Reference(
1322 reference.type, reference.name, refreshed_commit.raw_id)
1325 reference.type, reference.name, refreshed_commit.raw_id)
1323 return refreshed_reference
1326 return refreshed_reference
1324
1327
1325 def _needs_merge_state_refresh(self, pull_request, target_reference):
1328 def _needs_merge_state_refresh(self, pull_request, target_reference):
1326 return not(
1329 return not(
1327 pull_request.revisions and
1330 pull_request.revisions and
1328 pull_request.revisions[0] == pull_request._last_merge_source_rev and
1331 pull_request.revisions[0] == pull_request._last_merge_source_rev and
1329 target_reference.commit_id == pull_request._last_merge_target_rev)
1332 target_reference.commit_id == pull_request._last_merge_target_rev)
1330
1333
1331 def _refresh_merge_state(self, pull_request, target_vcs, target_reference):
1334 def _refresh_merge_state(self, pull_request, target_vcs, target_reference):
1332 workspace_id = self._workspace_id(pull_request)
1335 workspace_id = self._workspace_id(pull_request)
1333 source_vcs = pull_request.source_repo.scm_instance()
1336 source_vcs = pull_request.source_repo.scm_instance()
1334 repo_id = pull_request.target_repo.repo_id
1337 repo_id = pull_request.target_repo.repo_id
1335 use_rebase = self._use_rebase_for_merging(pull_request)
1338 use_rebase = self._use_rebase_for_merging(pull_request)
1336 close_branch = self._close_branch_before_merging(pull_request)
1339 close_branch = self._close_branch_before_merging(pull_request)
1337 merge_state = target_vcs.merge(
1340 merge_state = target_vcs.merge(
1338 repo_id, workspace_id,
1341 repo_id, workspace_id,
1339 target_reference, source_vcs, pull_request.source_ref_parts,
1342 target_reference, source_vcs, pull_request.source_ref_parts,
1340 dry_run=True, use_rebase=use_rebase,
1343 dry_run=True, use_rebase=use_rebase,
1341 close_branch=close_branch)
1344 close_branch=close_branch)
1342
1345
1343 # Do not store the response if there was an unknown error.
1346 # Do not store the response if there was an unknown error.
1344 if merge_state.failure_reason != MergeFailureReason.UNKNOWN:
1347 if merge_state.failure_reason != MergeFailureReason.UNKNOWN:
1345 pull_request._last_merge_source_rev = \
1348 pull_request._last_merge_source_rev = \
1346 pull_request.source_ref_parts.commit_id
1349 pull_request.source_ref_parts.commit_id
1347 pull_request._last_merge_target_rev = target_reference.commit_id
1350 pull_request._last_merge_target_rev = target_reference.commit_id
1348 pull_request.last_merge_status = merge_state.failure_reason
1351 pull_request.last_merge_status = merge_state.failure_reason
1349 pull_request.shadow_merge_ref = merge_state.merge_ref
1352 pull_request.shadow_merge_ref = merge_state.merge_ref
1350 Session().add(pull_request)
1353 Session().add(pull_request)
1351 Session().commit()
1354 Session().commit()
1352
1355
1353 return merge_state
1356 return merge_state
1354
1357
1355 def _workspace_id(self, pull_request):
1358 def _workspace_id(self, pull_request):
1356 workspace_id = 'pr-%s' % pull_request.pull_request_id
1359 workspace_id = 'pr-%s' % pull_request.pull_request_id
1357 return workspace_id
1360 return workspace_id
1358
1361
1359 def merge_status_message(self, status_code):
1362 def merge_status_message(self, status_code):
1360 """
1363 """
1361 Return a human friendly error message for the given merge status code.
1364 Return a human friendly error message for the given merge status code.
1362 """
1365 """
1363 return self.MERGE_STATUS_MESSAGES[status_code]
1366 return self.MERGE_STATUS_MESSAGES[status_code]
1364
1367
1365 def generate_repo_data(self, repo, commit_id=None, branch=None,
1368 def generate_repo_data(self, repo, commit_id=None, branch=None,
1366 bookmark=None, translator=None):
1369 bookmark=None, translator=None):
1367 from rhodecode.model.repo import RepoModel
1370 from rhodecode.model.repo import RepoModel
1368
1371
1369 all_refs, selected_ref = \
1372 all_refs, selected_ref = \
1370 self._get_repo_pullrequest_sources(
1373 self._get_repo_pullrequest_sources(
1371 repo.scm_instance(), commit_id=commit_id,
1374 repo.scm_instance(), commit_id=commit_id,
1372 branch=branch, bookmark=bookmark, translator=translator)
1375 branch=branch, bookmark=bookmark, translator=translator)
1373
1376
1374 refs_select2 = []
1377 refs_select2 = []
1375 for element in all_refs:
1378 for element in all_refs:
1376 children = [{'id': x[0], 'text': x[1]} for x in element[0]]
1379 children = [{'id': x[0], 'text': x[1]} for x in element[0]]
1377 refs_select2.append({'text': element[1], 'children': children})
1380 refs_select2.append({'text': element[1], 'children': children})
1378
1381
1379 return {
1382 return {
1380 'user': {
1383 'user': {
1381 'user_id': repo.user.user_id,
1384 'user_id': repo.user.user_id,
1382 'username': repo.user.username,
1385 'username': repo.user.username,
1383 'firstname': repo.user.first_name,
1386 'firstname': repo.user.first_name,
1384 'lastname': repo.user.last_name,
1387 'lastname': repo.user.last_name,
1385 'gravatar_link': h.gravatar_url(repo.user.email, 14),
1388 'gravatar_link': h.gravatar_url(repo.user.email, 14),
1386 },
1389 },
1387 'name': repo.repo_name,
1390 'name': repo.repo_name,
1388 'link': RepoModel().get_url(repo),
1391 'link': RepoModel().get_url(repo),
1389 'description': h.chop_at_smart(repo.description_safe, '\n'),
1392 'description': h.chop_at_smart(repo.description_safe, '\n'),
1390 'refs': {
1393 'refs': {
1391 'all_refs': all_refs,
1394 'all_refs': all_refs,
1392 'selected_ref': selected_ref,
1395 'selected_ref': selected_ref,
1393 'select2_refs': refs_select2
1396 'select2_refs': refs_select2
1394 }
1397 }
1395 }
1398 }
1396
1399
1397 def generate_pullrequest_title(self, source, source_ref, target):
1400 def generate_pullrequest_title(self, source, source_ref, target):
1398 return u'{source}#{at_ref} to {target}'.format(
1401 return u'{source}#{at_ref} to {target}'.format(
1399 source=source,
1402 source=source,
1400 at_ref=source_ref,
1403 at_ref=source_ref,
1401 target=target,
1404 target=target,
1402 )
1405 )
1403
1406
1404 def _cleanup_merge_workspace(self, pull_request):
1407 def _cleanup_merge_workspace(self, pull_request):
1405 # Merging related cleanup
1408 # Merging related cleanup
1406 repo_id = pull_request.target_repo.repo_id
1409 repo_id = pull_request.target_repo.repo_id
1407 target_scm = pull_request.target_repo.scm_instance()
1410 target_scm = pull_request.target_repo.scm_instance()
1408 workspace_id = self._workspace_id(pull_request)
1411 workspace_id = self._workspace_id(pull_request)
1409
1412
1410 try:
1413 try:
1411 target_scm.cleanup_merge_workspace(repo_id, workspace_id)
1414 target_scm.cleanup_merge_workspace(repo_id, workspace_id)
1412 except NotImplementedError:
1415 except NotImplementedError:
1413 pass
1416 pass
1414
1417
1415 def _get_repo_pullrequest_sources(
1418 def _get_repo_pullrequest_sources(
1416 self, repo, commit_id=None, branch=None, bookmark=None,
1419 self, repo, commit_id=None, branch=None, bookmark=None,
1417 translator=None):
1420 translator=None):
1418 """
1421 """
1419 Return a structure with repo's interesting commits, suitable for
1422 Return a structure with repo's interesting commits, suitable for
1420 the selectors in pullrequest controller
1423 the selectors in pullrequest controller
1421
1424
1422 :param commit_id: a commit that must be in the list somehow
1425 :param commit_id: a commit that must be in the list somehow
1423 and selected by default
1426 and selected by default
1424 :param branch: a branch that must be in the list and selected
1427 :param branch: a branch that must be in the list and selected
1425 by default - even if closed
1428 by default - even if closed
1426 :param bookmark: a bookmark that must be in the list and selected
1429 :param bookmark: a bookmark that must be in the list and selected
1427 """
1430 """
1428 _ = translator or get_current_request().translate
1431 _ = translator or get_current_request().translate
1429
1432
1430 commit_id = safe_str(commit_id) if commit_id else None
1433 commit_id = safe_str(commit_id) if commit_id else None
1431 branch = safe_str(branch) if branch else None
1434 branch = safe_str(branch) if branch else None
1432 bookmark = safe_str(bookmark) if bookmark else None
1435 bookmark = safe_str(bookmark) if bookmark else None
1433
1436
1434 selected = None
1437 selected = None
1435
1438
1436 # order matters: first source that has commit_id in it will be selected
1439 # order matters: first source that has commit_id in it will be selected
1437 sources = []
1440 sources = []
1438 sources.append(('book', repo.bookmarks.items(), _('Bookmarks'), bookmark))
1441 sources.append(('book', repo.bookmarks.items(), _('Bookmarks'), bookmark))
1439 sources.append(('branch', repo.branches.items(), _('Branches'), branch))
1442 sources.append(('branch', repo.branches.items(), _('Branches'), branch))
1440
1443
1441 if commit_id:
1444 if commit_id:
1442 ref_commit = (h.short_id(commit_id), commit_id)
1445 ref_commit = (h.short_id(commit_id), commit_id)
1443 sources.append(('rev', [ref_commit], _('Commit IDs'), commit_id))
1446 sources.append(('rev', [ref_commit], _('Commit IDs'), commit_id))
1444
1447
1445 sources.append(
1448 sources.append(
1446 ('branch', repo.branches_closed.items(), _('Closed Branches'), branch),
1449 ('branch', repo.branches_closed.items(), _('Closed Branches'), branch),
1447 )
1450 )
1448
1451
1449 groups = []
1452 groups = []
1450 for group_key, ref_list, group_name, match in sources:
1453 for group_key, ref_list, group_name, match in sources:
1451 group_refs = []
1454 group_refs = []
1452 for ref_name, ref_id in ref_list:
1455 for ref_name, ref_id in ref_list:
1453 ref_key = '%s:%s:%s' % (group_key, ref_name, ref_id)
1456 ref_key = '%s:%s:%s' % (group_key, ref_name, ref_id)
1454 group_refs.append((ref_key, ref_name))
1457 group_refs.append((ref_key, ref_name))
1455
1458
1456 if not selected:
1459 if not selected:
1457 if set([commit_id, match]) & set([ref_id, ref_name]):
1460 if set([commit_id, match]) & set([ref_id, ref_name]):
1458 selected = ref_key
1461 selected = ref_key
1459
1462
1460 if group_refs:
1463 if group_refs:
1461 groups.append((group_refs, group_name))
1464 groups.append((group_refs, group_name))
1462
1465
1463 if not selected:
1466 if not selected:
1464 ref = commit_id or branch or bookmark
1467 ref = commit_id or branch or bookmark
1465 if ref:
1468 if ref:
1466 raise CommitDoesNotExistError(
1469 raise CommitDoesNotExistError(
1467 'No commit refs could be found matching: %s' % ref)
1470 'No commit refs could be found matching: %s' % ref)
1468 elif repo.DEFAULT_BRANCH_NAME in repo.branches:
1471 elif repo.DEFAULT_BRANCH_NAME in repo.branches:
1469 selected = 'branch:%s:%s' % (
1472 selected = 'branch:%s:%s' % (
1470 repo.DEFAULT_BRANCH_NAME,
1473 repo.DEFAULT_BRANCH_NAME,
1471 repo.branches[repo.DEFAULT_BRANCH_NAME]
1474 repo.branches[repo.DEFAULT_BRANCH_NAME]
1472 )
1475 )
1473 elif repo.commit_ids:
1476 elif repo.commit_ids:
1474 # make the user select in this case
1477 # make the user select in this case
1475 selected = None
1478 selected = None
1476 else:
1479 else:
1477 raise EmptyRepositoryError()
1480 raise EmptyRepositoryError()
1478 return groups, selected
1481 return groups, selected
1479
1482
1480 def get_diff(self, source_repo, source_ref_id, target_ref_id, context=DIFF_CONTEXT):
1483 def get_diff(self, source_repo, source_ref_id, target_ref_id, context=DIFF_CONTEXT):
1481 return self._get_diff_from_pr_or_version(
1484 return self._get_diff_from_pr_or_version(
1482 source_repo, source_ref_id, target_ref_id, context=context)
1485 source_repo, source_ref_id, target_ref_id, context=context)
1483
1486
1484 def _get_diff_from_pr_or_version(
1487 def _get_diff_from_pr_or_version(
1485 self, source_repo, source_ref_id, target_ref_id, context):
1488 self, source_repo, source_ref_id, target_ref_id, context):
1486 target_commit = source_repo.get_commit(
1489 target_commit = source_repo.get_commit(
1487 commit_id=safe_str(target_ref_id))
1490 commit_id=safe_str(target_ref_id))
1488 source_commit = source_repo.get_commit(
1491 source_commit = source_repo.get_commit(
1489 commit_id=safe_str(source_ref_id))
1492 commit_id=safe_str(source_ref_id))
1490 if isinstance(source_repo, Repository):
1493 if isinstance(source_repo, Repository):
1491 vcs_repo = source_repo.scm_instance()
1494 vcs_repo = source_repo.scm_instance()
1492 else:
1495 else:
1493 vcs_repo = source_repo
1496 vcs_repo = source_repo
1494
1497
1495 # TODO: johbo: In the context of an update, we cannot reach
1498 # TODO: johbo: In the context of an update, we cannot reach
1496 # the old commit anymore with our normal mechanisms. It needs
1499 # the old commit anymore with our normal mechanisms. It needs
1497 # some sort of special support in the vcs layer to avoid this
1500 # some sort of special support in the vcs layer to avoid this
1498 # workaround.
1501 # workaround.
1499 if (source_commit.raw_id == vcs_repo.EMPTY_COMMIT_ID and
1502 if (source_commit.raw_id == vcs_repo.EMPTY_COMMIT_ID and
1500 vcs_repo.alias == 'git'):
1503 vcs_repo.alias == 'git'):
1501 source_commit.raw_id = safe_str(source_ref_id)
1504 source_commit.raw_id = safe_str(source_ref_id)
1502
1505
1503 log.debug('calculating diff between '
1506 log.debug('calculating diff between '
1504 'source_ref:%s and target_ref:%s for repo `%s`',
1507 'source_ref:%s and target_ref:%s for repo `%s`',
1505 target_ref_id, source_ref_id,
1508 target_ref_id, source_ref_id,
1506 safe_unicode(vcs_repo.path))
1509 safe_unicode(vcs_repo.path))
1507
1510
1508 vcs_diff = vcs_repo.get_diff(
1511 vcs_diff = vcs_repo.get_diff(
1509 commit1=target_commit, commit2=source_commit, context=context)
1512 commit1=target_commit, commit2=source_commit, context=context)
1510 return vcs_diff
1513 return vcs_diff
1511
1514
1512 def _is_merge_enabled(self, pull_request):
1515 def _is_merge_enabled(self, pull_request):
1513 return self._get_general_setting(
1516 return self._get_general_setting(
1514 pull_request, 'rhodecode_pr_merge_enabled')
1517 pull_request, 'rhodecode_pr_merge_enabled')
1515
1518
1516 def _use_rebase_for_merging(self, pull_request):
1519 def _use_rebase_for_merging(self, pull_request):
1517 repo_type = pull_request.target_repo.repo_type
1520 repo_type = pull_request.target_repo.repo_type
1518 if repo_type == 'hg':
1521 if repo_type == 'hg':
1519 return self._get_general_setting(
1522 return self._get_general_setting(
1520 pull_request, 'rhodecode_hg_use_rebase_for_merging')
1523 pull_request, 'rhodecode_hg_use_rebase_for_merging')
1521 elif repo_type == 'git':
1524 elif repo_type == 'git':
1522 return self._get_general_setting(
1525 return self._get_general_setting(
1523 pull_request, 'rhodecode_git_use_rebase_for_merging')
1526 pull_request, 'rhodecode_git_use_rebase_for_merging')
1524
1527
1525 return False
1528 return False
1526
1529
1527 def _close_branch_before_merging(self, pull_request):
1530 def _close_branch_before_merging(self, pull_request):
1528 repo_type = pull_request.target_repo.repo_type
1531 repo_type = pull_request.target_repo.repo_type
1529 if repo_type == 'hg':
1532 if repo_type == 'hg':
1530 return self._get_general_setting(
1533 return self._get_general_setting(
1531 pull_request, 'rhodecode_hg_close_branch_before_merging')
1534 pull_request, 'rhodecode_hg_close_branch_before_merging')
1532 elif repo_type == 'git':
1535 elif repo_type == 'git':
1533 return self._get_general_setting(
1536 return self._get_general_setting(
1534 pull_request, 'rhodecode_git_close_branch_before_merging')
1537 pull_request, 'rhodecode_git_close_branch_before_merging')
1535
1538
1536 return False
1539 return False
1537
1540
1538 def _get_general_setting(self, pull_request, settings_key, default=False):
1541 def _get_general_setting(self, pull_request, settings_key, default=False):
1539 settings_model = VcsSettingsModel(repo=pull_request.target_repo)
1542 settings_model = VcsSettingsModel(repo=pull_request.target_repo)
1540 settings = settings_model.get_general_settings()
1543 settings = settings_model.get_general_settings()
1541 return settings.get(settings_key, default)
1544 return settings.get(settings_key, default)
1542
1545
1543 def _log_audit_action(self, action, action_data, user, pull_request):
1546 def _log_audit_action(self, action, action_data, user, pull_request):
1544 audit_logger.store(
1547 audit_logger.store(
1545 action=action,
1548 action=action,
1546 action_data=action_data,
1549 action_data=action_data,
1547 user=user,
1550 user=user,
1548 repo=pull_request.target_repo)
1551 repo=pull_request.target_repo)
1549
1552
1550 def get_reviewer_functions(self):
1553 def get_reviewer_functions(self):
1551 """
1554 """
1552 Fetches functions for validation and fetching default reviewers.
1555 Fetches functions for validation and fetching default reviewers.
1553 If available we use the EE package, else we fallback to CE
1556 If available we use the EE package, else we fallback to CE
1554 package functions
1557 package functions
1555 """
1558 """
1556 try:
1559 try:
1557 from rc_reviewers.utils import get_default_reviewers_data
1560 from rc_reviewers.utils import get_default_reviewers_data
1558 from rc_reviewers.utils import validate_default_reviewers
1561 from rc_reviewers.utils import validate_default_reviewers
1559 except ImportError:
1562 except ImportError:
1560 from rhodecode.apps.repository.utils import \
1563 from rhodecode.apps.repository.utils import \
1561 get_default_reviewers_data
1564 get_default_reviewers_data
1562 from rhodecode.apps.repository.utils import \
1565 from rhodecode.apps.repository.utils import \
1563 validate_default_reviewers
1566 validate_default_reviewers
1564
1567
1565 return get_default_reviewers_data, validate_default_reviewers
1568 return get_default_reviewers_data, validate_default_reviewers
1566
1569
1567
1570
1568 class MergeCheck(object):
1571 class MergeCheck(object):
1569 """
1572 """
1570 Perform Merge Checks and returns a check object which stores information
1573 Perform Merge Checks and returns a check object which stores information
1571 about merge errors, and merge conditions
1574 about merge errors, and merge conditions
1572 """
1575 """
1573 TODO_CHECK = 'todo'
1576 TODO_CHECK = 'todo'
1574 PERM_CHECK = 'perm'
1577 PERM_CHECK = 'perm'
1575 REVIEW_CHECK = 'review'
1578 REVIEW_CHECK = 'review'
1576 MERGE_CHECK = 'merge'
1579 MERGE_CHECK = 'merge'
1577
1580
1578 def __init__(self):
1581 def __init__(self):
1579 self.review_status = None
1582 self.review_status = None
1580 self.merge_possible = None
1583 self.merge_possible = None
1581 self.merge_msg = ''
1584 self.merge_msg = ''
1582 self.failed = None
1585 self.failed = None
1583 self.errors = []
1586 self.errors = []
1584 self.error_details = OrderedDict()
1587 self.error_details = OrderedDict()
1585
1588
1586 def push_error(self, error_type, message, error_key, details):
1589 def push_error(self, error_type, message, error_key, details):
1587 self.failed = True
1590 self.failed = True
1588 self.errors.append([error_type, message])
1591 self.errors.append([error_type, message])
1589 self.error_details[error_key] = dict(
1592 self.error_details[error_key] = dict(
1590 details=details,
1593 details=details,
1591 error_type=error_type,
1594 error_type=error_type,
1592 message=message
1595 message=message
1593 )
1596 )
1594
1597
1595 @classmethod
1598 @classmethod
1596 def validate(cls, pull_request, user, translator, fail_early=False,
1599 def validate(cls, pull_request, user, translator, fail_early=False,
1597 force_shadow_repo_refresh=False):
1600 force_shadow_repo_refresh=False):
1598 _ = translator
1601 _ = translator
1599 merge_check = cls()
1602 merge_check = cls()
1600
1603
1601 # permissions to merge
1604 # permissions to merge
1602 user_allowed_to_merge = PullRequestModel().check_user_merge(
1605 user_allowed_to_merge = PullRequestModel().check_user_merge(
1603 pull_request, user)
1606 pull_request, user)
1604 if not user_allowed_to_merge:
1607 if not user_allowed_to_merge:
1605 log.debug("MergeCheck: cannot merge, approval is pending.")
1608 log.debug("MergeCheck: cannot merge, approval is pending.")
1606
1609
1607 msg = _('User `{}` not allowed to perform merge.').format(user.username)
1610 msg = _('User `{}` not allowed to perform merge.').format(user.username)
1608 merge_check.push_error('error', msg, cls.PERM_CHECK, user.username)
1611 merge_check.push_error('error', msg, cls.PERM_CHECK, user.username)
1609 if fail_early:
1612 if fail_early:
1610 return merge_check
1613 return merge_check
1611
1614
1612 # review status, must be always present
1615 # review status, must be always present
1613 review_status = pull_request.calculated_review_status()
1616 review_status = pull_request.calculated_review_status()
1614 merge_check.review_status = review_status
1617 merge_check.review_status = review_status
1615
1618
1616 status_approved = review_status == ChangesetStatus.STATUS_APPROVED
1619 status_approved = review_status == ChangesetStatus.STATUS_APPROVED
1617 if not status_approved:
1620 if not status_approved:
1618 log.debug("MergeCheck: cannot merge, approval is pending.")
1621 log.debug("MergeCheck: cannot merge, approval is pending.")
1619
1622
1620 msg = _('Pull request reviewer approval is pending.')
1623 msg = _('Pull request reviewer approval is pending.')
1621
1624
1622 merge_check.push_error(
1625 merge_check.push_error(
1623 'warning', msg, cls.REVIEW_CHECK, review_status)
1626 'warning', msg, cls.REVIEW_CHECK, review_status)
1624
1627
1625 if fail_early:
1628 if fail_early:
1626 return merge_check
1629 return merge_check
1627
1630
1628 # left over TODOs
1631 # left over TODOs
1629 todos = CommentsModel().get_unresolved_todos(pull_request)
1632 todos = CommentsModel().get_unresolved_todos(pull_request)
1630 if todos:
1633 if todos:
1631 log.debug("MergeCheck: cannot merge, {} "
1634 log.debug("MergeCheck: cannot merge, {} "
1632 "unresolved todos left.".format(len(todos)))
1635 "unresolved todos left.".format(len(todos)))
1633
1636
1634 if len(todos) == 1:
1637 if len(todos) == 1:
1635 msg = _('Cannot merge, {} TODO still not resolved.').format(
1638 msg = _('Cannot merge, {} TODO still not resolved.').format(
1636 len(todos))
1639 len(todos))
1637 else:
1640 else:
1638 msg = _('Cannot merge, {} TODOs still not resolved.').format(
1641 msg = _('Cannot merge, {} TODOs still not resolved.').format(
1639 len(todos))
1642 len(todos))
1640
1643
1641 merge_check.push_error('warning', msg, cls.TODO_CHECK, todos)
1644 merge_check.push_error('warning', msg, cls.TODO_CHECK, todos)
1642
1645
1643 if fail_early:
1646 if fail_early:
1644 return merge_check
1647 return merge_check
1645
1648
1646 # merge possible, here is the filesystem simulation + shadow repo
1649 # merge possible, here is the filesystem simulation + shadow repo
1647 merge_status, msg = PullRequestModel().merge_status(
1650 merge_status, msg = PullRequestModel().merge_status(
1648 pull_request, translator=translator,
1651 pull_request, translator=translator,
1649 force_shadow_repo_refresh=force_shadow_repo_refresh)
1652 force_shadow_repo_refresh=force_shadow_repo_refresh)
1650 merge_check.merge_possible = merge_status
1653 merge_check.merge_possible = merge_status
1651 merge_check.merge_msg = msg
1654 merge_check.merge_msg = msg
1652 if not merge_status:
1655 if not merge_status:
1653 log.debug(
1656 log.debug(
1654 "MergeCheck: cannot merge, pull request merge not possible.")
1657 "MergeCheck: cannot merge, pull request merge not possible.")
1655 merge_check.push_error('warning', msg, cls.MERGE_CHECK, None)
1658 merge_check.push_error('warning', msg, cls.MERGE_CHECK, None)
1656
1659
1657 if fail_early:
1660 if fail_early:
1658 return merge_check
1661 return merge_check
1659
1662
1660 log.debug('MergeCheck: is failed: %s', merge_check.failed)
1663 log.debug('MergeCheck: is failed: %s', merge_check.failed)
1661 return merge_check
1664 return merge_check
1662
1665
1663 @classmethod
1666 @classmethod
1664 def get_merge_conditions(cls, pull_request, translator):
1667 def get_merge_conditions(cls, pull_request, translator):
1665 _ = translator
1668 _ = translator
1666 merge_details = {}
1669 merge_details = {}
1667
1670
1668 model = PullRequestModel()
1671 model = PullRequestModel()
1669 use_rebase = model._use_rebase_for_merging(pull_request)
1672 use_rebase = model._use_rebase_for_merging(pull_request)
1670
1673
1671 if use_rebase:
1674 if use_rebase:
1672 merge_details['merge_strategy'] = dict(
1675 merge_details['merge_strategy'] = dict(
1673 details={},
1676 details={},
1674 message=_('Merge strategy: rebase')
1677 message=_('Merge strategy: rebase')
1675 )
1678 )
1676 else:
1679 else:
1677 merge_details['merge_strategy'] = dict(
1680 merge_details['merge_strategy'] = dict(
1678 details={},
1681 details={},
1679 message=_('Merge strategy: explicit merge commit')
1682 message=_('Merge strategy: explicit merge commit')
1680 )
1683 )
1681
1684
1682 close_branch = model._close_branch_before_merging(pull_request)
1685 close_branch = model._close_branch_before_merging(pull_request)
1683 if close_branch:
1686 if close_branch:
1684 repo_type = pull_request.target_repo.repo_type
1687 repo_type = pull_request.target_repo.repo_type
1685 if repo_type == 'hg':
1688 if repo_type == 'hg':
1686 close_msg = _('Source branch will be closed after merge.')
1689 close_msg = _('Source branch will be closed after merge.')
1687 elif repo_type == 'git':
1690 elif repo_type == 'git':
1688 close_msg = _('Source branch will be deleted after merge.')
1691 close_msg = _('Source branch will be deleted after merge.')
1689
1692
1690 merge_details['close_branch'] = dict(
1693 merge_details['close_branch'] = dict(
1691 details={},
1694 details={},
1692 message=close_msg
1695 message=close_msg
1693 )
1696 )
1694
1697
1695 return merge_details
1698 return merge_details
1696
1699
1697 ChangeTuple = collections.namedtuple(
1700 ChangeTuple = collections.namedtuple(
1698 'ChangeTuple', ['added', 'common', 'removed', 'total'])
1701 'ChangeTuple', ['added', 'common', 'removed', 'total'])
1699
1702
1700 FileChangeTuple = collections.namedtuple(
1703 FileChangeTuple = collections.namedtuple(
1701 'FileChangeTuple', ['added', 'modified', 'removed'])
1704 'FileChangeTuple', ['added', 'modified', 'removed'])
@@ -1,550 +1,551 b''
1 // # Copyright (C) 2010-2018 RhodeCode GmbH
1 // # Copyright (C) 2010-2018 RhodeCode GmbH
2 // #
2 // #
3 // # This program is free software: you can redistribute it and/or modify
3 // # This program is free software: you can redistribute it and/or modify
4 // # it under the terms of the GNU Affero General Public License, version 3
4 // # it under the terms of the GNU Affero General Public License, version 3
5 // # (only), as published by the Free Software Foundation.
5 // # (only), as published by the Free Software Foundation.
6 // #
6 // #
7 // # This program is distributed in the hope that it will be useful,
7 // # This program is distributed in the hope that it will be useful,
8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // # GNU General Public License for more details.
10 // # GNU General Public License for more details.
11 // #
11 // #
12 // # You should have received a copy of the GNU Affero General Public License
12 // # You should have received a copy of the GNU Affero General Public License
13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 // #
14 // #
15 // # This program is dual-licensed. If you wish to learn more about the
15 // # This program is dual-licensed. If you wish to learn more about the
16 // # RhodeCode Enterprise Edition, including its added features, Support services,
16 // # RhodeCode Enterprise Edition, including its added features, Support services,
17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
18
18
19
19
20 var prButtonLockChecks = {
20 var prButtonLockChecks = {
21 'compare': false,
21 'compare': false,
22 'reviewers': false
22 'reviewers': false
23 };
23 };
24
24
25 /**
25 /**
26 * lock button until all checks and loads are made. E.g reviewer calculation
26 * lock button until all checks and loads are made. E.g reviewer calculation
27 * should prevent from submitting a PR
27 * should prevent from submitting a PR
28 * @param lockEnabled
28 * @param lockEnabled
29 * @param msg
29 * @param msg
30 * @param scope
30 * @param scope
31 */
31 */
32 var prButtonLock = function(lockEnabled, msg, scope) {
32 var prButtonLock = function(lockEnabled, msg, scope) {
33 scope = scope || 'all';
33 scope = scope || 'all';
34 if (scope == 'all'){
34 if (scope == 'all'){
35 prButtonLockChecks['compare'] = !lockEnabled;
35 prButtonLockChecks['compare'] = !lockEnabled;
36 prButtonLockChecks['reviewers'] = !lockEnabled;
36 prButtonLockChecks['reviewers'] = !lockEnabled;
37 } else if (scope == 'compare') {
37 } else if (scope == 'compare') {
38 prButtonLockChecks['compare'] = !lockEnabled;
38 prButtonLockChecks['compare'] = !lockEnabled;
39 } else if (scope == 'reviewers'){
39 } else if (scope == 'reviewers'){
40 prButtonLockChecks['reviewers'] = !lockEnabled;
40 prButtonLockChecks['reviewers'] = !lockEnabled;
41 }
41 }
42 var checksMeet = prButtonLockChecks.compare && prButtonLockChecks.reviewers;
42 var checksMeet = prButtonLockChecks.compare && prButtonLockChecks.reviewers;
43 if (lockEnabled) {
43 if (lockEnabled) {
44 $('#pr_submit').attr('disabled', 'disabled');
44 $('#pr_submit').attr('disabled', 'disabled');
45 }
45 }
46 else if (checksMeet) {
46 else if (checksMeet) {
47 $('#pr_submit').removeAttr('disabled');
47 $('#pr_submit').removeAttr('disabled');
48 }
48 }
49
49
50 if (msg) {
50 if (msg) {
51 $('#pr_open_message').html(msg);
51 $('#pr_open_message').html(msg);
52 }
52 }
53 };
53 };
54
54
55
55
56 /**
56 /**
57 Generate Title and Description for a PullRequest.
57 Generate Title and Description for a PullRequest.
58 In case of 1 commits, the title and description is that one commit
58 In case of 1 commits, the title and description is that one commit
59 in case of multiple commits, we iterate on them with max N number of commits,
59 in case of multiple commits, we iterate on them with max N number of commits,
60 and build description in a form
60 and build description in a form
61 - commitN
61 - commitN
62 - commitN+1
62 - commitN+1
63 ...
63 ...
64
64
65 Title is then constructed from branch names, or other references,
65 Title is then constructed from branch names, or other references,
66 replacing '-' and '_' into spaces
66 replacing '-' and '_' into spaces
67
67
68 * @param sourceRef
68 * @param sourceRef
69 * @param elements
69 * @param elements
70 * @param limit
70 * @param limit
71 * @returns {*[]}
71 * @returns {*[]}
72 */
72 */
73 var getTitleAndDescription = function(sourceRef, elements, limit) {
73 var getTitleAndDescription = function(sourceRef, elements, limit) {
74 var title = '';
74 var title = '';
75 var desc = '';
75 var desc = '';
76
76
77 $.each($(elements).get().reverse().slice(0, limit), function(idx, value) {
77 $.each($(elements).get().reverse().slice(0, limit), function(idx, value) {
78 var rawMessage = $(value).find('td.td-description .message').data('messageRaw');
78 var rawMessage = $(value).find('td.td-description .message').data('messageRaw');
79 desc += '- ' + rawMessage.split('\n')[0].replace(/\n+$/, "") + '\n';
79 desc += '- ' + rawMessage.split('\n')[0].replace(/\n+$/, "") + '\n';
80 });
80 });
81 // only 1 commit, use commit message as title
81 // only 1 commit, use commit message as title
82 if (elements.length === 1) {
82 if (elements.length === 1) {
83 title = $(elements[0]).find('td.td-description .message').data('messageRaw').split('\n')[0];
83 title = $(elements[0]).find('td.td-description .message').data('messageRaw').split('\n')[0];
84 }
84 }
85 else {
85 else {
86 // use reference name
86 // use reference name
87 title = sourceRef.replace(/-/g, ' ').replace(/_/g, ' ').capitalizeFirstLetter();
87 title = sourceRef.replace(/-/g, ' ').replace(/_/g, ' ').capitalizeFirstLetter();
88 }
88 }
89
89
90 return [title, desc]
90 return [title, desc]
91 };
91 };
92
92
93
93
94
94
95 ReviewersController = function () {
95 ReviewersController = function () {
96 var self = this;
96 var self = this;
97 this.$reviewRulesContainer = $('#review_rules');
97 this.$reviewRulesContainer = $('#review_rules');
98 this.$rulesList = this.$reviewRulesContainer.find('.pr-reviewer-rules');
98 this.$rulesList = this.$reviewRulesContainer.find('.pr-reviewer-rules');
99 this.forbidReviewUsers = undefined;
99 this.forbidReviewUsers = undefined;
100 this.$reviewMembers = $('#review_members');
100 this.$reviewMembers = $('#review_members');
101 this.currentRequest = null;
101 this.currentRequest = null;
102
102
103 this.defaultForbidReviewUsers = function() {
103 this.defaultForbidReviewUsers = function() {
104 return [
104 return [
105 {'username': 'default',
105 {'username': 'default',
106 'user_id': templateContext.default_user.user_id}
106 'user_id': templateContext.default_user.user_id}
107 ];
107 ];
108 };
108 };
109
109
110 this.hideReviewRules = function() {
110 this.hideReviewRules = function() {
111 self.$reviewRulesContainer.hide();
111 self.$reviewRulesContainer.hide();
112 };
112 };
113
113
114 this.showReviewRules = function() {
114 this.showReviewRules = function() {
115 self.$reviewRulesContainer.show();
115 self.$reviewRulesContainer.show();
116 };
116 };
117
117
118 this.addRule = function(ruleText) {
118 this.addRule = function(ruleText) {
119 self.showReviewRules();
119 self.showReviewRules();
120 return '<div>- {0}</div>'.format(ruleText)
120 return '<div>- {0}</div>'.format(ruleText)
121 };
121 };
122
122
123 this.loadReviewRules = function(data) {
123 this.loadReviewRules = function(data) {
124 // reset forbidden Users
124 // reset forbidden Users
125 this.forbidReviewUsers = self.defaultForbidReviewUsers();
125 this.forbidReviewUsers = self.defaultForbidReviewUsers();
126
126
127 // reset state of review rules
127 // reset state of review rules
128 self.$rulesList.html('');
128 self.$rulesList.html('');
129
129
130 if (!data || data.rules === undefined || $.isEmptyObject(data.rules)) {
130 if (!data || data.rules === undefined || $.isEmptyObject(data.rules)) {
131 // default rule, case for older repo that don't have any rules stored
131 // default rule, case for older repo that don't have any rules stored
132 self.$rulesList.append(
132 self.$rulesList.append(
133 self.addRule(
133 self.addRule(
134 _gettext('All reviewers must vote.'))
134 _gettext('All reviewers must vote.'))
135 );
135 );
136 return self.forbidReviewUsers
136 return self.forbidReviewUsers
137 }
137 }
138
138
139 if (data.rules.voting !== undefined) {
139 if (data.rules.voting !== undefined) {
140 if (data.rules.voting < 0) {
140 if (data.rules.voting < 0) {
141 self.$rulesList.append(
141 self.$rulesList.append(
142 self.addRule(
142 self.addRule(
143 _gettext('All individual reviewers must vote.'))
143 _gettext('All individual reviewers must vote.'))
144 )
144 )
145 } else if (data.rules.voting === 1) {
145 } else if (data.rules.voting === 1) {
146 self.$rulesList.append(
146 self.$rulesList.append(
147 self.addRule(
147 self.addRule(
148 _gettext('At least {0} reviewer must vote.').format(data.rules.voting))
148 _gettext('At least {0} reviewer must vote.').format(data.rules.voting))
149 )
149 )
150
150
151 } else {
151 } else {
152 self.$rulesList.append(
152 self.$rulesList.append(
153 self.addRule(
153 self.addRule(
154 _gettext('At least {0} reviewers must vote.').format(data.rules.voting))
154 _gettext('At least {0} reviewers must vote.').format(data.rules.voting))
155 )
155 )
156 }
156 }
157 }
157 }
158
158
159 if (data.rules.voting_groups !== undefined) {
159 if (data.rules.voting_groups !== undefined) {
160 $.each(data.rules.voting_groups, function(index, rule_data) {
160 $.each(data.rules.voting_groups, function(index, rule_data) {
161 self.$rulesList.append(
161 self.$rulesList.append(
162 self.addRule(rule_data.text)
162 self.addRule(rule_data.text)
163 )
163 )
164 });
164 });
165 }
165 }
166
166
167 if (data.rules.use_code_authors_for_review) {
167 if (data.rules.use_code_authors_for_review) {
168 self.$rulesList.append(
168 self.$rulesList.append(
169 self.addRule(
169 self.addRule(
170 _gettext('Reviewers picked from source code changes.'))
170 _gettext('Reviewers picked from source code changes.'))
171 )
171 )
172 }
172 }
173 if (data.rules.forbid_adding_reviewers) {
173 if (data.rules.forbid_adding_reviewers) {
174 $('#add_reviewer_input').remove();
174 $('#add_reviewer_input').remove();
175 self.$rulesList.append(
175 self.$rulesList.append(
176 self.addRule(
176 self.addRule(
177 _gettext('Adding new reviewers is forbidden.'))
177 _gettext('Adding new reviewers is forbidden.'))
178 )
178 )
179 }
179 }
180 if (data.rules.forbid_author_to_review) {
180 if (data.rules.forbid_author_to_review) {
181 self.forbidReviewUsers.push(data.rules_data.pr_author);
181 self.forbidReviewUsers.push(data.rules_data.pr_author);
182 self.$rulesList.append(
182 self.$rulesList.append(
183 self.addRule(
183 self.addRule(
184 _gettext('Author is not allowed to be a reviewer.'))
184 _gettext('Author is not allowed to be a reviewer.'))
185 )
185 )
186 }
186 }
187 if (data.rules.forbid_commit_author_to_review) {
187 if (data.rules.forbid_commit_author_to_review) {
188
188
189 if (data.rules_data.forbidden_users) {
189 if (data.rules_data.forbidden_users) {
190 $.each(data.rules_data.forbidden_users, function(index, member_data) {
190 $.each(data.rules_data.forbidden_users, function(index, member_data) {
191 self.forbidReviewUsers.push(member_data)
191 self.forbidReviewUsers.push(member_data)
192 });
192 });
193
193
194 }
194 }
195
195
196 self.$rulesList.append(
196 self.$rulesList.append(
197 self.addRule(
197 self.addRule(
198 _gettext('Commit Authors are not allowed to be a reviewer.'))
198 _gettext('Commit Authors are not allowed to be a reviewer.'))
199 )
199 )
200 }
200 }
201
201
202 return self.forbidReviewUsers
202 return self.forbidReviewUsers
203 };
203 };
204
204
205 this.loadDefaultReviewers = function(sourceRepo, sourceRef, targetRepo, targetRef) {
205 this.loadDefaultReviewers = function(sourceRepo, sourceRef, targetRepo, targetRef) {
206
206
207 if (self.currentRequest) {
207 if (self.currentRequest) {
208 // make sure we cleanup old running requests before triggering this
208 // make sure we cleanup old running requests before triggering this
209 // again
209 // again
210 self.currentRequest.abort();
210 self.currentRequest.abort();
211 }
211 }
212
212
213 $('.calculate-reviewers').show();
213 $('.calculate-reviewers').show();
214 // reset reviewer members
214 // reset reviewer members
215 self.$reviewMembers.empty();
215 self.$reviewMembers.empty();
216
216
217 prButtonLock(true, null, 'reviewers');
217 prButtonLock(true, null, 'reviewers');
218 $('#user').hide(); // hide user autocomplete before load
218 $('#user').hide(); // hide user autocomplete before load
219
219
220 if (sourceRef.length !== 3 || targetRef.length !== 3) {
220 if (sourceRef.length !== 3 || targetRef.length !== 3) {
221 // don't load defaults in case we're missing some refs...
221 // don't load defaults in case we're missing some refs...
222 $('.calculate-reviewers').hide();
222 $('.calculate-reviewers').hide();
223 return
223 return
224 }
224 }
225
225
226 var url = pyroutes.url('repo_default_reviewers_data',
226 var url = pyroutes.url('repo_default_reviewers_data',
227 {
227 {
228 'repo_name': templateContext.repo_name,
228 'repo_name': templateContext.repo_name,
229 'source_repo': sourceRepo,
229 'source_repo': sourceRepo,
230 'source_ref': sourceRef[2],
230 'source_ref': sourceRef[2],
231 'target_repo': targetRepo,
231 'target_repo': targetRepo,
232 'target_ref': targetRef[2]
232 'target_ref': targetRef[2]
233 });
233 });
234
234
235 self.currentRequest = $.get(url)
235 self.currentRequest = $.get(url)
236 .done(function(data) {
236 .done(function(data) {
237 self.currentRequest = null;
237 self.currentRequest = null;
238
238
239 // review rules
239 // review rules
240 self.loadReviewRules(data);
240 self.loadReviewRules(data);
241
241
242 for (var i = 0; i < data.reviewers.length; i++) {
242 for (var i = 0; i < data.reviewers.length; i++) {
243 var reviewer = data.reviewers[i];
243 var reviewer = data.reviewers[i];
244 self.addReviewMember(
244 self.addReviewMember(
245 reviewer, reviewer.reasons, reviewer.mandatory);
245 reviewer, reviewer.reasons, reviewer.mandatory);
246 }
246 }
247 $('.calculate-reviewers').hide();
247 $('.calculate-reviewers').hide();
248 prButtonLock(false, null, 'reviewers');
248 prButtonLock(false, null, 'reviewers');
249 $('#user').show(); // show user autocomplete after load
249 $('#user').show(); // show user autocomplete after load
250 });
250 });
251 };
251 };
252
252
253 // check those, refactor
253 // check those, refactor
254 this.removeReviewMember = function(reviewer_id, mark_delete) {
254 this.removeReviewMember = function(reviewer_id, mark_delete) {
255 var reviewer = $('#reviewer_{0}'.format(reviewer_id));
255 var reviewer = $('#reviewer_{0}'.format(reviewer_id));
256
256
257 if(typeof(mark_delete) === undefined){
257 if(typeof(mark_delete) === undefined){
258 mark_delete = false;
258 mark_delete = false;
259 }
259 }
260
260
261 if(mark_delete === true){
261 if(mark_delete === true){
262 if (reviewer){
262 if (reviewer){
263 // now delete the input
263 // now delete the input
264 $('#reviewer_{0} input'.format(reviewer_id)).remove();
264 $('#reviewer_{0} input'.format(reviewer_id)).remove();
265 // mark as to-delete
265 // mark as to-delete
266 var obj = $('#reviewer_{0}_name'.format(reviewer_id));
266 var obj = $('#reviewer_{0}_name'.format(reviewer_id));
267 obj.addClass('to-delete');
267 obj.addClass('to-delete');
268 obj.css({"text-decoration":"line-through", "opacity": 0.5});
268 obj.css({"text-decoration":"line-through", "opacity": 0.5});
269 }
269 }
270 }
270 }
271 else{
271 else{
272 $('#reviewer_{0}'.format(reviewer_id)).remove();
272 $('#reviewer_{0}'.format(reviewer_id)).remove();
273 }
273 }
274 };
274 };
275 this.reviewMemberEntry = function() {
275 this.reviewMemberEntry = function() {
276
276
277 };
277 };
278 this.addReviewMember = function(reviewer_obj, reasons, mandatory) {
278 this.addReviewMember = function(reviewer_obj, reasons, mandatory) {
279 var members = self.$reviewMembers.get(0);
279 var members = self.$reviewMembers.get(0);
280 var id = reviewer_obj.user_id;
280 var id = reviewer_obj.user_id;
281 var username = reviewer_obj.username;
281 var username = reviewer_obj.username;
282
282
283 var reasons = reasons || [];
283 var reasons = reasons || [];
284 var mandatory = mandatory || false;
284 var mandatory = mandatory || false;
285
285
286 // register IDS to check if we don't have this ID already in
286 // register IDS to check if we don't have this ID already in
287 var currentIds = [];
287 var currentIds = [];
288 var _els = self.$reviewMembers.find('li').toArray();
288 var _els = self.$reviewMembers.find('li').toArray();
289 for (el in _els){
289 for (el in _els){
290 currentIds.push(_els[el].id)
290 currentIds.push(_els[el].id)
291 }
291 }
292
292
293 var userAllowedReview = function(userId) {
293 var userAllowedReview = function(userId) {
294 var allowed = true;
294 var allowed = true;
295 $.each(self.forbidReviewUsers, function(index, member_data) {
295 $.each(self.forbidReviewUsers, function(index, member_data) {
296 if (parseInt(userId) === member_data['user_id']) {
296 if (parseInt(userId) === member_data['user_id']) {
297 allowed = false;
297 allowed = false;
298 return false // breaks the loop
298 return false // breaks the loop
299 }
299 }
300 });
300 });
301 return allowed
301 return allowed
302 };
302 };
303
303
304 var userAllowed = userAllowedReview(id);
304 var userAllowed = userAllowedReview(id);
305 if (!userAllowed){
305 if (!userAllowed){
306 alert(_gettext('User `{0}` not allowed to be a reviewer').format(username));
306 alert(_gettext('User `{0}` not allowed to be a reviewer').format(username));
307 } else {
307 } else {
308 // only add if it's not there
308 // only add if it's not there
309 var alreadyReviewer = currentIds.indexOf('reviewer_'+id) != -1;
309 var alreadyReviewer = currentIds.indexOf('reviewer_'+id) != -1;
310
310
311 if (alreadyReviewer) {
311 if (alreadyReviewer) {
312 alert(_gettext('User `{0}` already in reviewers').format(username));
312 alert(_gettext('User `{0}` already in reviewers').format(username));
313 } else {
313 } else {
314 members.innerHTML += renderTemplate('reviewMemberEntry', {
314 members.innerHTML += renderTemplate('reviewMemberEntry', {
315 'member': reviewer_obj,
315 'member': reviewer_obj,
316 'mandatory': mandatory,
316 'mandatory': mandatory,
317 'allowed_to_update': true,
317 'allowed_to_update': true,
318 'review_status': 'not_reviewed',
318 'review_status': 'not_reviewed',
319 'review_status_label': _gettext('Not Reviewed'),
319 'review_status_label': _gettext('Not Reviewed'),
320 'reasons': reasons,
320 'reasons': reasons,
321 'create': true
321 'create': true
322 });
322 });
323 }
323 }
324 }
324 }
325
325
326 };
326 };
327
327
328 this.updateReviewers = function(repo_name, pull_request_id){
328 this.updateReviewers = function(repo_name, pull_request_id){
329 var postData = $('#reviewers input').serialize();
329 var postData = $('#reviewers input').serialize();
330 _updatePullRequest(repo_name, pull_request_id, postData);
330 _updatePullRequest(repo_name, pull_request_id, postData);
331 };
331 };
332
332
333 };
333 };
334
334
335
335
336 var _updatePullRequest = function(repo_name, pull_request_id, postData) {
336 var _updatePullRequest = function(repo_name, pull_request_id, postData) {
337 var url = pyroutes.url(
337 var url = pyroutes.url(
338 'pullrequest_update',
338 'pullrequest_update',
339 {"repo_name": repo_name, "pull_request_id": pull_request_id});
339 {"repo_name": repo_name, "pull_request_id": pull_request_id});
340 if (typeof postData === 'string' ) {
340 if (typeof postData === 'string' ) {
341 postData += '&csrf_token=' + CSRF_TOKEN;
341 postData += '&csrf_token=' + CSRF_TOKEN;
342 } else {
342 } else {
343 postData.csrf_token = CSRF_TOKEN;
343 postData.csrf_token = CSRF_TOKEN;
344 }
344 }
345 var success = function(o) {
345 var success = function(o) {
346 window.location.reload();
346 window.location.reload();
347 };
347 };
348 ajaxPOST(url, postData, success);
348 ajaxPOST(url, postData, success);
349 };
349 };
350
350
351 /**
351 /**
352 * PULL REQUEST update commits
352 * PULL REQUEST update commits
353 */
353 */
354 var updateCommits = function(repo_name, pull_request_id) {
354 var updateCommits = function(repo_name, pull_request_id) {
355 var postData = {
355 var postData = {
356 'update_commits': true};
356 'update_commits': true};
357 _updatePullRequest(repo_name, pull_request_id, postData);
357 _updatePullRequest(repo_name, pull_request_id, postData);
358 };
358 };
359
359
360
360
361 /**
361 /**
362 * PULL REQUEST edit info
362 * PULL REQUEST edit info
363 */
363 */
364 var editPullRequest = function(repo_name, pull_request_id, title, description) {
364 var editPullRequest = function(repo_name, pull_request_id, title, description, renderer) {
365 var url = pyroutes.url(
365 var url = pyroutes.url(
366 'pullrequest_update',
366 'pullrequest_update',
367 {"repo_name": repo_name, "pull_request_id": pull_request_id});
367 {"repo_name": repo_name, "pull_request_id": pull_request_id});
368
368
369 var postData = {
369 var postData = {
370 'title': title,
370 'title': title,
371 'description': description,
371 'description': description,
372 'description_renderer': renderer,
372 'edit_pull_request': true,
373 'edit_pull_request': true,
373 'csrf_token': CSRF_TOKEN
374 'csrf_token': CSRF_TOKEN
374 };
375 };
375 var success = function(o) {
376 var success = function(o) {
376 window.location.reload();
377 window.location.reload();
377 };
378 };
378 ajaxPOST(url, postData, success);
379 ajaxPOST(url, postData, success);
379 };
380 };
380
381
381
382
382 /**
383 /**
383 * Reviewer autocomplete
384 * Reviewer autocomplete
384 */
385 */
385 var ReviewerAutoComplete = function(inputId) {
386 var ReviewerAutoComplete = function(inputId) {
386 $(inputId).autocomplete({
387 $(inputId).autocomplete({
387 serviceUrl: pyroutes.url('user_autocomplete_data'),
388 serviceUrl: pyroutes.url('user_autocomplete_data'),
388 minChars:2,
389 minChars:2,
389 maxHeight:400,
390 maxHeight:400,
390 deferRequestBy: 300, //miliseconds
391 deferRequestBy: 300, //miliseconds
391 showNoSuggestionNotice: true,
392 showNoSuggestionNotice: true,
392 tabDisabled: true,
393 tabDisabled: true,
393 autoSelectFirst: true,
394 autoSelectFirst: true,
394 params: { user_id: templateContext.rhodecode_user.user_id, user_groups:true, user_groups_expand:true, skip_default_user:true },
395 params: { user_id: templateContext.rhodecode_user.user_id, user_groups:true, user_groups_expand:true, skip_default_user:true },
395 formatResult: autocompleteFormatResult,
396 formatResult: autocompleteFormatResult,
396 lookupFilter: autocompleteFilterResult,
397 lookupFilter: autocompleteFilterResult,
397 onSelect: function(element, data) {
398 onSelect: function(element, data) {
398 var mandatory = false;
399 var mandatory = false;
399 var reasons = [_gettext('added manually by "{0}"').format(templateContext.rhodecode_user.username)];
400 var reasons = [_gettext('added manually by "{0}"').format(templateContext.rhodecode_user.username)];
400
401
401 // add whole user groups
402 // add whole user groups
402 if (data.value_type == 'user_group') {
403 if (data.value_type == 'user_group') {
403 reasons.push(_gettext('member of "{0}"').format(data.value_display));
404 reasons.push(_gettext('member of "{0}"').format(data.value_display));
404
405
405 $.each(data.members, function(index, member_data) {
406 $.each(data.members, function(index, member_data) {
406 var reviewer = member_data;
407 var reviewer = member_data;
407 reviewer['user_id'] = member_data['id'];
408 reviewer['user_id'] = member_data['id'];
408 reviewer['gravatar_link'] = member_data['icon_link'];
409 reviewer['gravatar_link'] = member_data['icon_link'];
409 reviewer['user_link'] = member_data['profile_link'];
410 reviewer['user_link'] = member_data['profile_link'];
410 reviewer['rules'] = [];
411 reviewer['rules'] = [];
411 reviewersController.addReviewMember(reviewer, reasons, mandatory);
412 reviewersController.addReviewMember(reviewer, reasons, mandatory);
412 })
413 })
413 }
414 }
414 // add single user
415 // add single user
415 else {
416 else {
416 var reviewer = data;
417 var reviewer = data;
417 reviewer['user_id'] = data['id'];
418 reviewer['user_id'] = data['id'];
418 reviewer['gravatar_link'] = data['icon_link'];
419 reviewer['gravatar_link'] = data['icon_link'];
419 reviewer['user_link'] = data['profile_link'];
420 reviewer['user_link'] = data['profile_link'];
420 reviewer['rules'] = [];
421 reviewer['rules'] = [];
421 reviewersController.addReviewMember(reviewer, reasons, mandatory);
422 reviewersController.addReviewMember(reviewer, reasons, mandatory);
422 }
423 }
423
424
424 $(inputId).val('');
425 $(inputId).val('');
425 }
426 }
426 });
427 });
427 };
428 };
428
429
429
430
430 VersionController = function () {
431 VersionController = function () {
431 var self = this;
432 var self = this;
432 this.$verSource = $('input[name=ver_source]');
433 this.$verSource = $('input[name=ver_source]');
433 this.$verTarget = $('input[name=ver_target]');
434 this.$verTarget = $('input[name=ver_target]');
434 this.$showVersionDiff = $('#show-version-diff');
435 this.$showVersionDiff = $('#show-version-diff');
435
436
436 this.adjustRadioSelectors = function (curNode) {
437 this.adjustRadioSelectors = function (curNode) {
437 var getVal = function (item) {
438 var getVal = function (item) {
438 if (item == 'latest') {
439 if (item == 'latest') {
439 return Number.MAX_SAFE_INTEGER
440 return Number.MAX_SAFE_INTEGER
440 }
441 }
441 else {
442 else {
442 return parseInt(item)
443 return parseInt(item)
443 }
444 }
444 };
445 };
445
446
446 var curVal = getVal($(curNode).val());
447 var curVal = getVal($(curNode).val());
447 var cleared = false;
448 var cleared = false;
448
449
449 $.each(self.$verSource, function (index, value) {
450 $.each(self.$verSource, function (index, value) {
450 var elVal = getVal($(value).val());
451 var elVal = getVal($(value).val());
451
452
452 if (elVal > curVal) {
453 if (elVal > curVal) {
453 if ($(value).is(':checked')) {
454 if ($(value).is(':checked')) {
454 cleared = true;
455 cleared = true;
455 }
456 }
456 $(value).attr('disabled', 'disabled');
457 $(value).attr('disabled', 'disabled');
457 $(value).removeAttr('checked');
458 $(value).removeAttr('checked');
458 $(value).css({'opacity': 0.1});
459 $(value).css({'opacity': 0.1});
459 }
460 }
460 else {
461 else {
461 $(value).css({'opacity': 1});
462 $(value).css({'opacity': 1});
462 $(value).removeAttr('disabled');
463 $(value).removeAttr('disabled');
463 }
464 }
464 });
465 });
465
466
466 if (cleared) {
467 if (cleared) {
467 // if we unchecked an active, set the next one to same loc.
468 // if we unchecked an active, set the next one to same loc.
468 $(this.$verSource).filter('[value={0}]'.format(
469 $(this.$verSource).filter('[value={0}]'.format(
469 curVal)).attr('checked', 'checked');
470 curVal)).attr('checked', 'checked');
470 }
471 }
471
472
472 self.setLockAction(false,
473 self.setLockAction(false,
473 $(curNode).data('verPos'),
474 $(curNode).data('verPos'),
474 $(this.$verSource).filter(':checked').data('verPos')
475 $(this.$verSource).filter(':checked').data('verPos')
475 );
476 );
476 };
477 };
477
478
478
479
479 this.attachVersionListener = function () {
480 this.attachVersionListener = function () {
480 self.$verTarget.change(function (e) {
481 self.$verTarget.change(function (e) {
481 self.adjustRadioSelectors(this)
482 self.adjustRadioSelectors(this)
482 });
483 });
483 self.$verSource.change(function (e) {
484 self.$verSource.change(function (e) {
484 self.adjustRadioSelectors(self.$verTarget.filter(':checked'))
485 self.adjustRadioSelectors(self.$verTarget.filter(':checked'))
485 });
486 });
486 };
487 };
487
488
488 this.init = function () {
489 this.init = function () {
489
490
490 var curNode = self.$verTarget.filter(':checked');
491 var curNode = self.$verTarget.filter(':checked');
491 self.adjustRadioSelectors(curNode);
492 self.adjustRadioSelectors(curNode);
492 self.setLockAction(true);
493 self.setLockAction(true);
493 self.attachVersionListener();
494 self.attachVersionListener();
494
495
495 };
496 };
496
497
497 this.setLockAction = function (state, selectedVersion, otherVersion) {
498 this.setLockAction = function (state, selectedVersion, otherVersion) {
498 var $showVersionDiff = this.$showVersionDiff;
499 var $showVersionDiff = this.$showVersionDiff;
499
500
500 if (state) {
501 if (state) {
501 $showVersionDiff.attr('disabled', 'disabled');
502 $showVersionDiff.attr('disabled', 'disabled');
502 $showVersionDiff.addClass('disabled');
503 $showVersionDiff.addClass('disabled');
503 $showVersionDiff.html($showVersionDiff.data('labelTextLocked'));
504 $showVersionDiff.html($showVersionDiff.data('labelTextLocked'));
504 }
505 }
505 else {
506 else {
506 $showVersionDiff.removeAttr('disabled');
507 $showVersionDiff.removeAttr('disabled');
507 $showVersionDiff.removeClass('disabled');
508 $showVersionDiff.removeClass('disabled');
508
509
509 if (selectedVersion == otherVersion) {
510 if (selectedVersion == otherVersion) {
510 $showVersionDiff.html($showVersionDiff.data('labelTextShow'));
511 $showVersionDiff.html($showVersionDiff.data('labelTextShow'));
511 } else {
512 } else {
512 $showVersionDiff.html($showVersionDiff.data('labelTextDiff'));
513 $showVersionDiff.html($showVersionDiff.data('labelTextDiff'));
513 }
514 }
514 }
515 }
515
516
516 };
517 };
517
518
518 this.showVersionDiff = function () {
519 this.showVersionDiff = function () {
519 var target = self.$verTarget.filter(':checked');
520 var target = self.$verTarget.filter(':checked');
520 var source = self.$verSource.filter(':checked');
521 var source = self.$verSource.filter(':checked');
521
522
522 if (target.val() && source.val()) {
523 if (target.val() && source.val()) {
523 var params = {
524 var params = {
524 'pull_request_id': templateContext.pull_request_data.pull_request_id,
525 'pull_request_id': templateContext.pull_request_data.pull_request_id,
525 'repo_name': templateContext.repo_name,
526 'repo_name': templateContext.repo_name,
526 'version': target.val(),
527 'version': target.val(),
527 'from_version': source.val()
528 'from_version': source.val()
528 };
529 };
529 window.location = pyroutes.url('pullrequest_show', params)
530 window.location = pyroutes.url('pullrequest_show', params)
530 }
531 }
531
532
532 return false;
533 return false;
533 };
534 };
534
535
535 this.toggleVersionView = function (elem) {
536 this.toggleVersionView = function (elem) {
536
537
537 if (this.$showVersionDiff.is(':visible')) {
538 if (this.$showVersionDiff.is(':visible')) {
538 $('.version-pr').hide();
539 $('.version-pr').hide();
539 this.$showVersionDiff.hide();
540 this.$showVersionDiff.hide();
540 $(elem).html($(elem).data('toggleOn'))
541 $(elem).html($(elem).data('toggleOn'))
541 } else {
542 } else {
542 $('.version-pr').show();
543 $('.version-pr').show();
543 this.$showVersionDiff.show();
544 this.$showVersionDiff.show();
544 $(elem).html($(elem).data('toggleOff'))
545 $(elem).html($(elem).data('toggleOff'))
545 }
546 }
546
547
547 return false
548 return false
548 }
549 }
549
550
550 }; No newline at end of file
551 };
@@ -1,547 +1,548 b''
1 <%inherit file="/base/base.mako"/>
1 <%inherit file="/base/base.mako"/>
2 <%namespace name="dt" file="/data_table/_dt_elements.mako"/>
2 <%namespace name="dt" file="/data_table/_dt_elements.mako"/>
3
3
4 <%def name="title()">
4 <%def name="title()">
5 ${c.repo_name} ${_('New pull request')}
5 ${c.repo_name} ${_('New pull request')}
6 </%def>
6 </%def>
7
7
8 <%def name="breadcrumbs_links()">
8 <%def name="breadcrumbs_links()">
9 ${_('New pull request')}
9 ${_('New pull request')}
10 </%def>
10 </%def>
11
11
12 <%def name="menu_bar_nav()">
12 <%def name="menu_bar_nav()">
13 ${self.menu_items(active='repositories')}
13 ${self.menu_items(active='repositories')}
14 </%def>
14 </%def>
15
15
16 <%def name="menu_bar_subnav()">
16 <%def name="menu_bar_subnav()">
17 ${self.repo_menu(active='showpullrequest')}
17 ${self.repo_menu(active='showpullrequest')}
18 </%def>
18 </%def>
19
19
20 <%def name="main()">
20 <%def name="main()">
21 <div class="box">
21 <div class="box">
22 <div class="title">
22 <div class="title">
23 ${self.repo_page_title(c.rhodecode_db_repo)}
23 ${self.repo_page_title(c.rhodecode_db_repo)}
24 </div>
24 </div>
25
25
26 ${h.secure_form(h.route_path('pullrequest_create', repo_name=c.repo_name, _query=request.GET.mixed()), id='pull_request_form', request=request)}
26 ${h.secure_form(h.route_path('pullrequest_create', repo_name=c.repo_name, _query=request.GET.mixed()), id='pull_request_form', request=request)}
27
27
28 ${self.breadcrumbs()}
28 ${self.breadcrumbs()}
29
29
30 <div class="box pr-summary">
30 <div class="box pr-summary">
31
31
32 <div class="summary-details block-left">
32 <div class="summary-details block-left">
33
33
34
34
35 <div class="pr-details-title">
35 <div class="pr-details-title">
36 ${_('Pull request summary')}
36 ${_('Pull request summary')}
37 </div>
37 </div>
38
38
39 <div class="form" style="padding-top: 10px">
39 <div class="form" style="padding-top: 10px">
40 <!-- fields -->
40 <!-- fields -->
41
41
42 <div class="fields" >
42 <div class="fields" >
43
43
44 <div class="field">
44 <div class="field">
45 <div class="label">
45 <div class="label">
46 <label for="pullrequest_title">${_('Title')}:</label>
46 <label for="pullrequest_title">${_('Title')}:</label>
47 </div>
47 </div>
48 <div class="input">
48 <div class="input">
49 ${h.text('pullrequest_title', c.default_title, class_="medium autogenerated-title")}
49 ${h.text('pullrequest_title', c.default_title, class_="medium autogenerated-title")}
50 </div>
50 </div>
51 </div>
51 </div>
52
52
53 <div class="field">
53 <div class="field">
54 <div class="label label-textarea">
54 <div class="label label-textarea">
55 <label for="pullrequest_desc">${_('Description')}:</label>
55 <label for="pullrequest_desc">${_('Description')}:</label>
56 </div>
56 </div>
57 <div class="textarea text-area editor">
57 <div class="textarea text-area editor">
58 <input id="pr-renderer-input" type="hidden" name="description_renderer" value="${c.visual.default_renderer}">
58 ${dt.markup_form('pullrequest_desc')}
59 ${dt.markup_form('pullrequest_desc')}
59 </div>
60 </div>
60 </div>
61 </div>
61
62
62 <div class="field">
63 <div class="field">
63 <div class="label label-textarea">
64 <div class="label label-textarea">
64 <label for="commit_flow">${_('Commit flow')}:</label>
65 <label for="commit_flow">${_('Commit flow')}:</label>
65 </div>
66 </div>
66
67
67 ## TODO: johbo: Abusing the "content" class here to get the
68 ## TODO: johbo: Abusing the "content" class here to get the
68 ## desired effect. Should be replaced by a proper solution.
69 ## desired effect. Should be replaced by a proper solution.
69
70
70 ##ORG
71 ##ORG
71 <div class="content">
72 <div class="content">
72 <strong>${_('Source repository')}:</strong>
73 <strong>${_('Source repository')}:</strong>
73 ${c.rhodecode_db_repo.description}
74 ${c.rhodecode_db_repo.description}
74 </div>
75 </div>
75 <div class="content">
76 <div class="content">
76 ${h.hidden('source_repo')}
77 ${h.hidden('source_repo')}
77 ${h.hidden('source_ref')}
78 ${h.hidden('source_ref')}
78 </div>
79 </div>
79
80
80 ##OTHER, most Probably the PARENT OF THIS FORK
81 ##OTHER, most Probably the PARENT OF THIS FORK
81 <div class="content">
82 <div class="content">
82 ## filled with JS
83 ## filled with JS
83 <div id="target_repo_desc"></div>
84 <div id="target_repo_desc"></div>
84 </div>
85 </div>
85
86
86 <div class="content">
87 <div class="content">
87 ${h.hidden('target_repo')}
88 ${h.hidden('target_repo')}
88 ${h.hidden('target_ref')}
89 ${h.hidden('target_ref')}
89 <span id="target_ref_loading" style="display: none">
90 <span id="target_ref_loading" style="display: none">
90 ${_('Loading refs...')}
91 ${_('Loading refs...')}
91 </span>
92 </span>
92 </div>
93 </div>
93 </div>
94 </div>
94
95
95 <div class="field">
96 <div class="field">
96 <div class="label label-textarea">
97 <div class="label label-textarea">
97 <label for="pullrequest_submit"></label>
98 <label for="pullrequest_submit"></label>
98 </div>
99 </div>
99 <div class="input">
100 <div class="input">
100 <div class="pr-submit-button">
101 <div class="pr-submit-button">
101 <input id="pr_submit" class="btn" name="save" type="submit" value="${_('Submit Pull Request')}">
102 <input id="pr_submit" class="btn" name="save" type="submit" value="${_('Submit Pull Request')}">
102 </div>
103 </div>
103 <div id="pr_open_message"></div>
104 <div id="pr_open_message"></div>
104 </div>
105 </div>
105 </div>
106 </div>
106
107
107 <div class="pr-spacing-container"></div>
108 <div class="pr-spacing-container"></div>
108 </div>
109 </div>
109 </div>
110 </div>
110 </div>
111 </div>
111 <div>
112 <div>
112 ## AUTHOR
113 ## AUTHOR
113 <div class="reviewers-title block-right">
114 <div class="reviewers-title block-right">
114 <div class="pr-details-title">
115 <div class="pr-details-title">
115 ${_('Author of this pull request')}
116 ${_('Author of this pull request')}
116 </div>
117 </div>
117 </div>
118 </div>
118 <div class="block-right pr-details-content reviewers">
119 <div class="block-right pr-details-content reviewers">
119 <ul class="group_members">
120 <ul class="group_members">
120 <li>
121 <li>
121 ${self.gravatar_with_user(c.rhodecode_user.email, 16)}
122 ${self.gravatar_with_user(c.rhodecode_user.email, 16)}
122 </li>
123 </li>
123 </ul>
124 </ul>
124 </div>
125 </div>
125
126
126 ## REVIEW RULES
127 ## REVIEW RULES
127 <div id="review_rules" style="display: none" class="reviewers-title block-right">
128 <div id="review_rules" style="display: none" class="reviewers-title block-right">
128 <div class="pr-details-title">
129 <div class="pr-details-title">
129 ${_('Reviewer rules')}
130 ${_('Reviewer rules')}
130 </div>
131 </div>
131 <div class="pr-reviewer-rules">
132 <div class="pr-reviewer-rules">
132 ## review rules will be appended here, by default reviewers logic
133 ## review rules will be appended here, by default reviewers logic
133 </div>
134 </div>
134 </div>
135 </div>
135
136
136 ## REVIEWERS
137 ## REVIEWERS
137 <div class="reviewers-title block-right">
138 <div class="reviewers-title block-right">
138 <div class="pr-details-title">
139 <div class="pr-details-title">
139 ${_('Pull request reviewers')}
140 ${_('Pull request reviewers')}
140 <span class="calculate-reviewers"> - ${_('loading...')}</span>
141 <span class="calculate-reviewers"> - ${_('loading...')}</span>
141 </div>
142 </div>
142 </div>
143 </div>
143 <div id="reviewers" class="block-right pr-details-content reviewers">
144 <div id="reviewers" class="block-right pr-details-content reviewers">
144 ## members goes here, filled via JS based on initial selection !
145 ## members goes here, filled via JS based on initial selection !
145 <input type="hidden" name="__start__" value="review_members:sequence">
146 <input type="hidden" name="__start__" value="review_members:sequence">
146 <ul id="review_members" class="group_members"></ul>
147 <ul id="review_members" class="group_members"></ul>
147 <input type="hidden" name="__end__" value="review_members:sequence">
148 <input type="hidden" name="__end__" value="review_members:sequence">
148 <div id="add_reviewer_input" class='ac'>
149 <div id="add_reviewer_input" class='ac'>
149 <div class="reviewer_ac">
150 <div class="reviewer_ac">
150 ${h.text('user', class_='ac-input', placeholder=_('Add reviewer or reviewer group'))}
151 ${h.text('user', class_='ac-input', placeholder=_('Add reviewer or reviewer group'))}
151 <div id="reviewers_container"></div>
152 <div id="reviewers_container"></div>
152 </div>
153 </div>
153 </div>
154 </div>
154 </div>
155 </div>
155 </div>
156 </div>
156 </div>
157 </div>
157 <div class="box">
158 <div class="box">
158 <div>
159 <div>
159 ## overview pulled by ajax
160 ## overview pulled by ajax
160 <div id="pull_request_overview"></div>
161 <div id="pull_request_overview"></div>
161 </div>
162 </div>
162 </div>
163 </div>
163 ${h.end_form()}
164 ${h.end_form()}
164 </div>
165 </div>
165
166
166 <script type="text/javascript">
167 <script type="text/javascript">
167 $(function(){
168 $(function(){
168 var defaultSourceRepo = '${c.default_repo_data['source_repo_name']}';
169 var defaultSourceRepo = '${c.default_repo_data['source_repo_name']}';
169 var defaultSourceRepoData = ${c.default_repo_data['source_refs_json']|n};
170 var defaultSourceRepoData = ${c.default_repo_data['source_refs_json']|n};
170 var defaultTargetRepo = '${c.default_repo_data['target_repo_name']}';
171 var defaultTargetRepo = '${c.default_repo_data['target_repo_name']}';
171 var defaultTargetRepoData = ${c.default_repo_data['target_refs_json']|n};
172 var defaultTargetRepoData = ${c.default_repo_data['target_refs_json']|n};
172
173
173 var $pullRequestForm = $('#pull_request_form');
174 var $pullRequestForm = $('#pull_request_form');
174 var $pullRequestSubmit = $('#pr_submit', $pullRequestForm);
175 var $pullRequestSubmit = $('#pr_submit', $pullRequestForm);
175 var $sourceRepo = $('#source_repo', $pullRequestForm);
176 var $sourceRepo = $('#source_repo', $pullRequestForm);
176 var $targetRepo = $('#target_repo', $pullRequestForm);
177 var $targetRepo = $('#target_repo', $pullRequestForm);
177 var $sourceRef = $('#source_ref', $pullRequestForm);
178 var $sourceRef = $('#source_ref', $pullRequestForm);
178 var $targetRef = $('#target_ref', $pullRequestForm);
179 var $targetRef = $('#target_ref', $pullRequestForm);
179
180
180 var sourceRepo = function() { return $sourceRepo.eq(0).val() };
181 var sourceRepo = function() { return $sourceRepo.eq(0).val() };
181 var sourceRef = function() { return $sourceRef.eq(0).val().split(':') };
182 var sourceRef = function() { return $sourceRef.eq(0).val().split(':') };
182
183
183 var targetRepo = function() { return $targetRepo.eq(0).val() };
184 var targetRepo = function() { return $targetRepo.eq(0).val() };
184 var targetRef = function() { return $targetRef.eq(0).val().split(':') };
185 var targetRef = function() { return $targetRef.eq(0).val().split(':') };
185
186
186 var calculateContainerWidth = function() {
187 var calculateContainerWidth = function() {
187 var maxWidth = 0;
188 var maxWidth = 0;
188 var repoSelect2Containers = ['#source_repo', '#target_repo'];
189 var repoSelect2Containers = ['#source_repo', '#target_repo'];
189 $.each(repoSelect2Containers, function(idx, value) {
190 $.each(repoSelect2Containers, function(idx, value) {
190 $(value).select2('container').width('auto');
191 $(value).select2('container').width('auto');
191 var curWidth = $(value).select2('container').width();
192 var curWidth = $(value).select2('container').width();
192 if (maxWidth <= curWidth) {
193 if (maxWidth <= curWidth) {
193 maxWidth = curWidth;
194 maxWidth = curWidth;
194 }
195 }
195 $.each(repoSelect2Containers, function(idx, value) {
196 $.each(repoSelect2Containers, function(idx, value) {
196 $(value).select2('container').width(maxWidth + 10);
197 $(value).select2('container').width(maxWidth + 10);
197 });
198 });
198 });
199 });
199 };
200 };
200
201
201 var initRefSelection = function(selectedRef) {
202 var initRefSelection = function(selectedRef) {
202 return function(element, callback) {
203 return function(element, callback) {
203 // translate our select2 id into a text, it's a mapping to show
204 // translate our select2 id into a text, it's a mapping to show
204 // simple label when selecting by internal ID.
205 // simple label when selecting by internal ID.
205 var id, refData;
206 var id, refData;
206 if (selectedRef === undefined || selectedRef === null) {
207 if (selectedRef === undefined || selectedRef === null) {
207 id = element.val();
208 id = element.val();
208 refData = element.val().split(':');
209 refData = element.val().split(':');
209
210
210 if (refData.length !== 3){
211 if (refData.length !== 3){
211 refData = ["", "", ""]
212 refData = ["", "", ""]
212 }
213 }
213 } else {
214 } else {
214 id = selectedRef;
215 id = selectedRef;
215 refData = selectedRef.split(':');
216 refData = selectedRef.split(':');
216 }
217 }
217
218
218 var text = refData[1];
219 var text = refData[1];
219 if (refData[0] === 'rev') {
220 if (refData[0] === 'rev') {
220 text = text.substring(0, 12);
221 text = text.substring(0, 12);
221 }
222 }
222
223
223 var data = {id: id, text: text};
224 var data = {id: id, text: text};
224 callback(data);
225 callback(data);
225 };
226 };
226 };
227 };
227
228
228 var formatRefSelection = function(item) {
229 var formatRefSelection = function(item) {
229 var prefix = '';
230 var prefix = '';
230 var refData = item.id.split(':');
231 var refData = item.id.split(':');
231 if (refData[0] === 'branch') {
232 if (refData[0] === 'branch') {
232 prefix = '<i class="icon-branch"></i>';
233 prefix = '<i class="icon-branch"></i>';
233 }
234 }
234 else if (refData[0] === 'book') {
235 else if (refData[0] === 'book') {
235 prefix = '<i class="icon-bookmark"></i>';
236 prefix = '<i class="icon-bookmark"></i>';
236 }
237 }
237 else if (refData[0] === 'tag') {
238 else if (refData[0] === 'tag') {
238 prefix = '<i class="icon-tag"></i>';
239 prefix = '<i class="icon-tag"></i>';
239 }
240 }
240
241
241 var originalOption = item.element;
242 var originalOption = item.element;
242 return prefix + item.text;
243 return prefix + item.text;
243 };
244 };
244
245
245 // custom code mirror
246 // custom code mirror
246 var codeMirrorInstance = $('#pullrequest_desc').get(0).MarkupForm.cm;
247 var codeMirrorInstance = $('#pullrequest_desc').get(0).MarkupForm.cm;
247
248
248 reviewersController = new ReviewersController();
249 reviewersController = new ReviewersController();
249
250
250 var queryTargetRepo = function(self, query) {
251 var queryTargetRepo = function(self, query) {
251 // cache ALL results if query is empty
252 // cache ALL results if query is empty
252 var cacheKey = query.term || '__';
253 var cacheKey = query.term || '__';
253 var cachedData = self.cachedDataSource[cacheKey];
254 var cachedData = self.cachedDataSource[cacheKey];
254
255
255 if (cachedData) {
256 if (cachedData) {
256 query.callback({results: cachedData.results});
257 query.callback({results: cachedData.results});
257 } else {
258 } else {
258 $.ajax({
259 $.ajax({
259 url: pyroutes.url('pullrequest_repo_destinations', {'repo_name': templateContext.repo_name}),
260 url: pyroutes.url('pullrequest_repo_destinations', {'repo_name': templateContext.repo_name}),
260 data: {query: query.term},
261 data: {query: query.term},
261 dataType: 'json',
262 dataType: 'json',
262 type: 'GET',
263 type: 'GET',
263 success: function(data) {
264 success: function(data) {
264 self.cachedDataSource[cacheKey] = data;
265 self.cachedDataSource[cacheKey] = data;
265 query.callback({results: data.results});
266 query.callback({results: data.results});
266 },
267 },
267 error: function(data, textStatus, errorThrown) {
268 error: function(data, textStatus, errorThrown) {
268 alert(
269 alert(
269 "Error while fetching entries.\nError code {0} ({1}).".format(data.status, data.statusText));
270 "Error while fetching entries.\nError code {0} ({1}).".format(data.status, data.statusText));
270 }
271 }
271 });
272 });
272 }
273 }
273 };
274 };
274
275
275 var queryTargetRefs = function(initialData, query) {
276 var queryTargetRefs = function(initialData, query) {
276 var data = {results: []};
277 var data = {results: []};
277 // filter initialData
278 // filter initialData
278 $.each(initialData, function() {
279 $.each(initialData, function() {
279 var section = this.text;
280 var section = this.text;
280 var children = [];
281 var children = [];
281 $.each(this.children, function() {
282 $.each(this.children, function() {
282 if (query.term.length === 0 ||
283 if (query.term.length === 0 ||
283 this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ) {
284 this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ) {
284 children.push({'id': this.id, 'text': this.text})
285 children.push({'id': this.id, 'text': this.text})
285 }
286 }
286 });
287 });
287 data.results.push({'text': section, 'children': children})
288 data.results.push({'text': section, 'children': children})
288 });
289 });
289 query.callback({results: data.results});
290 query.callback({results: data.results});
290 };
291 };
291
292
292 var loadRepoRefDiffPreview = function() {
293 var loadRepoRefDiffPreview = function() {
293
294
294 var url_data = {
295 var url_data = {
295 'repo_name': targetRepo(),
296 'repo_name': targetRepo(),
296 'target_repo': sourceRepo(),
297 'target_repo': sourceRepo(),
297 'source_ref': targetRef()[2],
298 'source_ref': targetRef()[2],
298 'source_ref_type': 'rev',
299 'source_ref_type': 'rev',
299 'target_ref': sourceRef()[2],
300 'target_ref': sourceRef()[2],
300 'target_ref_type': 'rev',
301 'target_ref_type': 'rev',
301 'merge': true,
302 'merge': true,
302 '_': Date.now() // bypass browser caching
303 '_': Date.now() // bypass browser caching
303 }; // gather the source/target ref and repo here
304 }; // gather the source/target ref and repo here
304
305
305 if (sourceRef().length !== 3 || targetRef().length !== 3) {
306 if (sourceRef().length !== 3 || targetRef().length !== 3) {
306 prButtonLock(true, "${_('Please select source and target')}");
307 prButtonLock(true, "${_('Please select source and target')}");
307 return;
308 return;
308 }
309 }
309 var url = pyroutes.url('repo_compare', url_data);
310 var url = pyroutes.url('repo_compare', url_data);
310
311
311 // lock PR button, so we cannot send PR before it's calculated
312 // lock PR button, so we cannot send PR before it's calculated
312 prButtonLock(true, "${_('Loading compare ...')}", 'compare');
313 prButtonLock(true, "${_('Loading compare ...')}", 'compare');
313
314
314 if (loadRepoRefDiffPreview._currentRequest) {
315 if (loadRepoRefDiffPreview._currentRequest) {
315 loadRepoRefDiffPreview._currentRequest.abort();
316 loadRepoRefDiffPreview._currentRequest.abort();
316 }
317 }
317
318
318 loadRepoRefDiffPreview._currentRequest = $.get(url)
319 loadRepoRefDiffPreview._currentRequest = $.get(url)
319 .error(function(data, textStatus, errorThrown) {
320 .error(function(data, textStatus, errorThrown) {
320 if (textStatus !== 'abort') {
321 if (textStatus !== 'abort') {
321 alert(
322 alert(
322 "Error while processing request.\nError code {0} ({1}).".format(
323 "Error while processing request.\nError code {0} ({1}).".format(
323 data.status, data.statusText));
324 data.status, data.statusText));
324 }
325 }
325
326
326 })
327 })
327 .done(function(data) {
328 .done(function(data) {
328 loadRepoRefDiffPreview._currentRequest = null;
329 loadRepoRefDiffPreview._currentRequest = null;
329 $('#pull_request_overview').html(data);
330 $('#pull_request_overview').html(data);
330
331
331 var commitElements = $(data).find('tr[commit_id]');
332 var commitElements = $(data).find('tr[commit_id]');
332
333
333 var prTitleAndDesc = getTitleAndDescription(
334 var prTitleAndDesc = getTitleAndDescription(
334 sourceRef()[1], commitElements, 5);
335 sourceRef()[1], commitElements, 5);
335
336
336 var title = prTitleAndDesc[0];
337 var title = prTitleAndDesc[0];
337 var proposedDescription = prTitleAndDesc[1];
338 var proposedDescription = prTitleAndDesc[1];
338
339
339 var useGeneratedTitle = (
340 var useGeneratedTitle = (
340 $('#pullrequest_title').hasClass('autogenerated-title') ||
341 $('#pullrequest_title').hasClass('autogenerated-title') ||
341 $('#pullrequest_title').val() === "");
342 $('#pullrequest_title').val() === "");
342
343
343 if (title && useGeneratedTitle) {
344 if (title && useGeneratedTitle) {
344 // use generated title if we haven't specified our own
345 // use generated title if we haven't specified our own
345 $('#pullrequest_title').val(title);
346 $('#pullrequest_title').val(title);
346 $('#pullrequest_title').addClass('autogenerated-title');
347 $('#pullrequest_title').addClass('autogenerated-title');
347
348
348 }
349 }
349
350
350 var useGeneratedDescription = (
351 var useGeneratedDescription = (
351 !codeMirrorInstance._userDefinedValue ||
352 !codeMirrorInstance._userDefinedValue ||
352 codeMirrorInstance.getValue() === "");
353 codeMirrorInstance.getValue() === "");
353
354
354 if (proposedDescription && useGeneratedDescription) {
355 if (proposedDescription && useGeneratedDescription) {
355 // set proposed content, if we haven't defined our own,
356 // set proposed content, if we haven't defined our own,
356 // or we don't have description written
357 // or we don't have description written
357 codeMirrorInstance._userDefinedValue = false; // reset state
358 codeMirrorInstance._userDefinedValue = false; // reset state
358 codeMirrorInstance.setValue(proposedDescription);
359 codeMirrorInstance.setValue(proposedDescription);
359 }
360 }
360
361
361 // refresh our codeMirror so events kicks in and it's change aware
362 // refresh our codeMirror so events kicks in and it's change aware
362 codeMirrorInstance.refresh();
363 codeMirrorInstance.refresh();
363
364
364 var msg = '';
365 var msg = '';
365 if (commitElements.length === 1) {
366 if (commitElements.length === 1) {
366 msg = "${_ungettext('This pull request will consist of __COMMITS__ commit.', 'This pull request will consist of __COMMITS__ commits.', 1)}";
367 msg = "${_ungettext('This pull request will consist of __COMMITS__ commit.', 'This pull request will consist of __COMMITS__ commits.', 1)}";
367 } else {
368 } else {
368 msg = "${_ungettext('This pull request will consist of __COMMITS__ commit.', 'This pull request will consist of __COMMITS__ commits.', 2)}";
369 msg = "${_ungettext('This pull request will consist of __COMMITS__ commit.', 'This pull request will consist of __COMMITS__ commits.', 2)}";
369 }
370 }
370
371
371 msg += ' <a id="pull_request_overview_url" href="{0}" target="_blank">${_('Show detailed compare.')}</a>'.format(url);
372 msg += ' <a id="pull_request_overview_url" href="{0}" target="_blank">${_('Show detailed compare.')}</a>'.format(url);
372
373
373 if (commitElements.length) {
374 if (commitElements.length) {
374 var commitsLink = '<a href="#pull_request_overview"><strong>{0}</strong></a>'.format(commitElements.length);
375 var commitsLink = '<a href="#pull_request_overview"><strong>{0}</strong></a>'.format(commitElements.length);
375 prButtonLock(false, msg.replace('__COMMITS__', commitsLink), 'compare');
376 prButtonLock(false, msg.replace('__COMMITS__', commitsLink), 'compare');
376 }
377 }
377 else {
378 else {
378 prButtonLock(true, "${_('There are no commits to merge.')}", 'compare');
379 prButtonLock(true, "${_('There are no commits to merge.')}", 'compare');
379 }
380 }
380
381
381
382
382 });
383 });
383 };
384 };
384
385
385 var Select2Box = function(element, overrides) {
386 var Select2Box = function(element, overrides) {
386 var globalDefaults = {
387 var globalDefaults = {
387 dropdownAutoWidth: true,
388 dropdownAutoWidth: true,
388 containerCssClass: "drop-menu",
389 containerCssClass: "drop-menu",
389 dropdownCssClass: "drop-menu-dropdown"
390 dropdownCssClass: "drop-menu-dropdown"
390 };
391 };
391
392
392 var initSelect2 = function(defaultOptions) {
393 var initSelect2 = function(defaultOptions) {
393 var options = jQuery.extend(globalDefaults, defaultOptions, overrides);
394 var options = jQuery.extend(globalDefaults, defaultOptions, overrides);
394 element.select2(options);
395 element.select2(options);
395 };
396 };
396
397
397 return {
398 return {
398 initRef: function() {
399 initRef: function() {
399 var defaultOptions = {
400 var defaultOptions = {
400 minimumResultsForSearch: 5,
401 minimumResultsForSearch: 5,
401 formatSelection: formatRefSelection
402 formatSelection: formatRefSelection
402 };
403 };
403
404
404 initSelect2(defaultOptions);
405 initSelect2(defaultOptions);
405 },
406 },
406
407
407 initRepo: function(defaultValue, readOnly) {
408 initRepo: function(defaultValue, readOnly) {
408 var defaultOptions = {
409 var defaultOptions = {
409 initSelection : function (element, callback) {
410 initSelection : function (element, callback) {
410 var data = {id: defaultValue, text: defaultValue};
411 var data = {id: defaultValue, text: defaultValue};
411 callback(data);
412 callback(data);
412 }
413 }
413 };
414 };
414
415
415 initSelect2(defaultOptions);
416 initSelect2(defaultOptions);
416
417
417 element.select2('val', defaultSourceRepo);
418 element.select2('val', defaultSourceRepo);
418 if (readOnly === true) {
419 if (readOnly === true) {
419 element.select2('readonly', true);
420 element.select2('readonly', true);
420 }
421 }
421 }
422 }
422 };
423 };
423 };
424 };
424
425
425 var initTargetRefs = function(refsData, selectedRef) {
426 var initTargetRefs = function(refsData, selectedRef) {
426
427
427 Select2Box($targetRef, {
428 Select2Box($targetRef, {
428 placeholder: "${_('Select commit reference')}",
429 placeholder: "${_('Select commit reference')}",
429 query: function(query) {
430 query: function(query) {
430 queryTargetRefs(refsData, query);
431 queryTargetRefs(refsData, query);
431 },
432 },
432 initSelection : initRefSelection(selectedRef)
433 initSelection : initRefSelection(selectedRef)
433 }).initRef();
434 }).initRef();
434
435
435 if (!(selectedRef === undefined)) {
436 if (!(selectedRef === undefined)) {
436 $targetRef.select2('val', selectedRef);
437 $targetRef.select2('val', selectedRef);
437 }
438 }
438 };
439 };
439
440
440 var targetRepoChanged = function(repoData) {
441 var targetRepoChanged = function(repoData) {
441 // generate new DESC of target repo displayed next to select
442 // generate new DESC of target repo displayed next to select
442 var prLink = pyroutes.url('pullrequest_new', {'repo_name': repoData['name']});
443 var prLink = pyroutes.url('pullrequest_new', {'repo_name': repoData['name']});
443 $('#target_repo_desc').html(
444 $('#target_repo_desc').html(
444 "<strong>${_('Target repository')}</strong>: {0}. <a href=\"{1}\">Switch base, and use as source.</a>".format(repoData['description'], prLink)
445 "<strong>${_('Target repository')}</strong>: {0}. <a href=\"{1}\">Switch base, and use as source.</a>".format(repoData['description'], prLink)
445 );
446 );
446
447
447 // generate dynamic select2 for refs.
448 // generate dynamic select2 for refs.
448 initTargetRefs(repoData['refs']['select2_refs'],
449 initTargetRefs(repoData['refs']['select2_refs'],
449 repoData['refs']['selected_ref']);
450 repoData['refs']['selected_ref']);
450
451
451 };
452 };
452
453
453 var sourceRefSelect2 = Select2Box($sourceRef, {
454 var sourceRefSelect2 = Select2Box($sourceRef, {
454 placeholder: "${_('Select commit reference')}",
455 placeholder: "${_('Select commit reference')}",
455 query: function(query) {
456 query: function(query) {
456 var initialData = defaultSourceRepoData['refs']['select2_refs'];
457 var initialData = defaultSourceRepoData['refs']['select2_refs'];
457 queryTargetRefs(initialData, query)
458 queryTargetRefs(initialData, query)
458 },
459 },
459 initSelection: initRefSelection()
460 initSelection: initRefSelection()
460 }
461 }
461 );
462 );
462
463
463 var sourceRepoSelect2 = Select2Box($sourceRepo, {
464 var sourceRepoSelect2 = Select2Box($sourceRepo, {
464 query: function(query) {}
465 query: function(query) {}
465 });
466 });
466
467
467 var targetRepoSelect2 = Select2Box($targetRepo, {
468 var targetRepoSelect2 = Select2Box($targetRepo, {
468 cachedDataSource: {},
469 cachedDataSource: {},
469 query: $.debounce(250, function(query) {
470 query: $.debounce(250, function(query) {
470 queryTargetRepo(this, query);
471 queryTargetRepo(this, query);
471 }),
472 }),
472 formatResult: formatRepoResult
473 formatResult: formatRepoResult
473 });
474 });
474
475
475 sourceRefSelect2.initRef();
476 sourceRefSelect2.initRef();
476
477
477 sourceRepoSelect2.initRepo(defaultSourceRepo, true);
478 sourceRepoSelect2.initRepo(defaultSourceRepo, true);
478
479
479 targetRepoSelect2.initRepo(defaultTargetRepo, false);
480 targetRepoSelect2.initRepo(defaultTargetRepo, false);
480
481
481 $sourceRef.on('change', function(e){
482 $sourceRef.on('change', function(e){
482 loadRepoRefDiffPreview();
483 loadRepoRefDiffPreview();
483 reviewersController.loadDefaultReviewers(
484 reviewersController.loadDefaultReviewers(
484 sourceRepo(), sourceRef(), targetRepo(), targetRef());
485 sourceRepo(), sourceRef(), targetRepo(), targetRef());
485 });
486 });
486
487
487 $targetRef.on('change', function(e){
488 $targetRef.on('change', function(e){
488 loadRepoRefDiffPreview();
489 loadRepoRefDiffPreview();
489 reviewersController.loadDefaultReviewers(
490 reviewersController.loadDefaultReviewers(
490 sourceRepo(), sourceRef(), targetRepo(), targetRef());
491 sourceRepo(), sourceRef(), targetRepo(), targetRef());
491 });
492 });
492
493
493 $targetRepo.on('change', function(e){
494 $targetRepo.on('change', function(e){
494 var repoName = $(this).val();
495 var repoName = $(this).val();
495 calculateContainerWidth();
496 calculateContainerWidth();
496 $targetRef.select2('destroy');
497 $targetRef.select2('destroy');
497 $('#target_ref_loading').show();
498 $('#target_ref_loading').show();
498
499
499 $.ajax({
500 $.ajax({
500 url: pyroutes.url('pullrequest_repo_refs',
501 url: pyroutes.url('pullrequest_repo_refs',
501 {'repo_name': templateContext.repo_name, 'target_repo_name':repoName}),
502 {'repo_name': templateContext.repo_name, 'target_repo_name':repoName}),
502 data: {},
503 data: {},
503 dataType: 'json',
504 dataType: 'json',
504 type: 'GET',
505 type: 'GET',
505 success: function(data) {
506 success: function(data) {
506 $('#target_ref_loading').hide();
507 $('#target_ref_loading').hide();
507 targetRepoChanged(data);
508 targetRepoChanged(data);
508 loadRepoRefDiffPreview();
509 loadRepoRefDiffPreview();
509 },
510 },
510 error: function(data, textStatus, errorThrown) {
511 error: function(data, textStatus, errorThrown) {
511 alert("Error while fetching entries.\nError code {0} ({1}).".format(data.status, data.statusText));
512 alert("Error while fetching entries.\nError code {0} ({1}).".format(data.status, data.statusText));
512 }
513 }
513 })
514 })
514
515
515 });
516 });
516
517
517 $pullRequestForm.on('submit', function(e){
518 $pullRequestForm.on('submit', function(e){
518 // Flush changes into textarea
519 // Flush changes into textarea
519 codeMirrorInstance.save();
520 codeMirrorInstance.save();
520 prButtonLock(true, null, 'all');
521 prButtonLock(true, null, 'all');
521 });
522 });
522
523
523 prButtonLock(true, "${_('Please select source and target')}", 'all');
524 prButtonLock(true, "${_('Please select source and target')}", 'all');
524
525
525 // auto-load on init, the target refs select2
526 // auto-load on init, the target refs select2
526 calculateContainerWidth();
527 calculateContainerWidth();
527 targetRepoChanged(defaultTargetRepoData);
528 targetRepoChanged(defaultTargetRepoData);
528
529
529 $('#pullrequest_title').on('keyup', function(e){
530 $('#pullrequest_title').on('keyup', function(e){
530 $(this).removeClass('autogenerated-title');
531 $(this).removeClass('autogenerated-title');
531 });
532 });
532
533
533 % if c.default_source_ref:
534 % if c.default_source_ref:
534 // in case we have a pre-selected value, use it now
535 // in case we have a pre-selected value, use it now
535 $sourceRef.select2('val', '${c.default_source_ref}');
536 $sourceRef.select2('val', '${c.default_source_ref}');
536 // diff preview load
537 // diff preview load
537 loadRepoRefDiffPreview();
538 loadRepoRefDiffPreview();
538 // default reviewers
539 // default reviewers
539 reviewersController.loadDefaultReviewers(
540 reviewersController.loadDefaultReviewers(
540 sourceRepo(), sourceRef(), targetRepo(), targetRef());
541 sourceRepo(), sourceRef(), targetRepo(), targetRef());
541 % endif
542 % endif
542
543
543 ReviewerAutoComplete('#user');
544 ReviewerAutoComplete('#user');
544 });
545 });
545 </script>
546 </script>
546
547
547 </%def>
548 </%def>
@@ -1,857 +1,859 b''
1 <%inherit file="/base/base.mako"/>
1 <%inherit file="/base/base.mako"/>
2 <%namespace name="base" file="/base/base.mako"/>
2 <%namespace name="base" file="/base/base.mako"/>
3 <%namespace name="dt" file="/data_table/_dt_elements.mako"/>
3 <%namespace name="dt" file="/data_table/_dt_elements.mako"/>
4
4
5 <%def name="title()">
5 <%def name="title()">
6 ${_('%s Pull Request #%s') % (c.repo_name, c.pull_request.pull_request_id)}
6 ${_('%s Pull Request #%s') % (c.repo_name, c.pull_request.pull_request_id)}
7 %if c.rhodecode_name:
7 %if c.rhodecode_name:
8 &middot; ${h.branding(c.rhodecode_name)}
8 &middot; ${h.branding(c.rhodecode_name)}
9 %endif
9 %endif
10 </%def>
10 </%def>
11
11
12 <%def name="breadcrumbs_links()">
12 <%def name="breadcrumbs_links()">
13 <span id="pr-title">
13 <span id="pr-title">
14 ${c.pull_request.title}
14 ${c.pull_request.title}
15 %if c.pull_request.is_closed():
15 %if c.pull_request.is_closed():
16 (${_('Closed')})
16 (${_('Closed')})
17 %endif
17 %endif
18 </span>
18 </span>
19 <div id="pr-title-edit" class="input" style="display: none;">
19 <div id="pr-title-edit" class="input" style="display: none;">
20 ${h.text('pullrequest_title', id_="pr-title-input", class_="large", value=c.pull_request.title)}
20 ${h.text('pullrequest_title', id_="pr-title-input", class_="large", value=c.pull_request.title)}
21 </div>
21 </div>
22 </%def>
22 </%def>
23
23
24 <%def name="menu_bar_nav()">
24 <%def name="menu_bar_nav()">
25 ${self.menu_items(active='repositories')}
25 ${self.menu_items(active='repositories')}
26 </%def>
26 </%def>
27
27
28 <%def name="menu_bar_subnav()">
28 <%def name="menu_bar_subnav()">
29 ${self.repo_menu(active='showpullrequest')}
29 ${self.repo_menu(active='showpullrequest')}
30 </%def>
30 </%def>
31
31
32 <%def name="main()">
32 <%def name="main()">
33
33
34 <script type="text/javascript">
34 <script type="text/javascript">
35 // TODO: marcink switch this to pyroutes
35 // TODO: marcink switch this to pyroutes
36 AJAX_COMMENT_DELETE_URL = "${h.route_path('pullrequest_comment_delete',repo_name=c.repo_name,pull_request_id=c.pull_request.pull_request_id,comment_id='__COMMENT_ID__')}";
36 AJAX_COMMENT_DELETE_URL = "${h.route_path('pullrequest_comment_delete',repo_name=c.repo_name,pull_request_id=c.pull_request.pull_request_id,comment_id='__COMMENT_ID__')}";
37 templateContext.pull_request_data.pull_request_id = ${c.pull_request.pull_request_id};
37 templateContext.pull_request_data.pull_request_id = ${c.pull_request.pull_request_id};
38 </script>
38 </script>
39 <div class="box">
39 <div class="box">
40
40
41 <div class="title">
41 <div class="title">
42 ${self.repo_page_title(c.rhodecode_db_repo)}
42 ${self.repo_page_title(c.rhodecode_db_repo)}
43 </div>
43 </div>
44
44
45 ${self.breadcrumbs()}
45 ${self.breadcrumbs()}
46
46
47 <div class="box pr-summary">
47 <div class="box pr-summary">
48
48
49 <div class="summary-details block-left">
49 <div class="summary-details block-left">
50 <% summary = lambda n:{False:'summary-short'}.get(n) %>
50 <% summary = lambda n:{False:'summary-short'}.get(n) %>
51 <div class="pr-details-title">
51 <div class="pr-details-title">
52 <a href="${h.route_path('pull_requests_global', pull_request_id=c.pull_request.pull_request_id)}">${_('Pull request #%s') % c.pull_request.pull_request_id}</a> ${_('From')} ${h.format_date(c.pull_request.created_on)}
52 <a href="${h.route_path('pull_requests_global', pull_request_id=c.pull_request.pull_request_id)}">${_('Pull request #%s') % c.pull_request.pull_request_id}</a> ${_('From')} ${h.format_date(c.pull_request.created_on)}
53 %if c.allowed_to_update:
53 %if c.allowed_to_update:
54 <div id="delete_pullrequest" class="pull-right action_button ${'' if c.allowed_to_delete else 'disabled' }" style="clear:inherit;padding: 0">
54 <div id="delete_pullrequest" class="pull-right action_button ${'' if c.allowed_to_delete else 'disabled' }" style="clear:inherit;padding: 0">
55 % if c.allowed_to_delete:
55 % if c.allowed_to_delete:
56 ${h.secure_form(h.route_path('pullrequest_delete', repo_name=c.pull_request.target_repo.repo_name, pull_request_id=c.pull_request.pull_request_id), request=request)}
56 ${h.secure_form(h.route_path('pullrequest_delete', repo_name=c.pull_request.target_repo.repo_name, pull_request_id=c.pull_request.pull_request_id), request=request)}
57 ${h.submit('remove_%s' % c.pull_request.pull_request_id, _('Delete'),
57 ${h.submit('remove_%s' % c.pull_request.pull_request_id, _('Delete'),
58 class_="btn btn-link btn-danger no-margin",onclick="return confirm('"+_('Confirm to delete this pull request')+"');")}
58 class_="btn btn-link btn-danger no-margin",onclick="return confirm('"+_('Confirm to delete this pull request')+"');")}
59 ${h.end_form()}
59 ${h.end_form()}
60 % else:
60 % else:
61 ${_('Delete')}
61 ${_('Delete')}
62 % endif
62 % endif
63 </div>
63 </div>
64 <div id="open_edit_pullrequest" class="pull-right action_button">${_('Edit')}</div>
64 <div id="open_edit_pullrequest" class="pull-right action_button">${_('Edit')}</div>
65 <div id="close_edit_pullrequest" class="pull-right action_button" style="display: none;padding: 0">${_('Cancel')}</div>
65 <div id="close_edit_pullrequest" class="pull-right action_button" style="display: none;padding: 0">${_('Cancel')}</div>
66 %endif
66 %endif
67 </div>
67 </div>
68
68
69 <div id="summary" class="fields pr-details-content">
69 <div id="summary" class="fields pr-details-content">
70 <div class="field">
70 <div class="field">
71 <div class="label-summary">
71 <div class="label-summary">
72 <label>${_('Source')}:</label>
72 <label>${_('Source')}:</label>
73 </div>
73 </div>
74 <div class="input">
74 <div class="input">
75 <div class="pr-origininfo">
75 <div class="pr-origininfo">
76 ## branch link is only valid if it is a branch
76 ## branch link is only valid if it is a branch
77 <span class="tag">
77 <span class="tag">
78 %if c.pull_request.source_ref_parts.type == 'branch':
78 %if c.pull_request.source_ref_parts.type == 'branch':
79 <a href="${h.route_path('repo_changelog', repo_name=c.pull_request.source_repo.repo_name, _query=dict(branch=c.pull_request.source_ref_parts.name))}">${c.pull_request.source_ref_parts.type}: ${c.pull_request.source_ref_parts.name}</a>
79 <a href="${h.route_path('repo_changelog', repo_name=c.pull_request.source_repo.repo_name, _query=dict(branch=c.pull_request.source_ref_parts.name))}">${c.pull_request.source_ref_parts.type}: ${c.pull_request.source_ref_parts.name}</a>
80 %else:
80 %else:
81 ${c.pull_request.source_ref_parts.type}: ${c.pull_request.source_ref_parts.name}
81 ${c.pull_request.source_ref_parts.type}: ${c.pull_request.source_ref_parts.name}
82 %endif
82 %endif
83 </span>
83 </span>
84 <span class="clone-url">
84 <span class="clone-url">
85 <a href="${h.route_path('repo_summary', repo_name=c.pull_request.source_repo.repo_name)}">${c.pull_request.source_repo.clone_url()}</a>
85 <a href="${h.route_path('repo_summary', repo_name=c.pull_request.source_repo.repo_name)}">${c.pull_request.source_repo.clone_url()}</a>
86 </span>
86 </span>
87 <br/>
87 <br/>
88 % if c.ancestor_commit:
88 % if c.ancestor_commit:
89 ${_('Common ancestor')}:
89 ${_('Common ancestor')}:
90 <code><a href="${h.route_path('repo_commit', repo_name=c.target_repo.repo_name, commit_id=c.ancestor_commit.raw_id)}">${h.show_id(c.ancestor_commit)}</a></code>
90 <code><a href="${h.route_path('repo_commit', repo_name=c.target_repo.repo_name, commit_id=c.ancestor_commit.raw_id)}">${h.show_id(c.ancestor_commit)}</a></code>
91 % endif
91 % endif
92 </div>
92 </div>
93 %if h.is_hg(c.pull_request.source_repo):
93 %if h.is_hg(c.pull_request.source_repo):
94 <% clone_url = 'hg pull -r {} {}'.format(h.short_id(c.source_ref), c.pull_request.source_repo.clone_url()) %>
94 <% clone_url = 'hg pull -r {} {}'.format(h.short_id(c.source_ref), c.pull_request.source_repo.clone_url()) %>
95 %elif h.is_git(c.pull_request.source_repo):
95 %elif h.is_git(c.pull_request.source_repo):
96 <% clone_url = 'git pull {} {}'.format(c.pull_request.source_repo.clone_url(), c.pull_request.source_ref_parts.name) %>
96 <% clone_url = 'git pull {} {}'.format(c.pull_request.source_repo.clone_url(), c.pull_request.source_ref_parts.name) %>
97 %endif
97 %endif
98
98
99 <div class="">
99 <div class="">
100 <input type="text" class="input-monospace pr-pullinfo" value="${clone_url}" readonly="readonly">
100 <input type="text" class="input-monospace pr-pullinfo" value="${clone_url}" readonly="readonly">
101 <i class="tooltip icon-clipboard clipboard-action pull-right pr-pullinfo-copy" data-clipboard-text="${clone_url}" title="${_('Copy the pull url')}"></i>
101 <i class="tooltip icon-clipboard clipboard-action pull-right pr-pullinfo-copy" data-clipboard-text="${clone_url}" title="${_('Copy the pull url')}"></i>
102 </div>
102 </div>
103
103
104 </div>
104 </div>
105 </div>
105 </div>
106 <div class="field">
106 <div class="field">
107 <div class="label-summary">
107 <div class="label-summary">
108 <label>${_('Target')}:</label>
108 <label>${_('Target')}:</label>
109 </div>
109 </div>
110 <div class="input">
110 <div class="input">
111 <div class="pr-targetinfo">
111 <div class="pr-targetinfo">
112 ## branch link is only valid if it is a branch
112 ## branch link is only valid if it is a branch
113 <span class="tag">
113 <span class="tag">
114 %if c.pull_request.target_ref_parts.type == 'branch':
114 %if c.pull_request.target_ref_parts.type == 'branch':
115 <a href="${h.route_path('repo_changelog', repo_name=c.pull_request.target_repo.repo_name, _query=dict(branch=c.pull_request.target_ref_parts.name))}">${c.pull_request.target_ref_parts.type}: ${c.pull_request.target_ref_parts.name}</a>
115 <a href="${h.route_path('repo_changelog', repo_name=c.pull_request.target_repo.repo_name, _query=dict(branch=c.pull_request.target_ref_parts.name))}">${c.pull_request.target_ref_parts.type}: ${c.pull_request.target_ref_parts.name}</a>
116 %else:
116 %else:
117 ${c.pull_request.target_ref_parts.type}: ${c.pull_request.target_ref_parts.name}
117 ${c.pull_request.target_ref_parts.type}: ${c.pull_request.target_ref_parts.name}
118 %endif
118 %endif
119 </span>
119 </span>
120 <span class="clone-url">
120 <span class="clone-url">
121 <a href="${h.route_path('repo_summary', repo_name=c.pull_request.target_repo.repo_name)}">${c.pull_request.target_repo.clone_url()}</a>
121 <a href="${h.route_path('repo_summary', repo_name=c.pull_request.target_repo.repo_name)}">${c.pull_request.target_repo.clone_url()}</a>
122 </span>
122 </span>
123 </div>
123 </div>
124 </div>
124 </div>
125 </div>
125 </div>
126
126
127 ## Link to the shadow repository.
127 ## Link to the shadow repository.
128 <div class="field">
128 <div class="field">
129 <div class="label-summary">
129 <div class="label-summary">
130 <label>${_('Merge')}:</label>
130 <label>${_('Merge')}:</label>
131 </div>
131 </div>
132 <div class="input">
132 <div class="input">
133 % if not c.pull_request.is_closed() and c.pull_request.shadow_merge_ref:
133 % if not c.pull_request.is_closed() and c.pull_request.shadow_merge_ref:
134 %if h.is_hg(c.pull_request.target_repo):
134 %if h.is_hg(c.pull_request.target_repo):
135 <% clone_url = 'hg clone --update {} {} pull-request-{}'.format(c.pull_request.shadow_merge_ref.name, c.shadow_clone_url, c.pull_request.pull_request_id) %>
135 <% clone_url = 'hg clone --update {} {} pull-request-{}'.format(c.pull_request.shadow_merge_ref.name, c.shadow_clone_url, c.pull_request.pull_request_id) %>
136 %elif h.is_git(c.pull_request.target_repo):
136 %elif h.is_git(c.pull_request.target_repo):
137 <% clone_url = 'git clone --branch {} {} pull-request-{}'.format(c.pull_request.shadow_merge_ref.name, c.shadow_clone_url, c.pull_request.pull_request_id) %>
137 <% clone_url = 'git clone --branch {} {} pull-request-{}'.format(c.pull_request.shadow_merge_ref.name, c.shadow_clone_url, c.pull_request.pull_request_id) %>
138 %endif
138 %endif
139 <div class="">
139 <div class="">
140 <input type="text" class="input-monospace pr-mergeinfo" value="${clone_url}" readonly="readonly">
140 <input type="text" class="input-monospace pr-mergeinfo" value="${clone_url}" readonly="readonly">
141 <i class="tooltip icon-clipboard clipboard-action pull-right pr-mergeinfo-copy" data-clipboard-text="${clone_url}" title="${_('Copy the clone url')}"></i>
141 <i class="tooltip icon-clipboard clipboard-action pull-right pr-mergeinfo-copy" data-clipboard-text="${clone_url}" title="${_('Copy the clone url')}"></i>
142 </div>
142 </div>
143 % else:
143 % else:
144 <div class="">
144 <div class="">
145 ${_('Shadow repository data not available')}.
145 ${_('Shadow repository data not available')}.
146 </div>
146 </div>
147 % endif
147 % endif
148 </div>
148 </div>
149 </div>
149 </div>
150
150
151 <div class="field">
151 <div class="field">
152 <div class="label-summary">
152 <div class="label-summary">
153 <label>${_('Review')}:</label>
153 <label>${_('Review')}:</label>
154 </div>
154 </div>
155 <div class="input">
155 <div class="input">
156 %if c.pull_request_review_status:
156 %if c.pull_request_review_status:
157 <div class="${'flag_status %s' % c.pull_request_review_status} tooltip pull-left"></div>
157 <div class="${'flag_status %s' % c.pull_request_review_status} tooltip pull-left"></div>
158 <span class="changeset-status-lbl tooltip">
158 <span class="changeset-status-lbl tooltip">
159 %if c.pull_request.is_closed():
159 %if c.pull_request.is_closed():
160 ${_('Closed')},
160 ${_('Closed')},
161 %endif
161 %endif
162 ${h.commit_status_lbl(c.pull_request_review_status)}
162 ${h.commit_status_lbl(c.pull_request_review_status)}
163 </span>
163 </span>
164 - ${_ungettext('calculated based on %s reviewer vote', 'calculated based on %s reviewers votes', len(c.pull_request_reviewers)) % len(c.pull_request_reviewers)}
164 - ${_ungettext('calculated based on %s reviewer vote', 'calculated based on %s reviewers votes', len(c.pull_request_reviewers)) % len(c.pull_request_reviewers)}
165 %endif
165 %endif
166 </div>
166 </div>
167 </div>
167 </div>
168 <div class="field">
168 <div class="field">
169 <div class="pr-description-label label-summary">
169 <div class="pr-description-label label-summary" title="${_('Rendered using {} renderer').format(c.renderer)}">
170 <label>${_('Description')}:</label>
170 <label>${_('Description')}:</label>
171 </div>
171 </div>
172 <div id="pr-desc" class="input">
172 <div id="pr-desc" class="input">
173 <div class="pr-description">${h.render(c.pull_request.description, renderer=c.visual.default_renderer)}</div>
173 <div class="pr-description">${h.render(c.pull_request.description, renderer=c.renderer)}</div>
174 </div>
174 </div>
175 <div id="pr-desc-edit" class="input textarea editor" style="display: none;">
175 <div id="pr-desc-edit" class="input textarea editor" style="display: none;">
176 <input id="pr-renderer-input" type="hidden" name="description_renderer" value="${c.visual.default_renderer}">
176 ${dt.markup_form('pr-description-input', form_text=c.pull_request.description)}
177 ${dt.markup_form('pr-description-input', form_text=c.pull_request.description)}
177 </div>
178 </div>
178 </div>
179 </div>
179
180
180 <div class="field">
181 <div class="field">
181 <div class="label-summary">
182 <div class="label-summary">
182 <label>${_('Versions')}:</label>
183 <label>${_('Versions')}:</label>
183 </div>
184 </div>
184
185
185 <% outdated_comm_count_ver = len(c.inline_versions[None]['outdated']) %>
186 <% outdated_comm_count_ver = len(c.inline_versions[None]['outdated']) %>
186 <% general_outdated_comm_count_ver = len(c.comment_versions[None]['outdated']) %>
187 <% general_outdated_comm_count_ver = len(c.comment_versions[None]['outdated']) %>
187
188
188 <div class="pr-versions">
189 <div class="pr-versions">
189 % if c.show_version_changes:
190 % if c.show_version_changes:
190 <% outdated_comm_count_ver = len(c.inline_versions[c.at_version_num]['outdated']) %>
191 <% outdated_comm_count_ver = len(c.inline_versions[c.at_version_num]['outdated']) %>
191 <% general_outdated_comm_count_ver = len(c.comment_versions[c.at_version_num]['outdated']) %>
192 <% general_outdated_comm_count_ver = len(c.comment_versions[c.at_version_num]['outdated']) %>
192 <a id="show-pr-versions" class="input" onclick="return versionController.toggleVersionView(this)" href="#show-pr-versions"
193 <a id="show-pr-versions" class="input" onclick="return versionController.toggleVersionView(this)" href="#show-pr-versions"
193 data-toggle-on="${_ungettext('{} version available for this pull request, show it.', '{} versions available for this pull request, show them.', len(c.versions)).format(len(c.versions))}"
194 data-toggle-on="${_ungettext('{} version available for this pull request, show it.', '{} versions available for this pull request, show them.', len(c.versions)).format(len(c.versions))}"
194 data-toggle-off="${_('Hide all versions of this pull request')}">
195 data-toggle-off="${_('Hide all versions of this pull request')}">
195 ${_ungettext('{} version available for this pull request, show it.', '{} versions available for this pull request, show them.', len(c.versions)).format(len(c.versions))}
196 ${_ungettext('{} version available for this pull request, show it.', '{} versions available for this pull request, show them.', len(c.versions)).format(len(c.versions))}
196 </a>
197 </a>
197 <table>
198 <table>
198 ## SHOW ALL VERSIONS OF PR
199 ## SHOW ALL VERSIONS OF PR
199 <% ver_pr = None %>
200 <% ver_pr = None %>
200
201
201 % for data in reversed(list(enumerate(c.versions, 1))):
202 % for data in reversed(list(enumerate(c.versions, 1))):
202 <% ver_pos = data[0] %>
203 <% ver_pos = data[0] %>
203 <% ver = data[1] %>
204 <% ver = data[1] %>
204 <% ver_pr = ver.pull_request_version_id %>
205 <% ver_pr = ver.pull_request_version_id %>
205 <% display_row = '' if c.at_version and (c.at_version_num == ver_pr or c.from_version_num == ver_pr) else 'none' %>
206 <% display_row = '' if c.at_version and (c.at_version_num == ver_pr or c.from_version_num == ver_pr) else 'none' %>
206
207
207 <tr class="version-pr" style="display: ${display_row}">
208 <tr class="version-pr" style="display: ${display_row}">
208 <td>
209 <td>
209 <code>
210 <code>
210 <a href="${request.current_route_path(_query=dict(version=ver_pr or 'latest'))}">v${ver_pos}</a>
211 <a href="${request.current_route_path(_query=dict(version=ver_pr or 'latest'))}">v${ver_pos}</a>
211 </code>
212 </code>
212 </td>
213 </td>
213 <td>
214 <td>
214 <input ${'checked="checked"' if c.from_version_num == ver_pr else ''} class="compare-radio-button" type="radio" name="ver_source" value="${ver_pr or 'latest'}" data-ver-pos="${ver_pos}"/>
215 <input ${'checked="checked"' if c.from_version_num == ver_pr else ''} class="compare-radio-button" type="radio" name="ver_source" value="${ver_pr or 'latest'}" data-ver-pos="${ver_pos}"/>
215 <input ${'checked="checked"' if c.at_version_num == ver_pr else ''} class="compare-radio-button" type="radio" name="ver_target" value="${ver_pr or 'latest'}" data-ver-pos="${ver_pos}"/>
216 <input ${'checked="checked"' if c.at_version_num == ver_pr else ''} class="compare-radio-button" type="radio" name="ver_target" value="${ver_pr or 'latest'}" data-ver-pos="${ver_pos}"/>
216 </td>
217 </td>
217 <td>
218 <td>
218 <% review_status = c.review_versions[ver_pr].status if ver_pr in c.review_versions else 'not_reviewed' %>
219 <% review_status = c.review_versions[ver_pr].status if ver_pr in c.review_versions else 'not_reviewed' %>
219 <div class="${'flag_status %s' % review_status} tooltip pull-left" title="${_('Your review status at this version')}">
220 <div class="${'flag_status %s' % review_status} tooltip pull-left" title="${_('Your review status at this version')}">
220 </div>
221 </div>
221 </td>
222 </td>
222 <td>
223 <td>
223 % if c.at_version_num != ver_pr:
224 % if c.at_version_num != ver_pr:
224 <i class="icon-comment"></i>
225 <i class="icon-comment"></i>
225 <code class="tooltip" title="${_('Comment from pull request version v{0}, general:{1} inline:{2}').format(ver_pos, len(c.comment_versions[ver_pr]['at']), len(c.inline_versions[ver_pr]['at']))}">
226 <code class="tooltip" title="${_('Comment from pull request version v{0}, general:{1} inline:{2}').format(ver_pos, len(c.comment_versions[ver_pr]['at']), len(c.inline_versions[ver_pr]['at']))}">
226 G:${len(c.comment_versions[ver_pr]['at'])} / I:${len(c.inline_versions[ver_pr]['at'])}
227 G:${len(c.comment_versions[ver_pr]['at'])} / I:${len(c.inline_versions[ver_pr]['at'])}
227 </code>
228 </code>
228 % endif
229 % endif
229 </td>
230 </td>
230 <td>
231 <td>
231 ##<code>${ver.source_ref_parts.commit_id[:6]}</code>
232 ##<code>${ver.source_ref_parts.commit_id[:6]}</code>
232 </td>
233 </td>
233 <td>
234 <td>
234 ${h.age_component(ver.updated_on, time_is_local=True)}
235 ${h.age_component(ver.updated_on, time_is_local=True)}
235 </td>
236 </td>
236 </tr>
237 </tr>
237 % endfor
238 % endfor
238
239
239 <tr>
240 <tr>
240 <td colspan="6">
241 <td colspan="6">
241 <button id="show-version-diff" onclick="return versionController.showVersionDiff()" class="btn btn-sm" style="display: none"
242 <button id="show-version-diff" onclick="return versionController.showVersionDiff()" class="btn btn-sm" style="display: none"
242 data-label-text-locked="${_('select versions to show changes')}"
243 data-label-text-locked="${_('select versions to show changes')}"
243 data-label-text-diff="${_('show changes between versions')}"
244 data-label-text-diff="${_('show changes between versions')}"
244 data-label-text-show="${_('show pull request for this version')}"
245 data-label-text-show="${_('show pull request for this version')}"
245 >
246 >
246 ${_('select versions to show changes')}
247 ${_('select versions to show changes')}
247 </button>
248 </button>
248 </td>
249 </td>
249 </tr>
250 </tr>
250
251
251 ## show comment/inline comments summary
252 ## show comment/inline comments summary
252 <%def name="comments_summary()">
253 <%def name="comments_summary()">
253 <tr>
254 <tr>
254 <td colspan="6" class="comments-summary-td">
255 <td colspan="6" class="comments-summary-td">
255
256
256 % if c.at_version:
257 % if c.at_version:
257 <% inline_comm_count_ver = len(c.inline_versions[c.at_version_num]['display']) %>
258 <% inline_comm_count_ver = len(c.inline_versions[c.at_version_num]['display']) %>
258 <% general_comm_count_ver = len(c.comment_versions[c.at_version_num]['display']) %>
259 <% general_comm_count_ver = len(c.comment_versions[c.at_version_num]['display']) %>
259 ${_('Comments at this version')}:
260 ${_('Comments at this version')}:
260 % else:
261 % else:
261 <% inline_comm_count_ver = len(c.inline_versions[c.at_version_num]['until']) %>
262 <% inline_comm_count_ver = len(c.inline_versions[c.at_version_num]['until']) %>
262 <% general_comm_count_ver = len(c.comment_versions[c.at_version_num]['until']) %>
263 <% general_comm_count_ver = len(c.comment_versions[c.at_version_num]['until']) %>
263 ${_('Comments for this pull request')}:
264 ${_('Comments for this pull request')}:
264 % endif
265 % endif
265
266
266
267
267 %if general_comm_count_ver:
268 %if general_comm_count_ver:
268 <a href="#comments">${_("%d General ") % general_comm_count_ver}</a>
269 <a href="#comments">${_("%d General ") % general_comm_count_ver}</a>
269 %else:
270 %else:
270 ${_("%d General ") % general_comm_count_ver}
271 ${_("%d General ") % general_comm_count_ver}
271 %endif
272 %endif
272
273
273 %if inline_comm_count_ver:
274 %if inline_comm_count_ver:
274 , <a href="#" onclick="return Rhodecode.comments.nextComment();" id="inline-comments-counter">${_("%d Inline") % inline_comm_count_ver}</a>
275 , <a href="#" onclick="return Rhodecode.comments.nextComment();" id="inline-comments-counter">${_("%d Inline") % inline_comm_count_ver}</a>
275 %else:
276 %else:
276 , ${_("%d Inline") % inline_comm_count_ver}
277 , ${_("%d Inline") % inline_comm_count_ver}
277 %endif
278 %endif
278
279
279 %if outdated_comm_count_ver:
280 %if outdated_comm_count_ver:
280 , <a href="#" onclick="showOutdated(); Rhodecode.comments.nextOutdatedComment(); return false;">${_("%d Outdated") % outdated_comm_count_ver}</a>
281 , <a href="#" onclick="showOutdated(); Rhodecode.comments.nextOutdatedComment(); return false;">${_("%d Outdated") % outdated_comm_count_ver}</a>
281 <a href="#" class="showOutdatedComments" onclick="showOutdated(this); return false;"> | ${_('show outdated comments')}</a>
282 <a href="#" class="showOutdatedComments" onclick="showOutdated(this); return false;"> | ${_('show outdated comments')}</a>
282 <a href="#" class="hideOutdatedComments" style="display: none" onclick="hideOutdated(this); return false;"> | ${_('hide outdated comments')}</a>
283 <a href="#" class="hideOutdatedComments" style="display: none" onclick="hideOutdated(this); return false;"> | ${_('hide outdated comments')}</a>
283 %else:
284 %else:
284 , ${_("%d Outdated") % outdated_comm_count_ver}
285 , ${_("%d Outdated") % outdated_comm_count_ver}
285 %endif
286 %endif
286 </td>
287 </td>
287 </tr>
288 </tr>
288 </%def>
289 </%def>
289 ${comments_summary()}
290 ${comments_summary()}
290 </table>
291 </table>
291 % else:
292 % else:
292 <div class="input">
293 <div class="input">
293 ${_('Pull request versions not available')}.
294 ${_('Pull request versions not available')}.
294 </div>
295 </div>
295 <div>
296 <div>
296 <table>
297 <table>
297 ${comments_summary()}
298 ${comments_summary()}
298 </table>
299 </table>
299 </div>
300 </div>
300 % endif
301 % endif
301 </div>
302 </div>
302 </div>
303 </div>
303
304
304 <div id="pr-save" class="field" style="display: none;">
305 <div id="pr-save" class="field" style="display: none;">
305 <div class="label-summary"></div>
306 <div class="label-summary"></div>
306 <div class="input">
307 <div class="input">
307 <span id="edit_pull_request" class="btn btn-small no-margin">${_('Save Changes')}</span>
308 <span id="edit_pull_request" class="btn btn-small no-margin">${_('Save Changes')}</span>
308 </div>
309 </div>
309 </div>
310 </div>
310 </div>
311 </div>
311 </div>
312 </div>
312 <div>
313 <div>
313 ## AUTHOR
314 ## AUTHOR
314 <div class="reviewers-title block-right">
315 <div class="reviewers-title block-right">
315 <div class="pr-details-title">
316 <div class="pr-details-title">
316 ${_('Author of this pull request')}
317 ${_('Author of this pull request')}
317 </div>
318 </div>
318 </div>
319 </div>
319 <div class="block-right pr-details-content reviewers">
320 <div class="block-right pr-details-content reviewers">
320 <ul class="group_members">
321 <ul class="group_members">
321 <li>
322 <li>
322 ${self.gravatar_with_user(c.pull_request.author.email, 16)}
323 ${self.gravatar_with_user(c.pull_request.author.email, 16)}
323 </li>
324 </li>
324 </ul>
325 </ul>
325 </div>
326 </div>
326
327
327 ## REVIEW RULES
328 ## REVIEW RULES
328 <div id="review_rules" style="display: none" class="reviewers-title block-right">
329 <div id="review_rules" style="display: none" class="reviewers-title block-right">
329 <div class="pr-details-title">
330 <div class="pr-details-title">
330 ${_('Reviewer rules')}
331 ${_('Reviewer rules')}
331 %if c.allowed_to_update:
332 %if c.allowed_to_update:
332 <span id="close_edit_reviewers" class="block-right action_button last-item" style="display: none;">${_('Close')}</span>
333 <span id="close_edit_reviewers" class="block-right action_button last-item" style="display: none;">${_('Close')}</span>
333 %endif
334 %endif
334 </div>
335 </div>
335 <div class="pr-reviewer-rules">
336 <div class="pr-reviewer-rules">
336 ## review rules will be appended here, by default reviewers logic
337 ## review rules will be appended here, by default reviewers logic
337 </div>
338 </div>
338 <input id="review_data" type="hidden" name="review_data" value="">
339 <input id="review_data" type="hidden" name="review_data" value="">
339 </div>
340 </div>
340
341
341 ## REVIEWERS
342 ## REVIEWERS
342 <div class="reviewers-title block-right">
343 <div class="reviewers-title block-right">
343 <div class="pr-details-title">
344 <div class="pr-details-title">
344 ${_('Pull request reviewers')}
345 ${_('Pull request reviewers')}
345 %if c.allowed_to_update:
346 %if c.allowed_to_update:
346 <span id="open_edit_reviewers" class="block-right action_button last-item">${_('Edit')}</span>
347 <span id="open_edit_reviewers" class="block-right action_button last-item">${_('Edit')}</span>
347 %endif
348 %endif
348 </div>
349 </div>
349 </div>
350 </div>
350 <div id="reviewers" class="block-right pr-details-content reviewers">
351 <div id="reviewers" class="block-right pr-details-content reviewers">
351
352
352 ## members redering block
353 ## members redering block
353 <input type="hidden" name="__start__" value="review_members:sequence">
354 <input type="hidden" name="__start__" value="review_members:sequence">
354 <ul id="review_members" class="group_members">
355 <ul id="review_members" class="group_members">
355
356
356 % for review_obj, member, reasons, mandatory, status in c.pull_request_reviewers:
357 % for review_obj, member, reasons, mandatory, status in c.pull_request_reviewers:
357 <script>
358 <script>
358 var member = ${h.json.dumps(h.reviewer_as_json(member, reasons=reasons, mandatory=mandatory, user_group=review_obj.rule_user_group_data()))|n};
359 var member = ${h.json.dumps(h.reviewer_as_json(member, reasons=reasons, mandatory=mandatory, user_group=review_obj.rule_user_group_data()))|n};
359 var status = "${(status[0][1].status if status else 'not_reviewed')}";
360 var status = "${(status[0][1].status if status else 'not_reviewed')}";
360 var status_lbl = "${h.commit_status_lbl(status[0][1].status if status else 'not_reviewed')}";
361 var status_lbl = "${h.commit_status_lbl(status[0][1].status if status else 'not_reviewed')}";
361 var allowed_to_update = ${h.json.dumps(c.allowed_to_update)};
362 var allowed_to_update = ${h.json.dumps(c.allowed_to_update)};
362
363
363 var entry = renderTemplate('reviewMemberEntry', {
364 var entry = renderTemplate('reviewMemberEntry', {
364 'member': member,
365 'member': member,
365 'mandatory': member.mandatory,
366 'mandatory': member.mandatory,
366 'reasons': member.reasons,
367 'reasons': member.reasons,
367 'allowed_to_update': allowed_to_update,
368 'allowed_to_update': allowed_to_update,
368 'review_status': status,
369 'review_status': status,
369 'review_status_label': status_lbl,
370 'review_status_label': status_lbl,
370 'user_group': member.user_group,
371 'user_group': member.user_group,
371 'create': false
372 'create': false
372 });
373 });
373 $('#review_members').append(entry)
374 $('#review_members').append(entry)
374 </script>
375 </script>
375
376
376 % endfor
377 % endfor
377
378
378 </ul>
379 </ul>
379 <input type="hidden" name="__end__" value="review_members:sequence">
380 <input type="hidden" name="__end__" value="review_members:sequence">
380 ## end members redering block
381 ## end members redering block
381
382
382 %if not c.pull_request.is_closed():
383 %if not c.pull_request.is_closed():
383 <div id="add_reviewer" class="ac" style="display: none;">
384 <div id="add_reviewer" class="ac" style="display: none;">
384 %if c.allowed_to_update:
385 %if c.allowed_to_update:
385 % if not c.forbid_adding_reviewers:
386 % if not c.forbid_adding_reviewers:
386 <div id="add_reviewer_input" class="reviewer_ac">
387 <div id="add_reviewer_input" class="reviewer_ac">
387 ${h.text('user', class_='ac-input', placeholder=_('Add reviewer or reviewer group'))}
388 ${h.text('user', class_='ac-input', placeholder=_('Add reviewer or reviewer group'))}
388 <div id="reviewers_container"></div>
389 <div id="reviewers_container"></div>
389 </div>
390 </div>
390 % endif
391 % endif
391 <div class="pull-right">
392 <div class="pull-right">
392 <button id="update_pull_request" class="btn btn-small no-margin">${_('Save Changes')}</button>
393 <button id="update_pull_request" class="btn btn-small no-margin">${_('Save Changes')}</button>
393 </div>
394 </div>
394 %endif
395 %endif
395 </div>
396 </div>
396 %endif
397 %endif
397 </div>
398 </div>
398 </div>
399 </div>
399 </div>
400 </div>
400 <div class="box">
401 <div class="box">
401 ##DIFF
402 ##DIFF
402 <div class="table" >
403 <div class="table" >
403 <div id="changeset_compare_view_content">
404 <div id="changeset_compare_view_content">
404 ##CS
405 ##CS
405 % if c.missing_requirements:
406 % if c.missing_requirements:
406 <div class="box">
407 <div class="box">
407 <div class="alert alert-warning">
408 <div class="alert alert-warning">
408 <div>
409 <div>
409 <strong>${_('Missing requirements:')}</strong>
410 <strong>${_('Missing requirements:')}</strong>
410 ${_('These commits cannot be displayed, because this repository uses the Mercurial largefiles extension, which was not enabled.')}
411 ${_('These commits cannot be displayed, because this repository uses the Mercurial largefiles extension, which was not enabled.')}
411 </div>
412 </div>
412 </div>
413 </div>
413 </div>
414 </div>
414 % elif c.missing_commits:
415 % elif c.missing_commits:
415 <div class="box">
416 <div class="box">
416 <div class="alert alert-warning">
417 <div class="alert alert-warning">
417 <div>
418 <div>
418 <strong>${_('Missing commits')}:</strong>
419 <strong>${_('Missing commits')}:</strong>
419 ${_('This pull request cannot be displayed, because one or more commits no longer exist in the source repository.')}
420 ${_('This pull request cannot be displayed, because one or more commits no longer exist in the source repository.')}
420 ${_('Please update this pull request, push the commits back into the source repository, or consider closing this pull request.')}
421 ${_('Please update this pull request, push the commits back into the source repository, or consider closing this pull request.')}
421 ${_('Consider doing a {force_refresh_url} in case you think this is an error.').format(force_refresh_url=h.link_to('force refresh', h.current_route_path(request, force_refresh='1')))|n}
422 ${_('Consider doing a {force_refresh_url} in case you think this is an error.').format(force_refresh_url=h.link_to('force refresh', h.current_route_path(request, force_refresh='1')))|n}
422 </div>
423 </div>
423 </div>
424 </div>
424 </div>
425 </div>
425 % endif
426 % endif
426
427
427 <div class="compare_view_commits_title">
428 <div class="compare_view_commits_title">
428 % if not c.compare_mode:
429 % if not c.compare_mode:
429
430
430 % if c.at_version_pos:
431 % if c.at_version_pos:
431 <h4>
432 <h4>
432 ${_('Showing changes at v%d, commenting is disabled.') % c.at_version_pos}
433 ${_('Showing changes at v%d, commenting is disabled.') % c.at_version_pos}
433 </h4>
434 </h4>
434 % endif
435 % endif
435
436
436 <div class="pull-left">
437 <div class="pull-left">
437 <div class="btn-group">
438 <div class="btn-group">
438 <a
439 <a
439 class="btn"
440 class="btn"
440 href="#"
441 href="#"
441 onclick="$('.compare_select').show();$('.compare_select_hidden').hide(); return false">
442 onclick="$('.compare_select').show();$('.compare_select_hidden').hide(); return false">
442 ${_ungettext('Expand %s commit','Expand %s commits', len(c.commit_ranges)) % len(c.commit_ranges)}
443 ${_ungettext('Expand %s commit','Expand %s commits', len(c.commit_ranges)) % len(c.commit_ranges)}
443 </a>
444 </a>
444 <a
445 <a
445 class="btn"
446 class="btn"
446 href="#"
447 href="#"
447 onclick="$('.compare_select').hide();$('.compare_select_hidden').show(); return false">
448 onclick="$('.compare_select').hide();$('.compare_select_hidden').show(); return false">
448 ${_ungettext('Collapse %s commit','Collapse %s commits', len(c.commit_ranges)) % len(c.commit_ranges)}
449 ${_ungettext('Collapse %s commit','Collapse %s commits', len(c.commit_ranges)) % len(c.commit_ranges)}
449 </a>
450 </a>
450 </div>
451 </div>
451 </div>
452 </div>
452
453
453 <div class="pull-right">
454 <div class="pull-right">
454 % if c.allowed_to_update and not c.pull_request.is_closed():
455 % if c.allowed_to_update and not c.pull_request.is_closed():
455 <a id="update_commits" class="btn btn-primary no-margin pull-right">${_('Update commits')}</a>
456 <a id="update_commits" class="btn btn-primary no-margin pull-right">${_('Update commits')}</a>
456 % else:
457 % else:
457 <a class="tooltip btn disabled pull-right" disabled="disabled" title="${_('Update is disabled for current view')}">${_('Update commits')}</a>
458 <a class="tooltip btn disabled pull-right" disabled="disabled" title="${_('Update is disabled for current view')}">${_('Update commits')}</a>
458 % endif
459 % endif
459
460
460 </div>
461 </div>
461 % endif
462 % endif
462 </div>
463 </div>
463
464
464 % if not c.missing_commits:
465 % if not c.missing_commits:
465 % if c.compare_mode:
466 % if c.compare_mode:
466 % if c.at_version:
467 % if c.at_version:
467 <h4>
468 <h4>
468 ${_('Commits and changes between v{ver_from} and {ver_to} of this pull request, commenting is disabled').format(ver_from=c.from_version_pos, ver_to=c.at_version_pos if c.at_version_pos else 'latest')}:
469 ${_('Commits and changes between v{ver_from} and {ver_to} of this pull request, commenting is disabled').format(ver_from=c.from_version_pos, ver_to=c.at_version_pos if c.at_version_pos else 'latest')}:
469 </h4>
470 </h4>
470
471
471 <div class="subtitle-compare">
472 <div class="subtitle-compare">
472 ${_('commits added: {}, removed: {}').format(len(c.commit_changes_summary.added), len(c.commit_changes_summary.removed))}
473 ${_('commits added: {}, removed: {}').format(len(c.commit_changes_summary.added), len(c.commit_changes_summary.removed))}
473 </div>
474 </div>
474
475
475 <div class="container">
476 <div class="container">
476 <table class="rctable compare_view_commits">
477 <table class="rctable compare_view_commits">
477 <tr>
478 <tr>
478 <th></th>
479 <th></th>
479 <th>${_('Time')}</th>
480 <th>${_('Time')}</th>
480 <th>${_('Author')}</th>
481 <th>${_('Author')}</th>
481 <th>${_('Commit')}</th>
482 <th>${_('Commit')}</th>
482 <th></th>
483 <th></th>
483 <th>${_('Description')}</th>
484 <th>${_('Description')}</th>
484 </tr>
485 </tr>
485
486
486 % for c_type, commit in c.commit_changes:
487 % for c_type, commit in c.commit_changes:
487 % if c_type in ['a', 'r']:
488 % if c_type in ['a', 'r']:
488 <%
489 <%
489 if c_type == 'a':
490 if c_type == 'a':
490 cc_title = _('Commit added in displayed changes')
491 cc_title = _('Commit added in displayed changes')
491 elif c_type == 'r':
492 elif c_type == 'r':
492 cc_title = _('Commit removed in displayed changes')
493 cc_title = _('Commit removed in displayed changes')
493 else:
494 else:
494 cc_title = ''
495 cc_title = ''
495 %>
496 %>
496 <tr id="row-${commit.raw_id}" commit_id="${commit.raw_id}" class="compare_select">
497 <tr id="row-${commit.raw_id}" commit_id="${commit.raw_id}" class="compare_select">
497 <td>
498 <td>
498 <div class="commit-change-indicator color-${c_type}-border">
499 <div class="commit-change-indicator color-${c_type}-border">
499 <div class="commit-change-content color-${c_type} tooltip" title="${h.tooltip(cc_title)}">
500 <div class="commit-change-content color-${c_type} tooltip" title="${h.tooltip(cc_title)}">
500 ${c_type.upper()}
501 ${c_type.upper()}
501 </div>
502 </div>
502 </div>
503 </div>
503 </td>
504 </td>
504 <td class="td-time">
505 <td class="td-time">
505 ${h.age_component(commit.date)}
506 ${h.age_component(commit.date)}
506 </td>
507 </td>
507 <td class="td-user">
508 <td class="td-user">
508 ${base.gravatar_with_user(commit.author, 16)}
509 ${base.gravatar_with_user(commit.author, 16)}
509 </td>
510 </td>
510 <td class="td-hash">
511 <td class="td-hash">
511 <code>
512 <code>
512 <a href="${h.route_path('repo_commit', repo_name=c.target_repo.repo_name, commit_id=commit.raw_id)}">
513 <a href="${h.route_path('repo_commit', repo_name=c.target_repo.repo_name, commit_id=commit.raw_id)}">
513 r${commit.revision}:${h.short_id(commit.raw_id)}
514 r${commit.revision}:${h.short_id(commit.raw_id)}
514 </a>
515 </a>
515 ${h.hidden('revisions', commit.raw_id)}
516 ${h.hidden('revisions', commit.raw_id)}
516 </code>
517 </code>
517 </td>
518 </td>
518 <td class="expand_commit" data-commit-id="${commit.raw_id}" title="${_( 'Expand commit message')}">
519 <td class="expand_commit" data-commit-id="${commit.raw_id}" title="${_( 'Expand commit message')}">
519 <div class="show_more_col">
520 <div class="show_more_col">
520 <i class="show_more"></i>
521 <i class="show_more"></i>
521 </div>
522 </div>
522 </td>
523 </td>
523 <td class="mid td-description">
524 <td class="mid td-description">
524 <div class="log-container truncate-wrap">
525 <div class="log-container truncate-wrap">
525 <div class="message truncate" id="c-${commit.raw_id}" data-message-raw="${commit.message}">
526 <div class="message truncate" id="c-${commit.raw_id}" data-message-raw="${commit.message}">
526 ${h.urlify_commit_message(commit.message, c.repo_name)}
527 ${h.urlify_commit_message(commit.message, c.repo_name)}
527 </div>
528 </div>
528 </div>
529 </div>
529 </td>
530 </td>
530 </tr>
531 </tr>
531 % endif
532 % endif
532 % endfor
533 % endfor
533 </table>
534 </table>
534 </div>
535 </div>
535
536
536 <script>
537 <script>
537 $('.expand_commit').on('click',function(e){
538 $('.expand_commit').on('click',function(e){
538 var target_expand = $(this);
539 var target_expand = $(this);
539 var cid = target_expand.data('commitId');
540 var cid = target_expand.data('commitId');
540
541
541 if (target_expand.hasClass('open')){
542 if (target_expand.hasClass('open')){
542 $('#c-'+cid).css({
543 $('#c-'+cid).css({
543 'height': '1.5em',
544 'height': '1.5em',
544 'white-space': 'nowrap',
545 'white-space': 'nowrap',
545 'text-overflow': 'ellipsis',
546 'text-overflow': 'ellipsis',
546 'overflow':'hidden'
547 'overflow':'hidden'
547 });
548 });
548 target_expand.removeClass('open');
549 target_expand.removeClass('open');
549 }
550 }
550 else {
551 else {
551 $('#c-'+cid).css({
552 $('#c-'+cid).css({
552 'height': 'auto',
553 'height': 'auto',
553 'white-space': 'pre-line',
554 'white-space': 'pre-line',
554 'text-overflow': 'initial',
555 'text-overflow': 'initial',
555 'overflow':'visible'
556 'overflow':'visible'
556 });
557 });
557 target_expand.addClass('open');
558 target_expand.addClass('open');
558 }
559 }
559 });
560 });
560 </script>
561 </script>
561
562
562 % endif
563 % endif
563
564
564 % else:
565 % else:
565 <%include file="/compare/compare_commits.mako" />
566 <%include file="/compare/compare_commits.mako" />
566 % endif
567 % endif
567
568
568 <div class="cs_files">
569 <div class="cs_files">
569 <%namespace name="cbdiffs" file="/codeblocks/diffs.mako"/>
570 <%namespace name="cbdiffs" file="/codeblocks/diffs.mako"/>
570 ${cbdiffs.render_diffset_menu()}
571 ${cbdiffs.render_diffset_menu()}
571 ${cbdiffs.render_diffset(
572 ${cbdiffs.render_diffset(
572 c.diffset, use_comments=True,
573 c.diffset, use_comments=True,
573 collapse_when_files_over=30,
574 collapse_when_files_over=30,
574 disable_new_comments=not c.allowed_to_comment,
575 disable_new_comments=not c.allowed_to_comment,
575 deleted_files_comments=c.deleted_files_comments,
576 deleted_files_comments=c.deleted_files_comments,
576 inline_comments=c.inline_comments)}
577 inline_comments=c.inline_comments)}
577 </div>
578 </div>
578 % else:
579 % else:
579 ## skipping commits we need to clear the view for missing commits
580 ## skipping commits we need to clear the view for missing commits
580 <div style="clear:both;"></div>
581 <div style="clear:both;"></div>
581 % endif
582 % endif
582
583
583 </div>
584 </div>
584 </div>
585 </div>
585
586
586 ## template for inline comment form
587 ## template for inline comment form
587 <%namespace name="comment" file="/changeset/changeset_file_comment.mako"/>
588 <%namespace name="comment" file="/changeset/changeset_file_comment.mako"/>
588
589
589 ## render general comments
590 ## render general comments
590
591
591 <div id="comment-tr-show">
592 <div id="comment-tr-show">
592 <div class="comment">
593 <div class="comment">
593 % if general_outdated_comm_count_ver:
594 % if general_outdated_comm_count_ver:
594 <div class="meta">
595 <div class="meta">
595 % if general_outdated_comm_count_ver == 1:
596 % if general_outdated_comm_count_ver == 1:
596 ${_('there is {num} general comment from older versions').format(num=general_outdated_comm_count_ver)},
597 ${_('there is {num} general comment from older versions').format(num=general_outdated_comm_count_ver)},
597 <a href="#show-hidden-comments" onclick="$('.comment-general.comment-outdated').show(); $(this).parent().hide(); return false;">${_('show it')}</a>
598 <a href="#show-hidden-comments" onclick="$('.comment-general.comment-outdated').show(); $(this).parent().hide(); return false;">${_('show it')}</a>
598 % else:
599 % else:
599 ${_('there are {num} general comments from older versions').format(num=general_outdated_comm_count_ver)},
600 ${_('there are {num} general comments from older versions').format(num=general_outdated_comm_count_ver)},
600 <a href="#show-hidden-comments" onclick="$('.comment-general.comment-outdated').show(); $(this).parent().hide(); return false;">${_('show them')}</a>
601 <a href="#show-hidden-comments" onclick="$('.comment-general.comment-outdated').show(); $(this).parent().hide(); return false;">${_('show them')}</a>
601 % endif
602 % endif
602 </div>
603 </div>
603 % endif
604 % endif
604 </div>
605 </div>
605 </div>
606 </div>
606
607
607 ${comment.generate_comments(c.comments, include_pull_request=True, is_pull_request=True)}
608 ${comment.generate_comments(c.comments, include_pull_request=True, is_pull_request=True)}
608
609
609 % if not c.pull_request.is_closed():
610 % if not c.pull_request.is_closed():
610 ## merge status, and merge action
611 ## merge status, and merge action
611 <div class="pull-request-merge">
612 <div class="pull-request-merge">
612 <%include file="/pullrequests/pullrequest_merge_checks.mako"/>
613 <%include file="/pullrequests/pullrequest_merge_checks.mako"/>
613 </div>
614 </div>
614
615
615 ## main comment form and it status
616 ## main comment form and it status
616 ${comment.comments(h.route_path('pullrequest_comment_create', repo_name=c.repo_name,
617 ${comment.comments(h.route_path('pullrequest_comment_create', repo_name=c.repo_name,
617 pull_request_id=c.pull_request.pull_request_id),
618 pull_request_id=c.pull_request.pull_request_id),
618 c.pull_request_review_status,
619 c.pull_request_review_status,
619 is_pull_request=True, change_status=c.allowed_to_change_status)}
620 is_pull_request=True, change_status=c.allowed_to_change_status)}
620 %endif
621 %endif
621
622
622 <script type="text/javascript">
623 <script type="text/javascript">
623 if (location.hash) {
624 if (location.hash) {
624 var result = splitDelimitedHash(location.hash);
625 var result = splitDelimitedHash(location.hash);
625 var line = $('html').find(result.loc);
626 var line = $('html').find(result.loc);
626 // show hidden comments if we use location.hash
627 // show hidden comments if we use location.hash
627 if (line.hasClass('comment-general')) {
628 if (line.hasClass('comment-general')) {
628 $(line).show();
629 $(line).show();
629 } else if (line.hasClass('comment-inline')) {
630 } else if (line.hasClass('comment-inline')) {
630 $(line).show();
631 $(line).show();
631 var $cb = $(line).closest('.cb');
632 var $cb = $(line).closest('.cb');
632 $cb.removeClass('cb-collapsed')
633 $cb.removeClass('cb-collapsed')
633 }
634 }
634 if (line.length > 0){
635 if (line.length > 0){
635 offsetScroll(line, 70);
636 offsetScroll(line, 70);
636 }
637 }
637 }
638 }
638
639
639 versionController = new VersionController();
640 versionController = new VersionController();
640 versionController.init();
641 versionController.init();
641
642
642 reviewersController = new ReviewersController();
643 reviewersController = new ReviewersController();
643
644
644 $(function(){
645 $(function(){
645
646
646 // custom code mirror
647 // custom code mirror
647 var codeMirrorInstance = $('#pr-description-input').get(0).MarkupForm.cm;
648 var codeMirrorInstance = $('#pr-description-input').get(0).MarkupForm.cm;
648
649
649 var PRDetails = {
650 var PRDetails = {
650 editButton: $('#open_edit_pullrequest'),
651 editButton: $('#open_edit_pullrequest'),
651 closeButton: $('#close_edit_pullrequest'),
652 closeButton: $('#close_edit_pullrequest'),
652 deleteButton: $('#delete_pullrequest'),
653 deleteButton: $('#delete_pullrequest'),
653 viewFields: $('#pr-desc, #pr-title'),
654 viewFields: $('#pr-desc, #pr-title'),
654 editFields: $('#pr-desc-edit, #pr-title-edit, #pr-save'),
655 editFields: $('#pr-desc-edit, #pr-title-edit, #pr-save'),
655
656
656 init: function() {
657 init: function() {
657 var that = this;
658 var that = this;
658 this.editButton.on('click', function(e) { that.edit(); });
659 this.editButton.on('click', function(e) { that.edit(); });
659 this.closeButton.on('click', function(e) { that.view(); });
660 this.closeButton.on('click', function(e) { that.view(); });
660 },
661 },
661
662
662 edit: function(event) {
663 edit: function(event) {
663 this.viewFields.hide();
664 this.viewFields.hide();
664 this.editButton.hide();
665 this.editButton.hide();
665 this.deleteButton.hide();
666 this.deleteButton.hide();
666 this.closeButton.show();
667 this.closeButton.show();
667 this.editFields.show();
668 this.editFields.show();
668 codeMirrorInstance.refresh();
669 codeMirrorInstance.refresh();
669 },
670 },
670
671
671 view: function(event) {
672 view: function(event) {
672 this.editButton.show();
673 this.editButton.show();
673 this.deleteButton.show();
674 this.deleteButton.show();
674 this.editFields.hide();
675 this.editFields.hide();
675 this.closeButton.hide();
676 this.closeButton.hide();
676 this.viewFields.show();
677 this.viewFields.show();
677 }
678 }
678 };
679 };
679
680
680 var ReviewersPanel = {
681 var ReviewersPanel = {
681 editButton: $('#open_edit_reviewers'),
682 editButton: $('#open_edit_reviewers'),
682 closeButton: $('#close_edit_reviewers'),
683 closeButton: $('#close_edit_reviewers'),
683 addButton: $('#add_reviewer'),
684 addButton: $('#add_reviewer'),
684 removeButtons: $('.reviewer_member_remove,.reviewer_member_mandatory_remove'),
685 removeButtons: $('.reviewer_member_remove,.reviewer_member_mandatory_remove'),
685
686
686 init: function() {
687 init: function() {
687 var self = this;
688 var self = this;
688 this.editButton.on('click', function(e) { self.edit(); });
689 this.editButton.on('click', function(e) { self.edit(); });
689 this.closeButton.on('click', function(e) { self.close(); });
690 this.closeButton.on('click', function(e) { self.close(); });
690 },
691 },
691
692
692 edit: function(event) {
693 edit: function(event) {
693 this.editButton.hide();
694 this.editButton.hide();
694 this.closeButton.show();
695 this.closeButton.show();
695 this.addButton.show();
696 this.addButton.show();
696 this.removeButtons.css('visibility', 'visible');
697 this.removeButtons.css('visibility', 'visible');
697 // review rules
698 // review rules
698 reviewersController.loadReviewRules(
699 reviewersController.loadReviewRules(
699 ${c.pull_request.reviewer_data_json | n});
700 ${c.pull_request.reviewer_data_json | n});
700 },
701 },
701
702
702 close: function(event) {
703 close: function(event) {
703 this.editButton.show();
704 this.editButton.show();
704 this.closeButton.hide();
705 this.closeButton.hide();
705 this.addButton.hide();
706 this.addButton.hide();
706 this.removeButtons.css('visibility', 'hidden');
707 this.removeButtons.css('visibility', 'hidden');
707 // hide review rules
708 // hide review rules
708 reviewersController.hideReviewRules()
709 reviewersController.hideReviewRules()
709 }
710 }
710 };
711 };
711
712
712 PRDetails.init();
713 PRDetails.init();
713 ReviewersPanel.init();
714 ReviewersPanel.init();
714
715
715 showOutdated = function(self){
716 showOutdated = function(self){
716 $('.comment-inline.comment-outdated').show();
717 $('.comment-inline.comment-outdated').show();
717 $('.filediff-outdated').show();
718 $('.filediff-outdated').show();
718 $('.showOutdatedComments').hide();
719 $('.showOutdatedComments').hide();
719 $('.hideOutdatedComments').show();
720 $('.hideOutdatedComments').show();
720 };
721 };
721
722
722 hideOutdated = function(self){
723 hideOutdated = function(self){
723 $('.comment-inline.comment-outdated').hide();
724 $('.comment-inline.comment-outdated').hide();
724 $('.filediff-outdated').hide();
725 $('.filediff-outdated').hide();
725 $('.hideOutdatedComments').hide();
726 $('.hideOutdatedComments').hide();
726 $('.showOutdatedComments').show();
727 $('.showOutdatedComments').show();
727 };
728 };
728
729
729 refreshMergeChecks = function(){
730 refreshMergeChecks = function(){
730 var loadUrl = "${request.current_route_path(_query=dict(merge_checks=1))}";
731 var loadUrl = "${request.current_route_path(_query=dict(merge_checks=1))}";
731 $('.pull-request-merge').css('opacity', 0.3);
732 $('.pull-request-merge').css('opacity', 0.3);
732 $('.action-buttons-extra').css('opacity', 0.3);
733 $('.action-buttons-extra').css('opacity', 0.3);
733
734
734 $('.pull-request-merge').load(
735 $('.pull-request-merge').load(
735 loadUrl, function() {
736 loadUrl, function() {
736 $('.pull-request-merge').css('opacity', 1);
737 $('.pull-request-merge').css('opacity', 1);
737
738
738 $('.action-buttons-extra').css('opacity', 1);
739 $('.action-buttons-extra').css('opacity', 1);
739 injectCloseAction();
740 injectCloseAction();
740 }
741 }
741 );
742 );
742 };
743 };
743
744
744 injectCloseAction = function() {
745 injectCloseAction = function() {
745 var closeAction = $('#close-pull-request-action').html();
746 var closeAction = $('#close-pull-request-action').html();
746 var $actionButtons = $('.action-buttons-extra');
747 var $actionButtons = $('.action-buttons-extra');
747 // clear the action before
748 // clear the action before
748 $actionButtons.html("");
749 $actionButtons.html("");
749 $actionButtons.html(closeAction);
750 $actionButtons.html(closeAction);
750 };
751 };
751
752
752 closePullRequest = function (status) {
753 closePullRequest = function (status) {
753 // inject closing flag
754 // inject closing flag
754 $('.action-buttons-extra').append('<input type="hidden" class="close-pr-input" id="close_pull_request" value="1">');
755 $('.action-buttons-extra').append('<input type="hidden" class="close-pr-input" id="close_pull_request" value="1">');
755 $(generalCommentForm.statusChange).select2("val", status).trigger('change');
756 $(generalCommentForm.statusChange).select2("val", status).trigger('change');
756 $(generalCommentForm.submitForm).submit();
757 $(generalCommentForm.submitForm).submit();
757 };
758 };
758
759
759 $('#show-outdated-comments').on('click', function(e){
760 $('#show-outdated-comments').on('click', function(e){
760 var button = $(this);
761 var button = $(this);
761 var outdated = $('.comment-outdated');
762 var outdated = $('.comment-outdated');
762
763
763 if (button.html() === "(Show)") {
764 if (button.html() === "(Show)") {
764 button.html("(Hide)");
765 button.html("(Hide)");
765 outdated.show();
766 outdated.show();
766 } else {
767 } else {
767 button.html("(Show)");
768 button.html("(Show)");
768 outdated.hide();
769 outdated.hide();
769 }
770 }
770 });
771 });
771
772
772 $('.show-inline-comments').on('change', function(e){
773 $('.show-inline-comments').on('change', function(e){
773 var show = 'none';
774 var show = 'none';
774 var target = e.currentTarget;
775 var target = e.currentTarget;
775 if(target.checked){
776 if(target.checked){
776 show = ''
777 show = ''
777 }
778 }
778 var boxid = $(target).attr('id_for');
779 var boxid = $(target).attr('id_for');
779 var comments = $('#{0} .inline-comments'.format(boxid));
780 var comments = $('#{0} .inline-comments'.format(boxid));
780 var fn_display = function(idx){
781 var fn_display = function(idx){
781 $(this).css('display', show);
782 $(this).css('display', show);
782 };
783 };
783 $(comments).each(fn_display);
784 $(comments).each(fn_display);
784 var btns = $('#{0} .inline-comments-button'.format(boxid));
785 var btns = $('#{0} .inline-comments-button'.format(boxid));
785 $(btns).each(fn_display);
786 $(btns).each(fn_display);
786 });
787 });
787
788
788 $('#merge_pull_request_form').submit(function() {
789 $('#merge_pull_request_form').submit(function() {
789 if (!$('#merge_pull_request').attr('disabled')) {
790 if (!$('#merge_pull_request').attr('disabled')) {
790 $('#merge_pull_request').attr('disabled', 'disabled');
791 $('#merge_pull_request').attr('disabled', 'disabled');
791 }
792 }
792 return true;
793 return true;
793 });
794 });
794
795
795 $('#edit_pull_request').on('click', function(e){
796 $('#edit_pull_request').on('click', function(e){
796 var title = $('#pr-title-input').val();
797 var title = $('#pr-title-input').val();
797 var description = codeMirrorInstance.getValue();
798 var description = codeMirrorInstance.getValue();
799 var renderer = $('#pr-renderer-input').val();
798 editPullRequest(
800 editPullRequest(
799 "${c.repo_name}", "${c.pull_request.pull_request_id}",
801 "${c.repo_name}", "${c.pull_request.pull_request_id}",
800 title, description);
802 title, description, renderer);
801 });
803 });
802
804
803 $('#update_pull_request').on('click', function(e){
805 $('#update_pull_request').on('click', function(e){
804 $(this).attr('disabled', 'disabled');
806 $(this).attr('disabled', 'disabled');
805 $(this).addClass('disabled');
807 $(this).addClass('disabled');
806 $(this).html(_gettext('Saving...'));
808 $(this).html(_gettext('Saving...'));
807 reviewersController.updateReviewers(
809 reviewersController.updateReviewers(
808 "${c.repo_name}", "${c.pull_request.pull_request_id}");
810 "${c.repo_name}", "${c.pull_request.pull_request_id}");
809 });
811 });
810
812
811 $('#update_commits').on('click', function(e){
813 $('#update_commits').on('click', function(e){
812 var isDisabled = !$(e.currentTarget).attr('disabled');
814 var isDisabled = !$(e.currentTarget).attr('disabled');
813 $(e.currentTarget).attr('disabled', 'disabled');
815 $(e.currentTarget).attr('disabled', 'disabled');
814 $(e.currentTarget).addClass('disabled');
816 $(e.currentTarget).addClass('disabled');
815 $(e.currentTarget).removeClass('btn-primary');
817 $(e.currentTarget).removeClass('btn-primary');
816 $(e.currentTarget).text(_gettext('Updating...'));
818 $(e.currentTarget).text(_gettext('Updating...'));
817 if(isDisabled){
819 if(isDisabled){
818 updateCommits(
820 updateCommits(
819 "${c.repo_name}", "${c.pull_request.pull_request_id}");
821 "${c.repo_name}", "${c.pull_request.pull_request_id}");
820 }
822 }
821 });
823 });
822 // fixing issue with caches on firefox
824 // fixing issue with caches on firefox
823 $('#update_commits').removeAttr("disabled");
825 $('#update_commits').removeAttr("disabled");
824
826
825 $('.show-inline-comments').on('click', function(e){
827 $('.show-inline-comments').on('click', function(e){
826 var boxid = $(this).attr('data-comment-id');
828 var boxid = $(this).attr('data-comment-id');
827 var button = $(this);
829 var button = $(this);
828
830
829 if(button.hasClass("comments-visible")) {
831 if(button.hasClass("comments-visible")) {
830 $('#{0} .inline-comments'.format(boxid)).each(function(index){
832 $('#{0} .inline-comments'.format(boxid)).each(function(index){
831 $(this).hide();
833 $(this).hide();
832 });
834 });
833 button.removeClass("comments-visible");
835 button.removeClass("comments-visible");
834 } else {
836 } else {
835 $('#{0} .inline-comments'.format(boxid)).each(function(index){
837 $('#{0} .inline-comments'.format(boxid)).each(function(index){
836 $(this).show();
838 $(this).show();
837 });
839 });
838 button.addClass("comments-visible");
840 button.addClass("comments-visible");
839 }
841 }
840 });
842 });
841
843
842 // register submit callback on commentForm form to track TODOs
844 // register submit callback on commentForm form to track TODOs
843 window.commentFormGlobalSubmitSuccessCallback = function(){
845 window.commentFormGlobalSubmitSuccessCallback = function(){
844 refreshMergeChecks();
846 refreshMergeChecks();
845 };
847 };
846 // initial injection
848 // initial injection
847 injectCloseAction();
849 injectCloseAction();
848
850
849 ReviewerAutoComplete('#user');
851 ReviewerAutoComplete('#user');
850
852
851 })
853 })
852 </script>
854 </script>
853
855
854 </div>
856 </div>
855 </div>
857 </div>
856
858
857 </%def>
859 </%def>
General Comments 0
You need to be logged in to leave comments. Login now