blob: 601915087be2cc1223ed418418c70a6d3a98b1fe [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 Pursehouse1a468f02015-11-09 13:33:08 -080039 -DarchetypeVersion=2.13-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
Doug Kelly732ad202015-11-13 13:11:32 -0800286 @Inject
Edwin Kempind4cfac12013-11-27 11:22:34 +0100287 public MyInitStep(@PluginName String pluginName, ConsoleUI ui,
288 AllProjectsConfig allProjectsConfig) {
289 this.pluginName = pluginName;
290 this.ui = ui;
291 this.allProjectsConfig = allProjectsConfig;
292 }
293
294 @Override
295 public void run() throws Exception {
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100296 }
297
298 @Override
299 public void postRun() throws Exception {
Edwin Kempind4cfac12013-11-27 11:22:34 +0100300 ui.message("\n");
301 ui.header(pluginName + " Integration");
302 boolean enabled = ui.yesno(true, "By default enabled for all projects");
Adrian Görlerd1612972014-10-20 17:06:07 +0200303 Config cfg = allProjectsConfig.load().getConfig();
Edwin Kempind4cfac12013-11-27 11:22:34 +0100304 if (enabled) {
305 cfg.setBoolean("plugin", pluginName, "enabled", enabled);
306 } else {
307 cfg.unset("plugin", pluginName, "enabled");
308 }
309 allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration");
310 }
311 }
312----
313
Luca Milanesio737285d2012-09-25 14:26:43 +0100314Bear in mind that the Plugin's InitStep class will be loaded but
315the standard Gerrit runtime environment is not available and the plugin's
316own Guice modules were not initialized.
317This means the InitStep for a plugin is not executed in the same way that
318the plugin executes within the server, and may mean a plugin author cannot
319trivially reuse runtime code during init.
320
321For instance a plugin that wants to verify connectivity may need to statically
322call the constructor of their connection class, passing in values obtained
323from the Section.Factory rather than from an injected Config object.
324
David Pursehoused128c892013-10-22 21:52:21 +0900325Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after
326the extraction of the plugins embedded in the distribution .war file into
327`$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade.
328
329A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit
330runtime objects injected at startup.
Luca Milanesio737285d2012-09-25 14:26:43 +0100331
David Pursehouse68153d72013-09-04 10:09:17 +0900332[source,java]
333----
334public class MyInitStep implements InitStep {
335 private final ConsoleUI ui;
336 private final Section.Factory sections;
337 private final String pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100338
David Pursehouse68153d72013-09-04 10:09:17 +0900339 @Inject
340 public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) {
341 this.ui = ui;
342 this.sections = sections;
343 this.pluginName = pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100344 }
David Pursehouse68153d72013-09-04 10:09:17 +0900345
346 @Override
347 public void run() throws Exception {
348 ui.header("\nMy plugin");
349
350 Section mySection = getSection("myplugin", null);
351 mySection.string("Link name", "linkname", "MyLink");
352 }
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100353
354 @Override
355 public void postRun() throws Exception {
356 }
David Pursehouse68153d72013-09-04 10:09:17 +0900357}
358----
Luca Milanesio737285d2012-09-25 14:26:43 +0100359
Edwin Kempinf5a77332012-07-18 11:17:53 +0200360[[classpath]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800361== Classpath
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700362
363Each plugin is loaded into its own ClassLoader, isolating plugins
364from each other. A plugin or extension inherits the Java runtime
365and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin)
366from the hosting server.
367
368Plugins are loaded from a single JAR file. If a plugin needs
369additional libraries, it must include those dependencies within
370its own JAR. Plugins built using Maven may be able to use the
371link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin]
372to package additional dependencies. Relocating (or renaming) classes
373should not be necessary due to the ClassLoader isolation.
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700374
Edwin Kempin98202662013-09-18 16:03:03 +0200375[[events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800376== Listening to Events
Edwin Kempin98202662013-09-18 16:03:03 +0200377
378Certain operations in Gerrit trigger events. Plugins may receive
379notifications of these events by implementing the corresponding
380listeners.
381
Martin Fick4c72aea2014-12-10 14:58:12 -0700382* `com.google.gerrit.common.EventListener`:
Edwin Kempin64059f52013-10-31 13:49:25 +0100383+
Martin Fick4c72aea2014-12-10 14:58:12 -0700384Allows to listen to events. These are the same
Edwin Kempin64059f52013-10-31 13:49:25 +0100385link:cmd-stream-events.html#events[events] that are also streamed by
386the link:cmd-stream-events.html[gerrit stream-events] command.
387
Edwin Kempin98202662013-09-18 16:03:03 +0200388* `com.google.gerrit.extensions.events.LifecycleListener`:
389+
Edwin Kempin3e7928a2013-12-03 07:39:00 +0100390Plugin start and stop
Edwin Kempin98202662013-09-18 16:03:03 +0200391
392* `com.google.gerrit.extensions.events.NewProjectCreatedListener`:
393+
394Project creation
395
396* `com.google.gerrit.extensions.events.ProjectDeletedListener`:
397+
398Project deletion
399
Edwin Kempinb27c9392013-11-19 13:12:43 +0100400* `com.google.gerrit.extensions.events.HeadUpdatedListener`:
401+
402Update of HEAD on a project
403
Stefan Lay310d77d2014-05-28 13:45:25 +0200404* `com.google.gerrit.extensions.events.UsageDataPublishedListener`:
405+
406Publication of usage data
407
Adrian Görlerf4a4c9a2014-08-22 17:09:18 +0200408* `com.google.gerrit.extensions.events.GarbageCollectorListener`:
409+
410Garbage collection ran on a project
411
Yang Zhenhui2659d422013-07-30 16:59:58 +0800412[[stream-events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800413== Sending Events to the Events Stream
Yang Zhenhui2659d422013-07-30 16:59:58 +0800414
415Plugins may send events to the events stream where consumers of
416Gerrit's `stream-events` ssh command will receive them.
417
418To send an event, the plugin must invoke one of the `postEvent`
419methods in the `ChangeHookRunner` class, passing an instance of
Martin Fick4c72aea2014-12-10 14:58:12 -0700420its own custom event class derived from
421`com.google.gerrit.server.events.Event`.
Yang Zhenhui2659d422013-07-30 16:59:58 +0800422
Martin Fick0aef6f12014-12-11 16:54:21 -0700423Plugins which define new Events should register them via the
424`com.google.gerrit.server.events.EventTypes.registerClass()`
425method. This will make the EventType known to the system.
Martin Fickf70c20a2014-12-11 17:03:15 -0700426Deserialzing events with the
427`com.google.gerrit.server.events.EventDeserializer` class requires
428that the event be registered in EventTypes.
Martin Fick0aef6f12014-12-11 16:54:21 -0700429
Edwin Kempin32737602014-01-23 09:04:58 +0100430[[validation]]
David Pursehouse91c5f5e2014-01-23 18:57:33 +0900431== Validation Listeners
Edwin Kempin32737602014-01-23 09:04:58 +0100432
433Certain operations in Gerrit can be validated by plugins by
434implementing the corresponding link:config-validation.html[listeners].
435
Saša Živkovec85a072014-01-28 10:08:25 +0100436[[receive-pack]]
437== Receive Pack Initializers
438
439Plugins may provide ReceivePack initializers which will be invoked
440by Gerrit just before a ReceivePack instance will be used. Usually,
441plugins will make use of the setXXX methods on the ReceivePack to
442set additional properties on it.
443
Saša Živkov626c7312014-02-24 17:15:08 +0100444[[post-receive-hook]]
445== Post Receive-Pack Hooks
446
447Plugins may register PostReceiveHook instances in order to get
448notified when JGit successfully receives a pack. This may be useful
449for those plugins which would like to monitor changes in Git
450repositories.
451
Hugo Arès572d5422014-06-17 14:22:03 -0400452[[pre-upload-hook]]
453== Pre Upload-Pack Hooks
454
455Plugins may register PreUploadHook instances in order to get
456notified when JGit is about to upload a pack. This may be useful
457for those plugins which would like to monitor usage in Git
458repositories.
459
Edwin Kempinf5a77332012-07-18 11:17:53 +0200460[[ssh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800461== SSH Commands
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700462
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700463Plugins may provide commands that can be accessed through the SSH
464interface (extensions do not have this option).
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700465
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700466Command implementations must extend the base class SshCommand:
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700467
David Pursehouse68153d72013-09-04 10:09:17 +0900468[source,java]
469----
470import com.google.gerrit.sshd.SshCommand;
David Ostrovskyb7d97752013-11-09 05:23:26 +0100471import com.google.gerrit.sshd.CommandMetaData;
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700472
Ian Bulle1a12202014-02-16 17:15:42 -0800473@CommandMetaData(name="print", description="Print hello command")
David Pursehouse68153d72013-09-04 10:09:17 +0900474class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800475 @Override
476 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900477 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700478 }
David Pursehouse68153d72013-09-04 10:09:17 +0900479}
480----
Nasser Grainawie033b262012-05-09 17:54:21 -0700481
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700482If no Guice modules are declared in the manifest, SSH commands may
Edwin Kempin948de0f2012-07-16 10:34:35 +0200483use auto-registration by providing an `@Export` annotation:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700484
David Pursehouse68153d72013-09-04 10:09:17 +0900485[source,java]
486----
487import com.google.gerrit.extensions.annotations.Export;
488import com.google.gerrit.sshd.SshCommand;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700489
David Pursehouse68153d72013-09-04 10:09:17 +0900490@Export("print")
491class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800492 @Override
493 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900494 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700495 }
David Pursehouse68153d72013-09-04 10:09:17 +0900496}
497----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700498
499If explicit registration is being used, a Guice module must be
500supplied to register the SSH command and declared in the manifest
501with the `Gerrit-SshModule` attribute:
502
David Pursehouse68153d72013-09-04 10:09:17 +0900503[source,java]
504----
505import com.google.gerrit.sshd.PluginCommandModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700506
David Pursehouse68153d72013-09-04 10:09:17 +0900507class MyCommands extends PluginCommandModule {
Ian Bulle1a12202014-02-16 17:15:42 -0800508 @Override
David Pursehouse68153d72013-09-04 10:09:17 +0900509 protected void configureCommands() {
David Ostrovskyb7d97752013-11-09 05:23:26 +0100510 command(PrintHello.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700511 }
David Pursehouse68153d72013-09-04 10:09:17 +0900512}
513----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700514
515For a plugin installed as name `helloworld`, the command implemented
516by PrintHello class will be available to users as:
517
518----
Keunhong Parka09a6f12012-07-10 14:45:02 -0600519$ ssh -p 29418 review.example.com helloworld print
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700520----
521
David Ostrovsky79c4d892014-03-15 13:52:46 +0100522[[multiple-commands]]
523=== Multiple Commands bound to one implementation
524
David Ostrovskye3172b32013-10-13 14:19:13 +0200525Multiple SSH commands can be bound to the same implementation class. For
526example a Gerrit Shell plugin can bind different shell commands to the same
527implementation class:
528
529[source,java]
530----
531public class SshShellModule extends PluginCommandModule {
532 @Override
533 protected void configureCommands() {
534 command("ls").to(ShellCommand.class);
535 command("ps").to(ShellCommand.class);
536 [...]
537 }
538}
539----
540
541With the possible implementation:
542
543[source,java]
544----
545public class ShellCommand extends SshCommand {
546 @Override
547 protected void run() throws UnloggedFailure {
548 String cmd = getName().substring(getPluginName().length() + 1);
549 ProcessBuilder proc = new ProcessBuilder(cmd);
550 Process cmd = proc.start();
551 [...]
552 }
553}
554----
555
556And the call:
557
558----
559$ ssh -p 29418 review.example.com shell ls
560$ ssh -p 29418 review.example.com shell ps
561----
562
David Ostrovsky79c4d892014-03-15 13:52:46 +0100563[[root-level-commands]]
564=== Root Level Commands
565
David Ostrovskyb7d97752013-11-09 05:23:26 +0100566Single command plugins are also supported. In this scenario plugin binds
567SSH command to its own name. `SshModule` must inherit from
568`SingleCommandPluginModule` class:
569
570[source,java]
571----
572public class SshModule extends SingleCommandPluginModule {
573 @Override
574 protected void configure(LinkedBindingBuilder<Command> b) {
575 b.to(ShellCommand.class);
576 }
577}
578----
579
580If the plugin above is deployed under sh.jar file in `$site/plugins`
David Pursehouse659860f2013-12-16 14:50:04 +0900581directory, generic commands can be called without specifying the
David Ostrovskyb7d97752013-11-09 05:23:26 +0100582actual SSH command. Note in the example below, that the called commands
583`ls` and `ps` was not explicitly bound:
584
585----
586$ ssh -p 29418 review.example.com sh ls
587$ ssh -p 29418 review.example.com sh ps
588----
589
Martin Fick5f6222912015-11-12 14:52:50 -0700590[[search_operators]]
591=== Search Operators ===
592
593Plugins can define new search operators to extend change searching by
594implementing the `ChangeQueryBuilder.ChangeOperatorFactory` interface
595and registering it to an operator name in the plugin module's
596`configure()` method. The search operator name is defined during
597registration via the DynamicMap annotation mechanism. The plugin
598name will get appended to the annotated name, with an underscore
599in between, leading to the final operator name. An example
600registration looks like this:
601
602 bind(ChangeOperatorFactory.class)
603 .annotatedWith(Exports.named("sample"))
604 .to(SampleOperator.class);
605
606If this is registered in the `myplugin` plugin, then the resulting
607operator will be named `sample_myplugin`.
608
609The search operator itself is implemented by ensuring that the
610`create()` method of the class implementing the
611`ChangeQueryBuilder.ChangeOperatorFactory` interface returns a
612`Predicate<ChangeData>`. Here is a sample operator factory
613defintion which creates a `MyPredicate`:
614
615[source,java]
616----
617@Singleton
618public class SampleOperator
619 implements ChangeQueryBuilder.ChangeOperatorFactory {
620 public static class MyPredicate extends OperatorPredicate<ChangeData> {
621 ...
622 }
623
624 @Override
625 public Predicate<ChangeData> create(ChangeQueryBuilder builder, String value)
626 throws QueryParseException {
627 return new MyPredicate(value);
628 }
629}
630----
631
Edwin Kempin78ca0942013-10-30 11:24:06 +0100632[[simple-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800633== Simple Configuration in `gerrit.config`
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200634
635In Gerrit, global configuration is stored in the `gerrit.config` file.
636If a plugin needs global configuration, this configuration should be
637stored in a `plugin` subsection in the `gerrit.config` file.
638
Edwin Kempinc9b68602013-10-30 09:32:43 +0100639This approach of storing the plugin configuration is only suitable for
640plugins that have a simple configuration that only consists of
641key-value pairs. With this approach it is not possible to have
642subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin78ca0942013-10-30 11:24:06 +0100643configuration need to store their configuration in their
644link:#configuration[own configuration file] where they can make use of
645subsections. On the other hand storing the plugin configuration in a
646'plugin' subsection in the `gerrit.config` file has the advantage that
647administrators have all configuration parameters in one file, instead
648of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100649
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200650To avoid conflicts with other plugins, it is recommended that plugins
651only use the `plugin` subsection with their own name. For example the
652`helloworld` plugin should store its configuration in the
653`plugin.helloworld` subsection:
654
655----
656[plugin "helloworld"]
657 language = Latin
658----
659
Sasa Zivkovacdf5332013-09-20 14:05:15 +0200660Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200661plugin can easily access its configuration and there is no need for a
662plugin to parse the `gerrit.config` file on its own:
663
664[source,java]
665----
David Pursehouse529ec252013-09-27 13:45:14 +0900666@Inject
667private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200668
David Pursehoused128c892013-10-22 21:52:21 +0900669[...]
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200670
Edwin Kempin122622d2013-10-29 16:45:44 +0100671String language = cfg.getFromGerritConfig("helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900672 .getString("language", "English");
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200673----
674
Edwin Kempin78ca0942013-10-30 11:24:06 +0100675[[configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800676== Configuration in own config file
Edwin Kempin78ca0942013-10-30 11:24:06 +0100677
678Plugins can store their configuration in an own configuration file.
679This makes sense if the plugin configuration is rather complex and
680requires the usage of subsections. Plugins that have a simple
681key-value pair configuration can store their configuration in a
682link:#simple-configuration[`plugin` subsection of the `gerrit.config`
683file].
684
685The plugin configuration file must be named after the plugin and must
686be located in the `etc` folder of the review site. For example a
687configuration file for a `default-reviewer` plugin could look like
688this:
689
690.$site_path/etc/default-reviewer.config
691----
692[branch "refs/heads/master"]
693 reviewer = Project Owners
694 reviewer = john.doe@example.com
695[match "file:^.*\.txt"]
696 reviewer = My Info Developers
697----
698
699Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
700plugin can easily access its configuration:
701
702[source,java]
703----
704@Inject
705private com.google.gerrit.server.config.PluginConfigFactory cfg;
706
707[...]
708
709String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer")
710 .getStringList("branch", "refs/heads/master", "reviewer");
711----
712
Edwin Kempin78ca0942013-10-30 11:24:06 +0100713
Edwin Kempin705f2842013-10-30 14:25:31 +0100714[[simple-project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800715== Simple Project Specific Configuration in `project.config`
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200716
717In Gerrit, project specific configuration is stored in the project's
718`project.config` file on the `refs/meta/config` branch. If a plugin
719needs configuration on project level (e.g. to enable its functionality
720only for certain projects), this configuration should be stored in a
721`plugin` subsection in the project's `project.config` file.
722
Edwin Kempinc9b68602013-10-30 09:32:43 +0100723This approach of storing the plugin configuration is only suitable for
724plugins that have a simple configuration that only consists of
725key-value pairs. With this approach it is not possible to have
726subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin705f2842013-10-30 14:25:31 +0100727configuration need to store their configuration in their
728link:#project-specific-configuration[own configuration file] where they
729can make use of subsections. On the other hand storing the plugin
730configuration in a 'plugin' subsection in the `project.config` file has
731the advantage that project owners have all configuration parameters in
732one file, instead of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100733
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200734To avoid conflicts with other plugins, it is recommended that plugins
735only use the `plugin` subsection with their own name. For example the
736`helloworld` plugin should store its configuration in the
737`plugin.helloworld` subsection:
738
739----
740 [plugin "helloworld"]
741 enabled = true
742----
743
744Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
745plugin can easily access its project specific configuration and there
746is no need for a plugin to parse the `project.config` file on its own:
747
748[source,java]
749----
David Pursehouse529ec252013-09-27 13:45:14 +0900750@Inject
751private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200752
David Pursehoused128c892013-10-22 21:52:21 +0900753[...]
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200754
Edwin Kempin122622d2013-10-29 16:45:44 +0100755boolean enabled = cfg.getFromProjectConfig(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900756 .getBoolean("enabled", false);
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200757----
758
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200759It is also possible to get missing configuration parameters inherited
760from the parent projects:
761
762[source,java]
763----
David Pursehouse529ec252013-09-27 13:45:14 +0900764@Inject
765private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200766
David Pursehoused128c892013-10-22 21:52:21 +0900767[...]
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200768
Edwin Kempin122622d2013-10-29 16:45:44 +0100769boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900770 .getBoolean("enabled", false);
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200771----
772
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200773Project owners can edit the project configuration by fetching the
774`refs/meta/config` branch, editing the `project.config` file and
775pushing the commit back.
776
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100777Plugin configuration values that are stored in the `project.config`
778file can be exposed in the ProjectInfoScreen to allow project owners
779to see and edit them from the UI.
780
781For this an instance of `ProjectConfigEntry` needs to be bound for each
782parameter. The export name must be a valid Git variable name. The
783variable name is case-insensitive, allows only alphanumeric characters
784and '-', and must start with an alphabetic character.
785
Edwin Kempina6c1c452013-11-28 16:55:22 +0100786The example below shows how the parameters `plugin.helloworld.enabled`
787and `plugin.helloworld.language` are bound to be editable from the
David Pursehousea1d633b2014-05-02 17:21:02 +0900788Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting"
Edwin Kempina6c1c452013-11-28 16:55:22 +0100789is provided as display name and the default value is set to `true`.
790For the parameter `plugin.helloworld.language` "Preferred Language"
791is provided as display name and "en" is set as default value.
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100792
793[source,java]
794----
795class Module extends AbstractModule {
796 @Override
797 protected void configure() {
798 bind(ProjectConfigEntry.class)
Edwin Kempina6c1c452013-11-28 16:55:22 +0100799 .annotatedWith(Exports.named("enabled"))
800 .toInstance(new ProjectConfigEntry("Enable Greeting", true));
801 bind(ProjectConfigEntry.class)
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100802 .annotatedWith(Exports.named("language"))
803 .toInstance(new ProjectConfigEntry("Preferred Language", "en"));
804 }
805}
806----
807
Edwin Kempinb64d3972013-11-17 18:55:48 +0100808By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins
809can be notified when this configuration parameter is updated on a
810project.
811
Janice Agustine5a9d012015-08-24 09:05:56 -0400812[[configuring-groups]]
813=== Referencing groups in `project.config`
814
815Plugins can refer to groups so that when they are renamed, the project
816config will also be updated in this section. The proper format to use is
817the string representation of a GroupReference, as shown below.
818
819----
820Group[group_name / group_uuid]
821----
822
Edwin Kempin705f2842013-10-30 14:25:31 +0100823[[project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800824== Project Specific Configuration in own config file
Edwin Kempin705f2842013-10-30 14:25:31 +0100825
826Plugins can store their project specific configuration in an own
827configuration file in the projects `refs/meta/config` branch.
828This makes sense if the plugins project specific configuration is
829rather complex and requires the usage of subsections. Plugins that
830have a simple key-value pair configuration can store their project
831specific configuration in a link:#simple-project-specific-configuration[
832`plugin` subsection of the `project.config` file].
833
834The plugin configuration file in the `refs/meta/config` branch must be
835named after the plugin. For example a configuration file for a
836`default-reviewer` plugin could look like this:
837
838.default-reviewer.config
839----
840[branch "refs/heads/master"]
841 reviewer = Project Owners
842 reviewer = john.doe@example.com
843[match "file:^.*\.txt"]
844 reviewer = My Info Developers
845----
846
847Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
848plugin can easily access its project specific configuration:
849
850[source,java]
851----
852@Inject
853private com.google.gerrit.server.config.PluginConfigFactory cfg;
854
855[...]
856
857String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer")
858 .getStringList("branch", "refs/heads/master", "reviewer");
859----
860
Edwin Kempin762da382013-10-30 14:50:01 +0100861It is also possible to get missing configuration parameters inherited
862from the parent projects:
863
864[source,java]
865----
866@Inject
867private com.google.gerrit.server.config.PluginConfigFactory cfg;
868
869[...]
870
David Ostrovsky468e4c32014-03-22 06:05:35 -0700871String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer")
Edwin Kempin762da382013-10-30 14:50:01 +0100872 .getStringList("branch", "refs/heads/master", "reviewer");
873----
874
Edwin Kempin705f2842013-10-30 14:25:31 +0100875Project owners can edit the project configuration by fetching the
876`refs/meta/config` branch, editing the `<plugin-name>.config` file and
877pushing the commit back.
878
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800879== React on changes in project configuration
Edwin Kempina46b6c92013-12-04 21:05:24 +0100880
881If a plugin wants to react on changes in the project configuration, it
882can implement a `GitReferenceUpdatedListener` and filter on events for
883the `refs/meta/config` branch:
884
885[source,java]
886----
887public class MyListener implements GitReferenceUpdatedListener {
888
889 private final MetaDataUpdate.Server metaDataUpdateFactory;
890
891 @Inject
892 MyListener(MetaDataUpdate.Server metaDataUpdateFactory) {
893 this.metaDataUpdateFactory = metaDataUpdateFactory;
894 }
895
896 @Override
897 public void onGitReferenceUpdated(Event event) {
Edwin Kempina951ba52014-01-03 14:07:28 +0100898 if (event.getRefName().equals(RefNames.REFS_CONFIG)) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100899 Project.NameKey p = new Project.NameKey(event.getProjectName());
900 try {
Edwin Kempina951ba52014-01-03 14:07:28 +0100901 ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId());
902 ProjectConfig newCfg = parseConfig(p, event.getNewObjectId());
Edwin Kempina46b6c92013-12-04 21:05:24 +0100903
Edwin Kempina951ba52014-01-03 14:07:28 +0100904 if (oldCfg != null && newCfg != null
905 && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100906 // submit type has changed
907 ...
908 }
909 } catch (IOException | ConfigInvalidException e) {
910 ...
911 }
912 }
913 }
Edwin Kempina951ba52014-01-03 14:07:28 +0100914
915 private ProjectConfig parseConfig(Project.NameKey p, String idStr)
916 throws IOException, ConfigInvalidException, RepositoryNotFoundException {
917 ObjectId id = ObjectId.fromString(idStr);
918 if (ObjectId.zeroId().equals(id)) {
919 return null;
920 }
921 return ProjectConfig.read(metaDataUpdateFactory.create(p), id);
922 }
Edwin Kempina46b6c92013-12-04 21:05:24 +0100923}
924----
925
926
David Ostrovsky7066cc02013-06-15 14:46:23 +0200927[[capabilities]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800928== Plugin Owned Capabilities
David Ostrovsky7066cc02013-06-15 14:46:23 +0200929
930Plugins may provide their own capabilities and restrict usage of SSH
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200931commands or `UiAction` to the users who are granted those capabilities.
David Ostrovsky7066cc02013-06-15 14:46:23 +0200932
933Plugins define the capabilities by overriding the `CapabilityDefinition`
934abstract class:
935
David Pursehouse68153d72013-09-04 10:09:17 +0900936[source,java]
937----
938public class PrintHelloCapability extends CapabilityDefinition {
939 @Override
940 public String getDescription() {
941 return "Print Hello";
David Ostrovsky7066cc02013-06-15 14:46:23 +0200942 }
David Pursehouse68153d72013-09-04 10:09:17 +0900943}
944----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200945
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200946If no Guice modules are declared in the manifest, capability may
David Ostrovsky7066cc02013-06-15 14:46:23 +0200947use auto-registration by providing an `@Export` annotation:
948
David Pursehouse68153d72013-09-04 10:09:17 +0900949[source,java]
950----
951@Export("printHello")
952public class PrintHelloCapability extends CapabilityDefinition {
David Pursehoused128c892013-10-22 21:52:21 +0900953 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900954}
955----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200956
957Otherwise the capability must be bound in a plugin module:
958
David Pursehouse68153d72013-09-04 10:09:17 +0900959[source,java]
960----
961public class HelloWorldModule extends AbstractModule {
962 @Override
963 protected void configure() {
964 bind(CapabilityDefinition.class)
965 .annotatedWith(Exports.named("printHello"))
966 .to(PrintHelloCapability.class);
David Ostrovsky7066cc02013-06-15 14:46:23 +0200967 }
David Pursehouse68153d72013-09-04 10:09:17 +0900968}
969----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200970
971With a plugin-owned capability defined in this way, it is possible to restrict
David Ostrovskyf86bae52013-09-01 09:10:39 +0200972usage of an SSH command or `UiAction` to members of the group that were granted
David Ostrovsky7066cc02013-06-15 14:46:23 +0200973this capability in the usual way, using the `RequiresCapability` annotation:
974
David Pursehouse68153d72013-09-04 10:09:17 +0900975[source,java]
976----
977@RequiresCapability("printHello")
978@CommandMetaData(name="print", description="Print greeting in different languages")
979public final class PrintHelloWorldCommand extends SshCommand {
David Pursehoused128c892013-10-22 21:52:21 +0900980 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900981}
982----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200983
David Ostrovskyf86bae52013-09-01 09:10:39 +0200984Or with `UiAction`:
David Ostrovsky7066cc02013-06-15 14:46:23 +0200985
David Pursehouse68153d72013-09-04 10:09:17 +0900986[source,java]
987----
988@RequiresCapability("printHello")
989public class SayHelloAction extends UiAction<RevisionResource>
990 implements RestModifyView<RevisionResource, SayHelloAction.Input> {
David Pursehoused128c892013-10-22 21:52:21 +0900991 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900992}
993----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200994
995Capability scope was introduced to differentiate between plugin-owned
David Pursehousebf053342013-09-05 14:55:29 +0900996capabilities and core capabilities. Per default the scope of the
997`@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means:
998
David Ostrovsky7066cc02013-06-15 14:46:23 +0200999* when `@RequiresCapability` is used within a plugin the scope of the
1000capability is assumed to be that plugin.
David Pursehousebf053342013-09-05 14:55:29 +09001001
David Ostrovsky7066cc02013-06-15 14:46:23 +02001002* If `@RequiresCapability` is used within the core Gerrit Code Review server
1003(and thus is outside of a plugin) the scope is the core server and will use
1004the `GlobalCapability` known to Gerrit Code Review server.
1005
1006If a plugin needs to use a core capability name (e.g. "administrateServer")
1007this can be specified by setting `scope = CapabilityScope.CORE`:
1008
David Pursehouse68153d72013-09-04 10:09:17 +09001009[source,java]
1010----
1011@RequiresCapability(value = "administrateServer", scope =
1012 CapabilityScope.CORE)
David Pursehoused128c892013-10-22 21:52:21 +09001013 [...]
David Pursehouse68153d72013-09-04 10:09:17 +09001014----
David Ostrovsky7066cc02013-06-15 14:46:23 +02001015
David Ostrovskyf86bae52013-09-01 09:10:39 +02001016[[ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001017== UI Extension
David Ostrovskyf86bae52013-09-01 09:10:39 +02001018
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001019[[panels]]
1020=== Panels
1021
1022GWT plugins can contribute panels to Gerrit screens.
1023
1024Gerrit screens define extension points where plugins can add GWT
1025panels with custom controls:
1026
1027* Change Screen:
Edwin Kempin2a8c5152015-07-08 14:28:57 +02001028** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER`:
1029+
1030Panel will be shown in the header bar to the right of the change
1031status.
1032
Edwin Kempin745021e2015-07-09 13:09:44 +02001033** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_BUTTONS`:
1034+
1035Panel will be shown in the header bar on the right side of the buttons.
1036
Edwin Kempincbc95252015-07-09 11:37:53 +02001037** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_POP_DOWNS`:
1038+
1039Panel will be shown in the header bar on the right side of the pop down
1040buttons.
1041
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001042** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK`:
1043+
1044Panel will be shown below the change info block.
1045
1046** The following parameters are provided:
Edwin Kempin5d683cc2015-07-10 15:47:14 +02001047*** `GerritUiExtensionPoint.Key.CHANGE_INFO`:
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001048+
Edwin Kempin5d683cc2015-07-10 15:47:14 +02001049The link:rest-api-changes.html#change-info[ChangeInfo] entity for the
1050current change.
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001051
Edwin Kempin88b947a2015-07-08 09:03:56 +02001052* Project Info Screen:
1053** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_TOP`:
1054+
1055Panel will be shown at the top of the screen.
1056
1057** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_BOTTOM`:
1058+
1059Panel will be shown at the bottom of the screen.
1060
1061** The following parameters are provided:
1062*** `GerritUiExtensionPoint.Key.PROJECT_NAME`:
1063+
1064The name of the project.
1065
Edwin Kempin241d9db2015-07-08 13:53:50 +02001066* User Password Screen:
1067** `GerritUiExtensionPoint.PASSWORD_SCREEN_BOTTOM`:
1068+
1069Panel will be shown at the bottom of the screen.
1070
Edwin Kempin30c6f472015-07-09 14:27:52 +02001071** The following parameters are provided:
1072*** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`:
1073+
1074The link:rest-api-accounts.html#account-info[AccountInfo] entity for
1075the current user.
1076
Edwin Kempin1cd95f92015-07-14 08:27:20 +02001077* User Preferences Screen:
1078** `GerritUiExtensionPoint.PREFERENCES_SCREEN_BOTTOM`:
1079+
1080Panel will be shown at the bottom of the screen.
1081
1082** The following parameters are provided:
1083*** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`:
1084+
1085The link:rest-api-accounts.html#account-info[AccountInfo] entity for
1086the current user.
1087
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001088* User Profile Screen:
1089** `GerritUiExtensionPoint.PROFILE_SCREEN_BOTTOM`:
1090+
1091Panel will be shown at the bottom of the screen below the grid with the
1092profile data.
1093
Edwin Kempin30c6f472015-07-09 14:27:52 +02001094** The following parameters are provided:
1095*** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`:
1096+
1097The link:rest-api-accounts.html#account-info[AccountInfo] entity for
1098the current user.
1099
Edwin Kempin52f79ac2015-07-07 16:37:50 +02001100Example panel:
1101[source,java]
1102----
1103public class MyPlugin extends PluginEntryPoint {
1104 @Override
1105 public void onPluginLoad() {
1106 Plugin.get().panel(GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK,
1107 new Panel.EntryPoint() {
1108 @Override
1109 public void onLoad(Panel panel) {
1110 panel.setWidget(new InlineLabel("My Panel for change "
1111 + panel.getInt(GerritUiExtensionPoint.Key.CHANGE_ID, -1));
1112 }
1113 });
1114 }
1115}
1116----
1117
1118[[actions]]
1119=== Actions
1120
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001121Plugins can contribute UI actions on core Gerrit pages. This is useful
1122for workflow customization or exposing plugin functionality through the
1123UI in addition to SSH commands and the REST API.
David Ostrovskyf86bae52013-09-01 09:10:39 +02001124
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001125For instance a plugin to integrate Jira with Gerrit changes may
1126contribute a "File bug" button to allow filing a bug from the change
1127page or plugins to integrate continuous integration systems may
1128contribute a "Schedule" button to allow a CI build to be scheduled
1129manually from the patch set panel.
David Ostrovskyf86bae52013-09-01 09:10:39 +02001130
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001131Two different places on core Gerrit pages are supported:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001132
1133* Change screen
1134* Project info screen
1135
1136Plugins contribute UI actions by implementing the `UiAction` interface:
1137
David Pursehouse68153d72013-09-04 10:09:17 +09001138[source,java]
1139----
1140@RequiresCapability("printHello")
1141class HelloWorldAction implements UiAction<RevisionResource>,
1142 RestModifyView<RevisionResource, HelloWorldAction.Input> {
1143 static class Input {
1144 boolean french;
1145 String message;
David Ostrovskyf86bae52013-09-01 09:10:39 +02001146 }
David Pursehouse68153d72013-09-04 10:09:17 +09001147
1148 private Provider<CurrentUser> user;
1149
1150 @Inject
1151 HelloWorldAction(Provider<CurrentUser> user) {
1152 this.user = user;
1153 }
1154
1155 @Override
1156 public String apply(RevisionResource rev, Input input) {
1157 final String greeting = input.french
1158 ? "Bonjour"
1159 : "Hello";
1160 return String.format("%s %s from change %s, patch set %d!",
1161 greeting,
1162 Strings.isNullOrEmpty(input.message)
1163 ? Objects.firstNonNull(user.get().getUserName(), "world")
1164 : input.message,
1165 rev.getChange().getId().toString(),
1166 rev.getPatchSet().getPatchSetId());
1167 }
1168
1169 @Override
1170 public Description getDescription(
1171 RevisionResource resource) {
1172 return new Description()
1173 .setLabel("Say hello")
1174 .setTitle("Say hello in different languages");
1175 }
1176}
1177----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001178
David Ostrovsky450eefe2013-10-21 21:18:11 +02001179Sometimes plugins may want to be able to change the state of a patch set or
1180change in the `UiAction.apply()` method and reflect these changes on the core
1181UI. For example a buildbot plugin which exposes a 'Schedule' button on the
1182patch set panel may want to disable that button after the build was scheduled
1183and update the tooltip of that button. But because of Gerrit's caching
1184strategy the following must be taken into consideration.
1185
1186The browser is allowed to cache the `UiAction` information until something on
1187the change is modified. More accurately the change row needs to be modified in
1188the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or
1189the +refs/meta/config+ of the project or any parents needs to change to a new
1190SHA-1. The ETag SHA-1 computation code can be found in the
1191`ChangeResource.getETag()` method.
1192
David Pursehoused128c892013-10-22 21:52:21 +09001193The easiest way to accomplish this is to update `lastUpdatedOn` of the change:
David Ostrovsky450eefe2013-10-21 21:18:11 +02001194
1195[source,java]
1196----
1197@Override
1198public Object apply(RevisionResource rcrs, Input in) {
1199 // schedule a build
1200 [...]
1201 // update change
1202 ReviewDb db = dbProvider.get();
1203 db.changes().beginTransaction(change.getId());
1204 try {
1205 change = db.changes().atomicUpdate(
1206 change.getId(),
1207 new AtomicUpdate<Change>() {
1208 @Override
1209 public Change update(Change change) {
1210 ChangeUtil.updated(change);
1211 return change;
1212 }
1213 });
1214 db.commit();
1215 } finally {
1216 db.rollback();
1217 }
David Pursehoused128c892013-10-22 21:52:21 +09001218 [...]
David Ostrovsky450eefe2013-10-21 21:18:11 +02001219}
1220----
1221
David Ostrovskyf86bae52013-09-01 09:10:39 +02001222`UiAction` must be bound in a plugin module:
1223
David Pursehouse68153d72013-09-04 10:09:17 +09001224[source,java]
1225----
1226public class Module extends AbstractModule {
1227 @Override
1228 protected void configure() {
1229 install(new RestApiModule() {
1230 @Override
1231 protected void configure() {
1232 post(REVISION_KIND, "say-hello")
1233 .to(HelloWorldAction.class);
1234 }
1235 });
David Ostrovskyf86bae52013-09-01 09:10:39 +02001236 }
David Pursehouse68153d72013-09-04 10:09:17 +09001237}
1238----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001239
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001240The module above must be declared in the `pom.xml` for Maven driven
1241plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001242
David Pursehouse68153d72013-09-04 10:09:17 +09001243[source,xml]
1244----
1245<manifestEntries>
1246 <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module>
1247</manifestEntries>
1248----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001249
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001250or in the `BUCK` configuration file for Buck driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001251
David Pursehouse68153d72013-09-04 10:09:17 +09001252[source,python]
1253----
1254manifest_entries = [
1255 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module',
1256]
1257----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001258
1259In some use cases more user input must be gathered, for that `UiAction` can be
1260combined with the JavaScript API. This would display a small popup near the
1261activation button to gather additional input from the user. The JS file is
1262typically put in the `static` folder within the plugin's directory:
1263
David Pursehouse68153d72013-09-04 10:09:17 +09001264[source,javascript]
1265----
1266Gerrit.install(function(self) {
1267 function onSayHello(c) {
1268 var f = c.textfield();
1269 var t = c.checkbox();
1270 var b = c.button('Say hello', {onclick: function(){
1271 c.call(
1272 {message: f.value, french: t.checked},
1273 function(r) {
1274 c.hide();
1275 window.alert(r);
1276 c.refresh();
1277 });
1278 }});
1279 c.popup(c.div(
1280 c.prependLabel('Greeting message', f),
1281 c.br(),
1282 c.label(t, 'french'),
1283 c.br(),
1284 b));
1285 f.focus();
1286 }
1287 self.onAction('revision', 'say-hello', onSayHello);
1288});
1289----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001290
1291The JS module must be exposed as a `WebUiPlugin` and bound as
1292an HTTP Module:
1293
David Pursehouse68153d72013-09-04 10:09:17 +09001294[source,java]
1295----
1296public class HttpModule extends HttpPluginModule {
1297 @Override
1298 protected void configureServlets() {
1299 DynamicSet.bind(binder(), WebUiPlugin.class)
1300 .toInstance(new JavaScriptPlugin("hello.js"));
David Ostrovskyf86bae52013-09-01 09:10:39 +02001301 }
David Pursehouse68153d72013-09-04 10:09:17 +09001302}
1303----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001304
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001305The HTTP module above must be declared in the `pom.xml` for Maven
1306driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001307
David Pursehouse68153d72013-09-04 10:09:17 +09001308[source,xml]
1309----
1310<manifestEntries>
1311 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule>
1312</manifestEntries>
1313----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001314
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001315or in the `BUCK` configuration file for Buck driven plugins
David Ostrovskyf86bae52013-09-01 09:10:39 +02001316
David Pursehouse68153d72013-09-04 10:09:17 +09001317[source,python]
1318----
1319manifest_entries = [
1320 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule',
1321]
1322----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001323
1324If `UiAction` is annotated with the `@RequiresCapability` annotation, then the
1325capability check is done during the `UiAction` gathering, so the plugin author
1326doesn't have to set `UiAction.Description.setVisible()` explicitly in this
1327case.
1328
1329The following prerequisities must be met, to satisfy the capability check:
1330
1331* user is authenticated
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001332* user is a member of a group which has the `Administrate Server` capability, or
David Ostrovskyf86bae52013-09-01 09:10:39 +02001333* user is a member of a group which has the required capability
1334
1335The `apply` method is called when the button is clicked. If `UiAction` is
1336combined with JavaScript API (its own JavaScript function is provided),
1337then a popup dialog is normally opened to gather additional user input.
1338A new button is placed on the popup dialog to actually send the request.
1339
1340Every `UiAction` exposes a REST API endpoint. The endpoint from the example above
1341can be accessed from any REST client, i. e.:
1342
1343====
1344 curl -X POST -H "Content-Type: application/json" \
1345 -d '{message: "François", french: true}' \
1346 --digest --user joe:secret \
1347 http://host:port/a/changes/1/revisions/1/cookbook~say-hello
1348 "Bonjour François from change 1, patch set 1!"
1349====
1350
David Pursehouse42245822013-09-24 09:48:20 +09001351A special case is to bind an endpoint without a view name. This is
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001352particularly useful for `DELETE` requests:
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001353
1354[source,java]
1355----
1356public class Module extends AbstractModule {
1357 @Override
1358 protected void configure() {
1359 install(new RestApiModule() {
1360 @Override
1361 protected void configure() {
1362 delete(PROJECT_KIND)
1363 .to(DeleteProject.class);
1364 }
1365 });
1366 }
1367}
1368----
1369
David Pursehouse42245822013-09-24 09:48:20 +09001370For a `UiAction` bound this way, a JS API function can be provided.
1371
1372Currently only one restriction exists: per plugin only one `UiAction`
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001373can be bound per resource without view name. To define a JS function
1374for the `UiAction`, "/" must be used as the name:
1375
1376[source,javascript]
1377----
1378Gerrit.install(function(self) {
1379 function onDeleteProject(c) {
1380 [...]
1381 }
1382 self.onAction('project', '/', onDeleteProject);
1383});
1384----
1385
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001386[[top-menu-extensions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001387== Top Menu Extensions
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001388
1389Plugins can contribute items to Gerrit's top menu.
1390
1391A single top menu extension can have multiple elements and will be put as
1392the last element in Gerrit's top menu.
1393
1394Plugins define the top menu entries by implementing `TopMenu` interface:
1395
1396[source,java]
1397----
1398public class MyTopMenuExtension implements TopMenu {
1399
1400 @Override
1401 public List<MenuEntry> getEntries() {
1402 return Lists.newArrayList(
1403 new MenuEntry("Top Menu Entry", Lists.newArrayList(
1404 new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1405 }
1406}
1407----
1408
Edwin Kempin77f23242013-09-30 14:53:20 +02001409Plugins can also add additional menu items to Gerrit's top menu entries
1410by defining a `MenuEntry` that has the same name as a Gerrit top menu
1411entry:
1412
1413[source,java]
1414----
1415public class MyTopMenuExtension implements TopMenu {
1416
1417 @Override
1418 public List<MenuEntry> getEntries() {
1419 return Lists.newArrayList(
Dariusz Luksza2d3afab2013-10-01 11:07:13 +02001420 new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList(
Edwin Kempin77f23242013-09-30 14:53:20 +02001421 new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
1422 }
1423}
1424----
1425
Dariusz Lukszae8de74f2014-06-04 19:39:20 +02001426`MenuItems` that are bound for the `MenuEntry` with the name
1427`GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder
1428which is automatically replaced by the actual project name.
1429
1430E.g. plugins may register an link:#http[HTTP Servlet] to handle project
1431specific requests and add an menu item for this:
1432
1433[source,java]
1434---
1435 new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}");
1436---
1437
1438This also enables plugins to provide menu items for project aware
1439screens:
1440
1441[source,java]
1442---
1443 new MenuItem("My Screen", "/x/my-screen/for/${projectName}");
1444---
1445
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001446If no Guice modules are declared in the manifest, the top menu extension may use
1447auto-registration by providing an `@Listen` annotation:
1448
1449[source,java]
1450----
1451@Listen
1452public class MyTopMenuExtension implements TopMenu {
David Pursehoused128c892013-10-22 21:52:21 +09001453 [...]
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001454}
1455----
1456
Luca Milanesiocb230402013-10-11 08:49:56 +01001457Otherwise the top menu extension must be bound in the plugin module used
1458for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF):
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001459
1460[source,java]
1461----
Luca Milanesiocb230402013-10-11 08:49:56 +01001462package com.googlesource.gerrit.plugins.helloworld;
1463
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001464public class HelloWorldModule extends AbstractModule {
1465 @Override
1466 protected void configure() {
1467 DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class);
1468 }
1469}
1470----
1471
Luca Milanesiocb230402013-10-11 08:49:56 +01001472[source,manifest]
1473----
1474Gerrit-ApiType: plugin
1475Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule
1476----
1477
Edwin Kempinb2e926a2013-11-11 16:38:30 +01001478It is also possible to show some menu entries only if the user has a
1479certain capability:
1480
1481[source,java]
1482----
1483public class MyTopMenuExtension implements TopMenu {
1484 private final String pluginName;
1485 private final Provider<CurrentUser> userProvider;
1486 private final List<MenuEntry> menuEntries;
1487
1488 @Inject
1489 public MyTopMenuExtension(@PluginName String pluginName,
1490 Provider<CurrentUser> userProvider) {
1491 this.pluginName = pluginName;
1492 this.userProvider = userProvider;
1493 menuEntries = new ArrayList<TopMenu.MenuEntry>();
1494
1495 // add menu entry that is only visible to users with a certain capability
1496 if (canSeeMenuEntry()) {
1497 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1498 .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1499 }
1500
1501 // add menu entry that is visible to all users (even anonymous users)
1502 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1503 .singletonList(new MenuItem("Documentation", "/plugins/myplugin/"))));
1504 }
1505
1506 private boolean canSeeMenuEntry() {
1507 if (userProvider.get().isIdentifiedUser()) {
1508 CapabilityControl ctl = userProvider.get().getCapabilities();
1509 return ctl.canPerform(pluginName + "-" + MyCapability.ID)
1510 || ctl.canAdministrateServer();
1511 } else {
1512 return false;
1513 }
1514 }
1515
1516 @Override
1517 public List<MenuEntry> getEntries() {
1518 return menuEntries;
1519 }
1520}
1521----
1522
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001523[[gwt_ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001524== GWT UI Extension
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001525Plugins can extend the Gerrit UI with own GWT code.
1526
1527The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to
1528generate a GWT plugin skeleton. How to use the Maven plugin archetypes
1529is described in the link:#getting-started[Getting started] section.
1530
1531The generated GWT plugin has a link:#top-menu-extensions[top menu] that
1532opens a GWT dialog box when the user clicks on it.
1533
Edwin Kempinb74daa92013-11-11 11:28:16 +01001534In addition to the Gerrit-Plugin API a GWT plugin depends on
1535`gerrit-plugin-gwtui`. This dependency must be specified in the
1536`pom.xml`:
1537
1538[source,xml]
1539----
1540<dependency>
1541 <groupId>com.google.gerrit</groupId>
1542 <artifactId>gerrit-plugin-gwtui</artifactId>
1543 <version>${Gerrit-ApiVersion}</version>
1544</dependency>
1545----
1546
1547A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`,
1548that bundles together all the configuration settings of the GWT plugin:
1549
1550[source,xml]
1551----
1552<?xml version="1.0" encoding="UTF-8"?>
1553<module rename-to="hello_gwt_plugin">
1554 <!-- Inherit the core Web Toolkit stuff. -->
1555 <inherits name="com.google.gwt.user.User"/>
1556 <!-- Other module inherits -->
1557 <inherits name="com.google.gerrit.Plugin"/>
1558 <inherits name="com.google.gwt.http.HTTP"/>
1559 <!-- Using GWT built-in themes adds a number of static -->
1560 <!-- resources to the plugin. No theme inherits lines were -->
1561 <!-- added in order to make this plugin as simple as possible -->
1562 <!-- Specify the app entry point class. -->
1563 <entry-point class="${package}.client.HelloPlugin"/>
1564 <stylesheet src="hello.css"/>
1565</module>
1566----
1567
1568The GWT module must inherit `com.google.gerrit.Plugin` and
1569`com.google.gwt.http.HTTP`.
1570
1571To register the GWT module a `GwtPlugin` needs to be bound.
1572
1573If no Guice modules are declared in the manifest, the GWT plugin may
1574use auto-registration by using the `@Listen` annotation:
1575
1576[source,java]
1577----
1578@Listen
1579public class MyExtension extends GwtPlugin {
1580 public MyExtension() {
1581 super("hello_gwt_plugin");
1582 }
1583}
1584----
1585
1586Otherwise the binding must be done in an `HttpModule`:
1587
1588[source,java]
1589----
1590public class HttpModule extends HttpPluginModule {
1591
1592 @Override
1593 protected void configureServlets() {
1594 DynamicSet.bind(binder(), WebUiPlugin.class)
1595 .toInstance(new GwtPlugin("hello_gwt_plugin"));
1596 }
1597}
1598----
1599
1600The HTTP module above must be declared in the `pom.xml` for Maven
1601driven plugins:
1602
1603[source,xml]
1604----
1605<manifestEntries>
1606 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule>
1607</manifestEntries>
1608----
1609
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001610The name that is provided to the `GwtPlugin` must match the GWT
1611module name compiled into the plugin. The name of the GWT module
1612can be explicitly set in the GWT module XML file by specifying
1613the `rename-to` attribute on the module. It is important that the
1614module name be unique across all plugins installed on the server,
1615as the module name determines the JavaScript namespace used by the
1616compiled plugin code.
Edwin Kempinb74daa92013-11-11 11:28:16 +01001617
1618[source,xml]
1619----
1620<module rename-to="hello_gwt_plugin">
1621----
1622
1623The actual GWT code must be implemented in a class that extends
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001624`com.google.gerrit.plugin.client.PluginEntryPoint`:
Edwin Kempinb74daa92013-11-11 11:28:16 +01001625
1626[source,java]
1627----
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001628public class HelloPlugin extends PluginEntryPoint {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001629
1630 @Override
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001631 public void onPluginLoad() {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001632 // Create the dialog box
1633 final DialogBox dialogBox = new DialogBox();
1634
1635 // The content of the dialog comes from a User specified Preference
1636 dialogBox.setText("Hello from GWT Gerrit UI plugin");
1637 dialogBox.setAnimationEnabled(true);
1638 Button closeButton = new Button("Close");
1639 VerticalPanel dialogVPanel = new VerticalPanel();
1640 dialogVPanel.setWidth("100%");
1641 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
1642 dialogVPanel.add(closeButton);
1643
1644 closeButton.addClickHandler(new ClickHandler() {
1645 public void onClick(ClickEvent event) {
1646 dialogBox.hide();
1647 }
1648 });
1649
1650 // Set the contents of the Widget
1651 dialogBox.setWidget(dialogVPanel);
1652
1653 RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1654 rootPanel.getElement().removeAttribute("href");
1655 rootPanel.addDomHandler(new ClickHandler() {
1656 @Override
1657 public void onClick(ClickEvent event) {
1658 dialogBox.center();
1659 dialogBox.show();
1660 }
1661 }, ClickEvent.getType());
1662 }
1663}
1664----
1665
1666This class must be set as entry point in the GWT module:
1667
1668[source,xml]
1669----
1670<entry-point class="${package}.client.HelloPlugin"/>
1671----
1672
1673In addition this class must be defined as module in the `pom.xml` for the
1674`gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin`
1675must be set to `${project.build.directory}/classes/static`:
1676
1677[source,xml]
1678----
1679<plugin>
1680 <groupId>org.codehaus.mojo</groupId>
1681 <artifactId>gwt-maven-plugin</artifactId>
David Pursehouse7ab81732015-05-07 12:00:47 +09001682 <version>2.7.0</version>
Edwin Kempinb74daa92013-11-11 11:28:16 +01001683 <configuration>
1684 <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module>
1685 <disableClassMetadata>true</disableClassMetadata>
1686 <disableCastChecking>true</disableCastChecking>
1687 <webappDirectory>${project.build.directory}/classes/static</webappDirectory>
1688 </configuration>
1689 <executions>
1690 <execution>
1691 <goals>
1692 <goal>compile</goal>
1693 </goals>
1694 </execution>
1695 </executions>
1696</plugin>
1697----
1698
1699To attach a GWT widget defined by the plugin to the Gerrit core UI
1700`com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the
1701Gerrit core widgets:
1702
1703[source,java]
1704----
1705RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1706rootPanel.getElement().removeAttribute("href");
1707rootPanel.addDomHandler(new ClickHandler() {
1708 @Override
1709 public void onClick(ClickEvent event) {
1710 dialogBox.center();
1711 dialogBox.show();
1712 }
1713}, ClickEvent.getType());
1714----
1715
1716GWT plugins can come with their own css file. This css file must have a
1717unique name and must be registered in the GWT module:
1718
1719[source,xml]
1720----
1721<stylesheet src="hello.css"/>
1722----
1723
Edwin Kempin2570b102013-11-11 11:44:50 +01001724If a GWT plugin wants to invoke the Gerrit REST API it can use
David Pursehouse3a388312014-02-25 16:41:47 +09001725`com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL
Edwin Kempin2570b102013-11-11 11:44:50 +01001726path and to trigger the REST calls.
1727
1728Example for invoking a Gerrit core REST endpoint:
1729
1730[source,java]
1731----
1732new RestApi("projects").id(projectName).view("description")
1733 .put("new description", new AsyncCallback<JavaScriptObject>() {
1734
1735 @Override
1736 public void onSuccess(JavaScriptObject result) {
1737 // TODO
1738 }
1739
1740 @Override
1741 public void onFailure(Throwable caught) {
1742 // never invoked
1743 }
1744});
1745----
1746
1747Example for invoking a REST endpoint defined by a plugin:
1748
1749[source,java]
1750----
1751new RestApi("projects").id(projectName).view("myplugin", "myview")
1752 .get(new AsyncCallback<JavaScriptObject>() {
1753
1754 @Override
1755 public void onSuccess(JavaScriptObject result) {
1756 // TODO
1757 }
1758
1759 @Override
1760 public void onFailure(Throwable caught) {
1761 // never invoked
1762 }
1763});
1764----
1765
1766The `onFailure(Throwable)` of the provided callback is never invoked.
1767If an error occurs, it is shown in an error dialog.
1768
1769In order to be able to do REST calls the GWT module must inherit
1770`com.google.gwt.json.JSON`:
1771
1772[source,xml]
1773----
1774<inherits name="com.google.gwt.json.JSON"/>
1775----
1776
Edwin Kempin15199792014-04-23 16:22:05 +02001777[[screen]]
Shawn Pearced5c844f2013-12-26 15:32:26 -08001778== Add Screen
Edwin Kempin15199792014-04-23 16:22:05 +02001779A link:#gwt_ui_extension[GWT plugin] can link:#top-menu-extensions[add
1780a menu item] that opens a screen that is implemented by the plugin.
1781This way plugin screens can be fully integrated into the Gerrit UI.
Shawn Pearced5c844f2013-12-26 15:32:26 -08001782
1783Example menu item:
1784[source,java]
1785----
1786public class MyMenu implements TopMenu {
1787 private final List<MenuEntry> menuEntries;
1788
1789 @Inject
1790 public MyMenu(@PluginName String name) {
1791 menuEntries = Lists.newArrayList();
1792 menuEntries.add(new MenuEntry("My Menu", Collections.singletonList(
1793 new MenuItem("My Screen", "#/x/" + name + "/my-screen", ""))));
1794 }
1795
1796 @Override
1797 public List<MenuEntry> getEntries() {
1798 return menuEntries;
1799 }
1800}
1801----
1802
1803Example screen:
1804[source,java]
1805----
1806public class MyPlugin extends PluginEntryPoint {
1807 @Override
1808 public void onPluginLoad() {
1809 Plugin.get().screen("my-screen", new Screen.EntryPoint() {
1810 @Override
1811 public void onLoad(Screen screen) {
1812 screen.add(new InlineLabel("My Screen");
1813 screen.show();
1814 }
1815 });
1816 }
1817}
1818----
1819
Edwin Kempin70e11122015-07-08 13:28:40 +02001820[[user-settings-screen]]
1821== Add User Settings Screen
1822
1823A link:#gwt_ui_extension[GWT plugin] can implement a user settings
1824screen that is integrated into the Gerrit user settings menu.
1825
1826Example settings screen:
1827[source,java]
1828----
1829public class MyPlugin extends PluginEntryPoint {
1830 @Override
1831 public void onPluginLoad() {
1832 Plugin.get().settingsScreen("my-preferences", "My Preferences",
1833 new Screen.EntryPoint() {
1834 @Override
1835 public void onLoad(Screen screen) {
1836 screen.setPageTitle("Settings");
1837 screen.add(new InlineLabel("My Preferences"));
1838 screen.show();
1839 }
1840 });
1841 }
1842}
1843----
1844
Edwin Kempinfa0d4942015-07-16 12:38:52 +02001845By defining an link:config-gerrit.html#urlAlias[urlAlias] Gerrit
1846administrators can map plugin screens into the Gerrit URL namespace or
1847even replace Gerrit screens by plugin screens.
1848
Edwin Kempinb1e6a3a2015-07-22 15:36:56 +02001849Plugins may also programatically add URL aliases in the preferences of
1850of a user. This way certain screens can be replaced for certain users.
1851E.g. the plugin may offer a user preferences setting for choosing a
1852screen that then sets/unsets a URL alias for the user.
1853
Edwin Kempin289f1a02014-02-04 16:08:25 +01001854[[settings-screen]]
1855== Plugin Settings Screen
1856
1857If a plugin implements a screen for administrating its settings that is
1858available under "#/x/<plugin-name>/settings" it is automatically linked
1859from the plugin list screen.
1860
Edwin Kempinf5a77332012-07-18 11:17:53 +02001861[[http]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001862== HTTP Servlets
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001863
1864Plugins or extensions may register additional HTTP servlets, and
1865wrap them with HTTP filters.
1866
1867Servlets may use auto-registration to declare the URL they handle:
1868
David Pursehouse68153d72013-09-04 10:09:17 +09001869[source,java]
1870----
1871import com.google.gerrit.extensions.annotations.Export;
1872import com.google.inject.Singleton;
1873import javax.servlet.http.HttpServlet;
1874import javax.servlet.http.HttpServletRequest;
1875import javax.servlet.http.HttpServletResponse;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001876
David Pursehouse68153d72013-09-04 10:09:17 +09001877@Export("/print")
1878@Singleton
1879class HelloServlet extends HttpServlet {
1880 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
1881 res.setContentType("text/plain");
1882 res.setCharacterEncoding("UTF-8");
1883 res.getWriter().write("Hello");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001884 }
David Pursehouse68153d72013-09-04 10:09:17 +09001885}
1886----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001887
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001888The auto registration only works for standard servlet mappings like
Jonathan Nieder5758f182015-03-30 11:28:55 -07001889`/foo` or `+/foo/*+`. Regex style bindings must use a Guice ServletModule
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001890to register the HTTP servlets and declare it explicitly in the manifest
1891with the `Gerrit-HttpModule` attribute:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001892
David Pursehouse68153d72013-09-04 10:09:17 +09001893[source,java]
1894----
1895import com.google.inject.servlet.ServletModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001896
David Pursehouse68153d72013-09-04 10:09:17 +09001897class MyWebUrls extends ServletModule {
1898 protected void configureServlets() {
1899 serve("/print").with(HelloServlet.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001900 }
David Pursehouse68153d72013-09-04 10:09:17 +09001901}
1902----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001903
1904For a plugin installed as name `helloworld`, the servlet implemented
1905by HelloServlet class will be available to users as:
1906
1907----
1908$ curl http://review.example.com/plugins/helloworld/print
1909----
Nasser Grainawie033b262012-05-09 17:54:21 -07001910
Edwin Kempinf5a77332012-07-18 11:17:53 +02001911[[data-directory]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001912== Data Directory
Edwin Kempin41f63912012-07-17 12:33:55 +02001913
Dave Borowitz9e158752015-02-24 10:17:04 -08001914Plugins can request a data directory with a `@PluginData` Path (or File,
1915deprecated) dependency. A data directory will be created automatically
1916by the server in `$site_path/data/$plugin_name` and passed to the
1917plugin.
Edwin Kempin41f63912012-07-17 12:33:55 +02001918
1919Plugins can use this to store any data they want.
1920
David Pursehouse68153d72013-09-04 10:09:17 +09001921[source,java]
1922----
1923@Inject
Dave Borowitz9e158752015-02-24 10:17:04 -08001924MyType(@PluginData java.nio.file.Path myDir) {
1925 this.in = Files.newInputStream(myDir.resolve("my.config"));
David Pursehouse68153d72013-09-04 10:09:17 +09001926}
1927----
Edwin Kempin41f63912012-07-17 12:33:55 +02001928
Dariusz Lukszaebab92a2014-09-10 11:14:19 +02001929[[secure-store]]
1930== SecureStore
1931
1932SecureStore allows to change the way Gerrit stores sensitive data like
1933passwords.
1934
1935In order to replace the default SecureStore (no-op) implementation,
1936a class that extends `com.google.gerrit.server.securestore.SecureStore`
1937needs to be provided (with dependencies) in a separate jar file. Then
1938link:pgm-SwitchSecureStore.html[SwitchSecureStore] must be run to
1939switch implementations.
1940
1941The SecureStore implementation is instantiated using a Guice injector
1942which binds the `File` annotated with the `@SitePath` annotation.
1943This means that a SecureStore implementation class can get access to
1944the `site_path` like in the following example:
1945
1946[source,java]
1947----
1948@Inject
1949MySecureStore(@SitePath java.io.File sitePath) {
1950 // your code
1951}
1952----
1953
1954No Guice bindings or modules are required. Gerrit will automatically
1955discover and bind the implementation.
1956
Michael Ochmann24612652016-02-12 17:26:18 +01001957[[accountcreation]]
1958== Account Creation
1959
1960Plugins can hook into the
1961link:rest-api-accounts.html#create-account[account creation] REST API and
1962inject additional external identifiers for an account that represents a user
1963in some external user store. For that, an implementation of the extension
1964point `com.google.gerrit.server.api.accounts.AccountExternalIdCreator`
1965must be registered.
1966
1967[source,java]
1968----
1969class MyExternalIdCreator implements AccountExternalIdCreator {
1970 @Override
1971 public List<AccountExternalId> create(Account.Id id, String username,
1972 String email) {
1973 // your code
1974 }
1975}
1976
1977bind(AccountExternalIdCreator.class)
1978 .annotatedWith(UniqueAnnotations.create())
1979 .to(MyExternalIdCreator.class);
1980}
1981----
1982
Edwin Kempinea621482013-10-16 12:58:24 +02001983[[download-commands]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001984== Download Commands
Edwin Kempinea621482013-10-16 12:58:24 +02001985
Edwin Kempineafde882015-05-11 15:40:44 +02001986Gerrit offers commands for downloading changes and cloning projects
1987using different download schemes (e.g. for downloading via different
1988network protocols). Plugins can contribute download schemes, download
1989commands and clone commands by implementing
1990`com.google.gerrit.extensions.config.DownloadScheme`,
1991`com.google.gerrit.extensions.config.DownloadCommand` and
1992`com.google.gerrit.extensions.config.CloneCommand`.
Edwin Kempinea621482013-10-16 12:58:24 +02001993
Edwin Kempineafde882015-05-11 15:40:44 +02001994The download schemes, download commands and clone commands which are
1995used most often are provided by the Gerrit core plugin
1996`download-commands`.
Edwin Kempinea621482013-10-16 12:58:24 +02001997
Edwin Kempin78279ba2015-05-22 15:22:41 +02001998[[included-in]]
1999== Included In
2000
2001For merged changes the link:user-review-ui.html#included-in[Included In]
2002drop-down panel shows the branches and tags in which the change is
2003included.
2004
2005Plugins can add additional systems in which the change can be included
2006by implementing `com.google.gerrit.extensions.config.ExternalIncludedIn`,
2007e.g. a plugin can provide a list of servers on which the change was
2008deployed.
2009
Sven Selbergae1a10c2014-02-14 14:24:29 +01002010[[links-to-external-tools]]
2011== Links To External Tools
2012
2013Gerrit has extension points that enables development of a
2014light-weight plugin that links commits to external
2015tools (GitBlit, CGit, company specific resources etc).
2016
2017PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI.
2018
2019[source, java]
2020----
2021import com.google.gerrit.extensions.annotations.Listen;
2022import com.google.gerrit.extensions.webui.PatchSetWebLink;;
Sven Selberga85e64d2014-09-24 10:52:21 +02002023import com.google.gerrit.extensions.webui.WebLinkTarget;
Sven Selbergae1a10c2014-02-14 14:24:29 +01002024
2025@Listen
2026public class MyWeblinkPlugin implements PatchSetWebLink {
2027
2028 private String name = "MyLink";
2029 private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s";
Sven Selberg55484202014-06-26 08:48:51 +02002030 private String imageUrl = "http://placehold.it/16x16.gif";
Sven Selbergae1a10c2014-02-14 14:24:29 +01002031
2032 @Override
Jonathan Niederb3cd6902015-03-12 16:19:15 -07002033 public WebLinkInfo getPatchSetWebLink(String projectName, String commit) {
Sven Selberga85e64d2014-09-24 10:52:21 +02002034 return new WebLinkInfo(name,
2035 imageUrl,
2036 String.format(placeHolderUrlProjectCommit, project, commit),
2037 WebLinkTarget.BLANK);
Edwin Kempinceeed6b2014-09-11 17:07:33 +02002038 }
Sven Selbergae1a10c2014-02-14 14:24:29 +01002039}
2040----
2041
Edwin Kempinb3696c82014-09-11 09:41:42 +02002042FileWebLinks will appear in the side-by-side diff screen on the right
2043side of the patch selection on each side.
2044
Edwin Kempin8cdce502014-12-06 10:55:38 +01002045DiffWebLinks will appear in the side-by-side and unified diff screen in
2046the header next to the navigation icons.
2047
Edwin Kempinea004752014-04-11 15:56:02 +02002048ProjectWebLinks will appear in the project list in the
2049`Repository Browser` column.
2050
Edwin Kempin0f697bd2014-09-10 18:23:29 +02002051BranchWebLinks will appear in the branch list in the last column.
2052
Edwin Kempinf5a77332012-07-18 11:17:53 +02002053[[documentation]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002054== Documentation
Nasser Grainawie033b262012-05-09 17:54:21 -07002055
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002056If a plugin does not register a filter or servlet to handle URLs
Jonathan Nieder5758f182015-03-30 11:28:55 -07002057`+/Documentation/*+` or `+/static/*+`, the core Gerrit server will
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002058automatically export these resources over HTTP from the plugin JAR.
2059
David Pursehouse6853b5a2013-07-10 11:38:03 +09002060Static resources under the `static/` directory in the JAR will be
Dave Borowitzb893ac82013-03-27 10:03:55 -04002061available as `/plugins/helloworld/static/resource`. This prefix is
2062configurable by setting the `Gerrit-HttpStaticPrefix` attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002063
David Pursehouse6853b5a2013-07-10 11:38:03 +09002064Documentation files under the `Documentation/` directory in the JAR
Dave Borowitzb893ac82013-03-27 10:03:55 -04002065will be available as `/plugins/helloworld/Documentation/resource`. This
2066prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix`
2067attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002068
Christian Aistleitner040cf822015-03-26 21:09:09 +01002069Documentation may be written in the Markdown flavor
2070link:https://github.com/sirthias/pegdown[pegdown]
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002071if the file name ends with `.md`. Gerrit will automatically convert
2072Markdown to HTML if accessed with extension `.html`.
Nasser Grainawie033b262012-05-09 17:54:21 -07002073
Edwin Kempinf5a77332012-07-18 11:17:53 +02002074[[macros]]
Edwin Kempinc78777d2012-07-16 15:55:11 +02002075Within the Markdown documentation files macros can be used that allow
2076to write documentation with reasonably accurate examples that adjust
2077automatically based on the installation.
2078
2079The following macros are supported:
2080
2081[width="40%",options="header"]
2082|===================================================
2083|Macro | Replacement
2084|@PLUGIN@ | name of the plugin
2085|@URL@ | Gerrit Web URL
2086|@SSH_HOST@ | SSH Host
2087|@SSH_PORT@ | SSH Port
2088|===================================================
2089
2090The macros will be replaced when the documentation files are rendered
2091from Markdown to HTML.
2092
2093Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@`
2094even if there is an expansion for `KEEP` in the future.
2095
Edwin Kempinf5a77332012-07-18 11:17:53 +02002096[[auto-index]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002097=== Automatic Index
Shawn O. Pearce795167c2012-05-12 11:20:18 -07002098
2099If a plugin does not handle its `/` URL itself, Gerrit will
2100redirect clients to the plugin's `/Documentation/index.html`.
2101Requests for `/Documentation/` (bare directory) will also redirect
2102to `/Documentation/index.html`.
2103
2104If neither resource `Documentation/index.html` or
2105`Documentation/index.md` exists in the plugin JAR, Gerrit will
2106automatically generate an index page for the plugin's documentation
2107tree by scanning every `*.md` and `*.html` file in the Documentation/
2108directory.
2109
2110For any discovered Markdown (`*.md`) file, Gerrit will parse the
2111header of the file and extract the first level one title. This
2112title text will be used as display text for a link to the HTML
2113version of the page.
2114
2115For any discovered HTML (`*.html`) file, Gerrit will use the name
2116of the file, minus the `*.html` extension, as the link text. Any
2117hyphens in the file name will be replaced with spaces.
2118
David Pursehouse6853b5a2013-07-10 11:38:03 +09002119If a discovered file is named `about.md` or `about.html`, its
2120content will be inserted in an 'About' section at the top of the
2121auto-generated index page. If both `about.md` and `about.html`
2122exist, only the first discovered file will be used.
2123
Shawn O. Pearce795167c2012-05-12 11:20:18 -07002124If a discovered file name beings with `cmd-` it will be clustered
David Pursehouse6853b5a2013-07-10 11:38:03 +09002125into a 'Commands' section of the generated index page.
2126
David Pursehousefe529152013-08-14 16:35:06 +09002127If a discovered file name beings with `servlet-` it will be clustered
2128into a 'Servlets' section of the generated index page.
2129
2130If a discovered file name beings with `rest-api-` it will be clustered
2131into a 'REST APIs' section of the generated index page.
2132
David Pursehouse6853b5a2013-07-10 11:38:03 +09002133All other files are clustered under a 'Documentation' section.
Shawn O. Pearce795167c2012-05-12 11:20:18 -07002134
2135Some optional information from the manifest is extracted and
2136displayed as part of the index page, if present in the manifest:
2137
2138[width="40%",options="header"]
2139|===================================================
2140|Field | Source Attribute
2141|Name | Implementation-Title
2142|Vendor | Implementation-Vendor
2143|Version | Implementation-Version
2144|URL | Implementation-URL
2145|API Version | Gerrit-ApiVersion
2146|===================================================
2147
Edwin Kempinf5a77332012-07-18 11:17:53 +02002148[[deployment]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002149== Deployment
Nasser Grainawie033b262012-05-09 17:54:21 -07002150
Edwin Kempinf7295742012-07-16 15:03:46 +02002151Compiled plugins and extensions can be deployed to a running Gerrit
2152server using the link:cmd-plugin-install.html[plugin install] command.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002153
David Pursehousea1d633b2014-05-02 17:21:02 +09002154Web UI plugins distributed as single `.js` file can be deployed
Dariusz Luksza357a2422012-11-12 06:16:26 +01002155without the overhead of JAR packaging, for more information refer to
2156link:cmd-plugin-install.html[plugin install] command.
2157
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002158Plugins can also be copied directly into the server's
Dariusz Luksza357a2422012-11-12 06:16:26 +01002159directory at `$site_path/plugins/$name.(jar|js)`. The name of
2160the JAR file, minus the `.jar` or `.js` extension, will be used as the
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07002161plugin name. Unless disabled, servers periodically scan this
2162directory for updated plugins. The time can be adjusted by
2163link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency].
Deniz Türkoglueb78b602012-05-07 14:02:36 -07002164
Edwin Kempinf7295742012-07-16 15:03:46 +02002165For disabling plugins the link:cmd-plugin-remove.html[plugin remove]
2166command can be used.
2167
Brad Larsond5e87c32012-07-11 12:18:49 -05002168Disabled plugins can be re-enabled using the
2169link:cmd-plugin-enable.html[plugin enable] command.
2170
Edwin Kempinc1a25102015-06-22 14:47:36 +02002171== Known issues and bugs
2172
2173=== Error handling in UI when using the REST API
2174
2175When a plugin invokes a REST endpoint in the UI, it provides an
2176`AsyncCallback` to handle the result. At the moment the
2177`onFailure(Throwable)` of the callback is never invoked, even if there
2178is an error. Errors are always handled by the Gerrit core UI which
2179shows the error dialog. This means currently plugins cannot do any
2180error handling and e.g. ignore expected errors.
2181
2182In the following example the REST endpoint would return '404 Not Found'
2183if there is no HTTP password and the Gerrit core UI would display an
2184error dialog for this. However having no HTTP password is not an error
2185and the plugin may like to handle this case.
2186
2187[source,java]
2188----
2189new RestApi("accounts").id("self").view("password.http")
2190 .get(new AsyncCallback<NativeString>() {
2191
2192 @Override
2193 public void onSuccess(NativeString httpPassword) {
2194 // TODO
2195 }
2196
2197 @Override
2198 public void onFailure(Throwable caught) {
2199 // never invoked
2200 }
2201});
2202----
2203
2204
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08002205== SEE ALSO
David Ostrovskyf86bae52013-09-01 09:10:39 +02002206
2207* link:js-api.html[JavaScript API]
2208* link:dev-rest-api.html[REST API Developers' Notes]
2209
Deniz Türkoglueb78b602012-05-07 14:02:36 -07002210GERRIT
2211------
2212Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07002213
2214SEARCHBOX
2215---------