README.md
1# go-gerrit
2
3[![GoDoc](https://godoc.org/github.com/andygrunwald/go-gerrit?status.svg)](https://godoc.org/github.com/andygrunwald/go-gerrit)
4[![Build Status](https://travis-ci.org/andygrunwald/go-gerrit.svg?branch=master)](https://travis-ci.org/andygrunwald/go-gerrit)
5[![Go Report Card](https://goreportcard.com/badge/github.com/andygrunwald/go-gerrit)](https://goreportcard.com/report/github.com/andygrunwald/go-gerrit)
6[![codecov](https://codecov.io/gh/andygrunwald/go-gerrit/branch/master/graph/badge.svg)](https://codecov.io/gh/andygrunwald/go-gerrit)
7
8go-gerrit is a [Go(lang)](https://golang.org/) client library for accessing the [Gerrit Code Review](https://www.gerritcodereview.com/) API.
9
10![go-gerrit - Go(lang) client/library for Gerrit Code Review](./img/logo.png "go-gerrit - Go(lang) client/library for Gerrit Code Review")
11
12## Features
13
14* [Authentication](https://godoc.org/github.com/andygrunwald/go-gerrit#AuthenticationService) (HTTP Basic, HTTP Digest, HTTP Cookie)
15* Every API Endpoint like Gerrit
16 * [/access/](https://godoc.org/github.com/andygrunwald/go-gerrit#AccessService)
17 * [/accounts/](https://godoc.org/github.com/andygrunwald/go-gerrit#AccountsService)
18 * [/changes/](https://godoc.org/github.com/andygrunwald/go-gerrit#ChangesService)
19 * [/config/](https://godoc.org/github.com/andygrunwald/go-gerrit#ConfigService)
20 * [/groups/](https://godoc.org/github.com/andygrunwald/go-gerrit#GroupsService)
21 * [/plugins/](https://godoc.org/github.com/andygrunwald/go-gerrit#PluginsService)
22 * [/projects/](https://godoc.org/github.com/andygrunwald/go-gerrit#ProjectsService)
23* Supports optional plugin APIs such as
24 * events-log - [About](https://gerrit.googlesource.com/plugins/events-log/+/master/src/main/resources/Documentation/about.md), [REST API](https://gerrit.googlesource.com/plugins/events-log/+/master/src/main/resources/Documentation/rest-api-events.md)
25
26
27## Installation
28
29go-gerrit requires Go version 1.10 or greater.
30
31It is go gettable ...
32
33```sh
34$ go get github.com/andygrunwald/go-gerrit
35```
36
37... (optional) to run checks and tests:
38
39**Tests Only**
40
41```sh
42$ cd $GOPATH/src/github.com/andygrunwald/go-gerrit
43$ go test -v
44```
45
46**Checks, Tests, Linters, etc**
47
48```sh
49$ cd $GOPATH/src/github.com/andygrunwald/go-gerrit
50$ make
51```
52
53## API / Usage
54
55Please have a look at the [GoDoc documentation](https://godoc.org/github.com/andygrunwald/go-gerrit) for a detailed API description.
56
57The [Gerrit Code Review - REST API](https://gerrit-review.googlesource.com/Documentation/rest-api.html) was the base document.
58
59### Authentication
60
61Gerrit support multiple ways for [authentication](https://gerrit-review.googlesource.com/Documentation/rest-api.html#authentication).
62
63#### HTTP Basic
64
65Some Gerrit instances (like [TYPO3](https://review.typo3.org/)) has [auth.gitBasicAuth](https://gerrit-review.googlesource.com/Documentation/config-gerrit.html#auth.gitBasicAuth) activated.
66With this you can authenticate with HTTP Basic like this:
67
68```go
69instance := "https://review.typo3.org/"
70client, _ := gerrit.NewClient(instance, nil)
71client.Authentication.SetBasicAuth("andy.grunwald", "my secrect password")
72
73self, _, _ := client.Accounts.GetAccount("self")
74
75fmt.Printf("Username: %s", self.Name)
76
77// Username: Andy Grunwald
78```
79
80If you get an `401 Unauthorized`, check your Account Settings and have a look at the `HTTP Password` configuration.
81
82#### HTTP Digest
83
84Some Gerrit instances (like [Wikimedia](https://gerrit.wikimedia.org/)) has [Digest access authentication](https://en.wikipedia.org/wiki/Digest_access_authentication) activated.
85
86```go
87instance := "https://gerrit.wikimedia.org/r/"
88client, _ := gerrit.NewClient(instance, nil)
89client.Authentication.SetDigestAuth("andy.grunwald", "my secrect http password")
90
91self, resp, err := client.Accounts.GetAccount("self")
92
93fmt.Printf("Username: %s", self.Name)
94
95// Username: Andy Grunwald
96```
97
98If digest auth is not supported by the choosen Gerrit instance, an error like `WWW-Authenticate header type is not Digest` is thrown.
99
100If you get an `401 Unauthorized`, check your Account Settings and have a look at the `HTTP Password` configuration.
101
102#### HTTP Cookie
103
104Some Gerrit instances hosted like the one hosted googlesource.com (e.g. [Go(lang)](https://go-review.googlesource.com/), [Android](https://android-review.googlesource.com/) or [Gerrit](https://gerrit-review.googlesource.com/)) support HTTP Cookie authentication.
105
106You need the cookie name and the cookie value.
107You can get them by click on "Settings > HTTP Password > Obtain Password" in your Gerrit instance.
108
109There you can receive your values.
110The cookie name will be (mostly) `o` (if hosted on googlesource.com).
111Your cookie secret will be something like `git-your@email.com=SomeHash...`.
112
113```go
114instance := "https://gerrit-review.googlesource.com/"
115client, _ := gerrit.NewClient(instance, nil)
116client.Authentication.SetCookieAuth("o", "my-cookie-secret")
117
118self, _, _ := client.Accounts.GetAccount("self")
119
120fmt.Printf("Username: %s", self.Name)
121
122// Username: Andy G.
123```
124
125### More more more
126
127In the examples chapter below you will find a few more examples.
128If you miss one or got a question how to do something please [open a new issue](https://github.com/andygrunwald/go-gerrit/issues/new) with your question.
129We will be happy to answer them.
130
131## Examples
132
133Further a few examples how the API can be used.
134A few more examples are available in the [GoDoc examples section](https://godoc.org/github.com/andygrunwald/go-gerrit#pkg-examples).
135
136### Get version of Gerrit instance
137
138Receive the version of the [Gerrit instance used by the Gerrit team](https://gerrit-review.googlesource.com/) for development:
139
140```go
141package main
142
143import (
144 "fmt"
145 "github.com/andygrunwald/go-gerrit"
146)
147
148func main() {
149 instance := "https://gerrit-review.googlesource.com/"
150 client, err := gerrit.NewClient(instance, nil)
151 if err != nil {
152 panic(err)
153 }
154
155 v, _, err := client.Config.GetVersion()
156
157 fmt.Printf("Version: %s", v)
158
159 // Version: 2.12.2-2512-g0b1bccd
160}
161```
162
163### Get all public projects
164
165List all projects from [Chromium](https://chromium-review.googlesource.com/):
166
167```go
168package main
169
170import (
171 "fmt"
172 "github.com/andygrunwald/go-gerrit"
173)
174
175func main() {
176 instance := "https://chromium-review.googlesource.com/"
177 client, err := gerrit.NewClient(instance, nil)
178 if err != nil {
179 panic(err)
180 }
181
182 opt := &gerrit.ProjectOptions{
183 Description: true,
184 }
185 projects, _, err := client.Projects.ListProjects(opt)
186 for name, p := range *projects {
187 fmt.Printf("%s - State: %s\n", name, p.State)
188 }
189
190 // chromiumos/platform/depthcharge - State: ACTIVE
191 // external/github.com/maruel/subcommands - State: ACTIVE
192 // external/junit - State: ACTIVE
193 // ...
194}
195```
196
197### Query changes
198
199Get some changes of the [kernel/common project](https://android-review.googlesource.com/#/q/project:kernel/common) from the [Android](http://source.android.com/) [Gerrit Review System](https://android-review.googlesource.com/).
200
201```go
202package main
203
204import (
205 "fmt"
206 "github.com/andygrunwald/go-gerrit"
207)
208
209func main() {
210 instance := "https://android-review.googlesource.com/"
211 client, err := gerrit.NewClient(instance, nil)
212 if err != nil {
213 panic(err)
214 }
215
216 opt := &gerrit.QueryChangeOptions{}
217 opt.Query = []string{"project:kernel/common"}
218 opt.AdditionalFields = []string{"LABELS"}
219 changes, _, err := client.Changes.QueryChanges(opt)
220
221 for _, change := range *changes {
222 fmt.Printf("Project: %s -> %s -> %s%d\n", change.Project, change.Subject, instance, change.Number)
223 }
224
225 // Project: kernel/common -> android: binder: Fix BR_ERROR usage and change LSM denials to use it. -> https://android-review.googlesource.com/150839
226 // Project: kernel/common -> android: binder: fix duplicate error return. -> https://android-review.googlesource.com/155031
227 // Project: kernel/common -> dm-verity: Add modes and emit uevent on corrupted blocks -> https://android-review.googlesource.com/169572
228 // ...
229}
230```
231
232## FAQ
233
234### How is the source code organized?
235
236The source code organisation was inspired by [go-github by Google](https://github.com/google/go-github).
237
238Every REST API Endpoint (e.g. [/access/](https://gerrit-review.googlesource.com/Documentation/rest-api-access.html) or [/changes/](https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html)) is coupled in a service (e.g. [AccessService in access.go](./access.go) or [ChangesService in changes.go](./changes.go)).
239Every service is part of [gerrit.Client](./gerrit.go) as a member variable.
240
241gerrit.Client can provide basic helper functions to avoid unnecessary code duplications such as building a new request, parse responses and so on.
242
243Based on this structure implementing a new API functionality is straight forwarded. Here is an example of *ChangeService.DeleteTopic* / [DELETE /changes/{change-id}/topic](https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-topic):
244
245```go
246func (s *ChangesService) DeleteTopic(changeID string) (*Response, error) {
247 u := fmt.Sprintf("changes/%s/topic", changeID)
248 return s.client.DeleteRequest(u, nil)
249}
250```
251
252### What about the version compatibility with Gerrit?
253
254The library was implemented based on the REST API of Gerrit version 2.11.3-1230-gb8336f1 and tested against this version.
255
256This library might be working with older versions as well.
257If you notice an incompatibility [open a new issue](https://github.com/andygrunwald/go-gerrit/issues/new) or try to fix it.
258We welcome contribution!
259
260
261### What about adding code to support the REST API of an optional plugin?
262
263It will depend on the plugin, you are welcome to [open a new issue](https://github.com/andygrunwald/go-gerrit/issues/new) first to propose the idea if you wish.
264As an example the addition of support for events-log plugin was supported because the plugin itself is fairly
265popular and the structures that the REST API uses could also be used by `gerrit stream-events`.
266
267
268## License
269
270This project is released under the terms of the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
271