How can you post diff on ReviewBoard through your API?

I am trying to publish diff on ReviewBoard through their API. I managed to log in to the server and create a new message, but I could not correctly place the contents of the diff file.

I am new to writing such an application, but my goal is to take one step of the script:

  • diff file (pre-commit) with svn repository,
  • add a view request on ReviewBoard and post the diff from the current file,

Perhaps a later script might be part of the svn pre-commit binding.

My python attempt looks like this:

import urllib.request import urllib.parse import os.path ... login to the reviewboard server with urllib.request.HTTPBasicAuthHandler ... diff_path = '/path/to/file' diff_name = 'my.diff' diff_path = os.path.join(diff_path, diff_name) diff_val = open(diff_path,'r') # load the diff into the http data POST request diff_header = \ '-- SoMe BoUnDaRy \n' \ + 'Content-Disposition: form-data; name=path; filename=' \ + '"' + diff_name + '"\n\n' \ + diff_val.read() + '\n' \ + '-- SoMe BoUnDaRy --' data ={'path': diff_header, 'basedir': '/path/to/file/in/rep'} print( data['path'] ) data = urllib.parse.urlencode(data) data = data.encode('utf-8') opener.open( \ 'http://xxx.xxx.x.xxx/api/review-requests/26/diffs/', data) 

Using this code, I get a BAD REQUEST (400) error, in particular: "One or more fields had errors" (105).

I know that there are some libraries that can communicate with the ReviewBoard API. I also know that a post review exists. I would prefer not to distribute another python library to other developers, and the post-review seems less flexible when comparing files from multiple locations.

From the suggestion below, I will add a server response:

 CREATING PASSWD MANAGER... CREATING PASSWD MANAGER... done CREATING PASSWD HANDLER... CREATING PASSWD HANDLER... done CREATING URL OPENER... CREATING URL OPENER... done LOADING DIFF... send: b'POST /api/review-requests/26/diffs/ HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 723\r\nHost: xxx.xxx.x.xxx\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nUser-Agent: [empty no username+password] Python-urllib/3.2\r\n\r\ npath=--+SoMe+BoUnDaRy+++%...[the rest of my post] reply: 'HTTP/1.1 401 UNAUTHORIZED\r\n' header: Date header: Server header: Content-Language header: Expires header: Vary header: Cache-Control header: WWW-Authenticate header: Content-Length header: Last-Modified header: Connection header: Content-Type send: b'POST /api/review-requests/26/diffs/ HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 723\r\nHost: xxx.xxx.x.xxx\r\nUser-Agent: Python-urllib/3.2\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nAuthorization: Basic [with username+password]\r\n\r\npath= --+SoMe+BoUnDaRy+++%0AContent-Disposition%... reply: 'HTTP/1.1 400 BAD REQUEST\r\n' header: Date header: Server header: Content-Language header: Expires header: Vary header: Cache-Control header: Set-Cookie header: Content-Length header: Last-Modified header: Connection header: Content-Type HTTPError thrown 

At first glance, my hunch is that something is happening with my password handler. I'm not sure what is happening to him. Just in case, here's how I can generate my authentication:

 manager_passwd = urllib.request.HTTPPasswordMgr() manager_passwd.add_password(...) handler_passwd = urllib.request.HTTPBasicAuthHandler(manager_passwd) opener = urllib.request.build_opener(handler_passwd) 

Authentication seems to work. I tested it by creating a new review post. Therefore, when I send diff, authentication fails.

+6
source share
1 answer

The review already has a python tool for publishing diff with their API, it is called postreview.py. You can find it at:

http://reviewboard.googlecode.com/svn/trunk/wxpostreview/postreview.py

Grab and use their ReviewBoardServer to login and send diff!

(In addition, your request requires yes authentication, but also a cookie. For this you need 2 requests (one to enter and receive a cookie, another to send diff).

+2
source

Source: https://habr.com/ru/post/901858/


All Articles