2011-11-18 41 views
6

我一直在努力通過API向ReviewBoard發佈差異。我設法登錄到服務器並創建一個新帖子,但是我沒有正確發佈diff文件的內容。你如何通過API向ReviewBoard發佈差異?

我是新來編寫這種應用,但我的目標是有一個一步到位的腳本:

  1. DIFF文件(預提交)與svn庫,
  2. 加ReviewBoard的審閱請求,並從當前文件中發佈差異

稍後,腳本可以是svn預提交鉤子的一部分。

我的Python嘗試看起來像:

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) 

有了這個代碼,我得到一個錯誤的請求(400)的錯誤,特別是: 「一個或多個字段有錯誤」(105)。

我知道有一些庫可以與ReviewBoard API交談。我也知道存在後期審查。我寧願不必向其他開發人員分發另一個Python庫,並且在從多個位置區分文件時,後審查似乎不太靈活。

從下面的建議,我在這裏添加的服務器響應:

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 

乍一看我的猜測是,事情是發生在我的密碼處理程序。我不確定發生了什麼事。以防萬一,這是我如何生成我的身份驗證:

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

身份驗證似乎工作。我已通過創建新的評論文章對其進行了測試。所以這是當我發佈認證失敗的差異。

+0

海報似乎是一個很好的選擇,但它是一個外部庫:[poster_doc](http://atlee.ca/software/poster/) – KlingonJoe

+0

這裏是一個類似問題的鏈接: [using multipartposthandler to post form數據與蟒蛇](http://stackoverflow.com/questions/680305/using-multipartposthandler-to-post-form-data-with-python) – KlingonJoe

+0

設置[httplib調試級別](http://stackoverflow.com/ questions/789856 /)to 1,這樣你就可以看到正在發送的頭文件,如果你沒有看到問題,請添加信息在你的問題上。 –

回答

2

Reviewboard已經有一個python工具用於發佈diff與他們的API,它被稱爲postreview.py。你可以發現:

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

抓住並利用他們的ReviewBoardServer進行登錄和發佈DIFF!

(另外,在您的要求,認證要求是,也有Cookie文件。這就是爲什麼你需要2個請求(一個用於登錄並獲得餅乾,另一個用於發送差異。))

+0

謝謝,ReviewBoardServer是一個很好的建議。我現在不必嗅探所有的後期審查。 – KlingonJoe