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 \ |
David Pursehouse | 481ba35 | 2017-01-06 09:47:10 +0900 | [diff] [blame] | 39 | -DarchetypeVersion=2.13.5 \ |
Edwin Kempin | 91155c2 | 2013-12-02 20:25:18 +0100 | [diff] [blame] | 40 | -DgroupId=com.googlesource.gerrit.plugins.testplugin \ |
| 41 | -DartifactId=testplugin |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 42 | ---- |
| 43 | + |
| 44 | Maven will ask for additional properties and then create the plugin in |
| 45 | the current directory. To change the default property values answer 'n' |
| 46 | when Maven asks to confirm the properties configuration. It will then |
| 47 | ask again for all properties including those with predefined default |
| 48 | values. |
| 49 | |
David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 50 | . clone the sample plugin: |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 51 | + |
David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 52 | This is a project that demonstrates the various features of the |
| 53 | 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] | 54 | + |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 55 | ---- |
David Pursehouse | 2cf0cb5 | 2013-08-27 16:09:53 +0900 | [diff] [blame] | 56 | $ git clone https://gerrit.googlesource.com/plugins/cookbook-plugin |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 57 | ---- |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 58 | + |
| 59 | When starting from this example one should take care to adapt the |
| 60 | `Gerrit-ApiVersion` in the `pom.xml` to the version of Gerrit for which |
| 61 | the plugin is developed. If the plugin is developed for a released |
| 62 | Gerrit version (no `SNAPSHOT` version) then the URL for the |
| 63 | `gerrit-api-repository` in the `pom.xml` needs to be changed to |
Shawn Pearce | d500500 | 2013-06-21 11:01:45 -0700 | [diff] [blame] | 64 | `https://gerrit-api.storage.googleapis.com/release/`. |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 65 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 66 | [[API]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 67 | == API |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 68 | |
| 69 | There are two different API formats offered against which plugins can |
| 70 | be developed: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 71 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 72 | gerrit-extension-api.jar:: |
| 73 | A stable but thin interface. Suitable for extensions that need |
| 74 | to be notified of events, but do not require tight coupling to |
| 75 | the internals of Gerrit. Extensions built against this API can |
| 76 | expect to be binary compatible across a wide range of server |
| 77 | versions. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 78 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 79 | gerrit-plugin-api.jar:: |
| 80 | The complete internals of the Gerrit server, permitting a |
| 81 | plugin to tightly couple itself and provide additional |
| 82 | functionality that is not possible as an extension. Plugins |
| 83 | built against this API are expected to break at the source |
| 84 | code level between every major.minor Gerrit release. A plugin |
| 85 | that compiles against 2.5 will probably need source code level |
| 86 | changes to work with 2.6, 2.7, and so on. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 87 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 88 | == Manifest |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 89 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 90 | Plugins may provide optional description information with standard |
| 91 | manifest fields: |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 92 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 93 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 94 | Implementation-Title: Example plugin showing examples |
| 95 | Implementation-Version: 1.0 |
| 96 | Implementation-Vendor: Example, Inc. |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 97 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 98 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 99 | === ApiType |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 100 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 101 | Plugins using the tightly coupled `gerrit-plugin-api.jar` must |
| 102 | declare this API dependency in the manifest to gain access to server |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 103 | internals. If no `Gerrit-ApiType` is specified the stable `extension` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 104 | API will be assumed. This may cause ClassNotFoundExceptions when |
| 105 | loading a plugin that needs the plugin API. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 106 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 107 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 108 | Gerrit-ApiType: plugin |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 109 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 110 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 111 | === Explicit Registration |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 112 | |
| 113 | Plugins that use explicit Guice registration must name the Guice |
| 114 | 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] | 115 | manifest. `Gerrit-Module` supplies bindings to the core server; |
| 116 | `Gerrit-SshModule` supplies SSH commands to the SSH server (if |
| 117 | enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 118 | server (if enabled). If no modules are named automatic registration |
| 119 | will be performed by scanning all classes in the plugin JAR for |
| 120 | `@Listen` and `@Export("")` annotations. |
| 121 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 122 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 123 | Gerrit-Module: tld.example.project.CoreModuleClassName |
| 124 | Gerrit-SshModule: tld.example.project.SshModuleClassName |
| 125 | Gerrit-HttpModule: tld.example.project.HttpModuleClassName |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 126 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 127 | |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 128 | [[plugin_name]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 129 | === Plugin Name |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 130 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 131 | A plugin can optionally provide its own plugin name. |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 132 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 133 | ---- |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 134 | Gerrit-PluginName: replication |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 135 | ---- |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 136 | |
| 137 | This is useful for plugins that contribute plugin-owned capabilities that |
| 138 | are stored in the `project.config` file. Another use case is to be able to put |
| 139 | project specific plugin configuration section in `project.config`. In this |
| 140 | case it is advantageous to reserve the plugin name to access the configuration |
| 141 | section in the `project.config` file. |
| 142 | |
| 143 | If `Gerrit-PluginName` is omitted, then the plugin's name is determined from |
| 144 | the plugin file name. |
| 145 | |
| 146 | If a plugin provides its own name, then that plugin cannot be deployed |
| 147 | multiple times under different file names on one Gerrit site. |
| 148 | |
| 149 | For Maven driven plugins, the following line must be included in the pom.xml |
| 150 | file: |
| 151 | |
| 152 | [source,xml] |
| 153 | ---- |
| 154 | <manifestEntries> |
| 155 | <Gerrit-PluginName>name</Gerrit-PluginName> |
| 156 | </manifestEntries> |
| 157 | ---- |
| 158 | |
| 159 | For Buck driven plugins, the following line must be included in the BUCK |
| 160 | configuration file: |
| 161 | |
| 162 | [source,python] |
| 163 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 164 | manifest_entries = [ |
| 165 | 'Gerrit-PluginName: name', |
| 166 | ] |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 167 | ---- |
| 168 | |
Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 169 | A plugin can get its own name injected at runtime: |
| 170 | |
| 171 | [source,java] |
| 172 | ---- |
| 173 | public class MyClass { |
| 174 | |
| 175 | private final String pluginName; |
| 176 | |
| 177 | @Inject |
| 178 | public MyClass(@PluginName String pluginName) { |
| 179 | this.pluginName = pluginName; |
| 180 | } |
| 181 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 182 | [...] |
Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 183 | } |
| 184 | ---- |
| 185 | |
David Pursehouse | 8ed0d92 | 2013-10-18 18:57:56 +0900 | [diff] [blame] | 186 | A plugin can get its canonical web URL injected at runtime: |
| 187 | |
| 188 | [source,java] |
| 189 | ---- |
| 190 | public class MyClass { |
| 191 | |
| 192 | private final String url; |
| 193 | |
| 194 | @Inject |
| 195 | public MyClass(@PluginCanonicalWebUrl String url) { |
| 196 | this.url = url; |
| 197 | } |
| 198 | |
| 199 | [...] |
| 200 | } |
| 201 | ---- |
| 202 | |
| 203 | The URL is composed of the server's canonical web URL and the plugin's |
| 204 | name, i.e. `http://review.example.com:8080/plugin-name`. |
| 205 | |
| 206 | The canonical web URL may be injected into any .jar plugin regardless of |
| 207 | whether or not the plugin provides an HTTP servlet. |
| 208 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 209 | [[reload_method]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 210 | === Reload Method |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 211 | |
| 212 | If a plugin holds an exclusive resource that must be released before |
| 213 | loading the plugin again (for example listening on a network port or |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 214 | acquiring a file lock) the manifest must declare `Gerrit-ReloadMode` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 215 | to be `restart`. Otherwise the preferred method of `reload` will |
| 216 | be used, as it enables the server to hot-patch an updated plugin |
| 217 | with no down time. |
| 218 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 219 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 220 | Gerrit-ReloadMode: restart |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 221 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 222 | |
| 223 | In either mode ('restart' or 'reload') any plugin or extension can |
| 224 | be updated without restarting the Gerrit server. The difference is |
| 225 | how Gerrit handles the upgrade: |
| 226 | |
| 227 | restart:: |
| 228 | The old plugin is completely stopped. All registrations of SSH |
| 229 | commands and HTTP servlets are removed. All registrations of any |
| 230 | extension points are removed. All registered LifecycleListeners |
| 231 | have their `stop()` method invoked in reverse order. The new |
| 232 | plugin is started, and registrations are made from the new |
| 233 | plugin. There is a brief window where neither the old nor the |
| 234 | new plugin is connected to the server. This means SSH commands |
| 235 | and HTTP servlets will return not found errors, and the plugin |
| 236 | will not be notified of events that occurred during the restart. |
| 237 | |
| 238 | reload:: |
| 239 | The new plugin is started. Its LifecycleListeners are permitted |
| 240 | to perform their `start()` methods. All SSH and HTTP registrations |
| 241 | are atomically swapped out from the old plugin to the new plugin, |
| 242 | ensuring the server never returns a not found error. All extension |
| 243 | point listeners are atomically swapped out from the old plugin to |
| 244 | the new plugin, ensuring no events are missed (however some events |
| 245 | may still route to the old plugin if the swap wasn't complete yet). |
| 246 | The old plugin is stopped. |
| 247 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 248 | To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload] |
| 249 | command can be used. |
| 250 | |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 251 | [[init_step]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 252 | === Init step |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 253 | |
| 254 | Plugins can contribute their own "init step" during the Gerrit init |
| 255 | wizard. This is useful for guiding the Gerrit administrator through |
David Pursehouse | 659860f | 2013-12-16 14:50:04 +0900 | [diff] [blame] | 256 | the settings needed by the plugin to work properly. |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 257 | |
| 258 | For instance plugins to integrate Jira issues to Gerrit changes may |
| 259 | contribute their own "init step" to allow configuring the Jira URL, |
| 260 | credentials and possibly verify connectivity to validate them. |
| 261 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 262 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 263 | Gerrit-InitStep: tld.example.project.MyInitStep |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 264 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 265 | |
| 266 | MyInitStep needs to follow the standard Gerrit InitStep syntax |
David Pursehouse | 9246356 | 2013-06-24 10:16:28 +0900 | [diff] [blame] | 267 | and behavior: writing to the console using the injected ConsoleUI |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 268 | and accessing / changing configuration settings using Section.Factory. |
| 269 | |
| 270 | In addition to the standard Gerrit init injections, plugins receive |
| 271 | the @PluginName String injection containing their own plugin name. |
| 272 | |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 273 | During their initialization plugins may get access to the |
| 274 | `project.config` file of the `All-Projects` project and they are able |
| 275 | to store configuration parameters in it. For this a plugin `InitStep` |
Jiří Engelthaler | 3033a0a | 2015-02-16 09:44:32 +0100 | [diff] [blame] | 276 | can get `com.google.gerrit.pgm.init.api.AllProjectsConfig` injected: |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 277 | |
| 278 | [source,java] |
| 279 | ---- |
| 280 | public class MyInitStep implements InitStep { |
| 281 | private final String pluginName; |
| 282 | private final ConsoleUI ui; |
| 283 | private final AllProjectsConfig allProjectsConfig; |
| 284 | |
Doug Kelly | 732ad20 | 2015-11-13 13:11:32 -0800 | [diff] [blame] | 285 | @Inject |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 286 | public MyInitStep(@PluginName String pluginName, ConsoleUI ui, |
| 287 | AllProjectsConfig allProjectsConfig) { |
| 288 | this.pluginName = pluginName; |
| 289 | this.ui = ui; |
| 290 | this.allProjectsConfig = allProjectsConfig; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void run() throws Exception { |
Edwin Kempin | 93e7d5d | 2014-01-03 09:53:20 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void postRun() throws Exception { |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 299 | ui.message("\n"); |
| 300 | ui.header(pluginName + " Integration"); |
| 301 | boolean enabled = ui.yesno(true, "By default enabled for all projects"); |
Adrian Görler | d161297 | 2014-10-20 17:06:07 +0200 | [diff] [blame] | 302 | Config cfg = allProjectsConfig.load().getConfig(); |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 303 | if (enabled) { |
| 304 | cfg.setBoolean("plugin", pluginName, "enabled", enabled); |
| 305 | } else { |
| 306 | cfg.unset("plugin", pluginName, "enabled"); |
| 307 | } |
| 308 | allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration"); |
| 309 | } |
| 310 | } |
| 311 | ---- |
| 312 | |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 313 | Bear in mind that the Plugin's InitStep class will be loaded but |
| 314 | the standard Gerrit runtime environment is not available and the plugin's |
| 315 | own Guice modules were not initialized. |
| 316 | This means the InitStep for a plugin is not executed in the same way that |
| 317 | the plugin executes within the server, and may mean a plugin author cannot |
| 318 | trivially reuse runtime code during init. |
| 319 | |
| 320 | For instance a plugin that wants to verify connectivity may need to statically |
| 321 | call the constructor of their connection class, passing in values obtained |
| 322 | from the Section.Factory rather than from an injected Config object. |
| 323 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 324 | Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after |
| 325 | the extraction of the plugins embedded in the distribution .war file into |
| 326 | `$GERRIT_SITE/plugins` and before the DB Schema initialization or upgrade. |
| 327 | |
| 328 | A plugin's InitStep cannot refer to Gerrit's DB Schema or any other Gerrit |
| 329 | runtime objects injected at startup. |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 330 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 331 | [source,java] |
| 332 | ---- |
| 333 | public class MyInitStep implements InitStep { |
| 334 | private final ConsoleUI ui; |
| 335 | private final Section.Factory sections; |
| 336 | private final String pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 337 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 338 | @Inject |
| 339 | public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) { |
| 340 | this.ui = ui; |
| 341 | this.sections = sections; |
| 342 | this.pluginName = pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 343 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 344 | |
| 345 | @Override |
| 346 | public void run() throws Exception { |
| 347 | ui.header("\nMy plugin"); |
| 348 | |
| 349 | Section mySection = getSection("myplugin", null); |
| 350 | mySection.string("Link name", "linkname", "MyLink"); |
| 351 | } |
Edwin Kempin | 93e7d5d | 2014-01-03 09:53:20 +0100 | [diff] [blame] | 352 | |
| 353 | @Override |
| 354 | public void postRun() throws Exception { |
| 355 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 356 | } |
| 357 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 358 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 359 | [[classpath]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 360 | == Classpath |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 361 | |
| 362 | Each plugin is loaded into its own ClassLoader, isolating plugins |
| 363 | from each other. A plugin or extension inherits the Java runtime |
| 364 | and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin) |
| 365 | from the hosting server. |
| 366 | |
| 367 | Plugins are loaded from a single JAR file. If a plugin needs |
| 368 | additional libraries, it must include those dependencies within |
| 369 | its own JAR. Plugins built using Maven may be able to use the |
| 370 | link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin] |
| 371 | to package additional dependencies. Relocating (or renaming) classes |
| 372 | should not be necessary due to the ClassLoader isolation. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 373 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 374 | [[events]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 375 | == Listening to Events |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 376 | |
| 377 | Certain operations in Gerrit trigger events. Plugins may receive |
| 378 | notifications of these events by implementing the corresponding |
| 379 | listeners. |
| 380 | |
Martin Fick | 4c72aea | 2014-12-10 14:58:12 -0700 | [diff] [blame] | 381 | * `com.google.gerrit.common.EventListener`: |
Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 382 | + |
Hugo Arès | bc1093d | 2016-02-23 15:04:50 -0500 | [diff] [blame] | 383 | Allows to listen to events without user visibility restrictions. These |
| 384 | are the same link:cmd-stream-events.html#events[events] that are also streamed by |
Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 385 | the link:cmd-stream-events.html[gerrit stream-events] command. |
| 386 | |
Hugo Arès | bc1093d | 2016-02-23 15:04:50 -0500 | [diff] [blame] | 387 | * `com.google.gerrit.common.UserScopedEventListener`: |
| 388 | + |
| 389 | Allows to listen to events visible to the specified user. These are the |
| 390 | same link:cmd-stream-events.html#events[events] that are also streamed |
| 391 | by the link:cmd-stream-events.html[gerrit stream-events] command. |
| 392 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 393 | * `com.google.gerrit.extensions.events.LifecycleListener`: |
| 394 | + |
Edwin Kempin | 3e7928a | 2013-12-03 07:39:00 +0100 | [diff] [blame] | 395 | Plugin start and stop |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 396 | |
| 397 | * `com.google.gerrit.extensions.events.NewProjectCreatedListener`: |
| 398 | + |
| 399 | Project creation |
| 400 | |
| 401 | * `com.google.gerrit.extensions.events.ProjectDeletedListener`: |
| 402 | + |
| 403 | Project deletion |
| 404 | |
Edwin Kempin | b27c939 | 2013-11-19 13:12:43 +0100 | [diff] [blame] | 405 | * `com.google.gerrit.extensions.events.HeadUpdatedListener`: |
| 406 | + |
| 407 | Update of HEAD on a project |
| 408 | |
Stefan Lay | 310d77d | 2014-05-28 13:45:25 +0200 | [diff] [blame] | 409 | * `com.google.gerrit.extensions.events.UsageDataPublishedListener`: |
| 410 | + |
| 411 | Publication of usage data |
| 412 | |
Adrian Görler | f4a4c9a | 2014-08-22 17:09:18 +0200 | [diff] [blame] | 413 | * `com.google.gerrit.extensions.events.GarbageCollectorListener`: |
| 414 | + |
| 415 | Garbage collection ran on a project |
| 416 | |
Hector Oswaldo Caballero | 5fbbdad | 2015-11-11 14:30:46 -0500 | [diff] [blame] | 417 | * `com.google.gerrit.server.extensions.events.ChangeIndexedListener`: |
| 418 | + |
| 419 | Update of the secondary index |
| 420 | |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 421 | [[stream-events]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 422 | == Sending Events to the Events Stream |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 423 | |
| 424 | Plugins may send events to the events stream where consumers of |
| 425 | Gerrit's `stream-events` ssh command will receive them. |
| 426 | |
| 427 | To send an event, the plugin must invoke one of the `postEvent` |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 428 | methods in the `EventDispatcher` interface, passing an instance of |
Martin Fick | 4c72aea | 2014-12-10 14:58:12 -0700 | [diff] [blame] | 429 | its own custom event class derived from |
| 430 | `com.google.gerrit.server.events.Event`. |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 431 | |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 432 | [source,java] |
| 433 | ---- |
| 434 | import com.google.gerrit.common.EventDispatcher; |
| 435 | import com.google.gerrit.extensions.registration.DynamicItem; |
| 436 | import com.google.gwtorm.server.OrmException; |
| 437 | import com.google.inject.Inject; |
| 438 | |
| 439 | class MyPlugin { |
| 440 | private final DynamicItem<EventDispatcher> eventDispatcher; |
| 441 | |
| 442 | @Inject |
| 443 | myPlugin(DynamicItem<EventDispatcher> eventDispatcher) { |
| 444 | this.eventDispatcher = eventDispatcher; |
| 445 | } |
| 446 | |
| 447 | private void postEvent(MyPluginEvent event) { |
| 448 | try { |
| 449 | eventDispatcher.get().postEvent(event); |
| 450 | } catch (OrmException e) { |
| 451 | // error handling |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | ---- |
| 456 | |
Martin Fick | 0aef6f1 | 2014-12-11 16:54:21 -0700 | [diff] [blame] | 457 | Plugins which define new Events should register them via the |
| 458 | `com.google.gerrit.server.events.EventTypes.registerClass()` |
| 459 | method. This will make the EventType known to the system. |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 460 | Deserializing events with the |
Martin Fick | f70c20a | 2014-12-11 17:03:15 -0700 | [diff] [blame] | 461 | `com.google.gerrit.server.events.EventDeserializer` class requires |
| 462 | that the event be registered in EventTypes. |
Martin Fick | 0aef6f1 | 2014-12-11 16:54:21 -0700 | [diff] [blame] | 463 | |
Martin Fick | ecafc93 | 2014-12-15 14:09:41 -0700 | [diff] [blame] | 464 | == Modifying the Stream Event Flow |
| 465 | |
| 466 | It is possible to modify the stream event flow from plugins by registering |
| 467 | an `com.google.gerrit.server.events.EventDispatcher`. A plugin may register |
| 468 | a Dispatcher class to replace the internal Dispatcher. EventDispatcher is |
| 469 | a DynamicItem, so Gerrit may only have one copy. |
| 470 | |
Edwin Kempin | 3273760 | 2014-01-23 09:04:58 +0100 | [diff] [blame] | 471 | [[validation]] |
David Pursehouse | 91c5f5e | 2014-01-23 18:57:33 +0900 | [diff] [blame] | 472 | == Validation Listeners |
Edwin Kempin | 3273760 | 2014-01-23 09:04:58 +0100 | [diff] [blame] | 473 | |
| 474 | Certain operations in Gerrit can be validated by plugins by |
| 475 | implementing the corresponding link:config-validation.html[listeners]. |
| 476 | |
Saša Živkov | ec85a07 | 2014-01-28 10:08:25 +0100 | [diff] [blame] | 477 | [[receive-pack]] |
| 478 | == Receive Pack Initializers |
| 479 | |
| 480 | Plugins may provide ReceivePack initializers which will be invoked |
| 481 | by Gerrit just before a ReceivePack instance will be used. Usually, |
| 482 | plugins will make use of the setXXX methods on the ReceivePack to |
| 483 | set additional properties on it. |
| 484 | |
Saša Živkov | 626c731 | 2014-02-24 17:15:08 +0100 | [diff] [blame] | 485 | [[post-receive-hook]] |
| 486 | == Post Receive-Pack Hooks |
| 487 | |
| 488 | Plugins may register PostReceiveHook instances in order to get |
| 489 | notified when JGit successfully receives a pack. This may be useful |
| 490 | for those plugins which would like to monitor changes in Git |
| 491 | repositories. |
| 492 | |
Hugo Arès | 572d542 | 2014-06-17 14:22:03 -0400 | [diff] [blame] | 493 | [[pre-upload-hook]] |
| 494 | == Pre Upload-Pack Hooks |
| 495 | |
| 496 | Plugins may register PreUploadHook instances in order to get |
| 497 | notified when JGit is about to upload a pack. This may be useful |
| 498 | for those plugins which would like to monitor usage in Git |
| 499 | repositories. |
| 500 | |
Hugo Arès | 41b4c0d | 2016-08-02 15:26:57 -0400 | [diff] [blame] | 501 | [[post-upload-hook]] |
| 502 | == Post Upload-Pack Hooks |
| 503 | |
| 504 | Plugins may register PostUploadHook instances in order to get notified after |
| 505 | JGit is done uploading a pack. |
| 506 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 507 | [[ssh]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 508 | == SSH Commands |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 509 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 510 | Plugins may provide commands that can be accessed through the SSH |
| 511 | interface (extensions do not have this option). |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 512 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 513 | Command implementations must extend the base class SshCommand: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 514 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 515 | [source,java] |
| 516 | ---- |
| 517 | import com.google.gerrit.sshd.SshCommand; |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 518 | import com.google.gerrit.sshd.CommandMetaData; |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 519 | |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 520 | @CommandMetaData(name="print", description="Print hello command") |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 521 | class PrintHello extends SshCommand { |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 522 | @Override |
| 523 | protected void run() { |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 524 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 525 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 526 | } |
| 527 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 528 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 529 | If no Guice modules are declared in the manifest, SSH commands may |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 530 | use auto-registration by providing an `@Export` annotation: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 531 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 532 | [source,java] |
| 533 | ---- |
| 534 | import com.google.gerrit.extensions.annotations.Export; |
| 535 | import com.google.gerrit.sshd.SshCommand; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 536 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 537 | @Export("print") |
| 538 | class PrintHello extends SshCommand { |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 539 | @Override |
| 540 | protected void run() { |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 541 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 542 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 543 | } |
| 544 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 545 | |
| 546 | If explicit registration is being used, a Guice module must be |
| 547 | supplied to register the SSH command and declared in the manifest |
| 548 | with the `Gerrit-SshModule` attribute: |
| 549 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 550 | [source,java] |
| 551 | ---- |
| 552 | import com.google.gerrit.sshd.PluginCommandModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 553 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 554 | class MyCommands extends PluginCommandModule { |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 555 | @Override |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 556 | protected void configureCommands() { |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 557 | command(PrintHello.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 558 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 559 | } |
| 560 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 561 | |
| 562 | For a plugin installed as name `helloworld`, the command implemented |
| 563 | by PrintHello class will be available to users as: |
| 564 | |
| 565 | ---- |
Keunhong Park | a09a6f1 | 2012-07-10 14:45:02 -0600 | [diff] [blame] | 566 | $ ssh -p 29418 review.example.com helloworld print |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 567 | ---- |
| 568 | |
David Ostrovsky | 79c4d89 | 2014-03-15 13:52:46 +0100 | [diff] [blame] | 569 | [[multiple-commands]] |
| 570 | === Multiple Commands bound to one implementation |
| 571 | |
David Ostrovsky | e3172b3 | 2013-10-13 14:19:13 +0200 | [diff] [blame] | 572 | Multiple SSH commands can be bound to the same implementation class. For |
| 573 | example a Gerrit Shell plugin can bind different shell commands to the same |
| 574 | implementation class: |
| 575 | |
| 576 | [source,java] |
| 577 | ---- |
| 578 | public class SshShellModule extends PluginCommandModule { |
| 579 | @Override |
| 580 | protected void configureCommands() { |
| 581 | command("ls").to(ShellCommand.class); |
| 582 | command("ps").to(ShellCommand.class); |
| 583 | [...] |
| 584 | } |
| 585 | } |
| 586 | ---- |
| 587 | |
| 588 | With the possible implementation: |
| 589 | |
| 590 | [source,java] |
| 591 | ---- |
| 592 | public class ShellCommand extends SshCommand { |
| 593 | @Override |
| 594 | protected void run() throws UnloggedFailure { |
| 595 | String cmd = getName().substring(getPluginName().length() + 1); |
| 596 | ProcessBuilder proc = new ProcessBuilder(cmd); |
| 597 | Process cmd = proc.start(); |
| 598 | [...] |
| 599 | } |
| 600 | } |
| 601 | ---- |
| 602 | |
| 603 | And the call: |
| 604 | |
| 605 | ---- |
| 606 | $ ssh -p 29418 review.example.com shell ls |
| 607 | $ ssh -p 29418 review.example.com shell ps |
| 608 | ---- |
| 609 | |
David Ostrovsky | 79c4d89 | 2014-03-15 13:52:46 +0100 | [diff] [blame] | 610 | [[root-level-commands]] |
| 611 | === Root Level Commands |
| 612 | |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 613 | Single command plugins are also supported. In this scenario plugin binds |
| 614 | SSH command to its own name. `SshModule` must inherit from |
| 615 | `SingleCommandPluginModule` class: |
| 616 | |
| 617 | [source,java] |
| 618 | ---- |
| 619 | public class SshModule extends SingleCommandPluginModule { |
| 620 | @Override |
| 621 | protected void configure(LinkedBindingBuilder<Command> b) { |
| 622 | b.to(ShellCommand.class); |
| 623 | } |
| 624 | } |
| 625 | ---- |
| 626 | |
| 627 | 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] | 628 | directory, generic commands can be called without specifying the |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 629 | actual SSH command. Note in the example below, that the called commands |
| 630 | `ls` and `ps` was not explicitly bound: |
| 631 | |
| 632 | ---- |
| 633 | $ ssh -p 29418 review.example.com sh ls |
| 634 | $ ssh -p 29418 review.example.com sh ps |
| 635 | ---- |
| 636 | |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 637 | [[search_operators]] |
| 638 | === Search Operators === |
| 639 | |
| 640 | Plugins can define new search operators to extend change searching by |
| 641 | implementing the `ChangeQueryBuilder.ChangeOperatorFactory` interface |
| 642 | and registering it to an operator name in the plugin module's |
| 643 | `configure()` method. The search operator name is defined during |
| 644 | registration via the DynamicMap annotation mechanism. The plugin |
| 645 | name will get appended to the annotated name, with an underscore |
| 646 | in between, leading to the final operator name. An example |
| 647 | registration looks like this: |
| 648 | |
| 649 | bind(ChangeOperatorFactory.class) |
| 650 | .annotatedWith(Exports.named("sample")) |
| 651 | .to(SampleOperator.class); |
| 652 | |
| 653 | If this is registered in the `myplugin` plugin, then the resulting |
| 654 | operator will be named `sample_myplugin`. |
| 655 | |
| 656 | The search operator itself is implemented by ensuring that the |
| 657 | `create()` method of the class implementing the |
| 658 | `ChangeQueryBuilder.ChangeOperatorFactory` interface returns a |
| 659 | `Predicate<ChangeData>`. Here is a sample operator factory |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 660 | definition which creates a `MyPredicate`: |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 661 | |
| 662 | [source,java] |
| 663 | ---- |
| 664 | @Singleton |
| 665 | public class SampleOperator |
| 666 | implements ChangeQueryBuilder.ChangeOperatorFactory { |
Edwin Kempin | cc82b24 | 2016-06-28 10:00:53 +0200 | [diff] [blame] | 667 | public static class MyPredicate extends OperatorChangePredicate<ChangeData> { |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 668 | ... |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public Predicate<ChangeData> create(ChangeQueryBuilder builder, String value) |
| 673 | throws QueryParseException { |
| 674 | return new MyPredicate(value); |
| 675 | } |
| 676 | } |
| 677 | ---- |
| 678 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 679 | [[simple-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 680 | == Simple Configuration in `gerrit.config` |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 681 | |
| 682 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 683 | If a plugin needs global configuration, this configuration should be |
| 684 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 685 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 686 | This approach of storing the plugin configuration is only suitable for |
| 687 | plugins that have a simple configuration that only consists of |
| 688 | key-value pairs. With this approach it is not possible to have |
| 689 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 690 | configuration need to store their configuration in their |
| 691 | link:#configuration[own configuration file] where they can make use of |
| 692 | subsections. On the other hand storing the plugin configuration in a |
| 693 | 'plugin' subsection in the `gerrit.config` file has the advantage that |
| 694 | administrators have all configuration parameters in one file, instead |
| 695 | of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 696 | |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 697 | To avoid conflicts with other plugins, it is recommended that plugins |
| 698 | only use the `plugin` subsection with their own name. For example the |
| 699 | `helloworld` plugin should store its configuration in the |
| 700 | `plugin.helloworld` subsection: |
| 701 | |
| 702 | ---- |
| 703 | [plugin "helloworld"] |
| 704 | language = Latin |
| 705 | ---- |
| 706 | |
Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 707 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 708 | plugin can easily access its configuration and there is no need for a |
| 709 | plugin to parse the `gerrit.config` file on its own: |
| 710 | |
| 711 | [source,java] |
| 712 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 713 | @Inject |
| 714 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 715 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 716 | [...] |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 717 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 718 | String language = cfg.getFromGerritConfig("helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 719 | .getString("language", "English"); |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 720 | ---- |
| 721 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 722 | [[configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 723 | == Configuration in own config file |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 724 | |
| 725 | Plugins can store their configuration in an own configuration file. |
| 726 | This makes sense if the plugin configuration is rather complex and |
| 727 | requires the usage of subsections. Plugins that have a simple |
| 728 | key-value pair configuration can store their configuration in a |
| 729 | link:#simple-configuration[`plugin` subsection of the `gerrit.config` |
| 730 | file]. |
| 731 | |
| 732 | The plugin configuration file must be named after the plugin and must |
| 733 | be located in the `etc` folder of the review site. For example a |
| 734 | configuration file for a `default-reviewer` plugin could look like |
| 735 | this: |
| 736 | |
| 737 | .$site_path/etc/default-reviewer.config |
| 738 | ---- |
| 739 | [branch "refs/heads/master"] |
| 740 | reviewer = Project Owners |
| 741 | reviewer = john.doe@example.com |
| 742 | [match "file:^.*\.txt"] |
| 743 | reviewer = My Info Developers |
| 744 | ---- |
| 745 | |
David Pursehouse | 5b47bc4 | 2016-07-22 11:00:25 +0900 | [diff] [blame] | 746 | Plugins that have sensitive configuration settings can store those settings in |
| 747 | an own secure configuration file. The plugin's secure configuration file must be |
| 748 | named after the plugin and must be located in the `etc` folder of the review |
| 749 | site. For example a secure configuration file for a `default-reviewer` plugin |
| 750 | could look like this: |
| 751 | |
| 752 | .$site_path/etc/default-reviewer.secure.config |
| 753 | ---- |
| 754 | [auth] |
| 755 | password = secret |
| 756 | ---- |
| 757 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 758 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 759 | plugin can easily access its configuration: |
| 760 | |
| 761 | [source,java] |
| 762 | ---- |
| 763 | @Inject |
| 764 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 765 | |
| 766 | [...] |
| 767 | |
| 768 | String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer") |
| 769 | .getStringList("branch", "refs/heads/master", "reviewer"); |
David Pursehouse | 5b47bc4 | 2016-07-22 11:00:25 +0900 | [diff] [blame] | 770 | String password = cfg.getGlobalPluginConfig("default-reviewer") |
| 771 | .getString("auth", null, "password"); |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 772 | ---- |
| 773 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 774 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 775 | [[simple-project-specific-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 776 | == Simple Project Specific Configuration in `project.config` |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 777 | |
| 778 | In Gerrit, project specific configuration is stored in the project's |
| 779 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 780 | needs configuration on project level (e.g. to enable its functionality |
| 781 | only for certain projects), this configuration should be stored in a |
| 782 | `plugin` subsection in the project's `project.config` file. |
| 783 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 784 | This approach of storing the plugin configuration is only suitable for |
| 785 | plugins that have a simple configuration that only consists of |
| 786 | key-value pairs. With this approach it is not possible to have |
| 787 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 788 | configuration need to store their configuration in their |
| 789 | link:#project-specific-configuration[own configuration file] where they |
| 790 | can make use of subsections. On the other hand storing the plugin |
| 791 | configuration in a 'plugin' subsection in the `project.config` file has |
| 792 | the advantage that project owners have all configuration parameters in |
| 793 | one file, instead of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 794 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 795 | To avoid conflicts with other plugins, it is recommended that plugins |
| 796 | only use the `plugin` subsection with their own name. For example the |
| 797 | `helloworld` plugin should store its configuration in the |
| 798 | `plugin.helloworld` subsection: |
| 799 | |
| 800 | ---- |
| 801 | [plugin "helloworld"] |
| 802 | enabled = true |
| 803 | ---- |
| 804 | |
| 805 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 806 | plugin can easily access its project specific configuration and there |
| 807 | is no need for a plugin to parse the `project.config` file on its own: |
| 808 | |
| 809 | [source,java] |
| 810 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 811 | @Inject |
| 812 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 813 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 814 | [...] |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 815 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 816 | boolean enabled = cfg.getFromProjectConfig(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 817 | .getBoolean("enabled", false); |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 818 | ---- |
| 819 | |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 820 | It is also possible to get missing configuration parameters inherited |
| 821 | from the parent projects: |
| 822 | |
| 823 | [source,java] |
| 824 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 825 | @Inject |
| 826 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 827 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 828 | [...] |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 829 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 830 | boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 831 | .getBoolean("enabled", false); |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 832 | ---- |
| 833 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 834 | Project owners can edit the project configuration by fetching the |
| 835 | `refs/meta/config` branch, editing the `project.config` file and |
| 836 | pushing the commit back. |
| 837 | |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 838 | Plugin configuration values that are stored in the `project.config` |
| 839 | file can be exposed in the ProjectInfoScreen to allow project owners |
| 840 | to see and edit them from the UI. |
| 841 | |
| 842 | For this an instance of `ProjectConfigEntry` needs to be bound for each |
| 843 | parameter. The export name must be a valid Git variable name. The |
| 844 | variable name is case-insensitive, allows only alphanumeric characters |
| 845 | and '-', and must start with an alphabetic character. |
| 846 | |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 847 | The example below shows how the parameters `plugin.helloworld.enabled` |
| 848 | and `plugin.helloworld.language` are bound to be editable from the |
David Pursehouse | a1d633b | 2014-05-02 17:21:02 +0900 | [diff] [blame] | 849 | Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting" |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 850 | is provided as display name and the default value is set to `true`. |
| 851 | For the parameter `plugin.helloworld.language` "Preferred Language" |
| 852 | is provided as display name and "en" is set as default value. |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 853 | |
| 854 | [source,java] |
| 855 | ---- |
| 856 | class Module extends AbstractModule { |
| 857 | @Override |
| 858 | protected void configure() { |
| 859 | bind(ProjectConfigEntry.class) |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 860 | .annotatedWith(Exports.named("enabled")) |
| 861 | .toInstance(new ProjectConfigEntry("Enable Greeting", true)); |
| 862 | bind(ProjectConfigEntry.class) |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 863 | .annotatedWith(Exports.named("language")) |
| 864 | .toInstance(new ProjectConfigEntry("Preferred Language", "en")); |
| 865 | } |
| 866 | } |
| 867 | ---- |
| 868 | |
Edwin Kempin | b64d397 | 2013-11-17 18:55:48 +0100 | [diff] [blame] | 869 | By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins |
| 870 | can be notified when this configuration parameter is updated on a |
| 871 | project. |
| 872 | |
Janice Agustin | e5a9d01 | 2015-08-24 09:05:56 -0400 | [diff] [blame] | 873 | [[configuring-groups]] |
| 874 | === Referencing groups in `project.config` |
| 875 | |
| 876 | Plugins can refer to groups so that when they are renamed, the project |
| 877 | config will also be updated in this section. The proper format to use is |
| 878 | the string representation of a GroupReference, as shown below. |
| 879 | |
| 880 | ---- |
| 881 | Group[group_name / group_uuid] |
| 882 | ---- |
| 883 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 884 | [[project-specific-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 885 | == Project Specific Configuration in own config file |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 886 | |
| 887 | Plugins can store their project specific configuration in an own |
| 888 | configuration file in the projects `refs/meta/config` branch. |
| 889 | This makes sense if the plugins project specific configuration is |
| 890 | rather complex and requires the usage of subsections. Plugins that |
| 891 | have a simple key-value pair configuration can store their project |
| 892 | specific configuration in a link:#simple-project-specific-configuration[ |
| 893 | `plugin` subsection of the `project.config` file]. |
| 894 | |
| 895 | The plugin configuration file in the `refs/meta/config` branch must be |
| 896 | named after the plugin. For example a configuration file for a |
| 897 | `default-reviewer` plugin could look like this: |
| 898 | |
| 899 | .default-reviewer.config |
| 900 | ---- |
| 901 | [branch "refs/heads/master"] |
| 902 | reviewer = Project Owners |
| 903 | reviewer = john.doe@example.com |
| 904 | [match "file:^.*\.txt"] |
| 905 | reviewer = My Info Developers |
| 906 | ---- |
| 907 | |
| 908 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 909 | plugin can easily access its project specific configuration: |
| 910 | |
| 911 | [source,java] |
| 912 | ---- |
| 913 | @Inject |
| 914 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 915 | |
| 916 | [...] |
| 917 | |
| 918 | String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer") |
| 919 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 920 | ---- |
| 921 | |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 922 | It is also possible to get missing configuration parameters inherited |
| 923 | from the parent projects: |
| 924 | |
| 925 | [source,java] |
| 926 | ---- |
| 927 | @Inject |
| 928 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 929 | |
| 930 | [...] |
| 931 | |
David Ostrovsky | 468e4c3 | 2014-03-22 06:05:35 -0700 | [diff] [blame] | 932 | String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer") |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 933 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 934 | ---- |
| 935 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 936 | Project owners can edit the project configuration by fetching the |
| 937 | `refs/meta/config` branch, editing the `<plugin-name>.config` file and |
| 938 | pushing the commit back. |
| 939 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 940 | == React on changes in project configuration |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 941 | |
| 942 | If a plugin wants to react on changes in the project configuration, it |
| 943 | can implement a `GitReferenceUpdatedListener` and filter on events for |
| 944 | the `refs/meta/config` branch: |
| 945 | |
| 946 | [source,java] |
| 947 | ---- |
| 948 | public class MyListener implements GitReferenceUpdatedListener { |
| 949 | |
| 950 | private final MetaDataUpdate.Server metaDataUpdateFactory; |
| 951 | |
| 952 | @Inject |
| 953 | MyListener(MetaDataUpdate.Server metaDataUpdateFactory) { |
| 954 | this.metaDataUpdateFactory = metaDataUpdateFactory; |
| 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public void onGitReferenceUpdated(Event event) { |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 959 | if (event.getRefName().equals(RefNames.REFS_CONFIG)) { |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 960 | Project.NameKey p = new Project.NameKey(event.getProjectName()); |
| 961 | try { |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 962 | ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId()); |
| 963 | ProjectConfig newCfg = parseConfig(p, event.getNewObjectId()); |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 964 | |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 965 | if (oldCfg != null && newCfg != null |
| 966 | && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) { |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 967 | // submit type has changed |
| 968 | ... |
| 969 | } |
| 970 | } catch (IOException | ConfigInvalidException e) { |
| 971 | ... |
| 972 | } |
| 973 | } |
| 974 | } |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 975 | |
| 976 | private ProjectConfig parseConfig(Project.NameKey p, String idStr) |
| 977 | throws IOException, ConfigInvalidException, RepositoryNotFoundException { |
| 978 | ObjectId id = ObjectId.fromString(idStr); |
| 979 | if (ObjectId.zeroId().equals(id)) { |
| 980 | return null; |
| 981 | } |
| 982 | return ProjectConfig.read(metaDataUpdateFactory.create(p), id); |
| 983 | } |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 984 | } |
| 985 | ---- |
| 986 | |
| 987 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 988 | [[capabilities]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 989 | == Plugin Owned Capabilities |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 990 | |
| 991 | Plugins may provide their own capabilities and restrict usage of SSH |
Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 992 | commands or `UiAction` to the users who are granted those capabilities. |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 993 | |
| 994 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 995 | abstract class: |
| 996 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 997 | [source,java] |
| 998 | ---- |
| 999 | public class PrintHelloCapability extends CapabilityDefinition { |
| 1000 | @Override |
| 1001 | public String getDescription() { |
| 1002 | return "Print Hello"; |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1003 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1004 | } |
| 1005 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1006 | |
Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 1007 | If no Guice modules are declared in the manifest, capability may |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1008 | use auto-registration by providing an `@Export` annotation: |
| 1009 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1010 | [source,java] |
| 1011 | ---- |
| 1012 | @Export("printHello") |
| 1013 | public class PrintHelloCapability extends CapabilityDefinition { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1014 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1015 | } |
| 1016 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1017 | |
| 1018 | Otherwise the capability must be bound in a plugin module: |
| 1019 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1020 | [source,java] |
| 1021 | ---- |
| 1022 | public class HelloWorldModule extends AbstractModule { |
| 1023 | @Override |
| 1024 | protected void configure() { |
| 1025 | bind(CapabilityDefinition.class) |
| 1026 | .annotatedWith(Exports.named("printHello")) |
| 1027 | .to(PrintHelloCapability.class); |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1028 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1029 | } |
| 1030 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1031 | |
| 1032 | 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] | 1033 | 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] | 1034 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 1035 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1036 | [source,java] |
| 1037 | ---- |
| 1038 | @RequiresCapability("printHello") |
| 1039 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 1040 | public final class PrintHelloWorldCommand extends SshCommand { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1041 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1042 | } |
| 1043 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1044 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1045 | Or with `UiAction`: |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1046 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1047 | [source,java] |
| 1048 | ---- |
| 1049 | @RequiresCapability("printHello") |
| 1050 | public class SayHelloAction extends UiAction<RevisionResource> |
| 1051 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1052 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1053 | } |
| 1054 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1055 | |
| 1056 | Capability scope was introduced to differentiate between plugin-owned |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 1057 | capabilities and core capabilities. Per default the scope of the |
| 1058 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 1059 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1060 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 1061 | capability is assumed to be that plugin. |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 1062 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1063 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 1064 | (and thus is outside of a plugin) the scope is the core server and will use |
| 1065 | the `GlobalCapability` known to Gerrit Code Review server. |
| 1066 | |
| 1067 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 1068 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 1069 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1070 | [source,java] |
| 1071 | ---- |
| 1072 | @RequiresCapability(value = "administrateServer", scope = |
| 1073 | CapabilityScope.CORE) |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1074 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1075 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1076 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1077 | [[ui_extension]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1078 | == UI Extension |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1079 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1080 | [[panels]] |
| 1081 | === Panels |
| 1082 | |
| 1083 | GWT plugins can contribute panels to Gerrit screens. |
| 1084 | |
| 1085 | Gerrit screens define extension points where plugins can add GWT |
| 1086 | panels with custom controls: |
| 1087 | |
| 1088 | * Change Screen: |
Edwin Kempin | 2a8c515 | 2015-07-08 14:28:57 +0200 | [diff] [blame] | 1089 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER`: |
| 1090 | + |
| 1091 | Panel will be shown in the header bar to the right of the change |
| 1092 | status. |
| 1093 | |
Edwin Kempin | 745021e | 2015-07-09 13:09:44 +0200 | [diff] [blame] | 1094 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_BUTTONS`: |
| 1095 | + |
| 1096 | Panel will be shown in the header bar on the right side of the buttons. |
| 1097 | |
Edwin Kempin | cbc9525 | 2015-07-09 11:37:53 +0200 | [diff] [blame] | 1098 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_POP_DOWNS`: |
| 1099 | + |
| 1100 | Panel will be shown in the header bar on the right side of the pop down |
| 1101 | buttons. |
| 1102 | |
Khai Do | 675afc0 | 2016-07-28 16:30:37 -0700 | [diff] [blame] | 1103 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK`: |
| 1104 | + |
| 1105 | Panel will be shown below the commit info block. |
| 1106 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1107 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK`: |
| 1108 | + |
| 1109 | Panel will be shown below the change info block. |
| 1110 | |
Khai Do | 76c830c | 2016-07-28 16:35:45 -0700 | [diff] [blame] | 1111 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_RELATED_INFO_BLOCK`: |
| 1112 | + |
| 1113 | Panel will be shown below the related info block. |
| 1114 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1115 | ** The following parameters are provided: |
Edwin Kempin | 5d683cc | 2015-07-10 15:47:14 +0200 | [diff] [blame] | 1116 | *** `GerritUiExtensionPoint.Key.CHANGE_INFO`: |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1117 | + |
Edwin Kempin | 5d683cc | 2015-07-10 15:47:14 +0200 | [diff] [blame] | 1118 | The link:rest-api-changes.html#change-info[ChangeInfo] entity for the |
| 1119 | current change. |
David Ostrovsky | 916ae0c | 2016-03-15 17:05:41 +0100 | [diff] [blame] | 1120 | + |
| 1121 | The link:rest-api-changes.html#revision-info[RevisionInfo] entity for |
| 1122 | the current patch set. |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1123 | |
Edwin Kempin | 88b947a | 2015-07-08 09:03:56 +0200 | [diff] [blame] | 1124 | * Project Info Screen: |
| 1125 | ** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_TOP`: |
| 1126 | + |
| 1127 | Panel will be shown at the top of the screen. |
| 1128 | |
| 1129 | ** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_BOTTOM`: |
| 1130 | + |
| 1131 | Panel will be shown at the bottom of the screen. |
| 1132 | |
| 1133 | ** The following parameters are provided: |
| 1134 | *** `GerritUiExtensionPoint.Key.PROJECT_NAME`: |
| 1135 | + |
| 1136 | The name of the project. |
| 1137 | |
Edwin Kempin | 241d9db | 2015-07-08 13:53:50 +0200 | [diff] [blame] | 1138 | * User Password Screen: |
| 1139 | ** `GerritUiExtensionPoint.PASSWORD_SCREEN_BOTTOM`: |
| 1140 | + |
| 1141 | Panel will be shown at the bottom of the screen. |
| 1142 | |
Edwin Kempin | 30c6f47 | 2015-07-09 14:27:52 +0200 | [diff] [blame] | 1143 | ** The following parameters are provided: |
| 1144 | *** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`: |
| 1145 | + |
| 1146 | The link:rest-api-accounts.html#account-info[AccountInfo] entity for |
| 1147 | the current user. |
| 1148 | |
Edwin Kempin | 1cd95f9 | 2015-07-14 08:27:20 +0200 | [diff] [blame] | 1149 | * User Preferences Screen: |
| 1150 | ** `GerritUiExtensionPoint.PREFERENCES_SCREEN_BOTTOM`: |
| 1151 | + |
| 1152 | Panel will be shown at the bottom of the screen. |
| 1153 | |
| 1154 | ** The following parameters are provided: |
| 1155 | *** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`: |
| 1156 | + |
| 1157 | The link:rest-api-accounts.html#account-info[AccountInfo] entity for |
| 1158 | the current user. |
| 1159 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1160 | * User Profile Screen: |
| 1161 | ** `GerritUiExtensionPoint.PROFILE_SCREEN_BOTTOM`: |
| 1162 | + |
| 1163 | Panel will be shown at the bottom of the screen below the grid with the |
| 1164 | profile data. |
| 1165 | |
Edwin Kempin | 30c6f47 | 2015-07-09 14:27:52 +0200 | [diff] [blame] | 1166 | ** The following parameters are provided: |
| 1167 | *** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`: |
| 1168 | + |
| 1169 | The link:rest-api-accounts.html#account-info[AccountInfo] entity for |
| 1170 | the current user. |
| 1171 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1172 | Example panel: |
| 1173 | [source,java] |
| 1174 | ---- |
| 1175 | public class MyPlugin extends PluginEntryPoint { |
| 1176 | @Override |
| 1177 | public void onPluginLoad() { |
| 1178 | Plugin.get().panel(GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK, |
| 1179 | new Panel.EntryPoint() { |
| 1180 | @Override |
| 1181 | public void onLoad(Panel panel) { |
| 1182 | panel.setWidget(new InlineLabel("My Panel for change " |
| 1183 | + panel.getInt(GerritUiExtensionPoint.Key.CHANGE_ID, -1)); |
| 1184 | } |
| 1185 | }); |
| 1186 | } |
| 1187 | } |
| 1188 | ---- |
| 1189 | |
| 1190 | [[actions]] |
| 1191 | === Actions |
| 1192 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1193 | Plugins can contribute UI actions on core Gerrit pages. This is useful |
| 1194 | for workflow customization or exposing plugin functionality through the |
| 1195 | UI in addition to SSH commands and the REST API. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1196 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1197 | For instance a plugin to integrate Jira with Gerrit changes may |
| 1198 | contribute a "File bug" button to allow filing a bug from the change |
| 1199 | page or plugins to integrate continuous integration systems may |
| 1200 | contribute a "Schedule" button to allow a CI build to be scheduled |
| 1201 | manually from the patch set panel. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1202 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1203 | Two different places on core Gerrit pages are supported: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1204 | |
| 1205 | * Change screen |
| 1206 | * Project info screen |
| 1207 | |
| 1208 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 1209 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1210 | [source,java] |
| 1211 | ---- |
| 1212 | @RequiresCapability("printHello") |
| 1213 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 1214 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 1215 | static class Input { |
| 1216 | boolean french; |
| 1217 | String message; |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1218 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1219 | |
| 1220 | private Provider<CurrentUser> user; |
| 1221 | |
| 1222 | @Inject |
| 1223 | HelloWorldAction(Provider<CurrentUser> user) { |
| 1224 | this.user = user; |
| 1225 | } |
| 1226 | |
| 1227 | @Override |
| 1228 | public String apply(RevisionResource rev, Input input) { |
| 1229 | final String greeting = input.french |
| 1230 | ? "Bonjour" |
| 1231 | : "Hello"; |
| 1232 | return String.format("%s %s from change %s, patch set %d!", |
| 1233 | greeting, |
| 1234 | Strings.isNullOrEmpty(input.message) |
| 1235 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 1236 | : input.message, |
| 1237 | rev.getChange().getId().toString(), |
| 1238 | rev.getPatchSet().getPatchSetId()); |
| 1239 | } |
| 1240 | |
| 1241 | @Override |
| 1242 | public Description getDescription( |
| 1243 | RevisionResource resource) { |
| 1244 | return new Description() |
| 1245 | .setLabel("Say hello") |
| 1246 | .setTitle("Say hello in different languages"); |
| 1247 | } |
| 1248 | } |
| 1249 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1250 | |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1251 | Sometimes plugins may want to be able to change the state of a patch set or |
| 1252 | change in the `UiAction.apply()` method and reflect these changes on the core |
| 1253 | UI. For example a buildbot plugin which exposes a 'Schedule' button on the |
| 1254 | patch set panel may want to disable that button after the build was scheduled |
| 1255 | and update the tooltip of that button. But because of Gerrit's caching |
| 1256 | strategy the following must be taken into consideration. |
| 1257 | |
| 1258 | The browser is allowed to cache the `UiAction` information until something on |
| 1259 | the change is modified. More accurately the change row needs to be modified in |
| 1260 | the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or |
| 1261 | the +refs/meta/config+ of the project or any parents needs to change to a new |
| 1262 | SHA-1. The ETag SHA-1 computation code can be found in the |
| 1263 | `ChangeResource.getETag()` method. |
| 1264 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1265 | 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] | 1266 | |
| 1267 | [source,java] |
| 1268 | ---- |
| 1269 | @Override |
| 1270 | public Object apply(RevisionResource rcrs, Input in) { |
| 1271 | // schedule a build |
| 1272 | [...] |
| 1273 | // update change |
| 1274 | ReviewDb db = dbProvider.get(); |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1275 | try (BatchUpdate bu = batchUpdateFactory.create( |
| 1276 | db, project.getNameKey(), user, TimeUtil.nowTs())) { |
| 1277 | bu.addOp(change.getId(), new BatchUpdate.Op() { |
| 1278 | @Override |
| 1279 | public boolean updateChange(BatchUpdate.ChangeContext ctx) { |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1280 | return true; |
| 1281 | } |
| 1282 | }); |
| 1283 | bu.execute(); |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1284 | } |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1285 | [...] |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1286 | } |
| 1287 | ---- |
| 1288 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1289 | `UiAction` must be bound in a plugin module: |
| 1290 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1291 | [source,java] |
| 1292 | ---- |
| 1293 | public class Module extends AbstractModule { |
| 1294 | @Override |
| 1295 | protected void configure() { |
| 1296 | install(new RestApiModule() { |
| 1297 | @Override |
| 1298 | protected void configure() { |
| 1299 | post(REVISION_KIND, "say-hello") |
| 1300 | .to(HelloWorldAction.class); |
| 1301 | } |
| 1302 | }); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1303 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1304 | } |
| 1305 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1306 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1307 | The module above must be declared in the `pom.xml` for Maven driven |
| 1308 | plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1309 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1310 | [source,xml] |
| 1311 | ---- |
| 1312 | <manifestEntries> |
| 1313 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 1314 | </manifestEntries> |
| 1315 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1316 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1317 | or in the `BUCK` configuration file for Buck driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1318 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1319 | [source,python] |
| 1320 | ---- |
| 1321 | manifest_entries = [ |
| 1322 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 1323 | ] |
| 1324 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1325 | |
| 1326 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 1327 | combined with the JavaScript API. This would display a small popup near the |
| 1328 | activation button to gather additional input from the user. The JS file is |
| 1329 | typically put in the `static` folder within the plugin's directory: |
| 1330 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1331 | [source,javascript] |
| 1332 | ---- |
| 1333 | Gerrit.install(function(self) { |
| 1334 | function onSayHello(c) { |
| 1335 | var f = c.textfield(); |
| 1336 | var t = c.checkbox(); |
| 1337 | var b = c.button('Say hello', {onclick: function(){ |
| 1338 | c.call( |
| 1339 | {message: f.value, french: t.checked}, |
| 1340 | function(r) { |
| 1341 | c.hide(); |
| 1342 | window.alert(r); |
| 1343 | c.refresh(); |
| 1344 | }); |
| 1345 | }}); |
| 1346 | c.popup(c.div( |
| 1347 | c.prependLabel('Greeting message', f), |
| 1348 | c.br(), |
| 1349 | c.label(t, 'french'), |
| 1350 | c.br(), |
| 1351 | b)); |
| 1352 | f.focus(); |
| 1353 | } |
| 1354 | self.onAction('revision', 'say-hello', onSayHello); |
| 1355 | }); |
| 1356 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1357 | |
| 1358 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 1359 | an HTTP Module: |
| 1360 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1361 | [source,java] |
| 1362 | ---- |
| 1363 | public class HttpModule extends HttpPluginModule { |
| 1364 | @Override |
| 1365 | protected void configureServlets() { |
| 1366 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1367 | .toInstance(new JavaScriptPlugin("hello.js")); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1368 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1369 | } |
| 1370 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1371 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1372 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1373 | driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1374 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1375 | [source,xml] |
| 1376 | ---- |
| 1377 | <manifestEntries> |
| 1378 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 1379 | </manifestEntries> |
| 1380 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1381 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1382 | or in the `BUCK` configuration file for Buck driven plugins |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1383 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1384 | [source,python] |
| 1385 | ---- |
| 1386 | manifest_entries = [ |
| 1387 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 1388 | ] |
| 1389 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1390 | |
| 1391 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 1392 | capability check is done during the `UiAction` gathering, so the plugin author |
| 1393 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 1394 | case. |
| 1395 | |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 1396 | The following prerequisites must be met, to satisfy the capability check: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1397 | |
| 1398 | * user is authenticated |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1399 | * 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] | 1400 | * user is a member of a group which has the required capability |
| 1401 | |
| 1402 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 1403 | combined with JavaScript API (its own JavaScript function is provided), |
| 1404 | then a popup dialog is normally opened to gather additional user input. |
| 1405 | A new button is placed on the popup dialog to actually send the request. |
| 1406 | |
| 1407 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 1408 | can be accessed from any REST client, i. e.: |
| 1409 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 1410 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1411 | curl -X POST -H "Content-Type: application/json" \ |
| 1412 | -d '{message: "François", french: true}' \ |
| 1413 | --digest --user joe:secret \ |
| 1414 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
| 1415 | "Bonjour François from change 1, patch set 1!" |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 1416 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1417 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1418 | 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] | 1419 | particularly useful for `DELETE` requests: |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1420 | |
| 1421 | [source,java] |
| 1422 | ---- |
| 1423 | public class Module extends AbstractModule { |
| 1424 | @Override |
| 1425 | protected void configure() { |
| 1426 | install(new RestApiModule() { |
| 1427 | @Override |
| 1428 | protected void configure() { |
| 1429 | delete(PROJECT_KIND) |
| 1430 | .to(DeleteProject.class); |
| 1431 | } |
| 1432 | }); |
| 1433 | } |
| 1434 | } |
| 1435 | ---- |
| 1436 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1437 | For a `UiAction` bound this way, a JS API function can be provided. |
| 1438 | |
| 1439 | Currently only one restriction exists: per plugin only one `UiAction` |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1440 | can be bound per resource without view name. To define a JS function |
| 1441 | for the `UiAction`, "/" must be used as the name: |
| 1442 | |
| 1443 | [source,javascript] |
| 1444 | ---- |
| 1445 | Gerrit.install(function(self) { |
| 1446 | function onDeleteProject(c) { |
| 1447 | [...] |
| 1448 | } |
| 1449 | self.onAction('project', '/', onDeleteProject); |
| 1450 | }); |
| 1451 | ---- |
| 1452 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1453 | [[top-menu-extensions]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1454 | == Top Menu Extensions |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1455 | |
| 1456 | Plugins can contribute items to Gerrit's top menu. |
| 1457 | |
| 1458 | A single top menu extension can have multiple elements and will be put as |
| 1459 | the last element in Gerrit's top menu. |
| 1460 | |
| 1461 | Plugins define the top menu entries by implementing `TopMenu` interface: |
| 1462 | |
| 1463 | [source,java] |
| 1464 | ---- |
| 1465 | public class MyTopMenuExtension implements TopMenu { |
| 1466 | |
| 1467 | @Override |
| 1468 | public List<MenuEntry> getEntries() { |
| 1469 | return Lists.newArrayList( |
| 1470 | new MenuEntry("Top Menu Entry", Lists.newArrayList( |
| 1471 | new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1472 | } |
| 1473 | } |
| 1474 | ---- |
| 1475 | |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1476 | Plugins can also add additional menu items to Gerrit's top menu entries |
| 1477 | by defining a `MenuEntry` that has the same name as a Gerrit top menu |
| 1478 | entry: |
| 1479 | |
| 1480 | [source,java] |
| 1481 | ---- |
| 1482 | public class MyTopMenuExtension implements TopMenu { |
| 1483 | |
| 1484 | @Override |
| 1485 | public List<MenuEntry> getEntries() { |
| 1486 | return Lists.newArrayList( |
Dariusz Luksza | 2d3afab | 2013-10-01 11:07:13 +0200 | [diff] [blame] | 1487 | new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList( |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1488 | new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/")))); |
| 1489 | } |
| 1490 | } |
| 1491 | ---- |
| 1492 | |
Dariusz Luksza | e8de74f | 2014-06-04 19:39:20 +0200 | [diff] [blame] | 1493 | `MenuItems` that are bound for the `MenuEntry` with the name |
| 1494 | `GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder |
| 1495 | which is automatically replaced by the actual project name. |
| 1496 | |
| 1497 | E.g. plugins may register an link:#http[HTTP Servlet] to handle project |
| 1498 | specific requests and add an menu item for this: |
| 1499 | |
| 1500 | [source,java] |
| 1501 | --- |
| 1502 | new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}"); |
| 1503 | --- |
| 1504 | |
| 1505 | This also enables plugins to provide menu items for project aware |
| 1506 | screens: |
| 1507 | |
| 1508 | [source,java] |
| 1509 | --- |
| 1510 | new MenuItem("My Screen", "/x/my-screen/for/${projectName}"); |
| 1511 | --- |
| 1512 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1513 | If no Guice modules are declared in the manifest, the top menu extension may use |
| 1514 | auto-registration by providing an `@Listen` annotation: |
| 1515 | |
| 1516 | [source,java] |
| 1517 | ---- |
| 1518 | @Listen |
| 1519 | public class MyTopMenuExtension implements TopMenu { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1520 | [...] |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1521 | } |
| 1522 | ---- |
| 1523 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1524 | Otherwise the top menu extension must be bound in the plugin module used |
| 1525 | for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF): |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1526 | |
| 1527 | [source,java] |
| 1528 | ---- |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1529 | package com.googlesource.gerrit.plugins.helloworld; |
| 1530 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1531 | public class HelloWorldModule extends AbstractModule { |
| 1532 | @Override |
| 1533 | protected void configure() { |
| 1534 | DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class); |
| 1535 | } |
| 1536 | } |
| 1537 | ---- |
| 1538 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1539 | [source,manifest] |
| 1540 | ---- |
| 1541 | Gerrit-ApiType: plugin |
| 1542 | Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule |
| 1543 | ---- |
| 1544 | |
Edwin Kempin | b2e926a | 2013-11-11 16:38:30 +0100 | [diff] [blame] | 1545 | It is also possible to show some menu entries only if the user has a |
| 1546 | certain capability: |
| 1547 | |
| 1548 | [source,java] |
| 1549 | ---- |
| 1550 | public class MyTopMenuExtension implements TopMenu { |
| 1551 | private final String pluginName; |
| 1552 | private final Provider<CurrentUser> userProvider; |
| 1553 | private final List<MenuEntry> menuEntries; |
| 1554 | |
| 1555 | @Inject |
| 1556 | public MyTopMenuExtension(@PluginName String pluginName, |
| 1557 | Provider<CurrentUser> userProvider) { |
| 1558 | this.pluginName = pluginName; |
| 1559 | this.userProvider = userProvider; |
| 1560 | menuEntries = new ArrayList<TopMenu.MenuEntry>(); |
| 1561 | |
| 1562 | // add menu entry that is only visible to users with a certain capability |
| 1563 | if (canSeeMenuEntry()) { |
| 1564 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1565 | .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1566 | } |
| 1567 | |
| 1568 | // add menu entry that is visible to all users (even anonymous users) |
| 1569 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1570 | .singletonList(new MenuItem("Documentation", "/plugins/myplugin/")))); |
| 1571 | } |
| 1572 | |
| 1573 | private boolean canSeeMenuEntry() { |
| 1574 | if (userProvider.get().isIdentifiedUser()) { |
| 1575 | CapabilityControl ctl = userProvider.get().getCapabilities(); |
| 1576 | return ctl.canPerform(pluginName + "-" + MyCapability.ID) |
| 1577 | || ctl.canAdministrateServer(); |
| 1578 | } else { |
| 1579 | return false; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | @Override |
| 1584 | public List<MenuEntry> getEntries() { |
| 1585 | return menuEntries; |
| 1586 | } |
| 1587 | } |
| 1588 | ---- |
| 1589 | |
Edwin Kempin | 3c024ea | 2013-11-11 10:43:46 +0100 | [diff] [blame] | 1590 | [[gwt_ui_extension]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1591 | == GWT UI Extension |
Edwin Kempin | 3c024ea | 2013-11-11 10:43:46 +0100 | [diff] [blame] | 1592 | Plugins can extend the Gerrit UI with own GWT code. |
| 1593 | |
| 1594 | The Maven archetype 'gerrit-plugin-gwt-archetype' can be used to |
| 1595 | generate a GWT plugin skeleton. How to use the Maven plugin archetypes |
| 1596 | is described in the link:#getting-started[Getting started] section. |
| 1597 | |
| 1598 | The generated GWT plugin has a link:#top-menu-extensions[top menu] that |
| 1599 | opens a GWT dialog box when the user clicks on it. |
| 1600 | |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1601 | In addition to the Gerrit-Plugin API a GWT plugin depends on |
| 1602 | `gerrit-plugin-gwtui`. This dependency must be specified in the |
| 1603 | `pom.xml`: |
| 1604 | |
| 1605 | [source,xml] |
| 1606 | ---- |
| 1607 | <dependency> |
| 1608 | <groupId>com.google.gerrit</groupId> |
| 1609 | <artifactId>gerrit-plugin-gwtui</artifactId> |
| 1610 | <version>${Gerrit-ApiVersion}</version> |
| 1611 | </dependency> |
| 1612 | ---- |
| 1613 | |
| 1614 | A GWT plugin must contain a GWT module file, e.g. `HelloPlugin.gwt.xml`, |
| 1615 | that bundles together all the configuration settings of the GWT plugin: |
| 1616 | |
| 1617 | [source,xml] |
| 1618 | ---- |
| 1619 | <?xml version="1.0" encoding="UTF-8"?> |
| 1620 | <module rename-to="hello_gwt_plugin"> |
| 1621 | <!-- Inherit the core Web Toolkit stuff. --> |
| 1622 | <inherits name="com.google.gwt.user.User"/> |
| 1623 | <!-- Other module inherits --> |
| 1624 | <inherits name="com.google.gerrit.Plugin"/> |
| 1625 | <inherits name="com.google.gwt.http.HTTP"/> |
| 1626 | <!-- Using GWT built-in themes adds a number of static --> |
| 1627 | <!-- resources to the plugin. No theme inherits lines were --> |
| 1628 | <!-- added in order to make this plugin as simple as possible --> |
| 1629 | <!-- Specify the app entry point class. --> |
| 1630 | <entry-point class="${package}.client.HelloPlugin"/> |
| 1631 | <stylesheet src="hello.css"/> |
| 1632 | </module> |
| 1633 | ---- |
| 1634 | |
| 1635 | The GWT module must inherit `com.google.gerrit.Plugin` and |
| 1636 | `com.google.gwt.http.HTTP`. |
| 1637 | |
| 1638 | To register the GWT module a `GwtPlugin` needs to be bound. |
| 1639 | |
| 1640 | If no Guice modules are declared in the manifest, the GWT plugin may |
| 1641 | use auto-registration by using the `@Listen` annotation: |
| 1642 | |
| 1643 | [source,java] |
| 1644 | ---- |
| 1645 | @Listen |
| 1646 | public class MyExtension extends GwtPlugin { |
| 1647 | public MyExtension() { |
| 1648 | super("hello_gwt_plugin"); |
| 1649 | } |
| 1650 | } |
| 1651 | ---- |
| 1652 | |
| 1653 | Otherwise the binding must be done in an `HttpModule`: |
| 1654 | |
| 1655 | [source,java] |
| 1656 | ---- |
| 1657 | public class HttpModule extends HttpPluginModule { |
| 1658 | |
| 1659 | @Override |
| 1660 | protected void configureServlets() { |
| 1661 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1662 | .toInstance(new GwtPlugin("hello_gwt_plugin")); |
| 1663 | } |
| 1664 | } |
| 1665 | ---- |
| 1666 | |
| 1667 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1668 | driven plugins: |
| 1669 | |
| 1670 | [source,xml] |
| 1671 | ---- |
| 1672 | <manifestEntries> |
| 1673 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.myplugin.HttpModule</Gerrit-HttpModule> |
| 1674 | </manifestEntries> |
| 1675 | ---- |
| 1676 | |
Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1677 | The name that is provided to the `GwtPlugin` must match the GWT |
| 1678 | module name compiled into the plugin. The name of the GWT module |
| 1679 | can be explicitly set in the GWT module XML file by specifying |
| 1680 | the `rename-to` attribute on the module. It is important that the |
| 1681 | module name be unique across all plugins installed on the server, |
| 1682 | as the module name determines the JavaScript namespace used by the |
| 1683 | compiled plugin code. |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1684 | |
| 1685 | [source,xml] |
| 1686 | ---- |
| 1687 | <module rename-to="hello_gwt_plugin"> |
| 1688 | ---- |
| 1689 | |
| 1690 | The actual GWT code must be implemented in a class that extends |
Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1691 | `com.google.gerrit.plugin.client.PluginEntryPoint`: |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1692 | |
| 1693 | [source,java] |
| 1694 | ---- |
Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1695 | public class HelloPlugin extends PluginEntryPoint { |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1696 | |
| 1697 | @Override |
Shawn Pearce | c8e96ad | 2013-12-09 08:20:44 -0800 | [diff] [blame] | 1698 | public void onPluginLoad() { |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1699 | // Create the dialog box |
| 1700 | final DialogBox dialogBox = new DialogBox(); |
| 1701 | |
| 1702 | // The content of the dialog comes from a User specified Preference |
| 1703 | dialogBox.setText("Hello from GWT Gerrit UI plugin"); |
| 1704 | dialogBox.setAnimationEnabled(true); |
| 1705 | Button closeButton = new Button("Close"); |
| 1706 | VerticalPanel dialogVPanel = new VerticalPanel(); |
| 1707 | dialogVPanel.setWidth("100%"); |
| 1708 | dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); |
| 1709 | dialogVPanel.add(closeButton); |
| 1710 | |
| 1711 | closeButton.addClickHandler(new ClickHandler() { |
| 1712 | public void onClick(ClickEvent event) { |
| 1713 | dialogBox.hide(); |
| 1714 | } |
| 1715 | }); |
| 1716 | |
| 1717 | // Set the contents of the Widget |
| 1718 | dialogBox.setWidget(dialogVPanel); |
| 1719 | |
| 1720 | RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID); |
| 1721 | rootPanel.getElement().removeAttribute("href"); |
| 1722 | rootPanel.addDomHandler(new ClickHandler() { |
| 1723 | @Override |
| 1724 | public void onClick(ClickEvent event) { |
| 1725 | dialogBox.center(); |
| 1726 | dialogBox.show(); |
| 1727 | } |
| 1728 | }, ClickEvent.getType()); |
| 1729 | } |
| 1730 | } |
| 1731 | ---- |
| 1732 | |
| 1733 | This class must be set as entry point in the GWT module: |
| 1734 | |
| 1735 | [source,xml] |
| 1736 | ---- |
| 1737 | <entry-point class="${package}.client.HelloPlugin"/> |
| 1738 | ---- |
| 1739 | |
| 1740 | In addition this class must be defined as module in the `pom.xml` for the |
| 1741 | `gwt-maven-plugin` and the `webappDirectory` option of `gwt-maven-plugin` |
| 1742 | must be set to `${project.build.directory}/classes/static`: |
| 1743 | |
| 1744 | [source,xml] |
| 1745 | ---- |
| 1746 | <plugin> |
| 1747 | <groupId>org.codehaus.mojo</groupId> |
| 1748 | <artifactId>gwt-maven-plugin</artifactId> |
David Pursehouse | 7ab8173 | 2015-05-07 12:00:47 +0900 | [diff] [blame] | 1749 | <version>2.7.0</version> |
Edwin Kempin | b74daa9 | 2013-11-11 11:28:16 +0100 | [diff] [blame] | 1750 | <configuration> |
| 1751 | <module>com.googlesource.gerrit.plugins.myplugin.HelloPlugin</module> |
| 1752 | <disableClassMetadata>true</disableClassMetadata> |
| 1753 | <disableCastChecking>true</disableCastChecking> |
| 1754 | <webappDirectory>${project.build.directory}/classes/static</webappDirectory> |
| 1755 | </configuration> |
| 1756 | <executions> |
| 1757 | <execution> |
| 1758 | <goals> |
| 1759 | <goal>compile</goal> |
| 1760 | </goals> |
| 1761 | </execution> |
| 1762 | </executions> |
| 1763 | </plugin> |
| 1764 | ---- |
| 1765 | |
| 1766 | To attach a GWT widget defined by the plugin to the Gerrit core UI |
| 1767 | `com.google.gwt.user.client.ui.RootPanel` can be used to manipulate the |
| 1768 | Gerrit core widgets: |
| 1769 | |
| 1770 | [source,java] |
| 1771 | ---- |
| 1772 | RootPanel rootPanel = RootPanel.get(HelloMenu.MENU_ID); |
| 1773 | rootPanel.getElement().removeAttribute("href"); |
| 1774 | rootPanel.addDomHandler(new ClickHandler() { |
| 1775 | @Override |
| 1776 | public void onClick(ClickEvent event) { |
| 1777 | dialogBox.center(); |
| 1778 | dialogBox.show(); |
| 1779 | } |
| 1780 | }, ClickEvent.getType()); |
| 1781 | ---- |
| 1782 | |
| 1783 | GWT plugins can come with their own css file. This css file must have a |
| 1784 | unique name and must be registered in the GWT module: |
| 1785 | |
| 1786 | [source,xml] |
| 1787 | ---- |
| 1788 | <stylesheet src="hello.css"/> |
| 1789 | ---- |
| 1790 | |
Edwin Kempin | 2570b10 | 2013-11-11 11:44:50 +0100 | [diff] [blame] | 1791 | 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] | 1792 | `com.google.gerrit.plugin.client.rpc.RestApi` to construct the URL |
Edwin Kempin | 2570b10 | 2013-11-11 11:44:50 +0100 | [diff] [blame] | 1793 | path and to trigger the REST calls. |
| 1794 | |
| 1795 | Example for invoking a Gerrit core REST endpoint: |
| 1796 | |
| 1797 | [source,java] |
| 1798 | ---- |
| 1799 | new RestApi("projects").id(projectName).view("description") |
| 1800 | .put("new description", new AsyncCallback<JavaScriptObject>() { |
| 1801 | |
| 1802 | @Override |
| 1803 | public void onSuccess(JavaScriptObject result) { |
| 1804 | // TODO |
| 1805 | } |
| 1806 | |
| 1807 | @Override |
| 1808 | public void onFailure(Throwable caught) { |
| 1809 | // never invoked |
| 1810 | } |
| 1811 | }); |
| 1812 | ---- |
| 1813 | |
| 1814 | Example for invoking a REST endpoint defined by a plugin: |
| 1815 | |
| 1816 | [source,java] |
| 1817 | ---- |
| 1818 | new RestApi("projects").id(projectName).view("myplugin", "myview") |
| 1819 | .get(new AsyncCallback<JavaScriptObject>() { |
| 1820 | |
| 1821 | @Override |
| 1822 | public void onSuccess(JavaScriptObject result) { |
| 1823 | // TODO |
| 1824 | } |
| 1825 | |
| 1826 | @Override |
| 1827 | public void onFailure(Throwable caught) { |
| 1828 | // never invoked |
| 1829 | } |
| 1830 | }); |
| 1831 | ---- |
| 1832 | |
| 1833 | The `onFailure(Throwable)` of the provided callback is never invoked. |
| 1834 | If an error occurs, it is shown in an error dialog. |
| 1835 | |
| 1836 | In order to be able to do REST calls the GWT module must inherit |
| 1837 | `com.google.gwt.json.JSON`: |
| 1838 | |
| 1839 | [source,xml] |
| 1840 | ---- |
| 1841 | <inherits name="com.google.gwt.json.JSON"/> |
| 1842 | ---- |
| 1843 | |
Edwin Kempin | 1519979 | 2014-04-23 16:22:05 +0200 | [diff] [blame] | 1844 | [[screen]] |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 1845 | == Add Screen |
Edwin Kempin | 1519979 | 2014-04-23 16:22:05 +0200 | [diff] [blame] | 1846 | A link:#gwt_ui_extension[GWT plugin] can link:#top-menu-extensions[add |
| 1847 | a menu item] that opens a screen that is implemented by the plugin. |
| 1848 | This way plugin screens can be fully integrated into the Gerrit UI. |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 1849 | |
| 1850 | Example menu item: |
| 1851 | [source,java] |
| 1852 | ---- |
| 1853 | public class MyMenu implements TopMenu { |
| 1854 | private final List<MenuEntry> menuEntries; |
| 1855 | |
| 1856 | @Inject |
| 1857 | public MyMenu(@PluginName String name) { |
David Pursehouse | ccdeae8 | 2016-05-03 23:16:15 +0900 | [diff] [blame] | 1858 | menuEntries = new ArrayList<>(); |
Shawn Pearce | d5c844f | 2013-12-26 15:32:26 -0800 | [diff] [blame] | 1859 | menuEntries.add(new MenuEntry("My Menu", Collections.singletonList( |
| 1860 | new MenuItem("My Screen", "#/x/" + name + "/my-screen", "")))); |
| 1861 | } |
| 1862 | |
| 1863 | @Override |
| 1864 | public List<MenuEntry> getEntries() { |
| 1865 | return menuEntries; |
| 1866 | } |
| 1867 | } |
| 1868 | ---- |
| 1869 | |
| 1870 | Example screen: |
| 1871 | [source,java] |
| 1872 | ---- |
| 1873 | public class MyPlugin extends PluginEntryPoint { |
| 1874 | @Override |
| 1875 | public void onPluginLoad() { |
| 1876 | Plugin.get().screen("my-screen", new Screen.EntryPoint() { |
| 1877 | @Override |
| 1878 | public void onLoad(Screen screen) { |
| 1879 | screen.add(new InlineLabel("My Screen"); |
| 1880 | screen.show(); |
| 1881 | } |
| 1882 | }); |
| 1883 | } |
| 1884 | } |
| 1885 | ---- |
| 1886 | |
Edwin Kempin | 70e1112 | 2015-07-08 13:28:40 +0200 | [diff] [blame] | 1887 | [[user-settings-screen]] |
| 1888 | == Add User Settings Screen |
| 1889 | |
| 1890 | A link:#gwt_ui_extension[GWT plugin] can implement a user settings |
| 1891 | screen that is integrated into the Gerrit user settings menu. |
| 1892 | |
| 1893 | Example settings screen: |
| 1894 | [source,java] |
| 1895 | ---- |
| 1896 | public class MyPlugin extends PluginEntryPoint { |
| 1897 | @Override |
| 1898 | public void onPluginLoad() { |
| 1899 | Plugin.get().settingsScreen("my-preferences", "My Preferences", |
| 1900 | new Screen.EntryPoint() { |
| 1901 | @Override |
| 1902 | public void onLoad(Screen screen) { |
| 1903 | screen.setPageTitle("Settings"); |
| 1904 | screen.add(new InlineLabel("My Preferences")); |
| 1905 | screen.show(); |
| 1906 | } |
| 1907 | }); |
| 1908 | } |
| 1909 | } |
| 1910 | ---- |
| 1911 | |
Edwin Kempin | fa0d494 | 2015-07-16 12:38:52 +0200 | [diff] [blame] | 1912 | By defining an link:config-gerrit.html#urlAlias[urlAlias] Gerrit |
| 1913 | administrators can map plugin screens into the Gerrit URL namespace or |
| 1914 | even replace Gerrit screens by plugin screens. |
| 1915 | |
Edwin Kempin | b1e6a3a | 2015-07-22 15:36:56 +0200 | [diff] [blame] | 1916 | Plugins may also programatically add URL aliases in the preferences of |
| 1917 | of a user. This way certain screens can be replaced for certain users. |
| 1918 | E.g. the plugin may offer a user preferences setting for choosing a |
| 1919 | screen that then sets/unsets a URL alias for the user. |
| 1920 | |
Edwin Kempin | 289f1a0 | 2014-02-04 16:08:25 +0100 | [diff] [blame] | 1921 | [[settings-screen]] |
| 1922 | == Plugin Settings Screen |
| 1923 | |
| 1924 | If a plugin implements a screen for administrating its settings that is |
| 1925 | available under "#/x/<plugin-name>/settings" it is automatically linked |
| 1926 | from the plugin list screen. |
| 1927 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1928 | [[http]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1929 | == HTTP Servlets |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1930 | |
| 1931 | Plugins or extensions may register additional HTTP servlets, and |
| 1932 | wrap them with HTTP filters. |
| 1933 | |
| 1934 | Servlets may use auto-registration to declare the URL they handle: |
| 1935 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1936 | [source,java] |
| 1937 | ---- |
| 1938 | import com.google.gerrit.extensions.annotations.Export; |
| 1939 | import com.google.inject.Singleton; |
| 1940 | import javax.servlet.http.HttpServlet; |
| 1941 | import javax.servlet.http.HttpServletRequest; |
| 1942 | import javax.servlet.http.HttpServletResponse; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1943 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1944 | @Export("/print") |
| 1945 | @Singleton |
| 1946 | class HelloServlet extends HttpServlet { |
| 1947 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 1948 | res.setContentType("text/plain"); |
| 1949 | res.setCharacterEncoding("UTF-8"); |
| 1950 | res.getWriter().write("Hello"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1951 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1952 | } |
| 1953 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1954 | |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1955 | The auto registration only works for standard servlet mappings like |
Jonathan Nieder | 5758f18 | 2015-03-30 11:28:55 -0700 | [diff] [blame] | 1956 | `/foo` or `+/foo/*+`. Regex style bindings must use a Guice ServletModule |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1957 | to register the HTTP servlets and declare it explicitly in the manifest |
| 1958 | with the `Gerrit-HttpModule` attribute: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1959 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1960 | [source,java] |
| 1961 | ---- |
| 1962 | import com.google.inject.servlet.ServletModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1963 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1964 | class MyWebUrls extends ServletModule { |
| 1965 | protected void configureServlets() { |
| 1966 | serve("/print").with(HelloServlet.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1967 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1968 | } |
| 1969 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1970 | |
| 1971 | For a plugin installed as name `helloworld`, the servlet implemented |
| 1972 | by HelloServlet class will be available to users as: |
| 1973 | |
| 1974 | ---- |
| 1975 | $ curl http://review.example.com/plugins/helloworld/print |
| 1976 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1977 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1978 | [[data-directory]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1979 | == Data Directory |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1980 | |
Dave Borowitz | 9e15875 | 2015-02-24 10:17:04 -0800 | [diff] [blame] | 1981 | Plugins can request a data directory with a `@PluginData` Path (or File, |
| 1982 | deprecated) dependency. A data directory will be created automatically |
| 1983 | by the server in `$site_path/data/$plugin_name` and passed to the |
| 1984 | plugin. |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1985 | |
| 1986 | Plugins can use this to store any data they want. |
| 1987 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1988 | [source,java] |
| 1989 | ---- |
| 1990 | @Inject |
Dave Borowitz | 9e15875 | 2015-02-24 10:17:04 -0800 | [diff] [blame] | 1991 | MyType(@PluginData java.nio.file.Path myDir) { |
| 1992 | this.in = Files.newInputStream(myDir.resolve("my.config")); |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1993 | } |
| 1994 | ---- |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1995 | |
Dariusz Luksza | ebab92a | 2014-09-10 11:14:19 +0200 | [diff] [blame] | 1996 | [[secure-store]] |
| 1997 | == SecureStore |
| 1998 | |
| 1999 | SecureStore allows to change the way Gerrit stores sensitive data like |
| 2000 | passwords. |
| 2001 | |
| 2002 | In order to replace the default SecureStore (no-op) implementation, |
| 2003 | a class that extends `com.google.gerrit.server.securestore.SecureStore` |
| 2004 | needs to be provided (with dependencies) in a separate jar file. Then |
| 2005 | link:pgm-SwitchSecureStore.html[SwitchSecureStore] must be run to |
| 2006 | switch implementations. |
| 2007 | |
| 2008 | The SecureStore implementation is instantiated using a Guice injector |
| 2009 | which binds the `File` annotated with the `@SitePath` annotation. |
| 2010 | This means that a SecureStore implementation class can get access to |
| 2011 | the `site_path` like in the following example: |
| 2012 | |
| 2013 | [source,java] |
| 2014 | ---- |
| 2015 | @Inject |
| 2016 | MySecureStore(@SitePath java.io.File sitePath) { |
| 2017 | // your code |
| 2018 | } |
| 2019 | ---- |
| 2020 | |
| 2021 | No Guice bindings or modules are required. Gerrit will automatically |
| 2022 | discover and bind the implementation. |
| 2023 | |
Michael Ochmann | 2461265 | 2016-02-12 17:26:18 +0100 | [diff] [blame] | 2024 | [[accountcreation]] |
| 2025 | == Account Creation |
| 2026 | |
| 2027 | Plugins can hook into the |
| 2028 | link:rest-api-accounts.html#create-account[account creation] REST API and |
| 2029 | inject additional external identifiers for an account that represents a user |
| 2030 | in some external user store. For that, an implementation of the extension |
| 2031 | point `com.google.gerrit.server.api.accounts.AccountExternalIdCreator` |
| 2032 | must be registered. |
| 2033 | |
| 2034 | [source,java] |
| 2035 | ---- |
| 2036 | class MyExternalIdCreator implements AccountExternalIdCreator { |
| 2037 | @Override |
| 2038 | public List<AccountExternalId> create(Account.Id id, String username, |
| 2039 | String email) { |
| 2040 | // your code |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | bind(AccountExternalIdCreator.class) |
| 2045 | .annotatedWith(UniqueAnnotations.create()) |
| 2046 | .to(MyExternalIdCreator.class); |
| 2047 | } |
| 2048 | ---- |
| 2049 | |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2050 | [[download-commands]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2051 | == Download Commands |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2052 | |
Edwin Kempin | eafde88 | 2015-05-11 15:40:44 +0200 | [diff] [blame] | 2053 | Gerrit offers commands for downloading changes and cloning projects |
| 2054 | using different download schemes (e.g. for downloading via different |
| 2055 | network protocols). Plugins can contribute download schemes, download |
| 2056 | commands and clone commands by implementing |
| 2057 | `com.google.gerrit.extensions.config.DownloadScheme`, |
| 2058 | `com.google.gerrit.extensions.config.DownloadCommand` and |
| 2059 | `com.google.gerrit.extensions.config.CloneCommand`. |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2060 | |
Edwin Kempin | eafde88 | 2015-05-11 15:40:44 +0200 | [diff] [blame] | 2061 | The download schemes, download commands and clone commands which are |
| 2062 | used most often are provided by the Gerrit core plugin |
| 2063 | `download-commands`. |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2064 | |
Edwin Kempin | 78279ba | 2015-05-22 15:22:41 +0200 | [diff] [blame] | 2065 | [[included-in]] |
| 2066 | == Included In |
| 2067 | |
| 2068 | For merged changes the link:user-review-ui.html#included-in[Included In] |
| 2069 | drop-down panel shows the branches and tags in which the change is |
| 2070 | included. |
| 2071 | |
| 2072 | Plugins can add additional systems in which the change can be included |
| 2073 | by implementing `com.google.gerrit.extensions.config.ExternalIncludedIn`, |
| 2074 | e.g. a plugin can provide a list of servers on which the change was |
| 2075 | deployed. |
| 2076 | |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2077 | [[links-to-external-tools]] |
| 2078 | == Links To External Tools |
| 2079 | |
| 2080 | Gerrit has extension points that enables development of a |
| 2081 | light-weight plugin that links commits to external |
| 2082 | tools (GitBlit, CGit, company specific resources etc). |
| 2083 | |
| 2084 | PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI. |
| 2085 | |
| 2086 | [source, java] |
| 2087 | ---- |
| 2088 | import com.google.gerrit.extensions.annotations.Listen; |
| 2089 | import com.google.gerrit.extensions.webui.PatchSetWebLink;; |
Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 2090 | import com.google.gerrit.extensions.webui.WebLinkTarget; |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2091 | |
| 2092 | @Listen |
| 2093 | public class MyWeblinkPlugin implements PatchSetWebLink { |
| 2094 | |
| 2095 | private String name = "MyLink"; |
| 2096 | private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s"; |
Sven Selberg | 5548420 | 2014-06-26 08:48:51 +0200 | [diff] [blame] | 2097 | private String imageUrl = "http://placehold.it/16x16.gif"; |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2098 | |
| 2099 | @Override |
Jonathan Nieder | b3cd690 | 2015-03-12 16:19:15 -0700 | [diff] [blame] | 2100 | public WebLinkInfo getPatchSetWebLink(String projectName, String commit) { |
Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 2101 | return new WebLinkInfo(name, |
| 2102 | imageUrl, |
| 2103 | String.format(placeHolderUrlProjectCommit, project, commit), |
| 2104 | WebLinkTarget.BLANK); |
Edwin Kempin | ceeed6b | 2014-09-11 17:07:33 +0200 | [diff] [blame] | 2105 | } |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2106 | } |
| 2107 | ---- |
| 2108 | |
David Pursehouse | 1aedee6 | 2016-12-09 11:12:27 +0900 | [diff] [blame] | 2109 | ParentWebLinks will appear to the right of the SHA1 of the parent |
| 2110 | revisions in the UI. The implementation should in most use cases direct |
| 2111 | to the same external service as PatchSetWebLink; it is provided as a |
| 2112 | separate interface because not all users want to have links for the |
| 2113 | parent revisions. |
| 2114 | |
Edwin Kempin | b3696c8 | 2014-09-11 09:41:42 +0200 | [diff] [blame] | 2115 | FileWebLinks will appear in the side-by-side diff screen on the right |
| 2116 | side of the patch selection on each side. |
| 2117 | |
Edwin Kempin | 8cdce50 | 2014-12-06 10:55:38 +0100 | [diff] [blame] | 2118 | DiffWebLinks will appear in the side-by-side and unified diff screen in |
| 2119 | the header next to the navigation icons. |
| 2120 | |
Edwin Kempin | ea00475 | 2014-04-11 15:56:02 +0200 | [diff] [blame] | 2121 | ProjectWebLinks will appear in the project list in the |
| 2122 | `Repository Browser` column. |
| 2123 | |
Edwin Kempin | 0f697bd | 2014-09-10 18:23:29 +0200 | [diff] [blame] | 2124 | BranchWebLinks will appear in the branch list in the last column. |
| 2125 | |
Edwin Kempin | f82b812 | 2016-06-03 09:20:16 +0200 | [diff] [blame] | 2126 | FileHistoryWebLinks will appear on the access rights screen. |
| 2127 | |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2128 | [[lfs-extension]] |
| 2129 | == LFS Storage Plugins |
| 2130 | |
David Pursehouse | 2463c54 | 2016-08-02 16:04:58 +0900 | [diff] [blame] | 2131 | Gerrit provides an extension point that enables development of |
| 2132 | link:https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md[ |
| 2133 | LFS (Large File Storage)] storage plugins. Gerrit core exposes the default LFS |
| 2134 | protocol endpoint `<project-name>/info/lfs/objects/batch` and forwards the requests |
| 2135 | to the configured link:config-gerrit.html#lfs[lfs.plugin] plugin which implements |
| 2136 | the LFS protocol. By exposing the default LFS endpoint, the git-lfs client can be |
| 2137 | used without any configuration. |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2138 | |
| 2139 | [source, java] |
| 2140 | ---- |
| 2141 | /** Provide an LFS protocol implementation */ |
| 2142 | import org.eclipse.jgit.lfs.server.LargeFileRepository; |
| 2143 | import org.eclipse.jgit.lfs.server.LfsProtocolServlet; |
| 2144 | |
| 2145 | @Singleton |
| 2146 | public class LfsApiServlet extends LfsProtocolServlet { |
| 2147 | private static final long serialVersionUID = 1L; |
| 2148 | |
| 2149 | private final S3LargeFileRepository repository; |
| 2150 | |
| 2151 | @Inject |
| 2152 | LfsApiServlet(S3LargeFileRepository repository) { |
| 2153 | this.repository = repository; |
| 2154 | } |
| 2155 | |
| 2156 | @Override |
| 2157 | protected LargeFileRepository getLargeFileRepository() { |
| 2158 | return repository; |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | /** Register the LfsApiServlet to listen on the default LFS protocol endpoint */ |
| 2163 | import static com.google.gerrit.httpd.plugins.LfsPluginServlet.URL_REGEX; |
| 2164 | |
| 2165 | import com.google.gerrit.httpd.plugins.HttpPluginModule; |
| 2166 | |
| 2167 | public class HttpModule extends HttpPluginModule { |
| 2168 | |
| 2169 | @Override |
| 2170 | protected void configureServlets() { |
| 2171 | serveRegex(URL_REGEX).with(LfsApiServlet.class); |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | /** Provide an implementation of the LargeFileRepository */ |
| 2176 | import org.eclipse.jgit.lfs.server.s3.S3Repository; |
| 2177 | |
| 2178 | public class S3LargeFileRepository extends S3Repository { |
| 2179 | ... |
| 2180 | } |
| 2181 | ---- |
| 2182 | |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2183 | [[metrics]] |
| 2184 | == Metrics |
| 2185 | |
| 2186 | === Metrics Reporting |
| 2187 | |
| 2188 | To send Gerrit's metrics data to an external reporting backend, a plugin can |
| 2189 | get a `MetricRegistry` injected and register an instance of a class that |
| 2190 | implements the `Reporter` interface from link:http://metrics.dropwizard.io/[ |
| 2191 | DropWizard Metrics]. |
| 2192 | |
| 2193 | Metric reporting plugin implementations are provided for |
| 2194 | link:https://gerrit.googlesource.com/plugins/metrics-reporter-jmx/[JMX], |
| 2195 | link:https://gerrit.googlesource.com/plugins/metrics-reporter-elasticsearch/[Elastic Search], |
| 2196 | and link:https://gerrit.googlesource.com/plugins/metrics-reporter-graphite/[Graphite]. |
| 2197 | |
| 2198 | There is also a working example of reporting metrics to the console in the |
| 2199 | link:https://gerrit.googlesource.com/plugins/cookbook-plugin/+/master/src/main/java/com/googlesource/gerrit/plugins/cookbook/ConsoleMetricReporter.jave[ |
| 2200 | cookbook plugin]. |
| 2201 | |
| 2202 | === Providing own metrics |
| 2203 | |
| 2204 | Plugins may provide metrics to be dispatched to external reporting services by |
| 2205 | getting a `MetricMaker` injected and creating instances of specific types of |
| 2206 | metric: |
| 2207 | |
| 2208 | * Counter |
| 2209 | + |
| 2210 | Metric whose value increments during the life of the process. |
| 2211 | |
| 2212 | * Timer |
| 2213 | + |
| 2214 | Metric recording time spent on an operation. |
| 2215 | |
| 2216 | * Histogram |
| 2217 | + |
| 2218 | Metric recording statistical distribution (rate) of values. |
| 2219 | |
David Pursehouse | 48d05ea | 2017-02-03 19:05:29 +0900 | [diff] [blame] | 2220 | Note that metrics cannot be recorded from plugin init steps that |
| 2221 | are run during site initialization. |
| 2222 | |
David Pursehouse | c3bbd56 | 2017-02-06 20:25:29 +0900 | [diff] [blame^] | 2223 | By default, plugin metrics are recorded under |
| 2224 | `plugins/${plugin-name}/${metric-name}`. This can be changed by |
| 2225 | setting `plugins.${plugin-name}.metricsPrefix` in the `gerrit.config` |
| 2226 | file. For example: |
| 2227 | |
| 2228 | ---- |
| 2229 | [plugin "my-plugin"] |
| 2230 | metricsPrefix = my-metrics |
| 2231 | ---- |
| 2232 | |
| 2233 | will cause the metrics to be recorded under `my-metrics/${metric-name}`. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2234 | |
| 2235 | See the replication metrics in the |
| 2236 | link:https://gerrit.googlesource.com/plugins/replication/+/master/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationMetrics.java[ |
| 2237 | replication plugin] for an example of usage. |
| 2238 | |
Edwin Kempin | da17bc3 | 2016-06-14 11:50:58 +0200 | [diff] [blame] | 2239 | [[account-patch-review-store]] |
| 2240 | == AccountPatchReviewStore |
| 2241 | |
| 2242 | The AccountPatchReviewStore is used to store reviewed flags on changes. |
| 2243 | A reviewed flag is a tuple of (patch set ID, file, account ID) and |
| 2244 | records whether the user has reviewed a file in a patch set. Each user |
| 2245 | can easily have thousands of reviewed flags and the number of reviewed |
| 2246 | flags is growing without bound. The store must be able handle this data |
| 2247 | volume efficiently. |
| 2248 | |
| 2249 | Gerrit implements this extension point, but plugins may bind another |
| 2250 | implementation, e.g. one that supports multi-master. |
| 2251 | |
| 2252 | ---- |
| 2253 | DynamicItem.bind(binder(), AccountPatchReviewStore.class) |
| 2254 | .to(MultiMasterAccountPatchReviewStore.class); |
| 2255 | |
| 2256 | ... |
| 2257 | |
| 2258 | public class MultiMasterAccountPatchReviewStore |
| 2259 | implements AccountPatchReviewStore { |
| 2260 | ... |
| 2261 | } |
| 2262 | ---- |
| 2263 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2264 | [[documentation]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2265 | == Documentation |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2266 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2267 | If a plugin does not register a filter or servlet to handle URLs |
Jonathan Nieder | 5758f18 | 2015-03-30 11:28:55 -0700 | [diff] [blame] | 2268 | `+/Documentation/*+` or `+/static/*+`, the core Gerrit server will |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2269 | automatically export these resources over HTTP from the plugin JAR. |
| 2270 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2271 | Static resources under the `static/` directory in the JAR will be |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 2272 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 2273 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2274 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2275 | Documentation files under the `Documentation/` directory in the JAR |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 2276 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 2277 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 2278 | attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2279 | |
Christian Aistleitner | 040cf82 | 2015-03-26 21:09:09 +0100 | [diff] [blame] | 2280 | Documentation may be written in the Markdown flavor |
| 2281 | link:https://github.com/sirthias/pegdown[pegdown] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2282 | if the file name ends with `.md`. Gerrit will automatically convert |
| 2283 | Markdown to HTML if accessed with extension `.html`. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2284 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2285 | [[macros]] |
Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 2286 | Within the Markdown documentation files macros can be used that allow |
| 2287 | to write documentation with reasonably accurate examples that adjust |
| 2288 | automatically based on the installation. |
| 2289 | |
| 2290 | The following macros are supported: |
| 2291 | |
| 2292 | [width="40%",options="header"] |
| 2293 | |=================================================== |
| 2294 | |Macro | Replacement |
| 2295 | |@PLUGIN@ | name of the plugin |
| 2296 | |@URL@ | Gerrit Web URL |
| 2297 | |@SSH_HOST@ | SSH Host |
| 2298 | |@SSH_PORT@ | SSH Port |
| 2299 | |=================================================== |
| 2300 | |
| 2301 | The macros will be replaced when the documentation files are rendered |
| 2302 | from Markdown to HTML. |
| 2303 | |
| 2304 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 2305 | even if there is an expansion for `KEEP` in the future. |
| 2306 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2307 | [[auto-index]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2308 | === Automatic Index |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2309 | |
| 2310 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 2311 | redirect clients to the plugin's `/Documentation/index.html`. |
| 2312 | Requests for `/Documentation/` (bare directory) will also redirect |
| 2313 | to `/Documentation/index.html`. |
| 2314 | |
| 2315 | If neither resource `Documentation/index.html` or |
| 2316 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
| 2317 | automatically generate an index page for the plugin's documentation |
| 2318 | tree by scanning every `*.md` and `*.html` file in the Documentation/ |
| 2319 | directory. |
| 2320 | |
| 2321 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 2322 | header of the file and extract the first level one title. This |
| 2323 | title text will be used as display text for a link to the HTML |
| 2324 | version of the page. |
| 2325 | |
| 2326 | For any discovered HTML (`*.html`) file, Gerrit will use the name |
| 2327 | of the file, minus the `*.html` extension, as the link text. Any |
| 2328 | hyphens in the file name will be replaced with spaces. |
| 2329 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2330 | If a discovered file is named `about.md` or `about.html`, its |
| 2331 | content will be inserted in an 'About' section at the top of the |
| 2332 | auto-generated index page. If both `about.md` and `about.html` |
| 2333 | exist, only the first discovered file will be used. |
| 2334 | |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2335 | If a discovered file name beings with `cmd-` it will be clustered |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2336 | into a 'Commands' section of the generated index page. |
| 2337 | |
David Pursehouse | fe52915 | 2013-08-14 16:35:06 +0900 | [diff] [blame] | 2338 | If a discovered file name beings with `servlet-` it will be clustered |
| 2339 | into a 'Servlets' section of the generated index page. |
| 2340 | |
| 2341 | If a discovered file name beings with `rest-api-` it will be clustered |
| 2342 | into a 'REST APIs' section of the generated index page. |
| 2343 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2344 | All other files are clustered under a 'Documentation' section. |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2345 | |
| 2346 | Some optional information from the manifest is extracted and |
| 2347 | displayed as part of the index page, if present in the manifest: |
| 2348 | |
| 2349 | [width="40%",options="header"] |
| 2350 | |=================================================== |
| 2351 | |Field | Source Attribute |
| 2352 | |Name | Implementation-Title |
| 2353 | |Vendor | Implementation-Vendor |
| 2354 | |Version | Implementation-Version |
| 2355 | |URL | Implementation-URL |
| 2356 | |API Version | Gerrit-ApiVersion |
| 2357 | |=================================================== |
| 2358 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2359 | [[deployment]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2360 | == Deployment |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2361 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 2362 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 2363 | server using the link:cmd-plugin-install.html[plugin install] command. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2364 | |
David Pursehouse | a1d633b | 2014-05-02 17:21:02 +0900 | [diff] [blame] | 2365 | Web UI plugins distributed as single `.js` file can be deployed |
Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame] | 2366 | without the overhead of JAR packaging, for more information refer to |
| 2367 | link:cmd-plugin-install.html[plugin install] command. |
| 2368 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2369 | Plugins can also be copied directly into the server's |
Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame] | 2370 | directory at `$site_path/plugins/$name.(jar|js)`. The name of |
| 2371 | 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] | 2372 | plugin name. Unless disabled, servers periodically scan this |
| 2373 | directory for updated plugins. The time can be adjusted by |
| 2374 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2375 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 2376 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 2377 | command can be used. |
| 2378 | |
Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 2379 | Disabled plugins can be re-enabled using the |
| 2380 | link:cmd-plugin-enable.html[plugin enable] command. |
| 2381 | |
Edwin Kempin | c1a2510 | 2015-06-22 14:47:36 +0200 | [diff] [blame] | 2382 | == Known issues and bugs |
| 2383 | |
| 2384 | === Error handling in UI when using the REST API |
| 2385 | |
| 2386 | When a plugin invokes a REST endpoint in the UI, it provides an |
| 2387 | `AsyncCallback` to handle the result. At the moment the |
| 2388 | `onFailure(Throwable)` of the callback is never invoked, even if there |
| 2389 | is an error. Errors are always handled by the Gerrit core UI which |
| 2390 | shows the error dialog. This means currently plugins cannot do any |
| 2391 | error handling and e.g. ignore expected errors. |
| 2392 | |
| 2393 | In the following example the REST endpoint would return '404 Not Found' |
| 2394 | if there is no HTTP password and the Gerrit core UI would display an |
| 2395 | error dialog for this. However having no HTTP password is not an error |
| 2396 | and the plugin may like to handle this case. |
| 2397 | |
| 2398 | [source,java] |
| 2399 | ---- |
| 2400 | new RestApi("accounts").id("self").view("password.http") |
| 2401 | .get(new AsyncCallback<NativeString>() { |
| 2402 | |
| 2403 | @Override |
| 2404 | public void onSuccess(NativeString httpPassword) { |
| 2405 | // TODO |
| 2406 | } |
| 2407 | |
| 2408 | @Override |
| 2409 | public void onFailure(Throwable caught) { |
| 2410 | // never invoked |
| 2411 | } |
| 2412 | }); |
| 2413 | ---- |
| 2414 | |
| 2415 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2416 | == SEE ALSO |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 2417 | |
| 2418 | * link:js-api.html[JavaScript API] |
| 2419 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 2420 | |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2421 | GERRIT |
| 2422 | ------ |
| 2423 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 2424 | |
| 2425 | SEARCHBOX |
| 2426 | --------- |