blob: c59f0e96280dc56b1487ab302e941e55da2179d9 [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 Pursehouse62864b72013-10-17 23:05:08 +090039 -DarchetypeVersion=2.9-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`
277can get `com.google.gerrit.pgm.init.AllProjectsConfig` injected:
278
279[source,java]
280----
281 public class MyInitStep implements InitStep {
282 private final String pluginName;
283 private final ConsoleUI ui;
284 private final AllProjectsConfig allProjectsConfig;
285
286 public MyInitStep(@PluginName String pluginName, ConsoleUI ui,
287 AllProjectsConfig allProjectsConfig) {
288 this.pluginName = pluginName;
289 this.ui = ui;
290 this.allProjectsConfig = allProjectsConfig;
291 }
292
293 @Override
294 public void run() throws Exception {
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100295 }
296
297 @Override
298 public void postRun() throws Exception {
Edwin Kempind4cfac12013-11-27 11:22:34 +0100299 ui.message("\n");
300 ui.header(pluginName + " Integration");
301 boolean enabled = ui.yesno(true, "By default enabled for all projects");
302 Config cfg = allProjectsConfig.load();
303 if (enabled) {
304 cfg.setBoolean("plugin", pluginName, "enabled", enabled);
305 } else {
306 cfg.unset("plugin", pluginName, "enabled");
307 }
308 allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration");
309 }
310 }
311----
312
Luca Milanesio737285d2012-09-25 14:26:43 +0100313Bear in mind that the Plugin's InitStep class will be loaded but
314the standard Gerrit runtime environment is not available and the plugin's
315own Guice modules were not initialized.
316This means the InitStep for a plugin is not executed in the same way that
317the plugin executes within the server, and may mean a plugin author cannot
318trivially reuse runtime code during init.
319
320For instance a plugin that wants to verify connectivity may need to statically
321call the constructor of their connection class, passing in values obtained
322from the Section.Factory rather than from an injected Config object.
323
David Pursehoused128c892013-10-22 21:52:21 +0900324Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after
325the extraction of the plugins embedded in the distribution .war file into
326`$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade.
327
328A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit
329runtime objects injected at startup.
Luca Milanesio737285d2012-09-25 14:26:43 +0100330
David Pursehouse68153d72013-09-04 10:09:17 +0900331[source,java]
332----
333public class MyInitStep implements InitStep {
334 private final ConsoleUI ui;
335 private final Section.Factory sections;
336 private final String pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100337
David Pursehouse68153d72013-09-04 10:09:17 +0900338 @Inject
339 public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) {
340 this.ui = ui;
341 this.sections = sections;
342 this.pluginName = pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100343 }
David Pursehouse68153d72013-09-04 10:09:17 +0900344
345 @Override
346 public void run() throws Exception {
347 ui.header("\nMy plugin");
348
349 Section mySection = getSection("myplugin", null);
350 mySection.string("Link name", "linkname", "MyLink");
351 }
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100352
353 @Override
354 public void postRun() throws Exception {
355 }
David Pursehouse68153d72013-09-04 10:09:17 +0900356}
357----
Luca Milanesio737285d2012-09-25 14:26:43 +0100358
Edwin Kempinf5a77332012-07-18 11:17:53 +0200359[[classpath]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800360== Classpath
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700361
362Each plugin is loaded into its own ClassLoader, isolating plugins
363from each other. A plugin or extension inherits the Java runtime
364and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin)
365from the hosting server.
366
367Plugins are loaded from a single JAR file. If a plugin needs
368additional libraries, it must include those dependencies within
369its own JAR. Plugins built using Maven may be able to use the
370link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin]
371to package additional dependencies. Relocating (or renaming) classes
372should not be necessary due to the ClassLoader isolation.
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700373
Edwin Kempin98202662013-09-18 16:03:03 +0200374[[events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800375== Listening to Events
Edwin Kempin98202662013-09-18 16:03:03 +0200376
377Certain operations in Gerrit trigger events. Plugins may receive
378notifications of these events by implementing the corresponding
379listeners.
380
Edwin Kempin64059f52013-10-31 13:49:25 +0100381* `com.google.gerrit.common.ChangeListener`:
382+
383Allows to listen to change events. These are the same
384link:cmd-stream-events.html#events[events] that are also streamed by
385the link:cmd-stream-events.html[gerrit stream-events] command.
386
Edwin Kempin98202662013-09-18 16:03:03 +0200387* `com.google.gerrit.extensions.events.LifecycleListener`:
388+
Edwin Kempin3e7928a2013-12-03 07:39:00 +0100389Plugin start and stop
Edwin Kempin98202662013-09-18 16:03:03 +0200390
391* `com.google.gerrit.extensions.events.NewProjectCreatedListener`:
392+
393Project creation
394
395* `com.google.gerrit.extensions.events.ProjectDeletedListener`:
396+
397Project deletion
398
Edwin Kempinb27c9392013-11-19 13:12:43 +0100399* `com.google.gerrit.extensions.events.HeadUpdatedListener`:
400+
401Update of HEAD on a project
402
Yang Zhenhui2659d422013-07-30 16:59:58 +0800403[[stream-events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800404== Sending Events to the Events Stream
Yang Zhenhui2659d422013-07-30 16:59:58 +0800405
406Plugins may send events to the events stream where consumers of
407Gerrit's `stream-events` ssh command will receive them.
408
409To send an event, the plugin must invoke one of the `postEvent`
410methods in the `ChangeHookRunner` class, passing an instance of
411its own custom event class derived from `ChangeEvent`.
412
Edwin Kempin32737602014-01-23 09:04:58 +0100413[[validation]]
David Pursehouse91c5f5e2014-01-23 18:57:33 +0900414== Validation Listeners
Edwin Kempin32737602014-01-23 09:04:58 +0100415
416Certain operations in Gerrit can be validated by plugins by
417implementing the corresponding link:config-validation.html[listeners].
418
Saša Živkovec85a072014-01-28 10:08:25 +0100419[[receive-pack]]
420== Receive Pack Initializers
421
422Plugins may provide ReceivePack initializers which will be invoked
423by Gerrit just before a ReceivePack instance will be used. Usually,
424plugins will make use of the setXXX methods on the ReceivePack to
425set additional properties on it.
426
Edwin Kempinf5a77332012-07-18 11:17:53 +0200427[[ssh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800428== SSH Commands
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700429
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700430Plugins may provide commands that can be accessed through the SSH
431interface (extensions do not have this option).
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700432
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700433Command implementations must extend the base class SshCommand:
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700434
David Pursehouse68153d72013-09-04 10:09:17 +0900435[source,java]
436----
437import com.google.gerrit.sshd.SshCommand;
David Ostrovskyb7d97752013-11-09 05:23:26 +0100438import com.google.gerrit.sshd.CommandMetaData;
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700439
Ian Bulle1a12202014-02-16 17:15:42 -0800440@CommandMetaData(name="print", description="Print hello command")
David Pursehouse68153d72013-09-04 10:09:17 +0900441class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800442 @Override
443 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900444 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700445 }
David Pursehouse68153d72013-09-04 10:09:17 +0900446}
447----
Nasser Grainawie033b262012-05-09 17:54:21 -0700448
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700449If no Guice modules are declared in the manifest, SSH commands may
Edwin Kempin948de0f2012-07-16 10:34:35 +0200450use auto-registration by providing an `@Export` annotation:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700451
David Pursehouse68153d72013-09-04 10:09:17 +0900452[source,java]
453----
454import com.google.gerrit.extensions.annotations.Export;
455import com.google.gerrit.sshd.SshCommand;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700456
David Pursehouse68153d72013-09-04 10:09:17 +0900457@Export("print")
458class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800459 @Override
460 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900461 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700462 }
David Pursehouse68153d72013-09-04 10:09:17 +0900463}
464----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700465
466If explicit registration is being used, a Guice module must be
467supplied to register the SSH command and declared in the manifest
468with the `Gerrit-SshModule` attribute:
469
David Pursehouse68153d72013-09-04 10:09:17 +0900470[source,java]
471----
472import com.google.gerrit.sshd.PluginCommandModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700473
David Pursehouse68153d72013-09-04 10:09:17 +0900474class MyCommands extends PluginCommandModule {
Ian Bulle1a12202014-02-16 17:15:42 -0800475 @Override
David Pursehouse68153d72013-09-04 10:09:17 +0900476 protected void configureCommands() {
David Ostrovskyb7d97752013-11-09 05:23:26 +0100477 command(PrintHello.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700478 }
David Pursehouse68153d72013-09-04 10:09:17 +0900479}
480----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700481
482For a plugin installed as name `helloworld`, the command implemented
483by PrintHello class will be available to users as:
484
485----
Keunhong Parka09a6f12012-07-10 14:45:02 -0600486$ ssh -p 29418 review.example.com helloworld print
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700487----
488
David Ostrovskye3172b32013-10-13 14:19:13 +0200489Multiple SSH commands can be bound to the same implementation class. For
490example a Gerrit Shell plugin can bind different shell commands to the same
491implementation class:
492
493[source,java]
494----
495public class SshShellModule extends PluginCommandModule {
496 @Override
497 protected void configureCommands() {
498 command("ls").to(ShellCommand.class);
499 command("ps").to(ShellCommand.class);
500 [...]
501 }
502}
503----
504
505With the possible implementation:
506
507[source,java]
508----
509public class ShellCommand extends SshCommand {
510 @Override
511 protected void run() throws UnloggedFailure {
512 String cmd = getName().substring(getPluginName().length() + 1);
513 ProcessBuilder proc = new ProcessBuilder(cmd);
514 Process cmd = proc.start();
515 [...]
516 }
517}
518----
519
520And the call:
521
522----
523$ ssh -p 29418 review.example.com shell ls
524$ ssh -p 29418 review.example.com shell ps
525----
526
David Ostrovskyb7d97752013-11-09 05:23:26 +0100527Single command plugins are also supported. In this scenario plugin binds
528SSH command to its own name. `SshModule` must inherit from
529`SingleCommandPluginModule` class:
530
531[source,java]
532----
533public class SshModule extends SingleCommandPluginModule {
534 @Override
535 protected void configure(LinkedBindingBuilder<Command> b) {
536 b.to(ShellCommand.class);
537 }
538}
539----
540
541If the plugin above is deployed under sh.jar file in `$site/plugins`
David Pursehouse659860f2013-12-16 14:50:04 +0900542directory, generic commands can be called without specifying the
David Ostrovskyb7d97752013-11-09 05:23:26 +0100543actual SSH command. Note in the example below, that the called commands
544`ls` and `ps` was not explicitly bound:
545
546----
547$ ssh -p 29418 review.example.com sh ls
548$ ssh -p 29418 review.example.com sh ps
549----
550
Edwin Kempin78ca0942013-10-30 11:24:06 +0100551[[simple-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800552== Simple Configuration in `gerrit.config`
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200553
554In Gerrit, global configuration is stored in the `gerrit.config` file.
555If a plugin needs global configuration, this configuration should be
556stored in a `plugin` subsection in the `gerrit.config` file.
557
Edwin Kempinc9b68602013-10-30 09:32:43 +0100558This approach of storing the plugin configuration is only suitable for
559plugins that have a simple configuration that only consists of
560key-value pairs. With this approach it is not possible to have
561subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin78ca0942013-10-30 11:24:06 +0100562configuration need to store their configuration in their
563link:#configuration[own configuration file] where they can make use of
564subsections. On the other hand storing the plugin configuration in a
565'plugin' subsection in the `gerrit.config` file has the advantage that
566administrators have all configuration parameters in one file, instead
567of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100568
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200569To avoid conflicts with other plugins, it is recommended that plugins
570only use the `plugin` subsection with their own name. For example the
571`helloworld` plugin should store its configuration in the
572`plugin.helloworld` subsection:
573
574----
575[plugin "helloworld"]
576 language = Latin
577----
578
Sasa Zivkovacdf5332013-09-20 14:05:15 +0200579Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200580plugin can easily access its configuration and there is no need for a
581plugin to parse the `gerrit.config` file on its own:
582
583[source,java]
584----
David Pursehouse529ec252013-09-27 13:45:14 +0900585@Inject
586private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200587
David Pursehoused128c892013-10-22 21:52:21 +0900588[...]
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200589
Edwin Kempin122622d2013-10-29 16:45:44 +0100590String language = cfg.getFromGerritConfig("helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900591 .getString("language", "English");
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200592----
593
Edwin Kempin78ca0942013-10-30 11:24:06 +0100594[[configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800595== Configuration in own config file
Edwin Kempin78ca0942013-10-30 11:24:06 +0100596
597Plugins can store their configuration in an own configuration file.
598This makes sense if the plugin configuration is rather complex and
599requires the usage of subsections. Plugins that have a simple
600key-value pair configuration can store their configuration in a
601link:#simple-configuration[`plugin` subsection of the `gerrit.config`
602file].
603
604The plugin configuration file must be named after the plugin and must
605be located in the `etc` folder of the review site. For example a
606configuration file for a `default-reviewer` plugin could look like
607this:
608
609.$site_path/etc/default-reviewer.config
610----
611[branch "refs/heads/master"]
612 reviewer = Project Owners
613 reviewer = john.doe@example.com
614[match "file:^.*\.txt"]
615 reviewer = My Info Developers
616----
617
618Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
619plugin can easily access its configuration:
620
621[source,java]
622----
623@Inject
624private com.google.gerrit.server.config.PluginConfigFactory cfg;
625
626[...]
627
628String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer")
629 .getStringList("branch", "refs/heads/master", "reviewer");
630----
631
632The plugin configuration is loaded only once and is then cached.
633Similar to changes in 'gerrit.config', changes to the plugin
634configuration file will only become effective after a Gerrit restart.
635
Edwin Kempin705f2842013-10-30 14:25:31 +0100636[[simple-project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800637== Simple Project Specific Configuration in `project.config`
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200638
639In Gerrit, project specific configuration is stored in the project's
640`project.config` file on the `refs/meta/config` branch. If a plugin
641needs configuration on project level (e.g. to enable its functionality
642only for certain projects), this configuration should be stored in a
643`plugin` subsection in the project's `project.config` file.
644
Edwin Kempinc9b68602013-10-30 09:32:43 +0100645This approach of storing the plugin configuration is only suitable for
646plugins that have a simple configuration that only consists of
647key-value pairs. With this approach it is not possible to have
648subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin705f2842013-10-30 14:25:31 +0100649configuration need to store their configuration in their
650link:#project-specific-configuration[own configuration file] where they
651can make use of subsections. On the other hand storing the plugin
652configuration in a 'plugin' subsection in the `project.config` file has
653the advantage that project owners have all configuration parameters in
654one file, instead of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100655
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200656To avoid conflicts with other plugins, it is recommended that plugins
657only use the `plugin` subsection with their own name. For example the
658`helloworld` plugin should store its configuration in the
659`plugin.helloworld` subsection:
660
661----
662 [plugin "helloworld"]
663 enabled = true
664----
665
666Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
667plugin can easily access its project specific configuration and there
668is no need for a plugin to parse the `project.config` file on its own:
669
670[source,java]
671----
David Pursehouse529ec252013-09-27 13:45:14 +0900672@Inject
673private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200674
David Pursehoused128c892013-10-22 21:52:21 +0900675[...]
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200676
Edwin Kempin122622d2013-10-29 16:45:44 +0100677boolean enabled = cfg.getFromProjectConfig(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900678 .getBoolean("enabled", false);
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200679----
680
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200681It is also possible to get missing configuration parameters inherited
682from the parent projects:
683
684[source,java]
685----
David Pursehouse529ec252013-09-27 13:45:14 +0900686@Inject
687private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200688
David Pursehoused128c892013-10-22 21:52:21 +0900689[...]
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200690
Edwin Kempin122622d2013-10-29 16:45:44 +0100691boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900692 .getBoolean("enabled", false);
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200693----
694
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200695Project owners can edit the project configuration by fetching the
696`refs/meta/config` branch, editing the `project.config` file and
697pushing the commit back.
698
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100699Plugin configuration values that are stored in the `project.config`
700file can be exposed in the ProjectInfoScreen to allow project owners
701to see and edit them from the UI.
702
703For this an instance of `ProjectConfigEntry` needs to be bound for each
704parameter. The export name must be a valid Git variable name. The
705variable name is case-insensitive, allows only alphanumeric characters
706and '-', and must start with an alphabetic character.
707
Edwin Kempina6c1c452013-11-28 16:55:22 +0100708The example below shows how the parameters `plugin.helloworld.enabled`
709and `plugin.helloworld.language` are bound to be editable from the
710WebUI. For the parameter `plugin.helloworld.enabled` "Enable Greeting"
711is provided as display name and the default value is set to `true`.
712For the parameter `plugin.helloworld.language` "Preferred Language"
713is provided as display name and "en" is set as default value.
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100714
715[source,java]
716----
717class Module extends AbstractModule {
718 @Override
719 protected void configure() {
720 bind(ProjectConfigEntry.class)
Edwin Kempina6c1c452013-11-28 16:55:22 +0100721 .annotatedWith(Exports.named("enabled"))
722 .toInstance(new ProjectConfigEntry("Enable Greeting", true));
723 bind(ProjectConfigEntry.class)
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100724 .annotatedWith(Exports.named("language"))
725 .toInstance(new ProjectConfigEntry("Preferred Language", "en"));
726 }
727}
728----
729
Edwin Kempinb64d3972013-11-17 18:55:48 +0100730By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins
731can be notified when this configuration parameter is updated on a
732project.
733
Edwin Kempin705f2842013-10-30 14:25:31 +0100734[[project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800735== Project Specific Configuration in own config file
Edwin Kempin705f2842013-10-30 14:25:31 +0100736
737Plugins can store their project specific configuration in an own
738configuration file in the projects `refs/meta/config` branch.
739This makes sense if the plugins project specific configuration is
740rather complex and requires the usage of subsections. Plugins that
741have a simple key-value pair configuration can store their project
742specific configuration in a link:#simple-project-specific-configuration[
743`plugin` subsection of the `project.config` file].
744
745The plugin configuration file in the `refs/meta/config` branch must be
746named after the plugin. For example a configuration file for a
747`default-reviewer` plugin could look like this:
748
749.default-reviewer.config
750----
751[branch "refs/heads/master"]
752 reviewer = Project Owners
753 reviewer = john.doe@example.com
754[match "file:^.*\.txt"]
755 reviewer = My Info Developers
756----
757
758Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
759plugin can easily access its project specific configuration:
760
761[source,java]
762----
763@Inject
764private com.google.gerrit.server.config.PluginConfigFactory cfg;
765
766[...]
767
768String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer")
769 .getStringList("branch", "refs/heads/master", "reviewer");
770----
771
Edwin Kempin762da382013-10-30 14:50:01 +0100772It is also possible to get missing configuration parameters inherited
773from the parent projects:
774
775[source,java]
776----
777@Inject
778private com.google.gerrit.server.config.PluginConfigFactory cfg;
779
780[...]
781
782String[] reviewers = cfg.getFromPluginConfigWithInheritance(project, "default-reviewer")
783 .getStringList("branch", "refs/heads/master", "reviewer");
784----
785
Edwin Kempin705f2842013-10-30 14:25:31 +0100786Project owners can edit the project configuration by fetching the
787`refs/meta/config` branch, editing the `<plugin-name>.config` file and
788pushing the commit back.
789
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800790== React on changes in project configuration
Edwin Kempina46b6c92013-12-04 21:05:24 +0100791
792If a plugin wants to react on changes in the project configuration, it
793can implement a `GitReferenceUpdatedListener` and filter on events for
794the `refs/meta/config` branch:
795
796[source,java]
797----
798public class MyListener implements GitReferenceUpdatedListener {
799
800 private final MetaDataUpdate.Server metaDataUpdateFactory;
801
802 @Inject
803 MyListener(MetaDataUpdate.Server metaDataUpdateFactory) {
804 this.metaDataUpdateFactory = metaDataUpdateFactory;
805 }
806
807 @Override
808 public void onGitReferenceUpdated(Event event) {
Edwin Kempina951ba52014-01-03 14:07:28 +0100809 if (event.getRefName().equals(RefNames.REFS_CONFIG)) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100810 Project.NameKey p = new Project.NameKey(event.getProjectName());
811 try {
Edwin Kempina951ba52014-01-03 14:07:28 +0100812 ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId());
813 ProjectConfig newCfg = parseConfig(p, event.getNewObjectId());
Edwin Kempina46b6c92013-12-04 21:05:24 +0100814
Edwin Kempina951ba52014-01-03 14:07:28 +0100815 if (oldCfg != null && newCfg != null
816 && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100817 // submit type has changed
818 ...
819 }
820 } catch (IOException | ConfigInvalidException e) {
821 ...
822 }
823 }
824 }
Edwin Kempina951ba52014-01-03 14:07:28 +0100825
826 private ProjectConfig parseConfig(Project.NameKey p, String idStr)
827 throws IOException, ConfigInvalidException, RepositoryNotFoundException {
828 ObjectId id = ObjectId.fromString(idStr);
829 if (ObjectId.zeroId().equals(id)) {
830 return null;
831 }
832 return ProjectConfig.read(metaDataUpdateFactory.create(p), id);
833 }
Edwin Kempina46b6c92013-12-04 21:05:24 +0100834}
835----
836
837
David Ostrovsky7066cc02013-06-15 14:46:23 +0200838[[capabilities]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800839== Plugin Owned Capabilities
David Ostrovsky7066cc02013-06-15 14:46:23 +0200840
841Plugins may provide their own capabilities and restrict usage of SSH
842commands to the users who are granted those capabilities.
843
844Plugins define the capabilities by overriding the `CapabilityDefinition`
845abstract class:
846
David Pursehouse68153d72013-09-04 10:09:17 +0900847[source,java]
848----
849public class PrintHelloCapability extends CapabilityDefinition {
850 @Override
851 public String getDescription() {
852 return "Print Hello";
David Ostrovsky7066cc02013-06-15 14:46:23 +0200853 }
David Pursehouse68153d72013-09-04 10:09:17 +0900854}
855----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200856
David Ostrovskyf86bae52013-09-01 09:10:39 +0200857If no Guice modules are declared in the manifest, UI actions may
David Ostrovsky7066cc02013-06-15 14:46:23 +0200858use auto-registration by providing an `@Export` annotation:
859
David Pursehouse68153d72013-09-04 10:09:17 +0900860[source,java]
861----
862@Export("printHello")
863public class PrintHelloCapability extends CapabilityDefinition {
David Pursehoused128c892013-10-22 21:52:21 +0900864 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900865}
866----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200867
868Otherwise the capability must be bound in a plugin module:
869
David Pursehouse68153d72013-09-04 10:09:17 +0900870[source,java]
871----
872public class HelloWorldModule extends AbstractModule {
873 @Override
874 protected void configure() {
875 bind(CapabilityDefinition.class)
876 .annotatedWith(Exports.named("printHello"))
877 .to(PrintHelloCapability.class);
David Ostrovsky7066cc02013-06-15 14:46:23 +0200878 }
David Pursehouse68153d72013-09-04 10:09:17 +0900879}
880----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200881
882With a plugin-owned capability defined in this way, it is possible to restrict
David Ostrovskyf86bae52013-09-01 09:10:39 +0200883usage of an SSH command or `UiAction` to members of the group that were granted
David Ostrovsky7066cc02013-06-15 14:46:23 +0200884this capability in the usual way, using the `RequiresCapability` annotation:
885
David Pursehouse68153d72013-09-04 10:09:17 +0900886[source,java]
887----
888@RequiresCapability("printHello")
889@CommandMetaData(name="print", description="Print greeting in different languages")
890public final class PrintHelloWorldCommand extends SshCommand {
David Pursehoused128c892013-10-22 21:52:21 +0900891 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900892}
893----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200894
David Ostrovskyf86bae52013-09-01 09:10:39 +0200895Or with `UiAction`:
David Ostrovsky7066cc02013-06-15 14:46:23 +0200896
David Pursehouse68153d72013-09-04 10:09:17 +0900897[source,java]
898----
899@RequiresCapability("printHello")
900public class SayHelloAction extends UiAction<RevisionResource>
901 implements RestModifyView<RevisionResource, SayHelloAction.Input> {
David Pursehoused128c892013-10-22 21:52:21 +0900902 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900903}
904----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200905
906Capability scope was introduced to differentiate between plugin-owned
David Pursehousebf053342013-09-05 14:55:29 +0900907capabilities and core capabilities. Per default the scope of the
908`@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means:
909
David Ostrovsky7066cc02013-06-15 14:46:23 +0200910* when `@RequiresCapability` is used within a plugin the scope of the
911capability is assumed to be that plugin.
David Pursehousebf053342013-09-05 14:55:29 +0900912
David Ostrovsky7066cc02013-06-15 14:46:23 +0200913* If `@RequiresCapability` is used within the core Gerrit Code Review server
914(and thus is outside of a plugin) the scope is the core server and will use
915the `GlobalCapability` known to Gerrit Code Review server.
916
917If a plugin needs to use a core capability name (e.g. "administrateServer")
918this can be specified by setting `scope = CapabilityScope.CORE`:
919
David Pursehouse68153d72013-09-04 10:09:17 +0900920[source,java]
921----
922@RequiresCapability(value = "administrateServer", scope =
923 CapabilityScope.CORE)
David Pursehoused128c892013-10-22 21:52:21 +0900924 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900925----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200926
David Ostrovskyf86bae52013-09-01 09:10:39 +0200927[[ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800928== UI Extension
David Ostrovskyf86bae52013-09-01 09:10:39 +0200929
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100930Plugins can contribute UI actions on core Gerrit pages. This is useful
931for workflow customization or exposing plugin functionality through the
932UI in addition to SSH commands and the REST API.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200933
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100934For instance a plugin to integrate Jira with Gerrit changes may
935contribute a "File bug" button to allow filing a bug from the change
936page or plugins to integrate continuous integration systems may
937contribute a "Schedule" button to allow a CI build to be scheduled
938manually from the patch set panel.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200939
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100940Two different places on core Gerrit pages are supported:
David Ostrovskyf86bae52013-09-01 09:10:39 +0200941
942* Change screen
943* Project info screen
944
945Plugins contribute UI actions by implementing the `UiAction` interface:
946
David Pursehouse68153d72013-09-04 10:09:17 +0900947[source,java]
948----
949@RequiresCapability("printHello")
950class HelloWorldAction implements UiAction<RevisionResource>,
951 RestModifyView<RevisionResource, HelloWorldAction.Input> {
952 static class Input {
953 boolean french;
954 String message;
David Ostrovskyf86bae52013-09-01 09:10:39 +0200955 }
David Pursehouse68153d72013-09-04 10:09:17 +0900956
957 private Provider<CurrentUser> user;
958
959 @Inject
960 HelloWorldAction(Provider<CurrentUser> user) {
961 this.user = user;
962 }
963
964 @Override
965 public String apply(RevisionResource rev, Input input) {
966 final String greeting = input.french
967 ? "Bonjour"
968 : "Hello";
969 return String.format("%s %s from change %s, patch set %d!",
970 greeting,
971 Strings.isNullOrEmpty(input.message)
972 ? Objects.firstNonNull(user.get().getUserName(), "world")
973 : input.message,
974 rev.getChange().getId().toString(),
975 rev.getPatchSet().getPatchSetId());
976 }
977
978 @Override
979 public Description getDescription(
980 RevisionResource resource) {
981 return new Description()
982 .setLabel("Say hello")
983 .setTitle("Say hello in different languages");
984 }
985}
986----
David Ostrovskyf86bae52013-09-01 09:10:39 +0200987
David Ostrovsky450eefe2013-10-21 21:18:11 +0200988Sometimes plugins may want to be able to change the state of a patch set or
989change in the `UiAction.apply()` method and reflect these changes on the core
990UI. For example a buildbot plugin which exposes a 'Schedule' button on the
991patch set panel may want to disable that button after the build was scheduled
992and update the tooltip of that button. But because of Gerrit's caching
993strategy the following must be taken into consideration.
994
995The browser is allowed to cache the `UiAction` information until something on
996the change is modified. More accurately the change row needs to be modified in
997the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or
998the +refs/meta/config+ of the project or any parents needs to change to a new
999SHA-1. The ETag SHA-1 computation code can be found in the
1000`ChangeResource.getETag()` method.
1001
David Pursehoused128c892013-10-22 21:52:21 +09001002The easiest way to accomplish this is to update `lastUpdatedOn` of the change:
David Ostrovsky450eefe2013-10-21 21:18:11 +02001003
1004[source,java]
1005----
1006@Override
1007public Object apply(RevisionResource rcrs, Input in) {
1008 // schedule a build
1009 [...]
1010 // update change
1011 ReviewDb db = dbProvider.get();
1012 db.changes().beginTransaction(change.getId());
1013 try {
1014 change = db.changes().atomicUpdate(
1015 change.getId(),
1016 new AtomicUpdate<Change>() {
1017 @Override
1018 public Change update(Change change) {
1019 ChangeUtil.updated(change);
1020 return change;
1021 }
1022 });
1023 db.commit();
1024 } finally {
1025 db.rollback();
1026 }
David Pursehoused128c892013-10-22 21:52:21 +09001027 [...]
David Ostrovsky450eefe2013-10-21 21:18:11 +02001028}
1029----
1030
David Ostrovskyf86bae52013-09-01 09:10:39 +02001031`UiAction` must be bound in a plugin module:
1032
David Pursehouse68153d72013-09-04 10:09:17 +09001033[source,java]
1034----
1035public class Module extends AbstractModule {
1036 @Override
1037 protected void configure() {
1038 install(new RestApiModule() {
1039 @Override
1040 protected void configure() {
1041 post(REVISION_KIND, "say-hello")
1042 .to(HelloWorldAction.class);
1043 }
1044 });
David Ostrovskyf86bae52013-09-01 09:10:39 +02001045 }
David Pursehouse68153d72013-09-04 10:09:17 +09001046}
1047----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001048
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001049The module above must be declared in the `pom.xml` for Maven driven
1050plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001051
David Pursehouse68153d72013-09-04 10:09:17 +09001052[source,xml]
1053----
1054<manifestEntries>
1055 <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module>
1056</manifestEntries>
1057----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001058
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001059or in the `BUCK` configuration file for Buck driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001060
David Pursehouse68153d72013-09-04 10:09:17 +09001061[source,python]
1062----
1063manifest_entries = [
1064 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module',
1065]
1066----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001067
1068In some use cases more user input must be gathered, for that `UiAction` can be
1069combined with the JavaScript API. This would display a small popup near the
1070activation button to gather additional input from the user. The JS file is
1071typically put in the `static` folder within the plugin's directory:
1072
David Pursehouse68153d72013-09-04 10:09:17 +09001073[source,javascript]
1074----
1075Gerrit.install(function(self) {
1076 function onSayHello(c) {
1077 var f = c.textfield();
1078 var t = c.checkbox();
1079 var b = c.button('Say hello', {onclick: function(){
1080 c.call(
1081 {message: f.value, french: t.checked},
1082 function(r) {
1083 c.hide();
1084 window.alert(r);
1085 c.refresh();
1086 });
1087 }});
1088 c.popup(c.div(
1089 c.prependLabel('Greeting message', f),
1090 c.br(),
1091 c.label(t, 'french'),
1092 c.br(),
1093 b));
1094 f.focus();
1095 }
1096 self.onAction('revision', 'say-hello', onSayHello);
1097});
1098----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001099
1100The JS module must be exposed as a `WebUiPlugin` and bound as
1101an HTTP Module:
1102
David Pursehouse68153d72013-09-04 10:09:17 +09001103[source,java]
1104----
1105public class HttpModule extends HttpPluginModule {
1106 @Override
1107 protected void configureServlets() {
1108 DynamicSet.bind(binder(), WebUiPlugin.class)
1109 .toInstance(new JavaScriptPlugin("hello.js"));
David Ostrovskyf86bae52013-09-01 09:10:39 +02001110 }
David Pursehouse68153d72013-09-04 10:09:17 +09001111}
1112----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001113
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001114The HTTP module above must be declared in the `pom.xml` for Maven
1115driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001116
David Pursehouse68153d72013-09-04 10:09:17 +09001117[source,xml]
1118----
1119<manifestEntries>
1120 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule>
1121</manifestEntries>
1122----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001123
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001124or in the `BUCK` configuration file for Buck driven plugins
David Ostrovskyf86bae52013-09-01 09:10:39 +02001125
David Pursehouse68153d72013-09-04 10:09:17 +09001126[source,python]
1127----
1128manifest_entries = [
1129 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule',
1130]
1131----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001132
1133If `UiAction` is annotated with the `@RequiresCapability` annotation, then the
1134capability check is done during the `UiAction` gathering, so the plugin author
1135doesn't have to set `UiAction.Description.setVisible()` explicitly in this
1136case.
1137
1138The following prerequisities must be met, to satisfy the capability check:
1139
1140* user is authenticated
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001141* user is a member of a group which has the `Administrate Server` capability, or
David Ostrovskyf86bae52013-09-01 09:10:39 +02001142* user is a member of a group which has the required capability
1143
1144The `apply` method is called when the button is clicked. If `UiAction` is
1145combined with JavaScript API (its own JavaScript function is provided),
1146then a popup dialog is normally opened to gather additional user input.
1147A new button is placed on the popup dialog to actually send the request.
1148
1149Every `UiAction` exposes a REST API endpoint. The endpoint from the example above
1150can be accessed from any REST client, i. e.:
1151
1152====
1153 curl -X POST -H "Content-Type: application/json" \
1154 -d '{message: "François", french: true}' \
1155 --digest --user joe:secret \
1156 http://host:port/a/changes/1/revisions/1/cookbook~say-hello
1157 "Bonjour François from change 1, patch set 1!"
1158====
1159
David Pursehouse42245822013-09-24 09:48:20 +09001160A special case is to bind an endpoint without a view name. This is
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001161particularly useful for `DELETE` requests:
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001162
1163[source,java]
1164----
1165public class Module extends AbstractModule {
1166 @Override
1167 protected void configure() {
1168 install(new RestApiModule() {
1169 @Override
1170 protected void configure() {
1171 delete(PROJECT_KIND)
1172 .to(DeleteProject.class);
1173 }
1174 });
1175 }
1176}
1177----
1178
David Pursehouse42245822013-09-24 09:48:20 +09001179For a `UiAction` bound this way, a JS API function can be provided.
1180
1181Currently only one restriction exists: per plugin only one `UiAction`
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001182can be bound per resource without view name. To define a JS function
1183for the `UiAction`, "/" must be used as the name:
1184
1185[source,javascript]
1186----
1187Gerrit.install(function(self) {
1188 function onDeleteProject(c) {
1189 [...]
1190 }
1191 self.onAction('project', '/', onDeleteProject);
1192});
1193----
1194
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001195[[top-menu-extensions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001196== Top Menu Extensions
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001197
1198Plugins can contribute items to Gerrit's top menu.
1199
1200A single top menu extension can have multiple elements and will be put as
1201the last element in Gerrit's top menu.
1202
1203Plugins define the top menu entries by implementing `TopMenu` interface:
1204
1205[source,java]
1206----
1207public class MyTopMenuExtension implements TopMenu {
1208
1209 @Override
1210 public List<MenuEntry> getEntries() {
1211 return Lists.newArrayList(
1212 new MenuEntry("Top Menu Entry", Lists.newArrayList(
1213 new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1214 }
1215}
1216----
1217
Edwin Kempin77f23242013-09-30 14:53:20 +02001218Plugins can also add additional menu items to Gerrit's top menu entries
1219by defining a `MenuEntry` that has the same name as a Gerrit top menu
1220entry:
1221
1222[source,java]
1223----
1224public class MyTopMenuExtension implements TopMenu {
1225
1226 @Override
1227 public List<MenuEntry> getEntries() {
1228 return Lists.newArrayList(
Dariusz Luksza2d3afab2013-10-01 11:07:13 +02001229 new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList(
Edwin Kempin77f23242013-09-30 14:53:20 +02001230 new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
1231 }
1232}
1233----
1234
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001235If no Guice modules are declared in the manifest, the top menu extension may use
1236auto-registration by providing an `@Listen` annotation:
1237
1238[source,java]
1239----
1240@Listen
1241public class MyTopMenuExtension implements TopMenu {
David Pursehoused128c892013-10-22 21:52:21 +09001242 [...]
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001243}
1244----
1245
Luca Milanesiocb230402013-10-11 08:49:56 +01001246Otherwise the top menu extension must be bound in the plugin module used
1247for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF):
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001248
1249[source,java]
1250----
Luca Milanesiocb230402013-10-11 08:49:56 +01001251package com.googlesource.gerrit.plugins.helloworld;
1252
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001253public class HelloWorldModule extends AbstractModule {
1254 @Override
1255 protected void configure() {
1256 DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class);
1257 }
1258}
1259----
1260
Luca Milanesiocb230402013-10-11 08:49:56 +01001261[source,manifest]
1262----
1263Gerrit-ApiType: plugin
1264Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule
1265----
1266
Edwin Kempinb2e926a2013-11-11 16:38:30 +01001267It is also possible to show some menu entries only if the user has a
1268certain capability:
1269
1270[source,java]
1271----
1272public class MyTopMenuExtension implements TopMenu {
1273 private final String pluginName;
1274 private final Provider<CurrentUser> userProvider;
1275 private final List<MenuEntry> menuEntries;
1276
1277 @Inject
1278 public MyTopMenuExtension(@PluginName String pluginName,
1279 Provider<CurrentUser> userProvider) {
1280 this.pluginName = pluginName;
1281 this.userProvider = userProvider;
1282 menuEntries = new ArrayList<TopMenu.MenuEntry>();
1283
1284 // add menu entry that is only visible to users with a certain capability
1285 if (canSeeMenuEntry()) {
1286 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1287 .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1288 }
1289
1290 // add menu entry that is visible to all users (even anonymous users)
1291 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1292 .singletonList(new MenuItem("Documentation", "/plugins/myplugin/"))));
1293 }
1294
1295 private boolean canSeeMenuEntry() {
1296 if (userProvider.get().isIdentifiedUser()) {
1297 CapabilityControl ctl = userProvider.get().getCapabilities();
1298 return ctl.canPerform(pluginName + "-" + MyCapability.ID)
1299 || ctl.canAdministrateServer();
1300 } else {
1301 return false;
1302 }
1303 }
1304
1305 @Override
1306 public List<MenuEntry> getEntries() {
1307 return menuEntries;
1308 }
1309}
1310----
1311
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001312[[gwt_ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001313== GWT UI Extension
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001314Plugins can extend the Gerrit UI with own GWT code.
1315
1316The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to
1317generate a GWT plugin skeleton. How to use the Maven plugin archetypes
1318is described in the link:#getting-started[Getting started] section.
1319
1320The generated GWT plugin has a link:#top-menu-extensions[top menu] that
1321opens a GWT dialog box when the user clicks on it.
1322
Edwin Kempinb74daa92013-11-11 11:28:16 +01001323In addition to the Gerrit-Plugin API a GWT plugin depends on
1324`gerrit-plugin-gwtui`. This dependency must be specified in the
1325`pom.xml`:
1326
1327[source,xml]
1328----
1329<dependency>
1330 <groupId>com.google.gerrit</groupId>
1331 <artifactId>gerrit-plugin-gwtui</artifactId>
1332 <version>${Gerrit-ApiVersion}</version>
1333</dependency>
1334----
1335
1336A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`,
1337that bundles together all the configuration settings of the GWT plugin:
1338
1339[source,xml]
1340----
1341<?xml version="1.0" encoding="UTF-8"?>
1342<module rename-to="hello_gwt_plugin">
1343 <!-- Inherit the core Web Toolkit stuff. -->
1344 <inherits name="com.google.gwt.user.User"/>
1345 <!-- Other module inherits -->
1346 <inherits name="com.google.gerrit.Plugin"/>
1347 <inherits name="com.google.gwt.http.HTTP"/>
1348 <!-- Using GWT built-in themes adds a number of static -->
1349 <!-- resources to the plugin. No theme inherits lines were -->
1350 <!-- added in order to make this plugin as simple as possible -->
1351 <!-- Specify the app entry point class. -->
1352 <entry-point class="${package}.client.HelloPlugin"/>
1353 <stylesheet src="hello.css"/>
1354</module>
1355----
1356
1357The GWT module must inherit `com.google.gerrit.Plugin` and
1358`com.google.gwt.http.HTTP`.
1359
1360To register the GWT module a `GwtPlugin` needs to be bound.
1361
1362If no Guice modules are declared in the manifest, the GWT plugin may
1363use auto-registration by using the `@Listen` annotation:
1364
1365[source,java]
1366----
1367@Listen
1368public class MyExtension extends GwtPlugin {
1369 public MyExtension() {
1370 super("hello_gwt_plugin");
1371 }
1372}
1373----
1374
1375Otherwise the binding must be done in an `HttpModule`:
1376
1377[source,java]
1378----
1379public class HttpModule extends HttpPluginModule {
1380
1381 @Override
1382 protected void configureServlets() {
1383 DynamicSet.bind(binder(), WebUiPlugin.class)
1384 .toInstance(new GwtPlugin("hello_gwt_plugin"));
1385 }
1386}
1387----
1388
1389The HTTP module above must be declared in the `pom.xml` for Maven
1390driven plugins:
1391
1392[source,xml]
1393----
1394<manifestEntries>
1395 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule>
1396</manifestEntries>
1397----
1398
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001399The name that is provided to the `GwtPlugin` must match the GWT
1400module name compiled into the plugin. The name of the GWT module
1401can be explicitly set in the GWT module XML file by specifying
1402the `rename-to` attribute on the module. It is important that the
1403module name be unique across all plugins installed on the server,
1404as the module name determines the JavaScript namespace used by the
1405compiled plugin code.
Edwin Kempinb74daa92013-11-11 11:28:16 +01001406
1407[source,xml]
1408----
1409<module rename-to="hello_gwt_plugin">
1410----
1411
1412The actual GWT code must be implemented in a class that extends
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001413`com.google.gerrit.plugin.client.PluginEntryPoint`:
Edwin Kempinb74daa92013-11-11 11:28:16 +01001414
1415[source,java]
1416----
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001417public class HelloPlugin extends PluginEntryPoint {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001418
1419 @Override
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001420 public void onPluginLoad() {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001421 // Create the dialog box
1422 final DialogBox dialogBox = new DialogBox();
1423
1424 // The content of the dialog comes from a User specified Preference
1425 dialogBox.setText("Hello from GWT Gerrit UI plugin");
1426 dialogBox.setAnimationEnabled(true);
1427 Button closeButton = new Button("Close");
1428 VerticalPanel dialogVPanel = new VerticalPanel();
1429 dialogVPanel.setWidth("100%");
1430 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
1431 dialogVPanel.add(closeButton);
1432
1433 closeButton.addClickHandler(new ClickHandler() {
1434 public void onClick(ClickEvent event) {
1435 dialogBox.hide();
1436 }
1437 });
1438
1439 // Set the contents of the Widget
1440 dialogBox.setWidget(dialogVPanel);
1441
1442 RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1443 rootPanel.getElement().removeAttribute("href");
1444 rootPanel.addDomHandler(new ClickHandler() {
1445 @Override
1446 public void onClick(ClickEvent event) {
1447 dialogBox.center();
1448 dialogBox.show();
1449 }
1450 }, ClickEvent.getType());
1451 }
1452}
1453----
1454
1455This class must be set as entry point in the GWT module:
1456
1457[source,xml]
1458----
1459<entry-point class="${package}.client.HelloPlugin"/>
1460----
1461
1462In addition this class must be defined as module in the `pom.xml` for the
1463`gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin`
1464must be set to `${project.build.directory}/classes/static`:
1465
1466[source,xml]
1467----
1468<plugin>
1469 <groupId>org.codehaus.mojo</groupId>
1470 <artifactId>gwt-maven-plugin</artifactId>
1471 <version>2.5.1</version>
1472 <configuration>
1473 <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module>
1474 <disableClassMetadata>true</disableClassMetadata>
1475 <disableCastChecking>true</disableCastChecking>
1476 <webappDirectory>${project.build.directory}/classes/static</webappDirectory>
1477 </configuration>
1478 <executions>
1479 <execution>
1480 <goals>
1481 <goal>compile</goal>
1482 </goals>
1483 </execution>
1484 </executions>
1485</plugin>
1486----
1487
1488To attach a GWT widget defined by the plugin to the Gerrit core UI
1489`com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the
1490Gerrit core widgets:
1491
1492[source,java]
1493----
1494RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1495rootPanel.getElement().removeAttribute("href");
1496rootPanel.addDomHandler(new ClickHandler() {
1497 @Override
1498 public void onClick(ClickEvent event) {
1499 dialogBox.center();
1500 dialogBox.show();
1501 }
1502}, ClickEvent.getType());
1503----
1504
1505GWT plugins can come with their own css file. This css file must have a
1506unique name and must be registered in the GWT module:
1507
1508[source,xml]
1509----
1510<stylesheet src="hello.css"/>
1511----
1512
Edwin Kempin2570b102013-11-11 11:44:50 +01001513If a GWT plugin wants to invoke the Gerrit REST API it can use
David Pursehouse3a388312014-02-25 16:41:47 +09001514`com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL
Edwin Kempin2570b102013-11-11 11:44:50 +01001515path and to trigger the REST calls.
1516
1517Example for invoking a Gerrit core REST endpoint:
1518
1519[source,java]
1520----
1521new RestApi("projects").id(projectName).view("description")
1522 .put("new description", new AsyncCallback<JavaScriptObject>() {
1523
1524 @Override
1525 public void onSuccess(JavaScriptObject result) {
1526 // TODO
1527 }
1528
1529 @Override
1530 public void onFailure(Throwable caught) {
1531 // never invoked
1532 }
1533});
1534----
1535
1536Example for invoking a REST endpoint defined by a plugin:
1537
1538[source,java]
1539----
1540new RestApi("projects").id(projectName).view("myplugin", "myview")
1541 .get(new AsyncCallback<JavaScriptObject>() {
1542
1543 @Override
1544 public void onSuccess(JavaScriptObject result) {
1545 // TODO
1546 }
1547
1548 @Override
1549 public void onFailure(Throwable caught) {
1550 // never invoked
1551 }
1552});
1553----
1554
1555The `onFailure(Throwable)` of the provided callback is never invoked.
1556If an error occurs, it is shown in an error dialog.
1557
1558In order to be able to do REST calls the GWT module must inherit
1559`com.google.gwt.json.JSON`:
1560
1561[source,xml]
1562----
1563<inherits name="com.google.gwt.json.JSON"/>
1564----
1565
Shawn Pearced5c844f2013-12-26 15:32:26 -08001566== Add Screen
1567A GWT plugin can add a menu item that opens a screen that is
1568implemented by the plugin. This way plugin screens can be fully
1569integrated into the Gerrit UI.
1570
1571Example menu item:
1572[source,java]
1573----
1574public class MyMenu implements TopMenu {
1575 private final List<MenuEntry> menuEntries;
1576
1577 @Inject
1578 public MyMenu(@PluginName String name) {
1579 menuEntries = Lists.newArrayList();
1580 menuEntries.add(new MenuEntry("My Menu", Collections.singletonList(
1581 new MenuItem("My Screen", "#/x/" + name + "/my-screen", ""))));
1582 }
1583
1584 @Override
1585 public List<MenuEntry> getEntries() {
1586 return menuEntries;
1587 }
1588}
1589----
1590
1591Example screen:
1592[source,java]
1593----
1594public class MyPlugin extends PluginEntryPoint {
1595 @Override
1596 public void onPluginLoad() {
1597 Plugin.get().screen("my-screen", new Screen.EntryPoint() {
1598 @Override
1599 public void onLoad(Screen screen) {
1600 screen.add(new InlineLabel("My Screen");
1601 screen.show();
1602 }
1603 });
1604 }
1605}
1606----
1607
Edwin Kempin289f1a02014-02-04 16:08:25 +01001608[[settings-screen]]
1609== Plugin Settings Screen
1610
1611If a plugin implements a screen for administrating its settings that is
1612available under "#/x/<plugin-name>/settings" it is automatically linked
1613from the plugin list screen.
1614
Edwin Kempinf5a77332012-07-18 11:17:53 +02001615[[http]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001616== HTTP Servlets
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001617
1618Plugins or extensions may register additional HTTP servlets, and
1619wrap them with HTTP filters.
1620
1621Servlets may use auto-registration to declare the URL they handle:
1622
David Pursehouse68153d72013-09-04 10:09:17 +09001623[source,java]
1624----
1625import com.google.gerrit.extensions.annotations.Export;
1626import com.google.inject.Singleton;
1627import javax.servlet.http.HttpServlet;
1628import javax.servlet.http.HttpServletRequest;
1629import javax.servlet.http.HttpServletResponse;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001630
David Pursehouse68153d72013-09-04 10:09:17 +09001631@Export("/print")
1632@Singleton
1633class HelloServlet extends HttpServlet {
1634 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
1635 res.setContentType("text/plain");
1636 res.setCharacterEncoding("UTF-8");
1637 res.getWriter().write("Hello");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001638 }
David Pursehouse68153d72013-09-04 10:09:17 +09001639}
1640----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001641
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001642The auto registration only works for standard servlet mappings like
1643`/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule
1644to register the HTTP servlets and declare it explicitly in the manifest
1645with the `Gerrit-HttpModule` attribute:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001646
David Pursehouse68153d72013-09-04 10:09:17 +09001647[source,java]
1648----
1649import com.google.inject.servlet.ServletModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001650
David Pursehouse68153d72013-09-04 10:09:17 +09001651class MyWebUrls extends ServletModule {
1652 protected void configureServlets() {
1653 serve("/print").with(HelloServlet.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001654 }
David Pursehouse68153d72013-09-04 10:09:17 +09001655}
1656----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001657
1658For a plugin installed as name `helloworld`, the servlet implemented
1659by HelloServlet class will be available to users as:
1660
1661----
1662$ curl http://review.example.com/plugins/helloworld/print
1663----
Nasser Grainawie033b262012-05-09 17:54:21 -07001664
Edwin Kempinf5a77332012-07-18 11:17:53 +02001665[[data-directory]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001666== Data Directory
Edwin Kempin41f63912012-07-17 12:33:55 +02001667
1668Plugins can request a data directory with a `@PluginData` File
1669dependency. A data directory will be created automatically by the
1670server in `$site_path/data/$plugin_name` and passed to the plugin.
1671
1672Plugins can use this to store any data they want.
1673
David Pursehouse68153d72013-09-04 10:09:17 +09001674[source,java]
1675----
1676@Inject
1677MyType(@PluginData java.io.File myDir) {
1678 new FileInputStream(new File(myDir, "my.config"));
1679}
1680----
Edwin Kempin41f63912012-07-17 12:33:55 +02001681
Edwin Kempinea621482013-10-16 12:58:24 +02001682[[download-commands]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001683== Download Commands
Edwin Kempinea621482013-10-16 12:58:24 +02001684
1685Gerrit offers commands for downloading changes using different
1686download schemes (e.g. for downloading via different network
1687protocols). Plugins can contribute download schemes and download
1688commands by implementing
1689`com.google.gerrit.extensions.config.DownloadScheme` and
1690`com.google.gerrit.extensions.config.DownloadCommand`.
1691
1692The download schemes and download commands which are used most often
1693are provided by the Gerrit core plugin `download-commands`.
1694
Edwin Kempinf5a77332012-07-18 11:17:53 +02001695[[documentation]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001696== Documentation
Nasser Grainawie033b262012-05-09 17:54:21 -07001697
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001698If a plugin does not register a filter or servlet to handle URLs
1699`/Documentation/*` or `/static/*`, the core Gerrit server will
1700automatically export these resources over HTTP from the plugin JAR.
1701
David Pursehouse6853b5a2013-07-10 11:38:03 +09001702Static resources under the `static/` directory in the JAR will be
Dave Borowitzb893ac82013-03-27 10:03:55 -04001703available as `/plugins/helloworld/static/resource`. This prefix is
1704configurable by setting the `Gerrit-HttpStaticPrefix` attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001705
David Pursehouse6853b5a2013-07-10 11:38:03 +09001706Documentation files under the `Documentation/` directory in the JAR
Dave Borowitzb893ac82013-03-27 10:03:55 -04001707will be available as `/plugins/helloworld/Documentation/resource`. This
1708prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix`
1709attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001710
1711Documentation may be written in
1712link:http://daringfireball.net/projects/markdown/[Markdown] style
1713if the file name ends with `.md`. Gerrit will automatically convert
1714Markdown to HTML if accessed with extension `.html`.
Nasser Grainawie033b262012-05-09 17:54:21 -07001715
Edwin Kempinf5a77332012-07-18 11:17:53 +02001716[[macros]]
Edwin Kempinc78777d2012-07-16 15:55:11 +02001717Within the Markdown documentation files macros can be used that allow
1718to write documentation with reasonably accurate examples that adjust
1719automatically based on the installation.
1720
1721The following macros are supported:
1722
1723[width="40%",options="header"]
1724|===================================================
1725|Macro | Replacement
1726|@PLUGIN@ | name of the plugin
1727|@URL@ | Gerrit Web URL
1728|@SSH_HOST@ | SSH Host
1729|@SSH_PORT@ | SSH Port
1730|===================================================
1731
1732The macros will be replaced when the documentation files are rendered
1733from Markdown to HTML.
1734
1735Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@`
1736even if there is an expansion for `KEEP` in the future.
1737
Edwin Kempinf5a77332012-07-18 11:17:53 +02001738[[auto-index]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001739=== Automatic Index
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001740
1741If a plugin does not handle its `/` URL itself, Gerrit will
1742redirect clients to the plugin's `/Documentation/index.html`.
1743Requests for `/Documentation/` (bare directory) will also redirect
1744to `/Documentation/index.html`.
1745
1746If neither resource `Documentation/index.html` or
1747`Documentation/index.md` exists in the plugin JAR, Gerrit will
1748automatically generate an index page for the plugin's documentation
1749tree by scanning every `*.md` and `*.html` file in the Documentation/
1750directory.
1751
1752For any discovered Markdown (`*.md`) file, Gerrit will parse the
1753header of the file and extract the first level one title. This
1754title text will be used as display text for a link to the HTML
1755version of the page.
1756
1757For any discovered HTML (`*.html`) file, Gerrit will use the name
1758of the file, minus the `*.html` extension, as the link text. Any
1759hyphens in the file name will be replaced with spaces.
1760
David Pursehouse6853b5a2013-07-10 11:38:03 +09001761If a discovered file is named `about.md` or `about.html`, its
1762content will be inserted in an 'About' section at the top of the
1763auto-generated index page. If both `about.md` and `about.html`
1764exist, only the first discovered file will be used.
1765
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001766If a discovered file name beings with `cmd-` it will be clustered
David Pursehouse6853b5a2013-07-10 11:38:03 +09001767into a 'Commands' section of the generated index page.
1768
David Pursehousefe529152013-08-14 16:35:06 +09001769If a discovered file name beings with `servlet-` it will be clustered
1770into a 'Servlets' section of the generated index page.
1771
1772If a discovered file name beings with `rest-api-` it will be clustered
1773into a 'REST APIs' section of the generated index page.
1774
David Pursehouse6853b5a2013-07-10 11:38:03 +09001775All other files are clustered under a 'Documentation' section.
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001776
1777Some optional information from the manifest is extracted and
1778displayed as part of the index page, if present in the manifest:
1779
1780[width="40%",options="header"]
1781|===================================================
1782|Field | Source Attribute
1783|Name | Implementation-Title
1784|Vendor | Implementation-Vendor
1785|Version | Implementation-Version
1786|URL | Implementation-URL
1787|API Version | Gerrit-ApiVersion
1788|===================================================
1789
Edwin Kempinf5a77332012-07-18 11:17:53 +02001790[[deployment]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001791== Deployment
Nasser Grainawie033b262012-05-09 17:54:21 -07001792
Edwin Kempinf7295742012-07-16 15:03:46 +02001793Compiled plugins and extensions can be deployed to a running Gerrit
1794server using the link:cmd-plugin-install.html[plugin install] command.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001795
Dariusz Luksza357a2422012-11-12 06:16:26 +01001796WebUI plugins distributed as single `.js` file can be deployed
1797without the overhead of JAR packaging, for more information refer to
1798link:cmd-plugin-install.html[plugin install] command.
1799
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001800Plugins can also be copied directly into the server's
Dariusz Luksza357a2422012-11-12 06:16:26 +01001801directory at `$site_path/plugins/$name.(jar|js)`. The name of
1802the JAR file, minus the `.jar` or `.js` extension, will be used as the
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001803plugin name. Unless disabled, servers periodically scan this
1804directory for updated plugins. The time can be adjusted by
1805link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency].
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001806
Edwin Kempinf7295742012-07-16 15:03:46 +02001807For disabling plugins the link:cmd-plugin-remove.html[plugin remove]
1808command can be used.
1809
Brad Larsond5e87c32012-07-11 12:18:49 -05001810Disabled plugins can be re-enabled using the
1811link:cmd-plugin-enable.html[plugin enable] command.
1812
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001813== SEE ALSO
David Ostrovskyf86bae52013-09-01 09:10:39 +02001814
1815* link:js-api.html[JavaScript API]
1816* link:dev-rest-api.html[REST API Developers' Notes]
1817
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001818GERRIT
1819------
1820Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07001821
1822SEARCHBOX
1823---------