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

1
  • 1
    I think you need to enable API access in Gerrit for the user. Commented Sep 1, 2015 at 11:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me, thanks! Had no idea that server/login form takes different credentials than the REST API's HTTP digest.
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.

Comments

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) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.