##// END OF EJS Templates
data checks
marcink -
r2444:b45e9fd7 codereview
parent child Browse files
Show More
@@ -1,99 +1,103 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.controllers.compare
4 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 compare controller for pylons showoing differences between two
7 7 repos, branches, bookmarks or tips
8 8
9 9 :created_on: May 6, 2012
10 10 :author: marcink
11 11 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
12 12 :license: GPLv3, see COPYING for more details.
13 13 """
14 14 # This program is free software: you can redistribute it and/or modify
15 15 # it under the terms of the GNU General Public License as published by
16 16 # the Free Software Foundation, either version 3 of the License, or
17 17 # (at your option) any later version.
18 18 #
19 19 # This program is distributed in the hope that it will be useful,
20 20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 22 # GNU General Public License for more details.
23 23 #
24 24 # You should have received a copy of the GNU General Public License
25 25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 26 import logging
27 27 import traceback
28 28
29 29 from webob.exc import HTTPNotFound
30 30 from pylons import request, response, session, tmpl_context as c, url
31 31 from pylons.controllers.util import abort, redirect
32 32
33 33 from rhodecode.lib import helpers as h
34 34 from rhodecode.lib.base import BaseRepoController, render
35 35 from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator
36 36 from rhodecode.lib import diffs
37 37
38 38 from rhodecode.model.db import Repository
39 39 from rhodecode.model.pull_request import PullRequestModel
40 40
41 41 log = logging.getLogger(__name__)
42 42
43 43
44 44 class CompareController(BaseRepoController):
45 45
46 46 @LoginRequired()
47 47 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
48 48 'repository.admin')
49 49 def __before__(self):
50 50 super(CompareController, self).__before__()
51 51
52 52 def index(self, org_ref_type, org_ref, other_ref_type, other_ref):
53 53
54 54 org_repo = c.rhodecode_db_repo.repo_name
55 55 org_ref = (org_ref_type, org_ref)
56 56 other_ref = (other_ref_type, other_ref)
57 57 other_repo = request.GET.get('repo', org_repo)
58 58
59 59 c.swap_url = h.url('compare_url', repo_name=other_repo,
60 60 org_ref_type=other_ref[0], org_ref=other_ref[1],
61 61 other_ref_type=org_ref[0], other_ref=org_ref[1],
62 62 repo=org_repo)
63 63
64 64 c.org_repo = org_repo = Repository.get_by_repo_name(org_repo)
65 65 c.other_repo = other_repo = Repository.get_by_repo_name(other_repo)
66 66
67 67 if c.org_repo is None or c.other_repo is None:
68 68 log.error('Could not found repo %s or %s' % (org_repo, other_repo))
69 69 raise HTTPNotFound
70 70
71 if c.org_repo.scm_instance.alias != 'hg':
72 log.error('Review not available for GIT REPOS')
73 raise HTTPNotFound
74
71 75 c.cs_ranges, discovery_data = PullRequestModel().get_compare_data(
72 76 org_repo, org_ref, other_repo, other_ref
73 77 )
74 78
75 79 c.statuses = c.rhodecode_db_repo.statuses([x.raw_id for x in
76 80 c.cs_ranges])
77 81 # defines that we need hidden inputs with changesets
78 82 c.as_form = request.GET.get('as_form', False)
79 83 if request.environ.get('HTTP_X_PARTIAL_XHR'):
80 84 return render('compare/compare_cs.html')
81 85
82 86 c.org_ref = org_ref[1]
83 87 c.other_ref = other_ref[1]
84 88 # diff needs to have swapped org with other to generate proper diff
85 89 _diff = diffs.differ(other_repo, other_ref, org_repo, org_ref,
86 90 discovery_data)
87 91 diff_processor = diffs.DiffProcessor(_diff, format='gitdiff')
88 92 _parsed = diff_processor.prepare()
89 93
90 94 c.files = []
91 95 c.changes = {}
92 96
93 97 for f in _parsed:
94 98 fid = h.FID('', f['filename'])
95 99 c.files.append([fid, f['operation'], f['filename'], f['stats']])
96 100 diff = diff_processor.as_html(enable_comments=False, diff_lines=[f])
97 101 c.changes[fid] = [f['operation'], f['filename'], diff]
98 102
99 103 return render('compare/compare_diff.html')
@@ -1,264 +1,277 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.controllers.pullrequests
4 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 pull requests controller for rhodecode for initializing pull requests
7 7
8 8 :created_on: May 7, 2012
9 9 :author: marcink
10 10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 11 :license: GPLv3, see COPYING for more details.
12 12 """
13 13 # This program is free software: you can redistribute it and/or modify
14 14 # it under the terms of the GNU General Public License as published by
15 15 # the Free Software Foundation, either version 3 of the License, or
16 16 # (at your option) any later version.
17 17 #
18 18 # This program is distributed in the hope that it will be useful,
19 19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 21 # GNU General Public License for more details.
22 22 #
23 23 # You should have received a copy of the GNU General Public License
24 24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 25 import logging
26 26 import traceback
27 27
28 28 from webob.exc import HTTPNotFound
29 29
30 30 from pylons import request, response, session, tmpl_context as c, url
31 31 from pylons.controllers.util import abort, redirect
32 32 from pylons.i18n.translation import _
33 33 from pylons.decorators import jsonify
34 34
35 35 from rhodecode.lib.base import BaseRepoController, render
36 36 from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator
37 37 from rhodecode.lib import helpers as h
38 38 from rhodecode.lib import diffs
39 39 from rhodecode.lib.utils import action_logger
40 40 from rhodecode.model.db import User, PullRequest, ChangesetStatus
41 41 from rhodecode.model.pull_request import PullRequestModel
42 42 from rhodecode.model.meta import Session
43 43 from rhodecode.model.repo import RepoModel
44 44 from rhodecode.model.comment import ChangesetCommentsModel
45 45 from rhodecode.model.changeset_status import ChangesetStatusModel
46 46
47 47 log = logging.getLogger(__name__)
48 48
49 49
50 50 class PullrequestsController(BaseRepoController):
51 51
52 52 @LoginRequired()
53 53 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
54 54 'repository.admin')
55 55 def __before__(self):
56 56 super(PullrequestsController, self).__before__()
57 57
58 58 def _get_repo_refs(self, repo):
59 59 hist_l = []
60 60
61 61 branches_group = ([('branch:%s:%s' % (k, v), k) for
62 62 k, v in repo.branches.iteritems()], _("Branches"))
63 63 bookmarks_group = ([('book:%s:%s' % (k, v), k) for
64 64 k, v in repo.bookmarks.iteritems()], _("Bookmarks"))
65 65 tags_group = ([('tag:%s:%s' % (k, v), k) for
66 66 k, v in repo.tags.iteritems()], _("Tags"))
67 67
68 68 hist_l.append(bookmarks_group)
69 69 hist_l.append(branches_group)
70 70 hist_l.append(tags_group)
71 71
72 72 return hist_l
73 73
74 74 def show_all(self, repo_name):
75 75 c.pull_requests = PullRequestModel().get_all(repo_name)
76 76 c.repo_name = repo_name
77 77 return render('/pullrequests/pullrequest_show_all.html')
78 78
79 79 def index(self):
80 80 org_repo = c.rhodecode_db_repo
81
82 if org_repo.scm_instance.alias != 'hg':
83 log.error('Review not available for GIT REPOS')
84 raise HTTPNotFound
85
81 86 c.org_refs = self._get_repo_refs(c.rhodecode_repo)
82 87 c.org_repos = []
83 88 c.other_repos = []
84 89 c.org_repos.append((org_repo.repo_name, '%s/%s' % (
85 90 org_repo.user.username, c.repo_name))
86 91 )
87 92
88 93 c.other_refs = c.org_refs
89 94 c.other_repos.extend(c.org_repos)
90 95 c.default_pull_request = org_repo.repo_name
91 96 #gather forks and add to this list
92 97 for fork in org_repo.forks:
93 98 c.other_repos.append((fork.repo_name, '%s/%s' % (
94 99 fork.user.username, fork.repo_name))
95 100 )
96 101 #add parents of this fork also
97 102 if org_repo.parent:
98 103 c.default_pull_request = org_repo.parent.repo_name
99 104 c.other_repos.append((org_repo.parent.repo_name, '%s/%s' % (
100 105 org_repo.parent.user.username,
101 106 org_repo.parent.repo_name))
102 107 )
103 108
104 #TODO: maybe the owner should be default ?
105 109 c.review_members = []
106 110 c.available_members = []
107 111 for u in User.query().filter(User.username != 'default').all():
108 112 uname = u.username
109 113 if org_repo.user == u:
110 114 uname = _('%s (owner)' % u.username)
111 115 # auto add owner to pull-request recipients
112 116 c.review_members.append([u.user_id, uname])
113 117 c.available_members.append([u.user_id, uname])
114 118 return render('/pullrequests/pullrequest.html')
115 119
116 120 def create(self, repo_name):
117 121 req_p = request.POST
118 122 org_repo = req_p['org_repo']
119 123 org_ref = req_p['org_ref']
120 124 other_repo = req_p['other_repo']
121 125 other_ref = req_p['other_ref']
122 126 revisions = req_p.getall('revisions')
123 127 reviewers = req_p.getall('review_members')
124 128 #TODO: wrap this into a FORM !!!
125 129
126 130 title = req_p['pullrequest_title']
127 131 description = req_p['pullrequest_desc']
128 132
129 133 try:
130 134 model = PullRequestModel()
131 135 model.create(self.rhodecode_user.user_id, org_repo,
132 136 org_ref, other_repo, other_ref, revisions,
133 137 reviewers, title, description)
134 138 Session.commit()
135 139 h.flash(_('Pull request send'), category='success')
136 140 except Exception:
137 141 raise
138 142 h.flash(_('Error occured during sending pull request'),
139 143 category='error')
140 144 log.error(traceback.format_exc())
141 145
142 146 return redirect(url('changelog_home', repo_name=repo_name))
143 147
144 148 def _load_compare_data(self, pull_request):
145 149 """
146 150 Load context data needed for generating compare diff
147 151
148 152 :param pull_request:
149 153 :type pull_request:
150 154 """
151 155
152 156 org_repo = pull_request.org_repo
153 157 org_ref_type, org_ref_, org_ref = pull_request.org_ref.split(':')
154 158 other_repo = pull_request.other_repo
155 159 other_ref_type, other_ref, other_ref_ = pull_request.other_ref.split(':')
156 160
157 161 org_ref = (org_ref_type, org_ref)
158 162 other_ref = (other_ref_type, other_ref)
159 163
160 164 c.org_repo = org_repo
161 165 c.other_repo = other_repo
162 166
163 167 c.cs_ranges, discovery_data = PullRequestModel().get_compare_data(
164 168 org_repo, org_ref, other_repo, other_ref
165 169 )
166 170
167 171 c.statuses = c.rhodecode_db_repo.statuses([x.raw_id for x in
168 172 c.cs_ranges])
169 173 # defines that we need hidden inputs with changesets
170 174 c.as_form = request.GET.get('as_form', False)
171 175
172 176 c.org_ref = org_ref[1]
173 177 c.other_ref = other_ref[1]
174 178 # diff needs to have swapped org with other to generate proper diff
175 179 _diff = diffs.differ(other_repo, other_ref, org_repo, org_ref,
176 180 discovery_data)
177 181 diff_processor = diffs.DiffProcessor(_diff, format='gitdiff')
178 182 _parsed = diff_processor.prepare()
179 183
180 184 c.files = []
181 185 c.changes = {}
182 186
183 187 for f in _parsed:
184 188 fid = h.FID('', f['filename'])
185 189 c.files.append([fid, f['operation'], f['filename'], f['stats']])
186 190 diff = diff_processor.as_html(enable_comments=False, diff_lines=[f])
187 191 c.changes[fid] = [f['operation'], f['filename'], diff]
188 192
189 193 def show(self, repo_name, pull_request_id):
190 194 repo_model = RepoModel()
191 195 c.users_array = repo_model.get_users_js()
192 196 c.users_groups_array = repo_model.get_users_groups_js()
193 197 c.pull_request = PullRequest.get(pull_request_id)
194 198
199 # valid ID
200 if not c.pull_request:
201 raise HTTPNotFound
202
203 # pull_requests repo_name we opened it against
204 # ie. other_repo must match
205 if repo_name != c.pull_request.other_repo.repo_name:
206 raise HTTPNotFound
207
195 208 # load compare data into template context
196 209 self._load_compare_data(c.pull_request)
197 210
198 211 # inline comments
199 212 c.inline_cnt = 0
200 213 c.inline_comments = ChangesetCommentsModel()\
201 214 .get_inline_comments(c.rhodecode_db_repo.repo_id,
202 215 pull_request=pull_request_id)
203 216 # count inline comments
204 217 for __, lines in c.inline_comments:
205 218 for comments in lines.values():
206 219 c.inline_cnt += len(comments)
207 220 # comments
208 221 c.comments = ChangesetCommentsModel()\
209 222 .get_comments(c.rhodecode_db_repo.repo_id,
210 223 pull_request=pull_request_id)
211 224
212 225 # changeset(pull-request) status
213 226 c.current_changeset_status = ChangesetStatusModel()\
214 227 .get_status(c.pull_request.org_repo,
215 228 pull_request=c.pull_request)
216 229 c.changeset_statuses = ChangesetStatus.STATUSES
217 230 return render('/pullrequests/pullrequest_show.html')
218 231
219 232 @jsonify
220 233 def comment(self, repo_name, pull_request_id):
221 234
222 235 status = request.POST.get('changeset_status')
223 236 change_status = request.POST.get('change_changeset_status')
224 237
225 238 comm = ChangesetCommentsModel().create(
226 239 text=request.POST.get('text'),
227 240 repo_id=c.rhodecode_db_repo.repo_id,
228 241 user_id=c.rhodecode_user.user_id,
229 242 pull_request=pull_request_id,
230 243 f_path=request.POST.get('f_path'),
231 244 line_no=request.POST.get('line'),
232 245 status_change=(ChangesetStatus.get_status_lbl(status)
233 246 if status and change_status else None)
234 247 )
235 248
236 249 # get status if set !
237 250 if status and change_status:
238 251 ChangesetStatusModel().set_status(
239 252 c.rhodecode_db_repo.repo_id,
240 253 status,
241 254 c.rhodecode_user.user_id,
242 255 comm,
243 256 pull_request=pull_request_id
244 257 )
245 258 action_logger(self.rhodecode_user,
246 259 'user_commented_pull_request:%s' % pull_request_id,
247 260 c.rhodecode_db_repo, self.ip_addr, self.sa)
248 261
249 262 Session.commit()
250 263
251 264 if not request.environ.get('HTTP_X_PARTIAL_XHR'):
252 265 return redirect(h.url('pullrequest_show', repo_name=repo_name,
253 266 pull_request_id=pull_request_id))
254 267
255 268 data = {
256 269 'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))),
257 270 }
258 271 if comm:
259 272 c.co = comm
260 273 data.update(comm.get_dict())
261 274 data.update({'rendered_text':
262 275 render('changeset/changeset_comment_block.html')})
263 276
264 return data No newline at end of file
277 return data
General Comments 0
You need to be logged in to leave comments. Login now