Using REST APIs
Asked Answered
G

3

7

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.??

Gravid answered 4/2, 2014 at 5:48 Comment(1)
I think you need to enable API access in Gerrit for the user.Carminecarmita
F
9

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.

Florrie answered 4/2, 2014 at 14:7 Comment(1)
Worked for me, thanks! Had no idea that server/login form takes different credentials than the REST API's HTTP digest.Solid
A
0

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

I think it has some APIs to easily access gerrit.

Allnight answered 22/7, 2016 at 20:58 Comment(0)
C
0

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) 
Claudelle answered 20/12, 2018 at 1:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.