summaryrefslogtreecommitdiffstats
path: root/Documentation/rest-api.txt
blob: 2f9d03ff3d3075a7a05a0e06550389a50e0433f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
Gerrit Code Review - REST API
=============================

Gerrit Code Review comes with a REST like API available over HTTP.
The API is suitable for automated tools to build upon, as well as
supporting some ad-hoc scripting use cases.

Protocol Details
----------------

[[authentication]]
Authentication
~~~~~~~~~~~~~~
By default all REST endpoints assume anonymous access and filter
results to correspond to what anonymous users can read (which may
be nothing at all).

Users (and programs) may authenticate using HTTP authentication by
supplying the HTTP password from the user's account settings page.
Gerrit by default uses HTTP digest authentication. To authenticate,
prefix the endpoint URL with `/a/`. For example to authenticate to
`/projects/` request URL `/a/projects/`.

[[output]]
Output Format
~~~~~~~~~~~~~
Most APIs return text format by default. JSON can be requested
by setting the `Accept` HTTP request header to include
`application/json`, for example:

----
  GET /projects/ HTTP/1.0
  Accept: application/json
----

JSON responses are encoded using UTF-8 and use content type
`application/json`. The JSON response body starts with a magic prefix
line that must be stripped before feeding the rest of the response
body to a JSON parser:

----
  )]}'
  [ ... valid JSON ... ]
----

The default JSON format is `JSON_COMPACT`, which skips unnecessary
whitespace. This is not the easiest format for a human to read. Many
examples in this documentation use `format=JSON` as a query parameter
to obtain pretty formatting in the response. Producing (and parsing)
the compact format is more efficient, so most tools should prefer the
default compact format.

Responses will be gzip compressed by the server if the HTTP
`Accept-Encoding` request header is set to `gzip`. This may
save on network transfer time for larger responses.

Endpoints
---------

[[accounts_self_capabilities]]
/accounts/self/capabilities (Account Capabilities)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the global capabilities (such as `createProject` or
`createGroup`) that are enabled for the calling user. This can be used
by UI tools to discover if administrative features are available
to the caller, so they can hide (or show) relevant UI actions.

----
  GET /accounts/self/capabilities?format=JSON HTTP/1.0

  )]}'
  {
    "queryLimit": {
      "min": 0,
      "max": 500
    }
  }
----

Administrator that has authenticated with digest authentication:
----
  GET /a/accounts/self/capabilities?format=JSON HTTP/1.0
  Authorization: Digest username="admin", realm="Gerrit Code Review", nonce="...

  )]}'
  {
    "administrateServer": true,
    "queryLimit": {
      "min": 0,
      "max": 500
    },
    "createAccount": true,
    "createGroup": true,
    "createProject": true,
    "killTask": true,
    "viewCaches": true,
    "flushCaches": true,
    "viewConnections": true,
    "viewQueue": true,
    "startReplication": true
  }
----

To filter the set of global capabilities the `q` parameter can be used.
Filtering may decrease the response time by avoiding looking at every
possible alternative for the caller.

----
  GET /a/accounts/self/capabilities?format=JSON&q=createAccount&q=createGroup HTTP/1.0
  Authorization: Digest username="admin", realm="Gerrit Code Review", nonce="...

  )]}'
  {
    "createAccount": true,
    "createGroup": true
  }
----

Most results are boolean, and a field is only present when its value
is `true`. link:json.html#queryLimit[`queryLimit`] is a range and is
presented as a nested JSON object with `min` and `max` members.

[[projects]]
/projects/ (List Projects)
~~~~~~~~~~~~~~~~~~~~~~~~~~
Lists the projects accessible by the caller. This is the same as
using the link:cmd-ls-projects.html[ls-projects] command over SSH,
and accepts the same options as query parameters.

----
  GET /projects/?format=JSON&d HTTP/1.0

  HTTP/1.1 200 OK
  Content-Disposition: attachment
  Content-Type: application/json;charset=UTF-8
   
  )]}'
  {
    "external/bison": {
      "description": "GNU parser generator"
    },
    "external/gcc": {},
    "external/openssl": {
      "description": "encryption\ncrypto routines"
    },
    "test": {
      "description": "\u003chtml\u003e is escaped"
    }
  }
----

[[suggest-projects]]
The `/projects/` URL also accepts a prefix string as part of the URL.
This limits the results to those projects that start with the specified
prefix.
List all projects that start with `platform/`:
----
GET /projects/platform/?format=JSON HTTP/1.0
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json;charset=UTF-8
)]}'
{
"platform/drivers": {},
"platform/tools": {}
}
----
E.g. this feature can be used by suggestion client UI's to limit results.

[[changes]]
/changes/ (Query Changes)
~~~~~~~~~~~~~~~~~~~~~~~~~
Queries changes visible to the caller. The query string must be
provided by the `q` parameter. The `n` parameter can be used to limit
the returned results.

Query for open changes of watched projects:
----
  GET /changes/?format=JSON&q=status:open+is:watched&n=2 HTTP/1.0

  HTTP/1.1 200 OK
  Content-Disposition: attachment
  Content-Type: application/json;charset=UTF-8

  )]}'
  {
    "project": "demo",
    "branch": "master",
    "id": "Idaf5e098d70898b7119f6f4af5a6c13343d64b57",
    "subject": "One change",
    "status": "NEW",
    "created": "2012-07-17 07:18:30.854000000",
    "updated": "2012-07-17 07:19:27.766000000",
    "reviewed": true,
    "_sortkey": "001e7057000006dc",
    "_number": 1756,
    "owner": {
      "name": "John Doe"
    },
  },
  {
    "project": "demo",
    "branch": "master",
    "id": "I09c8041b5867d5b33170316e2abc34b79bbb8501",
    "subject": "Another change",
    "status": "NEW",
    "created": "2012-07-17 07:18:30.884000000",
    "updated": "2012-07-17 07:18:30.885000000",
    "_sortkey": "001e7056000006dd",
    "_number": 1757,
    "owner": {
      "name": "John Doe"
    },
    "_more_changes": true
  }
----

The change output is sorted by the last update time, most recently
updated to oldest update.

If the `n` query parameter is supplied and additional changes exist
that match the query beyond the end, the last change object has a
`_more_changes: true` JSON field set. Callers can resume a query with
the `n` query parameter, supplying the last change's `_sortkey` field
as the value. When going in the reverse direction with the `p` query
parameter a `_more_changes: true` is put in the first change object if
there are results *before* the first change returned.

Clients are allowed to specify more than one query by setting the `q`
parameter multiple times. In this case the result is an array of
arrays, one per query in the same order the queries were given in.

Query that retrieves changes for a user's dashboard:
----
  GET /changes/?format=JSON&q=is:open+owner:self&q=is:open+reviewer:self+-owner:self&q=is:closed+owner:self+limit:5&o=LABELS HTTP/1.0

  HTTP/1.1 200 OK
  Content-Disposition: attachment
  Content-Type: application/json;charset=UTF-8

  )]}'
  [
    [
      {
        "project": "demo",
        "branch": "master",
        "id": "Idaf5e098d70898b7119f6f4af5a6c13343d64b57",
        "subject": "One change",
        "status": "NEW",
        "created": "2012-07-17 07:18:30.854000000",
        "updated": "2012-07-17 07:19:27.766000000",
        "reviewed": true,
        "_sortkey": "001e7057000006dc",
        "_number": 1756,
        "owner": {
          "name": "John Doe"
        },
        "labels": {
          "Verified": {},
          "Code-Review": {}
        }
      }
    ],
    [],
    []
  ]
----

Additional fields can be obtained by adding `o` parameters, each
option requires more database lookups and slows down the query
response time to the client so they are generally disabled by
default. Optional fields are:

* `LABELS`: a summary of each label required for submit, and
  approvers that have granted (or rejected) with that label.

* `CURRENT_REVISION`: describe the current revision (patch set)
  of the change, including the commit SHA-1 and URLs to fetch from.

* `ALL_REVISIONS`: describe all revisions, not just current.

* `CURRENT_COMMIT`: parse and output all header fields from the
  commit object, including message. Only valid when the current
  revision or all revisions are selected.

* `ALL_COMMITS`: parse and output all header fields from the
  output revisions. If only `CURRENT_REVISION` was requested
  then only the current revision's commit data will be output.

* `CURRENT_FILES`: list files modified by the commit, including
  basic line counts inserted/deleted per file. Only valid when
  the current revision or all revisions are selected.

* `ALL_FILES`: list files modified by the commit, including
  basic line counts inserted/deleted per file. If only the
  `CURRENT_REVISION` was requested the only that commit's
  modified files will be output.

----
  GET /changes/?q=97&format=JSON&o=CURRENT_REVISION&o=CURRENT_COMMIT&o=CURRENT_FILES HTTP/1.0

  HTTP/1.1 200 OK
  Content-Disposition: attachment
  Content-Type: application/json;charset=UTF-8

  )]}'
  [
    {
      "project": "gerrit",
      "branch": "master",
      "id": "I7ea46d2e2ee5c64c0d807677859cfb7d90b8966a",
      "subject": "Use an EventBus to manage star icons",
      "status": "NEW",
      "created": "2012-04-25 00:52:25.580000000",
      "updated": "2012-04-25 00:52:25.586000000",
      "_sortkey": "001c9bf400000061",
      "_number": 97,
      "owner": {
        "name": "Shawn Pearce"
      },
      "current_revision": "184ebe53805e102605d11f6b143486d15c23a09c",
      "revisions": {
        "184ebe53805e102605d11f6b143486d15c23a09c": {
          "_number": 1,
          "fetch": {
            "git": {
              "url": "git://localhost/gerrit",
              "ref": "refs/changes/97/97/1"
            },
            "http": {
              "url": "http://127.0.0.1:8080/gerrit",
              "ref": "refs/changes/97/97/1"
            }
          },
          "commit": {
            "parents": [
              {
                "commit": "1eee2c9d8f352483781e772f35dc586a69ff5646",
                "subject": "Migrate contributor agreements to All-Projects."
              }
            ],
            "author": {
              "name": "Shawn O. Pearce",
              "email": "sop@google.com",
              "date": "2012-04-24 18:08:08.000000000",
              "tz": -420
            },
            "committer": {
              "name": "Shawn O. Pearce",
              "email": "sop@google.com",
              "date": "2012-04-24 18:08:08.000000000",
              "tz": -420
            },
            "subject": "Use an EventBus to manage star icons",
            "message": "Use an EventBus to manage star icons\n\nImage widgets that need to ..."
          },
          "files": {
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeCache.java": {
              "lines_deleted": 8
            },
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeDetailCache.java": {
              "lines_inserted": 1
            },
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeScreen.java": {
              "lines_inserted": 11,
              "lines_deleted": 19
            },
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/ChangeTable.java": {
              "lines_inserted": 23,
              "lines_deleted": 20
            },
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/StarCache.java": {
              "status": "D",
              "lines_deleted": 139
            },
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/changes/StarredChanges.java": {
              "status": "A",
              "lines_inserted": 204
            },
            "gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/Screen.java": {
              "lines_deleted": 9
            }
          }
        }
      }
    }
  ]
----


GERRIT
------
Part of link:index.html[Gerrit Code Review]