Using REST APIs

倖福魔咒の 提交于 2020-01-13 09:06:31

问题


I am new to gerrit. I am using gerrit V. 2.6 . I want to use gerrit REST APIs in my python script. But not able to figure out how to use it. I tried below code but getting errors.

curl --digest --user user:password http://server/a/changes/path/to/project~branch~change_id/rebase

getting error :

401 Authorization Required

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

Am I missing something.??


回答1:


Are you using the correct username:password combination? This isn't your network password - it is the HTTP password that gerrit generates. You can find it by going to Settings->HTTP Password. If the password box is blank, click the button to have Gerrit generate a new password.




回答2:


You may try using pygerrit. https://pypi.python.org/pypi/pygerrit/0.2.1

I think it has some APIs to easily access gerrit.




回答3:


As @Ramraj mentioned, you can try using pygerrit or pygerrit2.

And I provide some examples that how I use gerrit REST APIs in my python script.

Here is the code.

auth = HTTPBasicAuth(username, password) 
rest = GerritRestAPI(url='http://review.xxxxxx.com:8080', auth=auth)

Query changes by change number.

info = rest.get("/changes/?q=change:{}".format(change_number))
change_id = info[0]['change_id']
subject = info[0]['subject']

Query changes by commit id.

info = rest.get("/changes/?q=commit:{}".format(commit_id))
change_id = info[0]['change_id']
subject = info[0]['subject']

Revert a change.

headers = {'content-type': 'application/json'} 
query = "/changes/" + str(change_number) + "/revert" 
my_data = {"message": "{}".format("Revert "+str(subject))} 
rest.post(query, data=json.dumps(my_data), timeout=30, headers=headers)

Review a change

headers = {'content-disposition': 'attachment', 'content-type': 'application/json'} 
query = "/changes/" + str(change_number) + "/revisions/current/review" 
my_data = { "labels": {"Code-Review": "+2", "Verified": "+1"} } 
rest.post(query, data=json.dumps(my_data), timeout=30, headers=headers) 


来源:https://stackoverflow.com/questions/21544870/using-rest-apis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!