blob: a672b177efa9c8d1cd01cb8add60fc8154c30041 [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 Pursehousef91675e2015-01-22 16:32:50 +090039 -DarchetypeVersion=2.12-SNAPSHOT \
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
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070093====
94 Implementation-Title: Example plugin showing examples
95 Implementation-Version: 1.0
96 Implementation-Vendor: Example, Inc.
97 Implementation-URL: http://example.com/opensource/plugin-foo/
98====
Nasser Grainawie033b262012-05-09 17:54:21 -070099
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800100=== ApiType
Nasser Grainawie033b262012-05-09 17:54:21 -0700101
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700102Plugins using the tightly coupled `gerrit-plugin-api.jar` must
103declare this API dependency in the manifest to gain access to server
Edwin Kempin948de0f2012-07-16 10:34:35 +0200104internals. If no `Gerrit-ApiType` is specified the stable `extension`
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700105API will be assumed. This may cause ClassNotFoundExceptions when
106loading a plugin that needs the plugin API.
Nasser Grainawie033b262012-05-09 17:54:21 -0700107
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700108====
109 Gerrit-ApiType: plugin
110====
111
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800112=== Explicit Registration
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700113
114Plugins that use explicit Guice registration must name the Guice
115modules in the manifest. Up to three modules can be named in the
Edwin Kempin948de0f2012-07-16 10:34:35 +0200116manifest. `Gerrit-Module` supplies bindings to the core server;
117`Gerrit-SshModule` supplies SSH commands to the SSH server (if
118enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700119server (if enabled). If no modules are named automatic registration
120will be performed by scanning all classes in the plugin JAR for
121`@Listen` and `@Export("")` annotations.
122
123====
124 Gerrit-Module: tld.example.project.CoreModuleClassName
125 Gerrit-SshModule: tld.example.project.SshModuleClassName
126 Gerrit-HttpModule: tld.example.project.HttpModuleClassName
127====
128
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200129[[plugin_name]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800130=== Plugin Name
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200131
David Pursehoused128c892013-10-22 21:52:21 +0900132A plugin can optionally provide its own plugin name.
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200133
134====
135 Gerrit-PluginName: replication
136====
137
138This is useful for plugins that contribute plugin-owned capabilities that
139are stored in the `project.config` file. Another use case is to be able to put
140project specific plugin configuration section in `project.config`. In this
141case it is advantageous to reserve the plugin name to access the configuration
142section in the `project.config` file.
143
144If `Gerrit-PluginName` is omitted, then the plugin's name is determined from
145the plugin file name.
146
147If a plugin provides its own name, then that plugin cannot be deployed
148multiple times under different file names on one Gerrit site.
149
150For Maven driven plugins, the following line must be included in the pom.xml
151file:
152
153[source,xml]
154----
155<manifestEntries>
156 <Gerrit-PluginName>name</Gerrit-PluginName>
157</manifestEntries>
158----
159
160For Buck driven plugins, the following line must be included in the BUCK
161configuration file:
162
163[source,python]
164----
David Pursehouse529ec252013-09-27 13:45:14 +0900165manifest_entries = [
166 'Gerrit-PluginName: name',
167]
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200168----
169
Edwin Kempinc0b1b0e2013-10-01 14:13:54 +0200170A plugin can get its own name injected at runtime:
171
172[source,java]
173----
174public class MyClass {
175
176 private final String pluginName;
177
178 @Inject
179 public MyClass(@PluginName String pluginName) {
180 this.pluginName = pluginName;
181 }
182
David Pursehoused128c892013-10-22 21:52:21 +0900183 [...]
Edwin Kempinc0b1b0e2013-10-01 14:13:54 +0200184}
185----
186
David Pursehouse8ed0d922013-10-18 18:57:56 +0900187A plugin can get its canonical web URL injected at runtime:
188
189[source,java]
190----
191public class MyClass {
192
193 private final String url;
194
195 @Inject
196 public MyClass(@PluginCanonicalWebUrl String url) {
197 this.url = url;
198 }
199
200 [...]
201}
202----
203
204The URL is composed of the server's canonical web URL and the plugin's
205name, i.e. `http://review.example.com:8080/plugin-name`.
206
207The canonical web URL may be injected into any .jar plugin regardless of
208whether or not the plugin provides an HTTP servlet.
209
Edwin Kempinf7295742012-07-16 15:03:46 +0200210[[reload_method]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800211=== Reload Method
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700212
213If a plugin holds an exclusive resource that must be released before
214loading the plugin again (for example listening on a network port or
Edwin Kempin948de0f2012-07-16 10:34:35 +0200215acquiring a file lock) the manifest must declare `Gerrit-ReloadMode`
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700216to be `restart`. Otherwise the preferred method of `reload` will
217be used, as it enables the server to hot-patch an updated plugin
218with no down time.
219
220====
221 Gerrit-ReloadMode: restart
222====
223
224In either mode ('restart' or 'reload') any plugin or extension can
225be updated without restarting the Gerrit server. The difference is
226how Gerrit handles the upgrade:
227
228restart::
229 The old plugin is completely stopped. All registrations of SSH
230 commands and HTTP servlets are removed. All registrations of any
231 extension points are removed. All registered LifecycleListeners
232 have their `stop()` method invoked in reverse order. The new
233 plugin is started, and registrations are made from the new
234 plugin. There is a brief window where neither the old nor the
235 new plugin is connected to the server. This means SSH commands
236 and HTTP servlets will return not found errors, and the plugin
237 will not be notified of events that occurred during the restart.
238
239reload::
240 The new plugin is started. Its LifecycleListeners are permitted
241 to perform their `start()` methods. All SSH and HTTP registrations
242 are atomically swapped out from the old plugin to the new plugin,
243 ensuring the server never returns a not found error. All extension
244 point listeners are atomically swapped out from the old plugin to
245 the new plugin, ensuring no events are missed (however some events
246 may still route to the old plugin if the swap wasn't complete yet).
247 The old plugin is stopped.
248
Edwin Kempinf7295742012-07-16 15:03:46 +0200249To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload]
250command can be used.
251
Luca Milanesio737285d2012-09-25 14:26:43 +0100252[[init_step]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800253=== Init step
Luca Milanesio737285d2012-09-25 14:26:43 +0100254
255Plugins can contribute their own "init step" during the Gerrit init
256wizard. This is useful for guiding the Gerrit administrator through
David Pursehouse659860f2013-12-16 14:50:04 +0900257the settings needed by the plugin to work properly.
Luca Milanesio737285d2012-09-25 14:26:43 +0100258
259For instance plugins to integrate Jira issues to Gerrit changes may
260contribute their own "init step" to allow configuring the Jira URL,
261credentials and possibly verify connectivity to validate them.
262
263====
264 Gerrit-InitStep: tld.example.project.MyInitStep
265====
266
267MyInitStep needs to follow the standard Gerrit InitStep syntax
David Pursehouse92463562013-06-24 10:16:28 +0900268and behavior: writing to the console using the injected ConsoleUI
Luca Milanesio737285d2012-09-25 14:26:43 +0100269and accessing / changing configuration settings using Section.Factory.
270
271In addition to the standard Gerrit init injections, plugins receive
272the @PluginName String injection containing their own plugin name.
273
Edwin Kempind4cfac12013-11-27 11:22:34 +0100274During their initialization plugins may get access to the
275`project.config` file of the `All-Projects` project and they are able
276to store configuration parameters in it. For this a plugin `InitStep`
Jiří Engelthaler3033a0a2015-02-16 09:44:32 +0100277can get `com.google.gerrit.pgm.init.api.AllProjectsConfig` injected:
Edwin Kempind4cfac12013-11-27 11:22:34 +0100278
279[source,java]
280----
281 public class MyInitStep implements InitStep {
282 private final String pluginName;
283 private final ConsoleUI ui;
284 private final AllProjectsConfig allProjectsConfig;
285
286 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+
Martin Fick4c72aea2014-12-10 14:58:12 -0700383Allows to listen to events. These are the same
Edwin Kempin64059f52013-10-31 13:49:25 +0100384link:cmd-stream-events.html#events[events] that are also streamed by
385the link:cmd-stream-events.html[gerrit stream-events] command.
386
Edwin Kempin98202662013-09-18 16:03:03 +0200387* `com.google.gerrit.extensions.events.LifecycleListener`:
388+
Edwin Kempin3e7928a2013-12-03 07:39:00 +0100389Plugin start and stop
Edwin Kempin98202662013-09-18 16:03:03 +0200390
391* `com.google.gerrit.extensions.events.NewProjectCreatedListener`:
392+
393Project creation
394
395* `com.google.gerrit.extensions.events.ProjectDeletedListener`:
396+
397Project deletion
398
Edwin Kempinb27c9392013-11-19 13:12:43 +0100399* `com.google.gerrit.extensions.events.HeadUpdatedListener`:
400+
401Update of HEAD on a project
402
Stefan Lay310d77d2014-05-28 13:45:25 +0200403* `com.google.gerrit.extensions.events.UsageDataPublishedListener`:
404+
405Publication of usage data
406
Yang Zhenhui2659d422013-07-30 16:59:58 +0800407[[stream-events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800408== Sending Events to the Events Stream
Yang Zhenhui2659d422013-07-30 16:59:58 +0800409
410Plugins may send events to the events stream where consumers of
411Gerrit's `stream-events` ssh command will receive them.
412
413To send an event, the plugin must invoke one of the `postEvent`
414methods in the `ChangeHookRunner` class, passing an instance of
Martin Fick4c72aea2014-12-10 14:58:12 -0700415its own custom event class derived from
416`com.google.gerrit.server.events.Event`.
Yang Zhenhui2659d422013-07-30 16:59:58 +0800417
Martin Fick0aef6f12014-12-11 16:54:21 -0700418Plugins which define new Events should register them via the
419`com.google.gerrit.server.events.EventTypes.registerClass()`
420method. This will make the EventType known to the system.
Martin Fickf70c20a2014-12-11 17:03:15 -0700421Deserialzing events with the
422`com.google.gerrit.server.events.EventDeserializer` class requires
423that the event be registered in EventTypes.
Martin Fick0aef6f12014-12-11 16:54:21 -0700424
Edwin Kempin32737602014-01-23 09:04:58 +0100425[[validation]]
David Pursehouse91c5f5e2014-01-23 18:57:33 +0900426== Validation Listeners
Edwin Kempin32737602014-01-23 09:04:58 +0100427
428Certain operations in Gerrit can be validated by plugins by
429implementing the corresponding link:config-validation.html[listeners].
430
Saša Živkovec85a072014-01-28 10:08:25 +0100431[[receive-pack]]
432== Receive Pack Initializers
433
434Plugins may provide ReceivePack initializers which will be invoked
435by Gerrit just before a ReceivePack instance will be used. Usually,
436plugins will make use of the setXXX methods on the ReceivePack to
437set additional properties on it.
438
Saša Živkov626c7312014-02-24 17:15:08 +0100439[[post-receive-hook]]
440== Post Receive-Pack Hooks
441
442Plugins may register PostReceiveHook instances in order to get
443notified when JGit successfully receives a pack. This may be useful
444for those plugins which would like to monitor changes in Git
445repositories.
446
Hugo Arès572d5422014-06-17 14:22:03 -0400447[[pre-upload-hook]]
448== Pre Upload-Pack Hooks
449
450Plugins may register PreUploadHook instances in order to get
451notified when JGit is about to upload a pack. This may be useful
452for those plugins which would like to monitor usage in Git
453repositories.
454
Edwin Kempinf5a77332012-07-18 11:17:53 +0200455[[ssh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800456== SSH Commands
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700457
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700458Plugins may provide commands that can be accessed through the SSH
459interface (extensions do not have this option).
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700460
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700461Command implementations must extend the base class SshCommand:
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700462
David Pursehouse68153d72013-09-04 10:09:17 +0900463[source,java]
464----
465import com.google.gerrit.sshd.SshCommand;
David Ostrovskyb7d97752013-11-09 05:23:26 +0100466import com.google.gerrit.sshd.CommandMetaData;
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700467
Ian Bulle1a12202014-02-16 17:15:42 -0800468@CommandMetaData(name="print", description="Print hello command")
David Pursehouse68153d72013-09-04 10:09:17 +0900469class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800470 @Override
471 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900472 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700473 }
David Pursehouse68153d72013-09-04 10:09:17 +0900474}
475----
Nasser Grainawie033b262012-05-09 17:54:21 -0700476
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700477If no Guice modules are declared in the manifest, SSH commands may
Edwin Kempin948de0f2012-07-16 10:34:35 +0200478use auto-registration by providing an `@Export` annotation:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700479
David Pursehouse68153d72013-09-04 10:09:17 +0900480[source,java]
481----
482import com.google.gerrit.extensions.annotations.Export;
483import com.google.gerrit.sshd.SshCommand;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700484
David Pursehouse68153d72013-09-04 10:09:17 +0900485@Export("print")
486class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800487 @Override
488 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900489 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700490 }
David Pursehouse68153d72013-09-04 10:09:17 +0900491}
492----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700493
494If explicit registration is being used, a Guice module must be
495supplied to register the SSH command and declared in the manifest
496with the `Gerrit-SshModule` attribute:
497
David Pursehouse68153d72013-09-04 10:09:17 +0900498[source,java]
499----
500import com.google.gerrit.sshd.PluginCommandModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700501
David Pursehouse68153d72013-09-04 10:09:17 +0900502class MyCommands extends PluginCommandModule {
Ian Bulle1a12202014-02-16 17:15:42 -0800503 @Override
David Pursehouse68153d72013-09-04 10:09:17 +0900504 protected void configureCommands() {
David Ostrovskyb7d97752013-11-09 05:23:26 +0100505 command(PrintHello.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700506 }
David Pursehouse68153d72013-09-04 10:09:17 +0900507}
508----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700509
510For a plugin installed as name `helloworld`, the command implemented
511by PrintHello class will be available to users as:
512
513----
Keunhong Parka09a6f12012-07-10 14:45:02 -0600514$ ssh -p 29418 review.example.com helloworld print
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700515----
516
David Ostrovsky79c4d892014-03-15 13:52:46 +0100517[[multiple-commands]]
518=== Multiple Commands bound to one implementation
519
David Ostrovskye3172b32013-10-13 14:19:13 +0200520Multiple SSH commands can be bound to the same implementation class. For
521example a Gerrit Shell plugin can bind different shell commands to the same
522implementation class:
523
524[source,java]
525----
526public class SshShellModule extends PluginCommandModule {
527 @Override
528 protected void configureCommands() {
529 command("ls").to(ShellCommand.class);
530 command("ps").to(ShellCommand.class);
531 [...]
532 }
533}
534----
535
536With the possible implementation:
537
538[source,java]
539----
540public class ShellCommand extends SshCommand {
541 @Override
542 protected void run() throws UnloggedFailure {
543 String cmd = getName().substring(getPluginName().length() + 1);
544 ProcessBuilder proc = new ProcessBuilder(cmd);
545 Process cmd = proc.start();
546 [...]
547 }
548}
549----
550
551And the call:
552
553----
554$ ssh -p 29418 review.example.com shell ls
555$ ssh -p 29418 review.example.com shell ps
556----
557
David Ostrovsky79c4d892014-03-15 13:52:46 +0100558[[root-level-commands]]
559=== Root Level Commands
560
David Ostrovskyb7d97752013-11-09 05:23:26 +0100561Single command plugins are also supported. In this scenario plugin binds
562SSH command to its own name. `SshModule` must inherit from
563`SingleCommandPluginModule` class:
564
565[source,java]
566----
567public class SshModule extends SingleCommandPluginModule {
568 @Override
569 protected void configure(LinkedBindingBuilder<Command> b) {
570 b.to(ShellCommand.class);
571 }
572}
573----
574
575If the plugin above is deployed under sh.jar file in `$site/plugins`
David Pursehouse659860f2013-12-16 14:50:04 +0900576directory, generic commands can be called without specifying the
David Ostrovskyb7d97752013-11-09 05:23:26 +0100577actual SSH command. Note in the example below, that the called commands
578`ls` and `ps` was not explicitly bound:
579
580----
581$ ssh -p 29418 review.example.com sh ls
582$ ssh -p 29418 review.example.com sh ps
583----
584
Edwin Kempin78ca0942013-10-30 11:24:06 +0100585[[simple-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800586== Simple Configuration in `gerrit.config`
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200587
588In Gerrit, global configuration is stored in the `gerrit.config` file.
589If a plugin needs global configuration, this configuration should be
590stored in a `plugin` subsection in the `gerrit.config` file.
591
Edwin Kempinc9b68602013-10-30 09:32:43 +0100592This approach of storing the plugin configuration is only suitable for
593plugins that have a simple configuration that only consists of
594key-value pairs. With this approach it is not possible to have
595subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin78ca0942013-10-30 11:24:06 +0100596configuration need to store their configuration in their
597link:#configuration[own configuration file] where they can make use of
598subsections. On the other hand storing the plugin configuration in a
599'plugin' subsection in the `gerrit.config` file has the advantage that
600administrators have all configuration parameters in one file, instead
601of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100602
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200603To avoid conflicts with other plugins, it is recommended that plugins
604only use the `plugin` subsection with their own name. For example the
605`helloworld` plugin should store its configuration in the
606`plugin.helloworld` subsection:
607
608----
609[plugin "helloworld"]
610 language = Latin
611----
612
Sasa Zivkovacdf5332013-09-20 14:05:15 +0200613Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200614plugin can easily access its configuration and there is no need for a
615plugin to parse the `gerrit.config` file on its own:
616
617[source,java]
618----
David Pursehouse529ec252013-09-27 13:45:14 +0900619@Inject
620private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200621
David Pursehoused128c892013-10-22 21:52:21 +0900622[...]
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200623
Edwin Kempin122622d2013-10-29 16:45:44 +0100624String language = cfg.getFromGerritConfig("helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900625 .getString("language", "English");
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200626----
627
Edwin Kempin78ca0942013-10-30 11:24:06 +0100628[[configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800629== Configuration in own config file
Edwin Kempin78ca0942013-10-30 11:24:06 +0100630
631Plugins can store their configuration in an own configuration file.
632This makes sense if the plugin configuration is rather complex and
633requires the usage of subsections. Plugins that have a simple
634key-value pair configuration can store their configuration in a
635link:#simple-configuration[`plugin` subsection of the `gerrit.config`
636file].
637
638The plugin configuration file must be named after the plugin and must
639be located in the `etc` folder of the review site. For example a
640configuration file for a `default-reviewer` plugin could look like
641this:
642
643.$site_path/etc/default-reviewer.config
644----
645[branch "refs/heads/master"]
646 reviewer = Project Owners
647 reviewer = john.doe@example.com
648[match "file:^.*\.txt"]
649 reviewer = My Info Developers
650----
651
652Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
653plugin can easily access its configuration:
654
655[source,java]
656----
657@Inject
658private com.google.gerrit.server.config.PluginConfigFactory cfg;
659
660[...]
661
662String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer")
663 .getStringList("branch", "refs/heads/master", "reviewer");
664----
665
Edwin Kempin78ca0942013-10-30 11:24:06 +0100666
Edwin Kempin705f2842013-10-30 14:25:31 +0100667[[simple-project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800668== Simple Project Specific Configuration in `project.config`
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200669
670In Gerrit, project specific configuration is stored in the project's
671`project.config` file on the `refs/meta/config` branch. If a plugin
672needs configuration on project level (e.g. to enable its functionality
673only for certain projects), this configuration should be stored in a
674`plugin` subsection in the project's `project.config` file.
675
Edwin Kempinc9b68602013-10-30 09:32:43 +0100676This approach of storing the plugin configuration is only suitable for
677plugins that have a simple configuration that only consists of
678key-value pairs. With this approach it is not possible to have
679subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin705f2842013-10-30 14:25:31 +0100680configuration need to store their configuration in their
681link:#project-specific-configuration[own configuration file] where they
682can make use of subsections. On the other hand storing the plugin
683configuration in a 'plugin' subsection in the `project.config` file has
684the advantage that project owners have all configuration parameters in
685one file, instead of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100686
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200687To avoid conflicts with other plugins, it is recommended that plugins
688only use the `plugin` subsection with their own name. For example the
689`helloworld` plugin should store its configuration in the
690`plugin.helloworld` subsection:
691
692----
693 [plugin "helloworld"]
694 enabled = true
695----
696
697Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
698plugin can easily access its project specific configuration and there
699is no need for a plugin to parse the `project.config` file on its own:
700
701[source,java]
702----
David Pursehouse529ec252013-09-27 13:45:14 +0900703@Inject
704private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200705
David Pursehoused128c892013-10-22 21:52:21 +0900706[...]
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200707
Edwin Kempin122622d2013-10-29 16:45:44 +0100708boolean enabled = cfg.getFromProjectConfig(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900709 .getBoolean("enabled", false);
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200710----
711
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200712It is also possible to get missing configuration parameters inherited
713from the parent projects:
714
715[source,java]
716----
David Pursehouse529ec252013-09-27 13:45:14 +0900717@Inject
718private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200719
David Pursehoused128c892013-10-22 21:52:21 +0900720[...]
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200721
Edwin Kempin122622d2013-10-29 16:45:44 +0100722boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900723 .getBoolean("enabled", false);
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200724----
725
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200726Project owners can edit the project configuration by fetching the
727`refs/meta/config` branch, editing the `project.config` file and
728pushing the commit back.
729
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100730Plugin configuration values that are stored in the `project.config`
731file can be exposed in the ProjectInfoScreen to allow project owners
732to see and edit them from the UI.
733
734For this an instance of `ProjectConfigEntry` needs to be bound for each
735parameter. The export name must be a valid Git variable name. The
736variable name is case-insensitive, allows only alphanumeric characters
737and '-', and must start with an alphabetic character.
738
Edwin Kempina6c1c452013-11-28 16:55:22 +0100739The example below shows how the parameters `plugin.helloworld.enabled`
740and `plugin.helloworld.language` are bound to be editable from the
David Pursehousea1d633b2014-05-02 17:21:02 +0900741Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting"
Edwin Kempina6c1c452013-11-28 16:55:22 +0100742is provided as display name and the default value is set to `true`.
743For the parameter `plugin.helloworld.language` "Preferred Language"
744is provided as display name and "en" is set as default value.
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100745
746[source,java]
747----
748class Module extends AbstractModule {
749 @Override
750 protected void configure() {
751 bind(ProjectConfigEntry.class)
Edwin Kempina6c1c452013-11-28 16:55:22 +0100752 .annotatedWith(Exports.named("enabled"))
753 .toInstance(new ProjectConfigEntry("Enable Greeting", true));
754 bind(ProjectConfigEntry.class)
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100755 .annotatedWith(Exports.named("language"))
756 .toInstance(new ProjectConfigEntry("Preferred Language", "en"));
757 }
758}
759----
760
Edwin Kempinb64d3972013-11-17 18:55:48 +0100761By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins
762can be notified when this configuration parameter is updated on a
763project.
764
Edwin Kempin705f2842013-10-30 14:25:31 +0100765[[project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800766== Project Specific Configuration in own config file
Edwin Kempin705f2842013-10-30 14:25:31 +0100767
768Plugins can store their project specific configuration in an own
769configuration file in the projects `refs/meta/config` branch.
770This makes sense if the plugins project specific configuration is
771rather complex and requires the usage of subsections. Plugins that
772have a simple key-value pair configuration can store their project
773specific configuration in a link:#simple-project-specific-configuration[
774`plugin` subsection of the `project.config` file].
775
776The plugin configuration file in the `refs/meta/config` branch must be
777named after the plugin. For example a configuration file for a
778`default-reviewer` plugin could look like this:
779
780.default-reviewer.config
781----
782[branch "refs/heads/master"]
783 reviewer = Project Owners
784 reviewer = john.doe@example.com
785[match "file:^.*\.txt"]
786 reviewer = My Info Developers
787----
788
789Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
790plugin can easily access its project specific configuration:
791
792[source,java]
793----
794@Inject
795private com.google.gerrit.server.config.PluginConfigFactory cfg;
796
797[...]
798
799String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer")
800 .getStringList("branch", "refs/heads/master", "reviewer");
801----
802
Edwin Kempin762da382013-10-30 14:50:01 +0100803It is also possible to get missing configuration parameters inherited
804from the parent projects:
805
806[source,java]
807----
808@Inject
809private com.google.gerrit.server.config.PluginConfigFactory cfg;
810
811[...]
812
David Ostrovsky468e4c32014-03-22 06:05:35 -0700813String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer")
Edwin Kempin762da382013-10-30 14:50:01 +0100814 .getStringList("branch", "refs/heads/master", "reviewer");
815----
816
Edwin Kempin705f2842013-10-30 14:25:31 +0100817Project owners can edit the project configuration by fetching the
818`refs/meta/config` branch, editing the `<plugin-name>.config` file and
819pushing the commit back.
820
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800821== React on changes in project configuration
Edwin Kempina46b6c92013-12-04 21:05:24 +0100822
823If a plugin wants to react on changes in the project configuration, it
824can implement a `GitReferenceUpdatedListener` and filter on events for
825the `refs/meta/config` branch:
826
827[source,java]
828----
829public class MyListener implements GitReferenceUpdatedListener {
830
831 private final MetaDataUpdate.Server metaDataUpdateFactory;
832
833 @Inject
834 MyListener(MetaDataUpdate.Server metaDataUpdateFactory) {
835 this.metaDataUpdateFactory = metaDataUpdateFactory;
836 }
837
838 @Override
839 public void onGitReferenceUpdated(Event event) {
Edwin Kempina951ba52014-01-03 14:07:28 +0100840 if (event.getRefName().equals(RefNames.REFS_CONFIG)) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100841 Project.NameKey p = new Project.NameKey(event.getProjectName());
842 try {
Edwin Kempina951ba52014-01-03 14:07:28 +0100843 ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId());
844 ProjectConfig newCfg = parseConfig(p, event.getNewObjectId());
Edwin Kempina46b6c92013-12-04 21:05:24 +0100845
Edwin Kempina951ba52014-01-03 14:07:28 +0100846 if (oldCfg != null && newCfg != null
847 && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100848 // submit type has changed
849 ...
850 }
851 } catch (IOException | ConfigInvalidException e) {
852 ...
853 }
854 }
855 }
Edwin Kempina951ba52014-01-03 14:07:28 +0100856
857 private ProjectConfig parseConfig(Project.NameKey p, String idStr)
858 throws IOException, ConfigInvalidException, RepositoryNotFoundException {
859 ObjectId id = ObjectId.fromString(idStr);
860 if (ObjectId.zeroId().equals(id)) {
861 return null;
862 }
863 return ProjectConfig.read(metaDataUpdateFactory.create(p), id);
864 }
Edwin Kempina46b6c92013-12-04 21:05:24 +0100865}
866----
867
868
David Ostrovsky7066cc02013-06-15 14:46:23 +0200869[[capabilities]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800870== Plugin Owned Capabilities
David Ostrovsky7066cc02013-06-15 14:46:23 +0200871
872Plugins may provide their own capabilities and restrict usage of SSH
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200873commands or `UiAction` to the users who are granted those capabilities.
David Ostrovsky7066cc02013-06-15 14:46:23 +0200874
875Plugins define the capabilities by overriding the `CapabilityDefinition`
876abstract class:
877
David Pursehouse68153d72013-09-04 10:09:17 +0900878[source,java]
879----
880public class PrintHelloCapability extends CapabilityDefinition {
881 @Override
882 public String getDescription() {
883 return "Print Hello";
David Ostrovsky7066cc02013-06-15 14:46:23 +0200884 }
David Pursehouse68153d72013-09-04 10:09:17 +0900885}
886----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200887
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200888If no Guice modules are declared in the manifest, capability may
David Ostrovsky7066cc02013-06-15 14:46:23 +0200889use auto-registration by providing an `@Export` annotation:
890
David Pursehouse68153d72013-09-04 10:09:17 +0900891[source,java]
892----
893@Export("printHello")
894public class PrintHelloCapability extends CapabilityDefinition {
David Pursehoused128c892013-10-22 21:52:21 +0900895 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900896}
897----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200898
899Otherwise the capability must be bound in a plugin module:
900
David Pursehouse68153d72013-09-04 10:09:17 +0900901[source,java]
902----
903public class HelloWorldModule extends AbstractModule {
904 @Override
905 protected void configure() {
906 bind(CapabilityDefinition.class)
907 .annotatedWith(Exports.named("printHello"))
908 .to(PrintHelloCapability.class);
David Ostrovsky7066cc02013-06-15 14:46:23 +0200909 }
David Pursehouse68153d72013-09-04 10:09:17 +0900910}
911----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200912
913With a plugin-owned capability defined in this way, it is possible to restrict
David Ostrovskyf86bae52013-09-01 09:10:39 +0200914usage of an SSH command or `UiAction` to members of the group that were granted
David Ostrovsky7066cc02013-06-15 14:46:23 +0200915this capability in the usual way, using the `RequiresCapability` annotation:
916
David Pursehouse68153d72013-09-04 10:09:17 +0900917[source,java]
918----
919@RequiresCapability("printHello")
920@CommandMetaData(name="print", description="Print greeting in different languages")
921public final class PrintHelloWorldCommand extends SshCommand {
David Pursehoused128c892013-10-22 21:52:21 +0900922 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900923}
924----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200925
David Ostrovskyf86bae52013-09-01 09:10:39 +0200926Or with `UiAction`:
David Ostrovsky7066cc02013-06-15 14:46:23 +0200927
David Pursehouse68153d72013-09-04 10:09:17 +0900928[source,java]
929----
930@RequiresCapability("printHello")
931public class SayHelloAction extends UiAction<RevisionResource>
932 implements RestModifyView<RevisionResource, SayHelloAction.Input> {
David Pursehoused128c892013-10-22 21:52:21 +0900933 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900934}
935----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200936
937Capability scope was introduced to differentiate between plugin-owned
David Pursehousebf053342013-09-05 14:55:29 +0900938capabilities and core capabilities. Per default the scope of the
939`@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means:
940
David Ostrovsky7066cc02013-06-15 14:46:23 +0200941* when `@RequiresCapability` is used within a plugin the scope of the
942capability is assumed to be that plugin.
David Pursehousebf053342013-09-05 14:55:29 +0900943
David Ostrovsky7066cc02013-06-15 14:46:23 +0200944* If `@RequiresCapability` is used within the core Gerrit Code Review server
945(and thus is outside of a plugin) the scope is the core server and will use
946the `GlobalCapability` known to Gerrit Code Review server.
947
948If a plugin needs to use a core capability name (e.g. "administrateServer")
949this can be specified by setting `scope = CapabilityScope.CORE`:
950
David Pursehouse68153d72013-09-04 10:09:17 +0900951[source,java]
952----
953@RequiresCapability(value = "administrateServer", scope =
954 CapabilityScope.CORE)
David Pursehoused128c892013-10-22 21:52:21 +0900955 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900956----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200957
David Ostrovskyf86bae52013-09-01 09:10:39 +0200958[[ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800959== UI Extension
David Ostrovskyf86bae52013-09-01 09:10:39 +0200960
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100961Plugins can contribute UI actions on core Gerrit pages. This is useful
962for workflow customization or exposing plugin functionality through the
963UI in addition to SSH commands and the REST API.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200964
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100965For instance a plugin to integrate Jira with Gerrit changes may
966contribute a "File bug" button to allow filing a bug from the change
967page or plugins to integrate continuous integration systems may
968contribute a "Schedule" button to allow a CI build to be scheduled
969manually from the patch set panel.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200970
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100971Two different places on core Gerrit pages are supported:
David Ostrovskyf86bae52013-09-01 09:10:39 +0200972
973* Change screen
974* Project info screen
975
976Plugins contribute UI actions by implementing the `UiAction` interface:
977
David Pursehouse68153d72013-09-04 10:09:17 +0900978[source,java]
979----
980@RequiresCapability("printHello")
981class HelloWorldAction implements UiAction<RevisionResource>,
982 RestModifyView<RevisionResource, HelloWorldAction.Input> {
983 static class Input {
984 boolean french;
985 String message;
David Ostrovskyf86bae52013-09-01 09:10:39 +0200986 }
David Pursehouse68153d72013-09-04 10:09:17 +0900987
988 private Provider<CurrentUser> user;
989
990 @Inject
991 HelloWorldAction(Provider<CurrentUser> user) {
992 this.user = user;
993 }
994
995 @Override
996 public String apply(RevisionResource rev, Input input) {
997 final String greeting = input.french
998 ? "Bonjour"
999 : "Hello";
1000 return String.format("%s %s from change %s, patch set %d!",
1001 greeting,
1002 Strings.isNullOrEmpty(input.message)
1003 ? Objects.firstNonNull(user.get().getUserName(), "world")
1004 : input.message,
1005 rev.getChange().getId().toString(),
1006 rev.getPatchSet().getPatchSetId());
1007 }
1008
1009 @Override
1010 public Description getDescription(
1011 RevisionResource resource) {
1012 return new Description()
1013 .setLabel("Say hello")
1014 .setTitle("Say hello in different languages");
1015 }
1016}
1017----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001018
David Ostrovsky450eefe2013-10-21 21:18:11 +02001019Sometimes plugins may want to be able to change the state of a patch set or
1020change in the `UiAction.apply()` method and reflect these changes on the core
1021UI. For example a buildbot plugin which exposes a 'Schedule' button on the
1022patch set panel may want to disable that button after the build was scheduled
1023and update the tooltip of that button. But because of Gerrit's caching
1024strategy the following must be taken into consideration.
1025
1026The browser is allowed to cache the `UiAction` information until something on
1027the change is modified. More accurately the change row needs to be modified in
1028the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or
1029the +refs/meta/config+ of the project or any parents needs to change to a new
1030SHA-1. The ETag SHA-1 computation code can be found in the
1031`ChangeResource.getETag()` method.
1032
David Pursehoused128c892013-10-22 21:52:21 +09001033The easiest way to accomplish this is to update `lastUpdatedOn` of the change:
David Ostrovsky450eefe2013-10-21 21:18:11 +02001034
1035[source,java]
1036----
1037@Override
1038public Object apply(RevisionResource rcrs, Input in) {
1039 // schedule a build
1040 [...]
1041 // update change
1042 ReviewDb db = dbProvider.get();
1043 db.changes().beginTransaction(change.getId());
1044 try {
1045 change = db.changes().atomicUpdate(
1046 change.getId(),
1047 new AtomicUpdate<Change>() {
1048 @Override
1049 public Change update(Change change) {
1050 ChangeUtil.updated(change);
1051 return change;
1052 }
1053 });
1054 db.commit();
1055 } finally {
1056 db.rollback();
1057 }
David Pursehoused128c892013-10-22 21:52:21 +09001058 [...]
David Ostrovsky450eefe2013-10-21 21:18:11 +02001059}
1060----
1061
David Ostrovskyf86bae52013-09-01 09:10:39 +02001062`UiAction` must be bound in a plugin module:
1063
David Pursehouse68153d72013-09-04 10:09:17 +09001064[source,java]
1065----
1066public class Module extends AbstractModule {
1067 @Override
1068 protected void configure() {
1069 install(new RestApiModule() {
1070 @Override
1071 protected void configure() {
1072 post(REVISION_KIND, "say-hello")
1073 .to(HelloWorldAction.class);
1074 }
1075 });
David Ostrovskyf86bae52013-09-01 09:10:39 +02001076 }
David Pursehouse68153d72013-09-04 10:09:17 +09001077}
1078----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001079
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001080The module above must be declared in the `pom.xml` for Maven driven
1081plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001082
David Pursehouse68153d72013-09-04 10:09:17 +09001083[source,xml]
1084----
1085<manifestEntries>
1086 <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module>
1087</manifestEntries>
1088----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001089
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001090or in the `BUCK` configuration file for Buck driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001091
David Pursehouse68153d72013-09-04 10:09:17 +09001092[source,python]
1093----
1094manifest_entries = [
1095 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module',
1096]
1097----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001098
1099In some use cases more user input must be gathered, for that `UiAction` can be
1100combined with the JavaScript API. This would display a small popup near the
1101activation button to gather additional input from the user. The JS file is
1102typically put in the `static` folder within the plugin's directory:
1103
David Pursehouse68153d72013-09-04 10:09:17 +09001104[source,javascript]
1105----
1106Gerrit.install(function(self) {
1107 function onSayHello(c) {
1108 var f = c.textfield();
1109 var t = c.checkbox();
1110 var b = c.button('Say hello', {onclick: function(){
1111 c.call(
1112 {message: f.value, french: t.checked},
1113 function(r) {
1114 c.hide();
1115 window.alert(r);
1116 c.refresh();
1117 });
1118 }});
1119 c.popup(c.div(
1120 c.prependLabel('Greeting message', f),
1121 c.br(),
1122 c.label(t, 'french'),
1123 c.br(),
1124 b));
1125 f.focus();
1126 }
1127 self.onAction('revision', 'say-hello', onSayHello);
1128});
1129----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001130
1131The JS module must be exposed as a `WebUiPlugin` and bound as
1132an HTTP Module:
1133
David Pursehouse68153d72013-09-04 10:09:17 +09001134[source,java]
1135----
1136public class HttpModule extends HttpPluginModule {
1137 @Override
1138 protected void configureServlets() {
1139 DynamicSet.bind(binder(), WebUiPlugin.class)
1140 .toInstance(new JavaScriptPlugin("hello.js"));
David Ostrovskyf86bae52013-09-01 09:10:39 +02001141 }
David Pursehouse68153d72013-09-04 10:09:17 +09001142}
1143----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001144
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001145The HTTP module above must be declared in the `pom.xml` for Maven
1146driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001147
David Pursehouse68153d72013-09-04 10:09:17 +09001148[source,xml]
1149----
1150<manifestEntries>
1151 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule>
1152</manifestEntries>
1153----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001154
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001155or in the `BUCK` configuration file for Buck driven plugins
David Ostrovskyf86bae52013-09-01 09:10:39 +02001156
David Pursehouse68153d72013-09-04 10:09:17 +09001157[source,python]
1158----
1159manifest_entries = [
1160 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule',
1161]
1162----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001163
1164If `UiAction` is annotated with the `@RequiresCapability` annotation, then the
1165capability check is done during the `UiAction` gathering, so the plugin author
1166doesn't have to set `UiAction.Description.setVisible()` explicitly in this
1167case.
1168
1169The following prerequisities must be met, to satisfy the capability check:
1170
1171* user is authenticated
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001172* user is a member of a group which has the `Administrate Server` capability, or
David Ostrovskyf86bae52013-09-01 09:10:39 +02001173* user is a member of a group which has the required capability
1174
1175The `apply` method is called when the button is clicked. If `UiAction` is
1176combined with JavaScript API (its own JavaScript function is provided),
1177then a popup dialog is normally opened to gather additional user input.
1178A new button is placed on the popup dialog to actually send the request.
1179
1180Every `UiAction` exposes a REST API endpoint. The endpoint from the example above
1181can be accessed from any REST client, i. e.:
1182
1183====
1184 curl -X POST -H "Content-Type: application/json" \
1185 -d '{message: "François", french: true}' \
1186 --digest --user joe:secret \
1187 http://host:port/a/changes/1/revisions/1/cookbook~say-hello
1188 "Bonjour François from change 1, patch set 1!"
1189====
1190
David Pursehouse42245822013-09-24 09:48:20 +09001191A special case is to bind an endpoint without a view name. This is
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001192particularly useful for `DELETE` requests:
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001193
1194[source,java]
1195----
1196public class Module extends AbstractModule {
1197 @Override
1198 protected void configure() {
1199 install(new RestApiModule() {
1200 @Override
1201 protected void configure() {
1202 delete(PROJECT_KIND)
1203 .to(DeleteProject.class);
1204 }
1205 });
1206 }
1207}
1208----
1209
David Pursehouse42245822013-09-24 09:48:20 +09001210For a `UiAction` bound this way, a JS API function can be provided.
1211
1212Currently only one restriction exists: per plugin only one `UiAction`
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001213can be bound per resource without view name. To define a JS function
1214for the `UiAction`, "/" must be used as the name:
1215
1216[source,javascript]
1217----
1218Gerrit.install(function(self) {
1219 function onDeleteProject(c) {
1220 [...]
1221 }
1222 self.onAction('project', '/', onDeleteProject);
1223});
1224----
1225
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001226[[top-menu-extensions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001227== Top Menu Extensions
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001228
1229Plugins can contribute items to Gerrit's top menu.
1230
1231A single top menu extension can have multiple elements and will be put as
1232the last element in Gerrit's top menu.
1233
1234Plugins define the top menu entries by implementing `TopMenu` interface:
1235
1236[source,java]
1237----
1238public class MyTopMenuExtension implements TopMenu {
1239
1240 @Override
1241 public List<MenuEntry> getEntries() {
1242 return Lists.newArrayList(
1243 new MenuEntry("Top Menu Entry", Lists.newArrayList(
1244 new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1245 }
1246}
1247----
1248
Edwin Kempin77f23242013-09-30 14:53:20 +02001249Plugins can also add additional menu items to Gerrit's top menu entries
1250by defining a `MenuEntry` that has the same name as a Gerrit top menu
1251entry:
1252
1253[source,java]
1254----
1255public class MyTopMenuExtension implements TopMenu {
1256
1257 @Override
1258 public List<MenuEntry> getEntries() {
1259 return Lists.newArrayList(
Dariusz Luksza2d3afab2013-10-01 11:07:13 +02001260 new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList(
Edwin Kempin77f23242013-09-30 14:53:20 +02001261 new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
1262 }
1263}
1264----
1265
Dariusz Lukszae8de74f2014-06-04 19:39:20 +02001266`MenuItems` that are bound for the `MenuEntry` with the name
1267`GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder
1268which is automatically replaced by the actual project name.
1269
1270E.g. plugins may register an link:#http[HTTP Servlet] to handle project
1271specific requests and add an menu item for this:
1272
1273[source,java]
1274---
1275 new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}");
1276---
1277
1278This also enables plugins to provide menu items for project aware
1279screens:
1280
1281[source,java]
1282---
1283 new MenuItem("My Screen", "/x/my-screen/for/${projectName}");
1284---
1285
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001286If no Guice modules are declared in the manifest, the top menu extension may use
1287auto-registration by providing an `@Listen` annotation:
1288
1289[source,java]
1290----
1291@Listen
1292public class MyTopMenuExtension implements TopMenu {
David Pursehoused128c892013-10-22 21:52:21 +09001293 [...]
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001294}
1295----
1296
Luca Milanesiocb230402013-10-11 08:49:56 +01001297Otherwise the top menu extension must be bound in the plugin module used
1298for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF):
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001299
1300[source,java]
1301----
Luca Milanesiocb230402013-10-11 08:49:56 +01001302package com.googlesource.gerrit.plugins.helloworld;
1303
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001304public class HelloWorldModule extends AbstractModule {
1305 @Override
1306 protected void configure() {
1307 DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class);
1308 }
1309}
1310----
1311
Luca Milanesiocb230402013-10-11 08:49:56 +01001312[source,manifest]
1313----
1314Gerrit-ApiType: plugin
1315Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule
1316----
1317
Edwin Kempinb2e926a2013-11-11 16:38:30 +01001318It is also possible to show some menu entries only if the user has a
1319certain capability:
1320
1321[source,java]
1322----
1323public class MyTopMenuExtension implements TopMenu {
1324 private final String pluginName;
1325 private final Provider<CurrentUser> userProvider;
1326 private final List<MenuEntry> menuEntries;
1327
1328 @Inject
1329 public MyTopMenuExtension(@PluginName String pluginName,
1330 Provider<CurrentUser> userProvider) {
1331 this.pluginName = pluginName;
1332 this.userProvider = userProvider;
1333 menuEntries = new ArrayList<TopMenu.MenuEntry>();
1334
1335 // add menu entry that is only visible to users with a certain capability
1336 if (canSeeMenuEntry()) {
1337 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1338 .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1339 }
1340
1341 // add menu entry that is visible to all users (even anonymous users)
1342 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1343 .singletonList(new MenuItem("Documentation", "/plugins/myplugin/"))));
1344 }
1345
1346 private boolean canSeeMenuEntry() {
1347 if (userProvider.get().isIdentifiedUser()) {
1348 CapabilityControl ctl = userProvider.get().getCapabilities();
1349 return ctl.canPerform(pluginName + "-" + MyCapability.ID)
1350 || ctl.canAdministrateServer();
1351 } else {
1352 return false;
1353 }
1354 }
1355
1356 @Override
1357 public List<MenuEntry> getEntries() {
1358 return menuEntries;
1359 }
1360}
1361----
1362
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001363[[gwt_ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001364== GWT UI Extension
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001365Plugins can extend the Gerrit UI with own GWT code.
1366
1367The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to
1368generate a GWT plugin skeleton. How to use the Maven plugin archetypes
1369is described in the link:#getting-started[Getting started] section.
1370
1371The generated GWT plugin has a link:#top-menu-extensions[top menu] that
1372opens a GWT dialog box when the user clicks on it.
1373
Edwin Kempinb74daa92013-11-11 11:28:16 +01001374In addition to the Gerrit-Plugin API a GWT plugin depends on
1375`gerrit-plugin-gwtui`. This dependency must be specified in the
1376`pom.xml`:
1377
1378[source,xml]
1379----
1380<dependency>
1381 <groupId>com.google.gerrit</groupId>
1382 <artifactId>gerrit-plugin-gwtui</artifactId>
1383 <version>${Gerrit-ApiVersion}</version>
1384</dependency>
1385----
1386
1387A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`,
1388that bundles together all the configuration settings of the GWT plugin:
1389
1390[source,xml]
1391----
1392<?xml version="1.0" encoding="UTF-8"?>
1393<module rename-to="hello_gwt_plugin">
1394 <!-- Inherit the core Web Toolkit stuff. -->
1395 <inherits name="com.google.gwt.user.User"/>
1396 <!-- Other module inherits -->
1397 <inherits name="com.google.gerrit.Plugin"/>
1398 <inherits name="com.google.gwt.http.HTTP"/>
1399 <!-- Using GWT built-in themes adds a number of static -->
1400 <!-- resources to the plugin. No theme inherits lines were -->
1401 <!-- added in order to make this plugin as simple as possible -->
1402 <!-- Specify the app entry point class. -->
1403 <entry-point class="${package}.client.HelloPlugin"/>
1404 <stylesheet src="hello.css"/>
1405</module>
1406----
1407
1408The GWT module must inherit `com.google.gerrit.Plugin` and
1409`com.google.gwt.http.HTTP`.
1410
1411To register the GWT module a `GwtPlugin` needs to be bound.
1412
1413If no Guice modules are declared in the manifest, the GWT plugin may
1414use auto-registration by using the `@Listen` annotation:
1415
1416[source,java]
1417----
1418@Listen
1419public class MyExtension extends GwtPlugin {
1420 public MyExtension() {
1421 super("hello_gwt_plugin");
1422 }
1423}
1424----
1425
1426Otherwise the binding must be done in an `HttpModule`:
1427
1428[source,java]
1429----
1430public class HttpModule extends HttpPluginModule {
1431
1432 @Override
1433 protected void configureServlets() {
1434 DynamicSet.bind(binder(), WebUiPlugin.class)
1435 .toInstance(new GwtPlugin("hello_gwt_plugin"));
1436 }
1437}
1438----
1439
1440The HTTP module above must be declared in the `pom.xml` for Maven
1441driven plugins:
1442
1443[source,xml]
1444----
1445<manifestEntries>
1446 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule>
1447</manifestEntries>
1448----
1449
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001450The name that is provided to the `GwtPlugin` must match the GWT
1451module name compiled into the plugin. The name of the GWT module
1452can be explicitly set in the GWT module XML file by specifying
1453the `rename-to` attribute on the module. It is important that the
1454module name be unique across all plugins installed on the server,
1455as the module name determines the JavaScript namespace used by the
1456compiled plugin code.
Edwin Kempinb74daa92013-11-11 11:28:16 +01001457
1458[source,xml]
1459----
1460<module rename-to="hello_gwt_plugin">
1461----
1462
1463The actual GWT code must be implemented in a class that extends
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001464`com.google.gerrit.plugin.client.PluginEntryPoint`:
Edwin Kempinb74daa92013-11-11 11:28:16 +01001465
1466[source,java]
1467----
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001468public class HelloPlugin extends PluginEntryPoint {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001469
1470 @Override
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001471 public void onPluginLoad() {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001472 // Create the dialog box
1473 final DialogBox dialogBox = new DialogBox();
1474
1475 // The content of the dialog comes from a User specified Preference
1476 dialogBox.setText("Hello from GWT Gerrit UI plugin");
1477 dialogBox.setAnimationEnabled(true);
1478 Button closeButton = new Button("Close");
1479 VerticalPanel dialogVPanel = new VerticalPanel();
1480 dialogVPanel.setWidth("100%");
1481 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
1482 dialogVPanel.add(closeButton);
1483
1484 closeButton.addClickHandler(new ClickHandler() {
1485 public void onClick(ClickEvent event) {
1486 dialogBox.hide();
1487 }
1488 });
1489
1490 // Set the contents of the Widget
1491 dialogBox.setWidget(dialogVPanel);
1492
1493 RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1494 rootPanel.getElement().removeAttribute("href");
1495 rootPanel.addDomHandler(new ClickHandler() {
1496 @Override
1497 public void onClick(ClickEvent event) {
1498 dialogBox.center();
1499 dialogBox.show();
1500 }
1501 }, ClickEvent.getType());
1502 }
1503}
1504----
1505
1506This class must be set as entry point in the GWT module:
1507
1508[source,xml]
1509----
1510<entry-point class="${package}.client.HelloPlugin"/>
1511----
1512
1513In addition this class must be defined as module in the `pom.xml` for the
1514`gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin`
1515must be set to `${project.build.directory}/classes/static`:
1516
1517[source,xml]
1518----
1519<plugin>
1520 <groupId>org.codehaus.mojo</groupId>
1521 <artifactId>gwt-maven-plugin</artifactId>
1522 <version>2.5.1</version>
1523 <configuration>
1524 <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module>
1525 <disableClassMetadata>true</disableClassMetadata>
1526 <disableCastChecking>true</disableCastChecking>
1527 <webappDirectory>${project.build.directory}/classes/static</webappDirectory>
1528 </configuration>
1529 <executions>
1530 <execution>
1531 <goals>
1532 <goal>compile</goal>
1533 </goals>
1534 </execution>
1535 </executions>
1536</plugin>
1537----
1538
1539To attach a GWT widget defined by the plugin to the Gerrit core UI
1540`com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the
1541Gerrit core widgets:
1542
1543[source,java]
1544----
1545RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1546rootPanel.getElement().removeAttribute("href");
1547rootPanel.addDomHandler(new ClickHandler() {
1548 @Override
1549 public void onClick(ClickEvent event) {
1550 dialogBox.center();
1551 dialogBox.show();
1552 }
1553}, ClickEvent.getType());
1554----
1555
1556GWT plugins can come with their own css file. This css file must have a
1557unique name and must be registered in the GWT module:
1558
1559[source,xml]
1560----
1561<stylesheet src="hello.css"/>
1562----
1563
Edwin Kempin2570b102013-11-11 11:44:50 +01001564If a GWT plugin wants to invoke the Gerrit REST API it can use
David Pursehouse3a388312014-02-25 16:41:47 +09001565`com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL
Edwin Kempin2570b102013-11-11 11:44:50 +01001566path and to trigger the REST calls.
1567
1568Example for invoking a Gerrit core REST endpoint:
1569
1570[source,java]
1571----
1572new RestApi("projects").id(projectName).view("description")
1573 .put("new description", new AsyncCallback<JavaScriptObject>() {
1574
1575 @Override
1576 public void onSuccess(JavaScriptObject result) {
1577 // TODO
1578 }
1579
1580 @Override
1581 public void onFailure(Throwable caught) {
1582 // never invoked
1583 }
1584});
1585----
1586
1587Example for invoking a REST endpoint defined by a plugin:
1588
1589[source,java]
1590----
1591new RestApi("projects").id(projectName).view("myplugin", "myview")
1592 .get(new AsyncCallback<JavaScriptObject>() {
1593
1594 @Override
1595 public void onSuccess(JavaScriptObject result) {
1596 // TODO
1597 }
1598
1599 @Override
1600 public void onFailure(Throwable caught) {
1601 // never invoked
1602 }
1603});
1604----
1605
1606The `onFailure(Throwable)` of the provided callback is never invoked.
1607If an error occurs, it is shown in an error dialog.
1608
1609In order to be able to do REST calls the GWT module must inherit
1610`com.google.gwt.json.JSON`:
1611
1612[source,xml]
1613----
1614<inherits name="com.google.gwt.json.JSON"/>
1615----
1616
Edwin Kempin15199792014-04-23 16:22:05 +02001617[[screen]]
Shawn Pearced5c844f2013-12-26 15:32:26 -08001618== Add Screen
Edwin Kempin15199792014-04-23 16:22:05 +02001619A link:#gwt_ui_extension[GWT plugin] can link:#top-menu-extensions[add
1620a menu item] that opens a screen that is implemented by the plugin.
1621This way plugin screens can be fully integrated into the Gerrit UI.
Shawn Pearced5c844f2013-12-26 15:32:26 -08001622
1623Example menu item:
1624[source,java]
1625----
1626public class MyMenu implements TopMenu {
1627 private final List<MenuEntry> menuEntries;
1628
1629 @Inject
1630 public MyMenu(@PluginName String name) {
1631 menuEntries = Lists.newArrayList();
1632 menuEntries.add(new MenuEntry("My Menu", Collections.singletonList(
1633 new MenuItem("My Screen", "#/x/" + name + "/my-screen", ""))));
1634 }
1635
1636 @Override
1637 public List<MenuEntry> getEntries() {
1638 return menuEntries;
1639 }
1640}
1641----
1642
1643Example screen:
1644[source,java]
1645----
1646public class MyPlugin extends PluginEntryPoint {
1647 @Override
1648 public void onPluginLoad() {
1649 Plugin.get().screen("my-screen", new Screen.EntryPoint() {
1650 @Override
1651 public void onLoad(Screen screen) {
1652 screen.add(new InlineLabel("My Screen");
1653 screen.show();
1654 }
1655 });
1656 }
1657}
1658----
1659
Edwin Kempin289f1a02014-02-04 16:08:25 +01001660[[settings-screen]]
1661== Plugin Settings Screen
1662
1663If a plugin implements a screen for administrating its settings that is
1664available under "#/x/<plugin-name>/settings" it is automatically linked
1665from the plugin list screen.
1666
Edwin Kempinf5a77332012-07-18 11:17:53 +02001667[[http]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001668== HTTP Servlets
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001669
1670Plugins or extensions may register additional HTTP servlets, and
1671wrap them with HTTP filters.
1672
1673Servlets may use auto-registration to declare the URL they handle:
1674
David Pursehouse68153d72013-09-04 10:09:17 +09001675[source,java]
1676----
1677import com.google.gerrit.extensions.annotations.Export;
1678import com.google.inject.Singleton;
1679import javax.servlet.http.HttpServlet;
1680import javax.servlet.http.HttpServletRequest;
1681import javax.servlet.http.HttpServletResponse;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001682
David Pursehouse68153d72013-09-04 10:09:17 +09001683@Export("/print")
1684@Singleton
1685class HelloServlet extends HttpServlet {
1686 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
1687 res.setContentType("text/plain");
1688 res.setCharacterEncoding("UTF-8");
1689 res.getWriter().write("Hello");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001690 }
David Pursehouse68153d72013-09-04 10:09:17 +09001691}
1692----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001693
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001694The auto registration only works for standard servlet mappings like
1695`/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule
1696to register the HTTP servlets and declare it explicitly in the manifest
1697with the `Gerrit-HttpModule` attribute:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001698
David Pursehouse68153d72013-09-04 10:09:17 +09001699[source,java]
1700----
1701import com.google.inject.servlet.ServletModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001702
David Pursehouse68153d72013-09-04 10:09:17 +09001703class MyWebUrls extends ServletModule {
1704 protected void configureServlets() {
1705 serve("/print").with(HelloServlet.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001706 }
David Pursehouse68153d72013-09-04 10:09:17 +09001707}
1708----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001709
1710For a plugin installed as name `helloworld`, the servlet implemented
1711by HelloServlet class will be available to users as:
1712
1713----
1714$ curl http://review.example.com/plugins/helloworld/print
1715----
Nasser Grainawie033b262012-05-09 17:54:21 -07001716
Edwin Kempinf5a77332012-07-18 11:17:53 +02001717[[data-directory]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001718== Data Directory
Edwin Kempin41f63912012-07-17 12:33:55 +02001719
Dave Borowitz9e158752015-02-24 10:17:04 -08001720Plugins can request a data directory with a `@PluginData` Path (or File,
1721deprecated) dependency. A data directory will be created automatically
1722by the server in `$site_path/data/$plugin_name` and passed to the
1723plugin.
Edwin Kempin41f63912012-07-17 12:33:55 +02001724
1725Plugins can use this to store any data they want.
1726
David Pursehouse68153d72013-09-04 10:09:17 +09001727[source,java]
1728----
1729@Inject
Dave Borowitz9e158752015-02-24 10:17:04 -08001730MyType(@PluginData java.nio.file.Path myDir) {
1731 this.in = Files.newInputStream(myDir.resolve("my.config"));
David Pursehouse68153d72013-09-04 10:09:17 +09001732}
1733----
Edwin Kempin41f63912012-07-17 12:33:55 +02001734
Dariusz Lukszaebab92a2014-09-10 11:14:19 +02001735[[secure-store]]
1736== SecureStore
1737
1738SecureStore allows to change the way Gerrit stores sensitive data like
1739passwords.
1740
1741In order to replace the default SecureStore (no-op) implementation,
1742a class that extends `com.google.gerrit.server.securestore.SecureStore`
1743needs to be provided (with dependencies) in a separate jar file. Then
1744link:pgm-SwitchSecureStore.html[SwitchSecureStore] must be run to
1745switch implementations.
1746
1747The SecureStore implementation is instantiated using a Guice injector
1748which binds the `File` annotated with the `@SitePath` annotation.
1749This means that a SecureStore implementation class can get access to
1750the `site_path` like in the following example:
1751
1752[source,java]
1753----
1754@Inject
1755MySecureStore(@SitePath java.io.File sitePath) {
1756 // your code
1757}
1758----
1759
1760No Guice bindings or modules are required. Gerrit will automatically
1761discover and bind the implementation.
1762
Edwin Kempinea621482013-10-16 12:58:24 +02001763[[download-commands]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001764== Download Commands
Edwin Kempinea621482013-10-16 12:58:24 +02001765
1766Gerrit offers commands for downloading changes using different
1767download schemes (e.g. for downloading via different network
1768protocols). Plugins can contribute download schemes and download
1769commands by implementing
1770`com.google.gerrit.extensions.config.DownloadScheme` and
1771`com.google.gerrit.extensions.config.DownloadCommand`.
1772
1773The download schemes and download commands which are used most often
1774are provided by the Gerrit core plugin `download-commands`.
1775
Sven Selbergae1a10c2014-02-14 14:24:29 +01001776[[links-to-external-tools]]
1777== Links To External Tools
1778
1779Gerrit has extension points that enables development of a
1780light-weight plugin that links commits to external
1781tools (GitBlit, CGit, company specific resources etc).
1782
1783PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI.
1784
1785[source, java]
1786----
1787import com.google.gerrit.extensions.annotations.Listen;
1788import com.google.gerrit.extensions.webui.PatchSetWebLink;;
Sven Selberga85e64d2014-09-24 10:52:21 +02001789import com.google.gerrit.extensions.webui.WebLinkTarget;
Sven Selbergae1a10c2014-02-14 14:24:29 +01001790
1791@Listen
1792public class MyWeblinkPlugin implements PatchSetWebLink {
1793
1794 private String name = "MyLink";
1795 private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s";
Sven Selberg55484202014-06-26 08:48:51 +02001796 private String imageUrl = "http://placehold.it/16x16.gif";
Sven Selbergae1a10c2014-02-14 14:24:29 +01001797
1798 @Override
Sven Selberga85e64d2014-09-24 10:52:21 +02001799 public WebLinkInfo getPathSetWebLink(String projectName, String commit) {
1800 return new WebLinkInfo(name,
1801 imageUrl,
1802 String.format(placeHolderUrlProjectCommit, project, commit),
1803 WebLinkTarget.BLANK);
Edwin Kempinceeed6b2014-09-11 17:07:33 +02001804 }
Sven Selbergae1a10c2014-02-14 14:24:29 +01001805}
1806----
1807
Edwin Kempinb3696c82014-09-11 09:41:42 +02001808FileWebLinks will appear in the side-by-side diff screen on the right
1809side of the patch selection on each side.
1810
Edwin Kempin8cdce502014-12-06 10:55:38 +01001811DiffWebLinks will appear in the side-by-side and unified diff screen in
1812the header next to the navigation icons.
1813
Edwin Kempinea004752014-04-11 15:56:02 +02001814ProjectWebLinks will appear in the project list in the
1815`Repository Browser` column.
1816
Edwin Kempin0f697bd2014-09-10 18:23:29 +02001817BranchWebLinks will appear in the branch list in the last column.
1818
Edwin Kempinf5a77332012-07-18 11:17:53 +02001819[[documentation]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001820== Documentation
Nasser Grainawie033b262012-05-09 17:54:21 -07001821
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001822If a plugin does not register a filter or servlet to handle URLs
1823`/Documentation/*` or `/static/*`, the core Gerrit server will
1824automatically export these resources over HTTP from the plugin JAR.
1825
David Pursehouse6853b5a2013-07-10 11:38:03 +09001826Static resources under the `static/` directory in the JAR will be
Dave Borowitzb893ac82013-03-27 10:03:55 -04001827available as `/plugins/helloworld/static/resource`. This prefix is
1828configurable by setting the `Gerrit-HttpStaticPrefix` attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001829
David Pursehouse6853b5a2013-07-10 11:38:03 +09001830Documentation files under the `Documentation/` directory in the JAR
Dave Borowitzb893ac82013-03-27 10:03:55 -04001831will be available as `/plugins/helloworld/Documentation/resource`. This
1832prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix`
1833attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001834
1835Documentation may be written in
1836link:http://daringfireball.net/projects/markdown/[Markdown] style
1837if the file name ends with `.md`. Gerrit will automatically convert
1838Markdown to HTML if accessed with extension `.html`.
Nasser Grainawie033b262012-05-09 17:54:21 -07001839
Edwin Kempinf5a77332012-07-18 11:17:53 +02001840[[macros]]
Edwin Kempinc78777d2012-07-16 15:55:11 +02001841Within the Markdown documentation files macros can be used that allow
1842to write documentation with reasonably accurate examples that adjust
1843automatically based on the installation.
1844
1845The following macros are supported:
1846
1847[width="40%",options="header"]
1848|===================================================
1849|Macro | Replacement
1850|@PLUGIN@ | name of the plugin
1851|@URL@ | Gerrit Web URL
1852|@SSH_HOST@ | SSH Host
1853|@SSH_PORT@ | SSH Port
1854|===================================================
1855
1856The macros will be replaced when the documentation files are rendered
1857from Markdown to HTML.
1858
1859Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@`
1860even if there is an expansion for `KEEP` in the future.
1861
Edwin Kempinf5a77332012-07-18 11:17:53 +02001862[[auto-index]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001863=== Automatic Index
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001864
1865If a plugin does not handle its `/` URL itself, Gerrit will
1866redirect clients to the plugin's `/Documentation/index.html`.
1867Requests for `/Documentation/` (bare directory) will also redirect
1868to `/Documentation/index.html`.
1869
1870If neither resource `Documentation/index.html` or
1871`Documentation/index.md` exists in the plugin JAR, Gerrit will
1872automatically generate an index page for the plugin's documentation
1873tree by scanning every `*.md` and `*.html` file in the Documentation/
1874directory.
1875
1876For any discovered Markdown (`*.md`) file, Gerrit will parse the
1877header of the file and extract the first level one title. This
1878title text will be used as display text for a link to the HTML
1879version of the page.
1880
1881For any discovered HTML (`*.html`) file, Gerrit will use the name
1882of the file, minus the `*.html` extension, as the link text. Any
1883hyphens in the file name will be replaced with spaces.
1884
David Pursehouse6853b5a2013-07-10 11:38:03 +09001885If a discovered file is named `about.md` or `about.html`, its
1886content will be inserted in an 'About' section at the top of the
1887auto-generated index page. If both `about.md` and `about.html`
1888exist, only the first discovered file will be used.
1889
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001890If a discovered file name beings with `cmd-` it will be clustered
David Pursehouse6853b5a2013-07-10 11:38:03 +09001891into a 'Commands' section of the generated index page.
1892
David Pursehousefe529152013-08-14 16:35:06 +09001893If a discovered file name beings with `servlet-` it will be clustered
1894into a 'Servlets' section of the generated index page.
1895
1896If a discovered file name beings with `rest-api-` it will be clustered
1897into a 'REST APIs' section of the generated index page.
1898
David Pursehouse6853b5a2013-07-10 11:38:03 +09001899All other files are clustered under a 'Documentation' section.
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001900
1901Some optional information from the manifest is extracted and
1902displayed as part of the index page, if present in the manifest:
1903
1904[width="40%",options="header"]
1905|===================================================
1906|Field | Source Attribute
1907|Name | Implementation-Title
1908|Vendor | Implementation-Vendor
1909|Version | Implementation-Version
1910|URL | Implementation-URL
1911|API Version | Gerrit-ApiVersion
1912|===================================================
1913
Edwin Kempinf5a77332012-07-18 11:17:53 +02001914[[deployment]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001915== Deployment
Nasser Grainawie033b262012-05-09 17:54:21 -07001916
Edwin Kempinf7295742012-07-16 15:03:46 +02001917Compiled plugins and extensions can be deployed to a running Gerrit
1918server using the link:cmd-plugin-install.html[plugin install] command.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001919
David Pursehousea1d633b2014-05-02 17:21:02 +09001920Web UI plugins distributed as single `.js` file can be deployed
Dariusz Luksza357a2422012-11-12 06:16:26 +01001921without the overhead of JAR packaging, for more information refer to
1922link:cmd-plugin-install.html[plugin install] command.
1923
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001924Plugins can also be copied directly into the server's
Dariusz Luksza357a2422012-11-12 06:16:26 +01001925directory at `$site_path/plugins/$name.(jar|js)`. The name of
1926the JAR file, minus the `.jar` or `.js` extension, will be used as the
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001927plugin name. Unless disabled, servers periodically scan this
1928directory for updated plugins. The time can be adjusted by
1929link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency].
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001930
Edwin Kempinf7295742012-07-16 15:03:46 +02001931For disabling plugins the link:cmd-plugin-remove.html[plugin remove]
1932command can be used.
1933
Brad Larsond5e87c32012-07-11 12:18:49 -05001934Disabled plugins can be re-enabled using the
1935link:cmd-plugin-enable.html[plugin enable] command.
1936
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001937== SEE ALSO
David Ostrovskyf86bae52013-09-01 09:10:39 +02001938
1939* link:js-api.html[JavaScript API]
1940* link:dev-rest-api.html[REST API Developers' Notes]
1941
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001942GERRIT
1943------
1944Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07001945
1946SEARCHBOX
1947---------