| 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 \ |
| 41 | -DarchetypeVersion=2.5-SNAPSHOT \ |
| 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 | |
| 139 | Plugin can optionally provide its own plugin name. |
| 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 | ---- |
| 172 | manifest_entries = [ |
| 173 | 'Gerrit-PluginName: name', |
| 174 | ] |
| 175 | ---- |
| 176 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 177 | [[reload_method]] |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 178 | Reload Method |
| 179 | ~~~~~~~~~~~~~ |
| 180 | |
| 181 | If a plugin holds an exclusive resource that must be released before |
| 182 | loading the plugin again (for example listening on a network port or |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 183 | acquiring a file lock) the manifest must declare `Gerrit-ReloadMode` |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 184 | to be `restart`. Otherwise the preferred method of `reload` will |
| 185 | be used, as it enables the server to hot-patch an updated plugin |
| 186 | with no down time. |
| 187 | |
| 188 | ==== |
| 189 | Gerrit-ReloadMode: restart |
| 190 | ==== |
| 191 | |
| 192 | In either mode ('restart' or 'reload') any plugin or extension can |
| 193 | be updated without restarting the Gerrit server. The difference is |
| 194 | how Gerrit handles the upgrade: |
| 195 | |
| 196 | restart:: |
| 197 | The old plugin is completely stopped. All registrations of SSH |
| 198 | commands and HTTP servlets are removed. All registrations of any |
| 199 | extension points are removed. All registered LifecycleListeners |
| 200 | have their `stop()` method invoked in reverse order. The new |
| 201 | plugin is started, and registrations are made from the new |
| 202 | plugin. There is a brief window where neither the old nor the |
| 203 | new plugin is connected to the server. This means SSH commands |
| 204 | and HTTP servlets will return not found errors, and the plugin |
| 205 | will not be notified of events that occurred during the restart. |
| 206 | |
| 207 | reload:: |
| 208 | The new plugin is started. Its LifecycleListeners are permitted |
| 209 | to perform their `start()` methods. All SSH and HTTP registrations |
| 210 | are atomically swapped out from the old plugin to the new plugin, |
| 211 | ensuring the server never returns a not found error. All extension |
| 212 | point listeners are atomically swapped out from the old plugin to |
| 213 | the new plugin, ensuring no events are missed (however some events |
| 214 | may still route to the old plugin if the swap wasn't complete yet). |
| 215 | The old plugin is stopped. |
| 216 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 217 | To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload] |
| 218 | command can be used. |
| 219 | |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 220 | [[init_step]] |
| 221 | Init step |
| 222 | ~~~~~~~~~ |
| 223 | |
| 224 | Plugins can contribute their own "init step" during the Gerrit init |
| 225 | wizard. This is useful for guiding the Gerrit administrator through |
| 226 | the settings needed by the plugin to work propertly. |
| 227 | |
| 228 | For instance plugins to integrate Jira issues to Gerrit changes may |
| 229 | contribute their own "init step" to allow configuring the Jira URL, |
| 230 | credentials and possibly verify connectivity to validate them. |
| 231 | |
| 232 | ==== |
| 233 | Gerrit-InitStep: tld.example.project.MyInitStep |
| 234 | ==== |
| 235 | |
| 236 | MyInitStep needs to follow the standard Gerrit InitStep syntax |
| David Pursehouse | 9246356 | 2013-06-24 10:16:28 +0900 | [diff] [blame] | 237 | and behavior: writing to the console using the injected ConsoleUI |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 238 | and accessing / changing configuration settings using Section.Factory. |
| 239 | |
| 240 | In addition to the standard Gerrit init injections, plugins receive |
| 241 | the @PluginName String injection containing their own plugin name. |
| 242 | |
| 243 | Bear in mind that the Plugin's InitStep class will be loaded but |
| 244 | the standard Gerrit runtime environment is not available and the plugin's |
| 245 | own Guice modules were not initialized. |
| 246 | This means the InitStep for a plugin is not executed in the same way that |
| 247 | the plugin executes within the server, and may mean a plugin author cannot |
| 248 | trivially reuse runtime code during init. |
| 249 | |
| 250 | For instance a plugin that wants to verify connectivity may need to statically |
| 251 | call the constructor of their connection class, passing in values obtained |
| 252 | from the Section.Factory rather than from an injected Config object. |
| 253 | |
| 254 | Plugins InitStep are executing during the "Gerrit Plugin init" phase, after |
| 255 | the extraction of the plugins embedded in Gerrit.war into $GERRIT_SITE/plugins |
| 256 | and before the DB Schema initialization or upgrade. |
| 257 | Plugins InitStep cannot refer to Gerrit DB Schema or any other Gerrit runtime |
| 258 | objects injected at startup. |
| 259 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 260 | [source,java] |
| 261 | ---- |
| 262 | public class MyInitStep implements InitStep { |
| 263 | private final ConsoleUI ui; |
| 264 | private final Section.Factory sections; |
| 265 | private final String pluginName; |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 266 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 267 | @Inject |
| 268 | public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) { |
| 269 | this.ui = ui; |
| 270 | this.sections = sections; |
| 271 | this.pluginName = pluginName; |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 272 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 273 | |
| 274 | @Override |
| 275 | public void run() throws Exception { |
| 276 | ui.header("\nMy plugin"); |
| 277 | |
| 278 | Section mySection = getSection("myplugin", null); |
| 279 | mySection.string("Link name", "linkname", "MyLink"); |
| 280 | } |
| 281 | } |
| 282 | ---- |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 283 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 284 | [[classpath]] |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 285 | Classpath |
| 286 | --------- |
| 287 | |
| 288 | Each plugin is loaded into its own ClassLoader, isolating plugins |
| 289 | from each other. A plugin or extension inherits the Java runtime |
| 290 | and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin) |
| 291 | from the hosting server. |
| 292 | |
| 293 | Plugins are loaded from a single JAR file. If a plugin needs |
| 294 | additional libraries, it must include those dependencies within |
| 295 | its own JAR. Plugins built using Maven may be able to use the |
| 296 | link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin] |
| 297 | to package additional dependencies. Relocating (or renaming) classes |
| 298 | should not be necessary due to the ClassLoader isolation. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 299 | |
| Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 300 | [[events]] |
| 301 | Listening to Events |
| 302 | ------------------- |
| 303 | |
| 304 | Certain operations in Gerrit trigger events. Plugins may receive |
| 305 | notifications of these events by implementing the corresponding |
| 306 | listeners. |
| 307 | |
| 308 | * `com.google.gerrit.extensions.events.LifecycleListener`: |
| 309 | + |
| 310 | Gerrit server startup and shutdown |
| 311 | |
| 312 | * `com.google.gerrit.extensions.events.NewProjectCreatedListener`: |
| 313 | + |
| 314 | Project creation |
| 315 | |
| 316 | * `com.google.gerrit.extensions.events.ProjectDeletedListener`: |
| 317 | + |
| 318 | Project deletion |
| 319 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 320 | [[ssh]] |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 321 | SSH Commands |
| 322 | ------------ |
| 323 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 324 | Plugins may provide commands that can be accessed through the SSH |
| 325 | interface (extensions do not have this option). |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 326 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 327 | Command implementations must extend the base class SshCommand: |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 328 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 329 | [source,java] |
| 330 | ---- |
| 331 | import com.google.gerrit.sshd.SshCommand; |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 332 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 333 | class PrintHello extends SshCommand { |
| 334 | protected abstract void run() { |
| 335 | stdout.print("Hello\n"); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 336 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 337 | } |
| 338 | ---- |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 339 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 340 | If no Guice modules are declared in the manifest, SSH commands may |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 341 | use auto-registration by providing an `@Export` annotation: |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 342 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 343 | [source,java] |
| 344 | ---- |
| 345 | import com.google.gerrit.extensions.annotations.Export; |
| 346 | import com.google.gerrit.sshd.SshCommand; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 347 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 348 | @Export("print") |
| 349 | class PrintHello extends SshCommand { |
| 350 | protected abstract void run() { |
| 351 | stdout.print("Hello\n"); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 352 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 353 | } |
| 354 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 355 | |
| 356 | If explicit registration is being used, a Guice module must be |
| 357 | supplied to register the SSH command and declared in the manifest |
| 358 | with the `Gerrit-SshModule` attribute: |
| 359 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 360 | [source,java] |
| 361 | ---- |
| 362 | import com.google.gerrit.sshd.PluginCommandModule; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 363 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 364 | class MyCommands extends PluginCommandModule { |
| 365 | protected void configureCommands() { |
| 366 | command("print").to(PrintHello.class); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 367 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 368 | } |
| 369 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 370 | |
| 371 | For a plugin installed as name `helloworld`, the command implemented |
| 372 | by PrintHello class will be available to users as: |
| 373 | |
| 374 | ---- |
| Keunhong Park | a09a6f1 | 2012-07-10 14:45:02 -0600 | [diff] [blame] | 375 | $ ssh -p 29418 review.example.com helloworld print |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 376 | ---- |
| 377 | |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 378 | [[configuration]] |
| 379 | Configuration |
| 380 | ------------- |
| 381 | |
| 382 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 383 | If a plugin needs global configuration, this configuration should be |
| 384 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 385 | |
| 386 | To avoid conflicts with other plugins, it is recommended that plugins |
| 387 | only use the `plugin` subsection with their own name. For example the |
| 388 | `helloworld` plugin should store its configuration in the |
| 389 | `plugin.helloworld` subsection: |
| 390 | |
| 391 | ---- |
| 392 | [plugin "helloworld"] |
| 393 | language = Latin |
| 394 | ---- |
| 395 | |
| Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 396 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 397 | plugin can easily access its configuration and there is no need for a |
| 398 | plugin to parse the `gerrit.config` file on its own: |
| 399 | |
| 400 | [source,java] |
| 401 | ---- |
| 402 | @Inject |
| Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 403 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 404 | |
| 405 | ... |
| 406 | |
| 407 | String language = cfg.get("helloworld") |
| 408 | .getString("language", "English"); |
| 409 | ---- |
| 410 | |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 411 | [[project-specific-configuration]] |
| 412 | Project Specific Configuration |
| 413 | ------------------------------ |
| 414 | |
| 415 | In Gerrit, project specific configuration is stored in the project's |
| 416 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 417 | needs configuration on project level (e.g. to enable its functionality |
| 418 | only for certain projects), this configuration should be stored in a |
| 419 | `plugin` subsection in the project's `project.config` file. |
| 420 | |
| 421 | To avoid conflicts with other plugins, it is recommended that plugins |
| 422 | only use the `plugin` subsection with their own name. For example the |
| 423 | `helloworld` plugin should store its configuration in the |
| 424 | `plugin.helloworld` subsection: |
| 425 | |
| 426 | ---- |
| 427 | [plugin "helloworld"] |
| 428 | enabled = true |
| 429 | ---- |
| 430 | |
| 431 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 432 | plugin can easily access its project specific configuration and there |
| 433 | is no need for a plugin to parse the `project.config` file on its own: |
| 434 | |
| 435 | [source,java] |
| 436 | ---- |
| 437 | @Inject |
| 438 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 439 | |
| 440 | ... |
| 441 | |
| 442 | boolean enabled = cfg.get(project, "helloworld") |
| 443 | .getBoolean("enabled", false); |
| 444 | ---- |
| 445 | |
| Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 446 | It is also possible to get missing configuration parameters inherited |
| 447 | from the parent projects: |
| 448 | |
| 449 | [source,java] |
| 450 | ---- |
| 451 | @Inject |
| 452 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 453 | |
| 454 | ... |
| 455 | |
| 456 | boolean enabled = cfg.getWithInheritance(project, "helloworld") |
| 457 | .getBoolean("enabled", false); |
| 458 | ---- |
| 459 | |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 460 | Project owners can edit the project configuration by fetching the |
| 461 | `refs/meta/config` branch, editing the `project.config` file and |
| 462 | pushing the commit back. |
| 463 | |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 464 | [[capabilities]] |
| 465 | Plugin Owned Capabilities |
| 466 | ------------------------- |
| 467 | |
| 468 | Plugins may provide their own capabilities and restrict usage of SSH |
| 469 | commands to the users who are granted those capabilities. |
| 470 | |
| 471 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 472 | abstract class: |
| 473 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 474 | [source,java] |
| 475 | ---- |
| 476 | public class PrintHelloCapability extends CapabilityDefinition { |
| 477 | @Override |
| 478 | public String getDescription() { |
| 479 | return "Print Hello"; |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 480 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 481 | } |
| 482 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 483 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 484 | If no Guice modules are declared in the manifest, UI actions may |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 485 | use auto-registration by providing an `@Export` annotation: |
| 486 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 487 | [source,java] |
| 488 | ---- |
| 489 | @Export("printHello") |
| 490 | public class PrintHelloCapability extends CapabilityDefinition { |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 491 | ... |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 492 | } |
| 493 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 494 | |
| 495 | Otherwise the capability must be bound in a plugin module: |
| 496 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 497 | [source,java] |
| 498 | ---- |
| 499 | public class HelloWorldModule extends AbstractModule { |
| 500 | @Override |
| 501 | protected void configure() { |
| 502 | bind(CapabilityDefinition.class) |
| 503 | .annotatedWith(Exports.named("printHello")) |
| 504 | .to(PrintHelloCapability.class); |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 505 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 506 | } |
| 507 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 508 | |
| 509 | 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] | 510 | 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] | 511 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 512 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 513 | [source,java] |
| 514 | ---- |
| 515 | @RequiresCapability("printHello") |
| 516 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 517 | public final class PrintHelloWorldCommand extends SshCommand { |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 518 | ... |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 519 | } |
| 520 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 521 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 522 | Or with `UiAction`: |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 523 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 524 | [source,java] |
| 525 | ---- |
| 526 | @RequiresCapability("printHello") |
| 527 | public class SayHelloAction extends UiAction<RevisionResource> |
| 528 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 529 | ... |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 530 | } |
| 531 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 532 | |
| 533 | Capability scope was introduced to differentiate between plugin-owned |
| David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 534 | capabilities and core capabilities. Per default the scope of the |
| 535 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 536 | |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 537 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 538 | capability is assumed to be that plugin. |
| David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 539 | |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 540 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 541 | (and thus is outside of a plugin) the scope is the core server and will use |
| 542 | the `GlobalCapability` known to Gerrit Code Review server. |
| 543 | |
| 544 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 545 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 546 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 547 | [source,java] |
| 548 | ---- |
| 549 | @RequiresCapability(value = "administrateServer", scope = |
| 550 | CapabilityScope.CORE) |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 551 | ... |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 552 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 553 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 554 | [[ui_extension]] |
| 555 | UI Extension |
| 556 | ------------ |
| 557 | |
| 558 | Plugins can contribute their own UI commands on core Gerrit pages. |
| 559 | This is useful for workflow customization or exposing plugin functionality |
| 560 | through the UI in addition to SSH commands and the REST API. |
| 561 | |
| 562 | For instance a plugin to integrate Jira with Gerrit changes may contribute its |
| 563 | own "File bug" button to allow filing a bug from the change page or plugins to |
| 564 | integrate continuous integration systems may contribute a "Schedule" button to |
| 565 | allow a CI build to be scheduled manually from the patch set panel. |
| 566 | |
| 567 | Two different places on core Gerrit pages are currently supported: |
| 568 | |
| 569 | * Change screen |
| 570 | * Project info screen |
| 571 | |
| 572 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 573 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 574 | [source,java] |
| 575 | ---- |
| 576 | @RequiresCapability("printHello") |
| 577 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 578 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 579 | static class Input { |
| 580 | boolean french; |
| 581 | String message; |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 582 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 583 | |
| 584 | private Provider<CurrentUser> user; |
| 585 | |
| 586 | @Inject |
| 587 | HelloWorldAction(Provider<CurrentUser> user) { |
| 588 | this.user = user; |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public String apply(RevisionResource rev, Input input) { |
| 593 | final String greeting = input.french |
| 594 | ? "Bonjour" |
| 595 | : "Hello"; |
| 596 | return String.format("%s %s from change %s, patch set %d!", |
| 597 | greeting, |
| 598 | Strings.isNullOrEmpty(input.message) |
| 599 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 600 | : input.message, |
| 601 | rev.getChange().getId().toString(), |
| 602 | rev.getPatchSet().getPatchSetId()); |
| 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public Description getDescription( |
| 607 | RevisionResource resource) { |
| 608 | return new Description() |
| 609 | .setLabel("Say hello") |
| 610 | .setTitle("Say hello in different languages"); |
| 611 | } |
| 612 | } |
| 613 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 614 | |
| 615 | `UiAction` must be bound in a plugin module: |
| 616 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 617 | [source,java] |
| 618 | ---- |
| 619 | public class Module extends AbstractModule { |
| 620 | @Override |
| 621 | protected void configure() { |
| 622 | install(new RestApiModule() { |
| 623 | @Override |
| 624 | protected void configure() { |
| 625 | post(REVISION_KIND, "say-hello") |
| 626 | .to(HelloWorldAction.class); |
| 627 | } |
| 628 | }); |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 629 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 630 | } |
| 631 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 632 | |
| 633 | The module above must be declared in pom.xml for Maven driven plugins: |
| 634 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 635 | [source,xml] |
| 636 | ---- |
| 637 | <manifestEntries> |
| 638 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 639 | </manifestEntries> |
| 640 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 641 | |
| 642 | or in the BUCK configuration file for Buck driven plugins: |
| 643 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 644 | [source,python] |
| 645 | ---- |
| 646 | manifest_entries = [ |
| 647 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 648 | ] |
| 649 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 650 | |
| 651 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 652 | combined with the JavaScript API. This would display a small popup near the |
| 653 | activation button to gather additional input from the user. The JS file is |
| 654 | typically put in the `static` folder within the plugin's directory: |
| 655 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 656 | [source,javascript] |
| 657 | ---- |
| 658 | Gerrit.install(function(self) { |
| 659 | function onSayHello(c) { |
| 660 | var f = c.textfield(); |
| 661 | var t = c.checkbox(); |
| 662 | var b = c.button('Say hello', {onclick: function(){ |
| 663 | c.call( |
| 664 | {message: f.value, french: t.checked}, |
| 665 | function(r) { |
| 666 | c.hide(); |
| 667 | window.alert(r); |
| 668 | c.refresh(); |
| 669 | }); |
| 670 | }}); |
| 671 | c.popup(c.div( |
| 672 | c.prependLabel('Greeting message', f), |
| 673 | c.br(), |
| 674 | c.label(t, 'french'), |
| 675 | c.br(), |
| 676 | b)); |
| 677 | f.focus(); |
| 678 | } |
| 679 | self.onAction('revision', 'say-hello', onSayHello); |
| 680 | }); |
| 681 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 682 | |
| 683 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 684 | an HTTP Module: |
| 685 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 686 | [source,java] |
| 687 | ---- |
| 688 | public class HttpModule extends HttpPluginModule { |
| 689 | @Override |
| 690 | protected void configureServlets() { |
| 691 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 692 | .toInstance(new JavaScriptPlugin("hello.js")); |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 693 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 694 | } |
| 695 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 696 | |
| 697 | The HTTP module above must be declared in pom.xml for Maven driven plugins: |
| 698 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 699 | [source,xml] |
| 700 | ---- |
| 701 | <manifestEntries> |
| 702 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 703 | </manifestEntries> |
| 704 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 705 | |
| 706 | or in the BUCK configuration file for Buck driven plugins |
| 707 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 708 | [source,python] |
| 709 | ---- |
| 710 | manifest_entries = [ |
| 711 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 712 | ] |
| 713 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 714 | |
| 715 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 716 | capability check is done during the `UiAction` gathering, so the plugin author |
| 717 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 718 | case. |
| 719 | |
| 720 | The following prerequisities must be met, to satisfy the capability check: |
| 721 | |
| 722 | * user is authenticated |
| 723 | * user is a member of the Administrators group, or |
| 724 | * user is a member of a group which has the required capability |
| 725 | |
| 726 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 727 | combined with JavaScript API (its own JavaScript function is provided), |
| 728 | then a popup dialog is normally opened to gather additional user input. |
| 729 | A new button is placed on the popup dialog to actually send the request. |
| 730 | |
| 731 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 732 | can be accessed from any REST client, i. e.: |
| 733 | |
| 734 | ==== |
| 735 | curl -X POST -H "Content-Type: application/json" \ |
| 736 | -d '{message: "François", french: true}' \ |
| 737 | --digest --user joe:secret \ |
| 738 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
| 739 | "Bonjour François from change 1, patch set 1!" |
| 740 | ==== |
| 741 | |
| David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 742 | A special case is to bind an endpoint without a view name. This is |
| David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 743 | particularly useful for DELETE requests: |
| 744 | |
| 745 | [source,java] |
| 746 | ---- |
| 747 | public class Module extends AbstractModule { |
| 748 | @Override |
| 749 | protected void configure() { |
| 750 | install(new RestApiModule() { |
| 751 | @Override |
| 752 | protected void configure() { |
| 753 | delete(PROJECT_KIND) |
| 754 | .to(DeleteProject.class); |
| 755 | } |
| 756 | }); |
| 757 | } |
| 758 | } |
| 759 | ---- |
| 760 | |
| David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 761 | For a `UiAction` bound this way, a JS API function can be provided. |
| 762 | |
| 763 | Currently only one restriction exists: per plugin only one `UiAction` |
| David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 764 | can be bound per resource without view name. To define a JS function |
| 765 | for the `UiAction`, "/" must be used as the name: |
| 766 | |
| 767 | [source,javascript] |
| 768 | ---- |
| 769 | Gerrit.install(function(self) { |
| 770 | function onDeleteProject(c) { |
| 771 | [...] |
| 772 | } |
| 773 | self.onAction('project', '/', onDeleteProject); |
| 774 | }); |
| 775 | ---- |
| 776 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 777 | [[http]] |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 778 | HTTP Servlets |
| 779 | ------------- |
| 780 | |
| 781 | Plugins or extensions may register additional HTTP servlets, and |
| 782 | wrap them with HTTP filters. |
| 783 | |
| 784 | Servlets may use auto-registration to declare the URL they handle: |
| 785 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 786 | [source,java] |
| 787 | ---- |
| 788 | import com.google.gerrit.extensions.annotations.Export; |
| 789 | import com.google.inject.Singleton; |
| 790 | import javax.servlet.http.HttpServlet; |
| 791 | import javax.servlet.http.HttpServletRequest; |
| 792 | import javax.servlet.http.HttpServletResponse; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 793 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 794 | @Export("/print") |
| 795 | @Singleton |
| 796 | class HelloServlet extends HttpServlet { |
| 797 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 798 | res.setContentType("text/plain"); |
| 799 | res.setCharacterEncoding("UTF-8"); |
| 800 | res.getWriter().write("Hello"); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 801 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 802 | } |
| 803 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 804 | |
| Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 805 | The auto registration only works for standard servlet mappings like |
| 806 | `/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule |
| 807 | to register the HTTP servlets and declare it explicitly in the manifest |
| 808 | with the `Gerrit-HttpModule` attribute: |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 809 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 810 | [source,java] |
| 811 | ---- |
| 812 | import com.google.inject.servlet.ServletModule; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 813 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 814 | class MyWebUrls extends ServletModule { |
| 815 | protected void configureServlets() { |
| 816 | serve("/print").with(HelloServlet.class); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 817 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 818 | } |
| 819 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 820 | |
| 821 | For a plugin installed as name `helloworld`, the servlet implemented |
| 822 | by HelloServlet class will be available to users as: |
| 823 | |
| 824 | ---- |
| 825 | $ curl http://review.example.com/plugins/helloworld/print |
| 826 | ---- |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 827 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 828 | [[data-directory]] |
| Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 829 | Data Directory |
| 830 | -------------- |
| 831 | |
| 832 | Plugins can request a data directory with a `@PluginData` File |
| 833 | dependency. A data directory will be created automatically by the |
| 834 | server in `$site_path/data/$plugin_name` and passed to the plugin. |
| 835 | |
| 836 | Plugins can use this to store any data they want. |
| 837 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 838 | [source,java] |
| 839 | ---- |
| 840 | @Inject |
| 841 | MyType(@PluginData java.io.File myDir) { |
| 842 | new FileInputStream(new File(myDir, "my.config")); |
| 843 | } |
| 844 | ---- |
| Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 845 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 846 | [[documentation]] |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 847 | Documentation |
| 848 | ------------- |
| 849 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 850 | If a plugin does not register a filter or servlet to handle URLs |
| 851 | `/Documentation/*` or `/static/*`, the core Gerrit server will |
| 852 | automatically export these resources over HTTP from the plugin JAR. |
| 853 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 854 | Static resources under the `static/` directory in the JAR will be |
| Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 855 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 856 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 857 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 858 | Documentation files under the `Documentation/` directory in the JAR |
| Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 859 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 860 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 861 | attribute. |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 862 | |
| 863 | Documentation may be written in |
| 864 | link:http://daringfireball.net/projects/markdown/[Markdown] style |
| 865 | if the file name ends with `.md`. Gerrit will automatically convert |
| 866 | Markdown to HTML if accessed with extension `.html`. |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 867 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 868 | [[macros]] |
| Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 869 | Within the Markdown documentation files macros can be used that allow |
| 870 | to write documentation with reasonably accurate examples that adjust |
| 871 | automatically based on the installation. |
| 872 | |
| 873 | The following macros are supported: |
| 874 | |
| 875 | [width="40%",options="header"] |
| 876 | |=================================================== |
| 877 | |Macro | Replacement |
| 878 | |@PLUGIN@ | name of the plugin |
| 879 | |@URL@ | Gerrit Web URL |
| 880 | |@SSH_HOST@ | SSH Host |
| 881 | |@SSH_PORT@ | SSH Port |
| 882 | |=================================================== |
| 883 | |
| 884 | The macros will be replaced when the documentation files are rendered |
| 885 | from Markdown to HTML. |
| 886 | |
| 887 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 888 | even if there is an expansion for `KEEP` in the future. |
| 889 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 890 | [[auto-index]] |
| Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 891 | Automatic Index |
| 892 | ~~~~~~~~~~~~~~~ |
| 893 | |
| 894 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 895 | redirect clients to the plugin's `/Documentation/index.html`. |
| 896 | Requests for `/Documentation/` (bare directory) will also redirect |
| 897 | to `/Documentation/index.html`. |
| 898 | |
| 899 | If neither resource `Documentation/index.html` or |
| 900 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
| 901 | automatically generate an index page for the plugin's documentation |
| 902 | tree by scanning every `*.md` and `*.html` file in the Documentation/ |
| 903 | directory. |
| 904 | |
| 905 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 906 | header of the file and extract the first level one title. This |
| 907 | title text will be used as display text for a link to the HTML |
| 908 | version of the page. |
| 909 | |
| 910 | For any discovered HTML (`*.html`) file, Gerrit will use the name |
| 911 | of the file, minus the `*.html` extension, as the link text. Any |
| 912 | hyphens in the file name will be replaced with spaces. |
| 913 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 914 | If a discovered file is named `about.md` or `about.html`, its |
| 915 | content will be inserted in an 'About' section at the top of the |
| 916 | auto-generated index page. If both `about.md` and `about.html` |
| 917 | exist, only the first discovered file will be used. |
| 918 | |
| Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 919 | If a discovered file name beings with `cmd-` it will be clustered |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 920 | into a 'Commands' section of the generated index page. |
| 921 | |
| David Pursehouse | fe52915 | 2013-08-14 16:35:06 +0900 | [diff] [blame] | 922 | If a discovered file name beings with `servlet-` it will be clustered |
| 923 | into a 'Servlets' section of the generated index page. |
| 924 | |
| 925 | If a discovered file name beings with `rest-api-` it will be clustered |
| 926 | into a 'REST APIs' section of the generated index page. |
| 927 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 928 | All other files are clustered under a 'Documentation' section. |
| Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 929 | |
| 930 | Some optional information from the manifest is extracted and |
| 931 | displayed as part of the index page, if present in the manifest: |
| 932 | |
| 933 | [width="40%",options="header"] |
| 934 | |=================================================== |
| 935 | |Field | Source Attribute |
| 936 | |Name | Implementation-Title |
| 937 | |Vendor | Implementation-Vendor |
| 938 | |Version | Implementation-Version |
| 939 | |URL | Implementation-URL |
| 940 | |API Version | Gerrit-ApiVersion |
| 941 | |=================================================== |
| 942 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 943 | [[deployment]] |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 944 | Deployment |
| 945 | ---------- |
| 946 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 947 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 948 | server using the link:cmd-plugin-install.html[plugin install] command. |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 949 | |
| 950 | Plugins can also be copied directly into the server's |
| 951 | directory at `$site_path/plugins/$name.jar`. The name of |
| 952 | the JAR file, minus the `.jar` extension, will be used as the |
| 953 | plugin name. Unless disabled, servers periodically scan this |
| 954 | directory for updated plugins. The time can be adjusted by |
| 955 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 956 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 957 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 958 | command can be used. |
| 959 | |
| Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 960 | Disabled plugins can be re-enabled using the |
| 961 | link:cmd-plugin-enable.html[plugin enable] command. |
| 962 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 963 | SEE ALSO |
| 964 | -------- |
| 965 | |
| 966 | * link:js-api.html[JavaScript API] |
| 967 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 968 | |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 969 | GERRIT |
| 970 | ------ |
| 971 | Part of link:index.html[Gerrit Code Review] |