Commits

iproetel  committed 4ea07ec

Added state check for existing reviews.

  • Participants
  • Parent commits 4f6fee1

Comments (0)

Files changed (2)

File check-state.patch

+# HG changeset patch
+# Parent f5d518fb6960aa7d14b1c02bd65839ff4555df7c
+Check if the an existing review is pending or if it should be reopened.
+If modification of an existing review was requested but it is not pending and it must not be reopened create a new request. 
+This is helpful for 'qpostreview' to handle alread closed reviews on previously posted patches.
+
+diff -r f5d518fb6960 mercurial_reviewboard/__init__.py
+--- a/mercurial_reviewboard/__init__.py	Tue Jul 05 18:06:34 2011 +0200
++++ b/mercurial_reviewboard/__init__.py	Tue Jul 05 18:10:02 2011 +0200
+@@ -189,12 +189,13 @@
+ def send_review(ui, repo, c, parentc, diff, parentdiff, opts):
+ 
+     fields = createfields(ui, repo, c, parentc, opts)
++    reviewboard = getreviewboard(ui, opts)
+ 
+     request_id = opts['existing']
+-    if request_id:
+-        update_review(request_id, ui, fields, diff, parentdiff, opts)
++    if request_id and check_update(reviewboard, request_id, ui, fields, diff, parentdiff, opts):
++        update_review(reviewboard, request_id, ui, fields, diff, parentdiff, opts)
+     else:
+-        request_id = new_review(ui, fields, diff, parentdiff,
++        request_id = new_review(reviewboard, ui, fields, diff, parentdiff,
+                                    opts)
+ 
+     request_url = '%s/%s/%s/' % (find_server(ui, opts), "r", request_id)
+@@ -262,19 +263,55 @@
+     except ReviewBoardError, msg:
+         raise util.Abort(_(str(msg)))
+ 
++def _ask_reopen(request_id, ui, status, opts):
++    ui.status('''Request %s is not 'pending' but '%s'.\n''' % (request_id, status))
++    while True:
++        resps = _('[Anrh]')
++        choices = (_('&Abort this request'),
++                _('Create &new review request'),
++                _('&Reopen the existing request'),
++                _('&Help'))
++        r = ui.promptchoice("%s %s" % ('what now?', resps), choices)
++        ui.write("\n")
++        if r == 3: # ?
++            for  choice in choices:
++                ui.status(' %s %s\n' % (choice[choice.index('&')+1].lower(),re.sub('\&','',choice)))
++            ui.status('\n')
++            continue
++        elif r == 0: # abort
++            raise util.Abort(_('user quit'))
++        elif r == 1: # new
++            return False
++        elif r == 2: # reopen
++            opts['reopen'] = 'reopen'
++            return True
+ 
+-def update_review(request_id, ui, fields, diff, parentdiff, opts):
+-    reviewboard = getreviewboard(ui, opts)
++def check_update(reviewboard, request_id, ui, fields, diff, parentdiff, opts):
++    ''' Test if request_id is a pending review request or if it should be reopened.
++        Returns True if an update is possible.'''
++    status = reviewboard.get_status(request_id)
++    if status == 'pending' or _ask_reopen(request_id, ui, status, opts):
++        return True
++    else:
++        return False
++
++
++def update_review(reviewboard, request_id, ui, fields, diff, parentdiff, opts, reopen = False):
+     try:
++        status = reviewboard.get_status(request_id)
++        if opts['reopen'] :
++            ui.debug('reopen request %s\n' % request_id)
++            reviewboard.set_status(request_id, 'pending')
++
++        ui.debug('updating request %s\n' % request_id)
+         reviewboard.update_request(request_id, fields, diff, parentdiff)
+         if opts['publish']:
+             reviewboard.publish(request_id)
+     except ReviewBoardError, msg:
+-        raise util.Abort(_(msg))
++        raise util.Abort(_(str(msg)))
+ 
+ 
+-def new_review(ui, fields, diff, parentdiff, opts):
+-    reviewboard = getreviewboard(ui, opts)
++def new_review(reviewboard, ui, fields, diff, parentdiff, opts):
+ 
+     repo_id = find_reviewboard_repo_id(ui, reviewboard, opts)
+ 
+diff -r f5d518fb6960 mercurial_reviewboard/reviewboard.py
+--- a/mercurial_reviewboard/reviewboard.py	Tue Jul 05 18:06:34 2011 +0200
++++ b/mercurial_reviewboard/reviewboard.py	Tue Jul 05 18:10:02 2011 +0200
+@@ -240,7 +240,7 @@
+             code = e.reason[0]
+             msg = "URL Error: " + e.reason[1]
+             raise ReviewBoardError({'err' : {'msg' : msg, 'code' : code}})
+-        
++
+     def _process_json(self, data):
+         """
+         Loads in a JSON file and returns the data if successful. On failure,
+@@ -331,8 +331,8 @@
+             self._pending_user_requests = []
+             for r in rsp['review_requests']:
+                 self._pending_user_requests += [Request(r['id'], r['summary'].strip())]
+-                
+-        return self._pending_user_requests    
++
++        return self._pending_user_requests
+ 
+     def new_request(self, repo_id, fields={}, diff='', parentdiff=''):
+         req = self._create_request(repo_id)
+@@ -342,13 +342,27 @@
+ 
+     def update_request(self, id, fields={}, diff='', parentdiff=''):
+         req = self._get_request(id)
+-        self._set_request_details(req, fields, diff, parentdiff)        
++        self._set_request_details(req, fields, diff, parentdiff)
+ 
+     def publish(self, id):
+         req = self._get_request(id)
+         drafturl = req['links']['draft']['href']
+         self._api_request('PUT', drafturl, {'public':'1'})
+ 
++    def get_status(self, id):
++        req =  self._get_request(id)
++        return req['status']
++
++    def set_status(self, id, status):
++        valids = {'pending' : 'pending', 'submitted' : 'submitted', 'discarded' : 'discarded'}
++        if not valids.has_key(status):
++            raise ReviewBoardError('invalid status: %s' % status)
++
++        fields = {}
++        fields['status'] = status
++        self._api_request('PUT', '/api/review-requests/%s/' % id, fields)
++
++
+     def _create_request(self, repo_id):
+         data = { 'repository': repo_id }
+         result = self._api_request('POST', '/api/review-requests/', data)
+@@ -482,7 +496,7 @@
+     if not username and not password:
+             if httpclient.has_valid_cookie():
+                 return
+-    
++
+     if not username:
+         username = mercurial.ui.ui().prompt('Username: ')
+     if not password:
 abort.patch
 qpostreview.patch
 short-description.patch
+check-state.patch