# HG changeset patch # User Marcin Kuzminski # Date 2019-09-10 13:28:20 # Node ID fde0bece3648f28059cfd94f7cb56dff1e3b1089 # Parent aa0b54b754dade1a66351dc93e9e37582c8d6b6d pull-requests: handle exceptions in state change and improve logging. diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -3772,21 +3772,35 @@ class _SetState(object): self._pr = pull_request self._org_state = back_state or pull_request.pull_request_state self._pr_state = pr_state + self._current_state = None def __enter__(self): log.debug('StateLock: entering set state context, setting state to: `%s`', self._pr_state) - self._pr.pull_request_state = self._pr_state - Session().add(self._pr) - Session().commit() + self.set_pr_state(self._pr_state) + return self def __exit__(self, exc_type, exc_val, exc_tb): + if exc_val is not None: + log.error(traceback.format_exc(exc_tb)) + return None + + self.set_pr_state(self._org_state) log.debug('StateLock: exiting set state context, setting state to: `%s`', self._org_state) - self._pr.pull_request_state = self._org_state - Session().add(self._pr) - Session().commit() - + @property + def state(self): + return self._current_state + + def set_pr_state(self, pr_state): + try: + self._pr.pull_request_state = pr_state + Session().add(self._pr) + Session().commit() + self._current_state = pr_state + except Exception: + log.exception('Failed to set PullRequest %s state to %s', self._pr, pr_state) + raise class _PullRequestBase(BaseModel): """ diff --git a/rhodecode/model/pull_request.py b/rhodecode/model/pull_request.py --- a/rhodecode/model/pull_request.py +++ b/rhodecode/model/pull_request.py @@ -507,10 +507,10 @@ class PullRequestModel(BaseModel): # operation pull_request = PullRequest.get(pull_request.pull_request_id) - # set as merging, for simulation, and if finished to created so we mark + # set as merging, for merge simulation, and if finished to created so we mark # simulation is working fine with pull_request.set_state(PullRequest.STATE_MERGING, - final_state=PullRequest.STATE_CREATED): + final_state=PullRequest.STATE_CREATED) as state_obj: MergeCheck.validate( pull_request, auth_user=auth_user, translator=translator) @@ -1099,6 +1099,8 @@ class PullRequestModel(BaseModel): if not reviewers_ids: return + log.debug('Notify following reviewers about pull-request %s', reviewers_ids) + pull_request_obj = pull_request # get the current participants of this pull request recipients = reviewers_ids