blob: 9752d14f6afaa70d83865562fcd6eb48535375c1 [file] [log] [blame]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001= Gerrit Code Review - Plugin Development
Deniz Türkoglueb78b602012-05-07 14:02:36 -07002
Edwin Kempinaf275322012-07-16 11:04:01 +02003The Gerrit server functionality can be extended by installing plugins.
4This page describes how plugins for Gerrit can be developed.
5
6Depending on how tightly the extension code is coupled with the Gerrit
7server code, there is a distinction between `plugins` and `extensions`.
8
Edwin Kempinf5a77332012-07-18 11:17:53 +02009[[plugin]]
Edwin Kempin948de0f2012-07-16 10:34:35 +020010A `plugin` in Gerrit is tightly coupled code that runs in the same
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070011JVM as Gerrit. It has full access to all server internals. Plugins
12are tightly coupled to a specific major.minor server version and
13may require source code changes to compile against a different
14server version.
15
Edwin Kempinf5a77332012-07-18 11:17:53 +020016[[extension]]
Edwin Kempin948de0f2012-07-16 10:34:35 +020017An `extension` in Gerrit runs inside of the same JVM as Gerrit
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070018in the same way as a plugin, but has limited visibility to the
Edwin Kempinfd19bfb2012-07-16 10:44:17 +020019server's internals. The limited visibility reduces the extension's
20dependencies, enabling it to be compatible across a wider range
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070021of server versions.
22
23Most of this documentation refers to either type as a plugin.
Deniz Türkoglueb78b602012-05-07 14:02:36 -070024
Edwin Kempinf878c4b2012-07-18 09:34:25 +020025[[getting-started]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080026== Getting started
Deniz Türkoglueb78b602012-05-07 14:02:36 -070027
Edwin Kempinf878c4b2012-07-18 09:34:25 +020028To get started with the development of a plugin there are two
29recommended ways:
Dave Borowitz5cc8f662012-05-21 09:51:36 -070030
Edwin Kempinf878c4b2012-07-18 09:34:25 +020031. use the Gerrit Plugin Maven archetype to create a new plugin project:
32+
33With the Gerrit Plugin Maven archetype you can create a skeleton for a
34plugin project.
35+
36----
37mvn archetype:generate -DarchetypeGroupId=com.google.gerrit \
38 -DarchetypeArtifactId=gerrit-plugin-archetype \
David Pursehouse481ba352017-01-06 09:47:10 +090039 -DarchetypeVersion=2.13.5 \
Edwin Kempin91155c22013-12-02 20:25:18 +010040 -DgroupId=com.googlesource.gerrit.plugins.testplugin \
41 -DartifactId=testplugin
Edwin Kempinf878c4b2012-07-18 09:34:25 +020042----
43+
44Maven will ask for additional properties and then create the plugin in
45the current directory. To change the default property values answer 'n'
46when Maven asks to confirm the properties configuration. It will then
47ask again for all properties including those with predefined default
48values.
49
David Pursehouse2cf0cb52013-08-27 16:09:53 +090050. clone the sample plugin:
Edwin Kempinf878c4b2012-07-18 09:34:25 +020051+
David Pursehouse2cf0cb52013-08-27 16:09:53 +090052This is a project that demonstrates the various features of the
53plugin API. It can be taken as an example to develop an own plugin.
Edwin Kempinf878c4b2012-07-18 09:34:25 +020054+
Dave Borowitz5cc8f662012-05-21 09:51:36 -070055----
David Pursehouse2cf0cb52013-08-27 16:09:53 +090056$ git clone https://gerrit.googlesource.com/plugins/cookbook-plugin
Dave Borowitz5cc8f662012-05-21 09:51:36 -070057----
Edwin Kempinf878c4b2012-07-18 09:34:25 +020058+
59When starting from this example one should take care to adapt the
60`Gerrit-ApiVersion` in the `pom.xml` to the version of Gerrit for which
61the plugin is developed. If the plugin is developed for a released
62Gerrit version (no `SNAPSHOT` version) then the URL for the
63`gerrit-api-repository` in the `pom.xml` needs to be changed to
Shawn Pearced5005002013-06-21 11:01:45 -070064`https://gerrit-api.storage.googleapis.com/release/`.
Dave Borowitz5cc8f662012-05-21 09:51:36 -070065
Edwin Kempinf878c4b2012-07-18 09:34:25 +020066[[API]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080067== API
Edwin Kempinf878c4b2012-07-18 09:34:25 +020068
69There are two different API formats offered against which plugins can
70be developed:
Deniz Türkoglueb78b602012-05-07 14:02:36 -070071
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070072gerrit-extension-api.jar::
73 A stable but thin interface. Suitable for extensions that need
74 to be notified of events, but do not require tight coupling to
75 the internals of Gerrit. Extensions built against this API can
76 expect to be binary compatible across a wide range of server
77 versions.
Deniz Türkoglueb78b602012-05-07 14:02:36 -070078
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070079gerrit-plugin-api.jar::
80 The complete internals of the Gerrit server, permitting a
81 plugin to tightly couple itself and provide additional
82 functionality that is not possible as an extension. Plugins
83 built against this API are expected to break at the source
84 code level between every major.minor Gerrit release. A plugin
85 that compiles against 2.5 will probably need source code level
86 changes to work with 2.6, 2.7, and so on.
Deniz Türkoglueb78b602012-05-07 14:02:36 -070087
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080088== Manifest
Deniz Türkoglueb78b602012-05-07 14:02:36 -070089
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070090Plugins may provide optional description information with standard
91manifest fields:
Nasser Grainawie033b262012-05-09 17:54:21 -070092
Michael Ochmannb99feab2016-07-06 14:10:22 +020093----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070094 Implementation-Title: Example plugin showing examples
95 Implementation-Version: 1.0
96 Implementation-Vendor: Example, Inc.
Michael Ochmannb99feab2016-07-06 14:10:22 +020097----
Nasser Grainawie033b262012-05-09 17:54:21 -070098
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080099=== ApiType
Nasser Grainawie033b262012-05-09 17:54:21 -0700100
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700101Plugins using the tightly coupled `gerrit-plugin-api.jar` must
102declare this API dependency in the manifest to gain access to server
Edwin Kempin948de0f2012-07-16 10:34:35 +0200103internals. If no `Gerrit-ApiType` is specified the stable `extension`
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700104API will be assumed. This may cause ClassNotFoundExceptions when
105loading a plugin that needs the plugin API.
Nasser Grainawie033b262012-05-09 17:54:21 -0700106
Michael Ochmannb99feab2016-07-06 14:10:22 +0200107----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700108 Gerrit-ApiType: plugin
Michael Ochmannb99feab2016-07-06 14:10:22 +0200109----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700110
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800111=== Explicit Registration
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700112
113Plugins that use explicit Guice registration must name the Guice
114modules in the manifest. Up to three modules can be named in the
Edwin Kempin948de0f2012-07-16 10:34:35 +0200115manifest. `Gerrit-Module` supplies bindings to the core server;
116`Gerrit-SshModule` supplies SSH commands to the SSH server (if
117enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700118server (if enabled). If no modules are named automatic registration
119will be performed by scanning all classes in the plugin JAR for
120`@Listen` and `@Export("")` annotations.
121
Michael Ochmannb99feab2016-07-06 14:10:22 +0200122----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700123 Gerrit-Module: tld.example.project.CoreModuleClassName
124 Gerrit-SshModule: tld.example.project.SshModuleClassName
125 Gerrit-HttpModule: tld.example.project.HttpModuleClassName
Michael Ochmannb99feab2016-07-06 14:10:22 +0200126----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700127
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200128[[plugin_name]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800129=== Plugin Name
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200130
David Pursehoused128c892013-10-22 21:52:21 +0900131A plugin can optionally provide its own plugin name.
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200132
Michael Ochmannb99feab2016-07-06 14:10:22 +0200133----
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200134 Gerrit-PluginName: replication
Michael Ochmannb99feab2016-07-06 14:10:22 +0200135----
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200136
137This is useful for plugins that contribute plugin-owned capabilities that
138are stored in the `project.config` file. Another use case is to be able to put
139project specific plugin configuration section in `project.config`. In this
140case it is advantageous to reserve the plugin name to access the configuration
141section in the `project.config` file.
142
143If `Gerrit-PluginName` is omitted, then the plugin's name is determined from
144the plugin file name.
145
146If a plugin provides its own name, then that plugin cannot be deployed
147multiple times under different file names on one Gerrit site.
148
149For Maven driven plugins, the following line must be included in the pom.xml
150file:
151
152[source,xml]
153----
154<manifestEntries>
155 <Gerrit-PluginName>name</Gerrit-PluginName>
156</manifestEntries>
157----
158
159For Buck driven plugins, the following line must be included in the BUCK
160configuration file:
161
162[source,python]
163----
David Pursehouse529ec252013-09-27 13:45:14 +0900164manifest_entries = [
165 'Gerrit-PluginName: name',
166]
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200167----
168
Edwin Kempinc0b1b0e2013-10-01 14:13:54 +0200169A plugin can get its own name injected at runtime:
170
171[source,java]
172----
173public class MyClass {
174
175 private final String pluginName;
176
177 @Inject
178 public MyClass(@PluginName String pluginName) {
179 this.pluginName = pluginName;
180 }
181
David Pursehoused128c892013-10-22 21:52:21 +0900182 [...]
Edwin Kempinc0b1b0e2013-10-01 14:13:54 +0200183}
184----
185
David Pursehouse8ed0d922013-10-18 18:57:56 +0900186A plugin can get its canonical web URL injected at runtime:
187
188[source,java]
189----
190public class MyClass {
191
192 private final String url;
193
194 @Inject
195 public MyClass(@PluginCanonicalWebUrl String url) {
196 this.url = url;
197 }
198
199 [...]
200}
201----
202
203The URL is composed of the server's canonical web URL and the plugin's
204name, i.e. `http://review.example.com:8080/plugin-name`.
205
206The canonical web URL may be injected into any .jar plugin regardless of
207whether or not the plugin provides an HTTP servlet.
208
Edwin Kempinf7295742012-07-16 15:03:46 +0200209[[reload_method]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800210=== Reload Method
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700211
212If a plugin holds an exclusive resource that must be released before
213loading the plugin again (for example listening on a network port or
Edwin Kempin948de0f2012-07-16 10:34:35 +0200214acquiring a file lock) the manifest must declare `Gerrit-ReloadMode`
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700215to be `restart`. Otherwise the preferred method of `reload` will
216be used, as it enables the server to hot-patch an updated plugin
217with no down time.
218
Michael Ochmannb99feab2016-07-06 14:10:22 +0200219----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700220 Gerrit-ReloadMode: restart
Michael Ochmannb99feab2016-07-06 14:10:22 +0200221----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700222
223In either mode ('restart' or 'reload') any plugin or extension can
224be updated without restarting the Gerrit server. The difference is
225how Gerrit handles the upgrade:
226
227restart::
228 The old plugin is completely stopped. All registrations of SSH
229 commands and HTTP servlets are removed. All registrations of any
230 extension points are removed. All registered LifecycleListeners
231 have their `stop()` method invoked in reverse order. The new
232 plugin is started, and registrations are made from the new
233 plugin. There is a brief window where neither the old nor the
234 new plugin is connected to the server. This means SSH commands
235 and HTTP servlets will return not found errors, and the plugin
236 will not be notified of events that occurred during the restart.
237
238reload::
239 The new plugin is started. Its LifecycleListeners are permitted
240 to perform their `start()` methods. All SSH and HTTP registrations
241 are atomically swapped out from the old plugin to the new plugin,
242 ensuring the server never returns a not found error. All extension
243 point listeners are atomically swapped out from the old plugin to
244 the new plugin, ensuring no events are missed (however some events
245 may still route to the old plugin if the swap wasn't complete yet).
246 The old plugin is stopped.
247
Edwin Kempinf7295742012-07-16 15:03:46 +0200248To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload]
249command can be used.
250
Luca Milanesio737285d2012-09-25 14:26:43 +0100251[[init_step]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800252=== Init step
Luca Milanesio737285d2012-09-25 14:26:43 +0100253
254Plugins can contribute their own "init step" during the Gerrit init
255wizard. This is useful for guiding the Gerrit administrator through
David Pursehouse659860f2013-12-16 14:50:04 +0900256the settings needed by the plugin to work properly.
Luca Milanesio737285d2012-09-25 14:26:43 +0100257
258For instance plugins to integrate Jira issues to Gerrit changes may
259contribute their own "init step" to allow configuring the Jira URL,
260credentials and possibly verify connectivity to validate them.
261
Michael Ochmannb99feab2016-07-06 14:10:22 +0200262----
Luca Milanesio737285d2012-09-25 14:26:43 +0100263 Gerrit-InitStep: tld.example.project.MyInitStep
Michael Ochmannb99feab2016-07-06 14:10:22 +0200264----
Luca Milanesio737285d2012-09-25 14:26:43 +0100265
266MyInitStep needs to follow the standard Gerrit InitStep syntax
David Pursehouse92463562013-06-24 10:16:28 +0900267and behavior: writing to the console using the injected ConsoleUI
Luca Milanesio737285d2012-09-25 14:26:43 +0100268and accessing / changing configuration settings using Section.Factory.
269
270In addition to the standard Gerrit init injections, plugins receive
271the @PluginName String injection containing their own plugin name.
272
Edwin Kempind4cfac12013-11-27 11:22:34 +0100273During their initialization plugins may get access to the
274`project.config` file of the `All-Projects` project and they are able
275to store configuration parameters in it. For this a plugin `InitStep`
Jiří Engelthaler3033a0a2015-02-16 09:44:32 +0100276can get `com.google.gerrit.pgm.init.api.AllProjectsConfig` injected:
Edwin Kempind4cfac12013-11-27 11:22:34 +0100277
278[source,java]
279----
280 public class MyInitStep implements InitStep {
281 private final String pluginName;
282 private final ConsoleUI ui;
283 private final AllProjectsConfig allProjectsConfig;
284
Doug Kelly732ad202015-11-13 13:11:32 -0800285 @Inject
Edwin Kempind4cfac12013-11-27 11:22:34 +0100286 public MyInitStep(@PluginName String pluginName, ConsoleUI ui,
287 AllProjectsConfig allProjectsConfig) {
288 this.pluginName = pluginName;
289 this.ui = ui;
290 this.allProjectsConfig = allProjectsConfig;
291 }
292
293 @Override
294 public void run() throws Exception {
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100295 }
296
297 @Override
298 public void postRun() throws Exception {
Edwin Kempind4cfac12013-11-27 11:22:34 +0100299 ui.message("\n");
300 ui.header(pluginName + " Integration");
301 boolean enabled = ui.yesno(true, "By default enabled for all projects");
Adrian Görlerd1612972014-10-20 17:06:07 +0200302 Config cfg = allProjectsConfig.load().getConfig();
Edwin Kempind4cfac12013-11-27 11:22:34 +0100303 if (enabled) {
304 cfg.setBoolean("plugin", pluginName, "enabled", enabled);
305 } else {
306 cfg.unset("plugin", pluginName, "enabled");
307 }
308 allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration");
309 }
310 }
311----
312
Luca Milanesio737285d2012-09-25 14:26:43 +0100313Bear in mind that the Plugin's InitStep class will be loaded but
314the standard Gerrit runtime environment is not available and the plugin's
315own Guice modules were not initialized.
316This means the InitStep for a plugin is not executed in the same way that
317the plugin executes within the server, and may mean a plugin author cannot
318trivially reuse runtime code during init.
319
320For instance a plugin that wants to verify connectivity may need to statically
321call the constructor of their connection class, passing in values obtained
322from the Section.Factory rather than from an injected Config object.
323
David Pursehoused128c892013-10-22 21:52:21 +0900324Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after
325the extraction of the plugins embedded in the distribution .war file into
326`$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade.
327
328A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit
329runtime objects injected at startup.
Luca Milanesio737285d2012-09-25 14:26:43 +0100330
David Pursehouse68153d72013-09-04 10:09:17 +0900331[source,java]
332----
333public class MyInitStep implements InitStep {
334 private final ConsoleUI ui;
335 private final Section.Factory sections;
336 private final String pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100337
David Pursehouse68153d72013-09-04 10:09:17 +0900338 @Inject
339 public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) {
340 this.ui = ui;
341 this.sections = sections;
342 this.pluginName = pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100343 }
David Pursehouse68153d72013-09-04 10:09:17 +0900344
345 @Override
346 public void run() throws Exception {
347 ui.header("\nMy plugin");
348
349 Section mySection = getSection("myplugin", null);
350 mySection.string("Link name", "linkname", "MyLink");
351 }
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100352
353 @Override
354 public void postRun() throws Exception {
355 }
David Pursehouse68153d72013-09-04 10:09:17 +0900356}
357----
Luca Milanesio737285d2012-09-25 14:26:43 +0100358
Edwin Kempinf5a77332012-07-18 11:17:53 +0200359[[classpath]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800360== Classpath
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700361
362Each plugin is loaded into its own ClassLoader, isolating plugins
363from each other. A plugin or extension inherits the Java runtime
364and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin)
365from the hosting server.
366
367Plugins are loaded from a single JAR file. If a plugin needs
368additional libraries, it must include those dependencies within
369its own JAR. Plugins built using Maven may be able to use the
370link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin]
371to package additional dependencies. Relocating (or renaming) classes
372should not be necessary due to the ClassLoader isolation.
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700373
Edwin Kempin98202662013-09-18 16:03:03 +0200374[[events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800375== Listening to Events
Edwin Kempin98202662013-09-18 16:03:03 +0200376
377Certain operations in Gerrit trigger events. Plugins may receive
378notifications of these events by implementing the corresponding
379listeners.
380
Martin Fick4c72aea2014-12-10 14:58:12 -0700381* `com.google.gerrit.common.EventListener`:
Edwin Kempin64059f52013-10-31 13:49:25 +0100382+
Hugo Arèsbc1093d2016-02-23 15:04:50 -0500383Allows to listen to events without user visibility restrictions. These
384are the same link:cmd-stream-events.html#events[events] that are also streamed by
Edwin Kempin64059f52013-10-31 13:49:25 +0100385the link:cmd-stream-events.html[gerrit stream-events] command.
386
Hugo Arèsbc1093d2016-02-23 15:04:50 -0500387* `com.google.gerrit.common.UserScopedEventListener`:
388+
389Allows to listen to events visible to the specified user. These are the
390same link:cmd-stream-events.html#events[events] that are also streamed
391by the link:cmd-stream-events.html[gerrit stream-events] command.
392
Edwin Kempin98202662013-09-18 16:03:03 +0200393* `com.google.gerrit.extensions.events.LifecycleListener`:
394+
Edwin Kempin3e7928a2013-12-03 07:39:00 +0100395Plugin start and stop
Edwin Kempin98202662013-09-18 16:03:03 +0200396
397* `com.google.gerrit.extensions.events.NewProjectCreatedListener`:
398+
399Project creation
400
401* `com.google.gerrit.extensions.events.ProjectDeletedListener`:
402+
403Project deletion
404
Edwin Kempinb27c9392013-11-19 13:12:43 +0100405* `com.google.gerrit.extensions.events.HeadUpdatedListener`:
406+
407Update of HEAD on a project
408
Stefan Lay310d77d2014-05-28 13:45:25 +0200409* `com.google.gerrit.extensions.events.UsageDataPublishedListener`:
410+
411Publication of usage data
412
Adrian Görlerf4a4c9a2014-08-22 17:09:18 +0200413* `com.google.gerrit.extensions.events.GarbageCollectorListener`:
414+
415Garbage collection ran on a project
416
Hector Oswaldo Caballero5fbbdad2015-11-11 14:30:46 -0500417* `com.google.gerrit.server.extensions.events.ChangeIndexedListener`:
418+
419Update of the secondary index
420
Yang Zhenhui2659d422013-07-30 16:59:58 +0800421[[stream-events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800422== Sending Events to the Events Stream
Yang Zhenhui2659d422013-07-30 16:59:58 +0800423
424Plugins may send events to the events stream where consumers of
425Gerrit's `stream-events` ssh command will receive them.
426
427To send an event, the plugin must invoke one of the `postEvent`
David Pursehousea9bf4762016-07-08 09:34:35 +0900428methods in the `EventDispatcher` interface, passing an instance of
Martin Fick4c72aea2014-12-10 14:58:12 -0700429its own custom event class derived from
430`com.google.gerrit.server.events.Event`.
Yang Zhenhui2659d422013-07-30 16:59:58 +0800431
David Pursehousea9bf4762016-07-08 09:34:35 +0900432[source,java]
433----
434import com.google.gerrit.common.EventDispatcher;
435import com.google.gerrit.extensions.registration.DynamicItem;
436import com.google.gwtorm.server.OrmException;
437import com.google.inject.Inject;
438
439class MyPlugin {
440 private final DynamicItem<EventDispatcher> eventDispatcher;
441
442 @Inject
443 myPlugin(DynamicItem<EventDispatcher> eventDispatcher) {
444 this.eventDispatcher = eventDispatcher;
445 }
446
447 private void postEvent(MyPluginEvent event) {
448 try {
449 eventDispatcher.get().postEvent(event);
450 } catch (OrmException e) {
451 // error handling
452 }
453 }
454}
455----
456
Martin Fick0aef6f12014-12-11 16:54:21 -0700457Plugins which define new Events should register them via the
458`com.google.gerrit.server.events.EventTypes.registerClass()`
459method. This will make the EventType known to the system.
David Pursehousea61ee502016-09-06 16:27:09 +0900460Deserializing events with the
Martin Fickf70c20a2014-12-11 17:03:15 -0700461`com.google.gerrit.server.events.EventDeserializer` class requires
462that the event be registered in EventTypes.
Martin Fick0aef6f12014-12-11 16:54:21 -0700463
Martin Fickecafc932014-12-15 14:09:41 -0700464== Modifying the Stream Event Flow
465
466It is possible to modify the stream event flow from plugins by registering
467an `com.google.gerrit.server.events.EventDispatcher`. A plugin may register
468a Dispatcher class to replace the internal Dispatcher. EventDispatcher is
469a DynamicItem, so Gerrit may only have one copy.
470
Edwin Kempin32737602014-01-23 09:04:58 +0100471[[validation]]
David Pursehouse91c5f5e2014-01-23 18:57:33 +0900472== Validation Listeners
Edwin Kempin32737602014-01-23 09:04:58 +0100473
474Certain operations in Gerrit can be validated by plugins by
475implementing the corresponding link:config-validation.html[listeners].
476
Saša Živkovec85a072014-01-28 10:08:25 +0100477[[receive-pack]]
478== Receive Pack Initializers
479
480Plugins may provide ReceivePack initializers which will be invoked
481by Gerrit just before a ReceivePack instance will be used. Usually,
482plugins will make use of the setXXX methods on the ReceivePack to
483set additional properties on it.
484
Saša Živkov626c7312014-02-24 17:15:08 +0100485[[post-receive-hook]]
486== Post Receive-Pack Hooks
487
488Plugins may register PostReceiveHook instances in order to get
489notified when JGit successfully receives a pack. This may be useful
490for those plugins which would like to monitor changes in Git
491repositories.
492
Hugo Arès572d5422014-06-17 14:22:03 -0400493[[pre-upload-hook]]
494== Pre Upload-Pack Hooks
495
496Plugins may register PreUploadHook instances in order to get
497notified when JGit is about to upload a pack. This may be useful
498for those plugins which would like to monitor usage in Git
499repositories.
500
Hugo Arès41b4c0d2016-08-02 15:26:57 -0400501[[post-upload-hook]]
502== Post Upload-Pack Hooks
503
504Plugins may register PostUploadHook instances in order to get notified after
505JGit is done uploading a pack.
506
Edwin Kempinf5a77332012-07-18 11:17:53 +0200507[[ssh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800508== SSH Commands
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700509
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700510Plugins may provide commands that can be accessed through the SSH
511interface (extensions do not have this option).
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700512
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700513Command implementations must extend the base class SshCommand:
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700514
David Pursehouse68153d72013-09-04 10:09:17 +0900515[source,java]
516----
517import com.google.gerrit.sshd.SshCommand;
David Ostrovskyb7d97752013-11-09 05:23:26 +0100518import com.google.gerrit.sshd.CommandMetaData;
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700519
Ian Bulle1a12202014-02-16 17:15:42 -0800520@CommandMetaData(name="print", description="Print hello command")
David Pursehouse68153d72013-09-04 10:09:17 +0900521class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800522 @Override
523 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900524 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700525 }
David Pursehouse68153d72013-09-04 10:09:17 +0900526}
527----
Nasser Grainawie033b262012-05-09 17:54:21 -0700528
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700529If no Guice modules are declared in the manifest, SSH commands may
Edwin Kempin948de0f2012-07-16 10:34:35 +0200530use auto-registration by providing an `@Export` annotation:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700531
David Pursehouse68153d72013-09-04 10:09:17 +0900532[source,java]
533----
534import com.google.gerrit.extensions.annotations.Export;
535import com.google.gerrit.sshd.SshCommand;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700536
David Pursehouse68153d72013-09-04 10:09:17 +0900537@Export("print")
538class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800539 @Override
540 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900541 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700542 }
David Pursehouse68153d72013-09-04 10:09:17 +0900543}
544----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700545
546If explicit registration is being used, a Guice module must be
547supplied to register the SSH command and declared in the manifest
548with the `Gerrit-SshModule` attribute:
549
David Pursehouse68153d72013-09-04 10:09:17 +0900550[source,java]
551----
552import com.google.gerrit.sshd.PluginCommandModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700553
David Pursehouse68153d72013-09-04 10:09:17 +0900554class MyCommands extends PluginCommandModule {
Ian Bulle1a12202014-02-16 17:15:42 -0800555 @Override
David Pursehouse68153d72013-09-04 10:09:17 +0900556 protected void configureCommands() {
David Ostrovskyb7d97752013-11-09 05:23:26 +0100557 command(PrintHello.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700558 }
David Pursehouse68153d72013-09-04 10:09:17 +0900559}
560----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700561
562For a plugin installed as name `helloworld`, the command implemented
563by PrintHello class will be available to users as:
564
565----
Keunhong Parka09a6f12012-07-10 14:45:02 -0600566$ ssh -p 29418 review.example.com helloworld print
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700567----
568
David Ostrovsky79c4d892014-03-15 13:52:46 +0100569[[multiple-commands]]
570=== Multiple Commands bound to one implementation
571
David Ostrovskye3172b32013-10-13 14:19:13 +0200572Multiple SSH commands can be bound to the same implementation class. For
573example a Gerrit Shell plugin can bind different shell commands to the same
574implementation class:
575
576[source,java]
577----
578public class SshShellModule extends PluginCommandModule {
579 @Override
580 protected void configureCommands() {
581 command("ls").to(ShellCommand.class);
582 command("ps").to(ShellCommand.class);
583 [...]
584 }
585}
586----
587
588With the possible implementation:
589
590[source,java]
591----
592public class ShellCommand extends SshCommand {
593 @Override
594 protected void run() throws UnloggedFailure {
595 String cmd = getName().substring(getPluginName().length() + 1);
596 ProcessBuilder proc = new ProcessBuilder(cmd);
597 Process cmd = proc.start();
598 [...]
599 }
600}
601----
602
603And the call:
604
605----
606$ ssh -p 29418 review.example.com shell ls
607$ ssh -p 29418 review.example.com shell ps
608----
609
David Ostrovsky79c4d892014-03-15 13:52:46 +0100610[[root-level-commands]]
611=== Root Level Commands
612
David Ostrovskyb7d97752013-11-09 05:23:26 +0100613Single command plugins are also supported. In this scenario plugin binds
614SSH command to its own name. `SshModule` must inherit from
615`SingleCommandPluginModule` class:
616
617[source,java]
618----
619public class SshModule extends SingleCommandPluginModule {
620 @Override
621 protected void configure(LinkedBindingBuilder<Command> b) {
622 b.to(ShellCommand.class);
623 }
624}
625----
626
627If the plugin above is deployed under sh.jar file in `$site/plugins`
David Pursehouse659860f2013-12-16 14:50:04 +0900628directory, generic commands can be called without specifying the
David Ostrovskyb7d97752013-11-09 05:23:26 +0100629actual SSH command. Note in the example below, that the called commands
630`ls` and `ps` was not explicitly bound:
631
632----
633$ ssh -p 29418 review.example.com sh ls
634$ ssh -p 29418 review.example.com sh ps
635----
636
Martin Fick5f6222912015-11-12 14:52:50 -0700637[[search_operators]]
638=== Search Operators ===
639
640Plugins can define new search operators to extend change searching by
641implementing the `ChangeQueryBuilder.ChangeOperatorFactory` interface
642and registering it to an operator name in the plugin module's
643`configure()` method. The search operator name is defined during
644registration via the DynamicMap annotation mechanism. The plugin
645name will get appended to the annotated name, with an underscore
646in between, leading to the final operator name. An example
647registration looks like this:
648
649 bind(ChangeOperatorFactory.class)
650 .annotatedWith(Exports.named("sample"))
651 .to(SampleOperator.class);
652
653If this is registered in the `myplugin` plugin, then the resulting
654operator will be named `sample_myplugin`.
655
656The search operator itself is implemented by ensuring that the
657`create()` method of the class implementing the
658`ChangeQueryBuilder.ChangeOperatorFactory` interface returns a
659`Predicate<ChangeData>`. Here is a sample operator factory
David Pursehousea61ee502016-09-06 16:27:09 +0900660definition which creates a `MyPredicate`:
Martin Fick5f6222912015-11-12 14:52:50 -0700661
662[source,java]
663----
664@Singleton
665public class SampleOperator
666 implements ChangeQueryBuilder.ChangeOperatorFactory {
Edwin Kempincc82b242016-06-28 10:00:53 +0200667 public static class MyPredicate extends OperatorChangePredicate<ChangeData> {
Martin Fick5f6222912015-11-12 14:52:50 -0700668 ...
669 }
670
671 @Override
672 public Predicate<ChangeData> create(ChangeQueryBuilder builder, String value)
673 throws QueryParseException {
674 return new MyPredicate(value);
675 }
676}
677----
678
Edwin Kempin78ca0942013-10-30 11:24:06 +0100679[[simple-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800680== Simple Configuration in `gerrit.config`
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200681
682In Gerrit, global configuration is stored in the `gerrit.config` file.
683If a plugin needs global configuration, this configuration should be
684stored in a `plugin` subsection in the `gerrit.config` file.
685
Edwin Kempinc9b68602013-10-30 09:32:43 +0100686This approach of storing the plugin configuration is only suitable for
687plugins that have a simple configuration that only consists of
688key-value pairs. With this approach it is not possible to have
689subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin78ca0942013-10-30 11:24:06 +0100690configuration need to store their configuration in their
691link:#configuration[own configuration file] where they can make use of
692subsections. On the other hand storing the plugin configuration in a
693'plugin' subsection in the `gerrit.config` file has the advantage that
694administrators have all configuration parameters in one file, instead
695of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100696
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200697To avoid conflicts with other plugins, it is recommended that plugins
698only use the `plugin` subsection with their own name. For example the
699`helloworld` plugin should store its configuration in the
700`plugin.helloworld` subsection:
701
702----
703[plugin "helloworld"]
704 language = Latin
705----
706
Sasa Zivkovacdf5332013-09-20 14:05:15 +0200707Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200708plugin can easily access its configuration and there is no need for a
709plugin to parse the `gerrit.config` file on its own:
710
711[source,java]
712----
David Pursehouse529ec252013-09-27 13:45:14 +0900713@Inject
714private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200715
David Pursehoused128c892013-10-22 21:52:21 +0900716[...]
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200717
Edwin Kempin122622d2013-10-29 16:45:44 +0100718String language = cfg.getFromGerritConfig("helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900719 .getString("language", "English");
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200720----
721
Edwin Kempin78ca0942013-10-30 11:24:06 +0100722[[configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800723== Configuration in own config file
Edwin Kempin78ca0942013-10-30 11:24:06 +0100724
725Plugins can store their configuration in an own configuration file.
726This makes sense if the plugin configuration is rather complex and
727requires the usage of subsections. Plugins that have a simple
728key-value pair configuration can store their configuration in a
729link:#simple-configuration[`plugin` subsection of the `gerrit.config`
730file].
731
732The plugin configuration file must be named after the plugin and must
733be located in the `etc` folder of the review site. For example a
734configuration file for a `default-reviewer` plugin could look like
735this:
736
737.$site_path/etc/default-reviewer.config
738----
739[branch "refs/heads/master"]
740 reviewer = Project Owners
741 reviewer = john.doe@example.com
742[match "file:^.*\.txt"]
743 reviewer = My Info Developers
744----
745
David Pursehouse5b47bc42016-07-22 11:00:25 +0900746Plugins that have sensitive configuration settings can store those settings in
747an own secure configuration file. The plugin's secure configuration file must be
748named after the plugin and must be located in the `etc` folder of the review
749site. For example a secure configuration file for a `default-reviewer` plugin
750could look like this:
751
752.$site_path/etc/default-reviewer.secure.config
753----
754[auth]
755 password = secret
756----
757
Edwin Kempin78ca0942013-10-30 11:24:06 +0100758Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
759plugin can easily access its configuration:
760
761[source,java]
762----
763@Inject
764private com.google.gerrit.server.config.PluginConfigFactory cfg;
765
766[...]
767
768String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer")
769 .getStringList("branch", "refs/heads/master", "reviewer");
David Pursehouse5b47bc42016-07-22 11:00:25 +0900770String password = cfg.getGlobalPluginConfig("default-reviewer")
771 .getString("auth", null, "password");
Edwin Kempin78ca0942013-10-30 11:24:06 +0100772----
773
Edwin Kempin78ca0942013-10-30 11:24:06 +0100774
Edwin Kempin705f2842013-10-30 14:25:31 +0100775[[simple-project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800776== Simple Project Specific Configuration in `project.config`
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200777
778In Gerrit, project specific configuration is stored in the project's
779`project.config` file on the `refs/meta/config` branch. If a plugin
780needs configuration on project level (e.g. to enable its functionality
781only for certain projects), this configuration should be stored in a
782`plugin` subsection in the project's `project.config` file.
783
Edwin Kempinc9b68602013-10-30 09:32:43 +0100784This approach of storing the plugin configuration is only suitable for
785plugins that have a simple configuration that only consists of
786key-value pairs. With this approach it is not possible to have
787subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin705f2842013-10-30 14:25:31 +0100788configuration need to store their configuration in their
789link:#project-specific-configuration[own configuration file] where they
790can make use of subsections. On the other hand storing the plugin
791configuration in a 'plugin' subsection in the `project.config` file has
792the advantage that project owners have all configuration parameters in
793one file, instead of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100794
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200795To avoid conflicts with other plugins, it is recommended that plugins
796only use the `plugin` subsection with their own name. For example the
797`helloworld` plugin should store its configuration in the
798`plugin.helloworld` subsection:
799
800----
801 [plugin "helloworld"]
802 enabled = true
803----
804
805Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
806plugin can easily access its project specific configuration and there
807is no need for a plugin to parse the `project.config` file on its own:
808
809[source,java]
810----
David Pursehouse529ec252013-09-27 13:45:14 +0900811@Inject
812private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200813
David Pursehoused128c892013-10-22 21:52:21 +0900814[...]
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200815
Edwin Kempin122622d2013-10-29 16:45:44 +0100816boolean enabled = cfg.getFromProjectConfig(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900817 .getBoolean("enabled", false);
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200818----
819
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200820It is also possible to get missing configuration parameters inherited
821from the parent projects:
822
823[source,java]
824----
David Pursehouse529ec252013-09-27 13:45:14 +0900825@Inject
826private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200827
David Pursehoused128c892013-10-22 21:52:21 +0900828[...]
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200829
Edwin Kempin122622d2013-10-29 16:45:44 +0100830boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900831 .getBoolean("enabled", false);
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200832----
833
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200834Project owners can edit the project configuration by fetching the
835`refs/meta/config` branch, editing the `project.config` file and
836pushing the commit back.
837
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100838Plugin configuration values that are stored in the `project.config`
839file can be exposed in the ProjectInfoScreen to allow project owners
840to see and edit them from the UI.
841
842For this an instance of `ProjectConfigEntry` needs to be bound for each
843parameter. The export name must be a valid Git variable name. The
844variable name is case-insensitive, allows only alphanumeric characters
845and '-', and must start with an alphabetic character.
846
Edwin Kempina6c1c452013-11-28 16:55:22 +0100847The example below shows how the parameters `plugin.helloworld.enabled`
848and `plugin.helloworld.language` are bound to be editable from the
David Pursehousea1d633b2014-05-02 17:21:02 +0900849Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting"
Edwin Kempina6c1c452013-11-28 16:55:22 +0100850is provided as display name and the default value is set to `true`.
851For the parameter `plugin.helloworld.language` "Preferred Language"
852is provided as display name and "en" is set as default value.
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100853
854[source,java]
855----
856class Module extends AbstractModule {
857 @Override
858 protected void configure() {
859 bind(ProjectConfigEntry.class)
Edwin Kempina6c1c452013-11-28 16:55:22 +0100860 .annotatedWith(Exports.named("enabled"))
861 .toInstance(new ProjectConfigEntry("Enable Greeting", true));
862 bind(ProjectConfigEntry.class)
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100863 .annotatedWith(Exports.named("language"))
864 .toInstance(new ProjectConfigEntry("Preferred Language", "en"));
865 }
866}
867----
868
Edwin Kempinb64d3972013-11-17 18:55:48 +0100869By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins
870can be notified when this configuration parameter is updated on a
871project.
872
Janice Agustine5a9d012015-08-24 09:05:56 -0400873[[configuring-groups]]
874=== Referencing groups in `project.config`
875
876Plugins can refer to groups so that when they are renamed, the project
877config will also be updated in this section. The proper format to use is
878the string representation of a GroupReference, as shown below.
879
880----
881Group[group_name / group_uuid]
882----
883
Edwin Kempin705f2842013-10-30 14:25:31 +0100884[[project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800885== Project Specific Configuration in own config file
Edwin Kempin705f2842013-10-30 14:25:31 +0100886
887Plugins can store their project specific configuration in an own
888configuration file in the projects `refs/meta/config` branch.
889This makes sense if the plugins project specific configuration is
890rather complex and requires the usage of subsections. Plugins that
891have a simple key-value pair configuration can store their project
892specific configuration in a link:#simple-project-specific-configuration[
893`plugin` subsection of the `project.config` file].
894
895The plugin configuration file in the `refs/meta/config` branch must be
896named after the plugin. For example a configuration file for a
897`default-reviewer` plugin could look like this:
898
899.default-reviewer.config
900----
901[branch "refs/heads/master"]
902 reviewer = Project Owners
903 reviewer = john.doe@example.com
904[match "file:^.*\.txt"]
905 reviewer = My Info Developers
906----
907
908Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
909plugin can easily access its project specific configuration:
910
911[source,java]
912----
913@Inject
914private com.google.gerrit.server.config.PluginConfigFactory cfg;
915
916[...]
917
918String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer")
919 .getStringList("branch", "refs/heads/master", "reviewer");
920----
921
Edwin Kempin762da382013-10-30 14:50:01 +0100922It is also possible to get missing configuration parameters inherited
923from the parent projects:
924
925[source,java]
926----
927@Inject
928private com.google.gerrit.server.config.PluginConfigFactory cfg;
929
930[...]
931
David Ostrovsky468e4c32014-03-22 06:05:35 -0700932String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer")
Edwin Kempin762da382013-10-30 14:50:01 +0100933 .getStringList("branch", "refs/heads/master", "reviewer");
934----
935
Edwin Kempin705f2842013-10-30 14:25:31 +0100936Project owners can edit the project configuration by fetching the
937`refs/meta/config` branch, editing the `<plugin-name>.config` file and
938pushing the commit back.
939
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800940== React on changes in project configuration
Edwin Kempina46b6c92013-12-04 21:05:24 +0100941
942If a plugin wants to react on changes in the project configuration, it
943can implement a `GitReferenceUpdatedListener` and filter on events for
944the `refs/meta/config` branch:
945
946[source,java]
947----
948public class MyListener implements GitReferenceUpdatedListener {
949
950 private final MetaDataUpdate.Server metaDataUpdateFactory;
951
952 @Inject
953 MyListener(MetaDataUpdate.Server metaDataUpdateFactory) {
954 this.metaDataUpdateFactory = metaDataUpdateFactory;
955 }
956
957 @Override
958 public void onGitReferenceUpdated(Event event) {
Edwin Kempina951ba52014-01-03 14:07:28 +0100959 if (event.getRefName().equals(RefNames.REFS_CONFIG)) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100960 Project.NameKey p = new Project.NameKey(event.getProjectName());
961 try {
Edwin Kempina951ba52014-01-03 14:07:28 +0100962 ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId());
963 ProjectConfig newCfg = parseConfig(p, event.getNewObjectId());
Edwin Kempina46b6c92013-12-04 21:05:24 +0100964
Edwin Kempina951ba52014-01-03 14:07:28 +0100965 if (oldCfg != null && newCfg != null
966 && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100967 // submit type has changed
968 ...
969 }
970 } catch (IOException | ConfigInvalidException e) {
971 ...
972 }
973 }
974 }
Edwin Kempina951ba52014-01-03 14:07:28 +0100975
976 private ProjectConfig parseConfig(Project.NameKey p, String idStr)
977 throws IOException, ConfigInvalidException, RepositoryNotFoundException {
978 ObjectId id = ObjectId.fromString(idStr);
979 if (ObjectId.zeroId().equals(id)) {
980 return null;
981 }
982 return ProjectConfig.read(metaDataUpdateFactory.create(p), id);
983 }
Edwin Kempina46b6c92013-12-04 21:05:24 +0100984}
985----
986
987
David Ostrovsky7066cc02013-06-15 14:46:23 +0200988[[capabilities]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800989== Plugin Owned Capabilities
David Ostrovsky7066cc02013-06-15 14:46:23 +0200990
991Plugins may provide their own capabilities and restrict usage of SSH
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200992commands or `UiAction` to the users who are granted those capabilities.
David Ostrovsky7066cc02013-06-15 14:46:23 +0200993
994Plugins define the capabilities by overriding the `CapabilityDefinition`
995abstract class:
996
David Pursehouse68153d72013-09-04 10:09:17 +0900997[source,java]
998----
999public class PrintHelloCapability extends CapabilityDefinition {
1000 @Override
1001 public String getDescription() {
1002 return "Print Hello";
David Ostrovsky7066cc02013-06-15 14:46:23 +02001003 }
David Pursehouse68153d72013-09-04 10:09:17 +09001004}
1005----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001006
Dariusz Luksza112d93a2014-06-01 16:52:23 +02001007If no Guice modules are declared in the manifest, capability may
David Ostrovsky7066cc02013-06-15 14:46:23 +02001008use auto-registration by providing an `@Export` annotation:
1009
David Pursehouse68153d72013-09-04 10:09:17 +09001010[source,java]
1011----
1012@Export("printHello")
1013public class PrintHelloCapability extends CapabilityDefinition {
David Pursehoused128c892013-10-22 21:52:21 +09001014 [...]
David Pursehouse68153d72013-09-04 10:09:17 +09001015}
1016----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001017
1018Otherwise the capability must be bound in a plugin module:
1019
David Pursehouse68153d72013-09-04 10:09:17 +09001020[source,java]
1021----
1022public class HelloWorldModule extends AbstractModule {
1023 @Override
1024 protected void configure() {
1025 bind(CapabilityDefinition.class)
1026 .annotatedWith(Exports.named("printHello"))
1027 .to(PrintHelloCapability.class);
David Ostrovsky7066cc02013-06-15 14:46:23 +02001028 }
David Pursehouse68153d72013-09-04 10:09:17 +09001029}
1030----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001031
1032With a plugin-owned capability defined in this way, it is possible to restrict
David Ostrovskyf86bae52013-09-01 09:10:39 +02001033usage of an SSH command or `UiAction` to members of the group that were granted
David Ostrovsky7066cc02013-06-15 14:46:23 +02001034this capability in the usual way, using the `RequiresCapability` annotation:
1035
David Pursehouse68153d72013-09-04 10:09:17 +09001036[source,java]
1037----
1038@RequiresCapability("printHello")
1039@CommandMetaData(name="print", description="Print greeting in different languages")
1040public final class PrintHelloWorldCommand extends SshCommand {
David Pursehoused128c892013-10-22 21:52:21 +09001041 [...]
David Pursehouse68153d72013-09-04 10:09:17 +09001042}
1043----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001044
David Ostrovskyf86bae52013-09-01 09:10:39 +02001045Or with `UiAction`:
David Ostrovsky7066cc02013-06-15 14:46:23 +02001046
David Pursehouse68153d72013-09-04 10:09:17 +09001047[source,java]
1048----
1049@RequiresCapability("printHello")
1050public class SayHelloAction extends UiAction<RevisionResource>
1051 implements RestModifyView<RevisionResource, SayHelloAction.Input> {
David Pursehoused128c892013-10-22 21:52:21 +09001052 [...]
David Pursehouse68153d72013-09-04 10:09:17 +09001053}
1054----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001055
1056Capability scope was introduced to differentiate between plugin-owned
David Pursehousebf053342013-09-05 14:55:29 +09001057capabilities and core capabilities. Per default the scope of the
1058`@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means:
1059
David Ostrovsky7066cc02013-06-15 14:46:23 +02001060* when `@RequiresCapability` is used within a plugin the scope of the
1061capability is assumed to be that plugin.
David Pursehousebf053342013-09-05 14:55:29 +09001062
David Ostrovsky7066cc02013-06-15 14:46:23 +02001063* If `@RequiresCapability` is used within the core Gerrit Code Review server
1064(and thus is outside of a plugin) the scope is the core server and will use
1065the `GlobalCapability` known to Gerrit Code Review server.
1066
1067If a plugin needs to use a core capability name (e.g. "administrateServer")
1068this can be specified by setting `scope = CapabilityScope.CORE`:
1069
David Pursehouse68153d72013-09-04 10:09:17 +09001070[source,java]
1071----
1072@RequiresCapability(value = "administrateServer", scope =
1073 CapabilityScope.CORE)
David Pursehoused128c892013-10-22 21:52:21 +09001074 [...]
David Pursehouse68153d72013-09-04 10:09:17 +09001075----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001076
David Ostrovskyf86bae52013-09-01 09:10:39 +02001077[[ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001078== UI Extension
David Ostrovskyf86bae52013-09-01 09:10:39 +02001079
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001080[[panels]]
1081=== Panels
1082
1083GWT plugins can contribute panels to Gerrit screens.
1084
1085Gerrit screens define extension points where plugins can add GWT
1086panels with custom controls:
1087
1088* Change Screen:
Edwin Kempin2a8c5152015-07-08 14:28:57 +02001089** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER`:
1090+
1091Panel will be shown in the header bar to the right of the change
1092status.
1093
Edwin Kempin745021e2015-07-09 13:09:44 +02001094** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_BUTTONS`:
1095+
1096Panel will be shown in the header bar on the right side of the buttons.
1097
Edwin Kempincbc95252015-07-09 11:37:53 +02001098** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_POP_DOWNS`:
1099+
1100Panel will be shown in the header bar on the right side of the pop down
1101buttons.
1102
Khai Do675afc02016-07-28 16:30:37 -07001103** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK`:
1104+
1105Panel will be shown below the commit info block.
1106
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001107** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK`:
1108+
1109Panel will be shown below the change info block.
1110
Khai Do76c830c2016-07-28 16:35:45 -07001111** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_RELATED_INFO_BLOCK`:
1112+
1113Panel will be shown below the related info block.
1114
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001115** The following parameters are provided:
Edwin Kempin5d683cc2015-07-10 15:47:14 +02001116*** `GerritUiExtensionPoint.Key.CHANGE_INFO`:
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001117+
Edwin Kempin5d683cc2015-07-10 15:47:14 +02001118The link:rest-api-changes.html#change-info[ChangeInfo] entity for the
1119current change.
David Ostrovsky916ae0c2016-03-15 17:05:41 +01001120+
1121The link:rest-api-changes.html#revision-info[RevisionInfo] entity for
1122the current patch set.
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001123
Edwin Kempin88b947a2015-07-08 09:03:56 +02001124* Project Info Screen:
1125** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_TOP`:
1126+
1127Panel will be shown at the top of the screen.
1128
1129** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_BOTTOM`:
1130+
1131Panel will be shown at the bottom of the screen.
1132
1133** The following parameters are provided:
1134*** `GerritUiExtensionPoint.Key.PROJECT_NAME`:
1135+
1136The name of the project.
1137
Edwin Kempin241d9db2015-07-08 13:53:50 +02001138* User Password Screen:
1139** `GerritUiExtensionPoint.PASSWORD_SCREEN_BOTTOM`:
1140+
1141Panel will be shown at the bottom of the screen.
1142
Edwin Kempin30c6f472015-07-09 14:27:52 +02001143** The following parameters are provided:
1144*** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`:
1145+
1146The link:rest-api-accounts.html#account-info[AccountInfo] entity for
1147the current user.
1148
Edwin Kempin1cd95f92015-07-14 08:27:20 +02001149* User Preferences Screen:
1150** `GerritUiExtensionPoint.PREFERENCES_SCREEN_BOTTOM`:
1151+
1152Panel will be shown at the bottom of the screen.
1153
1154** The following parameters are provided:
1155*** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`:
1156+
1157The link:rest-api-accounts.html#account-info[AccountInfo] entity for
1158the current user.
1159
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001160* User Profile Screen:
1161** `GerritUiExtensionPoint.PROFILE_SCREEN_BOTTOM`:
1162+
1163Panel will be shown at the bottom of the screen below the grid with the
1164profile data.
1165
Edwin Kempin30c6f472015-07-09 14:27:52 +02001166** The following parameters are provided:
1167*** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`:
1168+
1169The link:rest-api-accounts.html#account-info[AccountInfo] entity for
1170the current user.
1171
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001172Example panel:
1173[source,java]
1174----
1175public class MyPlugin extends PluginEntryPoint {
1176 @Override
1177 public void onPluginLoad() {
1178 Plugin.get().panel(GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK,
1179 new Panel.EntryPoint() {
1180 @Override
1181 public void onLoad(Panel panel) {
1182 panel.setWidget(new InlineLabel("My Panel for change "
1183 + panel.getInt(GerritUiExtensionPoint.Key.CHANGE_ID, -1));
1184 }
1185 });
1186 }
1187}
1188----
1189
1190[[actions]]
1191=== Actions
1192
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001193Plugins can contribute UI actions on core Gerrit pages. This is useful
1194for workflow customization or exposing plugin functionality through the
1195UI in addition to SSH commands and the REST API.
David Ostrovskyf86bae52013-09-01 09:10:39 +02001196
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001197For instance a plugin to integrate Jira with Gerrit changes may
1198contribute a "File bug" button to allow filing a bug from the change
1199page or plugins to integrate continuous integration systems may
1200contribute a "Schedule" button to allow a CI build to be scheduled
1201manually from the patch set panel.
David Ostrovskyf86bae52013-09-01 09:10:39 +02001202
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001203Two different places on core Gerrit pages are supported:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001204
1205* Change screen
1206* Project info screen
1207
1208Plugins contribute UI actions by implementing the `UiAction` interface:
1209
David Pursehouse68153d72013-09-04 10:09:17 +09001210[source,java]
1211----
1212@RequiresCapability("printHello")
1213class HelloWorldAction implements UiAction<RevisionResource>,
1214 RestModifyView<RevisionResource, HelloWorldAction.Input> {
1215 static class Input {
1216 boolean french;
1217 String message;
David Ostrovskyf86bae52013-09-01 09:10:39 +02001218 }
David Pursehouse68153d72013-09-04 10:09:17 +09001219
1220 private Provider<CurrentUser> user;
1221
1222 @Inject
1223 HelloWorldAction(Provider<CurrentUser> user) {
1224 this.user = user;
1225 }
1226
1227 @Override
1228 public String apply(RevisionResource rev, Input input) {
1229 final String greeting = input.french
1230 ? "Bonjour"
1231 : "Hello";
1232 return String.format("%s %s from change %s, patch set %d!",
1233 greeting,
1234 Strings.isNullOrEmpty(input.message)
1235 ? Objects.firstNonNull(user.get().getUserName(), "world")
1236 : input.message,
1237 rev.getChange().getId().toString(),
1238 rev.getPatchSet().getPatchSetId());
1239 }
1240
1241 @Override
1242 public Description getDescription(
1243 RevisionResource resource) {
1244 return new Description()
1245 .setLabel("Say hello")
1246 .setTitle("Say hello in different languages");
1247 }
1248}
1249----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001250
David Ostrovsky450eefe2013-10-21 21:18:11 +02001251Sometimes plugins may want to be able to change the state of a patch set or
1252change in the `UiAction.apply()` method and reflect these changes on the core
1253UI. For example a buildbot plugin which exposes a 'Schedule' button on the
1254patch set panel may want to disable that button after the build was scheduled
1255and update the tooltip of that button. But because of Gerrit's caching
1256strategy the following must be taken into consideration.
1257
1258The browser is allowed to cache the `UiAction` information until something on
1259the change is modified. More accurately the change row needs to be modified in
1260the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or
1261the +refs/meta/config+ of the project or any parents needs to change to a new
1262SHA-1. The ETag SHA-1 computation code can be found in the
1263`ChangeResource.getETag()` method.
1264
David Pursehoused128c892013-10-22 21:52:21 +09001265The easiest way to accomplish this is to update `lastUpdatedOn` of the change:
David Ostrovsky450eefe2013-10-21 21:18:11 +02001266
1267[source,java]
1268----
1269@Override
1270public Object apply(RevisionResource rcrs, Input in) {
1271 // schedule a build
1272 [...]
1273 // update change
1274 ReviewDb db = dbProvider.get();
Edwin Kempine2d06b02016-02-17 18:34:17 +01001275 try (BatchUpdate bu = batchUpdateFactory.create(
1276 db, project.getNameKey(), user, TimeUtil.nowTs())) {
1277 bu.addOp(change.getId(), new BatchUpdate.Op() {
1278 @Override
1279 public boolean updateChange(BatchUpdate.ChangeContext ctx) {
Edwin Kempine2d06b02016-02-17 18:34:17 +01001280 return true;
1281 }
1282 });
1283 bu.execute();
David Ostrovsky450eefe2013-10-21 21:18:11 +02001284 }
David Pursehoused128c892013-10-22 21:52:21 +09001285 [...]
David Ostrovsky450eefe2013-10-21 21:18:11 +02001286}
1287----
1288
David Ostrovskyf86bae52013-09-01 09:10:39 +02001289`UiAction` must be bound in a plugin module:
1290
David Pursehouse68153d72013-09-04 10:09:17 +09001291[source,java]
1292----
1293public class Module extends AbstractModule {
1294 @Override
1295 protected void configure() {
1296 install(new RestApiModule() {
1297 @Override
1298 protected void configure() {
1299 post(REVISION_KIND, "say-hello")
1300 .to(HelloWorldAction.class);
1301 }
1302 });
David Ostrovskyf86bae52013-09-01 09:10:39 +02001303 }
David Pursehouse68153d72013-09-04 10:09:17 +09001304}
1305----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001306
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001307The module above must be declared in the `pom.xml` for Maven driven
1308plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001309
David Pursehouse68153d72013-09-04 10:09:17 +09001310[source,xml]
1311----
1312<manifestEntries>
1313 <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module>
1314</manifestEntries>
1315----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001316
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001317or in the `BUCK` configuration file for Buck driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001318
David Pursehouse68153d72013-09-04 10:09:17 +09001319[source,python]
1320----
1321manifest_entries = [
1322 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module',
1323]
1324----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001325
1326In some use cases more user input must be gathered, for that `UiAction` can be
1327combined with the JavaScript API. This would display a small popup near the
1328activation button to gather additional input from the user. The JS file is
1329typically put in the `static` folder within the plugin's directory:
1330
David Pursehouse68153d72013-09-04 10:09:17 +09001331[source,javascript]
1332----
1333Gerrit.install(function(self) {
1334 function onSayHello(c) {
1335 var f = c.textfield();
1336 var t = c.checkbox();
1337 var b = c.button('Say hello', {onclick: function(){
1338 c.call(
1339 {message: f.value, french: t.checked},
1340 function(r) {
1341 c.hide();
1342 window.alert(r);
1343 c.refresh();
1344 });
1345 }});
1346 c.popup(c.div(
1347 c.prependLabel('Greeting message', f),
1348 c.br(),
1349 c.label(t, 'french'),
1350 c.br(),
1351 b));
1352 f.focus();
1353 }
1354 self.onAction('revision', 'say-hello', onSayHello);
1355});
1356----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001357
1358The JS module must be exposed as a `WebUiPlugin` and bound as
1359an HTTP Module:
1360
David Pursehouse68153d72013-09-04 10:09:17 +09001361[source,java]
1362----
1363public class HttpModule extends HttpPluginModule {
1364 @Override
1365 protected void configureServlets() {
1366 DynamicSet.bind(binder(), WebUiPlugin.class)
1367 .toInstance(new JavaScriptPlugin("hello.js"));
David Ostrovskyf86bae52013-09-01 09:10:39 +02001368 }
David Pursehouse68153d72013-09-04 10:09:17 +09001369}
1370----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001371
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001372The HTTP module above must be declared in the `pom.xml` for Maven
1373driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001374
David Pursehouse68153d72013-09-04 10:09:17 +09001375[source,xml]
1376----
1377<manifestEntries>
1378 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule>
1379</manifestEntries>
1380----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001381
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001382or in the `BUCK` configuration file for Buck driven plugins
David Ostrovskyf86bae52013-09-01 09:10:39 +02001383
David Pursehouse68153d72013-09-04 10:09:17 +09001384[source,python]
1385----
1386manifest_entries = [
1387 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule',
1388]
1389----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001390
1391If `UiAction` is annotated with the `@RequiresCapability` annotation, then the
1392capability check is done during the `UiAction` gathering, so the plugin author
1393doesn't have to set `UiAction.Description.setVisible()` explicitly in this
1394case.
1395
David Pursehousea61ee502016-09-06 16:27:09 +09001396The following prerequisites must be met, to satisfy the capability check:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001397
1398* user is authenticated
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001399* user is a member of a group which has the `Administrate Server` capability, or
David Ostrovskyf86bae52013-09-01 09:10:39 +02001400* user is a member of a group which has the required capability
1401
1402The `apply` method is called when the button is clicked. If `UiAction` is
1403combined with JavaScript API (its own JavaScript function is provided),
1404then a popup dialog is normally opened to gather additional user input.
1405A new button is placed on the popup dialog to actually send the request.
1406
1407Every `UiAction` exposes a REST API endpoint. The endpoint from the example above
1408can be accessed from any REST client, i. e.:
1409
Michael Ochmannb99feab2016-07-06 14:10:22 +02001410----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001411 curl -X POST -H "Content-Type: application/json" \
1412 -d '{message: "François", french: true}' \
1413 --digest --user joe:secret \
1414 http://host:port/a/changes/1/revisions/1/cookbook~say-hello
1415 "Bonjour François from change 1, patch set 1!"
Michael Ochmannb99feab2016-07-06 14:10:22 +02001416----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001417
David Pursehouse42245822013-09-24 09:48:20 +09001418A special case is to bind an endpoint without a view name. This is
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001419particularly useful for `DELETE` requests:
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001420
1421[source,java]
1422----
1423public class Module extends AbstractModule {
1424 @Override
1425 protected void configure() {
1426 install(new RestApiModule() {
1427 @Override
1428 protected void configure() {
1429 delete(PROJECT_KIND)
1430 .to(DeleteProject.class);
1431 }
1432 });
1433 }
1434}
1435----
1436
David Pursehouse42245822013-09-24 09:48:20 +09001437For a `UiAction` bound this way, a JS API function can be provided.
1438
1439Currently only one restriction exists: per plugin only one `UiAction`
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001440can be bound per resource without view name. To define a JS function
1441for the `UiAction`, "/" must be used as the name:
1442
1443[source,javascript]
1444----
1445Gerrit.install(function(self) {
1446 function onDeleteProject(c) {
1447 [...]
1448 }
1449 self.onAction('project', '/', onDeleteProject);
1450});
1451----
1452
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001453[[top-menu-extensions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001454== Top Menu Extensions
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001455
1456Plugins can contribute items to Gerrit's top menu.
1457
1458A single top menu extension can have multiple elements and will be put as
1459the last element in Gerrit's top menu.
1460
1461Plugins define the top menu entries by implementing `TopMenu` interface:
1462
1463[source,java]
1464----
1465public class MyTopMenuExtension implements TopMenu {
1466
1467 @Override
1468 public List<MenuEntry> getEntries() {
1469 return Lists.newArrayList(
1470 new MenuEntry("Top Menu Entry", Lists.newArrayList(
1471 new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1472 }
1473}
1474----
1475
Edwin Kempin77f23242013-09-30 14:53:20 +02001476Plugins can also add additional menu items to Gerrit's top menu entries
1477by defining a `MenuEntry` that has the same name as a Gerrit top menu
1478entry:
1479
1480[source,java]
1481----
1482public class MyTopMenuExtension implements TopMenu {
1483
1484 @Override
1485 public List<MenuEntry> getEntries() {
1486 return Lists.newArrayList(
Dariusz Luksza2d3afab2013-10-01 11:07:13 +02001487 new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList(
Edwin Kempin77f23242013-09-30 14:53:20 +02001488 new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
1489 }
1490}
1491----
1492
Dariusz Lukszae8de74f2014-06-04 19:39:20 +02001493`MenuItems` that are bound for the `MenuEntry` with the name
1494`GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder
1495which is automatically replaced by the actual project name.
1496
1497E.g. plugins may register an link:#http[HTTP Servlet] to handle project
1498specific requests and add an menu item for this:
1499
1500[source,java]
1501---
1502 new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}");
1503---
1504
1505This also enables plugins to provide menu items for project aware
1506screens:
1507
1508[source,java]
1509---
1510 new MenuItem("My Screen", "/x/my-screen/for/${projectName}");
1511---
1512
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001513If no Guice modules are declared in the manifest, the top menu extension may use
1514auto-registration by providing an `@Listen` annotation:
1515
1516[source,java]
1517----
1518@Listen
1519public class MyTopMenuExtension implements TopMenu {
David Pursehoused128c892013-10-22 21:52:21 +09001520 [...]
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001521}
1522----
1523
Luca Milanesiocb230402013-10-11 08:49:56 +01001524Otherwise the top menu extension must be bound in the plugin module used
1525for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF):
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001526
1527[source,java]
1528----
Luca Milanesiocb230402013-10-11 08:49:56 +01001529package com.googlesource.gerrit.plugins.helloworld;
1530
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001531public class HelloWorldModule extends AbstractModule {
1532 @Override
1533 protected void configure() {
1534 DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class);
1535 }
1536}
1537----
1538
Luca Milanesiocb230402013-10-11 08:49:56 +01001539[source,manifest]
1540----
1541Gerrit-ApiType: plugin
1542Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule
1543----
1544
Edwin Kempinb2e926a2013-11-11 16:38:30 +01001545It is also possible to show some menu entries only if the user has a
1546certain capability:
1547
1548[source,java]
1549----
1550public class MyTopMenuExtension implements TopMenu {
1551 private final String pluginName;
1552 private final Provider<CurrentUser> userProvider;
1553 private final List<MenuEntry> menuEntries;
1554
1555 @Inject
1556 public MyTopMenuExtension(@PluginName String pluginName,
1557 Provider<CurrentUser> userProvider) {
1558 this.pluginName = pluginName;
1559 this.userProvider = userProvider;
1560 menuEntries = new ArrayList<TopMenu.MenuEntry>();
1561
1562 // add menu entry that is only visible to users with a certain capability
1563 if (canSeeMenuEntry()) {
1564 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1565 .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1566 }
1567
1568 // add menu entry that is visible to all users (even anonymous users)
1569 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1570 .singletonList(new MenuItem("Documentation", "/plugins/myplugin/"))));
1571 }
1572
1573 private boolean canSeeMenuEntry() {
1574 if (userProvider.get().isIdentifiedUser()) {
1575 CapabilityControl ctl = userProvider.get().getCapabilities();
1576 return ctl.canPerform(pluginName + "-" + MyCapability.ID)
1577 || ctl.canAdministrateServer();
1578 } else {
1579 return false;
1580 }
1581 }
1582
1583 @Override
1584 public List<MenuEntry> getEntries() {
1585 return menuEntries;
1586 }
1587}
1588----
1589
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001590[[gwt_ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001591== GWT UI Extension
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001592Plugins can extend the Gerrit UI with own GWT code.
1593
1594The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to
1595generate a GWT plugin skeleton. How to use the Maven plugin archetypes
1596is described in the link:#getting-started[Getting started] section.
1597
1598The generated GWT plugin has a link:#top-menu-extensions[top menu] that
1599opens a GWT dialog box when the user clicks on it.
1600
Edwin Kempinb74daa92013-11-11 11:28:16 +01001601In addition to the Gerrit-Plugin API a GWT plugin depends on
1602`gerrit-plugin-gwtui`. This dependency must be specified in the
1603`pom.xml`:
1604
1605[source,xml]
1606----
1607<dependency>
1608 <groupId>com.google.gerrit</groupId>
1609 <artifactId>gerrit-plugin-gwtui</artifactId>
1610 <version>${Gerrit-ApiVersion}</version>
1611</dependency>
1612----
1613
1614A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`,
1615that bundles together all the configuration settings of the GWT plugin:
1616
1617[source,xml]
1618----
1619<?xml version="1.0" encoding="UTF-8"?>
1620<module rename-to="hello_gwt_plugin">
1621 <!-- Inherit the core Web Toolkit stuff. -->
1622 <inherits name="com.google.gwt.user.User"/>
1623 <!-- Other module inherits -->
1624 <inherits name="com.google.gerrit.Plugin"/>
1625 <inherits name="com.google.gwt.http.HTTP"/>
1626 <!-- Using GWT built-in themes adds a number of static -->
1627 <!-- resources to the plugin. No theme inherits lines were -->
1628 <!-- added in order to make this plugin as simple as possible -->
1629 <!-- Specify the app entry point class. -->
1630 <entry-point class="${package}.client.HelloPlugin"/>
1631 <stylesheet src="hello.css"/>
1632</module>
1633----
1634
1635The GWT module must inherit `com.google.gerrit.Plugin` and
1636`com.google.gwt.http.HTTP`.
1637
1638To register the GWT module a `GwtPlugin` needs to be bound.
1639
1640If no Guice modules are declared in the manifest, the GWT plugin may
1641use auto-registration by using the `@Listen` annotation:
1642
1643[source,java]
1644----
1645@Listen
1646public class MyExtension extends GwtPlugin {
1647 public MyExtension() {
1648 super("hello_gwt_plugin");
1649 }
1650}
1651----
1652
1653Otherwise the binding must be done in an `HttpModule`:
1654
1655[source,java]
1656----
1657public class HttpModule extends HttpPluginModule {
1658
1659 @Override
1660 protected void configureServlets() {
1661 DynamicSet.bind(binder(), WebUiPlugin.class)
1662 .toInstance(new GwtPlugin("hello_gwt_plugin"));
1663 }
1664}
1665----
1666
1667The HTTP module above must be declared in the `pom.xml` for Maven
1668driven plugins:
1669
1670[source,xml]
1671----
1672<manifestEntries>
1673 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule>
1674</manifestEntries>
1675----
1676
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001677The name that is provided to the `GwtPlugin` must match the GWT
1678module name compiled into the plugin. The name of the GWT module
1679can be explicitly set in the GWT module XML file by specifying
1680the `rename-to` attribute on the module. It is important that the
1681module name be unique across all plugins installed on the server,
1682as the module name determines the JavaScript namespace used by the
1683compiled plugin code.
Edwin Kempinb74daa92013-11-11 11:28:16 +01001684
1685[source,xml]
1686----
1687<module rename-to="hello_gwt_plugin">
1688----
1689
1690The actual GWT code must be implemented in a class that extends
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001691`com.google.gerrit.plugin.client.PluginEntryPoint`:
Edwin Kempinb74daa92013-11-11 11:28:16 +01001692
1693[source,java]
1694----
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001695public class HelloPlugin extends PluginEntryPoint {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001696
1697 @Override
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001698 public void onPluginLoad() {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001699 // Create the dialog box
1700 final DialogBox dialogBox = new DialogBox();
1701
1702 // The content of the dialog comes from a User specified Preference
1703 dialogBox.setText("Hello from GWT Gerrit UI plugin");
1704 dialogBox.setAnimationEnabled(true);
1705 Button closeButton = new Button("Close");
1706 VerticalPanel dialogVPanel = new VerticalPanel();
1707 dialogVPanel.setWidth("100%");
1708 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
1709 dialogVPanel.add(closeButton);
1710
1711 closeButton.addClickHandler(new ClickHandler() {
1712 public void onClick(ClickEvent event) {
1713 dialogBox.hide();
1714 }
1715 });
1716
1717 // Set the contents of the Widget
1718 dialogBox.setWidget(dialogVPanel);
1719
1720 RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1721 rootPanel.getElement().removeAttribute("href");
1722 rootPanel.addDomHandler(new ClickHandler() {
1723 @Override
1724 public void onClick(ClickEvent event) {
1725 dialogBox.center();
1726 dialogBox.show();
1727 }
1728 }, ClickEvent.getType());
1729 }
1730}
1731----
1732
1733This class must be set as entry point in the GWT module:
1734
1735[source,xml]
1736----
1737<entry-point class="${package}.client.HelloPlugin"/>
1738----
1739
1740In addition this class must be defined as module in the `pom.xml` for the
1741`gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin`
1742must be set to `${project.build.directory}/classes/static`:
1743
1744[source,xml]
1745----
1746<plugin>
1747 <groupId>org.codehaus.mojo</groupId>
1748 <artifactId>gwt-maven-plugin</artifactId>
David Pursehouse7ab81732015-05-07 12:00:47 +09001749 <version>2.7.0</version>
Edwin Kempinb74daa92013-11-11 11:28:16 +01001750 <configuration>
1751 <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module>
1752 <disableClassMetadata>true</disableClassMetadata>
1753 <disableCastChecking>true</disableCastChecking>
1754 <webappDirectory>${project.build.directory}/classes/static</webappDirectory>
1755 </configuration>
1756 <executions>
1757 <execution>
1758 <goals>
1759 <goal>compile</goal>
1760 </goals>
1761 </execution>
1762 </executions>
1763</plugin>
1764----
1765
1766To attach a GWT widget defined by the plugin to the Gerrit core UI
1767`com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the
1768Gerrit core widgets:
1769
1770[source,java]
1771----
1772RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1773rootPanel.getElement().removeAttribute("href");
1774rootPanel.addDomHandler(new ClickHandler() {
1775 @Override
1776 public void onClick(ClickEvent event) {
1777 dialogBox.center();
1778 dialogBox.show();
1779 }
1780}, ClickEvent.getType());
1781----
1782
1783GWT plugins can come with their own css file. This css file must have a
1784unique name and must be registered in the GWT module:
1785
1786[source,xml]
1787----
1788<stylesheet src="hello.css"/>
1789----
1790
Edwin Kempin2570b102013-11-11 11:44:50 +01001791If a GWT plugin wants to invoke the Gerrit REST API it can use
David Pursehouse3a388312014-02-25 16:41:47 +09001792`com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL
Edwin Kempin2570b102013-11-11 11:44:50 +01001793path and to trigger the REST calls.
1794
1795Example for invoking a Gerrit core REST endpoint:
1796
1797[source,java]
1798----
1799new RestApi("projects").id(projectName).view("description")
1800 .put("new description", new AsyncCallback<JavaScriptObject>() {
1801
1802 @Override
1803 public void onSuccess(JavaScriptObject result) {
1804 // TODO
1805 }
1806
1807 @Override
1808 public void onFailure(Throwable caught) {
1809 // never invoked
1810 }
1811});
1812----
1813
1814Example for invoking a REST endpoint defined by a plugin:
1815
1816[source,java]
1817----
1818new RestApi("projects").id(projectName).view("myplugin", "myview")
1819 .get(new AsyncCallback<JavaScriptObject>() {
1820
1821 @Override
1822 public void onSuccess(JavaScriptObject result) {
1823 // TODO
1824 }
1825
1826 @Override
1827 public void onFailure(Throwable caught) {
1828 // never invoked
1829 }
1830});
1831----
1832
1833The `onFailure(Throwable)` of the provided callback is never invoked.
1834If an error occurs, it is shown in an error dialog.
1835
1836In order to be able to do REST calls the GWT module must inherit
1837`com.google.gwt.json.JSON`:
1838
1839[source,xml]
1840----
1841<inherits name="com.google.gwt.json.JSON"/>
1842----
1843
Edwin Kempin15199792014-04-23 16:22:05 +02001844[[screen]]
Shawn Pearced5c844f2013-12-26 15:32:26 -08001845== Add Screen
Edwin Kempin15199792014-04-23 16:22:05 +02001846A link:#gwt_ui_extension[GWT plugin] can link:#top-menu-extensions[add
1847a menu item] that opens a screen that is implemented by the plugin.
1848This way plugin screens can be fully integrated into the Gerrit UI.
Shawn Pearced5c844f2013-12-26 15:32:26 -08001849
1850Example menu item:
1851[source,java]
1852----
1853public class MyMenu implements TopMenu {
1854 private final List<MenuEntry> menuEntries;
1855
1856 @Inject
1857 public MyMenu(@PluginName String name) {
David Pursehouseccdeae82016-05-03 23:16:15 +09001858 menuEntries = new ArrayList<>();
Shawn Pearced5c844f2013-12-26 15:32:26 -08001859 menuEntries.add(new MenuEntry("My Menu", Collections.singletonList(
1860 new MenuItem("My Screen", "#/x/" + name + "/my-screen", ""))));
1861 }
1862
1863 @Override
1864 public List<MenuEntry> getEntries() {
1865 return menuEntries;
1866 }
1867}
1868----
1869
1870Example screen:
1871[source,java]
1872----
1873public class MyPlugin extends PluginEntryPoint {
1874 @Override
1875 public void onPluginLoad() {
1876 Plugin.get().screen("my-screen", new Screen.EntryPoint() {
1877 @Override
1878 public void onLoad(Screen screen) {
1879 screen.add(new InlineLabel("My Screen");
1880 screen.show();
1881 }
1882 });
1883 }
1884}
1885----
1886
Edwin Kempin70e11122015-07-08 13:28:40 +02001887[[user-settings-screen]]
1888== Add User Settings Screen
1889
1890A link:#gwt_ui_extension[GWT plugin] can implement a user settings
1891screen that is integrated into the Gerrit user settings menu.
1892
1893Example settings screen:
1894[source,java]
1895----
1896public class MyPlugin extends PluginEntryPoint {
1897 @Override
1898 public void onPluginLoad() {
1899 Plugin.get().settingsScreen("my-preferences", "My Preferences",
1900 new Screen.EntryPoint() {
1901 @Override
1902 public void onLoad(Screen screen) {
1903 screen.setPageTitle("Settings");
1904 screen.add(new InlineLabel("My Preferences"));
1905 screen.show();
1906 }
1907 });
1908 }
1909}
1910----
1911
Edwin Kempinfa0d4942015-07-16 12:38:52 +02001912By defining an link:config-gerrit.html#urlAlias[urlAlias] Gerrit
1913administrators can map plugin screens into the Gerrit URL namespace or
1914even replace Gerrit screens by plugin screens.
1915
Edwin Kempinb1e6a3a2015-07-22 15:36:56 +02001916Plugins may also programatically add URL aliases in the preferences of
1917of a user. This way certain screens can be replaced for certain users.
1918E.g. the plugin may offer a user preferences setting for choosing a
1919screen that then sets/unsets a URL alias for the user.
1920
Edwin Kempin289f1a02014-02-04 16:08:25 +01001921[[settings-screen]]
1922== Plugin Settings Screen
1923
1924If a plugin implements a screen for administrating its settings that is
1925available under "#/x/<plugin-name>/settings" it is automatically linked
1926from the plugin list screen.
1927
Edwin Kempinf5a77332012-07-18 11:17:53 +02001928[[http]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001929== HTTP Servlets
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001930
1931Plugins or extensions may register additional HTTP servlets, and
1932wrap them with HTTP filters.
1933
1934Servlets may use auto-registration to declare the URL they handle:
1935
David Pursehouse68153d72013-09-04 10:09:17 +09001936[source,java]
1937----
1938import com.google.gerrit.extensions.annotations.Export;
1939import com.google.inject.Singleton;
1940import javax.servlet.http.HttpServlet;
1941import javax.servlet.http.HttpServletRequest;
1942import javax.servlet.http.HttpServletResponse;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001943
David Pursehouse68153d72013-09-04 10:09:17 +09001944@Export("/print")
1945@Singleton
1946class HelloServlet extends HttpServlet {
1947 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
1948 res.setContentType("text/plain");
1949 res.setCharacterEncoding("UTF-8");
1950 res.getWriter().write("Hello");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001951 }
David Pursehouse68153d72013-09-04 10:09:17 +09001952}
1953----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001954
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001955The auto registration only works for standard servlet mappings like
Jonathan Nieder5758f182015-03-30 11:28:55 -07001956`/foo` or `+/foo/*+`. Regex style bindings must use a Guice ServletModule
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001957to register the HTTP servlets and declare it explicitly in the manifest
1958with the `Gerrit-HttpModule` attribute:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001959
David Pursehouse68153d72013-09-04 10:09:17 +09001960[source,java]
1961----
1962import com.google.inject.servlet.ServletModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001963
David Pursehouse68153d72013-09-04 10:09:17 +09001964class MyWebUrls extends ServletModule {
1965 protected void configureServlets() {
1966 serve("/print").with(HelloServlet.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001967 }
David Pursehouse68153d72013-09-04 10:09:17 +09001968}
1969----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001970
1971For a plugin installed as name `helloworld`, the servlet implemented
1972by HelloServlet class will be available to users as:
1973
1974----
1975$ curl http://review.example.com/plugins/helloworld/print
1976----
Nasser Grainawie033b262012-05-09 17:54:21 -07001977
Edwin Kempinf5a77332012-07-18 11:17:53 +02001978[[data-directory]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001979== Data Directory
Edwin Kempin41f63912012-07-17 12:33:55 +02001980
Dave Borowitz9e158752015-02-24 10:17:04 -08001981Plugins can request a data directory with a `@PluginData` Path (or File,
1982deprecated) dependency. A data directory will be created automatically
1983by the server in `$site_path/data/$plugin_name` and passed to the
1984plugin.
Edwin Kempin41f63912012-07-17 12:33:55 +02001985
1986Plugins can use this to store any data they want.
1987
David Pursehouse68153d72013-09-04 10:09:17 +09001988[source,java]
1989----
1990@Inject
Dave Borowitz9e158752015-02-24 10:17:04 -08001991MyType(@PluginData java.nio.file.Path myDir) {
1992 this.in = Files.newInputStream(myDir.resolve("my.config"));
David Pursehouse68153d72013-09-04 10:09:17 +09001993}
1994----
Edwin Kempin41f63912012-07-17 12:33:55 +02001995
Dariusz Lukszaebab92a2014-09-10 11:14:19 +02001996[[secure-store]]
1997== SecureStore
1998
1999SecureStore allows to change the way Gerrit stores sensitive data like
2000passwords.
2001
2002In order to replace the default SecureStore (no-op) implementation,
2003a class that extends `com.google.gerrit.server.securestore.SecureStore`
2004needs to be provided (with dependencies) in a separate jar file. Then
2005link:pgm-SwitchSecureStore.html[SwitchSecureStore] must be run to
2006switch implementations.
2007
2008The SecureStore implementation is instantiated using a Guice injector
2009which binds the `File` annotated with the `@SitePath` annotation.
2010This means that a SecureStore implementation class can get access to
2011the `site_path` like in the following example:
2012
2013[source,java]
2014----
2015@Inject
2016MySecureStore(@SitePath java.io.File sitePath) {
2017 // your code
2018}
2019----
2020
2021No Guice bindings or modules are required. Gerrit will automatically
2022discover and bind the implementation.
2023
Michael Ochmann24612652016-02-12 17:26:18 +01002024[[accountcreation]]
2025== Account Creation
2026
2027Plugins can hook into the
2028link:rest-api-accounts.html#create-account[account creation] REST API and
2029inject additional external identifiers for an account that represents a user
2030in some external user store. For that, an implementation of the extension
2031point `com.google.gerrit.server.api.accounts.AccountExternalIdCreator`
2032must be registered.
2033
2034[source,java]
2035----
2036class MyExternalIdCreator implements AccountExternalIdCreator {
2037 @Override
2038 public List<AccountExternalId> create(Account.Id id, String username,
2039 String email) {
2040 // your code
2041 }
2042}
2043
2044bind(AccountExternalIdCreator.class)
2045 .annotatedWith(UniqueAnnotations.create())
2046 .to(MyExternalIdCreator.class);
2047}
2048----
2049
Edwin Kempinea621482013-10-16 12:58:24 +02002050[[download-commands]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002051== Download Commands
Edwin Kempinea621482013-10-16 12:58:24 +02002052
Edwin Kempineafde882015-05-11 15:40:44 +02002053Gerrit offers commands for downloading changes and cloning projects
2054using different download schemes (e.g. for downloading via different
2055network protocols). Plugins can contribute download schemes, download
2056commands and clone commands by implementing
2057`com.google.gerrit.extensions.config.DownloadScheme`,
2058`com.google.gerrit.extensions.config.DownloadCommand` and
2059`com.google.gerrit.extensions.config.CloneCommand`.
Edwin Kempinea621482013-10-16 12:58:24 +02002060
Edwin Kempineafde882015-05-11 15:40:44 +02002061The download schemes, download commands and clone commands which are
2062used most often are provided by the Gerrit core plugin
2063`download-commands`.
Edwin Kempinea621482013-10-16 12:58:24 +02002064
Edwin Kempin78279ba2015-05-22 15:22:41 +02002065[[included-in]]
2066== Included In
2067
2068For merged changes the link:user-review-ui.html#included-in[Included In]
2069drop-down panel shows the branches and tags in which the change is
2070included.
2071
2072Plugins can add additional systems in which the change can be included
2073by implementing `com.google.gerrit.extensions.config.ExternalIncludedIn`,
2074e.g. a plugin can provide a list of servers on which the change was
2075deployed.
2076
Sven Selbergae1a10c2014-02-14 14:24:29 +01002077[[links-to-external-tools]]
2078== Links To External Tools
2079
2080Gerrit has extension points that enables development of a
2081light-weight plugin that links commits to external
2082tools (GitBlit, CGit, company specific resources etc).
2083
2084PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI.
2085
2086[source, java]
2087----
2088import com.google.gerrit.extensions.annotations.Listen;
2089import com.google.gerrit.extensions.webui.PatchSetWebLink;;
Sven Selberga85e64d2014-09-24 10:52:21 +02002090import com.google.gerrit.extensions.webui.WebLinkTarget;
Sven Selbergae1a10c2014-02-14 14:24:29 +01002091
2092@Listen
2093public class MyWeblinkPlugin implements PatchSetWebLink {
2094
2095 private String name = "MyLink";
2096 private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s";
Sven Selberg55484202014-06-26 08:48:51 +02002097 private String imageUrl = "http://placehold.it/16x16.gif";
Sven Selbergae1a10c2014-02-14 14:24:29 +01002098
2099 @Override
Jonathan Niederb3cd6902015-03-12 16:19:15 -07002100 public WebLinkInfo getPatchSetWebLink(String projectName, String commit) {
Sven Selberga85e64d2014-09-24 10:52:21 +02002101 return new WebLinkInfo(name,
2102 imageUrl,
2103 String.format(placeHolderUrlProjectCommit, project, commit),
2104 WebLinkTarget.BLANK);
Edwin Kempinceeed6b2014-09-11 17:07:33 +02002105 }
Sven Selbergae1a10c2014-02-14 14:24:29 +01002106}
2107----
2108
David Pursehouse1aedee62016-12-09 11:12:27 +09002109ParentWebLinks will appear to the right of the SHA1 of the parent
2110revisions in the UI. The implementation should in most use cases direct
2111to the same external service as PatchSetWebLink; it is provided as a
2112separate interface because not all users want to have links for the
2113parent revisions.
2114
Edwin Kempinb3696c82014-09-11 09:41:42 +02002115FileWebLinks will appear in the side-by-side diff screen on the right
2116side of the patch selection on each side.
2117
Edwin Kempin8cdce502014-12-06 10:55:38 +01002118DiffWebLinks will appear in the side-by-side and unified diff screen in
2119the header next to the navigation icons.
2120
Edwin Kempinea004752014-04-11 15:56:02 +02002121ProjectWebLinks will appear in the project list in the
2122`Repository Browser` column.
2123
Edwin Kempin0f697bd2014-09-10 18:23:29 +02002124BranchWebLinks will appear in the branch list in the last column.
2125
Edwin Kempinf82b8122016-06-03 09:20:16 +02002126FileHistoryWebLinks will appear on the access rights screen.
2127
Saša Živkovca7a67e2015-12-01 14:25:10 +01002128[[lfs-extension]]
2129== LFS Storage Plugins
2130
David Pursehouse2463c542016-08-02 16:04:58 +09002131Gerrit provides an extension point that enables development of
2132link:https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md[
2133LFS (Large File Storage)] storage plugins. Gerrit core exposes the default LFS
2134protocol endpoint `<project-name>/info/lfs/objects/batch` and forwards the requests
2135to the configured link:config-gerrit.html#lfs[lfs.plugin] plugin which implements
2136the LFS protocol. By exposing the default LFS endpoint, the git-lfs client can be
2137used without any configuration.
Saša Živkovca7a67e2015-12-01 14:25:10 +01002138
2139[source, java]
2140----
2141/** Provide an LFS protocol implementation */
2142import org.eclipse.jgit.lfs.server.LargeFileRepository;
2143import org.eclipse.jgit.lfs.server.LfsProtocolServlet;
2144
2145@Singleton
2146public class LfsApiServlet extends LfsProtocolServlet {
2147 private static final long serialVersionUID = 1L;
2148
2149 private final S3LargeFileRepository repository;
2150
2151 @Inject
2152 LfsApiServlet(S3LargeFileRepository repository) {
2153 this.repository = repository;
2154 }
2155
2156 @Override
2157 protected LargeFileRepository getLargeFileRepository() {
2158 return repository;
2159 }
2160}
2161
2162/** Register the LfsApiServlet to listen on the default LFS protocol endpoint */
2163import static com.google.gerrit.httpd.plugins.LfsPluginServlet.URL_REGEX;
2164
2165import com.google.gerrit.httpd.plugins.HttpPluginModule;
2166
2167public class HttpModule extends HttpPluginModule {
2168
2169 @Override
2170 protected void configureServlets() {
2171 serveRegex(URL_REGEX).with(LfsApiServlet.class);
2172 }
2173}
2174
2175/** Provide an implementation of the LargeFileRepository */
2176import org.eclipse.jgit.lfs.server.s3.S3Repository;
2177
2178public class S3LargeFileRepository extends S3Repository {
2179...
2180}
2181----
2182
David Pursehouse8ad11732016-08-29 15:00:14 +09002183[[metrics]]
2184== Metrics
2185
2186=== Metrics Reporting
2187
2188To send Gerrit's metrics data to an external reporting backend, a plugin can
2189get a `MetricRegistry` injected and register an instance of a class that
2190implements the `Reporter` interface from link:http://metrics.dropwizard.io/[
2191DropWizard Metrics].
2192
2193Metric reporting plugin implementations are provided for
2194link:https://gerrit.googlesource.com/plugins/metrics-reporter-jmx/[JMX],
2195link:https://gerrit.googlesource.com/plugins/metrics-reporter-elasticsearch/[Elastic Search],
2196and link:https://gerrit.googlesource.com/plugins/metrics-reporter-graphite/[Graphite].
2197
2198There is also a working example of reporting metrics to the console in the
2199link:https://gerrit.googlesource.com/plugins/cookbook-plugin/+/master/src/main/java/com/googlesource/gerrit/plugins/cookbook/ConsoleMetricReporter.jave[
2200cookbook plugin].
2201
2202=== Providing own metrics
2203
2204Plugins may provide metrics to be dispatched to external reporting services by
2205getting a `MetricMaker` injected and creating instances of specific types of
2206metric:
2207
2208* Counter
2209+
2210Metric whose value increments during the life of the process.
2211
2212* Timer
2213+
2214Metric recording time spent on an operation.
2215
2216* Histogram
2217+
2218Metric recording statistical distribution (rate) of values.
2219
David Pursehouse48d05ea2017-02-03 19:05:29 +09002220Note that metrics cannot be recorded from plugin init steps that
2221are run during site initialization.
2222
David Pursehousec3bbd562017-02-06 20:25:29 +09002223By default, plugin metrics are recorded under
2224`plugins/${plugin-name}/${metric-name}`. This can be changed by
2225setting `plugins.${plugin-name}.metricsPrefix` in the `gerrit.config`
2226file. For example:
2227
2228----
2229 [plugin "my-plugin"]
2230 metricsPrefix = my-metrics
2231----
2232
2233will cause the metrics to be recorded under `my-metrics/${metric-name}`.
David Pursehouse8ad11732016-08-29 15:00:14 +09002234
2235See the replication metrics in the
2236link:https://gerrit.googlesource.com/plugins/replication/+/master/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationMetrics.java[
2237replication plugin] for an example of usage.
2238
Edwin Kempinda17bc32016-06-14 11:50:58 +02002239[[account-patch-review-store]]
2240== AccountPatchReviewStore
2241
2242The AccountPatchReviewStore is used to store reviewed flags on changes.
2243A reviewed flag is a tuple of (patch set ID, file, account ID) and
2244records whether the user has reviewed a file in a patch set. Each user
2245can easily have thousands of reviewed flags and the number of reviewed
2246flags is growing without bound. The store must be able handle this data
2247volume efficiently.
2248
2249Gerrit implements this extension point, but plugins may bind another
2250implementation, e.g. one that supports multi-master.
2251
2252----
2253DynamicItem.bind(binder(), AccountPatchReviewStore.class)
2254 .to(MultiMasterAccountPatchReviewStore.class);
2255
2256...
2257
2258public class MultiMasterAccountPatchReviewStore
2259 implements AccountPatchReviewStore {
2260 ...
2261}
2262----
2263
Edwin Kempinf5a77332012-07-18 11:17:53 +02002264[[documentation]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002265== Documentation
Nasser Grainawie033b262012-05-09 17:54:21 -07002266
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002267If a plugin does not register a filter or servlet to handle URLs
Jonathan Nieder5758f182015-03-30 11:28:55 -07002268`+/Documentation/*+` or `+/static/*+`, the core Gerrit server will
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002269automatically export these resources over HTTP from the plugin JAR.
2270
David Pursehouse6853b5a2013-07-10 11:38:03 +09002271Static resources under the `static/` directory in the JAR will be
Dave Borowitzb893ac82013-03-27 10:03:55 -04002272available as `/plugins/helloworld/static/resource`. This prefix is
2273configurable by setting the `Gerrit-HttpStaticPrefix` attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002274
David Pursehouse6853b5a2013-07-10 11:38:03 +09002275Documentation files under the `Documentation/` directory in the JAR
Dave Borowitzb893ac82013-03-27 10:03:55 -04002276will be available as `/plugins/helloworld/Documentation/resource`. This
2277prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix`
2278attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002279
Christian Aistleitner040cf822015-03-26 21:09:09 +01002280Documentation may be written in the Markdown flavor
2281link:https://github.com/sirthias/pegdown[pegdown]
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002282if the file name ends with `.md`. Gerrit will automatically convert
2283Markdown to HTML if accessed with extension `.html`.
Nasser Grainawie033b262012-05-09 17:54:21 -07002284
Edwin Kempinf5a77332012-07-18 11:17:53 +02002285[[macros]]
Edwin Kempinc78777d2012-07-16 15:55:11 +02002286Within the Markdown documentation files macros can be used that allow
2287to write documentation with reasonably accurate examples that adjust
2288automatically based on the installation.
2289
2290The following macros are supported:
2291
2292[width="40%",options="header"]
2293|===================================================
2294|Macro | Replacement
2295|@PLUGIN@ | name of the plugin
2296|@URL@ | Gerrit Web URL
2297|@SSH_HOST@ | SSH Host
2298|@SSH_PORT@ | SSH Port
2299|===================================================
2300
2301The macros will be replaced when the documentation files are rendered
2302from Markdown to HTML.
2303
2304Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@`
2305even if there is an expansion for `KEEP` in the future.
2306
Edwin Kempinf5a77332012-07-18 11:17:53 +02002307[[auto-index]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002308=== Automatic Index
Shawn O. Pearce795167c2012-05-12 11:20:18 -07002309
2310If a plugin does not handle its `/` URL itself, Gerrit will
2311redirect clients to the plugin's `/Documentation/index.html`.
2312Requests for `/Documentation/` (bare directory) will also redirect
2313to `/Documentation/index.html`.
2314
2315If neither resource `Documentation/index.html` or
2316`Documentation/index.md` exists in the plugin JAR, Gerrit will
2317automatically generate an index page for the plugin's documentation
2318tree by scanning every `*.md` and `*.html` file in the Documentation/
2319directory.
2320
2321For any discovered Markdown (`*.md`) file, Gerrit will parse the
2322header of the file and extract the first level one title. This
2323title text will be used as display text for a link to the HTML
2324version of the page.
2325
2326For any discovered HTML (`*.html`) file, Gerrit will use the name
2327of the file, minus the `*.html` extension, as the link text. Any
2328hyphens in the file name will be replaced with spaces.
2329
David Pursehouse6853b5a2013-07-10 11:38:03 +09002330If a discovered file is named `about.md` or `about.html`, its
2331content will be inserted in an 'About' section at the top of the
2332auto-generated index page. If both `about.md` and `about.html`
2333exist, only the first discovered file will be used.
2334
Shawn O. Pearce795167c2012-05-12 11:20:18 -07002335If a discovered file name beings with `cmd-` it will be clustered
David Pursehouse6853b5a2013-07-10 11:38:03 +09002336into a 'Commands' section of the generated index page.
2337
David Pursehousefe529152013-08-14 16:35:06 +09002338If a discovered file name beings with `servlet-` it will be clustered
2339into a 'Servlets' section of the generated index page.
2340
2341If a discovered file name beings with `rest-api-` it will be clustered
2342into a 'REST APIs' section of the generated index page.
2343
David Pursehouse6853b5a2013-07-10 11:38:03 +09002344All other files are clustered under a 'Documentation' section.
Shawn O. Pearce795167c2012-05-12 11:20:18 -07002345
2346Some optional information from the manifest is extracted and
2347displayed as part of the index page, if present in the manifest:
2348
2349[width="40%",options="header"]
2350|===================================================
2351|Field | Source Attribute
2352|Name | Implementation-Title
2353|Vendor | Implementation-Vendor
2354|Version | Implementation-Version
2355|URL | Implementation-URL
2356|API Version | Gerrit-ApiVersion
2357|===================================================
2358
Edwin Kempinf5a77332012-07-18 11:17:53 +02002359[[deployment]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002360== Deployment
Nasser Grainawie033b262012-05-09 17:54:21 -07002361
Edwin Kempinf7295742012-07-16 15:03:46 +02002362Compiled plugins and extensions can be deployed to a running Gerrit
2363server using the link:cmd-plugin-install.html[plugin install] command.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002364
David Pursehousea1d633b2014-05-02 17:21:02 +09002365Web UI plugins distributed as single `.js` file can be deployed
Dariusz Luksza357a2422012-11-12 06:16:26 +01002366without the overhead of JAR packaging, for more information refer to
2367link:cmd-plugin-install.html[plugin install] command.
2368
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002369Plugins can also be copied directly into the server's
Dariusz Luksza357a2422012-11-12 06:16:26 +01002370directory at `$site_path/plugins/$name.(jar|js)`. The name of
2371the JAR file, minus the `.jar` or `.js` extension, will be used as the
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002372plugin name. Unless disabled, servers periodically scan this
2373directory for updated plugins. The time can be adjusted by
2374link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency].
Deniz Türkoglueb78b602012-05-07 14:02:36 -07002375
Edwin Kempinf7295742012-07-16 15:03:46 +02002376For disabling plugins the link:cmd-plugin-remove.html[plugin remove]
2377command can be used.
2378
Brad Larsond5e87c32012-07-11 12:18:49 -05002379Disabled plugins can be re-enabled using the
2380link:cmd-plugin-enable.html[plugin enable] command.
2381
Edwin Kempinc1a25102015-06-22 14:47:36 +02002382== Known issues and bugs
2383
2384=== Error handling in UI when using the REST API
2385
2386When a plugin invokes a REST endpoint in the UI, it provides an
2387`AsyncCallback` to handle the result. At the moment the
2388`onFailure(Throwable)` of the callback is never invoked, even if there
2389is an error. Errors are always handled by the Gerrit core UI which
2390shows the error dialog. This means currently plugins cannot do any
2391error handling and e.g. ignore expected errors.
2392
2393In the following example the REST endpoint would return '404 Not Found'
2394if there is no HTTP password and the Gerrit core UI would display an
2395error dialog for this. However having no HTTP password is not an error
2396and the plugin may like to handle this case.
2397
2398[source,java]
2399----
2400new RestApi("accounts").id("self").view("password.http")
2401 .get(new AsyncCallback<NativeString>() {
2402
2403 @Override
2404 public void onSuccess(NativeString httpPassword) {
2405 // TODO
2406 }
2407
2408 @Override
2409 public void onFailure(Throwable caught) {
2410 // never invoked
2411 }
2412});
2413----
2414
2415
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002416== SEE ALSO
David Ostrovskyf86bae52013-09-01 09:10:39 +02002417
2418* link:js-api.html[JavaScript API]
2419* link:dev-rest-api.html[REST API Developers' Notes]
2420
Deniz Türkoglueb78b602012-05-07 14:02:36 -07002421GERRIT
2422------
2423Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07002424
2425SEARCHBOX
2426---------