Changeset - 4fbbc65e8cd5
[Not reviewed]
beta
0 5 0
Marcin Kuzminski - 13 years ago 2012-07-30 23:29:03
marcin@python-works.com
Forbid changing changset status when it is associated with a closed pull request
- fixed some issues with cascade deleting repos with changeset statuses attached
5 files changed with 36 insertions and 6 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/changeset.py
Show inline comments
 
@@ -49,6 +49,7 @@ from rhodecode.model.changeset_status im
 
from rhodecode.model.meta import Session
 
from rhodecode.lib.diffs import wrapped_diff
 
from rhodecode.model.repo import RepoModel
 
from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError
 

	
 
log = logging.getLogger(__name__)
 

	
 
@@ -388,18 +389,31 @@ class ChangesetController(BaseRepoContro
 

	
 
        # get status if set !
 
        if status and change_status:
 
            # if latest status was from pull request and it's closed
 
            # disallow changing status ! 
 
            # dont_allow_on_closed_pull_request = True !
 

	
 
            try:
 
            ChangesetStatusModel().set_status(
 
                c.rhodecode_db_repo.repo_id,
 
                status,
 
                c.rhodecode_user.user_id,
 
                comm,
 
                revision=revision,
 
                    dont_allow_on_closed_pull_request=True
 
            )
 
            except StatusChangeOnClosedPullRequestError:
 
                log.error(traceback.format_exc())
 
                msg = _('Changing status on a changeset associated with'
 
                        'a closed pull request is not allowed')
 
                h.flash(msg, category='warning')
 
                return redirect(h.url('changeset_home', repo_name=repo_name,
 
                                      revision=revision))
 
        action_logger(self.rhodecode_user,
 
                      'user_commented_revision:%s' % revision,
 
                      c.rhodecode_db_repo, self.ip_addr, self.sa)
 

	
 
        Session.commit()
 
        Session().commit()
 

	
 
        if not request.environ.get('HTTP_X_PARTIAL_XHR'):
 
            return redirect(h.url('changeset_home', repo_name=repo_name,
 
@@ -422,7 +436,7 @@ class ChangesetController(BaseRepoContro
 
        owner = lambda: co.author.user_id == c.rhodecode_user.user_id
 
        if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner:
 
            ChangesetCommentsModel().delete(comment=co)
 
            Session.commit()
 
            Session().commit()
 
            return True
 
        else:
 
            raise HTTPForbidden()
rhodecode/lib/exceptions.py
Show inline comments
 
@@ -50,3 +50,7 @@ class UserOwnsReposException(Exception):
 

	
 
class UsersGroupsAssignedException(Exception):
 
    pass
 

	
 

	
 
class StatusChangeOnClosedPullRequestError(Exception):
 
    pass
 
\ No newline at end of file
rhodecode/model/changeset_status.py
Show inline comments
 
@@ -28,6 +28,7 @@ from collections import  defaultdict
 

	
 
from rhodecode.model import BaseModel
 
from rhodecode.model.db import ChangesetStatus, PullRequest
 
from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError
 

	
 
log = logging.getLogger(__name__)
 

	
 
@@ -111,7 +112,7 @@ class ChangesetStatusModel(BaseModel):
 
        return str(st)
 

	
 
    def set_status(self, repo, status, user, comment, revision=None,
 
                   pull_request=None):
 
                   pull_request=None, dont_allow_on_closed_pull_request=False):
 
        """
 
        Creates new status for changeset or updates the old ones bumping their
 
        version, leaving the current status at
 
@@ -126,6 +127,9 @@ class ChangesetStatusModel(BaseModel):
 
        :type user:
 
        :param comment:
 
        :type comment:
 
        :param dont_allow_on_closed_pull_request: don't allow a status change
 
            if last status was for pull request and it's closed. We shouldn't
 
            mess around this manually
 
        """
 
        repo = self._get_repo(repo)
 

	
 
@@ -140,6 +144,14 @@ class ChangesetStatusModel(BaseModel):
 
            q = q.filter(ChangesetStatus.pull_request == pull_request)
 
        cur_statuses = q.all()
 

	
 
        #if statuses exists and last is associated with a closed pull request
 
        # we need to check if we can allow this status change
 
        if (dont_allow_on_closed_pull_request and cur_statuses 
 
            and cur_statuses[0].pull_request.status == PullRequest.STATUS_CLOSED):
 
            raise StatusChangeOnClosedPullRequestError(
 
                'Changing status on closed pull request is not allowed'
 
            )
 

	
 
        if cur_statuses:
 
            for st in cur_statuses:
 
                st.version += 1
rhodecode/model/db.py
Show inline comments
 
@@ -1449,7 +1449,7 @@ class ChangesetComment(Base, BaseModel):
 

	
 
    author = relationship('User', lazy='joined')
 
    repo = relationship('Repository')
 
    status_change = relationship('ChangesetStatus', uselist=False)
 
    status_change = relationship('ChangesetStatus', cascade="all, delete, delete-orphan")
 
    pull_request = relationship('PullRequest', lazy='joined')
 

	
 
    @classmethod
rhodecode/templates/changeset/changeset_file_comment.html
Show inline comments
 
@@ -17,8 +17,8 @@
 
        %if co.status_change:
 
           <div  style="float:left" class="changeset-status-container">
 
             <div style="float:left;padding:0px 2px 0px 2px"><span style="font-size: 18px;">&rsaquo;</span></div>
 
             <div title="${_('Changeset status')}" class="changeset-status-lbl"> ${co.status_change.status_lbl}</div>
 
             <div class="changeset-status-ico"><img src="${h.url(str('/images/icons/flag_status_%s.png' % co.status_change.status))}" /></div>
 
             <div title="${_('Changeset status')}" class="changeset-status-lbl"> ${co.status_change[0].status_lbl}</div>
 
             <div class="changeset-status-ico"><img src="${h.url(str('/images/icons/flag_status_%s.png' % co.status_change[0].status))}" /></div>
 
           </div>
 
        %endif
 
      %if h.HasPermissionAny('hg.admin', 'repository.admin')() or co.author.user_id == c.rhodecode_user.user_id:
0 comments (0 inline, 0 general)