blob: f935eb40ffdddf14812746d54f451424a8435396 [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 \
Edwin Kempin10ee9f32014-04-14 09:25:30 +020039 -DarchetypeVersion=2.10-SNAPSHOT \
Edwin Kempin441a0b82014-06-04 14:56:23 +020040 -DarchetypeRepository=https://oss.sonatype.org/content/repositories/snapshots/ \
Edwin Kempin91155c22013-12-02 20:25:18 +010041 -DgroupId=com.googlesource.gerrit.plugins.testplugin \
42 -DartifactId=testplugin
Edwin Kempinf878c4b2012-07-18 09:34:25 +020043----
44+
45Maven will ask for additional properties and then create the plugin in
46the current directory. To change the default property values answer 'n'
47when Maven asks to confirm the properties configuration. It will then
48ask again for all properties including those with predefined default
49values.
50
David Pursehouse2cf0cb52013-08-27 16:09:53 +090051. clone the sample plugin:
Edwin Kempinf878c4b2012-07-18 09:34:25 +020052+
David Pursehouse2cf0cb52013-08-27 16:09:53 +090053This is a project that demonstrates the various features of the
54plugin API. It can be taken as an example to develop an own plugin.
Edwin Kempinf878c4b2012-07-18 09:34:25 +020055+
Dave Borowitz5cc8f662012-05-21 09:51:36 -070056----
David Pursehouse2cf0cb52013-08-27 16:09:53 +090057$ git clone https://gerrit.googlesource.com/plugins/cookbook-plugin
Dave Borowitz5cc8f662012-05-21 09:51:36 -070058----
Edwin Kempinf878c4b2012-07-18 09:34:25 +020059+
60When starting from this example one should take care to adapt the
61`Gerrit-ApiVersion` in the `pom.xml` to the version of Gerrit for which
62the plugin is developed. If the plugin is developed for a released
63Gerrit version (no `SNAPSHOT` version) then the URL for the
64`gerrit-api-repository` in the `pom.xml` needs to be changed to
Shawn Pearced5005002013-06-21 11:01:45 -070065`https://gerrit-api.storage.googleapis.com/release/`.
Dave Borowitz5cc8f662012-05-21 09:51:36 -070066
Edwin Kempinf878c4b2012-07-18 09:34:25 +020067[[API]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080068== API
Edwin Kempinf878c4b2012-07-18 09:34:25 +020069
70There are two different API formats offered against which plugins can
71be developed:
Deniz Türkoglueb78b602012-05-07 14:02:36 -070072
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070073gerrit-extension-api.jar::
74 A stable but thin interface. Suitable for extensions that need
75 to be notified of events, but do not require tight coupling to
76 the internals of Gerrit. Extensions built against this API can
77 expect to be binary compatible across a wide range of server
78 versions.
Deniz Türkoglueb78b602012-05-07 14:02:36 -070079
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070080gerrit-plugin-api.jar::
81 The complete internals of the Gerrit server, permitting a
82 plugin to tightly couple itself and provide additional
83 functionality that is not possible as an extension. Plugins
84 built against this API are expected to break at the source
85 code level between every major.minor Gerrit release. A plugin
86 that compiles against 2.5 will probably need source code level
87 changes to work with 2.6, 2.7, and so on.
Deniz Türkoglueb78b602012-05-07 14:02:36 -070088
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -080089== Manifest
Deniz Türkoglueb78b602012-05-07 14:02:36 -070090
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070091Plugins may provide optional description information with standard
92manifest fields:
Nasser Grainawie033b262012-05-09 17:54:21 -070093
Shawn O. Pearceda4919a2012-05-10 16:54:28 -070094====
95 Implementation-Title: Example plugin showing examples
96 Implementation-Version: 1.0
97 Implementation-Vendor: Example, Inc.
98 Implementation-URL: http://example.com/opensource/plugin-foo/
99====
Nasser Grainawie033b262012-05-09 17:54:21 -0700100
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800101=== ApiType
Nasser Grainawie033b262012-05-09 17:54:21 -0700102
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700103Plugins using the tightly coupled `gerrit-plugin-api.jar` must
104declare this API dependency in the manifest to gain access to server
Edwin Kempin948de0f2012-07-16 10:34:35 +0200105internals. If no `Gerrit-ApiType` is specified the stable `extension`
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700106API will be assumed. This may cause ClassNotFoundExceptions when
107loading a plugin that needs the plugin API.
Nasser Grainawie033b262012-05-09 17:54:21 -0700108
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700109====
110 Gerrit-ApiType: plugin
111====
112
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800113=== Explicit Registration
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700114
115Plugins that use explicit Guice registration must name the Guice
116modules in the manifest. Up to three modules can be named in the
Edwin Kempin948de0f2012-07-16 10:34:35 +0200117manifest. `Gerrit-Module` supplies bindings to the core server;
118`Gerrit-SshModule` supplies SSH commands to the SSH server (if
119enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700120server (if enabled). If no modules are named automatic registration
121will be performed by scanning all classes in the plugin JAR for
122`@Listen` and `@Export("")` annotations.
123
124====
125 Gerrit-Module: tld.example.project.CoreModuleClassName
126 Gerrit-SshModule: tld.example.project.SshModuleClassName
127 Gerrit-HttpModule: tld.example.project.HttpModuleClassName
128====
129
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200130[[plugin_name]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800131=== Plugin Name
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200132
David Pursehoused128c892013-10-22 21:52:21 +0900133A plugin can optionally provide its own plugin name.
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200134
135====
136 Gerrit-PluginName: replication
137====
138
139This is useful for plugins that contribute plugin-owned capabilities that
140are stored in the `project.config` file. Another use case is to be able to put
141project specific plugin configuration section in `project.config`. In this
142case it is advantageous to reserve the plugin name to access the configuration
143section in the `project.config` file.
144
145If `Gerrit-PluginName` is omitted, then the plugin's name is determined from
146the plugin file name.
147
148If a plugin provides its own name, then that plugin cannot be deployed
149multiple times under different file names on one Gerrit site.
150
151For Maven driven plugins, the following line must be included in the pom.xml
152file:
153
154[source,xml]
155----
156<manifestEntries>
157 <Gerrit-PluginName>name</Gerrit-PluginName>
158</manifestEntries>
159----
160
161For Buck driven plugins, the following line must be included in the BUCK
162configuration file:
163
164[source,python]
165----
David Pursehouse529ec252013-09-27 13:45:14 +0900166manifest_entries = [
167 'Gerrit-PluginName: name',
168]
David Ostrovsky366ad0e2013-09-05 19:59:09 +0200169----
170
Edwin Kempinc0b1b0e2013-10-01 14:13:54 +0200171A plugin can get its own name injected at runtime:
172
173[source,java]
174----
175public class MyClass {
176
177 private final String pluginName;
178
179 @Inject
180 public MyClass(@PluginName String pluginName) {
181 this.pluginName = pluginName;
182 }
183
David Pursehoused128c892013-10-22 21:52:21 +0900184 [...]
Edwin Kempinc0b1b0e2013-10-01 14:13:54 +0200185}
186----
187
David Pursehouse8ed0d922013-10-18 18:57:56 +0900188A plugin can get its canonical web URL injected at runtime:
189
190[source,java]
191----
192public class MyClass {
193
194 private final String url;
195
196 @Inject
197 public MyClass(@PluginCanonicalWebUrl String url) {
198 this.url = url;
199 }
200
201 [...]
202}
203----
204
205The URL is composed of the server's canonical web URL and the plugin's
206name, i.e. `http://review.example.com:8080/plugin-name`.
207
208The canonical web URL may be injected into any .jar plugin regardless of
209whether or not the plugin provides an HTTP servlet.
210
Edwin Kempinf7295742012-07-16 15:03:46 +0200211[[reload_method]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800212=== Reload Method
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700213
214If a plugin holds an exclusive resource that must be released before
215loading the plugin again (for example listening on a network port or
Edwin Kempin948de0f2012-07-16 10:34:35 +0200216acquiring a file lock) the manifest must declare `Gerrit-ReloadMode`
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700217to be `restart`. Otherwise the preferred method of `reload` will
218be used, as it enables the server to hot-patch an updated plugin
219with no down time.
220
221====
222 Gerrit-ReloadMode: restart
223====
224
225In either mode ('restart' or 'reload') any plugin or extension can
226be updated without restarting the Gerrit server. The difference is
227how Gerrit handles the upgrade:
228
229restart::
230 The old plugin is completely stopped. All registrations of SSH
231 commands and HTTP servlets are removed. All registrations of any
232 extension points are removed. All registered LifecycleListeners
233 have their `stop()` method invoked in reverse order. The new
234 plugin is started, and registrations are made from the new
235 plugin. There is a brief window where neither the old nor the
236 new plugin is connected to the server. This means SSH commands
237 and HTTP servlets will return not found errors, and the plugin
238 will not be notified of events that occurred during the restart.
239
240reload::
241 The new plugin is started. Its LifecycleListeners are permitted
242 to perform their `start()` methods. All SSH and HTTP registrations
243 are atomically swapped out from the old plugin to the new plugin,
244 ensuring the server never returns a not found error. All extension
245 point listeners are atomically swapped out from the old plugin to
246 the new plugin, ensuring no events are missed (however some events
247 may still route to the old plugin if the swap wasn't complete yet).
248 The old plugin is stopped.
249
Edwin Kempinf7295742012-07-16 15:03:46 +0200250To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload]
251command can be used.
252
Luca Milanesio737285d2012-09-25 14:26:43 +0100253[[init_step]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800254=== Init step
Luca Milanesio737285d2012-09-25 14:26:43 +0100255
256Plugins can contribute their own "init step" during the Gerrit init
257wizard. This is useful for guiding the Gerrit administrator through
David Pursehouse659860f2013-12-16 14:50:04 +0900258the settings needed by the plugin to work properly.
Luca Milanesio737285d2012-09-25 14:26:43 +0100259
260For instance plugins to integrate Jira issues to Gerrit changes may
261contribute their own "init step" to allow configuring the Jira URL,
262credentials and possibly verify connectivity to validate them.
263
264====
265 Gerrit-InitStep: tld.example.project.MyInitStep
266====
267
268MyInitStep needs to follow the standard Gerrit InitStep syntax
David Pursehouse92463562013-06-24 10:16:28 +0900269and behavior: writing to the console using the injected ConsoleUI
Luca Milanesio737285d2012-09-25 14:26:43 +0100270and accessing / changing configuration settings using Section.Factory.
271
272In addition to the standard Gerrit init injections, plugins receive
273the @PluginName String injection containing their own plugin name.
274
Edwin Kempind4cfac12013-11-27 11:22:34 +0100275During their initialization plugins may get access to the
276`project.config` file of the `All-Projects` project and they are able
277to store configuration parameters in it. For this a plugin `InitStep`
278can get `com.google.gerrit.pgm.init.AllProjectsConfig` injected:
279
280[source,java]
281----
282 public class MyInitStep implements InitStep {
283 private final String pluginName;
284 private final ConsoleUI ui;
285 private final AllProjectsConfig allProjectsConfig;
286
287 public MyInitStep(@PluginName String pluginName, ConsoleUI ui,
288 AllProjectsConfig allProjectsConfig) {
289 this.pluginName = pluginName;
290 this.ui = ui;
291 this.allProjectsConfig = allProjectsConfig;
292 }
293
294 @Override
295 public void run() throws Exception {
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100296 }
297
298 @Override
299 public void postRun() throws Exception {
Edwin Kempind4cfac12013-11-27 11:22:34 +0100300 ui.message("\n");
301 ui.header(pluginName + " Integration");
302 boolean enabled = ui.yesno(true, "By default enabled for all projects");
Adrian Görlerd1612972014-10-20 17:06:07 +0200303 Config cfg = allProjectsConfig.load().getConfig();
Edwin Kempind4cfac12013-11-27 11:22:34 +0100304 if (enabled) {
305 cfg.setBoolean("plugin", pluginName, "enabled", enabled);
306 } else {
307 cfg.unset("plugin", pluginName, "enabled");
308 }
309 allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration");
310 }
311 }
312----
313
Luca Milanesio737285d2012-09-25 14:26:43 +0100314Bear in mind that the Plugin's InitStep class will be loaded but
315the standard Gerrit runtime environment is not available and the plugin's
316own Guice modules were not initialized.
317This means the InitStep for a plugin is not executed in the same way that
318the plugin executes within the server, and may mean a plugin author cannot
319trivially reuse runtime code during init.
320
321For instance a plugin that wants to verify connectivity may need to statically
322call the constructor of their connection class, passing in values obtained
323from the Section.Factory rather than from an injected Config object.
324
David Pursehoused128c892013-10-22 21:52:21 +0900325Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after
326the extraction of the plugins embedded in the distribution .war file into
327`$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade.
328
329A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit
330runtime objects injected at startup.
Luca Milanesio737285d2012-09-25 14:26:43 +0100331
David Pursehouse68153d72013-09-04 10:09:17 +0900332[source,java]
333----
334public class MyInitStep implements InitStep {
335 private final ConsoleUI ui;
336 private final Section.Factory sections;
337 private final String pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100338
David Pursehouse68153d72013-09-04 10:09:17 +0900339 @Inject
340 public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) {
341 this.ui = ui;
342 this.sections = sections;
343 this.pluginName = pluginName;
Luca Milanesio737285d2012-09-25 14:26:43 +0100344 }
David Pursehouse68153d72013-09-04 10:09:17 +0900345
346 @Override
347 public void run() throws Exception {
348 ui.header("\nMy plugin");
349
350 Section mySection = getSection("myplugin", null);
351 mySection.string("Link name", "linkname", "MyLink");
352 }
Edwin Kempin93e7d5d2014-01-03 09:53:20 +0100353
354 @Override
355 public void postRun() throws Exception {
356 }
David Pursehouse68153d72013-09-04 10:09:17 +0900357}
358----
Luca Milanesio737285d2012-09-25 14:26:43 +0100359
Edwin Kempinf5a77332012-07-18 11:17:53 +0200360[[classpath]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800361== Classpath
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700362
363Each plugin is loaded into its own ClassLoader, isolating plugins
364from each other. A plugin or extension inherits the Java runtime
365and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin)
366from the hosting server.
367
368Plugins are loaded from a single JAR file. If a plugin needs
369additional libraries, it must include those dependencies within
370its own JAR. Plugins built using Maven may be able to use the
371link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin]
372to package additional dependencies. Relocating (or renaming) classes
373should not be necessary due to the ClassLoader isolation.
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700374
Edwin Kempin98202662013-09-18 16:03:03 +0200375[[events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800376== Listening to Events
Edwin Kempin98202662013-09-18 16:03:03 +0200377
378Certain operations in Gerrit trigger events. Plugins may receive
379notifications of these events by implementing the corresponding
380listeners.
381
Martin Fick4c72aea2014-12-10 14:58:12 -0700382* `com.google.gerrit.common.EventListener`:
Edwin Kempin64059f52013-10-31 13:49:25 +0100383+
Martin Fick4c72aea2014-12-10 14:58:12 -0700384Allows to listen to events. These are the same
Edwin Kempin64059f52013-10-31 13:49:25 +0100385link:cmd-stream-events.html#events[events] that are also streamed by
386the link:cmd-stream-events.html[gerrit stream-events] command.
387
Edwin Kempin98202662013-09-18 16:03:03 +0200388* `com.google.gerrit.extensions.events.LifecycleListener`:
389+
Edwin Kempin3e7928a2013-12-03 07:39:00 +0100390Plugin start and stop
Edwin Kempin98202662013-09-18 16:03:03 +0200391
392* `com.google.gerrit.extensions.events.NewProjectCreatedListener`:
393+
394Project creation
395
396* `com.google.gerrit.extensions.events.ProjectDeletedListener`:
397+
398Project deletion
399
Edwin Kempinb27c9392013-11-19 13:12:43 +0100400* `com.google.gerrit.extensions.events.HeadUpdatedListener`:
401+
402Update of HEAD on a project
403
Stefan Lay310d77d2014-05-28 13:45:25 +0200404* `com.google.gerrit.extensions.events.UsageDataPublishedListener`:
405+
406Publication of usage data
407
Yang Zhenhui2659d422013-07-30 16:59:58 +0800408[[stream-events]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800409== Sending Events to the Events Stream
Yang Zhenhui2659d422013-07-30 16:59:58 +0800410
411Plugins may send events to the events stream where consumers of
412Gerrit's `stream-events` ssh command will receive them.
413
414To send an event, the plugin must invoke one of the `postEvent`
415methods in the `ChangeHookRunner` class, passing an instance of
Martin Fick4c72aea2014-12-10 14:58:12 -0700416its own custom event class derived from
417`com.google.gerrit.server.events.Event`.
Yang Zhenhui2659d422013-07-30 16:59:58 +0800418
Edwin Kempin32737602014-01-23 09:04:58 +0100419[[validation]]
David Pursehouse91c5f5e2014-01-23 18:57:33 +0900420== Validation Listeners
Edwin Kempin32737602014-01-23 09:04:58 +0100421
422Certain operations in Gerrit can be validated by plugins by
423implementing the corresponding link:config-validation.html[listeners].
424
Saša Živkovec85a072014-01-28 10:08:25 +0100425[[receive-pack]]
426== Receive Pack Initializers
427
428Plugins may provide ReceivePack initializers which will be invoked
429by Gerrit just before a ReceivePack instance will be used. Usually,
430plugins will make use of the setXXX methods on the ReceivePack to
431set additional properties on it.
432
Saša Živkov626c7312014-02-24 17:15:08 +0100433[[post-receive-hook]]
434== Post Receive-Pack Hooks
435
436Plugins may register PostReceiveHook instances in order to get
437notified when JGit successfully receives a pack. This may be useful
438for those plugins which would like to monitor changes in Git
439repositories.
440
Hugo Arès572d5422014-06-17 14:22:03 -0400441[[pre-upload-hook]]
442== Pre Upload-Pack Hooks
443
444Plugins may register PreUploadHook instances in order to get
445notified when JGit is about to upload a pack. This may be useful
446for those plugins which would like to monitor usage in Git
447repositories.
448
Edwin Kempinf5a77332012-07-18 11:17:53 +0200449[[ssh]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800450== SSH Commands
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700451
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700452Plugins may provide commands that can be accessed through the SSH
453interface (extensions do not have this option).
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700454
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700455Command implementations must extend the base class SshCommand:
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700456
David Pursehouse68153d72013-09-04 10:09:17 +0900457[source,java]
458----
459import com.google.gerrit.sshd.SshCommand;
David Ostrovskyb7d97752013-11-09 05:23:26 +0100460import com.google.gerrit.sshd.CommandMetaData;
Deniz Türkoglueb78b602012-05-07 14:02:36 -0700461
Ian Bulle1a12202014-02-16 17:15:42 -0800462@CommandMetaData(name="print", description="Print hello command")
David Pursehouse68153d72013-09-04 10:09:17 +0900463class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800464 @Override
465 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900466 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700467 }
David Pursehouse68153d72013-09-04 10:09:17 +0900468}
469----
Nasser Grainawie033b262012-05-09 17:54:21 -0700470
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700471If no Guice modules are declared in the manifest, SSH commands may
Edwin Kempin948de0f2012-07-16 10:34:35 +0200472use auto-registration by providing an `@Export` annotation:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700473
David Pursehouse68153d72013-09-04 10:09:17 +0900474[source,java]
475----
476import com.google.gerrit.extensions.annotations.Export;
477import com.google.gerrit.sshd.SshCommand;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700478
David Pursehouse68153d72013-09-04 10:09:17 +0900479@Export("print")
480class PrintHello extends SshCommand {
Ian Bulle1a12202014-02-16 17:15:42 -0800481 @Override
482 protected void run() {
David Pursehouse68153d72013-09-04 10:09:17 +0900483 stdout.print("Hello\n");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700484 }
David Pursehouse68153d72013-09-04 10:09:17 +0900485}
486----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700487
488If explicit registration is being used, a Guice module must be
489supplied to register the SSH command and declared in the manifest
490with the `Gerrit-SshModule` attribute:
491
David Pursehouse68153d72013-09-04 10:09:17 +0900492[source,java]
493----
494import com.google.gerrit.sshd.PluginCommandModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700495
David Pursehouse68153d72013-09-04 10:09:17 +0900496class MyCommands extends PluginCommandModule {
Ian Bulle1a12202014-02-16 17:15:42 -0800497 @Override
David Pursehouse68153d72013-09-04 10:09:17 +0900498 protected void configureCommands() {
David Ostrovskyb7d97752013-11-09 05:23:26 +0100499 command(PrintHello.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700500 }
David Pursehouse68153d72013-09-04 10:09:17 +0900501}
502----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700503
504For a plugin installed as name `helloworld`, the command implemented
505by PrintHello class will be available to users as:
506
507----
Keunhong Parka09a6f12012-07-10 14:45:02 -0600508$ ssh -p 29418 review.example.com helloworld print
Shawn O. Pearceda4919a2012-05-10 16:54:28 -0700509----
510
David Ostrovsky79c4d892014-03-15 13:52:46 +0100511[[multiple-commands]]
512=== Multiple Commands bound to one implementation
513
David Ostrovskye3172b32013-10-13 14:19:13 +0200514Multiple SSH commands can be bound to the same implementation class. For
515example a Gerrit Shell plugin can bind different shell commands to the same
516implementation class:
517
518[source,java]
519----
520public class SshShellModule extends PluginCommandModule {
521 @Override
522 protected void configureCommands() {
523 command("ls").to(ShellCommand.class);
524 command("ps").to(ShellCommand.class);
525 [...]
526 }
527}
528----
529
530With the possible implementation:
531
532[source,java]
533----
534public class ShellCommand extends SshCommand {
535 @Override
536 protected void run() throws UnloggedFailure {
537 String cmd = getName().substring(getPluginName().length() + 1);
538 ProcessBuilder proc = new ProcessBuilder(cmd);
539 Process cmd = proc.start();
540 [...]
541 }
542}
543----
544
545And the call:
546
547----
548$ ssh -p 29418 review.example.com shell ls
549$ ssh -p 29418 review.example.com shell ps
550----
551
David Ostrovsky79c4d892014-03-15 13:52:46 +0100552[[root-level-commands]]
553=== Root Level Commands
554
David Ostrovskyb7d97752013-11-09 05:23:26 +0100555Single command plugins are also supported. In this scenario plugin binds
556SSH command to its own name. `SshModule` must inherit from
557`SingleCommandPluginModule` class:
558
559[source,java]
560----
561public class SshModule extends SingleCommandPluginModule {
562 @Override
563 protected void configure(LinkedBindingBuilder<Command> b) {
564 b.to(ShellCommand.class);
565 }
566}
567----
568
569If the plugin above is deployed under sh.jar file in `$site/plugins`
David Pursehouse659860f2013-12-16 14:50:04 +0900570directory, generic commands can be called without specifying the
David Ostrovskyb7d97752013-11-09 05:23:26 +0100571actual SSH command. Note in the example below, that the called commands
572`ls` and `ps` was not explicitly bound:
573
574----
575$ ssh -p 29418 review.example.com sh ls
576$ ssh -p 29418 review.example.com sh ps
577----
578
Edwin Kempin78ca0942013-10-30 11:24:06 +0100579[[simple-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800580== Simple Configuration in `gerrit.config`
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200581
582In Gerrit, global configuration is stored in the `gerrit.config` file.
583If a plugin needs global configuration, this configuration should be
584stored in a `plugin` subsection in the `gerrit.config` file.
585
Edwin Kempinc9b68602013-10-30 09:32:43 +0100586This approach of storing the plugin configuration is only suitable for
587plugins that have a simple configuration that only consists of
588key-value pairs. With this approach it is not possible to have
589subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin78ca0942013-10-30 11:24:06 +0100590configuration need to store their configuration in their
591link:#configuration[own configuration file] where they can make use of
592subsections. On the other hand storing the plugin configuration in a
593'plugin' subsection in the `gerrit.config` file has the advantage that
594administrators have all configuration parameters in one file, instead
595of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100596
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200597To avoid conflicts with other plugins, it is recommended that plugins
598only use the `plugin` subsection with their own name. For example the
599`helloworld` plugin should store its configuration in the
600`plugin.helloworld` subsection:
601
602----
603[plugin "helloworld"]
604 language = Latin
605----
606
Sasa Zivkovacdf5332013-09-20 14:05:15 +0200607Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200608plugin can easily access its configuration and there is no need for a
609plugin to parse the `gerrit.config` file on its own:
610
611[source,java]
612----
David Pursehouse529ec252013-09-27 13:45:14 +0900613@Inject
614private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200615
David Pursehoused128c892013-10-22 21:52:21 +0900616[...]
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200617
Edwin Kempin122622d2013-10-29 16:45:44 +0100618String language = cfg.getFromGerritConfig("helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900619 .getString("language", "English");
Edwin Kempinf7bfff82013-09-17 13:34:20 +0200620----
621
Edwin Kempin78ca0942013-10-30 11:24:06 +0100622[[configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800623== Configuration in own config file
Edwin Kempin78ca0942013-10-30 11:24:06 +0100624
625Plugins can store their configuration in an own configuration file.
626This makes sense if the plugin configuration is rather complex and
627requires the usage of subsections. Plugins that have a simple
628key-value pair configuration can store their configuration in a
629link:#simple-configuration[`plugin` subsection of the `gerrit.config`
630file].
631
632The plugin configuration file must be named after the plugin and must
633be located in the `etc` folder of the review site. For example a
634configuration file for a `default-reviewer` plugin could look like
635this:
636
637.$site_path/etc/default-reviewer.config
638----
639[branch "refs/heads/master"]
640 reviewer = Project Owners
641 reviewer = john.doe@example.com
642[match "file:^.*\.txt"]
643 reviewer = My Info Developers
644----
645
646Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
647plugin can easily access its configuration:
648
649[source,java]
650----
651@Inject
652private com.google.gerrit.server.config.PluginConfigFactory cfg;
653
654[...]
655
656String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer")
657 .getStringList("branch", "refs/heads/master", "reviewer");
658----
659
Edwin Kempin78ca0942013-10-30 11:24:06 +0100660
Edwin Kempin705f2842013-10-30 14:25:31 +0100661[[simple-project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800662== Simple Project Specific Configuration in `project.config`
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200663
664In Gerrit, project specific configuration is stored in the project's
665`project.config` file on the `refs/meta/config` branch. If a plugin
666needs configuration on project level (e.g. to enable its functionality
667only for certain projects), this configuration should be stored in a
668`plugin` subsection in the project's `project.config` file.
669
Edwin Kempinc9b68602013-10-30 09:32:43 +0100670This approach of storing the plugin configuration is only suitable for
671plugins that have a simple configuration that only consists of
672key-value pairs. With this approach it is not possible to have
673subsections in the plugin configuration. Plugins that require a complex
Edwin Kempin705f2842013-10-30 14:25:31 +0100674configuration need to store their configuration in their
675link:#project-specific-configuration[own configuration file] where they
676can make use of subsections. On the other hand storing the plugin
677configuration in a 'plugin' subsection in the `project.config` file has
678the advantage that project owners have all configuration parameters in
679one file, instead of having one configuration file per plugin.
Edwin Kempinc9b68602013-10-30 09:32:43 +0100680
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200681To avoid conflicts with other plugins, it is recommended that plugins
682only use the `plugin` subsection with their own name. For example the
683`helloworld` plugin should store its configuration in the
684`plugin.helloworld` subsection:
685
686----
687 [plugin "helloworld"]
688 enabled = true
689----
690
691Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
692plugin can easily access its project specific configuration and there
693is no need for a plugin to parse the `project.config` file on its own:
694
695[source,java]
696----
David Pursehouse529ec252013-09-27 13:45:14 +0900697@Inject
698private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200699
David Pursehoused128c892013-10-22 21:52:21 +0900700[...]
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200701
Edwin Kempin122622d2013-10-29 16:45:44 +0100702boolean enabled = cfg.getFromProjectConfig(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900703 .getBoolean("enabled", false);
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200704----
705
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200706It is also possible to get missing configuration parameters inherited
707from the parent projects:
708
709[source,java]
710----
David Pursehouse529ec252013-09-27 13:45:14 +0900711@Inject
712private com.google.gerrit.server.config.PluginConfigFactory cfg;
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200713
David Pursehoused128c892013-10-22 21:52:21 +0900714[...]
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200715
Edwin Kempin122622d2013-10-29 16:45:44 +0100716boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld")
David Pursehouse529ec252013-09-27 13:45:14 +0900717 .getBoolean("enabled", false);
Edwin Kempinca7ad8e2013-09-16 16:43:05 +0200718----
719
Edwin Kempin7b2f4cc2013-08-26 15:44:19 +0200720Project owners can edit the project configuration by fetching the
721`refs/meta/config` branch, editing the `project.config` file and
722pushing the commit back.
723
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100724Plugin configuration values that are stored in the `project.config`
725file can be exposed in the ProjectInfoScreen to allow project owners
726to see and edit them from the UI.
727
728For this an instance of `ProjectConfigEntry` needs to be bound for each
729parameter. The export name must be a valid Git variable name. The
730variable name is case-insensitive, allows only alphanumeric characters
731and '-', and must start with an alphabetic character.
732
Edwin Kempina6c1c452013-11-28 16:55:22 +0100733The example below shows how the parameters `plugin.helloworld.enabled`
734and `plugin.helloworld.language` are bound to be editable from the
David Pursehousea1d633b2014-05-02 17:21:02 +0900735Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting"
Edwin Kempina6c1c452013-11-28 16:55:22 +0100736is provided as display name and the default value is set to `true`.
737For the parameter `plugin.helloworld.language` "Preferred Language"
738is provided as display name and "en" is set as default value.
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100739
740[source,java]
741----
742class Module extends AbstractModule {
743 @Override
744 protected void configure() {
745 bind(ProjectConfigEntry.class)
Edwin Kempina6c1c452013-11-28 16:55:22 +0100746 .annotatedWith(Exports.named("enabled"))
747 .toInstance(new ProjectConfigEntry("Enable Greeting", true));
748 bind(ProjectConfigEntry.class)
Edwin Kempin9ce4f552013-11-15 16:00:00 +0100749 .annotatedWith(Exports.named("language"))
750 .toInstance(new ProjectConfigEntry("Preferred Language", "en"));
751 }
752}
753----
754
Edwin Kempinb64d3972013-11-17 18:55:48 +0100755By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins
756can be notified when this configuration parameter is updated on a
757project.
758
Edwin Kempin705f2842013-10-30 14:25:31 +0100759[[project-specific-configuration]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800760== Project Specific Configuration in own config file
Edwin Kempin705f2842013-10-30 14:25:31 +0100761
762Plugins can store their project specific configuration in an own
763configuration file in the projects `refs/meta/config` branch.
764This makes sense if the plugins project specific configuration is
765rather complex and requires the usage of subsections. Plugins that
766have a simple key-value pair configuration can store their project
767specific configuration in a link:#simple-project-specific-configuration[
768`plugin` subsection of the `project.config` file].
769
770The plugin configuration file in the `refs/meta/config` branch must be
771named after the plugin. For example a configuration file for a
772`default-reviewer` plugin could look like this:
773
774.default-reviewer.config
775----
776[branch "refs/heads/master"]
777 reviewer = Project Owners
778 reviewer = john.doe@example.com
779[match "file:^.*\.txt"]
780 reviewer = My Info Developers
781----
782
783Via the `com.google.gerrit.server.config.PluginConfigFactory` class a
784plugin can easily access its project specific configuration:
785
786[source,java]
787----
788@Inject
789private com.google.gerrit.server.config.PluginConfigFactory cfg;
790
791[...]
792
793String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer")
794 .getStringList("branch", "refs/heads/master", "reviewer");
795----
796
Edwin Kempin762da382013-10-30 14:50:01 +0100797It is also possible to get missing configuration parameters inherited
798from the parent projects:
799
800[source,java]
801----
802@Inject
803private com.google.gerrit.server.config.PluginConfigFactory cfg;
804
805[...]
806
David Ostrovsky468e4c32014-03-22 06:05:35 -0700807String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer")
Edwin Kempin762da382013-10-30 14:50:01 +0100808 .getStringList("branch", "refs/heads/master", "reviewer");
809----
810
Edwin Kempin705f2842013-10-30 14:25:31 +0100811Project owners can edit the project configuration by fetching the
812`refs/meta/config` branch, editing the `<plugin-name>.config` file and
813pushing the commit back.
814
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800815== React on changes in project configuration
Edwin Kempina46b6c92013-12-04 21:05:24 +0100816
817If a plugin wants to react on changes in the project configuration, it
818can implement a `GitReferenceUpdatedListener` and filter on events for
819the `refs/meta/config` branch:
820
821[source,java]
822----
823public class MyListener implements GitReferenceUpdatedListener {
824
825 private final MetaDataUpdate.Server metaDataUpdateFactory;
826
827 @Inject
828 MyListener(MetaDataUpdate.Server metaDataUpdateFactory) {
829 this.metaDataUpdateFactory = metaDataUpdateFactory;
830 }
831
832 @Override
833 public void onGitReferenceUpdated(Event event) {
Edwin Kempina951ba52014-01-03 14:07:28 +0100834 if (event.getRefName().equals(RefNames.REFS_CONFIG)) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100835 Project.NameKey p = new Project.NameKey(event.getProjectName());
836 try {
Edwin Kempina951ba52014-01-03 14:07:28 +0100837 ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId());
838 ProjectConfig newCfg = parseConfig(p, event.getNewObjectId());
Edwin Kempina46b6c92013-12-04 21:05:24 +0100839
Edwin Kempina951ba52014-01-03 14:07:28 +0100840 if (oldCfg != null && newCfg != null
841 && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) {
Edwin Kempina46b6c92013-12-04 21:05:24 +0100842 // submit type has changed
843 ...
844 }
845 } catch (IOException | ConfigInvalidException e) {
846 ...
847 }
848 }
849 }
Edwin Kempina951ba52014-01-03 14:07:28 +0100850
851 private ProjectConfig parseConfig(Project.NameKey p, String idStr)
852 throws IOException, ConfigInvalidException, RepositoryNotFoundException {
853 ObjectId id = ObjectId.fromString(idStr);
854 if (ObjectId.zeroId().equals(id)) {
855 return null;
856 }
857 return ProjectConfig.read(metaDataUpdateFactory.create(p), id);
858 }
Edwin Kempina46b6c92013-12-04 21:05:24 +0100859}
860----
861
862
David Ostrovsky7066cc02013-06-15 14:46:23 +0200863[[capabilities]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800864== Plugin Owned Capabilities
David Ostrovsky7066cc02013-06-15 14:46:23 +0200865
866Plugins may provide their own capabilities and restrict usage of SSH
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200867commands or `UiAction` to the users who are granted those capabilities.
David Ostrovsky7066cc02013-06-15 14:46:23 +0200868
869Plugins define the capabilities by overriding the `CapabilityDefinition`
870abstract class:
871
David Pursehouse68153d72013-09-04 10:09:17 +0900872[source,java]
873----
874public class PrintHelloCapability extends CapabilityDefinition {
875 @Override
876 public String getDescription() {
877 return "Print Hello";
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
Dariusz Luksza112d93a2014-06-01 16:52:23 +0200882If no Guice modules are declared in the manifest, capability may
David Ostrovsky7066cc02013-06-15 14:46:23 +0200883use auto-registration by providing an `@Export` annotation:
884
David Pursehouse68153d72013-09-04 10:09:17 +0900885[source,java]
886----
887@Export("printHello")
888public class PrintHelloCapability extends CapabilityDefinition {
David Pursehoused128c892013-10-22 21:52:21 +0900889 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900890}
891----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200892
893Otherwise the capability must be bound in a plugin module:
894
David Pursehouse68153d72013-09-04 10:09:17 +0900895[source,java]
896----
897public class HelloWorldModule extends AbstractModule {
898 @Override
899 protected void configure() {
900 bind(CapabilityDefinition.class)
901 .annotatedWith(Exports.named("printHello"))
902 .to(PrintHelloCapability.class);
David Ostrovsky7066cc02013-06-15 14:46:23 +0200903 }
David Pursehouse68153d72013-09-04 10:09:17 +0900904}
905----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200906
907With a plugin-owned capability defined in this way, it is possible to restrict
David Ostrovskyf86bae52013-09-01 09:10:39 +0200908usage of an SSH command or `UiAction` to members of the group that were granted
David Ostrovsky7066cc02013-06-15 14:46:23 +0200909this capability in the usual way, using the `RequiresCapability` annotation:
910
David Pursehouse68153d72013-09-04 10:09:17 +0900911[source,java]
912----
913@RequiresCapability("printHello")
914@CommandMetaData(name="print", description="Print greeting in different languages")
915public final class PrintHelloWorldCommand extends SshCommand {
David Pursehoused128c892013-10-22 21:52:21 +0900916 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900917}
918----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200919
David Ostrovskyf86bae52013-09-01 09:10:39 +0200920Or with `UiAction`:
David Ostrovsky7066cc02013-06-15 14:46:23 +0200921
David Pursehouse68153d72013-09-04 10:09:17 +0900922[source,java]
923----
924@RequiresCapability("printHello")
925public class SayHelloAction extends UiAction<RevisionResource>
926 implements RestModifyView<RevisionResource, SayHelloAction.Input> {
David Pursehoused128c892013-10-22 21:52:21 +0900927 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900928}
929----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200930
931Capability scope was introduced to differentiate between plugin-owned
David Pursehousebf053342013-09-05 14:55:29 +0900932capabilities and core capabilities. Per default the scope of the
933`@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means:
934
David Ostrovsky7066cc02013-06-15 14:46:23 +0200935* when `@RequiresCapability` is used within a plugin the scope of the
936capability is assumed to be that plugin.
David Pursehousebf053342013-09-05 14:55:29 +0900937
David Ostrovsky7066cc02013-06-15 14:46:23 +0200938* If `@RequiresCapability` is used within the core Gerrit Code Review server
939(and thus is outside of a plugin) the scope is the core server and will use
940the `GlobalCapability` known to Gerrit Code Review server.
941
942If a plugin needs to use a core capability name (e.g. "administrateServer")
943this can be specified by setting `scope = CapabilityScope.CORE`:
944
David Pursehouse68153d72013-09-04 10:09:17 +0900945[source,java]
946----
947@RequiresCapability(value = "administrateServer", scope =
948 CapabilityScope.CORE)
David Pursehoused128c892013-10-22 21:52:21 +0900949 [...]
David Pursehouse68153d72013-09-04 10:09:17 +0900950----
David Ostrovsky7066cc02013-06-15 14:46:23 +0200951
David Ostrovskyf86bae52013-09-01 09:10:39 +0200952[[ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -0800953== UI Extension
David Ostrovskyf86bae52013-09-01 09:10:39 +0200954
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100955Plugins can contribute UI actions on core Gerrit pages. This is useful
956for workflow customization or exposing plugin functionality through the
957UI in addition to SSH commands and the REST API.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200958
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100959For instance a plugin to integrate Jira with Gerrit changes may
960contribute a "File bug" button to allow filing a bug from the change
961page or plugins to integrate continuous integration systems may
962contribute a "Schedule" button to allow a CI build to be scheduled
963manually from the patch set panel.
David Ostrovskyf86bae52013-09-01 09:10:39 +0200964
Edwin Kempin7afa73c2013-11-08 07:48:47 +0100965Two different places on core Gerrit pages are supported:
David Ostrovskyf86bae52013-09-01 09:10:39 +0200966
967* Change screen
968* Project info screen
969
970Plugins contribute UI actions by implementing the `UiAction` interface:
971
David Pursehouse68153d72013-09-04 10:09:17 +0900972[source,java]
973----
974@RequiresCapability("printHello")
975class HelloWorldAction implements UiAction<RevisionResource>,
976 RestModifyView<RevisionResource, HelloWorldAction.Input> {
977 static class Input {
978 boolean french;
979 String message;
David Ostrovskyf86bae52013-09-01 09:10:39 +0200980 }
David Pursehouse68153d72013-09-04 10:09:17 +0900981
982 private Provider<CurrentUser> user;
983
984 @Inject
985 HelloWorldAction(Provider<CurrentUser> user) {
986 this.user = user;
987 }
988
989 @Override
990 public String apply(RevisionResource rev, Input input) {
991 final String greeting = input.french
992 ? "Bonjour"
993 : "Hello";
994 return String.format("%s %s from change %s, patch set %d!",
995 greeting,
996 Strings.isNullOrEmpty(input.message)
997 ? Objects.firstNonNull(user.get().getUserName(), "world")
998 : input.message,
999 rev.getChange().getId().toString(),
1000 rev.getPatchSet().getPatchSetId());
1001 }
1002
1003 @Override
1004 public Description getDescription(
1005 RevisionResource resource) {
1006 return new Description()
1007 .setLabel("Say hello")
1008 .setTitle("Say hello in different languages");
1009 }
1010}
1011----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001012
David Ostrovsky450eefe2013-10-21 21:18:11 +02001013Sometimes plugins may want to be able to change the state of a patch set or
1014change in the `UiAction.apply()` method and reflect these changes on the core
1015UI. For example a buildbot plugin which exposes a 'Schedule' button on the
1016patch set panel may want to disable that button after the build was scheduled
1017and update the tooltip of that button. But because of Gerrit's caching
1018strategy the following must be taken into consideration.
1019
1020The browser is allowed to cache the `UiAction` information until something on
1021the change is modified. More accurately the change row needs to be modified in
1022the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or
1023the +refs/meta/config+ of the project or any parents needs to change to a new
1024SHA-1. The ETag SHA-1 computation code can be found in the
1025`ChangeResource.getETag()` method.
1026
David Pursehoused128c892013-10-22 21:52:21 +09001027The easiest way to accomplish this is to update `lastUpdatedOn` of the change:
David Ostrovsky450eefe2013-10-21 21:18:11 +02001028
1029[source,java]
1030----
1031@Override
1032public Object apply(RevisionResource rcrs, Input in) {
1033 // schedule a build
1034 [...]
1035 // update change
1036 ReviewDb db = dbProvider.get();
1037 db.changes().beginTransaction(change.getId());
1038 try {
1039 change = db.changes().atomicUpdate(
1040 change.getId(),
1041 new AtomicUpdate<Change>() {
1042 @Override
1043 public Change update(Change change) {
1044 ChangeUtil.updated(change);
1045 return change;
1046 }
1047 });
1048 db.commit();
1049 } finally {
1050 db.rollback();
1051 }
David Pursehoused128c892013-10-22 21:52:21 +09001052 [...]
David Ostrovsky450eefe2013-10-21 21:18:11 +02001053}
1054----
1055
David Ostrovskyf86bae52013-09-01 09:10:39 +02001056`UiAction` must be bound in a plugin module:
1057
David Pursehouse68153d72013-09-04 10:09:17 +09001058[source,java]
1059----
1060public class Module extends AbstractModule {
1061 @Override
1062 protected void configure() {
1063 install(new RestApiModule() {
1064 @Override
1065 protected void configure() {
1066 post(REVISION_KIND, "say-hello")
1067 .to(HelloWorldAction.class);
1068 }
1069 });
David Ostrovskyf86bae52013-09-01 09:10:39 +02001070 }
David Pursehouse68153d72013-09-04 10:09:17 +09001071}
1072----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001073
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001074The module above must be declared in the `pom.xml` for Maven driven
1075plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001076
David Pursehouse68153d72013-09-04 10:09:17 +09001077[source,xml]
1078----
1079<manifestEntries>
1080 <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module>
1081</manifestEntries>
1082----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001083
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001084or in the `BUCK` configuration file for Buck driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001085
David Pursehouse68153d72013-09-04 10:09:17 +09001086[source,python]
1087----
1088manifest_entries = [
1089 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module',
1090]
1091----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001092
1093In some use cases more user input must be gathered, for that `UiAction` can be
1094combined with the JavaScript API. This would display a small popup near the
1095activation button to gather additional input from the user. The JS file is
1096typically put in the `static` folder within the plugin's directory:
1097
David Pursehouse68153d72013-09-04 10:09:17 +09001098[source,javascript]
1099----
1100Gerrit.install(function(self) {
1101 function onSayHello(c) {
1102 var f = c.textfield();
1103 var t = c.checkbox();
1104 var b = c.button('Say hello', {onclick: function(){
1105 c.call(
1106 {message: f.value, french: t.checked},
1107 function(r) {
1108 c.hide();
1109 window.alert(r);
1110 c.refresh();
1111 });
1112 }});
1113 c.popup(c.div(
1114 c.prependLabel('Greeting message', f),
1115 c.br(),
1116 c.label(t, 'french'),
1117 c.br(),
1118 b));
1119 f.focus();
1120 }
1121 self.onAction('revision', 'say-hello', onSayHello);
1122});
1123----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001124
1125The JS module must be exposed as a `WebUiPlugin` and bound as
1126an HTTP Module:
1127
David Pursehouse68153d72013-09-04 10:09:17 +09001128[source,java]
1129----
1130public class HttpModule extends HttpPluginModule {
1131 @Override
1132 protected void configureServlets() {
1133 DynamicSet.bind(binder(), WebUiPlugin.class)
1134 .toInstance(new JavaScriptPlugin("hello.js"));
David Ostrovskyf86bae52013-09-01 09:10:39 +02001135 }
David Pursehouse68153d72013-09-04 10:09:17 +09001136}
1137----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001138
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001139The HTTP module above must be declared in the `pom.xml` for Maven
1140driven plugins:
David Ostrovskyf86bae52013-09-01 09:10:39 +02001141
David Pursehouse68153d72013-09-04 10:09:17 +09001142[source,xml]
1143----
1144<manifestEntries>
1145 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule>
1146</manifestEntries>
1147----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001148
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001149or in the `BUCK` configuration file for Buck driven plugins
David Ostrovskyf86bae52013-09-01 09:10:39 +02001150
David Pursehouse68153d72013-09-04 10:09:17 +09001151[source,python]
1152----
1153manifest_entries = [
1154 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule',
1155]
1156----
David Ostrovskyf86bae52013-09-01 09:10:39 +02001157
1158If `UiAction` is annotated with the `@RequiresCapability` annotation, then the
1159capability check is done during the `UiAction` gathering, so the plugin author
1160doesn't have to set `UiAction.Description.setVisible()` explicitly in this
1161case.
1162
1163The following prerequisities must be met, to satisfy the capability check:
1164
1165* user is authenticated
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001166* user is a member of a group which has the `Administrate Server` capability, or
David Ostrovskyf86bae52013-09-01 09:10:39 +02001167* user is a member of a group which has the required capability
1168
1169The `apply` method is called when the button is clicked. If `UiAction` is
1170combined with JavaScript API (its own JavaScript function is provided),
1171then a popup dialog is normally opened to gather additional user input.
1172A new button is placed on the popup dialog to actually send the request.
1173
1174Every `UiAction` exposes a REST API endpoint. The endpoint from the example above
1175can be accessed from any REST client, i. e.:
1176
1177====
1178 curl -X POST -H "Content-Type: application/json" \
1179 -d '{message: "François", french: true}' \
1180 --digest --user joe:secret \
1181 http://host:port/a/changes/1/revisions/1/cookbook~say-hello
1182 "Bonjour François from change 1, patch set 1!"
1183====
1184
David Pursehouse42245822013-09-24 09:48:20 +09001185A special case is to bind an endpoint without a view name. This is
Edwin Kempin7afa73c2013-11-08 07:48:47 +01001186particularly useful for `DELETE` requests:
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001187
1188[source,java]
1189----
1190public class Module extends AbstractModule {
1191 @Override
1192 protected void configure() {
1193 install(new RestApiModule() {
1194 @Override
1195 protected void configure() {
1196 delete(PROJECT_KIND)
1197 .to(DeleteProject.class);
1198 }
1199 });
1200 }
1201}
1202----
1203
David Pursehouse42245822013-09-24 09:48:20 +09001204For a `UiAction` bound this way, a JS API function can be provided.
1205
1206Currently only one restriction exists: per plugin only one `UiAction`
David Ostrovskyc6d19ed2013-09-20 21:30:18 +02001207can be bound per resource without view name. To define a JS function
1208for the `UiAction`, "/" must be used as the name:
1209
1210[source,javascript]
1211----
1212Gerrit.install(function(self) {
1213 function onDeleteProject(c) {
1214 [...]
1215 }
1216 self.onAction('project', '/', onDeleteProject);
1217});
1218----
1219
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001220[[top-menu-extensions]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001221== Top Menu Extensions
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001222
1223Plugins can contribute items to Gerrit's top menu.
1224
1225A single top menu extension can have multiple elements and will be put as
1226the last element in Gerrit's top menu.
1227
1228Plugins define the top menu entries by implementing `TopMenu` interface:
1229
1230[source,java]
1231----
1232public class MyTopMenuExtension implements TopMenu {
1233
1234 @Override
1235 public List<MenuEntry> getEntries() {
1236 return Lists.newArrayList(
1237 new MenuEntry("Top Menu Entry", Lists.newArrayList(
1238 new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1239 }
1240}
1241----
1242
Edwin Kempin77f23242013-09-30 14:53:20 +02001243Plugins can also add additional menu items to Gerrit's top menu entries
1244by defining a `MenuEntry` that has the same name as a Gerrit top menu
1245entry:
1246
1247[source,java]
1248----
1249public class MyTopMenuExtension implements TopMenu {
1250
1251 @Override
1252 public List<MenuEntry> getEntries() {
1253 return Lists.newArrayList(
Dariusz Luksza2d3afab2013-10-01 11:07:13 +02001254 new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList(
Edwin Kempin77f23242013-09-30 14:53:20 +02001255 new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/"))));
1256 }
1257}
1258----
1259
Dariusz Lukszae8de74f2014-06-04 19:39:20 +02001260`MenuItems` that are bound for the `MenuEntry` with the name
1261`GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder
1262which is automatically replaced by the actual project name.
1263
1264E.g. plugins may register an link:#http[HTTP Servlet] to handle project
1265specific requests and add an menu item for this:
1266
1267[source,java]
1268---
1269 new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}");
1270---
1271
1272This also enables plugins to provide menu items for project aware
1273screens:
1274
1275[source,java]
1276---
1277 new MenuItem("My Screen", "/x/my-screen/for/${projectName}");
1278---
1279
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001280If no Guice modules are declared in the manifest, the top menu extension may use
1281auto-registration by providing an `@Listen` annotation:
1282
1283[source,java]
1284----
1285@Listen
1286public class MyTopMenuExtension implements TopMenu {
David Pursehoused128c892013-10-22 21:52:21 +09001287 [...]
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001288}
1289----
1290
Luca Milanesiocb230402013-10-11 08:49:56 +01001291Otherwise the top menu extension must be bound in the plugin module used
1292for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF):
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001293
1294[source,java]
1295----
Luca Milanesiocb230402013-10-11 08:49:56 +01001296package com.googlesource.gerrit.plugins.helloworld;
1297
Dariusz Luksza589ba00aa2013-05-07 17:21:23 +02001298public class HelloWorldModule extends AbstractModule {
1299 @Override
1300 protected void configure() {
1301 DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class);
1302 }
1303}
1304----
1305
Luca Milanesiocb230402013-10-11 08:49:56 +01001306[source,manifest]
1307----
1308Gerrit-ApiType: plugin
1309Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule
1310----
1311
Edwin Kempinb2e926a2013-11-11 16:38:30 +01001312It is also possible to show some menu entries only if the user has a
1313certain capability:
1314
1315[source,java]
1316----
1317public class MyTopMenuExtension implements TopMenu {
1318 private final String pluginName;
1319 private final Provider<CurrentUser> userProvider;
1320 private final List<MenuEntry> menuEntries;
1321
1322 @Inject
1323 public MyTopMenuExtension(@PluginName String pluginName,
1324 Provider<CurrentUser> userProvider) {
1325 this.pluginName = pluginName;
1326 this.userProvider = userProvider;
1327 menuEntries = new ArrayList<TopMenu.MenuEntry>();
1328
1329 // add menu entry that is only visible to users with a certain capability
1330 if (canSeeMenuEntry()) {
1331 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1332 .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/"))));
1333 }
1334
1335 // add menu entry that is visible to all users (even anonymous users)
1336 menuEntries.add(new MenuEntry("Top Menu Entry", Collections
1337 .singletonList(new MenuItem("Documentation", "/plugins/myplugin/"))));
1338 }
1339
1340 private boolean canSeeMenuEntry() {
1341 if (userProvider.get().isIdentifiedUser()) {
1342 CapabilityControl ctl = userProvider.get().getCapabilities();
1343 return ctl.canPerform(pluginName + "-" + MyCapability.ID)
1344 || ctl.canAdministrateServer();
1345 } else {
1346 return false;
1347 }
1348 }
1349
1350 @Override
1351 public List<MenuEntry> getEntries() {
1352 return menuEntries;
1353 }
1354}
1355----
1356
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001357[[gwt_ui_extension]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001358== GWT UI Extension
Edwin Kempin3c024ea2013-11-11 10:43:46 +01001359Plugins can extend the Gerrit UI with own GWT code.
1360
1361The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to
1362generate a GWT plugin skeleton. How to use the Maven plugin archetypes
1363is described in the link:#getting-started[Getting started] section.
1364
1365The generated GWT plugin has a link:#top-menu-extensions[top menu] that
1366opens a GWT dialog box when the user clicks on it.
1367
Edwin Kempinb74daa92013-11-11 11:28:16 +01001368In addition to the Gerrit-Plugin API a GWT plugin depends on
1369`gerrit-plugin-gwtui`. This dependency must be specified in the
1370`pom.xml`:
1371
1372[source,xml]
1373----
1374<dependency>
1375 <groupId>com.google.gerrit</groupId>
1376 <artifactId>gerrit-plugin-gwtui</artifactId>
1377 <version>${Gerrit-ApiVersion}</version>
1378</dependency>
1379----
1380
1381A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`,
1382that bundles together all the configuration settings of the GWT plugin:
1383
1384[source,xml]
1385----
1386<?xml version="1.0" encoding="UTF-8"?>
1387<module rename-to="hello_gwt_plugin">
1388 <!-- Inherit the core Web Toolkit stuff. -->
1389 <inherits name="com.google.gwt.user.User"/>
1390 <!-- Other module inherits -->
1391 <inherits name="com.google.gerrit.Plugin"/>
1392 <inherits name="com.google.gwt.http.HTTP"/>
1393 <!-- Using GWT built-in themes adds a number of static -->
1394 <!-- resources to the plugin. No theme inherits lines were -->
1395 <!-- added in order to make this plugin as simple as possible -->
1396 <!-- Specify the app entry point class. -->
1397 <entry-point class="${package}.client.HelloPlugin"/>
1398 <stylesheet src="hello.css"/>
1399</module>
1400----
1401
1402The GWT module must inherit `com.google.gerrit.Plugin` and
1403`com.google.gwt.http.HTTP`.
1404
1405To register the GWT module a `GwtPlugin` needs to be bound.
1406
1407If no Guice modules are declared in the manifest, the GWT plugin may
1408use auto-registration by using the `@Listen` annotation:
1409
1410[source,java]
1411----
1412@Listen
1413public class MyExtension extends GwtPlugin {
1414 public MyExtension() {
1415 super("hello_gwt_plugin");
1416 }
1417}
1418----
1419
1420Otherwise the binding must be done in an `HttpModule`:
1421
1422[source,java]
1423----
1424public class HttpModule extends HttpPluginModule {
1425
1426 @Override
1427 protected void configureServlets() {
1428 DynamicSet.bind(binder(), WebUiPlugin.class)
1429 .toInstance(new GwtPlugin("hello_gwt_plugin"));
1430 }
1431}
1432----
1433
1434The HTTP module above must be declared in the `pom.xml` for Maven
1435driven plugins:
1436
1437[source,xml]
1438----
1439<manifestEntries>
1440 <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule>
1441</manifestEntries>
1442----
1443
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001444The name that is provided to the `GwtPlugin` must match the GWT
1445module name compiled into the plugin. The name of the GWT module
1446can be explicitly set in the GWT module XML file by specifying
1447the `rename-to` attribute on the module. It is important that the
1448module name be unique across all plugins installed on the server,
1449as the module name determines the JavaScript namespace used by the
1450compiled plugin code.
Edwin Kempinb74daa92013-11-11 11:28:16 +01001451
1452[source,xml]
1453----
1454<module rename-to="hello_gwt_plugin">
1455----
1456
1457The actual GWT code must be implemented in a class that extends
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001458`com.google.gerrit.plugin.client.PluginEntryPoint`:
Edwin Kempinb74daa92013-11-11 11:28:16 +01001459
1460[source,java]
1461----
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001462public class HelloPlugin extends PluginEntryPoint {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001463
1464 @Override
Shawn Pearcec8e96ad2013-12-09 08:20:44 -08001465 public void onPluginLoad() {
Edwin Kempinb74daa92013-11-11 11:28:16 +01001466 // Create the dialog box
1467 final DialogBox dialogBox = new DialogBox();
1468
1469 // The content of the dialog comes from a User specified Preference
1470 dialogBox.setText("Hello from GWT Gerrit UI plugin");
1471 dialogBox.setAnimationEnabled(true);
1472 Button closeButton = new Button("Close");
1473 VerticalPanel dialogVPanel = new VerticalPanel();
1474 dialogVPanel.setWidth("100%");
1475 dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
1476 dialogVPanel.add(closeButton);
1477
1478 closeButton.addClickHandler(new ClickHandler() {
1479 public void onClick(ClickEvent event) {
1480 dialogBox.hide();
1481 }
1482 });
1483
1484 // Set the contents of the Widget
1485 dialogBox.setWidget(dialogVPanel);
1486
1487 RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1488 rootPanel.getElement().removeAttribute("href");
1489 rootPanel.addDomHandler(new ClickHandler() {
1490 @Override
1491 public void onClick(ClickEvent event) {
1492 dialogBox.center();
1493 dialogBox.show();
1494 }
1495 }, ClickEvent.getType());
1496 }
1497}
1498----
1499
1500This class must be set as entry point in the GWT module:
1501
1502[source,xml]
1503----
1504<entry-point class="${package}.client.HelloPlugin"/>
1505----
1506
1507In addition this class must be defined as module in the `pom.xml` for the
1508`gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin`
1509must be set to `${project.build.directory}/classes/static`:
1510
1511[source,xml]
1512----
1513<plugin>
1514 <groupId>org.codehaus.mojo</groupId>
1515 <artifactId>gwt-maven-plugin</artifactId>
1516 <version>2.5.1</version>
1517 <configuration>
1518 <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module>
1519 <disableClassMetadata>true</disableClassMetadata>
1520 <disableCastChecking>true</disableCastChecking>
1521 <webappDirectory>${project.build.directory}/classes/static</webappDirectory>
1522 </configuration>
1523 <executions>
1524 <execution>
1525 <goals>
1526 <goal>compile</goal>
1527 </goals>
1528 </execution>
1529 </executions>
1530</plugin>
1531----
1532
1533To attach a GWT widget defined by the plugin to the Gerrit core UI
1534`com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the
1535Gerrit core widgets:
1536
1537[source,java]
1538----
1539RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID);
1540rootPanel.getElement().removeAttribute("href");
1541rootPanel.addDomHandler(new ClickHandler() {
1542 @Override
1543 public void onClick(ClickEvent event) {
1544 dialogBox.center();
1545 dialogBox.show();
1546 }
1547}, ClickEvent.getType());
1548----
1549
1550GWT plugins can come with their own css file. This css file must have a
1551unique name and must be registered in the GWT module:
1552
1553[source,xml]
1554----
1555<stylesheet src="hello.css"/>
1556----
1557
Edwin Kempin2570b102013-11-11 11:44:50 +01001558If a GWT plugin wants to invoke the Gerrit REST API it can use
David Pursehouse3a388312014-02-25 16:41:47 +09001559`com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL
Edwin Kempin2570b102013-11-11 11:44:50 +01001560path and to trigger the REST calls.
1561
1562Example for invoking a Gerrit core REST endpoint:
1563
1564[source,java]
1565----
1566new RestApi("projects").id(projectName).view("description")
1567 .put("new description", new AsyncCallback<JavaScriptObject>() {
1568
1569 @Override
1570 public void onSuccess(JavaScriptObject result) {
1571 // TODO
1572 }
1573
1574 @Override
1575 public void onFailure(Throwable caught) {
1576 // never invoked
1577 }
1578});
1579----
1580
1581Example for invoking a REST endpoint defined by a plugin:
1582
1583[source,java]
1584----
1585new RestApi("projects").id(projectName).view("myplugin", "myview")
1586 .get(new AsyncCallback<JavaScriptObject>() {
1587
1588 @Override
1589 public void onSuccess(JavaScriptObject result) {
1590 // TODO
1591 }
1592
1593 @Override
1594 public void onFailure(Throwable caught) {
1595 // never invoked
1596 }
1597});
1598----
1599
1600The `onFailure(Throwable)` of the provided callback is never invoked.
1601If an error occurs, it is shown in an error dialog.
1602
1603In order to be able to do REST calls the GWT module must inherit
1604`com.google.gwt.json.JSON`:
1605
1606[source,xml]
1607----
1608<inherits name="com.google.gwt.json.JSON"/>
1609----
1610
Edwin Kempin15199792014-04-23 16:22:05 +02001611[[screen]]
Shawn Pearced5c844f2013-12-26 15:32:26 -08001612== Add Screen
Edwin Kempin15199792014-04-23 16:22:05 +02001613A link:#gwt_ui_extension[GWT plugin] can link:#top-menu-extensions[add
1614a menu item] that opens a screen that is implemented by the plugin.
1615This way plugin screens can be fully integrated into the Gerrit UI.
Shawn Pearced5c844f2013-12-26 15:32:26 -08001616
1617Example menu item:
1618[source,java]
1619----
1620public class MyMenu implements TopMenu {
1621 private final List<MenuEntry> menuEntries;
1622
1623 @Inject
1624 public MyMenu(@PluginName String name) {
1625 menuEntries = Lists.newArrayList();
1626 menuEntries.add(new MenuEntry("My Menu", Collections.singletonList(
1627 new MenuItem("My Screen", "#/x/" + name + "/my-screen", ""))));
1628 }
1629
1630 @Override
1631 public List<MenuEntry> getEntries() {
1632 return menuEntries;
1633 }
1634}
1635----
1636
1637Example screen:
1638[source,java]
1639----
1640public class MyPlugin extends PluginEntryPoint {
1641 @Override
1642 public void onPluginLoad() {
1643 Plugin.get().screen("my-screen", new Screen.EntryPoint() {
1644 @Override
1645 public void onLoad(Screen screen) {
1646 screen.add(new InlineLabel("My Screen");
1647 screen.show();
1648 }
1649 });
1650 }
1651}
1652----
1653
Edwin Kempin289f1a02014-02-04 16:08:25 +01001654[[settings-screen]]
1655== Plugin Settings Screen
1656
1657If a plugin implements a screen for administrating its settings that is
1658available under "#/x/<plugin-name>/settings" it is automatically linked
1659from the plugin list screen.
1660
Edwin Kempinf5a77332012-07-18 11:17:53 +02001661[[http]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001662== HTTP Servlets
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001663
1664Plugins or extensions may register additional HTTP servlets, and
1665wrap them with HTTP filters.
1666
1667Servlets may use auto-registration to declare the URL they handle:
1668
David Pursehouse68153d72013-09-04 10:09:17 +09001669[source,java]
1670----
1671import com.google.gerrit.extensions.annotations.Export;
1672import com.google.inject.Singleton;
1673import javax.servlet.http.HttpServlet;
1674import javax.servlet.http.HttpServletRequest;
1675import javax.servlet.http.HttpServletResponse;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001676
David Pursehouse68153d72013-09-04 10:09:17 +09001677@Export("/print")
1678@Singleton
1679class HelloServlet extends HttpServlet {
1680 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
1681 res.setContentType("text/plain");
1682 res.setCharacterEncoding("UTF-8");
1683 res.getWriter().write("Hello");
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001684 }
David Pursehouse68153d72013-09-04 10:09:17 +09001685}
1686----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001687
Edwin Kempin8aa650f2012-07-18 11:25:48 +02001688The auto registration only works for standard servlet mappings like
1689`/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule
1690to register the HTTP servlets and declare it explicitly in the manifest
1691with the `Gerrit-HttpModule` attribute:
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001692
David Pursehouse68153d72013-09-04 10:09:17 +09001693[source,java]
1694----
1695import com.google.inject.servlet.ServletModule;
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001696
David Pursehouse68153d72013-09-04 10:09:17 +09001697class MyWebUrls extends ServletModule {
1698 protected void configureServlets() {
1699 serve("/print").with(HelloServlet.class);
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001700 }
David Pursehouse68153d72013-09-04 10:09:17 +09001701}
1702----
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001703
1704For a plugin installed as name `helloworld`, the servlet implemented
1705by HelloServlet class will be available to users as:
1706
1707----
1708$ curl http://review.example.com/plugins/helloworld/print
1709----
Nasser Grainawie033b262012-05-09 17:54:21 -07001710
Edwin Kempinf5a77332012-07-18 11:17:53 +02001711[[data-directory]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001712== Data Directory
Edwin Kempin41f63912012-07-17 12:33:55 +02001713
1714Plugins can request a data directory with a `@PluginData` File
1715dependency. A data directory will be created automatically by the
1716server in `$site_path/data/$plugin_name` and passed to the plugin.
1717
1718Plugins can use this to store any data they want.
1719
David Pursehouse68153d72013-09-04 10:09:17 +09001720[source,java]
1721----
1722@Inject
1723MyType(@PluginData java.io.File myDir) {
1724 new FileInputStream(new File(myDir, "my.config"));
1725}
1726----
Edwin Kempin41f63912012-07-17 12:33:55 +02001727
Edwin Kempinea621482013-10-16 12:58:24 +02001728[[download-commands]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001729== Download Commands
Edwin Kempinea621482013-10-16 12:58:24 +02001730
1731Gerrit offers commands for downloading changes using different
1732download schemes (e.g. for downloading via different network
1733protocols). Plugins can contribute download schemes and download
1734commands by implementing
1735`com.google.gerrit.extensions.config.DownloadScheme` and
1736`com.google.gerrit.extensions.config.DownloadCommand`.
1737
1738The download schemes and download commands which are used most often
1739are provided by the Gerrit core plugin `download-commands`.
1740
Sven Selbergae1a10c2014-02-14 14:24:29 +01001741[[links-to-external-tools]]
1742== Links To External Tools
1743
1744Gerrit has extension points that enables development of a
1745light-weight plugin that links commits to external
1746tools (GitBlit, CGit, company specific resources etc).
1747
1748PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI.
1749
1750[source, java]
1751----
1752import com.google.gerrit.extensions.annotations.Listen;
1753import com.google.gerrit.extensions.webui.PatchSetWebLink;;
Sven Selberga85e64d2014-09-24 10:52:21 +02001754import com.google.gerrit.extensions.webui.WebLinkTarget;
Sven Selbergae1a10c2014-02-14 14:24:29 +01001755
1756@Listen
1757public class MyWeblinkPlugin implements PatchSetWebLink {
1758
1759 private String name = "MyLink";
1760 private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s";
Sven Selberg55484202014-06-26 08:48:51 +02001761 private String imageUrl = "http://placehold.it/16x16.gif";
Sven Selbergae1a10c2014-02-14 14:24:29 +01001762
1763 @Override
Sven Selberga85e64d2014-09-24 10:52:21 +02001764 public WebLinkInfo getPathSetWebLink(String projectName, String commit) {
1765 return new WebLinkInfo(name,
1766 imageUrl,
1767 String.format(placeHolderUrlProjectCommit, project, commit),
1768 WebLinkTarget.BLANK);
Edwin Kempinceeed6b2014-09-11 17:07:33 +02001769 }
Sven Selbergae1a10c2014-02-14 14:24:29 +01001770}
1771----
1772
Edwin Kempinb3696c82014-09-11 09:41:42 +02001773FileWebLinks will appear in the side-by-side diff screen on the right
1774side of the patch selection on each side.
1775
Edwin Kempin8cdce502014-12-06 10:55:38 +01001776DiffWebLinks will appear in the side-by-side and unified diff screen in
1777the header next to the navigation icons.
1778
Edwin Kempinea004752014-04-11 15:56:02 +02001779ProjectWebLinks will appear in the project list in the
1780`Repository Browser` column.
1781
Edwin Kempin0f697bd2014-09-10 18:23:29 +02001782BranchWebLinks will appear in the branch list in the last column.
1783
Edwin Kempinf5a77332012-07-18 11:17:53 +02001784[[documentation]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001785== Documentation
Nasser Grainawie033b262012-05-09 17:54:21 -07001786
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001787If a plugin does not register a filter or servlet to handle URLs
1788`/Documentation/*` or `/static/*`, the core Gerrit server will
1789automatically export these resources over HTTP from the plugin JAR.
1790
David Pursehouse6853b5a2013-07-10 11:38:03 +09001791Static resources under the `static/` directory in the JAR will be
Dave Borowitzb893ac82013-03-27 10:03:55 -04001792available as `/plugins/helloworld/static/resource`. This prefix is
1793configurable by setting the `Gerrit-HttpStaticPrefix` attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001794
David Pursehouse6853b5a2013-07-10 11:38:03 +09001795Documentation files under the `Documentation/` directory in the JAR
Dave Borowitzb893ac82013-03-27 10:03:55 -04001796will be available as `/plugins/helloworld/Documentation/resource`. This
1797prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix`
1798attribute.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001799
1800Documentation may be written in
1801link:http://daringfireball.net/projects/markdown/[Markdown] style
1802if the file name ends with `.md`. Gerrit will automatically convert
1803Markdown to HTML if accessed with extension `.html`.
Nasser Grainawie033b262012-05-09 17:54:21 -07001804
Edwin Kempinf5a77332012-07-18 11:17:53 +02001805[[macros]]
Edwin Kempinc78777d2012-07-16 15:55:11 +02001806Within the Markdown documentation files macros can be used that allow
1807to write documentation with reasonably accurate examples that adjust
1808automatically based on the installation.
1809
1810The following macros are supported:
1811
1812[width="40%",options="header"]
1813|===================================================
1814|Macro | Replacement
1815|@PLUGIN@ | name of the plugin
1816|@URL@ | Gerrit Web URL
1817|@SSH_HOST@ | SSH Host
1818|@SSH_PORT@ | SSH Port
1819|===================================================
1820
1821The macros will be replaced when the documentation files are rendered
1822from Markdown to HTML.
1823
1824Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@`
1825even if there is an expansion for `KEEP` in the future.
1826
Edwin Kempinf5a77332012-07-18 11:17:53 +02001827[[auto-index]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001828=== Automatic Index
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001829
1830If a plugin does not handle its `/` URL itself, Gerrit will
1831redirect clients to the plugin's `/Documentation/index.html`.
1832Requests for `/Documentation/` (bare directory) will also redirect
1833to `/Documentation/index.html`.
1834
1835If neither resource `Documentation/index.html` or
1836`Documentation/index.md` exists in the plugin JAR, Gerrit will
1837automatically generate an index page for the plugin's documentation
1838tree by scanning every `*.md` and `*.html` file in the Documentation/
1839directory.
1840
1841For any discovered Markdown (`*.md`) file, Gerrit will parse the
1842header of the file and extract the first level one title. This
1843title text will be used as display text for a link to the HTML
1844version of the page.
1845
1846For any discovered HTML (`*.html`) file, Gerrit will use the name
1847of the file, minus the `*.html` extension, as the link text. Any
1848hyphens in the file name will be replaced with spaces.
1849
David Pursehouse6853b5a2013-07-10 11:38:03 +09001850If a discovered file is named `about.md` or `about.html`, its
1851content will be inserted in an 'About' section at the top of the
1852auto-generated index page. If both `about.md` and `about.html`
1853exist, only the first discovered file will be used.
1854
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001855If a discovered file name beings with `cmd-` it will be clustered
David Pursehouse6853b5a2013-07-10 11:38:03 +09001856into a 'Commands' section of the generated index page.
1857
David Pursehousefe529152013-08-14 16:35:06 +09001858If a discovered file name beings with `servlet-` it will be clustered
1859into a 'Servlets' section of the generated index page.
1860
1861If a discovered file name beings with `rest-api-` it will be clustered
1862into a 'REST APIs' section of the generated index page.
1863
David Pursehouse6853b5a2013-07-10 11:38:03 +09001864All other files are clustered under a 'Documentation' section.
Shawn O. Pearce795167c2012-05-12 11:20:18 -07001865
1866Some optional information from the manifest is extracted and
1867displayed as part of the index page, if present in the manifest:
1868
1869[width="40%",options="header"]
1870|===================================================
1871|Field | Source Attribute
1872|Name | Implementation-Title
1873|Vendor | Implementation-Vendor
1874|Version | Implementation-Version
1875|URL | Implementation-URL
1876|API Version | Gerrit-ApiVersion
1877|===================================================
1878
Edwin Kempinf5a77332012-07-18 11:17:53 +02001879[[deployment]]
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001880== Deployment
Nasser Grainawie033b262012-05-09 17:54:21 -07001881
Edwin Kempinf7295742012-07-16 15:03:46 +02001882Compiled plugins and extensions can be deployed to a running Gerrit
1883server using the link:cmd-plugin-install.html[plugin install] command.
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001884
David Pursehousea1d633b2014-05-02 17:21:02 +09001885Web UI plugins distributed as single `.js` file can be deployed
Dariusz Luksza357a2422012-11-12 06:16:26 +01001886without the overhead of JAR packaging, for more information refer to
1887link:cmd-plugin-install.html[plugin install] command.
1888
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001889Plugins can also be copied directly into the server's
Dariusz Luksza357a2422012-11-12 06:16:26 +01001890directory at `$site_path/plugins/$name.(jar|js)`. The name of
1891the JAR file, minus the `.jar` or `.js` extension, will be used as the
Shawn O. Pearceda4919a2012-05-10 16:54:28 -07001892plugin name. Unless disabled, servers periodically scan this
1893directory for updated plugins. The time can be adjusted by
1894link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency].
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001895
Edwin Kempinf7295742012-07-16 15:03:46 +02001896For disabling plugins the link:cmd-plugin-remove.html[plugin remove]
1897command can be used.
1898
Brad Larsond5e87c32012-07-11 12:18:49 -05001899Disabled plugins can be re-enabled using the
1900link:cmd-plugin-enable.html[plugin enable] command.
1901
Yuxuan 'fishy' Wang61698b12013-12-20 12:55:51 -08001902== SEE ALSO
David Ostrovskyf86bae52013-09-01 09:10:39 +02001903
1904* link:js-api.html[JavaScript API]
1905* link:dev-rest-api.html[REST API Developers' Notes]
1906
Deniz Türkoglueb78b602012-05-07 14:02:36 -07001907GERRIT
1908------
1909Part of link:index.html[Gerrit Code Review]
Yuxuan 'fishy' Wang99cb68d2013-10-31 17:26:00 -07001910
1911SEARCHBOX
1912---------