How to use the gerrit REST API

2,166 views
Skip to first unread message

Olivier Paule

unread,
Sep 30, 2014, 5:47:29 AM9/30/14
to repo-d...@googlegroups.com
Hi all,

I hope I'm in the good group to ask my question.

Usually, I use the command "ssh www.myserver.com gerrit query --commit-message --current-patch-set 123456" to retrieve a JSON with data I need.

I would like to retrieve the same data using the REST API of gerrit (on the url https://www.myserver.com/123456) but I'm stuck.

Can someone help me please? :-)

Thanks

David Pursehouse

unread,
Sep 30, 2014, 6:21:43 AM9/30/14
to Olivier Paule, repo-d...@googlegroups.com
To get the same data as in your ssh command you will want to do
something like:

curl -n --digest
"http://www.myserver.com/a/changes/CHANGEID/detail/?o=CURRENT_REVISION&o=CURRENT_COMMIT"

- The HTTP username and password (from the "HTTP password" page on your
user settings) should be in the ~/.netrc file.

- Replace "CHANGEID" with the Change Id in the format
"project~branch~revision". The project name and branch name should be
url encoded.

For further details check the documentation [1].

[1]
https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html

Olivier Paule

unread,
Sep 30, 2014, 8:39:59 AM9/30/14
to repo-d...@googlegroups.com, olivie...@gmail.com
Thanks for your response.

I forgot something. This is for a php page. And I am under Windows (using wampserver). I cannot find where is the  ~/.netrc for my Apache user.

I tried with curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); but that does not work.

Jan Kundrát

unread,
Sep 30, 2014, 9:28:56 AM9/30/14
to repo-d...@googlegroups.com
On Tuesday, 30 September 2014 14:39:59 CEST, Olivier Paule wrote:
> I tried with curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
> but that does not work.

What does "doesn't work" mean?

Are you using a service user, or a regular user? If it's a regular user,
are you using the extra HTTP password to be set through the settings, or
your main password?

Cheers,
Jan

--
Trojitá, a fast Qt IMAP e-mail client -- http://trojita.flaska.net/

David Pursehouse

unread,
Sep 30, 2014, 9:31:03 AM9/30/14
to Olivier Paule, repo-discuss

On 30 Sep 2014 21:40, "Olivier Paule" <olivie...@gmail.com> wrote:
>
> Thanks for your response.
>
> I forgot something. This is for a php page. And I am under Windows (using wampserver). I cannot find where is the  ~/.netrc for my Apache user.
>

This link should help [1].  Scroll down a bit and it explains the environment vars you need to set.  Also note that the name of the file is _netrc on Windows, not .netrc as it is on Ubuntu et al.

[1]
http://stackoverflow.com/questions/6031214/git-how-to-use-netrc-file-on-windows-to-save-user-and-password

> I tried with curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); but that does not work.
>

Please be more specific than "does not work".  Does it fail to authenticate?  Any error message?

It's a long time since I used php so I can't help with that.  All I can suggest is to make sure the request is using digest authentication, or, try it without authentication by removing /a/ from the url.

> Le mardi 30 septembre 2014 12:21:43 UTC+2, David Pursehouse a écrit :
>>
>> On 09/30/2014 06:47 PM, Olivier Paule wrote:
>> > Hi all,
>> >
>> > I hope I'm in the good group to ask my question.
>> >
>> > Usually, I use the command "ssh www.myserver.com gerrit
>> > query --commit-message --current-patch-set 123456" to retrieve a JSON
>> > with data I need.
>> >
>> > I would like to retrieve the same data using the REST API of gerrit (on
>> > the url https://www.myserver.com/123456) but I'm stuck.
>> >
>> > Can someone help me please? :-)
>> >
>>
>> To get the same data as in your ssh command you will want to do
>> something like:
>>
>>    curl -n --digest
>> "http://www.myserver.com/a/changes/CHANGEID/detail/?o=CURRENT_REVISION&o=CURRENT_COMMIT"
>>
>> - The HTTP username and password (from the "HTTP password" page on your
>> user settings) should be in the ~/.netrc file.
>>
>> - Replace "CHANGEID" with the Change Id in the format
>> "project~branch~revision".  The project name and branch name should be
>> url encoded.
>>
>> For further details check the documentation [1].
>>
>> [1]
>> https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html
>

> --
> --
> To unsubscribe, email repo-discuss...@googlegroups.com
> More info at http://groups.google.com/group/repo-discuss?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups "Repo and Gerrit Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to repo-discuss...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Olivier PAULE

unread,
Sep 30, 2014, 9:32:26 AM9/30/14
to Jan Kundrát, repo-d...@googlegroups.com
It means "Unauthorized access".

Wampserver uses a service user "nt authority\system". Creating a ssh key for this user would be great. But for now, I don't know how.

I tried with both my password and the http password.



--
--
To unsubscribe, email repo-discuss+unsubscribe@googlegroups.com
--- You received this message because you are subscribed to a topic in the Google Groups "Repo and Gerrit Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/repo-discuss/PPQz0b3WPnE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to repo-discuss+unsubscribe@googlegroups.com.

Olivier PAULE

unread,
Sep 30, 2014, 9:33:55 AM9/30/14
to David Pursehouse, repo-discuss
I get: "Unauthorized access".

I'll see how to use digest authentication with curl_setopt.

Olivier PAULE

unread,
Sep 30, 2014, 9:40:38 AM9/30/14
to David Pursehouse, repo-discuss
Here is what I did:

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$issue_list = (curl_exec($curl));
var_dump($issue_list);

And I get:

tring '<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing /a/changes/I6297404807fbd1e99a30c28fxxxfda3a5404391/detail/. Reason:
<pre>    Unauthorized</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
Reply all
Reply to author
Forward
0 new messages