blob: 16e901a6f81ef5157d2c89082089b720bef5cfaf [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 Kempinf5a77332012-07-18 11:17:53 +0200413[[ssh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800414== SSH Commands
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700415
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700416Plugins may provide commands that can be accessed through the SSH
417interface (extensions do not have this option).
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700418
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700419Command implementations must extend the base class SshCommand:
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700420
David Pursehouse68153d72013-09-04 10:09:17 +0900421[source,java]
422----
423import com.google.gerrit.sshd.SshCommand;
David Ostrovskyb7d97752013-11-09 05:23:26 +0100424import com.google.gerrit.sshd.CommandMetaData;
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700425
David Ostrovskyb7d97752013-11-09 05:23:26 +0100426@CommandMetaData(name="print", descr="Print hello command")
David Pursehouse68153d72013-09-04 10:09:17 +0900427class PrintHello extends SshCommand {
428 protected abstract void run() {
429 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700430 }
David Pursehouse68153d72013-09-04 10:09:17 +0900431}
432----
Nasser Grainawie033b262012-05-09 17:54:21 -0700433
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700434If no Guice modules are declared in the manifest, SSH commands may
Edwin Kempin948de0f2012-07-16 10:34:35 +0200435use auto-registration by providing an `@Export` annotation:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700436
David Pursehouse68153d72013-09-04 10:09:17 +0900437[source,java]
438----
439import com.google.gerrit.extensions.annotations.Export;
440import com.google.gerrit.sshd.SshCommand;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700441
David Pursehouse68153d72013-09-04 10:09:17 +0900442@Export("print")
443class PrintHello extends SshCommand {
444 protected abstract void run() {
445 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700446 }
David Pursehouse68153d72013-09-04 10:09:17 +0900447}
448----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700449
450If explicit registration is being used, a Guice module must be
451supplied to register the SSH command and declared in the manifest
452with the `Gerrit-SshModule` attribute:
453
David Pursehouse68153d72013-09-04 10:09:17 +0900454[source,java]
455----
456import com.google.gerrit.sshd.PluginCommandModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700457
David Pursehouse68153d72013-09-04 10:09:17 +0900458class MyCommands extends PluginCommandModule {
459 protected void configureCommands() {
David Ostrovskyb7d97752013-11-09 05:23:26 +0100460 command(PrintHello.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700461 }
David Pursehouse68153d72013-09-04 10:09:17 +0900462}
463----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700464
465For a plugin installed as name `helloworld`, the command implemented
466by PrintHello class will be available to users as:
467
468----
Keunhong Parka09a6f12012-07-10 14:45:02 -0600469$ ssh -p 29418 review.example.com helloworld print
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700470----
471
David Ostrovskye3172b32013-10-13 14:19:13 +0200472Multiple SSH commands can be bound to the same implementation class. For
473example a Gerrit Shell plugin can bind different shell commands to the same
474implementation class:
475
476[source,java]
477----
478public class SshShellModule extends PluginCommandModule {
479 @Override
480 protected void configureCommands() {
481 command("ls").to(ShellCommand.class);
482 command("ps").to(ShellCommand.class);
483 [...]
484 }
485}
486----
487
488With the possible implementation:
489
490[source,java]
491----
492public class ShellCommand extends SshCommand {
493 @Override
494 protected void run() throws UnloggedFailure {
495 String cmd = getName().substring(getPluginName().length() + 1);
496 ProcessBuilder proc = new ProcessBuilder(cmd);
497 Process cmd = proc.start();
498 [...]
499 }
500}
501----
502
503And the call:
504
505----
506$ ssh -p 29418 review.example.com shell ls
507$ ssh -p 29418 review.example.com shell ps
508----
509
David Ostrovskyb7d97752013-11-09 05:23:26 +0100510Single command plugins are also supported. In this scenario plugin binds
511SSH command to its own name. `SshModule` must inherit from
512`SingleCommandPluginModule` class:
513
514[source,java]
515----
516public class SshModule extends SingleCommandPluginModule {
517 @Override
518 protected void configure(LinkedBindingBuilder<Command> b) {
519 b.to(ShellCommand.class);
520 }
521}
522----
523
524If the plugin above is deployed under sh.jar file in `$site/plugins`
David Pursehouse659860f2013-12-16 14:50:04 +0900525directory, generic commands can be called without specifying the
David Ostrovskyb7d97752013-11-09 05:23:26 +0100526actual SSH command. Note in the example below, that the called commands
527`ls` and `ps` was not explicitly bound:
528
529----
530$ ssh -p 29418 review.example.com sh ls
531$ ssh -p 29418 review.example.com sh ps
532----
533
Edwin Kempin78ca0942013-10-30 11:24:06 +0100534[[simple-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800535== Simple Configuration in `gerrit.config`
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200536
537In Gerrit, global configuration is stored in the `gerrit.config` file.
538If a plugin needs global configuration, this configuration should be
539stored in a `plugin` subsection in the `gerrit.config` file.
540
Edwin Kempinc9b68602013-10-30 09:32:43 +0100541This approach of storing the plugin configuration is only suitable for
542plugins that have a simple configuration that only consists of
543key-value pairs. With this approach it is not possible to have
544subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin78ca0942013-10-30 11:24:06 +0100545configuration need to store their configuration in their
546link:#configuration[own configuration file] where they can make use of
547subsections. On the other hand storing the plugin configuration in a
548'plugin' subsection in the `gerrit.config` file has the advantage that
549administrators have all configuration parameters in one file, instead
550of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100551
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200552To avoid conflicts with other plugins, it is recommended that plugins
553only use the `plugin` subsection with their own name. For example the
554`helloworld` plugin should store its configuration in the
555`plugin.helloworld` subsection:
556
557----
558[plugin "helloworld"]
559 language = Latin
560----
561
Sasa Zivkovacdf5332013-09-20 14:05:15 +0200562Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200563plugin can easily access its configuration and there is no need for a
564plugin to parse the `gerrit.config` file on its own:
565
566[source,java]
567----
David Pursehouse529ec252013-09-27 13:45:14 +0900568@Inject
569private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200570
David Pursehoused128c892013-10-22 21:52:21 +0900571[...]
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200572
Edwin Kempin122622d2013-10-29 16:45:44 +0100573String language = cfg.getFromGerritConfig("helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900574 .getString("language", "English");
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200575----
576
Edwin Kempin78ca0942013-10-30 11:24:06 +0100577[[configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800578== Configuration in own config file
Edwin Kempin78ca0942013-10-30 11:24:06 +0100579
580Plugins can store their configuration in an own configuration file.
581This makes sense if the plugin configuration is rather complex and
582requires the usage of subsections. Plugins that have a simple
583key-value pair configuration can store their configuration in a
584link:#simple-configuration[`plugin` subsection of the `gerrit.config`
585file].
586
587The plugin configuration file must be named after the plugin and must
588be located in the `etc` folder of the review site. For example a
589configuration file for a `default-reviewer` plugin could look like
590this:
591
592.$site_path/etc/default-reviewer.config
593----
594[branch "refs/heads/master"]
595 reviewer = Project Owners
596 reviewer = john.doe@example.com
597[match "file:^.*\.txt"]
598 reviewer = My Info Developers
599----
600
601Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
602plugin can easily access its configuration:
603
604[source,java]
605----
606@Inject
607private com.google.gerrit.server.config.PluginConfigFactory cfg;
608
609[...]
610
611String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer")
612 .getStringList("branch", "refs/heads/master", "reviewer");
613----
614
615The plugin configuration is loaded only once and is then cached.
616Similar to changes in 'gerrit.config', changes to the plugin
617configuration file will only become effective after a Gerrit restart.
618
Edwin Kempin705f2842013-10-30 14:25:31 +0100619[[simple-project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800620== Simple Project Specific Configuration in `project.config`
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200621
622In Gerrit, project specific configuration is stored in the project's
623`project.config` file on the `refs/meta/config` branch. If a plugin
624needs configuration on project level (e.g. to enable its functionality
625only for certain projects), this configuration should be stored in a
626`plugin` subsection in the project's `project.config` file.
627
Edwin Kempinc9b68602013-10-30 09:32:43 +0100628This approach of storing the plugin configuration is only suitable for
629plugins that have a simple configuration that only consists of
630key-value pairs. With this approach it is not possible to have
631subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin705f2842013-10-30 14:25:31 +0100632configuration need to store their configuration in their
633link:#project-specific-configuration[own configuration file] where they
634can make use of subsections. On the other hand storing the plugin
635configuration in a 'plugin' subsection in the `project.config` file has
636the advantage that project owners have all configuration parameters in
637one file, instead of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100638
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200639To avoid conflicts with other plugins, it is recommended that plugins
640only use the `plugin` subsection with their own name. For example the
641`helloworld` plugin should store its configuration in the
642`plugin.helloworld` subsection:
643
644----
645 [plugin "helloworld"]
646 enabled = true
647----
648
649Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
650plugin can easily access its project specific configuration and there
651is no need for a plugin to parse the `project.config` file on its own:
652
653[source,java]
654----
David Pursehouse529ec252013-09-27 13:45:14 +0900655@Inject
656private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200657
David Pursehoused128c892013-10-22 21:52:21 +0900658[...]
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200659
Edwin Kempin122622d2013-10-29 16:45:44 +0100660boolean enabled = cfg.getFromProjectConfig(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900661 .getBoolean("enabled", false);
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200662----
663
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200664It is also possible to get missing configuration parameters inherited
665from the parent projects:
666
667[source,java]
668----
David Pursehouse529ec252013-09-27 13:45:14 +0900669@Inject
670private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200671
David Pursehoused128c892013-10-22 21:52:21 +0900672[...]
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200673
Edwin Kempin122622d2013-10-29 16:45:44 +0100674boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900675 .getBoolean("enabled", false);
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200676----
677
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200678Project owners can edit the project configuration by fetching the
679`refs/meta/config` branch, editing the `project.config` file and
680pushing the commit back.
681
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100682Plugin configuration values that are stored in the `project.config`
683file can be exposed in the ProjectInfoScreen to allow project owners
684to see and edit them from the UI.
685
686For this an instance of `ProjectConfigEntry` needs to be bound for each
687parameter. The export name must be a valid Git variable name. The
688variable name is case-insensitive, allows only alphanumeric characters
689and '-', and must start with an alphabetic character.
690
691The example below shows how the parameter `plugin.helloworld.language`
692is bound to be editable from the WebUI. "Preferred Language" is
693provided as display name and "en" is set as default value.
694
695[source,java]
696----
697class Module extends AbstractModule {
698 @Override
699 protected void configure() {
700 bind(ProjectConfigEntry.class)
701 .annotatedWith(Exports.named("language"))
702 .toInstance(new ProjectConfigEntry("Preferred Language", "en"));
703 }
704}
705----
706
Edwin Kempin705f2842013-10-30 14:25:31 +0100707[[project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800708== Project Specific Configuration in own config file
Edwin Kempin705f2842013-10-30 14:25:31 +0100709
710Plugins can store their project specific configuration in an own
711configuration file in the projects `refs/meta/config` branch.
712This makes sense if the plugins project specific configuration is
713rather complex and requires the usage of subsections. Plugins that
714have a simple key-value pair configuration can store their project
715specific configuration in a link:#simple-project-specific-configuration[
716`plugin` subsection of the `project.config` file].
717
718The plugin configuration file in the `refs/meta/config` branch must be
719named after the plugin. For example a configuration file for a
720`default-reviewer` plugin could look like this:
721
722.default-reviewer.config
723----
724[branch "refs/heads/master"]
725 reviewer = Project Owners
726 reviewer = john.doe@example.com
727[match "file:^.*\.txt"]
728 reviewer = My Info Developers
729----
730
731Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
732plugin can easily access its project specific configuration:
733
734[source,java]
735----
736@Inject
737private com.google.gerrit.server.config.PluginConfigFactory cfg;
738
739[...]
740
741String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer")
742 .getStringList("branch", "refs/heads/master", "reviewer");
743----
744
Edwin Kempin762da382013-10-30 14:50:01 +0100745It is also possible to get missing configuration parameters inherited
746from the parent projects:
747
748[source,java]
749----
750@Inject
751private com.google.gerrit.server.config.PluginConfigFactory cfg;
752
753[...]
754
755String[] reviewers = cfg.getFromPluginConfigWithInheritance(project, "default-reviewer")
756 .getStringList("branch", "refs/heads/master", "reviewer");
757----
758
Edwin Kempin705f2842013-10-30 14:25:31 +0100759Project owners can edit the project configuration by fetching the
760`refs/meta/config` branch, editing the `<plugin-name>.config` file and
761pushing the commit back.
762
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800763== React on changes in project configuration
Edwin Kempina46b6c92013-12-04 21:05:24 +0100764
765If a plugin wants to react on changes in the project configuration, it
766can implement a `GitReferenceUpdatedListener` and filter on events for
767the `refs/meta/config` branch:
768
769[source,java]
770----
771public class MyListener implements GitReferenceUpdatedListener {
772
773 private final MetaDataUpdate.Server metaDataUpdateFactory;
774
775 @Inject
776 MyListener(MetaDataUpdate.Server metaDataUpdateFactory) {
777 this.metaDataUpdateFactory = metaDataUpdateFactory;
778 }
779
780 @Override
781 public void onGitReferenceUpdated(Event event) {
782 if (event.getRefName().equals(GitRepositoryManager.REF_CONFIG)) {
783 Project.NameKey p = new Project.NameKey(event.getProjectName());
784 try {
785 ProjectConfig oldCfg =
786 ProjectConfig.read(metaDataUpdateFactory.create(p),
787 ObjectId.fromString(event.getOldObjectId()));
788 ProjectConfig newCfg =
789 ProjectConfig.read(metaDataUpdateFactory.create(p),
790 ObjectId.fromString(event.getNewObjectId()));
791
792 if (!oldCfg.getProject().getSubmitType().equals(
793 newCfg.getProject().getSubmitType())) {
794 // submit type has changed
795 ...
796 }
797 } catch (IOException | ConfigInvalidException e) {
798 ...
799 }
800 }
801 }
802}
803----
804
805
David Ostrovsky7066cc02013-06-15 14:46:23 +0200806[[capabilities]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800807== Plugin Owned Capabilities
David Ostrovsky7066cc02013-06-15 14:46:23 +0200808
809Plugins may provide their own capabilities and restrict usage of SSH
810commands to the users who are granted those capabilities.
811
812Plugins define the capabilities by overriding the `CapabilityDefinition`
813abstract class:
814
David Pursehouse68153d72013-09-04 10:09:17 +0900815[source,java]
816----
817public class PrintHelloCapability extends CapabilityDefinition {
818 @Override
819 public String getDescription() {
820 return "Print Hello";
David Ostrovsky7066cc02013-06-15 14:46:23 +0200821 }
David Pursehouse68153d72013-09-04 10:09:17 +0900822}
823----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200824
David Ostrovskyf86bae52013-09-01 09:10:39 +0200825If no Guice modules are declared in the manifest, UI actions may
David Ostrovsky7066cc02013-06-15 14:46:23 +0200826use auto-registration by providing an `@Export` annotation:
827
David Pursehouse68153d72013-09-04 10:09:17 +0900828[source,java]
829----
830@Export("printHello")
831public class PrintHelloCapability extends CapabilityDefinition {
David Pursehoused128c892013-10-22 21:52:21 +0900832 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900833}
834----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200835
836Otherwise the capability must be bound in a plugin module:
837
David Pursehouse68153d72013-09-04 10:09:17 +0900838[source,java]
839----
840public class HelloWorldModule extends AbstractModule {
841 @Override
842 protected void configure() {
843 bind(CapabilityDefinition.class)
844 .annotatedWith(Exports.named("printHello"))
845 .to(PrintHelloCapability.class);
David Ostrovsky7066cc02013-06-15 14:46:23 +0200846 }
David Pursehouse68153d72013-09-04 10:09:17 +0900847}
848----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200849
850With a plugin-owned capability defined in this way, it is possible to restrict
David Ostrovskyf86bae52013-09-01 09:10:39 +0200851usage of an SSH command or `UiAction` to members of the group that were granted
David Ostrovsky7066cc02013-06-15 14:46:23 +0200852this capability in the usual way, using the `RequiresCapability` annotation:
853
David Pursehouse68153d72013-09-04 10:09:17 +0900854[source,java]
855----
856@RequiresCapability("printHello")
857@CommandMetaData(name="print", description="Print greeting in different languages")
858public final class PrintHelloWorldCommand extends SshCommand {
David Pursehoused128c892013-10-22 21:52:21 +0900859 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900860}
861----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200862
David Ostrovskyf86bae52013-09-01 09:10:39 +0200863Or with `UiAction`:
David Ostrovsky7066cc02013-06-15 14:46:23 +0200864
David Pursehouse68153d72013-09-04 10:09:17 +0900865[source,java]
866----
867@RequiresCapability("printHello")
868public class SayHelloAction extends UiAction<RevisionResource>
869 implements RestModifyView<RevisionResource, SayHelloAction.Input> {
David Pursehoused128c892013-10-22 21:52:21 +0900870 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900871}
872----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200873
874Capability scope was introduced to differentiate between plugin-owned
David Pursehousebf053342013-09-05 14:55:29 +0900875capabilities and core capabilities. Per default the scope of the
876`@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means:
877
David Ostrovsky7066cc02013-06-15 14:46:23 +0200878* when `@RequiresCapability` is used within a plugin the scope of the
879capability is assumed to be that plugin.
David Pursehousebf053342013-09-05 14:55:29 +0900880
David Ostrovsky7066cc02013-06-15 14:46:23 +0200881* If `@RequiresCapability` is used within the core Gerrit Code Review server
882(and thus is outside of a plugin) the scope is the core server and will use
883the `GlobalCapability` known to Gerrit Code Review server.
884
885If a plugin needs to use a core capability name (e.g. "administrateServer")
886this can be specified by setting `scope = CapabilityScope.CORE`:
887
David Pursehouse68153d72013-09-04 10:09:17 +0900888[source,java]
889----
890@RequiresCapability(value = "administrateServer", scope =
891 CapabilityScope.CORE)
David Pursehoused128c892013-10-22 21:52:21 +0900892 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900893----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200894
David Ostrovskyf86bae52013-09-01 09:10:39 +0200895[[ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800896== UI Extension
David Ostrovskyf86bae52013-09-01 09:10:39 +0200897
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100898Plugins can contribute UI actions on core Gerrit pages. This is useful
899for workflow customization or exposing plugin functionality through the
900UI in addition to SSH commands and the REST API.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200901
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100902For instance a plugin to integrate Jira with Gerrit changes may
903contribute a "File bug" button to allow filing a bug from the change
904page or plugins to integrate continuous integration systems may
905contribute a "Schedule" button to allow a CI build to be scheduled
906manually from the patch set panel.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200907
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100908Two different places on core Gerrit pages are supported:
David Ostrovskyf86bae52013-09-01 09:10:39 +0200909
910* Change screen
911* Project info screen
912
913Plugins contribute UI actions by implementing the `UiAction` interface:
914
David Pursehouse68153d72013-09-04 10:09:17 +0900915[source,java]
916----
917@RequiresCapability("printHello")
918class HelloWorldAction implements UiAction<RevisionResource>,
919 RestModifyView<RevisionResource, HelloWorldAction.Input> {
920 static class Input {
921 boolean french;
922 String message;
David Ostrovskyf86bae52013-09-01 09:10:39 +0200923 }
David Pursehouse68153d72013-09-04 10:09:17 +0900924
925 private Provider<CurrentUser> user;
926
927 @Inject
928 HelloWorldAction(Provider<CurrentUser> user) {
929 this.user = user;
930 }
931
932 @Override
933 public String apply(RevisionResource rev, Input input) {
934 final String greeting = input.french
935 ? "Bonjour"
936 : "Hello";
937 return String.format("%s %s from change %s, patch set %d!",
938 greeting,
939 Strings.isNullOrEmpty(input.message)
940 ? Objects.firstNonNull(user.get().getUserName(), "world")
941 : input.message,
942 rev.getChange().getId().toString(),
943 rev.getPatchSet().getPatchSetId());
944 }
945
946 @Override
947 public Description getDescription(
948 RevisionResource resource) {
949 return new Description()
950 .setLabel("Say hello")
951 .setTitle("Say hello in different languages");
952 }
953}
954----
David Ostrovskyf86bae52013-09-01 09:10:39 +0200955
David Ostrovsky450eefe2013-10-21 21:18:11 +0200956Sometimes plugins may want to be able to change the state of a patch set or
957change in the `UiAction.apply()` method and reflect these changes on the core
958UI. For example a buildbot plugin which exposes a 'Schedule' button on the
959patch set panel may want to disable that button after the build was scheduled
960and update the tooltip of that button. But because of Gerrit's caching
961strategy the following must be taken into consideration.
962
963The browser is allowed to cache the `UiAction` information until something on
964the change is modified. More accurately the change row needs to be modified in
965the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or
966the +refs/meta/config+ of the project or any parents needs to change to a new
967SHA-1. The ETag SHA-1 computation code can be found in the
968`ChangeResource.getETag()` method.
969
David Pursehoused128c892013-10-22 21:52:21 +0900970The easiest way to accomplish this is to update `lastUpdatedOn` of the change:
David Ostrovsky450eefe2013-10-21 21:18:11 +0200971
972[source,java]
973----
974@Override
975public Object apply(RevisionResource rcrs, Input in) {
976 // schedule a build
977 [...]
978 // update change
979 ReviewDb db = dbProvider.get();
980 db.changes().beginTransaction(change.getId());
981 try {
982 change = db.changes().atomicUpdate(
983 change.getId(),
984 new AtomicUpdate<Change>() {
985 @Override
986 public Change update(Change change) {
987 ChangeUtil.updated(change);
988 return change;
989 }
990 });
991 db.commit();
992 } finally {
993 db.rollback();
994 }
David Pursehoused128c892013-10-22 21:52:21 +0900995 [...]
David Ostrovsky450eefe2013-10-21 21:18:11 +0200996}
997----
998
David Ostrovskyf86bae52013-09-01 09:10:39 +0200999`UiAction` must be bound in a plugin module:
1000
David Pursehouse68153d72013-09-04 10:09:17 +09001001[source,java]
1002----
1003public class Module extends AbstractModule {
1004 @Override
1005 protected void configure() {
1006 install(new RestApiModule() {
1007 @Override
1008 protected void configure() {
1009 post(REVISION_KIND, "say-hello")
1010 .to(HelloWorldAction.class);
1011 }
1012 });
David Ostrovskyf86bae52013-09-01 09:10:39 +02001013 }
David Pursehouse68153d72013-09-04 10:09:17 +09001014}
1015----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001016
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001017The module above must be declared in the `pom.xml` for Maven driven
1018plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001019
David Pursehouse68153d72013-09-04 10:09:17 +09001020[source,xml]
1021----
1022<manifestEntries>
1023 <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module>
1024</manifestEntries>
1025----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001026
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001027or in the `BUCK` configuration file for Buck driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001028
David Pursehouse68153d72013-09-04 10:09:17 +09001029[source,python]
1030----
1031manifest_entries = [
1032 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module',
1033]
1034----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001035
1036In some use cases more user input must be gathered, for that `UiAction` can be
1037combined with the JavaScript API. This would display a small popup near the
1038activation button to gather additional input from the user. The JS file is
1039typically put in the `static` folder within the plugin's directory:
1040
David Pursehouse68153d72013-09-04 10:09:17 +09001041[source,javascript]
1042----
1043Gerrit.install(function(self) {
1044 function onSayHello(c) {
1045 var f = c.textfield();
1046 var t = c.checkbox();
1047 var b = c.button('Say hello', {onclick: function(){
1048 c.call(
1049 {message: f.value, french: t.checked},
1050 function(r) {
1051 c.hide();
1052 window.alert(r);
1053 c.refresh();
1054 });
1055 }});
1056 c.popup(c.div(
1057 c.prependLabel('Greeting message', f),
1058 c.br(),
1059 c.label(t, 'french'),
1060 c.br(),
1061 b));
1062 f.focus();
1063 }
1064 self.onAction('revision', 'say-hello', onSayHello);
1065});
1066----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001067
1068The JS module must be exposed as a `WebUiPlugin` and bound as
1069an HTTP Module:
1070
David Pursehouse68153d72013-09-04 10:09:17 +09001071[source,java]
1072----
1073public class HttpModule extends HttpPluginModule {
1074 @Override
1075 protected void configureServlets() {
1076 DynamicSet.bind(binder(), WebUiPlugin.class)
1077 .toInstance(new JavaScriptPlugin("hello.js"));
David Ostrovskyf86bae52013-09-01 09:10:39 +02001078 }
David Pursehouse68153d72013-09-04 10:09:17 +09001079}
1080----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001081
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001082The HTTP module above must be declared in the `pom.xml` for Maven
1083driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001084
David Pursehouse68153d72013-09-04 10:09:17 +09001085[source,xml]
1086----
1087<manifestEntries>
1088 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule>
1089</manifestEntries>
1090----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001091
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001092or in the `BUCK` configuration file for Buck driven plugins
David Ostrovskyf86bae52013-09-01 09:10:39 +02001093
David Pursehouse68153d72013-09-04 10:09:17 +09001094[source,python]
1095----
1096manifest_entries = [
1097 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule',
1098]
1099----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001100
1101If `UiAction` is annotated with the `@RequiresCapability` annotation, then the
1102capability check is done during the `UiAction` gathering, so the plugin author
1103doesn't have to set `UiAction.Description.setVisible()` explicitly in this
1104case.
1105
1106The following prerequisities must be met, to satisfy the capability check:
1107
1108* user is authenticated
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001109* user is a member of a group which has the `Administrate Server` capability, or
David Ostrovskyf86bae52013-09-01 09:10:39 +02001110* user is a member of a group which has the required capability
1111
1112The `apply` method is called when the button is clicked. If `UiAction` is
1113combined with JavaScript API (its own JavaScript function is provided),
1114then a popup dialog is normally opened to gather additional user input.
1115A new button is placed on the popup dialog to actually send the request.
1116
1117Every `UiAction` exposes a REST API endpoint. The endpoint from the example above
1118can be accessed from any REST client, i. e.:
1119
1120====
1121 curl -X POST -H "Content-Type: application/json" \
1122 -d '{message: "François", french: true}' \
1123 --digest --user joe:secret \
1124 http://host:port/a/changes/1/revisions/1/cookbook~say-hello
1125 "Bonjour François from change 1, patch set 1!"
1126====
1127
David Pursehouse42245822013-09-24 09:48:20 +09001128A special case is to bind an endpoint without a view name. This is
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001129particularly useful for `DELETE` requests:
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001130
1131[source,java]
1132----
1133public class Module extends AbstractModule {
1134 @Override
1135 protected void configure() {
1136 install(new RestApiModule() {
1137 @Override
1138 protected void configure() {
1139 delete(PROJECT_KIND)
1140 .to(DeleteProject.class);
1141 }
1142 });
1143 }
1144}
1145----
1146
David Pursehouse42245822013-09-24 09:48:20 +09001147For a `UiAction` bound this way, a JS API function can be provided.
1148
1149Currently only one restriction exists: per plugin only one `UiAction`
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001150can be bound per resource without view name. To define a JS function
1151for the `UiAction`, "/" must be used as the name:
1152
1153[source,javascript]
1154----
1155Gerrit.install(function(self) {
1156 function onDeleteProject(c) {
1157 [...]
1158 }
1159 self.onAction('project', '/', onDeleteProject);
1160});
1161----
1162
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001163[[top-menu-extensions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001164== Top Menu Extensions
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001165
1166Plugins can contribute items to Gerrit's top menu.
1167
1168A single top menu extension can have multiple elements and will be put as
1169the last element in Gerrit's top menu.
1170
1171Plugins define the top menu entries by implementing `TopMenu` interface:
1172
1173[source,java]
1174----
1175public class MyTopMenuExtension implements TopMenu {
1176
1177 @Override
1178 public List<MenuEntry> getEntries() {
1179 return Lists.newArrayList(
1180 new MenuEntry("Top Menu Entry", Lists.newArrayList(
1181 new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1182 }
1183}
1184----
1185
Edwin Kempin77f23242013-09-30 14:53:20 +02001186Plugins can also add additional menu items to Gerrit's top menu entries
1187by defining a `MenuEntry` that has the same name as a Gerrit top menu
1188entry:
1189
1190[source,java]
1191----
1192public class MyTopMenuExtension implements TopMenu {
1193
1194 @Override
1195 public List<MenuEntry> getEntries() {
1196 return Lists.newArrayList(
Dariusz Luksza2d3afab2013-10-01 11:07:13 +02001197 new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList(
Edwin Kempin77f23242013-09-30 14:53:20 +02001198 new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
1199 }
1200}
1201----
1202
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001203If no Guice modules are declared in the manifest, the top menu extension may use
1204auto-registration by providing an `@Listen` annotation:
1205
1206[source,java]
1207----
1208@Listen
1209public class MyTopMenuExtension implements TopMenu {
David Pursehoused128c892013-10-22 21:52:21 +09001210 [...]
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001211}
1212----
1213
Luca Milanesiocb230402013-10-11 08:49:56 +01001214Otherwise the top menu extension must be bound in the plugin module used
1215for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF):
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001216
1217[source,java]
1218----
Luca Milanesiocb230402013-10-11 08:49:56 +01001219package com.googlesource.gerrit.plugins.helloworld;
1220
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001221public class HelloWorldModule extends AbstractModule {
1222 @Override
1223 protected void configure() {
1224 DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class);
1225 }
1226}
1227----
1228
Luca Milanesiocb230402013-10-11 08:49:56 +01001229[source,manifest]
1230----
1231Gerrit-ApiType: plugin
1232Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule
1233----
1234
Edwin Kempinb2e926a2013-11-11 16:38:30 +01001235It is also possible to show some menu entries only if the user has a
1236certain capability:
1237
1238[source,java]
1239----
1240public class MyTopMenuExtension implements TopMenu {
1241 private final String pluginName;
1242 private final Provider<CurrentUser> userProvider;
1243 private final List<MenuEntry> menuEntries;
1244
1245 @Inject
1246 public MyTopMenuExtension(@PluginName String pluginName,
1247 Provider<CurrentUser> userProvider) {
1248 this.pluginName = pluginName;
1249 this.userProvider = userProvider;
1250 menuEntries = new ArrayList<TopMenu.MenuEntry>();
1251
1252 // add menu entry that is only visible to users with a certain capability
1253 if (canSeeMenuEntry()) {
1254 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1255 .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1256 }
1257
1258 // add menu entry that is visible to all users (even anonymous users)
1259 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1260 .singletonList(new MenuItem("Documentation", "/plugins/myplugin/"))));
1261 }
1262
1263 private boolean canSeeMenuEntry() {
1264 if (userProvider.get().isIdentifiedUser()) {
1265 CapabilityControl ctl = userProvider.get().getCapabilities();
1266 return ctl.canPerform(pluginName + "-" + MyCapability.ID)
1267 || ctl.canAdministrateServer();
1268 } else {
1269 return false;
1270 }
1271 }
1272
1273 @Override
1274 public List<MenuEntry> getEntries() {
1275 return menuEntries;
1276 }
1277}
1278----
1279
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001280[[gwt_ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001281== GWT UI Extension
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001282Plugins can extend the Gerrit UI with own GWT code.
1283
1284The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to
1285generate a GWT plugin skeleton. How to use the Maven plugin archetypes
1286is described in the link:#getting-started[Getting started] section.
1287
1288The generated GWT plugin has a link:#top-menu-extensions[top menu] that
1289opens a GWT dialog box when the user clicks on it.
1290
Edwin Kempinb74daa92013-11-11 11:28:16 +01001291In addition to the Gerrit-Plugin API a GWT plugin depends on
1292`gerrit-plugin-gwtui`. This dependency must be specified in the
1293`pom.xml`:
1294
1295[source,xml]
1296----
1297<dependency>
1298 <groupId>com.google.gerrit</groupId>
1299 <artifactId>gerrit-plugin-gwtui</artifactId>
1300 <version>${Gerrit-ApiVersion}</version>
1301</dependency>
1302----
1303
1304A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`,
1305that bundles together all the configuration settings of the GWT plugin:
1306
1307[source,xml]
1308----
1309<?xml version="1.0" encoding="UTF-8"?>
1310<module rename-to="hello_gwt_plugin">
1311 <!-- Inherit the core Web Toolkit stuff. -->
1312 <inherits name="com.google.gwt.user.User"/>
1313 <!-- Other module inherits -->
1314 <inherits name="com.google.gerrit.Plugin"/>
1315 <inherits name="com.google.gwt.http.HTTP"/>
1316 <!-- Using GWT built-in themes adds a number of static -->
1317 <!-- resources to the plugin. No theme inherits lines were -->
1318 <!-- added in order to make this plugin as simple as possible -->
1319 <!-- Specify the app entry point class. -->
1320 <entry-point class="${package}.client.HelloPlugin"/>
1321 <stylesheet src="hello.css"/>
1322</module>
1323----
1324
1325The GWT module must inherit `com.google.gerrit.Plugin` and
1326`com.google.gwt.http.HTTP`.
1327
1328To register the GWT module a `GwtPlugin` needs to be bound.
1329
1330If no Guice modules are declared in the manifest, the GWT plugin may
1331use auto-registration by using the `@Listen` annotation:
1332
1333[source,java]
1334----
1335@Listen
1336public class MyExtension extends GwtPlugin {
1337 public MyExtension() {
1338 super("hello_gwt_plugin");
1339 }
1340}
1341----
1342
1343Otherwise the binding must be done in an `HttpModule`:
1344
1345[source,java]
1346----
1347public class HttpModule extends HttpPluginModule {
1348
1349 @Override
1350 protected void configureServlets() {
1351 DynamicSet.bind(binder(), WebUiPlugin.class)
1352 .toInstance(new GwtPlugin("hello_gwt_plugin"));
1353 }
1354}
1355----
1356
1357The HTTP module above must be declared in the `pom.xml` for Maven
1358driven plugins:
1359
1360[source,xml]
1361----
1362<manifestEntries>
1363 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule>
1364</manifestEntries>
1365----
1366
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001367The name that is provided to the `GwtPlugin` must match the GWT
1368module name compiled into the plugin. The name of the GWT module
1369can be explicitly set in the GWT module XML file by specifying
1370the `rename-to` attribute on the module. It is important that the
1371module name be unique across all plugins installed on the server,
1372as the module name determines the JavaScript namespace used by the
1373compiled plugin code.
Edwin Kempinb74daa92013-11-11 11:28:16 +01001374
1375[source,xml]
1376----
1377<module rename-to="hello_gwt_plugin">
1378----
1379
1380The actual GWT code must be implemented in a class that extends
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001381`com.google.gerrit.plugin.client.PluginEntryPoint`:
Edwin Kempinb74daa92013-11-11 11:28:16 +01001382
1383[source,java]
1384----
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001385public class HelloPlugin extends PluginEntryPoint {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001386
1387 @Override
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001388 public void onPluginLoad() {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001389 // Create the dialog box
1390 final DialogBox dialogBox = new DialogBox();
1391
1392 // The content of the dialog comes from a User specified Preference
1393 dialogBox.setText("Hello from GWT Gerrit UI plugin");
1394 dialogBox.setAnimationEnabled(true);
1395 Button closeButton = new Button("Close");
1396 VerticalPanel dialogVPanel = new VerticalPanel();
1397 dialogVPanel.setWidth("100%");
1398 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
1399 dialogVPanel.add(closeButton);
1400
1401 closeButton.addClickHandler(new ClickHandler() {
1402 public void onClick(ClickEvent event) {
1403 dialogBox.hide();
1404 }
1405 });
1406
1407 // Set the contents of the Widget
1408 dialogBox.setWidget(dialogVPanel);
1409
1410 RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1411 rootPanel.getElement().removeAttribute("href");
1412 rootPanel.addDomHandler(new ClickHandler() {
1413 @Override
1414 public void onClick(ClickEvent event) {
1415 dialogBox.center();
1416 dialogBox.show();
1417 }
1418 }, ClickEvent.getType());
1419 }
1420}
1421----
1422
1423This class must be set as entry point in the GWT module:
1424
1425[source,xml]
1426----
1427<entry-point class="${package}.client.HelloPlugin"/>
1428----
1429
1430In addition this class must be defined as module in the `pom.xml` for the
1431`gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin`
1432must be set to `${project.build.directory}/classes/static`:
1433
1434[source,xml]
1435----
1436<plugin>
1437 <groupId>org.codehaus.mojo</groupId>
1438 <artifactId>gwt-maven-plugin</artifactId>
1439 <version>2.5.1</version>
1440 <configuration>
1441 <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module>
1442 <disableClassMetadata>true</disableClassMetadata>
1443 <disableCastChecking>true</disableCastChecking>
1444 <webappDirectory>${project.build.directory}/classes/static</webappDirectory>
1445 </configuration>
1446 <executions>
1447 <execution>
1448 <goals>
1449 <goal>compile</goal>
1450 </goals>
1451 </execution>
1452 </executions>
1453</plugin>
1454----
1455
1456To attach a GWT widget defined by the plugin to the Gerrit core UI
1457`com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the
1458Gerrit core widgets:
1459
1460[source,java]
1461----
1462RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1463rootPanel.getElement().removeAttribute("href");
1464rootPanel.addDomHandler(new ClickHandler() {
1465 @Override
1466 public void onClick(ClickEvent event) {
1467 dialogBox.center();
1468 dialogBox.show();
1469 }
1470}, ClickEvent.getType());
1471----
1472
1473GWT plugins can come with their own css file. This css file must have a
1474unique name and must be registered in the GWT module:
1475
1476[source,xml]
1477----
1478<stylesheet src="hello.css"/>
1479----
1480
Edwin Kempin2570b102013-11-11 11:44:50 +01001481If a GWT plugin wants to invoke the Gerrit REST API it can use
1482`com.google.gerrit.plugin.client.rpc.RestApi` to contruct the URL
1483path and to trigger the REST calls.
1484
1485Example for invoking a Gerrit core REST endpoint:
1486
1487[source,java]
1488----
1489new RestApi("projects").id(projectName).view("description")
1490 .put("new description", new AsyncCallback<JavaScriptObject>() {
1491
1492 @Override
1493 public void onSuccess(JavaScriptObject result) {
1494 // TODO
1495 }
1496
1497 @Override
1498 public void onFailure(Throwable caught) {
1499 // never invoked
1500 }
1501});
1502----
1503
1504Example for invoking a REST endpoint defined by a plugin:
1505
1506[source,java]
1507----
1508new RestApi("projects").id(projectName).view("myplugin", "myview")
1509 .get(new AsyncCallback<JavaScriptObject>() {
1510
1511 @Override
1512 public void onSuccess(JavaScriptObject result) {
1513 // TODO
1514 }
1515
1516 @Override
1517 public void onFailure(Throwable caught) {
1518 // never invoked
1519 }
1520});
1521----
1522
1523The `onFailure(Throwable)` of the provided callback is never invoked.
1524If an error occurs, it is shown in an error dialog.
1525
1526In order to be able to do REST calls the GWT module must inherit
1527`com.google.gwt.json.JSON`:
1528
1529[source,xml]
1530----
1531<inherits name="com.google.gwt.json.JSON"/>
1532----
1533
Shawn Pearced5c844f2013-12-26 15:32:26 -08001534== Add Screen
1535A GWT plugin can add a menu item that opens a screen that is
1536implemented by the plugin. This way plugin screens can be fully
1537integrated into the Gerrit UI.
1538
1539Example menu item:
1540[source,java]
1541----
1542public class MyMenu implements TopMenu {
1543 private final List<MenuEntry> menuEntries;
1544
1545 @Inject
1546 public MyMenu(@PluginName String name) {
1547 menuEntries = Lists.newArrayList();
1548 menuEntries.add(new MenuEntry("My Menu", Collections.singletonList(
1549 new MenuItem("My Screen", "#/x/" + name + "/my-screen", ""))));
1550 }
1551
1552 @Override
1553 public List<MenuEntry> getEntries() {
1554 return menuEntries;
1555 }
1556}
1557----
1558
1559Example screen:
1560[source,java]
1561----
1562public class MyPlugin extends PluginEntryPoint {
1563 @Override
1564 public void onPluginLoad() {
1565 Plugin.get().screen("my-screen", new Screen.EntryPoint() {
1566 @Override
1567 public void onLoad(Screen screen) {
1568 screen.add(new InlineLabel("My Screen");
1569 screen.show();
1570 }
1571 });
1572 }
1573}
1574----
1575
Edwin Kempinf5a77332012-07-18 11:17:53 +02001576[[http]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001577== HTTP Servlets
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001578
1579Plugins or extensions may register additional HTTP servlets, and
1580wrap them with HTTP filters.
1581
1582Servlets may use auto-registration to declare the URL they handle:
1583
David Pursehouse68153d72013-09-04 10:09:17 +09001584[source,java]
1585----
1586import com.google.gerrit.extensions.annotations.Export;
1587import com.google.inject.Singleton;
1588import javax.servlet.http.HttpServlet;
1589import javax.servlet.http.HttpServletRequest;
1590import javax.servlet.http.HttpServletResponse;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001591
David Pursehouse68153d72013-09-04 10:09:17 +09001592@Export("/print")
1593@Singleton
1594class HelloServlet extends HttpServlet {
1595 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
1596 res.setContentType("text/plain");
1597 res.setCharacterEncoding("UTF-8");
1598 res.getWriter().write("Hello");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001599 }
David Pursehouse68153d72013-09-04 10:09:17 +09001600}
1601----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001602
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001603The auto registration only works for standard servlet mappings like
1604`/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule
1605to register the HTTP servlets and declare it explicitly in the manifest
1606with the `Gerrit-HttpModule` attribute:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001607
David Pursehouse68153d72013-09-04 10:09:17 +09001608[source,java]
1609----
1610import com.google.inject.servlet.ServletModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001611
David Pursehouse68153d72013-09-04 10:09:17 +09001612class MyWebUrls extends ServletModule {
1613 protected void configureServlets() {
1614 serve("/print").with(HelloServlet.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001615 }
David Pursehouse68153d72013-09-04 10:09:17 +09001616}
1617----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001618
1619For a plugin installed as name `helloworld`, the servlet implemented
1620by HelloServlet class will be available to users as:
1621
1622----
1623$ curl http://review.example.com/plugins/helloworld/print
1624----
Nasser Grainawie033b262012-05-09 17:54:21 -07001625
Edwin Kempinf5a77332012-07-18 11:17:53 +02001626[[data-directory]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001627== Data Directory
Edwin Kempin41f63912012-07-17 12:33:55 +02001628
1629Plugins can request a data directory with a `@PluginData` File
1630dependency. A data directory will be created automatically by the
1631server in `$site_path/data/$plugin_name` and passed to the plugin.
1632
1633Plugins can use this to store any data they want.
1634
David Pursehouse68153d72013-09-04 10:09:17 +09001635[source,java]
1636----
1637@Inject
1638MyType(@PluginData java.io.File myDir) {
1639 new FileInputStream(new File(myDir, "my.config"));
1640}
1641----
Edwin Kempin41f63912012-07-17 12:33:55 +02001642
Edwin Kempinea621482013-10-16 12:58:24 +02001643[[download-commands]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001644== Download Commands
Edwin Kempinea621482013-10-16 12:58:24 +02001645
1646Gerrit offers commands for downloading changes using different
1647download schemes (e.g. for downloading via different network
1648protocols). Plugins can contribute download schemes and download
1649commands by implementing
1650`com.google.gerrit.extensions.config.DownloadScheme` and
1651`com.google.gerrit.extensions.config.DownloadCommand`.
1652
1653The download schemes and download commands which are used most often
1654are provided by the Gerrit core plugin `download-commands`.
1655
Edwin Kempinf5a77332012-07-18 11:17:53 +02001656[[documentation]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001657== Documentation
Nasser Grainawie033b262012-05-09 17:54:21 -07001658
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001659If a plugin does not register a filter or servlet to handle URLs
1660`/Documentation/*` or `/static/*`, the core Gerrit server will
1661automatically export these resources over HTTP from the plugin JAR.
1662
David Pursehouse6853b5a2013-07-10 11:38:03 +09001663Static resources under the `static/` directory in the JAR will be
Dave Borowitzb893ac82013-03-27 10:03:55 -04001664available as `/plugins/helloworld/static/resource`. This prefix is
1665configurable by setting the `Gerrit-HttpStaticPrefix` attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001666
David Pursehouse6853b5a2013-07-10 11:38:03 +09001667Documentation files under the `Documentation/` directory in the JAR
Dave Borowitzb893ac82013-03-27 10:03:55 -04001668will be available as `/plugins/helloworld/Documentation/resource`. This
1669prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix`
1670attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001671
1672Documentation may be written in
1673link:http://daringfireball.net/projects/markdown/[Markdown] style
1674if the file name ends with `.md`. Gerrit will automatically convert
1675Markdown to HTML if accessed with extension `.html`.
Nasser Grainawie033b262012-05-09 17:54:21 -07001676
Edwin Kempinf5a77332012-07-18 11:17:53 +02001677[[macros]]
Edwin Kempinc78777d2012-07-16 15:55:11 +02001678Within the Markdown documentation files macros can be used that allow
1679to write documentation with reasonably accurate examples that adjust
1680automatically based on the installation.
1681
1682The following macros are supported:
1683
1684[width="40%",options="header"]
1685|===================================================
1686|Macro | Replacement
1687|@PLUGIN@ | name of the plugin
1688|@URL@ | Gerrit Web URL
1689|@SSH_HOST@ | SSH Host
1690|@SSH_PORT@ | SSH Port
1691|===================================================
1692
1693The macros will be replaced when the documentation files are rendered
1694from Markdown to HTML.
1695
1696Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@`
1697even if there is an expansion for `KEEP` in the future.
1698
Edwin Kempinf5a77332012-07-18 11:17:53 +02001699[[auto-index]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001700=== Automatic Index
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001701
1702If a plugin does not handle its `/` URL itself, Gerrit will
1703redirect clients to the plugin's `/Documentation/index.html`.
1704Requests for `/Documentation/` (bare directory) will also redirect
1705to `/Documentation/index.html`.
1706
1707If neither resource `Documentation/index.html` or
1708`Documentation/index.md` exists in the plugin JAR, Gerrit will
1709automatically generate an index page for the plugin's documentation
1710tree by scanning every `*.md` and `*.html` file in the Documentation/
1711directory.
1712
1713For any discovered Markdown (`*.md`) file, Gerrit will parse the
1714header of the file and extract the first level one title. This
1715title text will be used as display text for a link to the HTML
1716version of the page.
1717
1718For any discovered HTML (`*.html`) file, Gerrit will use the name
1719of the file, minus the `*.html` extension, as the link text. Any
1720hyphens in the file name will be replaced with spaces.
1721
David Pursehouse6853b5a2013-07-10 11:38:03 +09001722If a discovered file is named `about.md` or `about.html`, its
1723content will be inserted in an 'About' section at the top of the
1724auto-generated index page. If both `about.md` and `about.html`
1725exist, only the first discovered file will be used.
1726
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001727If a discovered file name beings with `cmd-` it will be clustered
David Pursehouse6853b5a2013-07-10 11:38:03 +09001728into a 'Commands' section of the generated index page.
1729
David Pursehousefe529152013-08-14 16:35:06 +09001730If a discovered file name beings with `servlet-` it will be clustered
1731into a 'Servlets' section of the generated index page.
1732
1733If a discovered file name beings with `rest-api-` it will be clustered
1734into a 'REST APIs' section of the generated index page.
1735
David Pursehouse6853b5a2013-07-10 11:38:03 +09001736All other files are clustered under a 'Documentation' section.
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001737
1738Some optional information from the manifest is extracted and
1739displayed as part of the index page, if present in the manifest:
1740
1741[width="40%",options="header"]
1742|===================================================
1743|Field | Source Attribute
1744|Name | Implementation-Title
1745|Vendor | Implementation-Vendor
1746|Version | Implementation-Version
1747|URL | Implementation-URL
1748|API Version | Gerrit-ApiVersion
1749|===================================================
1750
Edwin Kempinf5a77332012-07-18 11:17:53 +02001751[[deployment]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001752== Deployment
Nasser Grainawie033b262012-05-09 17:54:21 -07001753
Edwin Kempinf7295742012-07-16 15:03:46 +02001754Compiled plugins and extensions can be deployed to a running Gerrit
1755server using the link:cmd-plugin-install.html[plugin install] command.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001756
Dariusz Luksza357a2422012-11-12 06:16:26 +01001757WebUI plugins distributed as single `.js` file can be deployed
1758without the overhead of JAR packaging, for more information refer to
1759link:cmd-plugin-install.html[plugin install] command.
1760
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001761Plugins can also be copied directly into the server's
Dariusz Luksza357a2422012-11-12 06:16:26 +01001762directory at `$site_path/plugins/$name.(jar|js)`. The name of
1763the JAR file, minus the `.jar` or `.js` extension, will be used as the
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001764plugin name. Unless disabled, servers periodically scan this
1765directory for updated plugins. The time can be adjusted by
1766link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency].
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001767
Edwin Kempinf7295742012-07-16 15:03:46 +02001768For disabling plugins the link:cmd-plugin-remove.html[plugin remove]
1769command can be used.
1770
Brad Larsond5e87c32012-07-11 12:18:49 -05001771Disabled plugins can be re-enabled using the
1772link:cmd-plugin-enable.html[plugin enable] command.
1773
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001774== SEE ALSO
David Ostrovskyf86bae52013-09-01 09:10:39 +02001775
1776* link:js-api.html[JavaScript API]
1777* link:dev-rest-api.html[REST API Developers' Notes]
1778
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001779GERRIT
1780------
1781Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07001782
1783SEARCHBOX
1784---------