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 | 91155c2 | 2013-12-02 20:25:18 +0100 | [diff] [blame] | 42 | -DgroupId=com.googlesource.gerrit.plugins.testplugin \ |
| 43 | -DartifactId=testplugin |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 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 | |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 283 | During their initialization plugins may get access to the |
| 284 | `project.config` file of the `All-Projects` project and they are able |
| 285 | to store configuration parameters in it. For this a plugin `InitStep` |
| 286 | can get `com.google.gerrit.pgm.init.AllProjectsConfig` injected: |
| 287 | |
| 288 | [source,java] |
| 289 | ---- |
| 290 | public class MyInitStep implements InitStep { |
| 291 | private final String pluginName; |
| 292 | private final ConsoleUI ui; |
| 293 | private final AllProjectsConfig allProjectsConfig; |
| 294 | |
| 295 | public MyInitStep(@PluginName String pluginName, ConsoleUI ui, |
| 296 | AllProjectsConfig allProjectsConfig) { |
| 297 | this.pluginName = pluginName; |
| 298 | this.ui = ui; |
| 299 | this.allProjectsConfig = allProjectsConfig; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void run() throws Exception { |
| 304 | ui.message("\n"); |
| 305 | ui.header(pluginName + " Integration"); |
| 306 | boolean enabled = ui.yesno(true, "By default enabled for all projects"); |
| 307 | Config cfg = allProjectsConfig.load(); |
| 308 | if (enabled) { |
| 309 | cfg.setBoolean("plugin", pluginName, "enabled", enabled); |
| 310 | } else { |
| 311 | cfg.unset("plugin", pluginName, "enabled"); |
| 312 | } |
| 313 | allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration"); |
| 314 | } |
| 315 | } |
| 316 | ---- |
| 317 | |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 318 | Bear in mind that the Plugin's InitStep class will be loaded but |
| 319 | the standard Gerrit runtime environment is not available and the plugin's |
| 320 | own Guice modules were not initialized. |
| 321 | This means the InitStep for a plugin is not executed in the same way that |
| 322 | the plugin executes within the server, and may mean a plugin author cannot |
| 323 | trivially reuse runtime code during init. |
| 324 | |
| 325 | For instance a plugin that wants to verify connectivity may need to statically |
| 326 | call the constructor of their connection class, passing in values obtained |
| 327 | from the Section.Factory rather than from an injected Config object. |
| 328 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 329 | Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after |
| 330 | the extraction of the plugins embedded in the distribution .war file into |
| 331 | `$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade. |
| 332 | |
| 333 | A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit |
| 334 | runtime objects injected at startup. |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 335 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 336 | [source,java] |
| 337 | ---- |
| 338 | public class MyInitStep implements InitStep { |
| 339 | private final ConsoleUI ui; |
| 340 | private final Section.Factory sections; |
| 341 | private final String pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 342 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 343 | @Inject |
| 344 | public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) { |
| 345 | this.ui = ui; |
| 346 | this.sections = sections; |
| 347 | this.pluginName = pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 348 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 349 | |
| 350 | @Override |
| 351 | public void run() throws Exception { |
| 352 | ui.header("\nMy plugin"); |
| 353 | |
| 354 | Section mySection = getSection("myplugin", null); |
| 355 | mySection.string("Link name", "linkname", "MyLink"); |
| 356 | } |
| 357 | } |
| 358 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 359 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 360 | [[classpath]] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 361 | Classpath |
| 362 | --------- |
| 363 | |
| 364 | Each plugin is loaded into its own ClassLoader, isolating plugins |
| 365 | from each other. A plugin or extension inherits the Java runtime |
| 366 | and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin) |
| 367 | from the hosting server. |
| 368 | |
| 369 | Plugins are loaded from a single JAR file. If a plugin needs |
| 370 | additional libraries, it must include those dependencies within |
| 371 | its own JAR. Plugins built using Maven may be able to use the |
| 372 | link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin] |
| 373 | to package additional dependencies. Relocating (or renaming) classes |
| 374 | should not be necessary due to the ClassLoader isolation. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 375 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 376 | [[events]] |
| 377 | Listening to Events |
| 378 | ------------------- |
| 379 | |
| 380 | Certain operations in Gerrit trigger events. Plugins may receive |
| 381 | notifications of these events by implementing the corresponding |
| 382 | listeners. |
| 383 | |
Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 384 | * `com.google.gerrit.common.ChangeListener`: |
| 385 | + |
| 386 | Allows to listen to change events. These are the same |
| 387 | link:cmd-stream-events.html#events[events] that are also streamed by |
| 388 | the link:cmd-stream-events.html[gerrit stream-events] command. |
| 389 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 390 | * `com.google.gerrit.extensions.events.LifecycleListener`: |
| 391 | + |
Edwin Kempin | 3e7928a | 2013-12-03 07:39:00 +0100 | [diff] [blame] | 392 | Plugin start and stop |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 393 | |
| 394 | * `com.google.gerrit.extensions.events.NewProjectCreatedListener`: |
| 395 | + |
| 396 | Project creation |
| 397 | |
| 398 | * `com.google.gerrit.extensions.events.ProjectDeletedListener`: |
| 399 | + |
| 400 | Project deletion |
| 401 | |
Edwin Kempin | b27c939 | 2013-11-19 13:12:43 +0100 | [diff] [blame] | 402 | * `com.google.gerrit.extensions.events.HeadUpdatedListener`: |
| 403 | + |
| 404 | Update of HEAD on a project |
| 405 | |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 406 | [[stream-events]] |
| 407 | Sending Events to the Events Stream |
| 408 | ----------------------------------- |
| 409 | |
| 410 | Plugins may send events to the events stream where consumers of |
| 411 | Gerrit's `stream-events` ssh command will receive them. |
| 412 | |
| 413 | To send an event, the plugin must invoke one of the `postEvent` |
| 414 | methods in the `ChangeHookRunner` class, passing an instance of |
| 415 | its own custom event class derived from `ChangeEvent`. |
| 416 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 417 | [[ssh]] |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 418 | SSH Commands |
| 419 | ------------ |
| 420 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 421 | Plugins may provide commands that can be accessed through the SSH |
| 422 | interface (extensions do not have this option). |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 423 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 424 | Command implementations must extend the base class SshCommand: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 425 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 426 | [source,java] |
| 427 | ---- |
| 428 | import com.google.gerrit.sshd.SshCommand; |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 429 | import com.google.gerrit.sshd.CommandMetaData; |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 430 | |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 431 | @CommandMetaData(name="print", descr="Print hello command") |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 432 | class PrintHello extends SshCommand { |
| 433 | protected abstract void run() { |
| 434 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 435 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 436 | } |
| 437 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 438 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 439 | If no Guice modules are declared in the manifest, SSH commands may |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 440 | use auto-registration by providing an `@Export` annotation: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 441 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 442 | [source,java] |
| 443 | ---- |
| 444 | import com.google.gerrit.extensions.annotations.Export; |
| 445 | import com.google.gerrit.sshd.SshCommand; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 446 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 447 | @Export("print") |
| 448 | class PrintHello extends SshCommand { |
| 449 | protected abstract void run() { |
| 450 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 451 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 452 | } |
| 453 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 454 | |
| 455 | If explicit registration is being used, a Guice module must be |
| 456 | supplied to register the SSH command and declared in the manifest |
| 457 | with the `Gerrit-SshModule` attribute: |
| 458 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 459 | [source,java] |
| 460 | ---- |
| 461 | import com.google.gerrit.sshd.PluginCommandModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 462 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 463 | class MyCommands extends PluginCommandModule { |
| 464 | protected void configureCommands() { |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 465 | command(PrintHello.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 466 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 467 | } |
| 468 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 469 | |
| 470 | For a plugin installed as name `helloworld`, the command implemented |
| 471 | by PrintHello class will be available to users as: |
| 472 | |
| 473 | ---- |
Keunhong Park | a09a6f1 | 2012-07-10 14:45:02 -0600 | [diff] [blame] | 474 | $ ssh -p 29418 review.example.com helloworld print |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 475 | ---- |
| 476 | |
David Ostrovsky | e3172b3 | 2013-10-13 14:19:13 +0200 | [diff] [blame] | 477 | Multiple SSH commands can be bound to the same implementation class. For |
| 478 | example a Gerrit Shell plugin can bind different shell commands to the same |
| 479 | implementation class: |
| 480 | |
| 481 | [source,java] |
| 482 | ---- |
| 483 | public class SshShellModule extends PluginCommandModule { |
| 484 | @Override |
| 485 | protected void configureCommands() { |
| 486 | command("ls").to(ShellCommand.class); |
| 487 | command("ps").to(ShellCommand.class); |
| 488 | [...] |
| 489 | } |
| 490 | } |
| 491 | ---- |
| 492 | |
| 493 | With the possible implementation: |
| 494 | |
| 495 | [source,java] |
| 496 | ---- |
| 497 | public class ShellCommand extends SshCommand { |
| 498 | @Override |
| 499 | protected void run() throws UnloggedFailure { |
| 500 | String cmd = getName().substring(getPluginName().length() + 1); |
| 501 | ProcessBuilder proc = new ProcessBuilder(cmd); |
| 502 | Process cmd = proc.start(); |
| 503 | [...] |
| 504 | } |
| 505 | } |
| 506 | ---- |
| 507 | |
| 508 | And the call: |
| 509 | |
| 510 | ---- |
| 511 | $ ssh -p 29418 review.example.com shell ls |
| 512 | $ ssh -p 29418 review.example.com shell ps |
| 513 | ---- |
| 514 | |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 515 | Single command plugins are also supported. In this scenario plugin binds |
| 516 | SSH command to its own name. `SshModule` must inherit from |
| 517 | `SingleCommandPluginModule` class: |
| 518 | |
| 519 | [source,java] |
| 520 | ---- |
| 521 | public class SshModule extends SingleCommandPluginModule { |
| 522 | @Override |
| 523 | protected void configure(LinkedBindingBuilder<Command> b) { |
| 524 | b.to(ShellCommand.class); |
| 525 | } |
| 526 | } |
| 527 | ---- |
| 528 | |
| 529 | If the plugin above is deployed under sh.jar file in `$site/plugins` |
| 530 | directory, generic commands can be called without specifing the |
| 531 | actual SSH command. Note in the example below, that the called commands |
| 532 | `ls` and `ps` was not explicitly bound: |
| 533 | |
| 534 | ---- |
| 535 | $ ssh -p 29418 review.example.com sh ls |
| 536 | $ ssh -p 29418 review.example.com sh ps |
| 537 | ---- |
| 538 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 539 | [[simple-configuration]] |
| 540 | Simple Configuration in `gerrit.config` |
| 541 | --------------------------------------- |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 542 | |
| 543 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 544 | If a plugin needs global configuration, this configuration should be |
| 545 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 546 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 547 | This approach of storing the plugin configuration is only suitable for |
| 548 | plugins that have a simple configuration that only consists of |
| 549 | key-value pairs. With this approach it is not possible to have |
| 550 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 551 | configuration need to store their configuration in their |
| 552 | link:#configuration[own configuration file] where they can make use of |
| 553 | subsections. On the other hand storing the plugin configuration in a |
| 554 | 'plugin' subsection in the `gerrit.config` file has the advantage that |
| 555 | administrators have all configuration parameters in one file, instead |
| 556 | of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 557 | |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 558 | To avoid conflicts with other plugins, it is recommended that plugins |
| 559 | only use the `plugin` subsection with their own name. For example the |
| 560 | `helloworld` plugin should store its configuration in the |
| 561 | `plugin.helloworld` subsection: |
| 562 | |
| 563 | ---- |
| 564 | [plugin "helloworld"] |
| 565 | language = Latin |
| 566 | ---- |
| 567 | |
Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 568 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 569 | plugin can easily access its configuration and there is no need for a |
| 570 | plugin to parse the `gerrit.config` file on its own: |
| 571 | |
| 572 | [source,java] |
| 573 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 574 | @Inject |
| 575 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 576 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 577 | [...] |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 578 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 579 | String language = cfg.getFromGerritConfig("helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 580 | .getString("language", "English"); |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 581 | ---- |
| 582 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 583 | [[configuration]] |
| 584 | Configuration in own config file |
| 585 | -------------------------------- |
| 586 | |
| 587 | Plugins can store their configuration in an own configuration file. |
| 588 | This makes sense if the plugin configuration is rather complex and |
| 589 | requires the usage of subsections. Plugins that have a simple |
| 590 | key-value pair configuration can store their configuration in a |
| 591 | link:#simple-configuration[`plugin` subsection of the `gerrit.config` |
| 592 | file]. |
| 593 | |
| 594 | The plugin configuration file must be named after the plugin and must |
| 595 | be located in the `etc` folder of the review site. For example a |
| 596 | configuration file for a `default-reviewer` plugin could look like |
| 597 | this: |
| 598 | |
| 599 | .$site_path/etc/default-reviewer.config |
| 600 | ---- |
| 601 | [branch "refs/heads/master"] |
| 602 | reviewer = Project Owners |
| 603 | reviewer = john.doe@example.com |
| 604 | [match "file:^.*\.txt"] |
| 605 | reviewer = My Info Developers |
| 606 | ---- |
| 607 | |
| 608 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 609 | plugin can easily access its configuration: |
| 610 | |
| 611 | [source,java] |
| 612 | ---- |
| 613 | @Inject |
| 614 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 615 | |
| 616 | [...] |
| 617 | |
| 618 | String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer") |
| 619 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 620 | ---- |
| 621 | |
| 622 | The plugin configuration is loaded only once and is then cached. |
| 623 | Similar to changes in 'gerrit.config', changes to the plugin |
| 624 | configuration file will only become effective after a Gerrit restart. |
| 625 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 626 | [[simple-project-specific-configuration]] |
| 627 | Simple Project Specific Configuration in `project.config` |
| 628 | --------------------------------------------------------- |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 629 | |
| 630 | In Gerrit, project specific configuration is stored in the project's |
| 631 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 632 | needs configuration on project level (e.g. to enable its functionality |
| 633 | only for certain projects), this configuration should be stored in a |
| 634 | `plugin` subsection in the project's `project.config` file. |
| 635 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 636 | This approach of storing the plugin configuration is only suitable for |
| 637 | plugins that have a simple configuration that only consists of |
| 638 | key-value pairs. With this approach it is not possible to have |
| 639 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 640 | configuration need to store their configuration in their |
| 641 | link:#project-specific-configuration[own configuration file] where they |
| 642 | can make use of subsections. On the other hand storing the plugin |
| 643 | configuration in a 'plugin' subsection in the `project.config` file has |
| 644 | the advantage that project owners have all configuration parameters in |
| 645 | one file, instead of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 646 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 647 | To avoid conflicts with other plugins, it is recommended that plugins |
| 648 | only use the `plugin` subsection with their own name. For example the |
| 649 | `helloworld` plugin should store its configuration in the |
| 650 | `plugin.helloworld` subsection: |
| 651 | |
| 652 | ---- |
| 653 | [plugin "helloworld"] |
| 654 | enabled = true |
| 655 | ---- |
| 656 | |
| 657 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 658 | plugin can easily access its project specific configuration and there |
| 659 | is no need for a plugin to parse the `project.config` file on its own: |
| 660 | |
| 661 | [source,java] |
| 662 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 663 | @Inject |
| 664 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 665 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 666 | [...] |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 667 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 668 | boolean enabled = cfg.getFromProjectConfig(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 669 | .getBoolean("enabled", false); |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 670 | ---- |
| 671 | |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 672 | It is also possible to get missing configuration parameters inherited |
| 673 | from the parent projects: |
| 674 | |
| 675 | [source,java] |
| 676 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 677 | @Inject |
| 678 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 679 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 680 | [...] |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 681 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 682 | boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 683 | .getBoolean("enabled", false); |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 684 | ---- |
| 685 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 686 | Project owners can edit the project configuration by fetching the |
| 687 | `refs/meta/config` branch, editing the `project.config` file and |
| 688 | pushing the commit back. |
| 689 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 690 | [[project-specific-configuration]] |
| 691 | Project Specific Configuration in own config file |
| 692 | ------------------------------------------------- |
| 693 | |
| 694 | Plugins can store their project specific configuration in an own |
| 695 | configuration file in the projects `refs/meta/config` branch. |
| 696 | This makes sense if the plugins project specific configuration is |
| 697 | rather complex and requires the usage of subsections. Plugins that |
| 698 | have a simple key-value pair configuration can store their project |
| 699 | specific configuration in a link:#simple-project-specific-configuration[ |
| 700 | `plugin` subsection of the `project.config` file]. |
| 701 | |
| 702 | The plugin configuration file in the `refs/meta/config` branch must be |
| 703 | named after the plugin. For example a configuration file for a |
| 704 | `default-reviewer` plugin could look like this: |
| 705 | |
| 706 | .default-reviewer.config |
| 707 | ---- |
| 708 | [branch "refs/heads/master"] |
| 709 | reviewer = Project Owners |
| 710 | reviewer = john.doe@example.com |
| 711 | [match "file:^.*\.txt"] |
| 712 | reviewer = My Info Developers |
| 713 | ---- |
| 714 | |
| 715 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 716 | plugin can easily access its project specific configuration: |
| 717 | |
| 718 | [source,java] |
| 719 | ---- |
| 720 | @Inject |
| 721 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 722 | |
| 723 | [...] |
| 724 | |
| 725 | String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer") |
| 726 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 727 | ---- |
| 728 | |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 729 | It is also possible to get missing configuration parameters inherited |
| 730 | from the parent projects: |
| 731 | |
| 732 | [source,java] |
| 733 | ---- |
| 734 | @Inject |
| 735 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 736 | |
| 737 | [...] |
| 738 | |
| 739 | String[] reviewers = cfg.getFromPluginConfigWithInheritance(project, "default-reviewer") |
| 740 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 741 | ---- |
| 742 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 743 | Project owners can edit the project configuration by fetching the |
| 744 | `refs/meta/config` branch, editing the `<plugin-name>.config` file and |
| 745 | pushing the commit back. |
| 746 | |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 747 | React on changes in project configuration |
| 748 | ----------------------------------------- |
| 749 | |
| 750 | If a plugin wants to react on changes in the project configuration, it |
| 751 | can implement a `GitReferenceUpdatedListener` and filter on events for |
| 752 | the `refs/meta/config` branch: |
| 753 | |
| 754 | [source,java] |
| 755 | ---- |
| 756 | public class MyListener implements GitReferenceUpdatedListener { |
| 757 | |
| 758 | private final MetaDataUpdate.Server metaDataUpdateFactory; |
| 759 | |
| 760 | @Inject |
| 761 | MyListener(MetaDataUpdate.Server metaDataUpdateFactory) { |
| 762 | this.metaDataUpdateFactory = metaDataUpdateFactory; |
| 763 | } |
| 764 | |
| 765 | @Override |
| 766 | public void onGitReferenceUpdated(Event event) { |
| 767 | if (event.getRefName().equals(GitRepositoryManager.REF_CONFIG)) { |
| 768 | Project.NameKey p = new Project.NameKey(event.getProjectName()); |
| 769 | try { |
| 770 | ProjectConfig oldCfg = |
| 771 | ProjectConfig.read(metaDataUpdateFactory.create(p), |
| 772 | ObjectId.fromString(event.getOldObjectId())); |
| 773 | ProjectConfig newCfg = |
| 774 | ProjectConfig.read(metaDataUpdateFactory.create(p), |
| 775 | ObjectId.fromString(event.getNewObjectId())); |
| 776 | |
| 777 | if (!oldCfg.getProject().getSubmitType().equals( |
| 778 | newCfg.getProject().getSubmitType())) { |
| 779 | // submit type has changed |
| 780 | ... |
| 781 | } |
| 782 | } catch (IOException | ConfigInvalidException e) { |
| 783 | ... |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | ---- |
| 789 | |
| 790 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 791 | [[capabilities]] |
| 792 | Plugin Owned Capabilities |
| 793 | ------------------------- |
| 794 | |
| 795 | Plugins may provide their own capabilities and restrict usage of SSH |
| 796 | commands to the users who are granted those capabilities. |
| 797 | |
| 798 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 799 | abstract class: |
| 800 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 801 | [source,java] |
| 802 | ---- |
| 803 | public class PrintHelloCapability extends CapabilityDefinition { |
| 804 | @Override |
| 805 | public String getDescription() { |
| 806 | return "Print Hello"; |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 807 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 808 | } |
| 809 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 810 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 811 | If no Guice modules are declared in the manifest, UI actions may |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 812 | use auto-registration by providing an `@Export` annotation: |
| 813 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 814 | [source,java] |
| 815 | ---- |
| 816 | @Export("printHello") |
| 817 | public class PrintHelloCapability extends CapabilityDefinition { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 818 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 819 | } |
| 820 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 821 | |
| 822 | Otherwise the capability must be bound in a plugin module: |
| 823 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 824 | [source,java] |
| 825 | ---- |
| 826 | public class HelloWorldModule extends AbstractModule { |
| 827 | @Override |
| 828 | protected void configure() { |
| 829 | bind(CapabilityDefinition.class) |
| 830 | .annotatedWith(Exports.named("printHello")) |
| 831 | .to(PrintHelloCapability.class); |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 832 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 833 | } |
| 834 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 835 | |
| 836 | 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] | 837 | 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] | 838 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 839 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 840 | [source,java] |
| 841 | ---- |
| 842 | @RequiresCapability("printHello") |
| 843 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 844 | public final class PrintHelloWorldCommand extends SshCommand { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 845 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 846 | } |
| 847 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 848 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 849 | Or with `UiAction`: |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 850 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 851 | [source,java] |
| 852 | ---- |
| 853 | @RequiresCapability("printHello") |
| 854 | public class SayHelloAction extends UiAction<RevisionResource> |
| 855 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 856 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 857 | } |
| 858 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 859 | |
| 860 | Capability scope was introduced to differentiate between plugin-owned |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 861 | capabilities and core capabilities. Per default the scope of the |
| 862 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 863 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 864 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 865 | capability is assumed to be that plugin. |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 866 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 867 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 868 | (and thus is outside of a plugin) the scope is the core server and will use |
| 869 | the `GlobalCapability` known to Gerrit Code Review server. |
| 870 | |
| 871 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 872 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 873 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 874 | [source,java] |
| 875 | ---- |
| 876 | @RequiresCapability(value = "administrateServer", scope = |
| 877 | CapabilityScope.CORE) |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 878 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 879 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 880 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 881 | [[ui_extension]] |
| 882 | UI Extension |
| 883 | ------------ |
| 884 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 885 | Plugins can contribute UI actions on core Gerrit pages. This is useful |
| 886 | for workflow customization or exposing plugin functionality through the |
| 887 | UI in addition to SSH commands and the REST API. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 888 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 889 | For instance a plugin to integrate Jira with Gerrit changes may |
| 890 | contribute a "File bug" button to allow filing a bug from the change |
| 891 | page or plugins to integrate continuous integration systems may |
| 892 | contribute a "Schedule" button to allow a CI build to be scheduled |
| 893 | manually from the patch set panel. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 894 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 895 | Two different places on core Gerrit pages are supported: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 896 | |
| 897 | * Change screen |
| 898 | * Project info screen |
| 899 | |
| 900 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 901 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 902 | [source,java] |
| 903 | ---- |
| 904 | @RequiresCapability("printHello") |
| 905 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 906 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 907 | static class Input { |
| 908 | boolean french; |
| 909 | String message; |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 910 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 911 | |
| 912 | private Provider<CurrentUser> user; |
| 913 | |
| 914 | @Inject |
| 915 | HelloWorldAction(Provider<CurrentUser> user) { |
| 916 | this.user = user; |
| 917 | } |
| 918 | |
| 919 | @Override |
| 920 | public String apply(RevisionResource rev, Input input) { |
| 921 | final String greeting = input.french |
| 922 | ? "Bonjour" |
| 923 | : "Hello"; |
| 924 | return String.format("%s %s from change %s, patch set %d!", |
| 925 | greeting, |
| 926 | Strings.isNullOrEmpty(input.message) |
| 927 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 928 | : input.message, |
| 929 | rev.getChange().getId().toString(), |
| 930 | rev.getPatchSet().getPatchSetId()); |
| 931 | } |
| 932 | |
| 933 | @Override |
| 934 | public Description getDescription( |
| 935 | RevisionResource resource) { |
| 936 | return new Description() |
| 937 | .setLabel("Say hello") |
| 938 | .setTitle("Say hello in different languages"); |
| 939 | } |
| 940 | } |
| 941 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 942 | |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 943 | Sometimes plugins may want to be able to change the state of a patch set or |
| 944 | change in the `UiAction.apply()` method and reflect these changes on the core |
| 945 | UI. For example a buildbot plugin which exposes a 'Schedule' button on the |
| 946 | patch set panel may want to disable that button after the build was scheduled |
| 947 | and update the tooltip of that button. But because of Gerrit's caching |
| 948 | strategy the following must be taken into consideration. |
| 949 | |
| 950 | The browser is allowed to cache the `UiAction` information until something on |
| 951 | the change is modified. More accurately the change row needs to be modified in |
| 952 | the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or |
| 953 | the +refs/meta/config+ of the project or any parents needs to change to a new |
| 954 | SHA-1. The ETag SHA-1 computation code can be found in the |
| 955 | `ChangeResource.getETag()` method. |
| 956 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 957 | 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] | 958 | |
| 959 | [source,java] |
| 960 | ---- |
| 961 | @Override |
| 962 | public Object apply(RevisionResource rcrs, Input in) { |
| 963 | // schedule a build |
| 964 | [...] |
| 965 | // update change |
| 966 | ReviewDb db = dbProvider.get(); |
| 967 | db.changes().beginTransaction(change.getId()); |
| 968 | try { |
| 969 | change = db.changes().atomicUpdate( |
| 970 | change.getId(), |
| 971 | new AtomicUpdate<Change>() { |
| 972 | @Override |
| 973 | public Change update(Change change) { |
| 974 | ChangeUtil.updated(change); |
| 975 | return change; |
| 976 | } |
| 977 | }); |
| 978 | db.commit(); |
| 979 | } finally { |
| 980 | db.rollback(); |
| 981 | } |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 982 | [...] |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 983 | } |
| 984 | ---- |
| 985 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 986 | `UiAction` must be bound in a plugin module: |
| 987 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 988 | [source,java] |
| 989 | ---- |
| 990 | public class Module extends AbstractModule { |
| 991 | @Override |
| 992 | protected void configure() { |
| 993 | install(new RestApiModule() { |
| 994 | @Override |
| 995 | protected void configure() { |
| 996 | post(REVISION_KIND, "say-hello") |
| 997 | .to(HelloWorldAction.class); |
| 998 | } |
| 999 | }); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1000 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1001 | } |
| 1002 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1003 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1004 | The module above must be declared in the `pom.xml` for Maven driven |
| 1005 | plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1006 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1007 | [source,xml] |
| 1008 | ---- |
| 1009 | <manifestEntries> |
| 1010 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 1011 | </manifestEntries> |
| 1012 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1013 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1014 | or in the `BUCK` configuration file for Buck driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1015 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1016 | [source,python] |
| 1017 | ---- |
| 1018 | manifest_entries = [ |
| 1019 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 1020 | ] |
| 1021 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1022 | |
| 1023 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 1024 | combined with the JavaScript API. This would display a small popup near the |
| 1025 | activation button to gather additional input from the user. The JS file is |
| 1026 | typically put in the `static` folder within the plugin's directory: |
| 1027 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1028 | [source,javascript] |
| 1029 | ---- |
| 1030 | Gerrit.install(function(self) { |
| 1031 | function onSayHello(c) { |
| 1032 | var f = c.textfield(); |
| 1033 | var t = c.checkbox(); |
| 1034 | var b = c.button('Say hello', {onclick: function(){ |
| 1035 | c.call( |
| 1036 | {message: f.value, french: t.checked}, |
| 1037 | function(r) { |
| 1038 | c.hide(); |
| 1039 | window.alert(r); |
| 1040 | c.refresh(); |
| 1041 | }); |
| 1042 | }}); |
| 1043 | c.popup(c.div( |
| 1044 | c.prependLabel('Greeting message', f), |
| 1045 | c.br(), |
| 1046 | c.label(t, 'french'), |
| 1047 | c.br(), |
| 1048 | b)); |
| 1049 | f.focus(); |
| 1050 | } |
| 1051 | self.onAction('revision', 'say-hello', onSayHello); |
| 1052 | }); |
| 1053 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1054 | |
| 1055 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 1056 | an HTTP Module: |
| 1057 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1058 | [source,java] |
| 1059 | ---- |
| 1060 | public class HttpModule extends HttpPluginModule { |
| 1061 | @Override |
| 1062 | protected void configureServlets() { |
| 1063 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1064 | .toInstance(new JavaScriptPlugin("hello.js")); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1065 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1066 | } |
| 1067 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1068 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1069 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1070 | driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1071 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1072 | [source,xml] |
| 1073 | ---- |
| 1074 | <manifestEntries> |
| 1075 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 1076 | </manifestEntries> |
| 1077 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1078 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1079 | or in the `BUCK` configuration file for Buck driven plugins |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1080 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1081 | [source,python] |
| 1082 | ---- |
| 1083 | manifest_entries = [ |
| 1084 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 1085 | ] |
| 1086 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1087 | |
| 1088 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 1089 | capability check is done during the `UiAction` gathering, so the plugin author |
| 1090 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 1091 | case. |
| 1092 | |
| 1093 | The following prerequisities must be met, to satisfy the capability check: |
| 1094 | |
| 1095 | * user is authenticated |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1096 | * 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] | 1097 | * user is a member of a group which has the required capability |
| 1098 | |
| 1099 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 1100 | combined with JavaScript API (its own JavaScript function is provided), |
| 1101 | then a popup dialog is normally opened to gather additional user input. |
| 1102 | A new button is placed on the popup dialog to actually send the request. |
| 1103 | |
| 1104 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 1105 | can be accessed from any REST client, i. e.: |
| 1106 | |
| 1107 | ==== |
| 1108 | curl -X POST -H "Content-Type: application/json" \ |
| 1109 | -d '{message: "François", french: true}' \ |
| 1110 | --digest --user joe:secret \ |
| 1111 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
| 1112 | "Bonjour François from change 1, patch set 1!" |
| 1113 | ==== |
| 1114 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1115 | 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] | 1116 | particularly useful for `DELETE` requests: |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1117 | |
| 1118 | [source,java] |
| 1119 | ---- |
| 1120 | public class Module extends AbstractModule { |
| 1121 | @Override |
| 1122 | protected void configure() { |
| 1123 | install(new RestApiModule() { |
| 1124 | @Override |
| 1125 | protected void configure() { |
| 1126 | delete(PROJECT_KIND) |
| 1127 | .to(DeleteProject.class); |
| 1128 | } |
| 1129 | }); |
| 1130 | } |
| 1131 | } |
| 1132 | ---- |
| 1133 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1134 | For a `UiAction` bound this way, a JS API function can be provided. |
| 1135 | |
| 1136 | Currently only one restriction exists: per plugin only one `UiAction` |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1137 | can be bound per resource without view name. To define a JS function |
| 1138 | for the `UiAction`, "/" must be used as the name: |
| 1139 | |
| 1140 | [source,javascript] |
| 1141 | ---- |
| 1142 | Gerrit.install(function(self) { |
| 1143 | function onDeleteProject(c) { |
| 1144 | [...] |
| 1145 | } |
| 1146 | self.onAction('project', '/', onDeleteProject); |
| 1147 | }); |
| 1148 | ---- |
| 1149 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1150 | [[top-menu-extensions]] |
| 1151 | Top Menu Extensions |
| 1152 | ------------------- |
| 1153 | |
| 1154 | Plugins can contribute items to Gerrit's top menu. |
| 1155 | |
| 1156 | A single top menu extension can have multiple elements and will be put as |
| 1157 | the last element in Gerrit's top menu. |
| 1158 | |
| 1159 | Plugins define the top menu entries by implementing `TopMenu` interface: |
| 1160 | |
| 1161 | [source,java] |
| 1162 | ---- |
| 1163 | public class MyTopMenuExtension implements TopMenu { |
| 1164 | |
| 1165 | @Override |
| 1166 | public List<MenuEntry> getEntries() { |
| 1167 | return Lists.newArrayList( |
| 1168 | new MenuEntry("Top Menu Entry", Lists.newArrayList( |
| 1169 | new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1170 | } |
| 1171 | } |
| 1172 | ---- |
| 1173 | |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1174 | Plugins can also add additional menu items to Gerrit's top menu entries |
| 1175 | by defining a `MenuEntry` that has the same name as a Gerrit top menu |
| 1176 | entry: |
| 1177 | |
| 1178 | [source,java] |
| 1179 | ---- |
| 1180 | public class MyTopMenuExtension implements TopMenu { |
| 1181 | |
| 1182 | @Override |
| 1183 | public List<MenuEntry> getEntries() { |
| 1184 | return Lists.newArrayList( |
Dariusz Luksza | 2d3afab | 2013-10-01 11:07:13 +0200 | [diff] [blame] | 1185 | new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList( |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1186 | new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/")))); |
| 1187 | } |
| 1188 | } |
| 1189 | ---- |
| 1190 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1191 | If no Guice modules are declared in the manifest, the top menu extension may use |
| 1192 | auto-registration by providing an `@Listen` annotation: |
| 1193 | |
| 1194 | [source,java] |
| 1195 | ---- |
| 1196 | @Listen |
| 1197 | public class MyTopMenuExtension implements TopMenu { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1198 | [...] |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1199 | } |
| 1200 | ---- |
| 1201 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1202 | Otherwise the top menu extension must be bound in the plugin module used |
| 1203 | for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF): |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1204 | |
| 1205 | [source,java] |
| 1206 | ---- |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1207 | package com.googlesource.gerrit.plugins.helloworld; |
| 1208 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1209 | public class HelloWorldModule extends AbstractModule { |
| 1210 | @Override |
| 1211 | protected void configure() { |
| 1212 | DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class); |
| 1213 | } |
| 1214 | } |
| 1215 | ---- |
| 1216 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1217 | [source,manifest] |
| 1218 | ---- |
| 1219 | Gerrit-ApiType: plugin |
| 1220 | Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule |
| 1221 | ---- |
| 1222 | |
Edwin Kempin | b2e926a | 2013-11-11 16:38:30 +0100 | [diff] [blame] | 1223 | It is also possible to show some menu entries only if the user has a |
| 1224 | certain capability: |
| 1225 | |
| 1226 | [source,java] |
| 1227 | ---- |
| 1228 | public class MyTopMenuExtension implements TopMenu { |
| 1229 | private final String pluginName; |
| 1230 | private final Provider<CurrentUser> userProvider; |
| 1231 | private final List<MenuEntry> menuEntries; |
| 1232 | |
| 1233 | @Inject |
| 1234 | public MyTopMenuExtension(@PluginName String pluginName, |
| 1235 | Provider<CurrentUser> userProvider) { |
| 1236 | this.pluginName = pluginName; |
| 1237 | this.userProvider = userProvider; |
| 1238 | menuEntries = new ArrayList<TopMenu.MenuEntry>(); |
| 1239 | |
| 1240 | // add menu entry that is only visible to users with a certain capability |
| 1241 | if (canSeeMenuEntry()) { |
| 1242 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1243 | .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1244 | } |
| 1245 | |
| 1246 | // add menu entry that is visible to all users (even anonymous users) |
| 1247 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1248 | .singletonList(new MenuItem("Documentation", "/plugins/myplugin/")))); |
| 1249 | } |
| 1250 | |
| 1251 | private boolean canSeeMenuEntry() { |
| 1252 | if (userProvider.get().isIdentifiedUser()) { |
| 1253 | CapabilityControl ctl = userProvider.get().getCapabilities(); |
| 1254 | return ctl.canPerform(pluginName + "-" + MyCapability.ID) |
| 1255 | || ctl.canAdministrateServer(); |
| 1256 | } else { |
| 1257 | return false; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | @Override |
| 1262 | public List<MenuEntry> getEntries() { |
| 1263 | return menuEntries; |
| 1264 | } |
| 1265 | } |
| 1266 | ---- |
| 1267 | |
Edwin Kempin | 3c024ea | 2013-11-11 10:43:46 +0100 | [diff] [blame] | 1268 | [[gwt_ui_extension]] |
| 1269 | GWT UI Extension |
| 1270 | ---------------- |
| 1271 | Plugins can extend the Gerrit UI with own GWT code. |
| 1272 | |
| 1273 | The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to |
| 1274 | generate a GWT plugin skeleton. How to use the Maven plugin archetypes |
| 1275 | is described in the link:#getting-started[Getting started] section. |
| 1276 | |
| 1277 | The generated GWT plugin has a link:#top-menu-extensions[top menu] that |
| 1278 | opens a GWT dialog box when the user clicks on it. |
| 1279 | |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1280 | In addition to the Gerrit-Plugin API a GWT plugin depends on |
| 1281 | `gerrit-plugin-gwtui`. This dependency must be specified in the |
| 1282 | `pom.xml`: |
| 1283 | |
| 1284 | [source,xml] |
| 1285 | ---- |
| 1286 | <dependency> |
| 1287 | <groupId>com.google.gerrit</groupId> |
| 1288 | <artifactId>gerrit-plugin-gwtui</artifactId> |
| 1289 | <version>${Gerrit-ApiVersion}</version> |
| 1290 | </dependency> |
| 1291 | ---- |
| 1292 | |
| 1293 | A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`, |
| 1294 | that bundles together all the configuration settings of the GWT plugin: |
| 1295 | |
| 1296 | [source,xml] |
| 1297 | ---- |
| 1298 | <?xml version="1.0" encoding="UTF-8"?> |
| 1299 | <module rename-to="hello_gwt_plugin"> |
| 1300 | <!-- Inherit the core Web Toolkit stuff. --> |
| 1301 | <inherits name="com.google.gwt.user.User"/> |
| 1302 | <!-- Other module inherits --> |
| 1303 | <inherits name="com.google.gerrit.Plugin"/> |
| 1304 | <inherits name="com.google.gwt.http.HTTP"/> |
| 1305 | <!-- Using GWT built-in themes adds a number of static --> |
| 1306 | <!-- resources to the plugin. No theme inherits lines were --> |
| 1307 | <!-- added in order to make this plugin as simple as possible --> |
| 1308 | <!-- Specify the app entry point class. --> |
| 1309 | <entry-point class="${package}.client.HelloPlugin"/> |
| 1310 | <stylesheet src="hello.css"/> |
| 1311 | </module> |
| 1312 | ---- |
| 1313 | |
| 1314 | The GWT module must inherit `com.google.gerrit.Plugin` and |
| 1315 | `com.google.gwt.http.HTTP`. |
| 1316 | |
| 1317 | To register the GWT module a `GwtPlugin` needs to be bound. |
| 1318 | |
| 1319 | If no Guice modules are declared in the manifest, the GWT plugin may |
| 1320 | use auto-registration by using the `@Listen` annotation: |
| 1321 | |
| 1322 | [source,java] |
| 1323 | ---- |
| 1324 | @Listen |
| 1325 | public class MyExtension extends GwtPlugin { |
| 1326 | public MyExtension() { |
| 1327 | super("hello_gwt_plugin"); |
| 1328 | } |
| 1329 | } |
| 1330 | ---- |
| 1331 | |
| 1332 | Otherwise the binding must be done in an `HttpModule`: |
| 1333 | |
| 1334 | [source,java] |
| 1335 | ---- |
| 1336 | public class HttpModule extends HttpPluginModule { |
| 1337 | |
| 1338 | @Override |
| 1339 | protected void configureServlets() { |
| 1340 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1341 | .toInstance(new GwtPlugin("hello_gwt_plugin")); |
| 1342 | } |
| 1343 | } |
| 1344 | ---- |
| 1345 | |
| 1346 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1347 | driven plugins: |
| 1348 | |
| 1349 | [source,xml] |
| 1350 | ---- |
| 1351 | <manifestEntries> |
| 1352 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule> |
| 1353 | </manifestEntries> |
| 1354 | ---- |
| 1355 | |
| 1356 | It is important that the module name that is provided to the |
| 1357 | `GwtPlugin` matches the GWT module contained in the plugin. The name |
| 1358 | of the GWT module can be explicitly set in the GWT module file by |
| 1359 | specifying the `rename-to` attribute on the module. |
| 1360 | |
| 1361 | [source,xml] |
| 1362 | ---- |
| 1363 | <module rename-to="hello_gwt_plugin"> |
| 1364 | ---- |
| 1365 | |
| 1366 | The actual GWT code must be implemented in a class that extends |
| 1367 | `com.google.gerrit.plugin.client.Plugin`: |
| 1368 | |
| 1369 | [source,java] |
| 1370 | ---- |
| 1371 | public class HelloPlugin extends Plugin { |
| 1372 | |
| 1373 | @Override |
| 1374 | public void onModuleLoad() { |
| 1375 | // Create the dialog box |
| 1376 | final DialogBox dialogBox = new DialogBox(); |
| 1377 | |
| 1378 | // The content of the dialog comes from a User specified Preference |
| 1379 | dialogBox.setText("Hello from GWT Gerrit UI plugin"); |
| 1380 | dialogBox.setAnimationEnabled(true); |
| 1381 | Button closeButton = new Button("Close"); |
| 1382 | VerticalPanel dialogVPanel = new VerticalPanel(); |
| 1383 | dialogVPanel.setWidth("100%"); |
| 1384 | dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); |
| 1385 | dialogVPanel.add(closeButton); |
| 1386 | |
| 1387 | closeButton.addClickHandler(new ClickHandler() { |
| 1388 | public void onClick(ClickEvent event) { |
| 1389 | dialogBox.hide(); |
| 1390 | } |
| 1391 | }); |
| 1392 | |
| 1393 | // Set the contents of the Widget |
| 1394 | dialogBox.setWidget(dialogVPanel); |
| 1395 | |
| 1396 | RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID); |
| 1397 | rootPanel.getElement().removeAttribute("href"); |
| 1398 | rootPanel.addDomHandler(new ClickHandler() { |
| 1399 | @Override |
| 1400 | public void onClick(ClickEvent event) { |
| 1401 | dialogBox.center(); |
| 1402 | dialogBox.show(); |
| 1403 | } |
| 1404 | }, ClickEvent.getType()); |
| 1405 | } |
| 1406 | } |
| 1407 | ---- |
| 1408 | |
| 1409 | This class must be set as entry point in the GWT module: |
| 1410 | |
| 1411 | [source,xml] |
| 1412 | ---- |
| 1413 | <entry-point class="${package}.client.HelloPlugin"/> |
| 1414 | ---- |
| 1415 | |
| 1416 | In addition this class must be defined as module in the `pom.xml` for the |
| 1417 | `gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin` |
| 1418 | must be set to `${project.build.directory}/classes/static`: |
| 1419 | |
| 1420 | [source,xml] |
| 1421 | ---- |
| 1422 | <plugin> |
| 1423 | <groupId>org.codehaus.mojo</groupId> |
| 1424 | <artifactId>gwt-maven-plugin</artifactId> |
| 1425 | <version>2.5.1</version> |
| 1426 | <configuration> |
| 1427 | <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module> |
| 1428 | <disableClassMetadata>true</disableClassMetadata> |
| 1429 | <disableCastChecking>true</disableCastChecking> |
| 1430 | <webappDirectory>${project.build.directory}/classes/static</webappDirectory> |
| 1431 | </configuration> |
| 1432 | <executions> |
| 1433 | <execution> |
| 1434 | <goals> |
| 1435 | <goal>compile</goal> |
| 1436 | </goals> |
| 1437 | </execution> |
| 1438 | </executions> |
| 1439 | </plugin> |
| 1440 | ---- |
| 1441 | |
| 1442 | To attach a GWT widget defined by the plugin to the Gerrit core UI |
| 1443 | `com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the |
| 1444 | Gerrit core widgets: |
| 1445 | |
| 1446 | [source,java] |
| 1447 | ---- |
| 1448 | RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID); |
| 1449 | rootPanel.getElement().removeAttribute("href"); |
| 1450 | rootPanel.addDomHandler(new ClickHandler() { |
| 1451 | @Override |
| 1452 | public void onClick(ClickEvent event) { |
| 1453 | dialogBox.center(); |
| 1454 | dialogBox.show(); |
| 1455 | } |
| 1456 | }, ClickEvent.getType()); |
| 1457 | ---- |
| 1458 | |
| 1459 | GWT plugins can come with their own css file. This css file must have a |
| 1460 | unique name and must be registered in the GWT module: |
| 1461 | |
| 1462 | [source,xml] |
| 1463 | ---- |
| 1464 | <stylesheet src="hello.css"/> |
| 1465 | ---- |
| 1466 | |
Edwin Kempin | 2570b10 | 2013-11-11 11:44:50 +0100 | [diff] [blame] | 1467 | If a GWT plugin wants to invoke the Gerrit REST API it can use |
| 1468 | `com.google.gerrit.plugin.client.rpc.RestApi` to contruct the URL |
| 1469 | path and to trigger the REST calls. |
| 1470 | |
| 1471 | Example for invoking a Gerrit core REST endpoint: |
| 1472 | |
| 1473 | [source,java] |
| 1474 | ---- |
| 1475 | new RestApi("projects").id(projectName).view("description") |
| 1476 | .put("new description", new AsyncCallback<JavaScriptObject>() { |
| 1477 | |
| 1478 | @Override |
| 1479 | public void onSuccess(JavaScriptObject result) { |
| 1480 | // TODO |
| 1481 | } |
| 1482 | |
| 1483 | @Override |
| 1484 | public void onFailure(Throwable caught) { |
| 1485 | // never invoked |
| 1486 | } |
| 1487 | }); |
| 1488 | ---- |
| 1489 | |
| 1490 | Example for invoking a REST endpoint defined by a plugin: |
| 1491 | |
| 1492 | [source,java] |
| 1493 | ---- |
| 1494 | new RestApi("projects").id(projectName).view("myplugin", "myview") |
| 1495 | .get(new AsyncCallback<JavaScriptObject>() { |
| 1496 | |
| 1497 | @Override |
| 1498 | public void onSuccess(JavaScriptObject result) { |
| 1499 | // TODO |
| 1500 | } |
| 1501 | |
| 1502 | @Override |
| 1503 | public void onFailure(Throwable caught) { |
| 1504 | // never invoked |
| 1505 | } |
| 1506 | }); |
| 1507 | ---- |
| 1508 | |
| 1509 | The `onFailure(Throwable)` of the provided callback is never invoked. |
| 1510 | If an error occurs, it is shown in an error dialog. |
| 1511 | |
| 1512 | In order to be able to do REST calls the GWT module must inherit |
| 1513 | `com.google.gwt.json.JSON`: |
| 1514 | |
| 1515 | [source,xml] |
| 1516 | ---- |
| 1517 | <inherits name="com.google.gwt.json.JSON"/> |
| 1518 | ---- |
| 1519 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1520 | [[http]] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1521 | HTTP Servlets |
| 1522 | ------------- |
| 1523 | |
| 1524 | Plugins or extensions may register additional HTTP servlets, and |
| 1525 | wrap them with HTTP filters. |
| 1526 | |
| 1527 | Servlets may use auto-registration to declare the URL they handle: |
| 1528 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1529 | [source,java] |
| 1530 | ---- |
| 1531 | import com.google.gerrit.extensions.annotations.Export; |
| 1532 | import com.google.inject.Singleton; |
| 1533 | import javax.servlet.http.HttpServlet; |
| 1534 | import javax.servlet.http.HttpServletRequest; |
| 1535 | import javax.servlet.http.HttpServletResponse; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1536 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1537 | @Export("/print") |
| 1538 | @Singleton |
| 1539 | class HelloServlet extends HttpServlet { |
| 1540 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 1541 | res.setContentType("text/plain"); |
| 1542 | res.setCharacterEncoding("UTF-8"); |
| 1543 | res.getWriter().write("Hello"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1544 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1545 | } |
| 1546 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1547 | |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1548 | The auto registration only works for standard servlet mappings like |
| 1549 | `/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule |
| 1550 | to register the HTTP servlets and declare it explicitly in the manifest |
| 1551 | with the `Gerrit-HttpModule` attribute: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1552 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1553 | [source,java] |
| 1554 | ---- |
| 1555 | import com.google.inject.servlet.ServletModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1556 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1557 | class MyWebUrls extends ServletModule { |
| 1558 | protected void configureServlets() { |
| 1559 | serve("/print").with(HelloServlet.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1560 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1561 | } |
| 1562 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1563 | |
| 1564 | For a plugin installed as name `helloworld`, the servlet implemented |
| 1565 | by HelloServlet class will be available to users as: |
| 1566 | |
| 1567 | ---- |
| 1568 | $ curl http://review.example.com/plugins/helloworld/print |
| 1569 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1570 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1571 | [[data-directory]] |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1572 | Data Directory |
| 1573 | -------------- |
| 1574 | |
| 1575 | Plugins can request a data directory with a `@PluginData` File |
| 1576 | dependency. A data directory will be created automatically by the |
| 1577 | server in `$site_path/data/$plugin_name` and passed to the plugin. |
| 1578 | |
| 1579 | Plugins can use this to store any data they want. |
| 1580 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1581 | [source,java] |
| 1582 | ---- |
| 1583 | @Inject |
| 1584 | MyType(@PluginData java.io.File myDir) { |
| 1585 | new FileInputStream(new File(myDir, "my.config")); |
| 1586 | } |
| 1587 | ---- |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1588 | |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 1589 | [[download-commands]] |
| 1590 | Download Commands |
| 1591 | ----------------- |
| 1592 | |
| 1593 | Gerrit offers commands for downloading changes using different |
| 1594 | download schemes (e.g. for downloading via different network |
| 1595 | protocols). Plugins can contribute download schemes and download |
| 1596 | commands by implementing |
| 1597 | `com.google.gerrit.extensions.config.DownloadScheme` and |
| 1598 | `com.google.gerrit.extensions.config.DownloadCommand`. |
| 1599 | |
| 1600 | The download schemes and download commands which are used most often |
| 1601 | are provided by the Gerrit core plugin `download-commands`. |
| 1602 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1603 | [[documentation]] |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1604 | Documentation |
| 1605 | ------------- |
| 1606 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1607 | If a plugin does not register a filter or servlet to handle URLs |
| 1608 | `/Documentation/*` or `/static/*`, the core Gerrit server will |
| 1609 | automatically export these resources over HTTP from the plugin JAR. |
| 1610 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1611 | Static resources under the `static/` directory in the JAR will be |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 1612 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 1613 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1614 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1615 | Documentation files under the `Documentation/` directory in the JAR |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 1616 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 1617 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 1618 | attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1619 | |
| 1620 | Documentation may be written in |
| 1621 | link:http://daringfireball.net/projects/markdown/[Markdown] style |
| 1622 | if the file name ends with `.md`. Gerrit will automatically convert |
| 1623 | Markdown to HTML if accessed with extension `.html`. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1624 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1625 | [[macros]] |
Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 1626 | Within the Markdown documentation files macros can be used that allow |
| 1627 | to write documentation with reasonably accurate examples that adjust |
| 1628 | automatically based on the installation. |
| 1629 | |
| 1630 | The following macros are supported: |
| 1631 | |
| 1632 | [width="40%",options="header"] |
| 1633 | |=================================================== |
| 1634 | |Macro | Replacement |
| 1635 | |@PLUGIN@ | name of the plugin |
| 1636 | |@URL@ | Gerrit Web URL |
| 1637 | |@SSH_HOST@ | SSH Host |
| 1638 | |@SSH_PORT@ | SSH Port |
| 1639 | |=================================================== |
| 1640 | |
| 1641 | The macros will be replaced when the documentation files are rendered |
| 1642 | from Markdown to HTML. |
| 1643 | |
| 1644 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 1645 | even if there is an expansion for `KEEP` in the future. |
| 1646 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1647 | [[auto-index]] |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1648 | Automatic Index |
| 1649 | ~~~~~~~~~~~~~~~ |
| 1650 | |
| 1651 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 1652 | redirect clients to the plugin's `/Documentation/index.html`. |
| 1653 | Requests for `/Documentation/` (bare directory) will also redirect |
| 1654 | to `/Documentation/index.html`. |
| 1655 | |
| 1656 | If neither resource `Documentation/index.html` or |
| 1657 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
| 1658 | automatically generate an index page for the plugin's documentation |
| 1659 | tree by scanning every `*.md` and `*.html` file in the Documentation/ |
| 1660 | directory. |
| 1661 | |
| 1662 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 1663 | header of the file and extract the first level one title. This |
| 1664 | title text will be used as display text for a link to the HTML |
| 1665 | version of the page. |
| 1666 | |
| 1667 | For any discovered HTML (`*.html`) file, Gerrit will use the name |
| 1668 | of the file, minus the `*.html` extension, as the link text. Any |
| 1669 | hyphens in the file name will be replaced with spaces. |
| 1670 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1671 | If a discovered file is named `about.md` or `about.html`, its |
| 1672 | content will be inserted in an 'About' section at the top of the |
| 1673 | auto-generated index page. If both `about.md` and `about.html` |
| 1674 | exist, only the first discovered file will be used. |
| 1675 | |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1676 | If a discovered file name beings with `cmd-` it will be clustered |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1677 | into a 'Commands' section of the generated index page. |
| 1678 | |
David Pursehouse | fe52915 | 2013-08-14 16:35:06 +0900 | [diff] [blame] | 1679 | If a discovered file name beings with `servlet-` it will be clustered |
| 1680 | into a 'Servlets' section of the generated index page. |
| 1681 | |
| 1682 | If a discovered file name beings with `rest-api-` it will be clustered |
| 1683 | into a 'REST APIs' section of the generated index page. |
| 1684 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1685 | All other files are clustered under a 'Documentation' section. |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1686 | |
| 1687 | Some optional information from the manifest is extracted and |
| 1688 | displayed as part of the index page, if present in the manifest: |
| 1689 | |
| 1690 | [width="40%",options="header"] |
| 1691 | |=================================================== |
| 1692 | |Field | Source Attribute |
| 1693 | |Name | Implementation-Title |
| 1694 | |Vendor | Implementation-Vendor |
| 1695 | |Version | Implementation-Version |
| 1696 | |URL | Implementation-URL |
| 1697 | |API Version | Gerrit-ApiVersion |
| 1698 | |=================================================== |
| 1699 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1700 | [[deployment]] |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1701 | Deployment |
| 1702 | ---------- |
| 1703 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 1704 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 1705 | server using the link:cmd-plugin-install.html[plugin install] command. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1706 | |
Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame^] | 1707 | WebUI plugins distributed as single `.js` file can be deployed |
| 1708 | without the overhead of JAR packaging, for more information refer to |
| 1709 | link:cmd-plugin-install.html[plugin install] command. |
| 1710 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1711 | Plugins can also be copied directly into the server's |
Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame^] | 1712 | directory at `$site_path/plugins/$name.(jar|js)`. The name of |
| 1713 | the JAR file, minus the `.jar` or `.js` extension, will be used as the |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1714 | plugin name. Unless disabled, servers periodically scan this |
| 1715 | directory for updated plugins. The time can be adjusted by |
| 1716 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1717 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 1718 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 1719 | command can be used. |
| 1720 | |
Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 1721 | Disabled plugins can be re-enabled using the |
| 1722 | link:cmd-plugin-enable.html[plugin enable] command. |
| 1723 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1724 | SEE ALSO |
| 1725 | -------- |
| 1726 | |
| 1727 | * link:js-api.html[JavaScript API] |
| 1728 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 1729 | |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1730 | GERRIT |
| 1731 | ------ |
| 1732 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 1733 | |
| 1734 | SEARCHBOX |
| 1735 | --------- |