Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1 | Gerrit Code Review - Plugin Development |
| 2 | ======================================= |
| 3 | |
Edwin Kempin | af27532 | 2012-07-16 11:04:01 +0200 | [diff] [blame] | 4 | The Gerrit server functionality can be extended by installing plugins. |
| 5 | This page describes how plugins for Gerrit can be developed. |
| 6 | |
| 7 | Depending on how tightly the extension code is coupled with the Gerrit |
| 8 | server code, there is a distinction between `plugins` and `extensions`. |
| 9 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 10 | [[plugin]] |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 11 | A `plugin` in Gerrit is tightly coupled code that runs in the same |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 12 | JVM as Gerrit. It has full access to all server internals. Plugins |
| 13 | are tightly coupled to a specific major.minor server version and |
| 14 | may require source code changes to compile against a different |
| 15 | server version. |
| 16 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 17 | [[extension]] |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 18 | An `extension` in Gerrit runs inside of the same JVM as Gerrit |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 19 | in the same way as a plugin, but has limited visibility to the |
Edwin Kempin | fd19bfb | 2012-07-16 10:44:17 +0200 | [diff] [blame] | 20 | server's internals. The limited visibility reduces the extension's |
| 21 | dependencies, enabling it to be compatible across a wider range |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 22 | of server versions. |
| 23 | |
| 24 | Most of this documentation refers to either type as a plugin. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 25 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 26 | [[getting-started]] |
| 27 | Getting started |
| 28 | --------------- |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 29 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 30 | To get started with the development of a plugin there are two |
| 31 | recommended ways: |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 32 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 33 | . use the Gerrit Plugin Maven archetype to create a new plugin project: |
| 34 | + |
| 35 | With the Gerrit Plugin Maven archetype you can create a skeleton for a |
| 36 | plugin project. |
| 37 | + |
| 38 | ---- |
| 39 | mvn archetype:generate -DarchetypeGroupId=com.google.gerrit \ |
| 40 | -DarchetypeArtifactId=gerrit-plugin-archetype \ |
David Pursehouse | 62864b7 | 2013-10-17 23:05:08 +0900 | [diff] [blame] | 41 | -DarchetypeVersion=2.9-SNAPSHOT \ |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 42 | -DgroupId=com.google.gerrit \ |
| 43 | -DartifactId=testPlugin |
| 44 | ---- |
| 45 | + |
| 46 | Maven will ask for additional properties and then create the plugin in |
| 47 | the current directory. To change the default property values answer 'n' |
| 48 | when Maven asks to confirm the properties configuration. It will then |
| 49 | ask again for all properties including those with predefined default |
| 50 | values. |
| 51 | |
David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 52 | . clone the sample plugin: |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 53 | + |
David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 54 | This is a project that demonstrates the various features of the |
| 55 | plugin API. It can be taken as an example to develop an own plugin. |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 56 | + |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 57 | ---- |
David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 58 | $ git clone https://gerrit.googlesource.com/plugins/cookbook-plugin |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 59 | ---- |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 60 | + |
| 61 | When starting from this example one should take care to adapt the |
| 62 | `Gerrit-ApiVersion` in the `pom.xml` to the version of Gerrit for which |
| 63 | the plugin is developed. If the plugin is developed for a released |
| 64 | Gerrit version (no `SNAPSHOT` version) then the URL for the |
| 65 | `gerrit-api-repository` in the `pom.xml` needs to be changed to |
Shawn Pearce | d500500 | 2013-06-21 11:01:45 -0700 | [diff] [blame] | 66 | `https://gerrit-api.storage.googleapis.com/release/`. |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 67 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 68 | [[API]] |
| 69 | API |
| 70 | --- |
| 71 | |
| 72 | There are two different API formats offered against which plugins can |
| 73 | be developed: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 74 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 75 | gerrit-extension-api.jar:: |
| 76 | A stable but thin interface. Suitable for extensions that need |
| 77 | to be notified of events, but do not require tight coupling to |
| 78 | the internals of Gerrit. Extensions built against this API can |
| 79 | expect to be binary compatible across a wide range of server |
| 80 | versions. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 81 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 82 | gerrit-plugin-api.jar:: |
| 83 | The complete internals of the Gerrit server, permitting a |
| 84 | plugin to tightly couple itself and provide additional |
| 85 | functionality that is not possible as an extension. Plugins |
| 86 | built against this API are expected to break at the source |
| 87 | code level between every major.minor Gerrit release. A plugin |
| 88 | that compiles against 2.5 will probably need source code level |
| 89 | changes to work with 2.6, 2.7, and so on. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 90 | |
| 91 | Manifest |
| 92 | -------- |
| 93 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 94 | Plugins may provide optional description information with standard |
| 95 | manifest fields: |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 96 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 97 | ==== |
| 98 | Implementation-Title: Example plugin showing examples |
| 99 | Implementation-Version: 1.0 |
| 100 | Implementation-Vendor: Example, Inc. |
| 101 | Implementation-URL: http://example.com/opensource/plugin-foo/ |
| 102 | ==== |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 103 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 104 | ApiType |
| 105 | ~~~~~~~ |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 106 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 107 | Plugins using the tightly coupled `gerrit-plugin-api.jar` must |
| 108 | declare this API dependency in the manifest to gain access to server |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 109 | internals. If no `Gerrit-ApiType` is specified the stable `extension` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 110 | API will be assumed. This may cause ClassNotFoundExceptions when |
| 111 | loading a plugin that needs the plugin API. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 112 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 113 | ==== |
| 114 | Gerrit-ApiType: plugin |
| 115 | ==== |
| 116 | |
| 117 | Explicit Registration |
| 118 | ~~~~~~~~~~~~~~~~~~~~~ |
| 119 | |
| 120 | Plugins that use explicit Guice registration must name the Guice |
| 121 | modules in the manifest. Up to three modules can be named in the |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 122 | manifest. `Gerrit-Module` supplies bindings to the core server; |
| 123 | `Gerrit-SshModule` supplies SSH commands to the SSH server (if |
| 124 | enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 125 | server (if enabled). If no modules are named automatic registration |
| 126 | will be performed by scanning all classes in the plugin JAR for |
| 127 | `@Listen` and `@Export("")` annotations. |
| 128 | |
| 129 | ==== |
| 130 | Gerrit-Module: tld.example.project.CoreModuleClassName |
| 131 | Gerrit-SshModule: tld.example.project.SshModuleClassName |
| 132 | Gerrit-HttpModule: tld.example.project.HttpModuleClassName |
| 133 | ==== |
| 134 | |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 135 | [[plugin_name]] |
| 136 | Plugin Name |
| 137 | ~~~~~~~~~~~ |
| 138 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 139 | A plugin can optionally provide its own plugin name. |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 140 | |
| 141 | ==== |
| 142 | Gerrit-PluginName: replication |
| 143 | ==== |
| 144 | |
| 145 | This is useful for plugins that contribute plugin-owned capabilities that |
| 146 | are stored in the `project.config` file. Another use case is to be able to put |
| 147 | project specific plugin configuration section in `project.config`. In this |
| 148 | case it is advantageous to reserve the plugin name to access the configuration |
| 149 | section in the `project.config` file. |
| 150 | |
| 151 | If `Gerrit-PluginName` is omitted, then the plugin's name is determined from |
| 152 | the plugin file name. |
| 153 | |
| 154 | If a plugin provides its own name, then that plugin cannot be deployed |
| 155 | multiple times under different file names on one Gerrit site. |
| 156 | |
| 157 | For Maven driven plugins, the following line must be included in the pom.xml |
| 158 | file: |
| 159 | |
| 160 | [source,xml] |
| 161 | ---- |
| 162 | <manifestEntries> |
| 163 | <Gerrit-PluginName>name</Gerrit-PluginName> |
| 164 | </manifestEntries> |
| 165 | ---- |
| 166 | |
| 167 | For Buck driven plugins, the following line must be included in the BUCK |
| 168 | configuration file: |
| 169 | |
| 170 | [source,python] |
| 171 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 172 | manifest_entries = [ |
| 173 | 'Gerrit-PluginName: name', |
| 174 | ] |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 175 | ---- |
| 176 | |
Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 177 | A plugin can get its own name injected at runtime: |
| 178 | |
| 179 | [source,java] |
| 180 | ---- |
| 181 | public class MyClass { |
| 182 | |
| 183 | private final String pluginName; |
| 184 | |
| 185 | @Inject |
| 186 | public MyClass(@PluginName String pluginName) { |
| 187 | this.pluginName = pluginName; |
| 188 | } |
| 189 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 190 | [...] |
Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 191 | } |
| 192 | ---- |
| 193 | |
David Pursehouse | 8ed0d92 | 2013-10-18 18:57:56 +0900 | [diff] [blame] | 194 | A plugin can get its canonical web URL injected at runtime: |
| 195 | |
| 196 | [source,java] |
| 197 | ---- |
| 198 | public class MyClass { |
| 199 | |
| 200 | private final String url; |
| 201 | |
| 202 | @Inject |
| 203 | public MyClass(@PluginCanonicalWebUrl String url) { |
| 204 | this.url = url; |
| 205 | } |
| 206 | |
| 207 | [...] |
| 208 | } |
| 209 | ---- |
| 210 | |
| 211 | The URL is composed of the server's canonical web URL and the plugin's |
| 212 | name, i.e. `http://review.example.com:8080/plugin-name`. |
| 213 | |
| 214 | The canonical web URL may be injected into any .jar plugin regardless of |
| 215 | whether or not the plugin provides an HTTP servlet. |
| 216 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 217 | [[reload_method]] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 218 | Reload Method |
| 219 | ~~~~~~~~~~~~~ |
| 220 | |
| 221 | If a plugin holds an exclusive resource that must be released before |
| 222 | loading the plugin again (for example listening on a network port or |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 223 | acquiring a file lock) the manifest must declare `Gerrit-ReloadMode` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 224 | to be `restart`. Otherwise the preferred method of `reload` will |
| 225 | be used, as it enables the server to hot-patch an updated plugin |
| 226 | with no down time. |
| 227 | |
| 228 | ==== |
| 229 | Gerrit-ReloadMode: restart |
| 230 | ==== |
| 231 | |
| 232 | In either mode ('restart' or 'reload') any plugin or extension can |
| 233 | be updated without restarting the Gerrit server. The difference is |
| 234 | how Gerrit handles the upgrade: |
| 235 | |
| 236 | restart:: |
| 237 | The old plugin is completely stopped. All registrations of SSH |
| 238 | commands and HTTP servlets are removed. All registrations of any |
| 239 | extension points are removed. All registered LifecycleListeners |
| 240 | have their `stop()` method invoked in reverse order. The new |
| 241 | plugin is started, and registrations are made from the new |
| 242 | plugin. There is a brief window where neither the old nor the |
| 243 | new plugin is connected to the server. This means SSH commands |
| 244 | and HTTP servlets will return not found errors, and the plugin |
| 245 | will not be notified of events that occurred during the restart. |
| 246 | |
| 247 | reload:: |
| 248 | The new plugin is started. Its LifecycleListeners are permitted |
| 249 | to perform their `start()` methods. All SSH and HTTP registrations |
| 250 | are atomically swapped out from the old plugin to the new plugin, |
| 251 | ensuring the server never returns a not found error. All extension |
| 252 | point listeners are atomically swapped out from the old plugin to |
| 253 | the new plugin, ensuring no events are missed (however some events |
| 254 | may still route to the old plugin if the swap wasn't complete yet). |
| 255 | The old plugin is stopped. |
| 256 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 257 | To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload] |
| 258 | command can be used. |
| 259 | |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 260 | [[init_step]] |
| 261 | Init step |
| 262 | ~~~~~~~~~ |
| 263 | |
| 264 | Plugins can contribute their own "init step" during the Gerrit init |
| 265 | wizard. This is useful for guiding the Gerrit administrator through |
| 266 | the settings needed by the plugin to work propertly. |
| 267 | |
| 268 | For instance plugins to integrate Jira issues to Gerrit changes may |
| 269 | contribute their own "init step" to allow configuring the Jira URL, |
| 270 | credentials and possibly verify connectivity to validate them. |
| 271 | |
| 272 | ==== |
| 273 | Gerrit-InitStep: tld.example.project.MyInitStep |
| 274 | ==== |
| 275 | |
| 276 | MyInitStep needs to follow the standard Gerrit InitStep syntax |
David Pursehouse | 9246356 | 2013-06-24 10:16:28 +0900 | [diff] [blame] | 277 | and behavior: writing to the console using the injected ConsoleUI |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 278 | and accessing / changing configuration settings using Section.Factory. |
| 279 | |
| 280 | In addition to the standard Gerrit init injections, plugins receive |
| 281 | the @PluginName String injection containing their own plugin name. |
| 282 | |
| 283 | Bear in mind that the Plugin's InitStep class will be loaded but |
| 284 | the standard Gerrit runtime environment is not available and the plugin's |
| 285 | own Guice modules were not initialized. |
| 286 | This means the InitStep for a plugin is not executed in the same way that |
| 287 | the plugin executes within the server, and may mean a plugin author cannot |
| 288 | trivially reuse runtime code during init. |
| 289 | |
| 290 | For instance a plugin that wants to verify connectivity may need to statically |
| 291 | call the constructor of their connection class, passing in values obtained |
| 292 | from the Section.Factory rather than from an injected Config object. |
| 293 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 294 | Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after |
| 295 | the extraction of the plugins embedded in the distribution .war file into |
| 296 | `$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade. |
| 297 | |
| 298 | A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit |
| 299 | runtime objects injected at startup. |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 300 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 301 | [source,java] |
| 302 | ---- |
| 303 | public class MyInitStep implements InitStep { |
| 304 | private final ConsoleUI ui; |
| 305 | private final Section.Factory sections; |
| 306 | private final String pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 307 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 308 | @Inject |
| 309 | public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) { |
| 310 | this.ui = ui; |
| 311 | this.sections = sections; |
| 312 | this.pluginName = pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 313 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 314 | |
| 315 | @Override |
| 316 | public void run() throws Exception { |
| 317 | ui.header("\nMy plugin"); |
| 318 | |
| 319 | Section mySection = getSection("myplugin", null); |
| 320 | mySection.string("Link name", "linkname", "MyLink"); |
| 321 | } |
| 322 | } |
| 323 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 324 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 325 | [[classpath]] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 326 | Classpath |
| 327 | --------- |
| 328 | |
| 329 | Each plugin is loaded into its own ClassLoader, isolating plugins |
| 330 | from each other. A plugin or extension inherits the Java runtime |
| 331 | and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin) |
| 332 | from the hosting server. |
| 333 | |
| 334 | Plugins are loaded from a single JAR file. If a plugin needs |
| 335 | additional libraries, it must include those dependencies within |
| 336 | its own JAR. Plugins built using Maven may be able to use the |
| 337 | link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin] |
| 338 | to package additional dependencies. Relocating (or renaming) classes |
| 339 | should not be necessary due to the ClassLoader isolation. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 340 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 341 | [[events]] |
| 342 | Listening to Events |
| 343 | ------------------- |
| 344 | |
| 345 | Certain operations in Gerrit trigger events. Plugins may receive |
| 346 | notifications of these events by implementing the corresponding |
| 347 | listeners. |
| 348 | |
Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 349 | * `com.google.gerrit.common.ChangeListener`: |
| 350 | + |
| 351 | Allows to listen to change events. These are the same |
| 352 | link:cmd-stream-events.html#events[events] that are also streamed by |
| 353 | the link:cmd-stream-events.html[gerrit stream-events] command. |
| 354 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 355 | * `com.google.gerrit.extensions.events.LifecycleListener`: |
| 356 | + |
| 357 | Gerrit server startup and shutdown |
| 358 | |
| 359 | * `com.google.gerrit.extensions.events.NewProjectCreatedListener`: |
| 360 | + |
| 361 | Project creation |
| 362 | |
| 363 | * `com.google.gerrit.extensions.events.ProjectDeletedListener`: |
| 364 | + |
| 365 | Project deletion |
| 366 | |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 367 | [[stream-events]] |
| 368 | Sending Events to the Events Stream |
| 369 | ----------------------------------- |
| 370 | |
| 371 | Plugins may send events to the events stream where consumers of |
| 372 | Gerrit's `stream-events` ssh command will receive them. |
| 373 | |
| 374 | To send an event, the plugin must invoke one of the `postEvent` |
| 375 | methods in the `ChangeHookRunner` class, passing an instance of |
| 376 | its own custom event class derived from `ChangeEvent`. |
| 377 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 378 | [[ssh]] |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 379 | SSH Commands |
| 380 | ------------ |
| 381 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 382 | Plugins may provide commands that can be accessed through the SSH |
| 383 | interface (extensions do not have this option). |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 384 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 385 | Command implementations must extend the base class SshCommand: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 386 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 387 | [source,java] |
| 388 | ---- |
| 389 | import com.google.gerrit.sshd.SshCommand; |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 390 | import com.google.gerrit.sshd.CommandMetaData; |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 391 | |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 392 | @CommandMetaData(name="print", descr="Print hello command") |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 393 | class PrintHello extends SshCommand { |
| 394 | protected abstract void run() { |
| 395 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 396 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 397 | } |
| 398 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 399 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 400 | If no Guice modules are declared in the manifest, SSH commands may |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 401 | use auto-registration by providing an `@Export` annotation: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 402 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 403 | [source,java] |
| 404 | ---- |
| 405 | import com.google.gerrit.extensions.annotations.Export; |
| 406 | import com.google.gerrit.sshd.SshCommand; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 407 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 408 | @Export("print") |
| 409 | class PrintHello extends SshCommand { |
| 410 | protected abstract void run() { |
| 411 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 412 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 413 | } |
| 414 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 415 | |
| 416 | If explicit registration is being used, a Guice module must be |
| 417 | supplied to register the SSH command and declared in the manifest |
| 418 | with the `Gerrit-SshModule` attribute: |
| 419 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 420 | [source,java] |
| 421 | ---- |
| 422 | import com.google.gerrit.sshd.PluginCommandModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 423 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 424 | class MyCommands extends PluginCommandModule { |
| 425 | protected void configureCommands() { |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 426 | command(PrintHello.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 427 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 428 | } |
| 429 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 430 | |
| 431 | For a plugin installed as name `helloworld`, the command implemented |
| 432 | by PrintHello class will be available to users as: |
| 433 | |
| 434 | ---- |
Keunhong Park | a09a6f1 | 2012-07-10 14:45:02 -0600 | [diff] [blame] | 435 | $ ssh -p 29418 review.example.com helloworld print |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 436 | ---- |
| 437 | |
David Ostrovsky | e3172b3 | 2013-10-13 14:19:13 +0200 | [diff] [blame] | 438 | Multiple SSH commands can be bound to the same implementation class. For |
| 439 | example a Gerrit Shell plugin can bind different shell commands to the same |
| 440 | implementation class: |
| 441 | |
| 442 | [source,java] |
| 443 | ---- |
| 444 | public class SshShellModule extends PluginCommandModule { |
| 445 | @Override |
| 446 | protected void configureCommands() { |
| 447 | command("ls").to(ShellCommand.class); |
| 448 | command("ps").to(ShellCommand.class); |
| 449 | [...] |
| 450 | } |
| 451 | } |
| 452 | ---- |
| 453 | |
| 454 | With the possible implementation: |
| 455 | |
| 456 | [source,java] |
| 457 | ---- |
| 458 | public class ShellCommand extends SshCommand { |
| 459 | @Override |
| 460 | protected void run() throws UnloggedFailure { |
| 461 | String cmd = getName().substring(getPluginName().length() + 1); |
| 462 | ProcessBuilder proc = new ProcessBuilder(cmd); |
| 463 | Process cmd = proc.start(); |
| 464 | [...] |
| 465 | } |
| 466 | } |
| 467 | ---- |
| 468 | |
| 469 | And the call: |
| 470 | |
| 471 | ---- |
| 472 | $ ssh -p 29418 review.example.com shell ls |
| 473 | $ ssh -p 29418 review.example.com shell ps |
| 474 | ---- |
| 475 | |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 476 | Single command plugins are also supported. In this scenario plugin binds |
| 477 | SSH command to its own name. `SshModule` must inherit from |
| 478 | `SingleCommandPluginModule` class: |
| 479 | |
| 480 | [source,java] |
| 481 | ---- |
| 482 | public class SshModule extends SingleCommandPluginModule { |
| 483 | @Override |
| 484 | protected void configure(LinkedBindingBuilder<Command> b) { |
| 485 | b.to(ShellCommand.class); |
| 486 | } |
| 487 | } |
| 488 | ---- |
| 489 | |
| 490 | If the plugin above is deployed under sh.jar file in `$site/plugins` |
| 491 | directory, generic commands can be called without specifing the |
| 492 | actual SSH command. Note in the example below, that the called commands |
| 493 | `ls` and `ps` was not explicitly bound: |
| 494 | |
| 495 | ---- |
| 496 | $ ssh -p 29418 review.example.com sh ls |
| 497 | $ ssh -p 29418 review.example.com sh ps |
| 498 | ---- |
| 499 | |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 500 | [[configuration]] |
| 501 | Configuration |
| 502 | ------------- |
| 503 | |
| 504 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 505 | If a plugin needs global configuration, this configuration should be |
| 506 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 507 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 508 | This approach of storing the plugin configuration is only suitable for |
| 509 | plugins that have a simple configuration that only consists of |
| 510 | key-value pairs. With this approach it is not possible to have |
| 511 | subsections in the plugin configuration. Plugins that require a complex |
| 512 | configuration need to store their configuration in their own |
| 513 | configuration file where they can make use of subsections. On the other |
| 514 | hand storing the plugin configuration in a 'plugin' subsection in the |
| 515 | `gerrit.config` file has the advantage that administrators have all |
| 516 | configuration parameters in one file, instead of having one |
| 517 | configuration file per plugin. |
| 518 | |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 519 | To avoid conflicts with other plugins, it is recommended that plugins |
| 520 | only use the `plugin` subsection with their own name. For example the |
| 521 | `helloworld` plugin should store its configuration in the |
| 522 | `plugin.helloworld` subsection: |
| 523 | |
| 524 | ---- |
| 525 | [plugin "helloworld"] |
| 526 | language = Latin |
| 527 | ---- |
| 528 | |
Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 529 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 530 | plugin can easily access its configuration and there is no need for a |
| 531 | plugin to parse the `gerrit.config` file on its own: |
| 532 | |
| 533 | [source,java] |
| 534 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 535 | @Inject |
| 536 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 537 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 538 | [...] |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 539 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 540 | String language = cfg.getFromGerritConfig("helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 541 | .getString("language", "English"); |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 542 | ---- |
| 543 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 544 | [[project-specific-configuration]] |
| 545 | Project Specific Configuration |
| 546 | ------------------------------ |
| 547 | |
| 548 | In Gerrit, project specific configuration is stored in the project's |
| 549 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 550 | needs configuration on project level (e.g. to enable its functionality |
| 551 | only for certain projects), this configuration should be stored in a |
| 552 | `plugin` subsection in the project's `project.config` file. |
| 553 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 554 | This approach of storing the plugin configuration is only suitable for |
| 555 | plugins that have a simple configuration that only consists of |
| 556 | key-value pairs. With this approach it is not possible to have |
| 557 | subsections in the plugin configuration. Plugins that require a complex |
| 558 | configuration need to store their configuration in their own |
| 559 | configuration file where they can make use of subsections. On the other |
| 560 | hand storing the plugin configuration in a 'plugin' subsection in the |
| 561 | `project.config` file has the advantage that project owners have all |
| 562 | configuration parameters in one file, instead of having one |
| 563 | configuration file per plugin. |
| 564 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 565 | To avoid conflicts with other plugins, it is recommended that plugins |
| 566 | only use the `plugin` subsection with their own name. For example the |
| 567 | `helloworld` plugin should store its configuration in the |
| 568 | `plugin.helloworld` subsection: |
| 569 | |
| 570 | ---- |
| 571 | [plugin "helloworld"] |
| 572 | enabled = true |
| 573 | ---- |
| 574 | |
| 575 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 576 | plugin can easily access its project specific configuration and there |
| 577 | is no need for a plugin to parse the `project.config` file on its own: |
| 578 | |
| 579 | [source,java] |
| 580 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 581 | @Inject |
| 582 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 583 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 584 | [...] |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 585 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 586 | boolean enabled = cfg.getFromProjectConfig(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 587 | .getBoolean("enabled", false); |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 588 | ---- |
| 589 | |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 590 | It is also possible to get missing configuration parameters inherited |
| 591 | from the parent projects: |
| 592 | |
| 593 | [source,java] |
| 594 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 595 | @Inject |
| 596 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 597 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 598 | [...] |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 599 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 600 | boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 601 | .getBoolean("enabled", false); |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 602 | ---- |
| 603 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 604 | Project owners can edit the project configuration by fetching the |
| 605 | `refs/meta/config` branch, editing the `project.config` file and |
| 606 | pushing the commit back. |
| 607 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 608 | [[capabilities]] |
| 609 | Plugin Owned Capabilities |
| 610 | ------------------------- |
| 611 | |
| 612 | Plugins may provide their own capabilities and restrict usage of SSH |
| 613 | commands to the users who are granted those capabilities. |
| 614 | |
| 615 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 616 | abstract class: |
| 617 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 618 | [source,java] |
| 619 | ---- |
| 620 | public class PrintHelloCapability extends CapabilityDefinition { |
| 621 | @Override |
| 622 | public String getDescription() { |
| 623 | return "Print Hello"; |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 624 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 625 | } |
| 626 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 627 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 628 | If no Guice modules are declared in the manifest, UI actions may |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 629 | use auto-registration by providing an `@Export` annotation: |
| 630 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 631 | [source,java] |
| 632 | ---- |
| 633 | @Export("printHello") |
| 634 | public class PrintHelloCapability extends CapabilityDefinition { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 635 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 636 | } |
| 637 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 638 | |
| 639 | Otherwise the capability must be bound in a plugin module: |
| 640 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 641 | [source,java] |
| 642 | ---- |
| 643 | public class HelloWorldModule extends AbstractModule { |
| 644 | @Override |
| 645 | protected void configure() { |
| 646 | bind(CapabilityDefinition.class) |
| 647 | .annotatedWith(Exports.named("printHello")) |
| 648 | .to(PrintHelloCapability.class); |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 649 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 650 | } |
| 651 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 652 | |
| 653 | With a plugin-owned capability defined in this way, it is possible to restrict |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 654 | usage of an SSH command or `UiAction` to members of the group that were granted |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 655 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 656 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 657 | [source,java] |
| 658 | ---- |
| 659 | @RequiresCapability("printHello") |
| 660 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 661 | public final class PrintHelloWorldCommand extends SshCommand { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 662 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 663 | } |
| 664 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 665 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 666 | Or with `UiAction`: |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 667 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 668 | [source,java] |
| 669 | ---- |
| 670 | @RequiresCapability("printHello") |
| 671 | public class SayHelloAction extends UiAction<RevisionResource> |
| 672 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 673 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 674 | } |
| 675 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 676 | |
| 677 | Capability scope was introduced to differentiate between plugin-owned |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 678 | capabilities and core capabilities. Per default the scope of the |
| 679 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 680 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 681 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 682 | capability is assumed to be that plugin. |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 683 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 684 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 685 | (and thus is outside of a plugin) the scope is the core server and will use |
| 686 | the `GlobalCapability` known to Gerrit Code Review server. |
| 687 | |
| 688 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 689 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 690 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 691 | [source,java] |
| 692 | ---- |
| 693 | @RequiresCapability(value = "administrateServer", scope = |
| 694 | CapabilityScope.CORE) |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 695 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 696 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 697 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 698 | [[ui_extension]] |
| 699 | UI Extension |
| 700 | ------------ |
| 701 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 702 | Plugins can contribute UI actions on core Gerrit pages. This is useful |
| 703 | for workflow customization or exposing plugin functionality through the |
| 704 | UI in addition to SSH commands and the REST API. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 705 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 706 | For instance a plugin to integrate Jira with Gerrit changes may |
| 707 | contribute a "File bug" button to allow filing a bug from the change |
| 708 | page or plugins to integrate continuous integration systems may |
| 709 | contribute a "Schedule" button to allow a CI build to be scheduled |
| 710 | manually from the patch set panel. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 711 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 712 | Two different places on core Gerrit pages are supported: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 713 | |
| 714 | * Change screen |
| 715 | * Project info screen |
| 716 | |
| 717 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 718 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 719 | [source,java] |
| 720 | ---- |
| 721 | @RequiresCapability("printHello") |
| 722 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 723 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 724 | static class Input { |
| 725 | boolean french; |
| 726 | String message; |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 727 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 728 | |
| 729 | private Provider<CurrentUser> user; |
| 730 | |
| 731 | @Inject |
| 732 | HelloWorldAction(Provider<CurrentUser> user) { |
| 733 | this.user = user; |
| 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public String apply(RevisionResource rev, Input input) { |
| 738 | final String greeting = input.french |
| 739 | ? "Bonjour" |
| 740 | : "Hello"; |
| 741 | return String.format("%s %s from change %s, patch set %d!", |
| 742 | greeting, |
| 743 | Strings.isNullOrEmpty(input.message) |
| 744 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 745 | : input.message, |
| 746 | rev.getChange().getId().toString(), |
| 747 | rev.getPatchSet().getPatchSetId()); |
| 748 | } |
| 749 | |
| 750 | @Override |
| 751 | public Description getDescription( |
| 752 | RevisionResource resource) { |
| 753 | return new Description() |
| 754 | .setLabel("Say hello") |
| 755 | .setTitle("Say hello in different languages"); |
| 756 | } |
| 757 | } |
| 758 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 759 | |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 760 | Sometimes plugins may want to be able to change the state of a patch set or |
| 761 | change in the `UiAction.apply()` method and reflect these changes on the core |
| 762 | UI. For example a buildbot plugin which exposes a 'Schedule' button on the |
| 763 | patch set panel may want to disable that button after the build was scheduled |
| 764 | and update the tooltip of that button. But because of Gerrit's caching |
| 765 | strategy the following must be taken into consideration. |
| 766 | |
| 767 | The browser is allowed to cache the `UiAction` information until something on |
| 768 | the change is modified. More accurately the change row needs to be modified in |
| 769 | the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or |
| 770 | the +refs/meta/config+ of the project or any parents needs to change to a new |
| 771 | SHA-1. The ETag SHA-1 computation code can be found in the |
| 772 | `ChangeResource.getETag()` method. |
| 773 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 774 | The easiest way to accomplish this is to update `lastUpdatedOn` of the change: |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 775 | |
| 776 | [source,java] |
| 777 | ---- |
| 778 | @Override |
| 779 | public Object apply(RevisionResource rcrs, Input in) { |
| 780 | // schedule a build |
| 781 | [...] |
| 782 | // update change |
| 783 | ReviewDb db = dbProvider.get(); |
| 784 | db.changes().beginTransaction(change.getId()); |
| 785 | try { |
| 786 | change = db.changes().atomicUpdate( |
| 787 | change.getId(), |
| 788 | new AtomicUpdate<Change>() { |
| 789 | @Override |
| 790 | public Change update(Change change) { |
| 791 | ChangeUtil.updated(change); |
| 792 | return change; |
| 793 | } |
| 794 | }); |
| 795 | db.commit(); |
| 796 | } finally { |
| 797 | db.rollback(); |
| 798 | } |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 799 | [...] |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 800 | } |
| 801 | ---- |
| 802 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 803 | `UiAction` must be bound in a plugin module: |
| 804 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 805 | [source,java] |
| 806 | ---- |
| 807 | public class Module extends AbstractModule { |
| 808 | @Override |
| 809 | protected void configure() { |
| 810 | install(new RestApiModule() { |
| 811 | @Override |
| 812 | protected void configure() { |
| 813 | post(REVISION_KIND, "say-hello") |
| 814 | .to(HelloWorldAction.class); |
| 815 | } |
| 816 | }); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 817 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 818 | } |
| 819 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 820 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 821 | The module above must be declared in the `pom.xml` for Maven driven |
| 822 | plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 823 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 824 | [source,xml] |
| 825 | ---- |
| 826 | <manifestEntries> |
| 827 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 828 | </manifestEntries> |
| 829 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 830 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 831 | or in the `BUCK` configuration file for Buck driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 832 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 833 | [source,python] |
| 834 | ---- |
| 835 | manifest_entries = [ |
| 836 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 837 | ] |
| 838 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 839 | |
| 840 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 841 | combined with the JavaScript API. This would display a small popup near the |
| 842 | activation button to gather additional input from the user. The JS file is |
| 843 | typically put in the `static` folder within the plugin's directory: |
| 844 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 845 | [source,javascript] |
| 846 | ---- |
| 847 | Gerrit.install(function(self) { |
| 848 | function onSayHello(c) { |
| 849 | var f = c.textfield(); |
| 850 | var t = c.checkbox(); |
| 851 | var b = c.button('Say hello', {onclick: function(){ |
| 852 | c.call( |
| 853 | {message: f.value, french: t.checked}, |
| 854 | function(r) { |
| 855 | c.hide(); |
| 856 | window.alert(r); |
| 857 | c.refresh(); |
| 858 | }); |
| 859 | }}); |
| 860 | c.popup(c.div( |
| 861 | c.prependLabel('Greeting message', f), |
| 862 | c.br(), |
| 863 | c.label(t, 'french'), |
| 864 | c.br(), |
| 865 | b)); |
| 866 | f.focus(); |
| 867 | } |
| 868 | self.onAction('revision', 'say-hello', onSayHello); |
| 869 | }); |
| 870 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 871 | |
| 872 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 873 | an HTTP Module: |
| 874 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 875 | [source,java] |
| 876 | ---- |
| 877 | public class HttpModule extends HttpPluginModule { |
| 878 | @Override |
| 879 | protected void configureServlets() { |
| 880 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 881 | .toInstance(new JavaScriptPlugin("hello.js")); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 882 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 883 | } |
| 884 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 885 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 886 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 887 | driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 888 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 889 | [source,xml] |
| 890 | ---- |
| 891 | <manifestEntries> |
| 892 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 893 | </manifestEntries> |
| 894 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 895 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 896 | or in the `BUCK` configuration file for Buck driven plugins |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 897 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 898 | [source,python] |
| 899 | ---- |
| 900 | manifest_entries = [ |
| 901 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 902 | ] |
| 903 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 904 | |
| 905 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 906 | capability check is done during the `UiAction` gathering, so the plugin author |
| 907 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 908 | case. |
| 909 | |
| 910 | The following prerequisities must be met, to satisfy the capability check: |
| 911 | |
| 912 | * user is authenticated |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 913 | * user is a member of a group which has the `Administrate Server` capability, or |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 914 | * user is a member of a group which has the required capability |
| 915 | |
| 916 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 917 | combined with JavaScript API (its own JavaScript function is provided), |
| 918 | then a popup dialog is normally opened to gather additional user input. |
| 919 | A new button is placed on the popup dialog to actually send the request. |
| 920 | |
| 921 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 922 | can be accessed from any REST client, i. e.: |
| 923 | |
| 924 | ==== |
| 925 | curl -X POST -H "Content-Type: application/json" \ |
| 926 | -d '{message: "François", french: true}' \ |
| 927 | --digest --user joe:secret \ |
| 928 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
| 929 | "Bonjour François from change 1, patch set 1!" |
| 930 | ==== |
| 931 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 932 | A special case is to bind an endpoint without a view name. This is |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 933 | particularly useful for `DELETE` requests: |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 934 | |
| 935 | [source,java] |
| 936 | ---- |
| 937 | public class Module extends AbstractModule { |
| 938 | @Override |
| 939 | protected void configure() { |
| 940 | install(new RestApiModule() { |
| 941 | @Override |
| 942 | protected void configure() { |
| 943 | delete(PROJECT_KIND) |
| 944 | .to(DeleteProject.class); |
| 945 | } |
| 946 | }); |
| 947 | } |
| 948 | } |
| 949 | ---- |
| 950 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 951 | For a `UiAction` bound this way, a JS API function can be provided. |
| 952 | |
| 953 | Currently only one restriction exists: per plugin only one `UiAction` |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 954 | can be bound per resource without view name. To define a JS function |
| 955 | for the `UiAction`, "/" must be used as the name: |
| 956 | |
| 957 | [source,javascript] |
| 958 | ---- |
| 959 | Gerrit.install(function(self) { |
| 960 | function onDeleteProject(c) { |
| 961 | [...] |
| 962 | } |
| 963 | self.onAction('project', '/', onDeleteProject); |
| 964 | }); |
| 965 | ---- |
| 966 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 967 | [[top-menu-extensions]] |
| 968 | Top Menu Extensions |
| 969 | ------------------- |
| 970 | |
| 971 | Plugins can contribute items to Gerrit's top menu. |
| 972 | |
| 973 | A single top menu extension can have multiple elements and will be put as |
| 974 | the last element in Gerrit's top menu. |
| 975 | |
| 976 | Plugins define the top menu entries by implementing `TopMenu` interface: |
| 977 | |
| 978 | [source,java] |
| 979 | ---- |
| 980 | public class MyTopMenuExtension implements TopMenu { |
| 981 | |
| 982 | @Override |
| 983 | public List<MenuEntry> getEntries() { |
| 984 | return Lists.newArrayList( |
| 985 | new MenuEntry("Top Menu Entry", Lists.newArrayList( |
| 986 | new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 987 | } |
| 988 | } |
| 989 | ---- |
| 990 | |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 991 | Plugins can also add additional menu items to Gerrit's top menu entries |
| 992 | by defining a `MenuEntry` that has the same name as a Gerrit top menu |
| 993 | entry: |
| 994 | |
| 995 | [source,java] |
| 996 | ---- |
| 997 | public class MyTopMenuExtension implements TopMenu { |
| 998 | |
| 999 | @Override |
| 1000 | public List<MenuEntry> getEntries() { |
| 1001 | return Lists.newArrayList( |
Dariusz Luksza | 2d3afab | 2013-10-01 11:07:13 +0200 | [diff] [blame] | 1002 | new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList( |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1003 | new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/")))); |
| 1004 | } |
| 1005 | } |
| 1006 | ---- |
| 1007 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1008 | If no Guice modules are declared in the manifest, the top menu extension may use |
| 1009 | auto-registration by providing an `@Listen` annotation: |
| 1010 | |
| 1011 | [source,java] |
| 1012 | ---- |
| 1013 | @Listen |
| 1014 | public class MyTopMenuExtension implements TopMenu { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1015 | [...] |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1016 | } |
| 1017 | ---- |
| 1018 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1019 | Otherwise the top menu extension must be bound in the plugin module used |
| 1020 | for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF): |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1021 | |
| 1022 | [source,java] |
| 1023 | ---- |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1024 | package com.googlesource.gerrit.plugins.helloworld; |
| 1025 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1026 | public class HelloWorldModule extends AbstractModule { |
| 1027 | @Override |
| 1028 | protected void configure() { |
| 1029 | DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class); |
| 1030 | } |
| 1031 | } |
| 1032 | ---- |
| 1033 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1034 | [source,manifest] |
| 1035 | ---- |
| 1036 | Gerrit-ApiType: plugin |
| 1037 | Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule |
| 1038 | ---- |
| 1039 | |
Edwin Kempin | b2e926a | 2013-11-11 16:38:30 +0100 | [diff] [blame^] | 1040 | It is also possible to show some menu entries only if the user has a |
| 1041 | certain capability: |
| 1042 | |
| 1043 | [source,java] |
| 1044 | ---- |
| 1045 | public class MyTopMenuExtension implements TopMenu { |
| 1046 | private final String pluginName; |
| 1047 | private final Provider<CurrentUser> userProvider; |
| 1048 | private final List<MenuEntry> menuEntries; |
| 1049 | |
| 1050 | @Inject |
| 1051 | public MyTopMenuExtension(@PluginName String pluginName, |
| 1052 | Provider<CurrentUser> userProvider) { |
| 1053 | this.pluginName = pluginName; |
| 1054 | this.userProvider = userProvider; |
| 1055 | menuEntries = new ArrayList<TopMenu.MenuEntry>(); |
| 1056 | |
| 1057 | // add menu entry that is only visible to users with a certain capability |
| 1058 | if (canSeeMenuEntry()) { |
| 1059 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1060 | .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1061 | } |
| 1062 | |
| 1063 | // add menu entry that is visible to all users (even anonymous users) |
| 1064 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1065 | .singletonList(new MenuItem("Documentation", "/plugins/myplugin/")))); |
| 1066 | } |
| 1067 | |
| 1068 | private boolean canSeeMenuEntry() { |
| 1069 | if (userProvider.get().isIdentifiedUser()) { |
| 1070 | CapabilityControl ctl = userProvider.get().getCapabilities(); |
| 1071 | return ctl.canPerform(pluginName + "-" + MyCapability.ID) |
| 1072 | || ctl.canAdministrateServer(); |
| 1073 | } else { |
| 1074 | return false; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | @Override |
| 1079 | public List<MenuEntry> getEntries() { |
| 1080 | return menuEntries; |
| 1081 | } |
| 1082 | } |
| 1083 | ---- |
| 1084 | |
Edwin Kempin | 3c024ea | 2013-11-11 10:43:46 +0100 | [diff] [blame] | 1085 | [[gwt_ui_extension]] |
| 1086 | GWT UI Extension |
| 1087 | ---------------- |
| 1088 | Plugins can extend the Gerrit UI with own GWT code. |
| 1089 | |
| 1090 | The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to |
| 1091 | generate a GWT plugin skeleton. How to use the Maven plugin archetypes |
| 1092 | is described in the link:#getting-started[Getting started] section. |
| 1093 | |
| 1094 | The generated GWT plugin has a link:#top-menu-extensions[top menu] that |
| 1095 | opens a GWT dialog box when the user clicks on it. |
| 1096 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1097 | [[http]] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1098 | HTTP Servlets |
| 1099 | ------------- |
| 1100 | |
| 1101 | Plugins or extensions may register additional HTTP servlets, and |
| 1102 | wrap them with HTTP filters. |
| 1103 | |
| 1104 | Servlets may use auto-registration to declare the URL they handle: |
| 1105 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1106 | [source,java] |
| 1107 | ---- |
| 1108 | import com.google.gerrit.extensions.annotations.Export; |
| 1109 | import com.google.inject.Singleton; |
| 1110 | import javax.servlet.http.HttpServlet; |
| 1111 | import javax.servlet.http.HttpServletRequest; |
| 1112 | import javax.servlet.http.HttpServletResponse; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1113 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1114 | @Export("/print") |
| 1115 | @Singleton |
| 1116 | class HelloServlet extends HttpServlet { |
| 1117 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 1118 | res.setContentType("text/plain"); |
| 1119 | res.setCharacterEncoding("UTF-8"); |
| 1120 | res.getWriter().write("Hello"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1121 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1122 | } |
| 1123 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1124 | |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1125 | The auto registration only works for standard servlet mappings like |
| 1126 | `/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule |
| 1127 | to register the HTTP servlets and declare it explicitly in the manifest |
| 1128 | with the `Gerrit-HttpModule` attribute: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1129 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1130 | [source,java] |
| 1131 | ---- |
| 1132 | import com.google.inject.servlet.ServletModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1133 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1134 | class MyWebUrls extends ServletModule { |
| 1135 | protected void configureServlets() { |
| 1136 | serve("/print").with(HelloServlet.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1137 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1138 | } |
| 1139 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1140 | |
| 1141 | For a plugin installed as name `helloworld`, the servlet implemented |
| 1142 | by HelloServlet class will be available to users as: |
| 1143 | |
| 1144 | ---- |
| 1145 | $ curl http://review.example.com/plugins/helloworld/print |
| 1146 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1147 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1148 | [[data-directory]] |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1149 | Data Directory |
| 1150 | -------------- |
| 1151 | |
| 1152 | Plugins can request a data directory with a `@PluginData` File |
| 1153 | dependency. A data directory will be created automatically by the |
| 1154 | server in `$site_path/data/$plugin_name` and passed to the plugin. |
| 1155 | |
| 1156 | Plugins can use this to store any data they want. |
| 1157 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1158 | [source,java] |
| 1159 | ---- |
| 1160 | @Inject |
| 1161 | MyType(@PluginData java.io.File myDir) { |
| 1162 | new FileInputStream(new File(myDir, "my.config")); |
| 1163 | } |
| 1164 | ---- |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1165 | |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 1166 | [[download-commands]] |
| 1167 | Download Commands |
| 1168 | ----------------- |
| 1169 | |
| 1170 | Gerrit offers commands for downloading changes using different |
| 1171 | download schemes (e.g. for downloading via different network |
| 1172 | protocols). Plugins can contribute download schemes and download |
| 1173 | commands by implementing |
| 1174 | `com.google.gerrit.extensions.config.DownloadScheme` and |
| 1175 | `com.google.gerrit.extensions.config.DownloadCommand`. |
| 1176 | |
| 1177 | The download schemes and download commands which are used most often |
| 1178 | are provided by the Gerrit core plugin `download-commands`. |
| 1179 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1180 | [[documentation]] |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1181 | Documentation |
| 1182 | ------------- |
| 1183 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1184 | If a plugin does not register a filter or servlet to handle URLs |
| 1185 | `/Documentation/*` or `/static/*`, the core Gerrit server will |
| 1186 | automatically export these resources over HTTP from the plugin JAR. |
| 1187 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1188 | Static resources under the `static/` directory in the JAR will be |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 1189 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 1190 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1191 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1192 | Documentation files under the `Documentation/` directory in the JAR |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 1193 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 1194 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 1195 | attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1196 | |
| 1197 | Documentation may be written in |
| 1198 | link:http://daringfireball.net/projects/markdown/[Markdown] style |
| 1199 | if the file name ends with `.md`. Gerrit will automatically convert |
| 1200 | Markdown to HTML if accessed with extension `.html`. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1201 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1202 | [[macros]] |
Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 1203 | Within the Markdown documentation files macros can be used that allow |
| 1204 | to write documentation with reasonably accurate examples that adjust |
| 1205 | automatically based on the installation. |
| 1206 | |
| 1207 | The following macros are supported: |
| 1208 | |
| 1209 | [width="40%",options="header"] |
| 1210 | |=================================================== |
| 1211 | |Macro | Replacement |
| 1212 | |@PLUGIN@ | name of the plugin |
| 1213 | |@URL@ | Gerrit Web URL |
| 1214 | |@SSH_HOST@ | SSH Host |
| 1215 | |@SSH_PORT@ | SSH Port |
| 1216 | |=================================================== |
| 1217 | |
| 1218 | The macros will be replaced when the documentation files are rendered |
| 1219 | from Markdown to HTML. |
| 1220 | |
| 1221 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 1222 | even if there is an expansion for `KEEP` in the future. |
| 1223 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1224 | [[auto-index]] |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1225 | Automatic Index |
| 1226 | ~~~~~~~~~~~~~~~ |
| 1227 | |
| 1228 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 1229 | redirect clients to the plugin's `/Documentation/index.html`. |
| 1230 | Requests for `/Documentation/` (bare directory) will also redirect |
| 1231 | to `/Documentation/index.html`. |
| 1232 | |
| 1233 | If neither resource `Documentation/index.html` or |
| 1234 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
| 1235 | automatically generate an index page for the plugin's documentation |
| 1236 | tree by scanning every `*.md` and `*.html` file in the Documentation/ |
| 1237 | directory. |
| 1238 | |
| 1239 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 1240 | header of the file and extract the first level one title. This |
| 1241 | title text will be used as display text for a link to the HTML |
| 1242 | version of the page. |
| 1243 | |
| 1244 | For any discovered HTML (`*.html`) file, Gerrit will use the name |
| 1245 | of the file, minus the `*.html` extension, as the link text. Any |
| 1246 | hyphens in the file name will be replaced with spaces. |
| 1247 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1248 | If a discovered file is named `about.md` or `about.html`, its |
| 1249 | content will be inserted in an 'About' section at the top of the |
| 1250 | auto-generated index page. If both `about.md` and `about.html` |
| 1251 | exist, only the first discovered file will be used. |
| 1252 | |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1253 | If a discovered file name beings with `cmd-` it will be clustered |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1254 | into a 'Commands' section of the generated index page. |
| 1255 | |
David Pursehouse | fe52915 | 2013-08-14 16:35:06 +0900 | [diff] [blame] | 1256 | If a discovered file name beings with `servlet-` it will be clustered |
| 1257 | into a 'Servlets' section of the generated index page. |
| 1258 | |
| 1259 | If a discovered file name beings with `rest-api-` it will be clustered |
| 1260 | into a 'REST APIs' section of the generated index page. |
| 1261 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1262 | All other files are clustered under a 'Documentation' section. |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1263 | |
| 1264 | Some optional information from the manifest is extracted and |
| 1265 | displayed as part of the index page, if present in the manifest: |
| 1266 | |
| 1267 | [width="40%",options="header"] |
| 1268 | |=================================================== |
| 1269 | |Field | Source Attribute |
| 1270 | |Name | Implementation-Title |
| 1271 | |Vendor | Implementation-Vendor |
| 1272 | |Version | Implementation-Version |
| 1273 | |URL | Implementation-URL |
| 1274 | |API Version | Gerrit-ApiVersion |
| 1275 | |=================================================== |
| 1276 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1277 | [[deployment]] |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1278 | Deployment |
| 1279 | ---------- |
| 1280 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 1281 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 1282 | server using the link:cmd-plugin-install.html[plugin install] command. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1283 | |
| 1284 | Plugins can also be copied directly into the server's |
| 1285 | directory at `$site_path/plugins/$name.jar`. The name of |
| 1286 | the JAR file, minus the `.jar` extension, will be used as the |
| 1287 | plugin name. Unless disabled, servers periodically scan this |
| 1288 | directory for updated plugins. The time can be adjusted by |
| 1289 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1290 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 1291 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 1292 | command can be used. |
| 1293 | |
Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 1294 | Disabled plugins can be re-enabled using the |
| 1295 | link:cmd-plugin-enable.html[plugin enable] command. |
| 1296 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1297 | SEE ALSO |
| 1298 | -------- |
| 1299 | |
| 1300 | * link:js-api.html[JavaScript API] |
| 1301 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 1302 | |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1303 | GERRIT |
| 1304 | ------ |
| 1305 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 1306 | |
| 1307 | SEARCHBOX |
| 1308 | --------- |