Commits

Marcin Kuzminski committed 1de45f5

added more validations when opening pull request
- fixed issues with moving tags/bookmarks/branches after opened pull-request
- no longer possible to open a pull-request without changesets
- title is required
- validate reviewers and revisions

Comments (0)

Files changed (3)

rhodecode/controllers/pullrequests.py

 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import logging
 import traceback
+import formencode
 
 from webob.exc import HTTPNotFound, HTTPForbidden
 from collections import defaultdict
 from rhodecode.model.repo import RepoModel
 from rhodecode.model.comment import ChangesetCommentsModel
 from rhodecode.model.changeset_status import ChangesetStatusModel
+from rhodecode.model.forms import PullRequestForm
 
 log = logging.getLogger(__name__)
 
 
     @NotAnonymous()
     def create(self, repo_name):
-        req_p = request.POST
-        org_repo = req_p['org_repo']
-        org_ref = req_p['org_ref']
-        other_repo = req_p['other_repo']
-        other_ref = req_p['other_ref']
-        revisions = req_p.getall('revisions')
-        reviewers = req_p.getall('review_members')
 
-        #TODO: wrap this into a FORM !!!
+        try:
+            _form = PullRequestForm()().to_python(request.POST)
+        except formencode.Invalid, errors:
+            log.error(traceback.format_exc())
+            if errors.error_dict.get('revisions'):
+                msg = _('Cannot open a pull request with '
+                        'empty list of changesets')
+            elif errors.error_dict.get('pullrequest_title'):
+                msg = _('Pull request requires a title with min. 3 chars')
+            else:
+                msg = _('error during creation of pull request')
 
-        title = req_p['pullrequest_title']
-        description = req_p['pullrequest_desc']
+            h.flash(msg, 'error')
+            return redirect(url('pullrequest_home', repo_name=repo_name))
+
+        org_repo = _form['org_repo']
+        org_ref = _form['org_ref']
+        other_repo = _form['other_repo']
+        other_ref = _form['other_ref']
+        revisions = _form['revisions']
+        reviewers = _form['review_members']
+
+        title = _form['pullrequest_title']
+        description = _form['pullrequest_desc']
 
         try:
             pull_request = PullRequestModel().create(
             h.flash(_('Error occurred during sending pull request'),
                     category='error')
             log.error(traceback.format_exc())
-            return redirect(url('changelog_home', repo_name=org_repo,))
+            return redirect(url('pullrequest_home', repo_name=repo_name))
 
         return redirect(url('pullrequest_show', repo_name=other_repo,
                             pull_request_id=pull_request.pull_request_id))
         """
 
         org_repo = pull_request.org_repo
-        org_ref_type, org_ref_, org_ref = pull_request.org_ref.split(':')
+        (org_ref_type,
+         org_ref_name,
+         org_ref_rev) = pull_request.org_ref.split(':')
+
         other_repo = pull_request.other_repo
-        other_ref_type, other_ref, other_ref_ = pull_request.other_ref.split(':')
+        (other_ref_type,
+         other_ref_name,
+         other_ref_rev) = pull_request.other_ref.split(':')
 
-        org_ref = (org_ref_type, org_ref)
-        other_ref = (other_ref_type, other_ref)
+        # dispite opening revisions for bookmarks/branches/tags, we always
+        # convert this to rev to prevent changes after book or branch change
+        org_ref = ('rev', org_ref_rev)
+        other_ref = ('rev', other_ref_rev)
 
         c.org_repo = org_repo
         c.other_repo = other_repo

rhodecode/model/forms.py

         email = All(v.UniqSystemEmail(), v.Email)
 
     return _UserExtraEmailForm
+
+
+def PullRequestForm():
+    class _PullRequestForm(formencode.Schema):
+        allow_extra_fields = True
+        filter_extra_fields = True
+
+        user = v.UnicodeString(strip=True, required=True)
+        org_repo = v.UnicodeString(strip=True, required=True)
+        org_ref = v.UnicodeString(strip=True, required=True)
+        other_repo = v.UnicodeString(strip=True, required=True)
+        other_ref = v.UnicodeString(strip=True, required=True)
+        revisions = v.Set(required=True)
+        review_members = v.Set(required=True)
+
+        pullrequest_title = v.UnicodeString(strip=True, required=True, min=3)
+        pullrequest_desc = v.UnicodeString(strip=True, required=False)
+
+    return _PullRequestForm

rhodecode/model/validators.py

 from webhelpers.pylonslib.secure_form import authentication_token
 
 from formencode.validators import (
-    UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set
+    UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set,
 )
-
 from rhodecode.lib.utils import repo_name_slug
 from rhodecode.model.db import RepoGroup, Repository, UsersGroup, User
 from rhodecode.lib.exceptions import LdapImportError