| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1 | = Gerrit Code Review - Plugin Development |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2 | |
| Edwin Kempin | af27532 | 2012-07-16 11:04:01 +0200 | [diff] [blame] | 3 | The Gerrit server functionality can be extended by installing plugins. |
| 4 | This page describes how plugins for Gerrit can be developed. |
| 5 | |
| 6 | Depending on how tightly the extension code is coupled with the Gerrit |
| 7 | server code, there is a distinction between `plugins` and `extensions`. |
| 8 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 9 | [[plugin]] |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 10 | 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] | 11 | JVM as Gerrit. It has full access to all server internals. Plugins |
| 12 | are tightly coupled to a specific major.minor server version and |
| 13 | may require source code changes to compile against a different |
| 14 | server version. |
| 15 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 16 | [[extension]] |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 17 | 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] | 18 | 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] | 19 | server's internals. The limited visibility reduces the extension's |
| 20 | dependencies, enabling it to be compatible across a wider range |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 21 | of server versions. |
| 22 | |
| 23 | Most of this documentation refers to either type as a plugin. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 24 | |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 25 | [[getting-started]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 26 | == Getting started |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 27 | |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 28 | To get started with the development of a plugin there are two |
| 29 | recommended ways: |
| Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 30 | |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 31 | . use the Gerrit Plugin Maven archetype to create a new plugin project: |
| 32 | + |
| 33 | With the Gerrit Plugin Maven archetype you can create a skeleton for a |
| 34 | plugin project. |
| 35 | + |
| 36 | ---- |
| 37 | mvn archetype:generate -DarchetypeGroupId=com.google.gerrit \ |
| 38 | -DarchetypeArtifactId=gerrit-plugin-archetype \ |
| Edwin Kempin | 10ee9f3 | 2014-04-14 09:25:30 +0200 | [diff] [blame] | 39 | -DarchetypeVersion=2.10-SNAPSHOT \ |
| Edwin Kempin | 441a0b8 | 2014-06-04 14:56:23 +0200 | [diff] [blame] | 40 | -DarchetypeRepository=https://oss.sonatype.org/content/repositories/snapshots/ \ |
| Edwin Kempin | 91155c2 | 2013-12-02 20:25:18 +0100 | [diff] [blame] | 41 | -DgroupId=com.googlesource.gerrit.plugins.testplugin \ |
| 42 | -DartifactId=testplugin |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 43 | ---- |
| 44 | + |
| 45 | Maven will ask for additional properties and then create the plugin in |
| 46 | the current directory. To change the default property values answer 'n' |
| 47 | when Maven asks to confirm the properties configuration. It will then |
| 48 | ask again for all properties including those with predefined default |
| 49 | values. |
| 50 | |
| David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 51 | . clone the sample plugin: |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 52 | + |
| David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 53 | This is a project that demonstrates the various features of the |
| 54 | 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] | 55 | + |
| Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 56 | ---- |
| David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 57 | $ git clone https://gerrit.googlesource.com/plugins/cookbook-plugin |
| Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 58 | ---- |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 59 | + |
| 60 | When starting from this example one should take care to adapt the |
| 61 | `Gerrit-ApiVersion` in the `pom.xml` to the version of Gerrit for which |
| 62 | the plugin is developed. If the plugin is developed for a released |
| 63 | Gerrit version (no `SNAPSHOT` version) then the URL for the |
| 64 | `gerrit-api-repository` in the `pom.xml` needs to be changed to |
| Shawn Pearce | d500500 | 2013-06-21 11:01:45 -0700 | [diff] [blame] | 65 | `https://gerrit-api.storage.googleapis.com/release/`. |
| Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 66 | |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 67 | [[API]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 68 | == API |
| Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 69 | |
| 70 | There are two different API formats offered against which plugins can |
| 71 | be developed: |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 72 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 73 | gerrit-extension-api.jar:: |
| 74 | A stable but thin interface. Suitable for extensions that need |
| 75 | to be notified of events, but do not require tight coupling to |
| 76 | the internals of Gerrit. Extensions built against this API can |
| 77 | expect to be binary compatible across a wide range of server |
| 78 | versions. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 79 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 80 | gerrit-plugin-api.jar:: |
| 81 | The complete internals of the Gerrit server, permitting a |
| 82 | plugin to tightly couple itself and provide additional |
| 83 | functionality that is not possible as an extension. Plugins |
| 84 | built against this API are expected to break at the source |
| 85 | code level between every major.minor Gerrit release. A plugin |
| 86 | that compiles against 2.5 will probably need source code level |
| 87 | changes to work with 2.6, 2.7, and so on. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 88 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 89 | == Manifest |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 90 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 91 | Plugins may provide optional description information with standard |
| 92 | manifest fields: |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 93 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 94 | ==== |
| 95 | Implementation-Title: Example plugin showing examples |
| 96 | Implementation-Version: 1.0 |
| 97 | Implementation-Vendor: Example, Inc. |
| 98 | Implementation-URL: http://example.com/opensource/plugin-foo/ |
| 99 | ==== |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 100 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 101 | === ApiType |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 102 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 103 | Plugins using the tightly coupled `gerrit-plugin-api.jar` must |
| 104 | declare this API dependency in the manifest to gain access to server |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 105 | internals. If no `Gerrit-ApiType` is specified the stable `extension` |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 106 | API will be assumed. This may cause ClassNotFoundExceptions when |
| 107 | loading a plugin that needs the plugin API. |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 108 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 109 | ==== |
| 110 | Gerrit-ApiType: plugin |
| 111 | ==== |
| 112 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 113 | === Explicit Registration |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 114 | |
| 115 | Plugins that use explicit Guice registration must name the Guice |
| 116 | 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] | 117 | manifest. `Gerrit-Module` supplies bindings to the core server; |
| 118 | `Gerrit-SshModule` supplies SSH commands to the SSH server (if |
| 119 | enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 120 | server (if enabled). If no modules are named automatic registration |
| 121 | will be performed by scanning all classes in the plugin JAR for |
| 122 | `@Listen` and `@Export("")` annotations. |
| 123 | |
| 124 | ==== |
| 125 | Gerrit-Module: tld.example.project.CoreModuleClassName |
| 126 | Gerrit-SshModule: tld.example.project.SshModuleClassName |
| 127 | Gerrit-HttpModule: tld.example.project.HttpModuleClassName |
| 128 | ==== |
| 129 | |
| David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 130 | [[plugin_name]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 131 | === Plugin Name |
| David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 132 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 133 | A plugin can optionally provide its own plugin name. |
| David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 134 | |
| 135 | ==== |
| 136 | Gerrit-PluginName: replication |
| 137 | ==== |
| 138 | |
| 139 | This is useful for plugins that contribute plugin-owned capabilities that |
| 140 | are stored in the `project.config` file. Another use case is to be able to put |
| 141 | project specific plugin configuration section in `project.config`. In this |
| 142 | case it is advantageous to reserve the plugin name to access the configuration |
| 143 | section in the `project.config` file. |
| 144 | |
| 145 | If `Gerrit-PluginName` is omitted, then the plugin's name is determined from |
| 146 | the plugin file name. |
| 147 | |
| 148 | If a plugin provides its own name, then that plugin cannot be deployed |
| 149 | multiple times under different file names on one Gerrit site. |
| 150 | |
| 151 | For Maven driven plugins, the following line must be included in the pom.xml |
| 152 | file: |
| 153 | |
| 154 | [source,xml] |
| 155 | ---- |
| 156 | <manifestEntries> |
| 157 | <Gerrit-PluginName>name</Gerrit-PluginName> |
| 158 | </manifestEntries> |
| 159 | ---- |
| 160 | |
| 161 | For Buck driven plugins, the following line must be included in the BUCK |
| 162 | configuration file: |
| 163 | |
| 164 | [source,python] |
| 165 | ---- |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 166 | manifest_entries = [ |
| 167 | 'Gerrit-PluginName: name', |
| 168 | ] |
| David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 169 | ---- |
| 170 | |
| Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 171 | A plugin can get its own name injected at runtime: |
| 172 | |
| 173 | [source,java] |
| 174 | ---- |
| 175 | public class MyClass { |
| 176 | |
| 177 | private final String pluginName; |
| 178 | |
| 179 | @Inject |
| 180 | public MyClass(@PluginName String pluginName) { |
| 181 | this.pluginName = pluginName; |
| 182 | } |
| 183 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 184 | [...] |
| Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 185 | } |
| 186 | ---- |
| 187 | |
| David Pursehouse | 8ed0d92 | 2013-10-18 18:57:56 +0900 | [diff] [blame] | 188 | A plugin can get its canonical web URL injected at runtime: |
| 189 | |
| 190 | [source,java] |
| 191 | ---- |
| 192 | public class MyClass { |
| 193 | |
| 194 | private final String url; |
| 195 | |
| 196 | @Inject |
| 197 | public MyClass(@PluginCanonicalWebUrl String url) { |
| 198 | this.url = url; |
| 199 | } |
| 200 | |
| 201 | [...] |
| 202 | } |
| 203 | ---- |
| 204 | |
| 205 | The URL is composed of the server's canonical web URL and the plugin's |
| 206 | name, i.e. `http://review.example.com:8080/plugin-name`. |
| 207 | |
| 208 | The canonical web URL may be injected into any .jar plugin regardless of |
| 209 | whether or not the plugin provides an HTTP servlet. |
| 210 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 211 | [[reload_method]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 212 | === Reload Method |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 213 | |
| 214 | If a plugin holds an exclusive resource that must be released before |
| 215 | loading the plugin again (for example listening on a network port or |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 216 | acquiring a file lock) the manifest must declare `Gerrit-ReloadMode` |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 217 | to be `restart`. Otherwise the preferred method of `reload` will |
| 218 | be used, as it enables the server to hot-patch an updated plugin |
| 219 | with no down time. |
| 220 | |
| 221 | ==== |
| 222 | Gerrit-ReloadMode: restart |
| 223 | ==== |
| 224 | |
| 225 | In either mode ('restart' or 'reload') any plugin or extension can |
| 226 | be updated without restarting the Gerrit server. The difference is |
| 227 | how Gerrit handles the upgrade: |
| 228 | |
| 229 | restart:: |
| 230 | The old plugin is completely stopped. All registrations of SSH |
| 231 | commands and HTTP servlets are removed. All registrations of any |
| 232 | extension points are removed. All registered LifecycleListeners |
| 233 | have their `stop()` method invoked in reverse order. The new |
| 234 | plugin is started, and registrations are made from the new |
| 235 | plugin. There is a brief window where neither the old nor the |
| 236 | new plugin is connected to the server. This means SSH commands |
| 237 | and HTTP servlets will return not found errors, and the plugin |
| 238 | will not be notified of events that occurred during the restart. |
| 239 | |
| 240 | reload:: |
| 241 | The new plugin is started. Its LifecycleListeners are permitted |
| 242 | to perform their `start()` methods. All SSH and HTTP registrations |
| 243 | are atomically swapped out from the old plugin to the new plugin, |
| 244 | ensuring the server never returns a not found error. All extension |
| 245 | point listeners are atomically swapped out from the old plugin to |
| 246 | the new plugin, ensuring no events are missed (however some events |
| 247 | may still route to the old plugin if the swap wasn't complete yet). |
| 248 | The old plugin is stopped. |
| 249 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 250 | To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload] |
| 251 | command can be used. |
| 252 | |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 253 | [[init_step]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 254 | === Init step |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 255 | |
| 256 | Plugins can contribute their own "init step" during the Gerrit init |
| 257 | wizard. This is useful for guiding the Gerrit administrator through |
| David Pursehouse | 659860f | 2013-12-16 14:50:04 +0900 | [diff] [blame] | 258 | the settings needed by the plugin to work properly. |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 259 | |
| 260 | For instance plugins to integrate Jira issues to Gerrit changes may |
| 261 | contribute their own "init step" to allow configuring the Jira URL, |
| 262 | credentials and possibly verify connectivity to validate them. |
| 263 | |
| 264 | ==== |
| 265 | Gerrit-InitStep: tld.example.project.MyInitStep |
| 266 | ==== |
| 267 | |
| 268 | MyInitStep needs to follow the standard Gerrit InitStep syntax |
| David Pursehouse | 9246356 | 2013-06-24 10:16:28 +0900 | [diff] [blame] | 269 | and behavior: writing to the console using the injected ConsoleUI |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 270 | and accessing / changing configuration settings using Section.Factory. |
| 271 | |
| 272 | In addition to the standard Gerrit init injections, plugins receive |
| 273 | the @PluginName String injection containing their own plugin name. |
| 274 | |
| Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 275 | During their initialization plugins may get access to the |
| 276 | `project.config` file of the `All-Projects` project and they are able |
| 277 | to store configuration parameters in it. For this a plugin `InitStep` |
| 278 | can get `com.google.gerrit.pgm.init.AllProjectsConfig` injected: |
| 279 | |
| 280 | [source,java] |
| 281 | ---- |
| 282 | public class MyInitStep implements InitStep { |
| 283 | private final String pluginName; |
| 284 | private final ConsoleUI ui; |
| 285 | private final AllProjectsConfig allProjectsConfig; |
| 286 | |
| 287 | public MyInitStep(@PluginName String pluginName, ConsoleUI ui, |
| 288 | AllProjectsConfig allProjectsConfig) { |
| 289 | this.pluginName = pluginName; |
| 290 | this.ui = ui; |
| 291 | this.allProjectsConfig = allProjectsConfig; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void run() throws Exception { |
| Edwin Kempin | 93e7d5d | 2014-01-03 09:53:20 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void postRun() throws Exception { |
| Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 300 | ui.message("\n"); |
| 301 | ui.header(pluginName + " Integration"); |
| 302 | boolean enabled = ui.yesno(true, "By default enabled for all projects"); |
| Adrian Görler | d161297 | 2014-10-20 17:06:07 +0200 | [diff] [blame] | 303 | Config cfg = allProjectsConfig.load().getConfig(); |
| Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 304 | if (enabled) { |
| 305 | cfg.setBoolean("plugin", pluginName, "enabled", enabled); |
| 306 | } else { |
| 307 | cfg.unset("plugin", pluginName, "enabled"); |
| 308 | } |
| 309 | allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration"); |
| 310 | } |
| 311 | } |
| 312 | ---- |
| 313 | |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 314 | Bear in mind that the Plugin's InitStep class will be loaded but |
| 315 | the standard Gerrit runtime environment is not available and the plugin's |
| 316 | own Guice modules were not initialized. |
| 317 | This means the InitStep for a plugin is not executed in the same way that |
| 318 | the plugin executes within the server, and may mean a plugin author cannot |
| 319 | trivially reuse runtime code during init. |
| 320 | |
| 321 | For instance a plugin that wants to verify connectivity may need to statically |
| 322 | call the constructor of their connection class, passing in values obtained |
| 323 | from the Section.Factory rather than from an injected Config object. |
| 324 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 325 | Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after |
| 326 | the extraction of the plugins embedded in the distribution .war file into |
| 327 | `$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade. |
| 328 | |
| 329 | A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit |
| 330 | runtime objects injected at startup. |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 331 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 332 | [source,java] |
| 333 | ---- |
| 334 | public class MyInitStep implements InitStep { |
| 335 | private final ConsoleUI ui; |
| 336 | private final Section.Factory sections; |
| 337 | private final String pluginName; |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 338 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 339 | @Inject |
| 340 | public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) { |
| 341 | this.ui = ui; |
| 342 | this.sections = sections; |
| 343 | this.pluginName = pluginName; |
| Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 344 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 345 | |
| 346 | @Override |
| 347 | public void run() throws Exception { |
| 348 | ui.header("\nMy plugin"); |
| 349 | |
| 350 | Section mySection = getSection("myplugin", null); |
| 351 | mySection.string("Link name", "linkname", "MyLink"); |
| 352 | } |
| Edwin Kempin | 93e7d5d | 2014-01-03 09:53:20 +0100 | [diff] [blame] | 353 | |
| 354 | @Override |
| 355 | public void postRun() throws Exception { |
| 356 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 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]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 361 | == Classpath |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 362 | |
| 363 | Each plugin is loaded into its own ClassLoader, isolating plugins |
| 364 | from each other. A plugin or extension inherits the Java runtime |
| 365 | and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin) |
| 366 | from the hosting server. |
| 367 | |
| 368 | Plugins are loaded from a single JAR file. If a plugin needs |
| 369 | additional libraries, it must include those dependencies within |
| 370 | its own JAR. Plugins built using Maven may be able to use the |
| 371 | link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin] |
| 372 | to package additional dependencies. Relocating (or renaming) classes |
| 373 | should not be necessary due to the ClassLoader isolation. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 374 | |
| Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 375 | [[events]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 376 | == Listening to Events |
| Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 377 | |
| 378 | Certain operations in Gerrit trigger events. Plugins may receive |
| 379 | notifications of these events by implementing the corresponding |
| 380 | listeners. |
| 381 | |
| Martin Fick | 4c72aea | 2014-12-10 14:58:12 -0700 | [diff] [blame^] | 382 | * `com.google.gerrit.common.EventListener`: |
| Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 383 | + |
| Martin Fick | 4c72aea | 2014-12-10 14:58:12 -0700 | [diff] [blame^] | 384 | Allows to listen to events. These are the same |
| Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 385 | link:cmd-stream-events.html#events[events] that are also streamed by |
| 386 | the link:cmd-stream-events.html[gerrit stream-events] command. |
| 387 | |
| Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 388 | * `com.google.gerrit.extensions.events.LifecycleListener`: |
| 389 | + |
| Edwin Kempin | 3e7928a | 2013-12-03 07:39:00 +0100 | [diff] [blame] | 390 | Plugin start and stop |
| Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 391 | |
| 392 | * `com.google.gerrit.extensions.events.NewProjectCreatedListener`: |
| 393 | + |
| 394 | Project creation |
| 395 | |
| 396 | * `com.google.gerrit.extensions.events.ProjectDeletedListener`: |
| 397 | + |
| 398 | Project deletion |
| 399 | |
| Edwin Kempin | b27c939 | 2013-11-19 13:12:43 +0100 | [diff] [blame] | 400 | * `com.google.gerrit.extensions.events.HeadUpdatedListener`: |
| 401 | + |
| 402 | Update of HEAD on a project |
| 403 | |
| Stefan Lay | 310d77d | 2014-05-28 13:45:25 +0200 | [diff] [blame] | 404 | * `com.google.gerrit.extensions.events.UsageDataPublishedListener`: |
| 405 | + |
| 406 | Publication of usage data |
| 407 | |
| Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 408 | [[stream-events]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 409 | == Sending Events to the Events Stream |
| Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 410 | |
| 411 | Plugins may send events to the events stream where consumers of |
| 412 | Gerrit's `stream-events` ssh command will receive them. |
| 413 | |
| 414 | To send an event, the plugin must invoke one of the `postEvent` |
| 415 | methods in the `ChangeHookRunner` class, passing an instance of |
| Martin Fick | 4c72aea | 2014-12-10 14:58:12 -0700 | [diff] [blame^] | 416 | its own custom event class derived from |
| 417 | `com.google.gerrit.server.events.Event`. |
| Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 418 | |
| Edwin Kempin | 3273760 | 2014-01-23 09:04:58 +0100 | [diff] [blame] | 419 | [[validation]] |
| David Pursehouse | 91c5f5e | 2014-01-23 18:57:33 +0900 | [diff] [blame] | 420 | == Validation Listeners |
| Edwin Kempin | 3273760 | 2014-01-23 09:04:58 +0100 | [diff] [blame] | 421 | |
| 422 | Certain operations in Gerrit can be validated by plugins by |
| 423 | implementing the corresponding link:config-validation.html[listeners]. |
| 424 | |
| Saša Živkov | ec85a07 | 2014-01-28 10:08:25 +0100 | [diff] [blame] | 425 | [[receive-pack]] |
| 426 | == Receive Pack Initializers |
| 427 | |
| 428 | Plugins may provide ReceivePack initializers which will be invoked |
| 429 | by Gerrit just before a ReceivePack instance will be used. Usually, |
| 430 | plugins will make use of the setXXX methods on the ReceivePack to |
| 431 | set additional properties on it. |
| 432 | |
| Saša Živkov | 626c731 | 2014-02-24 17:15:08 +0100 | [diff] [blame] | 433 | [[post-receive-hook]] |
| 434 | == Post Receive-Pack Hooks |
| 435 | |
| 436 | Plugins may register PostReceiveHook instances in order to get |
| 437 | notified when JGit successfully receives a pack. This may be useful |
| 438 | for those plugins which would like to monitor changes in Git |
| 439 | repositories. |
| 440 | |
| Hugo Arès | 572d542 | 2014-06-17 14:22:03 -0400 | [diff] [blame] | 441 | [[pre-upload-hook]] |
| 442 | == Pre Upload-Pack Hooks |
| 443 | |
| 444 | Plugins may register PreUploadHook instances in order to get |
| 445 | notified when JGit is about to upload a pack. This may be useful |
| 446 | for those plugins which would like to monitor usage in Git |
| 447 | repositories. |
| 448 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 449 | [[ssh]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 450 | == SSH Commands |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 451 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 452 | Plugins may provide commands that can be accessed through the SSH |
| 453 | interface (extensions do not have this option). |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 454 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 455 | Command implementations must extend the base class SshCommand: |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 456 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 457 | [source,java] |
| 458 | ---- |
| 459 | import com.google.gerrit.sshd.SshCommand; |
| David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 460 | import com.google.gerrit.sshd.CommandMetaData; |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 461 | |
| Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 462 | @CommandMetaData(name="print", description="Print hello command") |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 463 | class PrintHello extends SshCommand { |
| Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 464 | @Override |
| 465 | protected void run() { |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 466 | stdout.print("Hello\n"); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 467 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 468 | } |
| 469 | ---- |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 470 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 471 | If no Guice modules are declared in the manifest, SSH commands may |
| Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 472 | use auto-registration by providing an `@Export` annotation: |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 473 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 474 | [source,java] |
| 475 | ---- |
| 476 | import com.google.gerrit.extensions.annotations.Export; |
| 477 | import com.google.gerrit.sshd.SshCommand; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 478 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 479 | @Export("print") |
| 480 | class PrintHello extends SshCommand { |
| Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 481 | @Override |
| 482 | protected void run() { |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 483 | stdout.print("Hello\n"); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 484 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 485 | } |
| 486 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 487 | |
| 488 | If explicit registration is being used, a Guice module must be |
| 489 | supplied to register the SSH command and declared in the manifest |
| 490 | with the `Gerrit-SshModule` attribute: |
| 491 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 492 | [source,java] |
| 493 | ---- |
| 494 | import com.google.gerrit.sshd.PluginCommandModule; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 495 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 496 | class MyCommands extends PluginCommandModule { |
| Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 497 | @Override |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 498 | protected void configureCommands() { |
| David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 499 | command(PrintHello.class); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 500 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 501 | } |
| 502 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 503 | |
| 504 | For a plugin installed as name `helloworld`, the command implemented |
| 505 | by PrintHello class will be available to users as: |
| 506 | |
| 507 | ---- |
| Keunhong Park | a09a6f1 | 2012-07-10 14:45:02 -0600 | [diff] [blame] | 508 | $ ssh -p 29418 review.example.com helloworld print |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 509 | ---- |
| 510 | |
| David Ostrovsky | 79c4d89 | 2014-03-15 13:52:46 +0100 | [diff] [blame] | 511 | [[multiple-commands]] |
| 512 | === Multiple Commands bound to one implementation |
| 513 | |
| David Ostrovsky | e3172b3 | 2013-10-13 14:19:13 +0200 | [diff] [blame] | 514 | Multiple SSH commands can be bound to the same implementation class. For |
| 515 | example a Gerrit Shell plugin can bind different shell commands to the same |
| 516 | implementation class: |
| 517 | |
| 518 | [source,java] |
| 519 | ---- |
| 520 | public class SshShellModule extends PluginCommandModule { |
| 521 | @Override |
| 522 | protected void configureCommands() { |
| 523 | command("ls").to(ShellCommand.class); |
| 524 | command("ps").to(ShellCommand.class); |
| 525 | [...] |
| 526 | } |
| 527 | } |
| 528 | ---- |
| 529 | |
| 530 | With the possible implementation: |
| 531 | |
| 532 | [source,java] |
| 533 | ---- |
| 534 | public class ShellCommand extends SshCommand { |
| 535 | @Override |
| 536 | protected void run() throws UnloggedFailure { |
| 537 | String cmd = getName().substring(getPluginName().length() + 1); |
| 538 | ProcessBuilder proc = new ProcessBuilder(cmd); |
| 539 | Process cmd = proc.start(); |
| 540 | [...] |
| 541 | } |
| 542 | } |
| 543 | ---- |
| 544 | |
| 545 | And the call: |
| 546 | |
| 547 | ---- |
| 548 | $ ssh -p 29418 review.example.com shell ls |
| 549 | $ ssh -p 29418 review.example.com shell ps |
| 550 | ---- |
| 551 | |
| David Ostrovsky | 79c4d89 | 2014-03-15 13:52:46 +0100 | [diff] [blame] | 552 | [[root-level-commands]] |
| 553 | === Root Level Commands |
| 554 | |
| David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 555 | Single command plugins are also supported. In this scenario plugin binds |
| 556 | SSH command to its own name. `SshModule` must inherit from |
| 557 | `SingleCommandPluginModule` class: |
| 558 | |
| 559 | [source,java] |
| 560 | ---- |
| 561 | public class SshModule extends SingleCommandPluginModule { |
| 562 | @Override |
| 563 | protected void configure(LinkedBindingBuilder<Command> b) { |
| 564 | b.to(ShellCommand.class); |
| 565 | } |
| 566 | } |
| 567 | ---- |
| 568 | |
| 569 | If the plugin above is deployed under sh.jar file in `$site/plugins` |
| David Pursehouse | 659860f | 2013-12-16 14:50:04 +0900 | [diff] [blame] | 570 | directory, generic commands can be called without specifying the |
| David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 571 | actual SSH command. Note in the example below, that the called commands |
| 572 | `ls` and `ps` was not explicitly bound: |
| 573 | |
| 574 | ---- |
| 575 | $ ssh -p 29418 review.example.com sh ls |
| 576 | $ ssh -p 29418 review.example.com sh ps |
| 577 | ---- |
| 578 | |
| Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 579 | [[simple-configuration]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 580 | == Simple Configuration in `gerrit.config` |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 581 | |
| 582 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 583 | If a plugin needs global configuration, this configuration should be |
| 584 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 585 | |
| Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 586 | This approach of storing the plugin configuration is only suitable for |
| 587 | plugins that have a simple configuration that only consists of |
| 588 | key-value pairs. With this approach it is not possible to have |
| 589 | subsections in the plugin configuration. Plugins that require a complex |
| Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 590 | configuration need to store their configuration in their |
| 591 | link:#configuration[own configuration file] where they can make use of |
| 592 | subsections. On the other hand storing the plugin configuration in a |
| 593 | 'plugin' subsection in the `gerrit.config` file has the advantage that |
| 594 | administrators have all configuration parameters in one file, instead |
| 595 | of having one configuration file per plugin. |
| Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 596 | |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 597 | To avoid conflicts with other plugins, it is recommended that plugins |
| 598 | only use the `plugin` subsection with their own name. For example the |
| 599 | `helloworld` plugin should store its configuration in the |
| 600 | `plugin.helloworld` subsection: |
| 601 | |
| 602 | ---- |
| 603 | [plugin "helloworld"] |
| 604 | language = Latin |
| 605 | ---- |
| 606 | |
| Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 607 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 608 | plugin can easily access its configuration and there is no need for a |
| 609 | plugin to parse the `gerrit.config` file on its own: |
| 610 | |
| 611 | [source,java] |
| 612 | ---- |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 613 | @Inject |
| 614 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 615 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 616 | [...] |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 617 | |
| Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 618 | String language = cfg.getFromGerritConfig("helloworld") |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 619 | .getString("language", "English"); |
| Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 620 | ---- |
| 621 | |
| Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 622 | [[configuration]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 623 | == Configuration in own config file |
| Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 624 | |
| 625 | Plugins can store their configuration in an own configuration file. |
| 626 | This makes sense if the plugin configuration is rather complex and |
| 627 | requires the usage of subsections. Plugins that have a simple |
| 628 | key-value pair configuration can store their configuration in a |
| 629 | link:#simple-configuration[`plugin` subsection of the `gerrit.config` |
| 630 | file]. |
| 631 | |
| 632 | The plugin configuration file must be named after the plugin and must |
| 633 | be located in the `etc` folder of the review site. For example a |
| 634 | configuration file for a `default-reviewer` plugin could look like |
| 635 | this: |
| 636 | |
| 637 | .$site_path/etc/default-reviewer.config |
| 638 | ---- |
| 639 | [branch "refs/heads/master"] |
| 640 | reviewer = Project Owners |
| 641 | reviewer = john.doe@example.com |
| 642 | [match "file:^.*\.txt"] |
| 643 | reviewer = My Info Developers |
| 644 | ---- |
| 645 | |
| 646 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 647 | plugin can easily access its configuration: |
| 648 | |
| 649 | [source,java] |
| 650 | ---- |
| 651 | @Inject |
| 652 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 653 | |
| 654 | [...] |
| 655 | |
| 656 | String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer") |
| 657 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 658 | ---- |
| 659 | |
| Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 660 | |
| Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 661 | [[simple-project-specific-configuration]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 662 | == Simple Project Specific Configuration in `project.config` |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 663 | |
| 664 | In Gerrit, project specific configuration is stored in the project's |
| 665 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 666 | needs configuration on project level (e.g. to enable its functionality |
| 667 | only for certain projects), this configuration should be stored in a |
| 668 | `plugin` subsection in the project's `project.config` file. |
| 669 | |
| Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 670 | This approach of storing the plugin configuration is only suitable for |
| 671 | plugins that have a simple configuration that only consists of |
| 672 | key-value pairs. With this approach it is not possible to have |
| 673 | subsections in the plugin configuration. Plugins that require a complex |
| Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 674 | configuration need to store their configuration in their |
| 675 | link:#project-specific-configuration[own configuration file] where they |
| 676 | can make use of subsections. On the other hand storing the plugin |
| 677 | configuration in a 'plugin' subsection in the `project.config` file has |
| 678 | the advantage that project owners have all configuration parameters in |
| 679 | one file, instead of having one configuration file per plugin. |
| Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 680 | |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 681 | To avoid conflicts with other plugins, it is recommended that plugins |
| 682 | only use the `plugin` subsection with their own name. For example the |
| 683 | `helloworld` plugin should store its configuration in the |
| 684 | `plugin.helloworld` subsection: |
| 685 | |
| 686 | ---- |
| 687 | [plugin "helloworld"] |
| 688 | enabled = true |
| 689 | ---- |
| 690 | |
| 691 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 692 | plugin can easily access its project specific configuration and there |
| 693 | is no need for a plugin to parse the `project.config` file on its own: |
| 694 | |
| 695 | [source,java] |
| 696 | ---- |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 697 | @Inject |
| 698 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 699 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 700 | [...] |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 701 | |
| Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 702 | boolean enabled = cfg.getFromProjectConfig(project, "helloworld") |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 703 | .getBoolean("enabled", false); |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 704 | ---- |
| 705 | |
| Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 706 | It is also possible to get missing configuration parameters inherited |
| 707 | from the parent projects: |
| 708 | |
| 709 | [source,java] |
| 710 | ---- |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 711 | @Inject |
| 712 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 713 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 714 | [...] |
| Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 715 | |
| Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 716 | boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld") |
| David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 717 | .getBoolean("enabled", false); |
| Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 718 | ---- |
| 719 | |
| Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 720 | Project owners can edit the project configuration by fetching the |
| 721 | `refs/meta/config` branch, editing the `project.config` file and |
| 722 | pushing the commit back. |
| 723 | |
| Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 724 | Plugin configuration values that are stored in the `project.config` |
| 725 | file can be exposed in the ProjectInfoScreen to allow project owners |
| 726 | to see and edit them from the UI. |
| 727 | |
| 728 | For this an instance of `ProjectConfigEntry` needs to be bound for each |
| 729 | parameter. The export name must be a valid Git variable name. The |
| 730 | variable name is case-insensitive, allows only alphanumeric characters |
| 731 | and '-', and must start with an alphabetic character. |
| 732 | |
| Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 733 | The example below shows how the parameters `plugin.helloworld.enabled` |
| 734 | and `plugin.helloworld.language` are bound to be editable from the |
| David Pursehouse | a1d633b | 2014-05-02 17:21:02 +0900 | [diff] [blame] | 735 | Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting" |
| Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 736 | is provided as display name and the default value is set to `true`. |
| 737 | For the parameter `plugin.helloworld.language` "Preferred Language" |
| 738 | is provided as display name and "en" is set as default value. |
| Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 739 | |
| 740 | [source,java] |
| 741 | ---- |
| 742 | class Module extends AbstractModule { |
| 743 | @Override |
| 744 | protected void configure() { |
| 745 | bind(ProjectConfigEntry.class) |
| Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 746 | .annotatedWith(Exports.named("enabled")) |
| 747 | .toInstance(new ProjectConfigEntry("Enable Greeting", true)); |
| 748 | bind(ProjectConfigEntry.class) |
| Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 749 | .annotatedWith(Exports.named("language")) |
| 750 | .toInstance(new ProjectConfigEntry("Preferred Language", "en")); |
| 751 | } |
| 752 | } |
| 753 | ---- |
| 754 | |
| Edwin Kempin | b64d397 | 2013-11-17 18:55:48 +0100 | [diff] [blame] | 755 | By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins |
| 756 | can be notified when this configuration parameter is updated on a |
| 757 | project. |
| 758 | |
| Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 759 | [[project-specific-configuration]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 760 | == Project Specific Configuration in own config file |
| Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 761 | |
| 762 | Plugins can store their project specific configuration in an own |
| 763 | configuration file in the projects `refs/meta/config` branch. |
| 764 | This makes sense if the plugins project specific configuration is |
| 765 | rather complex and requires the usage of subsections. Plugins that |
| 766 | have a simple key-value pair configuration can store their project |
| 767 | specific configuration in a link:#simple-project-specific-configuration[ |
| 768 | `plugin` subsection of the `project.config` file]. |
| 769 | |
| 770 | The plugin configuration file in the `refs/meta/config` branch must be |
| 771 | named after the plugin. For example a configuration file for a |
| 772 | `default-reviewer` plugin could look like this: |
| 773 | |
| 774 | .default-reviewer.config |
| 775 | ---- |
| 776 | [branch "refs/heads/master"] |
| 777 | reviewer = Project Owners |
| 778 | reviewer = john.doe@example.com |
| 779 | [match "file:^.*\.txt"] |
| 780 | reviewer = My Info Developers |
| 781 | ---- |
| 782 | |
| 783 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 784 | plugin can easily access its project specific configuration: |
| 785 | |
| 786 | [source,java] |
| 787 | ---- |
| 788 | @Inject |
| 789 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 790 | |
| 791 | [...] |
| 792 | |
| 793 | String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer") |
| 794 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 795 | ---- |
| 796 | |
| Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 797 | It is also possible to get missing configuration parameters inherited |
| 798 | from the parent projects: |
| 799 | |
| 800 | [source,java] |
| 801 | ---- |
| 802 | @Inject |
| 803 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 804 | |
| 805 | [...] |
| 806 | |
| David Ostrovsky | 468e4c3 | 2014-03-22 06:05:35 -0700 | [diff] [blame] | 807 | String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer") |
| Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 808 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 809 | ---- |
| 810 | |
| Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 811 | Project owners can edit the project configuration by fetching the |
| 812 | `refs/meta/config` branch, editing the `<plugin-name>.config` file and |
| 813 | pushing the commit back. |
| 814 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 815 | == React on changes in project configuration |
| Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 816 | |
| 817 | If a plugin wants to react on changes in the project configuration, it |
| 818 | can implement a `GitReferenceUpdatedListener` and filter on events for |
| 819 | the `refs/meta/config` branch: |
| 820 | |
| 821 | [source,java] |
| 822 | ---- |
| 823 | public class MyListener implements GitReferenceUpdatedListener { |
| 824 | |
| 825 | private final MetaDataUpdate.Server metaDataUpdateFactory; |
| 826 | |
| 827 | @Inject |
| 828 | MyListener(MetaDataUpdate.Server metaDataUpdateFactory) { |
| 829 | this.metaDataUpdateFactory = metaDataUpdateFactory; |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public void onGitReferenceUpdated(Event event) { |
| Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 834 | if (event.getRefName().equals(RefNames.REFS_CONFIG)) { |
| Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 835 | Project.NameKey p = new Project.NameKey(event.getProjectName()); |
| 836 | try { |
| Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 837 | ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId()); |
| 838 | ProjectConfig newCfg = parseConfig(p, event.getNewObjectId()); |
| Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 839 | |
| Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 840 | if (oldCfg != null && newCfg != null |
| 841 | && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) { |
| Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 842 | // submit type has changed |
| 843 | ... |
| 844 | } |
| 845 | } catch (IOException | ConfigInvalidException e) { |
| 846 | ... |
| 847 | } |
| 848 | } |
| 849 | } |
| Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 850 | |
| 851 | private ProjectConfig parseConfig(Project.NameKey p, String idStr) |
| 852 | throws IOException, ConfigInvalidException, RepositoryNotFoundException { |
| 853 | ObjectId id = ObjectId.fromString(idStr); |
| 854 | if (ObjectId.zeroId().equals(id)) { |
| 855 | return null; |
| 856 | } |
| 857 | return ProjectConfig.read(metaDataUpdateFactory.create(p), id); |
| 858 | } |
| Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 859 | } |
| 860 | ---- |
| 861 | |
| 862 | |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 863 | [[capabilities]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 864 | == Plugin Owned Capabilities |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 865 | |
| 866 | Plugins may provide their own capabilities and restrict usage of SSH |
| Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 867 | commands or `UiAction` to the users who are granted those capabilities. |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 868 | |
| 869 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 870 | abstract class: |
| 871 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 872 | [source,java] |
| 873 | ---- |
| 874 | public class PrintHelloCapability extends CapabilityDefinition { |
| 875 | @Override |
| 876 | public String getDescription() { |
| 877 | return "Print Hello"; |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 878 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 879 | } |
| 880 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 881 | |
| Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 882 | If no Guice modules are declared in the manifest, capability may |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 883 | use auto-registration by providing an `@Export` annotation: |
| 884 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 885 | [source,java] |
| 886 | ---- |
| 887 | @Export("printHello") |
| 888 | public class PrintHelloCapability extends CapabilityDefinition { |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 889 | [...] |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 890 | } |
| 891 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 892 | |
| 893 | Otherwise the capability must be bound in a plugin module: |
| 894 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 895 | [source,java] |
| 896 | ---- |
| 897 | public class HelloWorldModule extends AbstractModule { |
| 898 | @Override |
| 899 | protected void configure() { |
| 900 | bind(CapabilityDefinition.class) |
| 901 | .annotatedWith(Exports.named("printHello")) |
| 902 | .to(PrintHelloCapability.class); |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 903 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 904 | } |
| 905 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 906 | |
| 907 | 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] | 908 | 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] | 909 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 910 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 911 | [source,java] |
| 912 | ---- |
| 913 | @RequiresCapability("printHello") |
| 914 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 915 | public final class PrintHelloWorldCommand extends SshCommand { |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 916 | [...] |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 917 | } |
| 918 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 919 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 920 | Or with `UiAction`: |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 921 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 922 | [source,java] |
| 923 | ---- |
| 924 | @RequiresCapability("printHello") |
| 925 | public class SayHelloAction extends UiAction<RevisionResource> |
| 926 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 927 | [...] |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 928 | } |
| 929 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 930 | |
| 931 | Capability scope was introduced to differentiate between plugin-owned |
| David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 932 | capabilities and core capabilities. Per default the scope of the |
| 933 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 934 | |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 935 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 936 | capability is assumed to be that plugin. |
| David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 937 | |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 938 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 939 | (and thus is outside of a plugin) the scope is the core server and will use |
| 940 | the `GlobalCapability` known to Gerrit Code Review server. |
| 941 | |
| 942 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 943 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 944 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 945 | [source,java] |
| 946 | ---- |
| 947 | @RequiresCapability(value = "administrateServer", scope = |
| 948 | CapabilityScope.CORE) |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 949 | [...] |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 950 | ---- |
| David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 951 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 952 | [[ui_extension]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 953 | == UI Extension |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 954 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 955 | Plugins can contribute UI actions on core Gerrit pages. This is useful |
| 956 | for workflow customization or exposing plugin functionality through the |
| 957 | UI in addition to SSH commands and the REST API. |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 958 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 959 | For instance a plugin to integrate Jira with Gerrit changes may |
| 960 | contribute a "File bug" button to allow filing a bug from the change |
| 961 | page or plugins to integrate continuous integration systems may |
| 962 | contribute a "Schedule" button to allow a CI build to be scheduled |
| 963 | manually from the patch set panel. |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 964 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 965 | Two different places on core Gerrit pages are supported: |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 966 | |
| 967 | * Change screen |
| 968 | * Project info screen |
| 969 | |
| 970 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 971 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 972 | [source,java] |
| 973 | ---- |
| 974 | @RequiresCapability("printHello") |
| 975 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 976 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 977 | static class Input { |
| 978 | boolean french; |
| 979 | String message; |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 980 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 981 | |
| 982 | private Provider<CurrentUser> user; |
| 983 | |
| 984 | @Inject |
| 985 | HelloWorldAction(Provider<CurrentUser> user) { |
| 986 | this.user = user; |
| 987 | } |
| 988 | |
| 989 | @Override |
| 990 | public String apply(RevisionResource rev, Input input) { |
| 991 | final String greeting = input.french |
| 992 | ? "Bonjour" |
| 993 | : "Hello"; |
| 994 | return String.format("%s %s from change %s, patch set %d!", |
| 995 | greeting, |
| 996 | Strings.isNullOrEmpty(input.message) |
| 997 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 998 | : input.message, |
| 999 | rev.getChange().getId().toString(), |
| 1000 | rev.getPatchSet().getPatchSetId()); |
| 1001 | } |
| 1002 | |
| 1003 | @Override |
| 1004 | public Description getDescription( |
| 1005 | RevisionResource resource) { |
| 1006 | return new Description() |
| 1007 | .setLabel("Say hello") |
| 1008 | .setTitle("Say hello in different languages"); |
| 1009 | } |
| 1010 | } |
| 1011 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1012 | |
| David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1013 | Sometimes plugins may want to be able to change the state of a patch set or |
| 1014 | change in the `UiAction.apply()` method and reflect these changes on the core |
| 1015 | UI. For example a buildbot plugin which exposes a 'Schedule' button on the |
| 1016 | patch set panel may want to disable that button after the build was scheduled |
| 1017 | and update the tooltip of that button. But because of Gerrit's caching |
| 1018 | strategy the following must be taken into consideration. |
| 1019 | |
| 1020 | The browser is allowed to cache the `UiAction` information until something on |
| 1021 | the change is modified. More accurately the change row needs to be modified in |
| 1022 | the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or |
| 1023 | the +refs/meta/config+ of the project or any parents needs to change to a new |
| 1024 | SHA-1. The ETag SHA-1 computation code can be found in the |
| 1025 | `ChangeResource.getETag()` method. |
| 1026 | |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1027 | 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] | 1028 | |
| 1029 | [source,java] |
| 1030 | ---- |
| 1031 | @Override |
| 1032 | public Object apply(RevisionResource rcrs, Input in) { |
| 1033 | // schedule a build |
| 1034 | [...] |
| 1035 | // update change |
| 1036 | ReviewDb db = dbProvider.get(); |
| 1037 | db.changes().beginTransaction(change.getId()); |
| 1038 | try { |
| 1039 | change = db.changes().atomicUpdate( |
| 1040 | change.getId(), |
| 1041 | new AtomicUpdate<Change>() { |
| 1042 | @Override |
| 1043 | public Change update(Change change) { |
| 1044 | ChangeUtil.updated(change); |
| 1045 | return change; |
| 1046 | } |
| 1047 | }); |
| 1048 | db.commit(); |
| 1049 | } finally { |
| 1050 | db.rollback(); |
| 1051 | } |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1052 | [...] |
| David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1053 | } |
| 1054 | ---- |
| 1055 | |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1056 | `UiAction` must be bound in a plugin module: |
| 1057 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1058 | [source,java] |
| 1059 | ---- |
| 1060 | public class Module extends AbstractModule { |
| 1061 | @Override |
| 1062 | protected void configure() { |
| 1063 | install(new RestApiModule() { |
| 1064 | @Override |
| 1065 | protected void configure() { |
| 1066 | post(REVISION_KIND, "say-hello") |
| 1067 | .to(HelloWorldAction.class); |
| 1068 | } |
| 1069 | }); |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1070 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1071 | } |
| 1072 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1073 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1074 | The module above must be declared in the `pom.xml` for Maven driven |
| 1075 | plugins: |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1076 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1077 | [source,xml] |
| 1078 | ---- |
| 1079 | <manifestEntries> |
| 1080 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 1081 | </manifestEntries> |
| 1082 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1083 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1084 | or in the `BUCK` configuration file for Buck driven plugins: |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1085 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1086 | [source,python] |
| 1087 | ---- |
| 1088 | manifest_entries = [ |
| 1089 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 1090 | ] |
| 1091 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1092 | |
| 1093 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 1094 | combined with the JavaScript API. This would display a small popup near the |
| 1095 | activation button to gather additional input from the user. The JS file is |
| 1096 | typically put in the `static` folder within the plugin's directory: |
| 1097 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1098 | [source,javascript] |
| 1099 | ---- |
| 1100 | Gerrit.install(function(self) { |
| 1101 | function onSayHello(c) { |
| 1102 | var f = c.textfield(); |
| 1103 | var t = c.checkbox(); |
| 1104 | var b = c.button('Say hello', {onclick: function(){ |
| 1105 | c.call( |
| 1106 | {message: f.value, french: t.checked}, |
| 1107 | function(r) { |
| 1108 | c.hide(); |
| 1109 | window.alert(r); |
| 1110 | c.refresh(); |
| 1111 | }); |
| 1112 | }}); |
| 1113 | c.popup(c.div( |
| 1114 | c.prependLabel('Greeting message', f), |
| 1115 | c.br(), |
| 1116 | c.label(t, 'french'), |
| 1117 | c.br(), |
| 1118 | b)); |
| 1119 | f.focus(); |
| 1120 | } |
| 1121 | self.onAction('revision', 'say-hello', onSayHello); |
| 1122 | }); |
| 1123 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1124 | |
| 1125 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 1126 | an HTTP Module: |
| 1127 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1128 | [source,java] |
| 1129 | ---- |
| 1130 | public class HttpModule extends HttpPluginModule { |
| 1131 | @Override |
| 1132 | protected void configureServlets() { |
| 1133 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1134 | .toInstance(new JavaScriptPlugin("hello.js")); |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1135 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1136 | } |
| 1137 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1138 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1139 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1140 | driven plugins: |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1141 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1142 | [source,xml] |
| 1143 | ---- |
| 1144 | <manifestEntries> |
| 1145 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 1146 | </manifestEntries> |
| 1147 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1148 | |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1149 | or in the `BUCK` configuration file for Buck driven plugins |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1150 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1151 | [source,python] |
| 1152 | ---- |
| 1153 | manifest_entries = [ |
| 1154 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 1155 | ] |
| 1156 | ---- |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1157 | |
| 1158 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 1159 | capability check is done during the `UiAction` gathering, so the plugin author |
| 1160 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 1161 | case. |
| 1162 | |
| 1163 | The following prerequisities must be met, to satisfy the capability check: |
| 1164 | |
| 1165 | * user is authenticated |
| Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1166 | * 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] | 1167 | * user is a member of a group which has the required capability |
| 1168 | |
| 1169 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 1170 | combined with JavaScript API (its own JavaScript function is provided), |
| 1171 | then a popup dialog is normally opened to gather additional user input. |
| 1172 | A new button is placed on the popup dialog to actually send the request. |
| 1173 | |
| 1174 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 1175 | can be accessed from any REST client, i. e.: |
| 1176 | |
| 1177 | ==== |
| 1178 | curl -X POST -H "Content-Type: application/json" \ |
| 1179 | -d '{message: "François", french: true}' \ |
| 1180 | --digest --user joe:secret \ |
| 1181 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
| 1182 | "Bonjour François from change 1, patch set 1!" |
| 1183 | ==== |
| 1184 | |
| David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1185 | 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] | 1186 | particularly useful for `DELETE` requests: |
| David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1187 | |
| 1188 | [source,java] |
| 1189 | ---- |
| 1190 | public class Module extends AbstractModule { |
| 1191 | @Override |
| 1192 | protected void configure() { |
| 1193 | install(new RestApiModule() { |
| 1194 | @Override |
| 1195 | protected void configure() { |
| 1196 | delete(PROJECT_KIND) |
| 1197 | .to(DeleteProject.class); |
| 1198 | } |
| 1199 | }); |
| 1200 | } |
| 1201 | } |
| 1202 | ---- |
| 1203 | |
| David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1204 | For a `UiAction` bound this way, a JS API function can be provided. |
| 1205 | |
| 1206 | Currently only one restriction exists: per plugin only one `UiAction` |
| David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1207 | can be bound per resource without view name. To define a JS function |
| 1208 | for the `UiAction`, "/" must be used as the name: |
| 1209 | |
| 1210 | [source,javascript] |
| 1211 | ---- |
| 1212 | Gerrit.install(function(self) { |
| 1213 | function onDeleteProject(c) { |
| 1214 | [...] |
| 1215 | } |
| 1216 | self.onAction('project', '/', onDeleteProject); |
| 1217 | }); |
| 1218 | ---- |
| 1219 | |
| Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1220 | [[top-menu-extensions]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1221 | == Top Menu Extensions |
| Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1222 | |
| 1223 | Plugins can contribute items to Gerrit's top menu. |
| 1224 | |
| 1225 | A single top menu extension can have multiple elements and will be put as |
| 1226 | the last element in Gerrit's top menu. |
| 1227 | |
| 1228 | Plugins define the top menu entries by implementing `TopMenu` interface: |
| 1229 | |
| 1230 | [source,java] |
| 1231 | ---- |
| 1232 | public class MyTopMenuExtension implements TopMenu { |
| 1233 | |
| 1234 | @Override |
| 1235 | public List<MenuEntry> getEntries() { |
| 1236 | return Lists.newArrayList( |
| 1237 | new MenuEntry("Top Menu Entry", Lists.newArrayList( |
| 1238 | new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1239 | } |
| 1240 | } |
| 1241 | ---- |
| 1242 | |
| Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1243 | Plugins can also add additional menu items to Gerrit's top menu entries |
| 1244 | by defining a `MenuEntry` that has the same name as a Gerrit top menu |
| 1245 | entry: |
| 1246 | |
| 1247 | [source,java] |
| 1248 | ---- |
| 1249 | public class MyTopMenuExtension implements TopMenu { |
| 1250 | |
| 1251 | @Override |
| 1252 | public List<MenuEntry> getEntries() { |
| 1253 | return Lists.newArrayList( |
| Dariusz Luksza | 2d3afab | 2013-10-01 11:07:13 +0200 | [diff] [blame] | 1254 | new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList( |
| Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1255 | new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/")))); |
| 1256 | } |
| 1257 | } |
| 1258 | ---- |
| 1259 | |
| Dariusz Luksza | e8de74f | 2014-06-04 19:39:20 +0200 | [diff] [blame] | 1260 | `MenuItems` that are bound for the `MenuEntry` with the name |
| 1261 | `GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder |
| 1262 | which is automatically replaced by the actual project name. |
| 1263 | |
| 1264 | E.g. plugins may register an link:#http[HTTP Servlet] to handle project |
| 1265 | specific requests and add an menu item for this: |
| 1266 | |
| 1267 | [source,java] |
| 1268 | --- |
| 1269 | new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}"); |
| 1270 | --- |
| 1271 | |
| 1272 | This also enables plugins to provide menu items for project aware |
| 1273 | screens: |
| 1274 | |
| 1275 | [source,java] |
| 1276 | --- |
| 1277 | new MenuItem("My Screen", "/x/my-screen/for/${projectName}"); |
| 1278 | --- |
| 1279 | |
| Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1280 | If no Guice modules are declared in the manifest, the top menu extension may use |
| 1281 | auto-registration by providing an `@Listen` annotation: |
| 1282 | |
| 1283 | [source,java] |
| 1284 | ---- |
| 1285 | @Listen |
| 1286 | public class MyTopMenuExtension implements TopMenu { |
| David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1287 | [...] |
| Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1288 | } |
| 1289 | ---- |
| 1290 | |
| Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1291 | Otherwise the top menu extension must be bound in the plugin module used |
| 1292 | for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF): |
| Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1293 | |
| 1294 | [source,java] |
| 1295 | ---- |
| Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1296 | package com.googlesource.gerrit.plugins.helloworld; |
| 1297 | |
| Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1298 | public class HelloWorldModule extends AbstractModule { |
| 1299 | @Override |
| 1300 | protected void configure() { |
| 1301 | DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class); |
| 1302 | } |
| 1303 | } |
| 1304 | ---- |
| 1305 | |
| Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1306 | [source,manifest] |
| 1307 | ---- |
| 1308 | Gerrit-ApiType: plugin |
| 1309 | Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule |
| 1310 | ---- |
| 1311 | |
| Edwin Kempin | b2e926a | 2013-11-11 16:38:30 +0100 | [diff] [blame] | 1312 | It is also possible to show some menu entries only if the user has a |
| 1313 | certain capability: |
| 1314 | |
| 1315 | [source,java] |
| 1316 | ---- |
| 1317 | public class MyTopMenuExtension implements TopMenu { |
| 1318 | private final String pluginName; |
| 1319 | private final Provider<CurrentUser> userProvider; |
| 1320 | private final List<MenuEntry> menuEntries; |
| 1321 | |
| 1322 | @Inject |
| 1323 | public MyTopMenuExtension(@PluginName String pluginName, |
| 1324 | Provider<CurrentUser> userProvider) { |
| 1325 | this.pluginName = pluginName; |
| 1326 | this.userProvider = userProvider; |
| 1327 | menuEntries = new ArrayList<TopMenu.MenuEntry>(); |
| 1328 | |
| 1329 | // add menu entry that is only visible to users with a certain capability |
| 1330 | if (canSeeMenuEntry()) { |
| 1331 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1332 | .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1333 | } |
| 1334 | |
| 1335 | // add menu entry that is visible to all users (even anonymous users) |
| 1336 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1337 | .singletonList(new MenuItem("Documentation", "/plugins/myplugin/")))); |
| 1338 | } |
| 1339 | |
| 1340 | private boolean canSeeMenuEntry() { |
| 1341 | if (userProvider.get().isIdentifiedUser()) { |
| 1342 | CapabilityControl ctl = userProvider.get().getCapabilities(); |
| 1343 | return ctl.canPerform(pluginName + "-" + MyCapability.ID) |
| 1344 | || ctl.canAdministrateServer(); |
| 1345 | } else { |
| 1346 | return false; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | @Override |
| 1351 | public List<MenuEntry> getEntries() { |
| 1352 | return menuEntries; |
| 1353 | } |
| 1354 | } |
| 1355 | ---- |
| 1356 | |
| Edwin Kempin | 3c024ea | 2013-11-11 10:43:46 +0100 | [diff] [blame] | 1357 | [[gwt_ui_extension]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1358 | == GWT UI Extension |
| Edwin Kempin | 3c024ea | 2013-11-11 10:43:46 +0100 | [diff] [blame] | 1359 | Plugins can extend the Gerrit UI with own GWT code. |
| 1360 | |
| 1361 | The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to |
| 1362 | generate a GWT plugin skeleton. How to use the Maven plugin archetypes |
| 1363 | is described in the link:#getting-started[Getting started] section. |
| 1364 | |
| 1365 | The generated GWT plugin has a link:#top-menu-extensions[top menu] that |
| 1366 | opens a GWT dialog box when the user clicks on it. |
| 1367 | |
| Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1368 | In addition to the Gerrit-Plugin API a GWT plugin depends on |
| 1369 | `gerrit-plugin-gwtui`. This dependency must be specified in the |
| 1370 | `pom.xml`: |
| 1371 | |
| 1372 | [source,xml] |
| 1373 | ---- |
| 1374 | <dependency> |
| 1375 | <groupId>com.google.gerrit</groupId> |
| 1376 | <artifactId>gerrit-plugin-gwtui</artifactId> |
| 1377 | <version>${Gerrit-ApiVersion}</version> |
| 1378 | </dependency> |
| 1379 | ---- |
| 1380 | |
| 1381 | A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`, |
| 1382 | that bundles together all the configuration settings of the GWT plugin: |
| 1383 | |
| 1384 | [source,xml] |
| 1385 | ---- |
| 1386 | <?xml version="1.0" encoding="UTF-8"?> |
| 1387 | <module rename-to="hello_gwt_plugin"> |
| 1388 | <!-- Inherit the core Web Toolkit stuff. --> |
| 1389 | <inherits name="com.google.gwt.user.User"/> |
| 1390 | <!-- Other module inherits --> |
| 1391 | <inherits name="com.google.gerrit.Plugin"/> |
| 1392 | <inherits name="com.google.gwt.http.HTTP"/> |
| 1393 | <!-- Using GWT built-in themes adds a number of static --> |
| 1394 | <!-- resources to the plugin. No theme inherits lines were --> |
| 1395 | <!-- added in order to make this plugin as simple as possible --> |
| 1396 | <!-- Specify the app entry point class. --> |
| 1397 | <entry-point class="${package}.client.HelloPlugin"/> |
| 1398 | <stylesheet src="hello.css"/> |
| 1399 | </module> |
| 1400 | ---- |
| 1401 | |
| 1402 | The GWT module must inherit `com.google.gerrit.Plugin` and |
| 1403 | `com.google.gwt.http.HTTP`. |
| 1404 | |
| 1405 | To register the GWT module a `GwtPlugin` needs to be bound. |
| 1406 | |
| 1407 | If no Guice modules are declared in the manifest, the GWT plugin may |
| 1408 | use auto-registration by using the `@Listen` annotation: |
| 1409 | |
| 1410 | [source,java] |
| 1411 | ---- |
| 1412 | @Listen |
| 1413 | public class MyExtension extends GwtPlugin { |
| 1414 | public MyExtension() { |
| 1415 | super("hello_gwt_plugin"); |
| 1416 | } |
| 1417 | } |
| 1418 | ---- |
| 1419 | |
| 1420 | Otherwise the binding must be done in an `HttpModule`: |
| 1421 | |
| 1422 | [source,java] |
| 1423 | ---- |
| 1424 | public class HttpModule extends HttpPluginModule { |
| 1425 | |
| 1426 | @Override |
| 1427 | protected void configureServlets() { |
| 1428 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1429 | .toInstance(new GwtPlugin("hello_gwt_plugin")); |
| 1430 | } |
| 1431 | } |
| 1432 | ---- |
| 1433 | |
| 1434 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1435 | driven plugins: |
| 1436 | |
| 1437 | [source,xml] |
| 1438 | ---- |
| 1439 | <manifestEntries> |
| 1440 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule> |
| 1441 | </manifestEntries> |
| 1442 | ---- |
| 1443 | |
| Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1444 | The name that is provided to the `GwtPlugin` must match the GWT |
| 1445 | module name compiled into the plugin. The name of the GWT module |
| 1446 | can be explicitly set in the GWT module XML file by specifying |
| 1447 | the `rename-to` attribute on the module. It is important that the |
| 1448 | module name be unique across all plugins installed on the server, |
| 1449 | as the module name determines the JavaScript namespace used by the |
| 1450 | compiled plugin code. |
| Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1451 | |
| 1452 | [source,xml] |
| 1453 | ---- |
| 1454 | <module rename-to="hello_gwt_plugin"> |
| 1455 | ---- |
| 1456 | |
| 1457 | The actual GWT code must be implemented in a class that extends |
| Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1458 | `com.google.gerrit.plugin.client.PluginEntryPoint`: |
| Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1459 | |
| 1460 | [source,java] |
| 1461 | ---- |
| Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1462 | public class HelloPlugin extends PluginEntryPoint { |
| Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1463 | |
| 1464 | @Override |
| Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1465 | public void onPluginLoad() { |
| Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1466 | // Create the dialog box |
| 1467 | final DialogBox dialogBox = new DialogBox(); |
| 1468 | |
| 1469 | // The content of the dialog comes from a User specified Preference |
| 1470 | dialogBox.setText("Hello from GWT Gerrit UI plugin"); |
| 1471 | dialogBox.setAnimationEnabled(true); |
| 1472 | Button closeButton = new Button("Close"); |
| 1473 | VerticalPanel dialogVPanel = new VerticalPanel(); |
| 1474 | dialogVPanel.setWidth("100%"); |
| 1475 | dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); |
| 1476 | dialogVPanel.add(closeButton); |
| 1477 | |
| 1478 | closeButton.addClickHandler(new ClickHandler() { |
| 1479 | public void onClick(ClickEvent event) { |
| 1480 | dialogBox.hide(); |
| 1481 | } |
| 1482 | }); |
| 1483 | |
| 1484 | // Set the contents of the Widget |
| 1485 | dialogBox.setWidget(dialogVPanel); |
| 1486 | |
| 1487 | RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID); |
| 1488 | rootPanel.getElement().removeAttribute("href"); |
| 1489 | rootPanel.addDomHandler(new ClickHandler() { |
| 1490 | @Override |
| 1491 | public void onClick(ClickEvent event) { |
| 1492 | dialogBox.center(); |
| 1493 | dialogBox.show(); |
| 1494 | } |
| 1495 | }, ClickEvent.getType()); |
| 1496 | } |
| 1497 | } |
| 1498 | ---- |
| 1499 | |
| 1500 | This class must be set as entry point in the GWT module: |
| 1501 | |
| 1502 | [source,xml] |
| 1503 | ---- |
| 1504 | <entry-point class="${package}.client.HelloPlugin"/> |
| 1505 | ---- |
| 1506 | |
| 1507 | In addition this class must be defined as module in the `pom.xml` for the |
| 1508 | `gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin` |
| 1509 | must be set to `${project.build.directory}/classes/static`: |
| 1510 | |
| 1511 | [source,xml] |
| 1512 | ---- |
| 1513 | <plugin> |
| 1514 | <groupId>org.codehaus.mojo</groupId> |
| 1515 | <artifactId>gwt-maven-plugin</artifactId> |
| 1516 | <version>2.5.1</version> |
| 1517 | <configuration> |
| 1518 | <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module> |
| 1519 | <disableClassMetadata>true</disableClassMetadata> |
| 1520 | <disableCastChecking>true</disableCastChecking> |
| 1521 | <webappDirectory>${project.build.directory}/classes/static</webappDirectory> |
| 1522 | </configuration> |
| 1523 | <executions> |
| 1524 | <execution> |
| 1525 | <goals> |
| 1526 | <goal>compile</goal> |
| 1527 | </goals> |
| 1528 | </execution> |
| 1529 | </executions> |
| 1530 | </plugin> |
| 1531 | ---- |
| 1532 | |
| 1533 | To attach a GWT widget defined by the plugin to the Gerrit core UI |
| 1534 | `com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the |
| 1535 | Gerrit core widgets: |
| 1536 | |
| 1537 | [source,java] |
| 1538 | ---- |
| 1539 | RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID); |
| 1540 | rootPanel.getElement().removeAttribute("href"); |
| 1541 | rootPanel.addDomHandler(new ClickHandler() { |
| 1542 | @Override |
| 1543 | public void onClick(ClickEvent event) { |
| 1544 | dialogBox.center(); |
| 1545 | dialogBox.show(); |
| 1546 | } |
| 1547 | }, ClickEvent.getType()); |
| 1548 | ---- |
| 1549 | |
| 1550 | GWT plugins can come with their own css file. This css file must have a |
| 1551 | unique name and must be registered in the GWT module: |
| 1552 | |
| 1553 | [source,xml] |
| 1554 | ---- |
| 1555 | <stylesheet src="hello.css"/> |
| 1556 | ---- |
| 1557 | |
| Edwin Kempin | 2570b10 | 2013-11-11 11:44:50 +0100 | [diff] [blame] | 1558 | If a GWT plugin wants to invoke the Gerrit REST API it can use |
| David Pursehouse | 3a38831 | 2014-02-25 16:41:47 +0900 | [diff] [blame] | 1559 | `com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL |
| Edwin Kempin | 2570b10 | 2013-11-11 11:44:50 +0100 | [diff] [blame] | 1560 | path and to trigger the REST calls. |
| 1561 | |
| 1562 | Example for invoking a Gerrit core REST endpoint: |
| 1563 | |
| 1564 | [source,java] |
| 1565 | ---- |
| 1566 | new RestApi("projects").id(projectName).view("description") |
| 1567 | .put("new description", new AsyncCallback<JavaScriptObject>() { |
| 1568 | |
| 1569 | @Override |
| 1570 | public void onSuccess(JavaScriptObject result) { |
| 1571 | // TODO |
| 1572 | } |
| 1573 | |
| 1574 | @Override |
| 1575 | public void onFailure(Throwable caught) { |
| 1576 | // never invoked |
| 1577 | } |
| 1578 | }); |
| 1579 | ---- |
| 1580 | |
| 1581 | Example for invoking a REST endpoint defined by a plugin: |
| 1582 | |
| 1583 | [source,java] |
| 1584 | ---- |
| 1585 | new RestApi("projects").id(projectName).view("myplugin", "myview") |
| 1586 | .get(new AsyncCallback<JavaScriptObject>() { |
| 1587 | |
| 1588 | @Override |
| 1589 | public void onSuccess(JavaScriptObject result) { |
| 1590 | // TODO |
| 1591 | } |
| 1592 | |
| 1593 | @Override |
| 1594 | public void onFailure(Throwable caught) { |
| 1595 | // never invoked |
| 1596 | } |
| 1597 | }); |
| 1598 | ---- |
| 1599 | |
| 1600 | The `onFailure(Throwable)` of the provided callback is never invoked. |
| 1601 | If an error occurs, it is shown in an error dialog. |
| 1602 | |
| 1603 | In order to be able to do REST calls the GWT module must inherit |
| 1604 | `com.google.gwt.json.JSON`: |
| 1605 | |
| 1606 | [source,xml] |
| 1607 | ---- |
| 1608 | <inherits name="com.google.gwt.json.JSON"/> |
| 1609 | ---- |
| 1610 | |
| Edwin Kempin | 1519979 | 2014-04-23 16:22:05 +0200 | [diff] [blame] | 1611 | [[screen]] |
| Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 1612 | == Add Screen |
| Edwin Kempin | 1519979 | 2014-04-23 16:22:05 +0200 | [diff] [blame] | 1613 | A link:#gwt_ui_extension[GWT plugin] can link:#top-menu-extensions[add |
| 1614 | a menu item] that opens a screen that is implemented by the plugin. |
| 1615 | This way plugin screens can be fully integrated into the Gerrit UI. |
| Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 1616 | |
| 1617 | Example menu item: |
| 1618 | [source,java] |
| 1619 | ---- |
| 1620 | public class MyMenu implements TopMenu { |
| 1621 | private final List<MenuEntry> menuEntries; |
| 1622 | |
| 1623 | @Inject |
| 1624 | public MyMenu(@PluginName String name) { |
| 1625 | menuEntries = Lists.newArrayList(); |
| 1626 | menuEntries.add(new MenuEntry("My Menu", Collections.singletonList( |
| 1627 | new MenuItem("My Screen", "#/x/" + name + "/my-screen", "")))); |
| 1628 | } |
| 1629 | |
| 1630 | @Override |
| 1631 | public List<MenuEntry> getEntries() { |
| 1632 | return menuEntries; |
| 1633 | } |
| 1634 | } |
| 1635 | ---- |
| 1636 | |
| 1637 | Example screen: |
| 1638 | [source,java] |
| 1639 | ---- |
| 1640 | public class MyPlugin extends PluginEntryPoint { |
| 1641 | @Override |
| 1642 | public void onPluginLoad() { |
| 1643 | Plugin.get().screen("my-screen", new Screen.EntryPoint() { |
| 1644 | @Override |
| 1645 | public void onLoad(Screen screen) { |
| 1646 | screen.add(new InlineLabel("My Screen"); |
| 1647 | screen.show(); |
| 1648 | } |
| 1649 | }); |
| 1650 | } |
| 1651 | } |
| 1652 | ---- |
| 1653 | |
| Edwin Kempin | 289f1a0 | 2014-02-04 16:08:25 +0100 | [diff] [blame] | 1654 | [[settings-screen]] |
| 1655 | == Plugin Settings Screen |
| 1656 | |
| 1657 | If a plugin implements a screen for administrating its settings that is |
| 1658 | available under "#/x/<plugin-name>/settings" it is automatically linked |
| 1659 | from the plugin list screen. |
| 1660 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1661 | [[http]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1662 | == HTTP Servlets |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1663 | |
| 1664 | Plugins or extensions may register additional HTTP servlets, and |
| 1665 | wrap them with HTTP filters. |
| 1666 | |
| 1667 | Servlets may use auto-registration to declare the URL they handle: |
| 1668 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1669 | [source,java] |
| 1670 | ---- |
| 1671 | import com.google.gerrit.extensions.annotations.Export; |
| 1672 | import com.google.inject.Singleton; |
| 1673 | import javax.servlet.http.HttpServlet; |
| 1674 | import javax.servlet.http.HttpServletRequest; |
| 1675 | import javax.servlet.http.HttpServletResponse; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1676 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1677 | @Export("/print") |
| 1678 | @Singleton |
| 1679 | class HelloServlet extends HttpServlet { |
| 1680 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 1681 | res.setContentType("text/plain"); |
| 1682 | res.setCharacterEncoding("UTF-8"); |
| 1683 | res.getWriter().write("Hello"); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1684 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1685 | } |
| 1686 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1687 | |
| Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1688 | The auto registration only works for standard servlet mappings like |
| 1689 | `/foo` or `/foo/*`. Regex style bindings must use a Guice ServletModule |
| 1690 | to register the HTTP servlets and declare it explicitly in the manifest |
| 1691 | with the `Gerrit-HttpModule` attribute: |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1692 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1693 | [source,java] |
| 1694 | ---- |
| 1695 | import com.google.inject.servlet.ServletModule; |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1696 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1697 | class MyWebUrls extends ServletModule { |
| 1698 | protected void configureServlets() { |
| 1699 | serve("/print").with(HelloServlet.class); |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1700 | } |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1701 | } |
| 1702 | ---- |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1703 | |
| 1704 | For a plugin installed as name `helloworld`, the servlet implemented |
| 1705 | by HelloServlet class will be available to users as: |
| 1706 | |
| 1707 | ---- |
| 1708 | $ curl http://review.example.com/plugins/helloworld/print |
| 1709 | ---- |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1710 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1711 | [[data-directory]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1712 | == Data Directory |
| Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1713 | |
| 1714 | Plugins can request a data directory with a `@PluginData` File |
| 1715 | dependency. A data directory will be created automatically by the |
| 1716 | server in `$site_path/data/$plugin_name` and passed to the plugin. |
| 1717 | |
| 1718 | Plugins can use this to store any data they want. |
| 1719 | |
| David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1720 | [source,java] |
| 1721 | ---- |
| 1722 | @Inject |
| 1723 | MyType(@PluginData java.io.File myDir) { |
| 1724 | new FileInputStream(new File(myDir, "my.config")); |
| 1725 | } |
| 1726 | ---- |
| Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1727 | |
| Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 1728 | [[download-commands]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1729 | == Download Commands |
| Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 1730 | |
| 1731 | Gerrit offers commands for downloading changes using different |
| 1732 | download schemes (e.g. for downloading via different network |
| 1733 | protocols). Plugins can contribute download schemes and download |
| 1734 | commands by implementing |
| 1735 | `com.google.gerrit.extensions.config.DownloadScheme` and |
| 1736 | `com.google.gerrit.extensions.config.DownloadCommand`. |
| 1737 | |
| 1738 | The download schemes and download commands which are used most often |
| 1739 | are provided by the Gerrit core plugin `download-commands`. |
| 1740 | |
| Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 1741 | [[links-to-external-tools]] |
| 1742 | == Links To External Tools |
| 1743 | |
| 1744 | Gerrit has extension points that enables development of a |
| 1745 | light-weight plugin that links commits to external |
| 1746 | tools (GitBlit, CGit, company specific resources etc). |
| 1747 | |
| 1748 | PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI. |
| 1749 | |
| 1750 | [source, java] |
| 1751 | ---- |
| 1752 | import com.google.gerrit.extensions.annotations.Listen; |
| 1753 | import com.google.gerrit.extensions.webui.PatchSetWebLink;; |
| Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 1754 | import com.google.gerrit.extensions.webui.WebLinkTarget; |
| Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 1755 | |
| 1756 | @Listen |
| 1757 | public class MyWeblinkPlugin implements PatchSetWebLink { |
| 1758 | |
| 1759 | private String name = "MyLink"; |
| 1760 | private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s"; |
| Sven Selberg | 5548420 | 2014-06-26 08:48:51 +0200 | [diff] [blame] | 1761 | private String imageUrl = "http://placehold.it/16x16.gif"; |
| Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 1762 | |
| 1763 | @Override |
| Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 1764 | public WebLinkInfo getPathSetWebLink(String projectName, String commit) { |
| 1765 | return new WebLinkInfo(name, |
| 1766 | imageUrl, |
| 1767 | String.format(placeHolderUrlProjectCommit, project, commit), |
| 1768 | WebLinkTarget.BLANK); |
| Edwin Kempin | ceeed6b | 2014-09-11 17:07:33 +0200 | [diff] [blame] | 1769 | } |
| Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 1770 | } |
| 1771 | ---- |
| 1772 | |
| Edwin Kempin | b3696c8 | 2014-09-11 09:41:42 +0200 | [diff] [blame] | 1773 | FileWebLinks will appear in the side-by-side diff screen on the right |
| 1774 | side of the patch selection on each side. |
| 1775 | |
| Edwin Kempin | 8cdce50 | 2014-12-06 10:55:38 +0100 | [diff] [blame] | 1776 | DiffWebLinks will appear in the side-by-side and unified diff screen in |
| 1777 | the header next to the navigation icons. |
| 1778 | |
| Edwin Kempin | ea00475 | 2014-04-11 15:56:02 +0200 | [diff] [blame] | 1779 | ProjectWebLinks will appear in the project list in the |
| 1780 | `Repository Browser` column. |
| 1781 | |
| Edwin Kempin | 0f697bd | 2014-09-10 18:23:29 +0200 | [diff] [blame] | 1782 | BranchWebLinks will appear in the branch list in the last column. |
| 1783 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1784 | [[documentation]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1785 | == Documentation |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1786 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1787 | If a plugin does not register a filter or servlet to handle URLs |
| 1788 | `/Documentation/*` or `/static/*`, the core Gerrit server will |
| 1789 | automatically export these resources over HTTP from the plugin JAR. |
| 1790 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1791 | Static resources under the `static/` directory in the JAR will be |
| Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 1792 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 1793 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1794 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1795 | Documentation files under the `Documentation/` directory in the JAR |
| Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 1796 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 1797 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 1798 | attribute. |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1799 | |
| 1800 | Documentation may be written in |
| 1801 | link:http://daringfireball.net/projects/markdown/[Markdown] style |
| 1802 | if the file name ends with `.md`. Gerrit will automatically convert |
| 1803 | Markdown to HTML if accessed with extension `.html`. |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1804 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1805 | [[macros]] |
| Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 1806 | Within the Markdown documentation files macros can be used that allow |
| 1807 | to write documentation with reasonably accurate examples that adjust |
| 1808 | automatically based on the installation. |
| 1809 | |
| 1810 | The following macros are supported: |
| 1811 | |
| 1812 | [width="40%",options="header"] |
| 1813 | |=================================================== |
| 1814 | |Macro | Replacement |
| 1815 | |@PLUGIN@ | name of the plugin |
| 1816 | |@URL@ | Gerrit Web URL |
| 1817 | |@SSH_HOST@ | SSH Host |
| 1818 | |@SSH_PORT@ | SSH Port |
| 1819 | |=================================================== |
| 1820 | |
| 1821 | The macros will be replaced when the documentation files are rendered |
| 1822 | from Markdown to HTML. |
| 1823 | |
| 1824 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 1825 | even if there is an expansion for `KEEP` in the future. |
| 1826 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1827 | [[auto-index]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1828 | === Automatic Index |
| Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1829 | |
| 1830 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 1831 | redirect clients to the plugin's `/Documentation/index.html`. |
| 1832 | Requests for `/Documentation/` (bare directory) will also redirect |
| 1833 | to `/Documentation/index.html`. |
| 1834 | |
| 1835 | If neither resource `Documentation/index.html` or |
| 1836 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
| 1837 | automatically generate an index page for the plugin's documentation |
| 1838 | tree by scanning every `*.md` and `*.html` file in the Documentation/ |
| 1839 | directory. |
| 1840 | |
| 1841 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 1842 | header of the file and extract the first level one title. This |
| 1843 | title text will be used as display text for a link to the HTML |
| 1844 | version of the page. |
| 1845 | |
| 1846 | For any discovered HTML (`*.html`) file, Gerrit will use the name |
| 1847 | of the file, minus the `*.html` extension, as the link text. Any |
| 1848 | hyphens in the file name will be replaced with spaces. |
| 1849 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1850 | If a discovered file is named `about.md` or `about.html`, its |
| 1851 | content will be inserted in an 'About' section at the top of the |
| 1852 | auto-generated index page. If both `about.md` and `about.html` |
| 1853 | exist, only the first discovered file will be used. |
| 1854 | |
| Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1855 | If a discovered file name beings with `cmd-` it will be clustered |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1856 | into a 'Commands' section of the generated index page. |
| 1857 | |
| David Pursehouse | fe52915 | 2013-08-14 16:35:06 +0900 | [diff] [blame] | 1858 | If a discovered file name beings with `servlet-` it will be clustered |
| 1859 | into a 'Servlets' section of the generated index page. |
| 1860 | |
| 1861 | If a discovered file name beings with `rest-api-` it will be clustered |
| 1862 | into a 'REST APIs' section of the generated index page. |
| 1863 | |
| David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 1864 | All other files are clustered under a 'Documentation' section. |
| Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 1865 | |
| 1866 | Some optional information from the manifest is extracted and |
| 1867 | displayed as part of the index page, if present in the manifest: |
| 1868 | |
| 1869 | [width="40%",options="header"] |
| 1870 | |=================================================== |
| 1871 | |Field | Source Attribute |
| 1872 | |Name | Implementation-Title |
| 1873 | |Vendor | Implementation-Vendor |
| 1874 | |Version | Implementation-Version |
| 1875 | |URL | Implementation-URL |
| 1876 | |API Version | Gerrit-ApiVersion |
| 1877 | |=================================================== |
| 1878 | |
| Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1879 | [[deployment]] |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1880 | == Deployment |
| Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1881 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 1882 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 1883 | server using the link:cmd-plugin-install.html[plugin install] command. |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1884 | |
| David Pursehouse | a1d633b | 2014-05-02 17:21:02 +0900 | [diff] [blame] | 1885 | Web UI plugins distributed as single `.js` file can be deployed |
| Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame] | 1886 | without the overhead of JAR packaging, for more information refer to |
| 1887 | link:cmd-plugin-install.html[plugin install] command. |
| 1888 | |
| Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1889 | Plugins can also be copied directly into the server's |
| Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame] | 1890 | directory at `$site_path/plugins/$name.(jar|js)`. The name of |
| 1891 | 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] | 1892 | plugin name. Unless disabled, servers periodically scan this |
| 1893 | directory for updated plugins. The time can be adjusted by |
| 1894 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1895 | |
| Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 1896 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 1897 | command can be used. |
| 1898 | |
| Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 1899 | Disabled plugins can be re-enabled using the |
| 1900 | link:cmd-plugin-enable.html[plugin enable] command. |
| 1901 | |
| Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1902 | == SEE ALSO |
| David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1903 | |
| 1904 | * link:js-api.html[JavaScript API] |
| 1905 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 1906 | |
| Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 1907 | GERRIT |
| 1908 | ------ |
| 1909 | Part of link:index.html[Gerrit Code Review] |
| Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 1910 | |
| 1911 | SEARCHBOX |
| 1912 | --------- |