Marian Harbach | ebeb154 | 2019-12-13 10:42:46 +0100 | [diff] [blame] | 1 | :linkattrs: |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2 | = Gerrit Code Review - Plugin Development |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 3 | |
Edwin Kempin | af27532 | 2012-07-16 11:04:01 +0200 | [diff] [blame] | 4 | The Gerrit server functionality can be extended by installing plugins. |
Luca Milanesio | 71e434c | 2019-06-03 13:13:13 +0100 | [diff] [blame] | 5 | This page describes how plugins for Gerrit can be developed and hosted |
| 6 | on gerrit-review.googlesource.com. |
Edwin Kempin | af27532 | 2012-07-16 11:04:01 +0200 | [diff] [blame] | 7 | |
Viktar Donich | 5055e8d | 2017-11-09 13:02:42 -0800 | [diff] [blame] | 8 | For PolyGerrit-specific plugin development, consult with |
| 9 | link:pg-plugin-dev.html[PolyGerrit Plugin Development] guide. |
| 10 | |
Edwin Kempin | af27532 | 2012-07-16 11:04:01 +0200 | [diff] [blame] | 11 | Depending on how tightly the extension code is coupled with the Gerrit |
| 12 | server code, there is a distinction between `plugins` and `extensions`. |
| 13 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 14 | [[plugin]] |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 15 | 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] | 16 | JVM as Gerrit. It has full access to all server internals. Plugins |
| 17 | are tightly coupled to a specific major.minor server version and |
| 18 | may require source code changes to compile against a different |
| 19 | server version. |
| 20 | |
Luca Milanesio | 86b9b6c | 2017-08-09 09:54:36 +0100 | [diff] [blame] | 21 | Plugins may require a specific major.minor.patch server version |
| 22 | and may need rebuild and revalidation across different |
| 23 | patch levels. A different patch level may only add new |
| 24 | API interfaces and never change or extend existing ones. |
| 25 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 26 | [[extension]] |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 27 | 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] | 28 | 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] | 29 | server's internals. The limited visibility reduces the extension's |
| 30 | dependencies, enabling it to be compatible across a wider range |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 31 | of server versions. |
| 32 | |
| 33 | Most of this documentation refers to either type as a plugin. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 34 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 35 | [[getting-started]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 36 | == Getting started |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 37 | |
David Pursehouse | 092ac5c | 2019-04-25 19:55:49 +0900 | [diff] [blame] | 38 | To get started with the development of a plugin, take a look at |
| 39 | the samples in the |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 40 | link:https://gerrit.googlesource.com/plugins/examples[examples plugin project,role=external,window=_blank]. |
David Pursehouse | cf2e900 | 2017-03-01 19:10:43 +0900 | [diff] [blame] | 41 | |
| 42 | This is a project that demonstrates the various features of the |
| 43 | plugin API. It can be taken as an example to develop an own plugin. |
| 44 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 45 | When starting from this example one should take care to adapt the |
David Ostrovsky | a052e52 | 2016-12-10 17:53:16 +0100 | [diff] [blame] | 46 | `Gerrit-ApiVersion` in the `BUILD` to the version of Gerrit for which |
| 47 | the plugin is developed. |
Dave Borowitz | 5cc8f66 | 2012-05-21 09:51:36 -0700 | [diff] [blame] | 48 | |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 49 | [[API]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 50 | == API |
Edwin Kempin | f878c4b | 2012-07-18 09:34:25 +0200 | [diff] [blame] | 51 | |
| 52 | There are two different API formats offered against which plugins can |
| 53 | be developed: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 54 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 55 | gerrit-extension-api.jar:: |
| 56 | A stable but thin interface. Suitable for extensions that need |
| 57 | to be notified of events, but do not require tight coupling to |
| 58 | the internals of Gerrit. Extensions built against this API can |
| 59 | expect to be binary compatible across a wide range of server |
| 60 | versions. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 61 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 62 | gerrit-plugin-api.jar:: |
| 63 | The complete internals of the Gerrit server, permitting a |
| 64 | plugin to tightly couple itself and provide additional |
| 65 | functionality that is not possible as an extension. Plugins |
| 66 | built against this API are expected to break at the source |
| 67 | code level between every major.minor Gerrit release. A plugin |
| 68 | that compiles against 2.5 will probably need source code level |
| 69 | changes to work with 2.6, 2.7, and so on. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 70 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 71 | == Manifest |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 72 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 73 | Plugins may provide optional description information with standard |
| 74 | manifest fields: |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 75 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 76 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 77 | Implementation-Title: Example plugin showing examples |
| 78 | Implementation-Version: 1.0 |
| 79 | Implementation-Vendor: Example, Inc. |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 80 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 81 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 82 | === ApiType |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 83 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 84 | Plugins using the tightly coupled `gerrit-plugin-api.jar` must |
| 85 | declare this API dependency in the manifest to gain access to server |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 86 | internals. If no `Gerrit-ApiType` is specified the stable `extension` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 87 | API will be assumed. This may cause ClassNotFoundExceptions when |
| 88 | loading a plugin that needs the plugin API. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 89 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 90 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 91 | Gerrit-ApiType: plugin |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 92 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 93 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 94 | === Explicit Registration |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 95 | |
| 96 | Plugins that use explicit Guice registration must name the Guice |
| 97 | 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] | 98 | manifest. `Gerrit-Module` supplies bindings to the core server; |
| 99 | `Gerrit-SshModule` supplies SSH commands to the SSH server (if |
| 100 | enabled); `Gerrit-HttpModule` supplies servlets and filters to the HTTP |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 101 | server (if enabled). If no modules are named automatic registration |
| 102 | will be performed by scanning all classes in the plugin JAR for |
| 103 | `@Listen` and `@Export("")` annotations. |
| 104 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 105 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 106 | Gerrit-Module: tld.example.project.CoreModuleClassName |
| 107 | Gerrit-SshModule: tld.example.project.SshModuleClassName |
| 108 | Gerrit-HttpModule: tld.example.project.HttpModuleClassName |
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 | |
Maxime Guerreiro | 5ccf0b9 | 2018-05-15 16:28:22 +0000 | [diff] [blame] | 111 | === Batch runtime |
| 112 | |
| 113 | Gerrit can be run as a server, serving HTTP or SSH requests, or as an |
| 114 | offline program. Plugins can contribute Guice modules to this batch |
| 115 | runtime by binding `Gerrit-BatchModule` to one of their classes. |
| 116 | The Guice injector is bound to less classes, and some Gerrit features |
| 117 | will be absent - on purpose. |
| 118 | |
| 119 | This feature was originally introduced to support plugins during an |
| 120 | offline reindexing task. |
| 121 | |
| 122 | ---- |
| 123 | Gerrit-BatchModule: tld.example.project.CoreModuleClassName |
| 124 | ---- |
| 125 | |
| 126 | In this runtime, only the module designated by `Gerrit-BatchModule` is |
| 127 | enabled, not `Gerrit-SysModule`. |
| 128 | |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 129 | [[plugin_name]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 130 | === Plugin Name |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 131 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 132 | A plugin can optionally provide its own plugin name. |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 133 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 134 | ---- |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 135 | Gerrit-PluginName: replication |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 136 | ---- |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 137 | |
| 138 | This is useful for plugins that contribute plugin-owned capabilities that |
| 139 | are stored in the `project.config` file. Another use case is to be able to put |
| 140 | project specific plugin configuration section in `project.config`. In this |
| 141 | case it is advantageous to reserve the plugin name to access the configuration |
| 142 | section in the `project.config` file. |
| 143 | |
| 144 | If `Gerrit-PluginName` is omitted, then the plugin's name is determined from |
| 145 | the plugin file name. |
| 146 | |
| 147 | If a plugin provides its own name, then that plugin cannot be deployed |
| 148 | multiple times under different file names on one Gerrit site. |
| 149 | |
| 150 | For Maven driven plugins, the following line must be included in the pom.xml |
| 151 | file: |
| 152 | |
| 153 | [source,xml] |
| 154 | ---- |
| 155 | <manifestEntries> |
| 156 | <Gerrit-PluginName>name</Gerrit-PluginName> |
| 157 | </manifestEntries> |
| 158 | ---- |
| 159 | |
David Ostrovsky | fdbfcad | 2016-11-15 06:35:29 -0800 | [diff] [blame] | 160 | For Bazel driven plugins, the following line must be included in the BUILD |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 161 | configuration file: |
| 162 | |
| 163 | [source,python] |
| 164 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 165 | manifest_entries = [ |
| 166 | 'Gerrit-PluginName: name', |
| 167 | ] |
David Ostrovsky | 366ad0e | 2013-09-05 19:59:09 +0200 | [diff] [blame] | 168 | ---- |
| 169 | |
Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 170 | A plugin can get its own name injected at runtime: |
| 171 | |
| 172 | [source,java] |
| 173 | ---- |
| 174 | public class MyClass { |
| 175 | |
| 176 | private final String pluginName; |
| 177 | |
| 178 | @Inject |
| 179 | public MyClass(@PluginName String pluginName) { |
| 180 | this.pluginName = pluginName; |
| 181 | } |
| 182 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 183 | [...] |
Edwin Kempin | c0b1b0e | 2013-10-01 14:13:54 +0200 | [diff] [blame] | 184 | } |
| 185 | ---- |
| 186 | |
David Pursehouse | 8ed0d92 | 2013-10-18 18:57:56 +0900 | [diff] [blame] | 187 | A plugin can get its canonical web URL injected at runtime: |
| 188 | |
| 189 | [source,java] |
| 190 | ---- |
| 191 | public class MyClass { |
| 192 | |
| 193 | private final String url; |
| 194 | |
| 195 | @Inject |
| 196 | public MyClass(@PluginCanonicalWebUrl String url) { |
| 197 | this.url = url; |
| 198 | } |
| 199 | |
| 200 | [...] |
| 201 | } |
| 202 | ---- |
| 203 | |
| 204 | The URL is composed of the server's canonical web URL and the plugin's |
| 205 | name, i.e. `http://review.example.com:8080/plugin-name`. |
| 206 | |
| 207 | The canonical web URL may be injected into any .jar plugin regardless of |
| 208 | whether or not the plugin provides an HTTP servlet. |
| 209 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 210 | [[reload_method]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 211 | === Reload Method |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 212 | |
| 213 | If a plugin holds an exclusive resource that must be released before |
| 214 | loading the plugin again (for example listening on a network port or |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 215 | acquiring a file lock) the manifest must declare `Gerrit-ReloadMode` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 216 | to be `restart`. Otherwise the preferred method of `reload` will |
| 217 | be used, as it enables the server to hot-patch an updated plugin |
| 218 | with no down time. |
| 219 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 220 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 221 | Gerrit-ReloadMode: restart |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 222 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 223 | |
| 224 | In either mode ('restart' or 'reload') any plugin or extension can |
| 225 | be updated without restarting the Gerrit server. The difference is |
| 226 | how Gerrit handles the upgrade: |
| 227 | |
| 228 | restart:: |
| 229 | The old plugin is completely stopped. All registrations of SSH |
| 230 | commands and HTTP servlets are removed. All registrations of any |
| 231 | extension points are removed. All registered LifecycleListeners |
| 232 | have their `stop()` method invoked in reverse order. The new |
| 233 | plugin is started, and registrations are made from the new |
| 234 | plugin. There is a brief window where neither the old nor the |
| 235 | new plugin is connected to the server. This means SSH commands |
| 236 | and HTTP servlets will return not found errors, and the plugin |
| 237 | will not be notified of events that occurred during the restart. |
| 238 | |
| 239 | reload:: |
| 240 | The new plugin is started. Its LifecycleListeners are permitted |
| 241 | to perform their `start()` methods. All SSH and HTTP registrations |
| 242 | are atomically swapped out from the old plugin to the new plugin, |
| 243 | ensuring the server never returns a not found error. All extension |
| 244 | point listeners are atomically swapped out from the old plugin to |
| 245 | the new plugin, ensuring no events are missed (however some events |
| 246 | may still route to the old plugin if the swap wasn't complete yet). |
| 247 | The old plugin is stopped. |
| 248 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 249 | To reload/restart a plugin the link:cmd-plugin-reload.html[plugin reload] |
| 250 | command can be used. |
| 251 | |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 252 | [[init_step]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 253 | === Init step |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 254 | |
| 255 | Plugins can contribute their own "init step" during the Gerrit init |
| 256 | wizard. This is useful for guiding the Gerrit administrator through |
David Pursehouse | 659860f | 2013-12-16 14:50:04 +0900 | [diff] [blame] | 257 | the settings needed by the plugin to work properly. |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 258 | |
| 259 | For instance plugins to integrate Jira issues to Gerrit changes may |
| 260 | contribute their own "init step" to allow configuring the Jira URL, |
| 261 | credentials and possibly verify connectivity to validate them. |
| 262 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 263 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 264 | Gerrit-InitStep: tld.example.project.MyInitStep |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 265 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 266 | |
| 267 | MyInitStep needs to follow the standard Gerrit InitStep syntax |
David Pursehouse | 9246356 | 2013-06-24 10:16:28 +0900 | [diff] [blame] | 268 | and behavior: writing to the console using the injected ConsoleUI |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 269 | and accessing / changing configuration settings using Section.Factory. |
| 270 | |
| 271 | In addition to the standard Gerrit init injections, plugins receive |
| 272 | the @PluginName String injection containing their own plugin name. |
| 273 | |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 274 | During their initialization plugins may get access to the |
| 275 | `project.config` file of the `All-Projects` project and they are able |
| 276 | to store configuration parameters in it. For this a plugin `InitStep` |
Jiří Engelthaler | 3033a0a | 2015-02-16 09:44:32 +0100 | [diff] [blame] | 277 | can get `com.google.gerrit.pgm.init.api.AllProjectsConfig` injected: |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 278 | |
| 279 | [source,java] |
| 280 | ---- |
| 281 | public class MyInitStep implements InitStep { |
| 282 | private final String pluginName; |
| 283 | private final ConsoleUI ui; |
| 284 | private final AllProjectsConfig allProjectsConfig; |
| 285 | |
Doug Kelly | 732ad20 | 2015-11-13 13:11:32 -0800 | [diff] [blame] | 286 | @Inject |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 287 | public MyInitStep(@PluginName String pluginName, ConsoleUI ui, |
| 288 | AllProjectsConfig allProjectsConfig) { |
| 289 | this.pluginName = pluginName; |
| 290 | this.ui = ui; |
| 291 | this.allProjectsConfig = allProjectsConfig; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void run() throws Exception { |
Edwin Kempin | 93e7d5d | 2014-01-03 09:53:20 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void postRun() throws Exception { |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 300 | ui.message("\n"); |
| 301 | ui.header(pluginName + " Integration"); |
| 302 | boolean enabled = ui.yesno(true, "By default enabled for all projects"); |
Adrian Görler | d161297 | 2014-10-20 17:06:07 +0200 | [diff] [blame] | 303 | Config cfg = allProjectsConfig.load().getConfig(); |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 304 | if (enabled) { |
| 305 | cfg.setBoolean("plugin", pluginName, "enabled", enabled); |
| 306 | } else { |
| 307 | cfg.unset("plugin", pluginName, "enabled"); |
| 308 | } |
| 309 | allProjectsConfig.save(pluginName, "Initialize " + pluginName + " Integration"); |
| 310 | } |
| 311 | } |
| 312 | ---- |
| 313 | |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 314 | Bear in mind that the Plugin's InitStep class will be loaded but |
| 315 | the standard Gerrit runtime environment is not available and the plugin's |
| 316 | own Guice modules were not initialized. |
| 317 | This means the InitStep for a plugin is not executed in the same way that |
| 318 | the plugin executes within the server, and may mean a plugin author cannot |
| 319 | trivially reuse runtime code during init. |
| 320 | |
| 321 | For instance a plugin that wants to verify connectivity may need to statically |
| 322 | call the constructor of their connection class, passing in values obtained |
| 323 | from the Section.Factory rather than from an injected Config object. |
| 324 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 325 | Plugins' InitSteps are executed during the "Gerrit Plugin init" phase, after |
| 326 | the extraction of the plugins embedded in the distribution .war file into |
Dave Borowitz | ada289c | 2018-12-18 13:24:14 -0800 | [diff] [blame] | 327 | `$GERRIT_SITE/plugins` and before the site initialization or upgrade. |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 328 | |
Dave Borowitz | ada289c | 2018-12-18 13:24:14 -0800 | [diff] [blame] | 329 | A plugin's InitStep cannot refer to any Gerrit runtime objects injected at |
| 330 | startup. |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 331 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 332 | [source,java] |
| 333 | ---- |
| 334 | public class MyInitStep implements InitStep { |
| 335 | private final ConsoleUI ui; |
| 336 | private final Section.Factory sections; |
| 337 | private final String pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 338 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 339 | @Inject |
| 340 | public GitBlitInitStep(final ConsoleUI ui, Section.Factory sections, @PluginName String pluginName) { |
| 341 | this.ui = ui; |
| 342 | this.sections = sections; |
| 343 | this.pluginName = pluginName; |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 344 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 345 | |
| 346 | @Override |
| 347 | public void run() throws Exception { |
| 348 | ui.header("\nMy plugin"); |
| 349 | |
| 350 | Section mySection = getSection("myplugin", null); |
| 351 | mySection.string("Link name", "linkname", "MyLink"); |
| 352 | } |
Edwin Kempin | 93e7d5d | 2014-01-03 09:53:20 +0100 | [diff] [blame] | 353 | |
| 354 | @Override |
| 355 | public void postRun() throws Exception { |
| 356 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 357 | } |
| 358 | ---- |
Luca Milanesio | 737285d | 2012-09-25 14:26:43 +0100 | [diff] [blame] | 359 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 360 | [[classpath]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 361 | == Classpath |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 362 | |
| 363 | Each plugin is loaded into its own ClassLoader, isolating plugins |
| 364 | from each other. A plugin or extension inherits the Java runtime |
| 365 | and the Gerrit API chosen by `Gerrit-ApiType` (extension or plugin) |
| 366 | from the hosting server. |
| 367 | |
| 368 | Plugins are loaded from a single JAR file. If a plugin needs |
| 369 | additional libraries, it must include those dependencies within |
| 370 | its own JAR. Plugins built using Maven may be able to use the |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 371 | link:http://maven.apache.org/plugins/maven-shade-plugin/[shade plugin,role=external,window=_blank] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 372 | to package additional dependencies. Relocating (or renaming) classes |
| 373 | should not be necessary due to the ClassLoader isolation. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 374 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 375 | [[events]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 376 | == Listening to Events |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 377 | |
| 378 | Certain operations in Gerrit trigger events. Plugins may receive |
| 379 | notifications of these events by implementing the corresponding |
| 380 | listeners. |
| 381 | |
Jakub Sokół | 87c5bfc4 | 2020-09-17 16:06:23 +0200 | [diff] [blame] | 382 | * `com.google.gerrit.server.events.EventListener`: |
Edwin Kempin | 64059f5 | 2013-10-31 13:49:25 +0100 | [diff] [blame] | 383 | + |
Hugo Arès | bc1093d | 2016-02-23 15:04:50 -0500 | [diff] [blame] | 384 | Allows to listen to events without user visibility restrictions. These |
| 385 | 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] | 386 | the link:cmd-stream-events.html[gerrit stream-events] command. |
| 387 | |
Jakub Sokół | 87c5bfc4 | 2020-09-17 16:06:23 +0200 | [diff] [blame] | 388 | * `com.google.gerrit.server.events.UserScopedEventListener`: |
Hugo Arès | bc1093d | 2016-02-23 15:04:50 -0500 | [diff] [blame] | 389 | + |
| 390 | Allows to listen to events visible to the specified user. These are the |
| 391 | same link:cmd-stream-events.html#events[events] that are also streamed |
| 392 | by the link:cmd-stream-events.html[gerrit stream-events] command. |
| 393 | |
Christian Aistleitner | db930bd | 2020-05-01 11:05:38 +0200 | [diff] [blame] | 394 | * `com.google.gerrit.extensions.events.AccountActivationListener`: |
| 395 | + |
| 396 | User account got activated or deactivated |
| 397 | |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 398 | * `com.google.gerrit.extensions.events.LifecycleListener`: |
| 399 | + |
Edwin Kempin | 3e7928a | 2013-12-03 07:39:00 +0100 | [diff] [blame] | 400 | Plugin start and stop |
Edwin Kempin | 9820266 | 2013-09-18 16:03:03 +0200 | [diff] [blame] | 401 | |
| 402 | * `com.google.gerrit.extensions.events.NewProjectCreatedListener`: |
| 403 | + |
| 404 | Project creation |
| 405 | |
| 406 | * `com.google.gerrit.extensions.events.ProjectDeletedListener`: |
| 407 | + |
| 408 | Project deletion |
| 409 | |
Edwin Kempin | b27c939 | 2013-11-19 13:12:43 +0100 | [diff] [blame] | 410 | * `com.google.gerrit.extensions.events.HeadUpdatedListener`: |
| 411 | + |
| 412 | Update of HEAD on a project |
| 413 | |
Stefan Lay | 310d77d | 2014-05-28 13:45:25 +0200 | [diff] [blame] | 414 | * `com.google.gerrit.extensions.events.UsageDataPublishedListener`: |
| 415 | + |
| 416 | Publication of usage data |
| 417 | |
Adrian Görler | f4a4c9a | 2014-08-22 17:09:18 +0200 | [diff] [blame] | 418 | * `com.google.gerrit.extensions.events.GarbageCollectorListener`: |
| 419 | + |
| 420 | Garbage collection ran on a project |
| 421 | |
Hector Oswaldo Caballero | 5fbbdad | 2015-11-11 14:30:46 -0500 | [diff] [blame] | 422 | * `com.google.gerrit.server.extensions.events.ChangeIndexedListener`: |
| 423 | + |
Hugo Arès | 682171f | 2017-04-24 13:44:43 +0200 | [diff] [blame] | 424 | Update of the change secondary index |
| 425 | |
| 426 | * `com.google.gerrit.server.extensions.events.AccountIndexedListener`: |
| 427 | + |
| 428 | Update of the account secondary index |
Hector Oswaldo Caballero | 5fbbdad | 2015-11-11 14:30:46 -0500 | [diff] [blame] | 429 | |
Hugo Arès | ee788ddb | 2017-05-08 10:23:45 -0400 | [diff] [blame] | 430 | * `com.google.gerrit.server.extensions.events.GroupIndexedListener`: |
| 431 | + |
| 432 | Update of the group secondary index |
| 433 | |
Xin Sun | 9716986 | 2017-07-13 17:31:16 -0700 | [diff] [blame] | 434 | * `com.google.gerrit.server.extensions.events.ProjectIndexedListener`: |
| 435 | + |
| 436 | Update of the project secondary index |
| 437 | |
Luca Milanesio | 45da618 | 2016-05-12 11:33:30 +0100 | [diff] [blame] | 438 | * `com.google.gerrit.httpd.WebLoginListener`: |
| 439 | + |
| 440 | User login or logout interactively on the Web user interface. |
| 441 | |
| 442 | The event listener is under the Gerrit http package to automatically |
| 443 | inherit the javax.servlet.http dependencies and allowing to influence |
| 444 | the login or logout flow with additional redirections. |
| 445 | |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 446 | [[stream-events]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 447 | == Sending Events to the Events Stream |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 448 | |
| 449 | Plugins may send events to the events stream where consumers of |
| 450 | Gerrit's `stream-events` ssh command will receive them. |
| 451 | |
| 452 | To send an event, the plugin must invoke one of the `postEvent` |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 453 | methods in the `EventDispatcher` interface, passing an instance of |
Martin Fick | 4c72aea | 2014-12-10 14:58:12 -0700 | [diff] [blame] | 454 | its own custom event class derived from |
| 455 | `com.google.gerrit.server.events.Event`. |
Yang Zhenhui | 2659d42 | 2013-07-30 16:59:58 +0800 | [diff] [blame] | 456 | |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 457 | [source,java] |
| 458 | ---- |
| 459 | import com.google.gerrit.common.EventDispatcher; |
Nasser Grainawi | ae45483 | 2019-11-15 14:25:51 -0800 | [diff] [blame] | 460 | import com.google.gerrit.exceptions.StorageException; |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 461 | import com.google.gerrit.extensions.registration.DynamicItem; |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 462 | import com.google.inject.Inject; |
| 463 | |
| 464 | class MyPlugin { |
| 465 | private final DynamicItem<EventDispatcher> eventDispatcher; |
| 466 | |
| 467 | @Inject |
| 468 | myPlugin(DynamicItem<EventDispatcher> eventDispatcher) { |
| 469 | this.eventDispatcher = eventDispatcher; |
| 470 | } |
| 471 | |
| 472 | private void postEvent(MyPluginEvent event) { |
| 473 | try { |
| 474 | eventDispatcher.get().postEvent(event); |
Dave Borowitz | 62f32fc | 2019-01-15 18:45:22 -0800 | [diff] [blame] | 475 | } catch (StorageException e) { |
David Pursehouse | a9bf476 | 2016-07-08 09:34:35 +0900 | [diff] [blame] | 476 | // error handling |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | ---- |
| 481 | |
Martin Fick | 0aef6f1 | 2014-12-11 16:54:21 -0700 | [diff] [blame] | 482 | Plugins which define new Events should register them via the |
David Ostrovsky | 5edf70e | 2020-02-04 22:57:14 +0100 | [diff] [blame] | 483 | `com.google.gerrit.server.events.EventTypes.register()` method. |
| 484 | This will make the EventType known to the system. Deserializing |
| 485 | events with the |
Martin Fick | f70c20a | 2014-12-11 17:03:15 -0700 | [diff] [blame] | 486 | `com.google.gerrit.server.events.EventDeserializer` class requires |
| 487 | that the event be registered in EventTypes. |
Martin Fick | 0aef6f1 | 2014-12-11 16:54:21 -0700 | [diff] [blame] | 488 | |
Martin Fick | ecafc93 | 2014-12-15 14:09:41 -0700 | [diff] [blame] | 489 | == Modifying the Stream Event Flow |
| 490 | |
| 491 | It is possible to modify the stream event flow from plugins by registering |
| 492 | an `com.google.gerrit.server.events.EventDispatcher`. A plugin may register |
| 493 | a Dispatcher class to replace the internal Dispatcher. EventDispatcher is |
| 494 | a DynamicItem, so Gerrit may only have one copy. |
| 495 | |
Edwin Kempin | 3273760 | 2014-01-23 09:04:58 +0100 | [diff] [blame] | 496 | [[validation]] |
David Pursehouse | 91c5f5e | 2014-01-23 18:57:33 +0900 | [diff] [blame] | 497 | == Validation Listeners |
Edwin Kempin | 3273760 | 2014-01-23 09:04:58 +0100 | [diff] [blame] | 498 | |
| 499 | Certain operations in Gerrit can be validated by plugins by |
| 500 | implementing the corresponding link:config-validation.html[listeners]. |
| 501 | |
Andrii Shyshkalov | 6fdc8eb | 2016-11-29 17:45:01 +0100 | [diff] [blame] | 502 | [[change-message-modifier]] |
| 503 | == Change Message Modifier |
| 504 | |
| 505 | `com.google.gerrit.server.git.ChangeMessageModifier`: |
| 506 | plugins implementing this can modify commit message of the change being |
| 507 | submitted by Rebase Always and Cherry Pick submit strategies as well as |
| 508 | change being queried with COMMIT_FOOTERS option. |
| 509 | |
Edwin Kempin | 19e89c8 | 2017-10-11 12:00:51 +0200 | [diff] [blame] | 510 | [[merge-super-set-computation]] |
| 511 | == Merge Super Set Computation |
| 512 | |
| 513 | The algorithm to compute the merge super set to detect changes that |
| 514 | should be submitted together can be customized by implementing |
| 515 | `com.google.gerrit.server.git.MergeSuperSetComputation`. |
| 516 | MergeSuperSetComputation is a DynamicItem, so Gerrit may only have one |
| 517 | implementation. |
| 518 | |
Saša Živkov | ec85a07 | 2014-01-28 10:08:25 +0100 | [diff] [blame] | 519 | [[receive-pack]] |
| 520 | == Receive Pack Initializers |
| 521 | |
Dave Borowitz | b8a2bae | 2017-10-03 10:34:28 +0100 | [diff] [blame] | 522 | Plugins may provide ReceivePackInitializer instances, which will be |
| 523 | invoked by Gerrit just before a ReceivePack instance will be used. |
| 524 | Usually, plugins will make use of the setXXX methods on the ReceivePack |
| 525 | to set additional properties on it. |
| 526 | |
| 527 | The interactions with the core Gerrit ReceivePack initialization and |
| 528 | between ReceivePackInitializers can be complex. Please read the |
| 529 | ReceivePack Javadoc and Gerrit AsyncReceiveCommits implementation |
| 530 | carefully. |
Saša Živkov | ec85a07 | 2014-01-28 10:08:25 +0100 | [diff] [blame] | 531 | |
Saša Živkov | 626c731 | 2014-02-24 17:15:08 +0100 | [diff] [blame] | 532 | [[post-receive-hook]] |
| 533 | == Post Receive-Pack Hooks |
| 534 | |
| 535 | Plugins may register PostReceiveHook instances in order to get |
| 536 | notified when JGit successfully receives a pack. This may be useful |
| 537 | for those plugins which would like to monitor changes in Git |
| 538 | repositories. |
| 539 | |
Dave Borowitz | 223580f | 2017-10-03 09:55:51 +0100 | [diff] [blame] | 540 | [[upload-pack]] |
| 541 | == Upload Pack Initializers |
| 542 | |
| 543 | Plugins may provide UploadPackInitializer instances, which will be |
| 544 | invoked by Gerrit just before a UploadPack instance will be used. |
| 545 | Usually, plugins will make use of the setXXX methods on the UploadPack |
| 546 | to set additional properties on it. |
| 547 | |
| 548 | The interactions with the core Gerrit UploadPack initialization and |
| 549 | between UploadPackInitializers can be complex. Please read the |
| 550 | UploadPack Javadoc and Gerrit Upload/UploadFactory implementations |
| 551 | carefully. |
| 552 | |
Hugo Arès | 572d542 | 2014-06-17 14:22:03 -0400 | [diff] [blame] | 553 | [[pre-upload-hook]] |
| 554 | == Pre Upload-Pack Hooks |
| 555 | |
| 556 | Plugins may register PreUploadHook instances in order to get |
| 557 | notified when JGit is about to upload a pack. This may be useful |
| 558 | for those plugins which would like to monitor usage in Git |
| 559 | repositories. |
| 560 | |
Hugo Arès | 41b4c0d | 2016-08-02 15:26:57 -0400 | [diff] [blame] | 561 | [[post-upload-hook]] |
| 562 | == Post Upload-Pack Hooks |
| 563 | |
| 564 | Plugins may register PostUploadHook instances in order to get notified after |
| 565 | JGit is done uploading a pack. |
| 566 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 567 | [[ssh]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 568 | == SSH Commands |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 569 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 570 | Plugins may provide commands that can be accessed through the SSH |
| 571 | interface (extensions do not have this option). |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 572 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 573 | Command implementations must extend the base class SshCommand: |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 574 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 575 | [source,java] |
| 576 | ---- |
| 577 | import com.google.gerrit.sshd.SshCommand; |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 578 | import com.google.gerrit.sshd.CommandMetaData; |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 579 | |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 580 | @CommandMetaData(name="print", description="Print hello command") |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 581 | class PrintHello extends SshCommand { |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 582 | @Override |
| 583 | protected void run() { |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 584 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 585 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 586 | } |
| 587 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 588 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 589 | If no Guice modules are declared in the manifest, SSH commands may |
Edwin Kempin | 948de0f | 2012-07-16 10:34:35 +0200 | [diff] [blame] | 590 | use auto-registration by providing an `@Export` annotation: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 591 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 592 | [source,java] |
| 593 | ---- |
| 594 | import com.google.gerrit.extensions.annotations.Export; |
| 595 | import com.google.gerrit.sshd.SshCommand; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 596 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 597 | @Export("print") |
| 598 | class PrintHello extends SshCommand { |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 599 | @Override |
| 600 | protected void run() { |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 601 | stdout.print("Hello\n"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 602 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 603 | } |
| 604 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 605 | |
| 606 | If explicit registration is being used, a Guice module must be |
| 607 | supplied to register the SSH command and declared in the manifest |
| 608 | with the `Gerrit-SshModule` attribute: |
| 609 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 610 | [source,java] |
| 611 | ---- |
| 612 | import com.google.gerrit.sshd.PluginCommandModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 613 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 614 | class MyCommands extends PluginCommandModule { |
Ian Bull | e1a1220 | 2014-02-16 17:15:42 -0800 | [diff] [blame] | 615 | @Override |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 616 | protected void configureCommands() { |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 617 | command(PrintHello.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 618 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 619 | } |
| 620 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 621 | |
| 622 | For a plugin installed as name `helloworld`, the command implemented |
| 623 | by PrintHello class will be available to users as: |
| 624 | |
| 625 | ---- |
Keunhong Park | a09a6f1 | 2012-07-10 14:45:02 -0600 | [diff] [blame] | 626 | $ ssh -p 29418 review.example.com helloworld print |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 627 | ---- |
| 628 | |
David Ostrovsky | 79c4d89 | 2014-03-15 13:52:46 +0100 | [diff] [blame] | 629 | [[multiple-commands]] |
| 630 | === Multiple Commands bound to one implementation |
| 631 | |
David Ostrovsky | e3172b3 | 2013-10-13 14:19:13 +0200 | [diff] [blame] | 632 | Multiple SSH commands can be bound to the same implementation class. For |
| 633 | example a Gerrit Shell plugin can bind different shell commands to the same |
| 634 | implementation class: |
| 635 | |
| 636 | [source,java] |
| 637 | ---- |
| 638 | public class SshShellModule extends PluginCommandModule { |
| 639 | @Override |
| 640 | protected void configureCommands() { |
| 641 | command("ls").to(ShellCommand.class); |
| 642 | command("ps").to(ShellCommand.class); |
| 643 | [...] |
| 644 | } |
| 645 | } |
| 646 | ---- |
| 647 | |
| 648 | With the possible implementation: |
| 649 | |
| 650 | [source,java] |
| 651 | ---- |
| 652 | public class ShellCommand extends SshCommand { |
| 653 | @Override |
| 654 | protected void run() throws UnloggedFailure { |
| 655 | String cmd = getName().substring(getPluginName().length() + 1); |
| 656 | ProcessBuilder proc = new ProcessBuilder(cmd); |
| 657 | Process cmd = proc.start(); |
| 658 | [...] |
| 659 | } |
| 660 | } |
| 661 | ---- |
| 662 | |
| 663 | And the call: |
| 664 | |
| 665 | ---- |
| 666 | $ ssh -p 29418 review.example.com shell ls |
| 667 | $ ssh -p 29418 review.example.com shell ps |
| 668 | ---- |
| 669 | |
David Ostrovsky | 79c4d89 | 2014-03-15 13:52:46 +0100 | [diff] [blame] | 670 | [[root-level-commands]] |
| 671 | === Root Level Commands |
| 672 | |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 673 | Single command plugins are also supported. In this scenario plugin binds |
| 674 | SSH command to its own name. `SshModule` must inherit from |
| 675 | `SingleCommandPluginModule` class: |
| 676 | |
| 677 | [source,java] |
| 678 | ---- |
| 679 | public class SshModule extends SingleCommandPluginModule { |
| 680 | @Override |
| 681 | protected void configure(LinkedBindingBuilder<Command> b) { |
| 682 | b.to(ShellCommand.class); |
| 683 | } |
| 684 | } |
| 685 | ---- |
| 686 | |
| 687 | 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] | 688 | directory, generic commands can be called without specifying the |
David Ostrovsky | b7d9775 | 2013-11-09 05:23:26 +0100 | [diff] [blame] | 689 | actual SSH command. Note in the example below, that the called commands |
| 690 | `ls` and `ps` was not explicitly bound: |
| 691 | |
| 692 | ---- |
| 693 | $ ssh -p 29418 review.example.com sh ls |
| 694 | $ ssh -p 29418 review.example.com sh ps |
| 695 | ---- |
| 696 | |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 697 | [[search_operators]] |
Edwin Kempin | 4b47977 | 2016-11-14 14:34:33 -0800 | [diff] [blame] | 698 | == Search Operators |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 699 | |
| 700 | Plugins can define new search operators to extend change searching by |
| 701 | implementing the `ChangeQueryBuilder.ChangeOperatorFactory` interface |
| 702 | and registering it to an operator name in the plugin module's |
| 703 | `configure()` method. The search operator name is defined during |
| 704 | registration via the DynamicMap annotation mechanism. The plugin |
| 705 | name will get appended to the annotated name, with an underscore |
| 706 | in between, leading to the final operator name. An example |
| 707 | registration looks like this: |
| 708 | |
| 709 | bind(ChangeOperatorFactory.class) |
| 710 | .annotatedWith(Exports.named("sample")) |
| 711 | .to(SampleOperator.class); |
| 712 | |
| 713 | If this is registered in the `myplugin` plugin, then the resulting |
| 714 | operator will be named `sample_myplugin`. |
| 715 | |
| 716 | The search operator itself is implemented by ensuring that the |
| 717 | `create()` method of the class implementing the |
| 718 | `ChangeQueryBuilder.ChangeOperatorFactory` interface returns a |
| 719 | `Predicate<ChangeData>`. Here is a sample operator factory |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 720 | definition which creates a `MyPredicate`: |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 721 | |
| 722 | [source,java] |
| 723 | ---- |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 724 | public class SampleOperator |
| 725 | implements ChangeQueryBuilder.ChangeOperatorFactory { |
Dave Borowitz | b53581b | 2019-04-08 08:25:32 -0700 | [diff] [blame] | 726 | public static class MyPredicate extends PostFilterPredicate<ChangeData> { |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 727 | ... |
| 728 | } |
| 729 | |
| 730 | @Override |
| 731 | public Predicate<ChangeData> create(ChangeQueryBuilder builder, String value) |
| 732 | throws QueryParseException { |
| 733 | return new MyPredicate(value); |
| 734 | } |
| 735 | } |
| 736 | ---- |
| 737 | |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 738 | [[search_operands]] |
| 739 | === Search Operands === |
| 740 | |
| 741 | Plugins can define new search operands to extend change searching. |
| 742 | Plugin methods implementing search operands (returning a |
| 743 | `Predicate<ChangeData>`), must be defined on a class implementing |
| 744 | one of the `ChangeQueryBuilder.ChangeOperandsFactory` interfaces |
Martin Fick | 08534c9 | 2020-08-05 18:09:41 -0500 | [diff] [blame] | 745 | (.e.g., ChangeQueryBuilder.ChangeHasOperandFactory or |
| 746 | ChangeQueryBuilder.ChangeIsOperandFactory). The specific |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 747 | `ChangeOperandFactory` class must also be bound to the `DynamicSet` from |
| 748 | a module's `configure()` method in the plugin. |
| 749 | |
| 750 | The new operand, when used in a search would appear as: |
| 751 | operatorName:operandName_pluginName |
| 752 | |
| 753 | A sample `ChangeHasOperandFactory` class implementing, and registering, a |
| 754 | new `has:sample_pluginName` operand is shown below: |
| 755 | |
| 756 | ==== |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 757 | public class SampleHasOperand implements ChangeHasOperandFactory { |
| 758 | public static class Module extends AbstractModule { |
| 759 | @Override |
| 760 | protected void configure() { |
| 761 | bind(ChangeHasOperandFactory.class) |
| 762 | .annotatedWith(Exports.named("sample") |
| 763 | .to(SampleHasOperand.class); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | @Override |
| 768 | public Predicate<ChangeData> create(ChangeQueryBuilder builder) |
| 769 | throws QueryParseException { |
| 770 | return new HasSamplePredicate(); |
| 771 | } |
| 772 | ==== |
| 773 | |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 774 | [[command_options]] |
| 775 | === Command Options === |
| 776 | |
| 777 | Plugins can provide additional options for each of the gerrit ssh and the |
| 778 | REST API commands by implementing the DynamicBean interface and registering |
| 779 | it to a command class name in the plugin module's `configure()` method. The |
| 780 | plugin's name will be prepended to the name of each @Option annotation found |
| 781 | on the DynamicBean object provided by the plugin. The example below shows a |
| 782 | plugin that adds an option to log a value from the gerrit 'ban-commits' |
| 783 | ssh command. |
| 784 | |
| 785 | [source, java] |
| 786 | ---- |
| 787 | public class SshModule extends AbstractModule { |
Edwin Kempin | 8860e8e | 2018-05-08 11:19:37 +0200 | [diff] [blame] | 788 | private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 789 | |
| 790 | @Override |
| 791 | protected void configure() { |
| 792 | bind(DynamicOptions.DynamicBean.class) |
| 793 | .annotatedWith(Exports.named( |
| 794 | com.google.gerrit.sshd.commands.BanCommitCommand.class)) |
| 795 | .to(BanOptions.class); |
| 796 | } |
| 797 | |
| 798 | public static class BanOptions implements DynamicOptions.DynamicBean { |
| 799 | @Option(name = "--log", aliases = { "-l" }, usage = "Say Hello in the Log") |
| 800 | private void parse(String arg) { |
Edwin Kempin | 8860e8e | 2018-05-08 11:19:37 +0200 | [diff] [blame] | 801 | logger.atSevere().log("Say Hello in the Log %s", arg); |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | ---- |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 805 | |
Prudhvi Akhil Alahari | 86f0223 | 2020-10-30 23:51:33 +0530 | [diff] [blame] | 806 | To provide additional Guice bindings for options to a command in another classloader, bind a |
| 807 | ModulesClassNamesProvider which provides the name of your Modules needed for your DynamicBean |
| 808 | in the other classLoader. |
| 809 | |
| 810 | Do this by binding to the name of the command you are going to bind to and providing an |
| 811 | Iterable of Module names to instantiate and add to the Injector used to instantiate the |
| 812 | DynamicBean in the other classLoader. This interface supports running LifecycleListeners |
| 813 | which are defined by the Modules being provided. The duration of the lifecycle starts when |
| 814 | a ssh or http request starts and ends when the request completes. |
| 815 | |
| 816 | [source, java] |
| 817 | ---- |
| 818 | bind(DynamicOptions.DynamicBean.class) |
| 819 | .annotatedWith(Exports.named( |
| 820 | "com.google.gerrit.plugins.otherplugin.command")) |
| 821 | .to(MyOptionsModulesClassNamesProvider.class); |
| 822 | |
| 823 | static class MyOptionsModulesClassNamesProvider implements DynamicOptions.ModulesClassNamesProvider { |
| 824 | {@literal @}Override |
| 825 | public String getClassName() { |
| 826 | return "com.googlesource.gerrit.plugins.myplugin.CommandOptions"; |
| 827 | } |
| 828 | {@literal @}Override |
| 829 | public Iterable<String> getModulesClassNames()() { |
| 830 | return "com.googlesource.gerrit.plugins.myplugin.MyOptionsModule"; |
| 831 | } |
| 832 | } |
| 833 | ---- |
| 834 | |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 835 | === Calling Command Options === |
| 836 | |
| 837 | Within an OptionHandler, during the processing of an option, plugins can |
| 838 | provide and call extra parameters on the current command during parsing |
| 839 | simulating as if they had been passed from the command line originally. |
| 840 | |
| 841 | To call additional parameters from within an option handler, instantiate |
| 842 | the com.google.gerrit.util.cli.CmdLineParser.Parameters class with the |
| 843 | existing parameters, and then call callParameters() with the additional |
| 844 | parameters to be parsed. OptionHandlers may optionally pass this class to |
| 845 | other methods which may then both parse/consume more parameters and call |
| 846 | additional parameters. |
| 847 | |
Zac Livingston | 99a1ad1 | 2017-11-21 12:13:29 -0700 | [diff] [blame] | 848 | When calling command options not provided by your plugin, there is always |
| 849 | a risk that the options may not exist, perhaps because the options being |
| 850 | called are to be provided by another plugin, and said plugin is not |
| 851 | currently installed. To protect againt this situation, it is possible to |
| 852 | define an option as being dependent on other options using the |
| 853 | @RequiresOptions() annotation. If the required options are not all not |
| 854 | currently present, then the dependent option will not be available or |
| 855 | visible in the help. |
| 856 | |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 857 | The example below shows a plugin that adds a "--special" option (perhaps |
Zac Livingston | 99a1ad1 | 2017-11-21 12:13:29 -0700 | [diff] [blame] | 858 | for use with the Query command) that calls (and requires) the |
| 859 | "--format json" option. |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 860 | |
| 861 | [source, java] |
| 862 | ---- |
| 863 | public class JsonOutputOptionHandler<T> extends OptionHandler<T> { |
| 864 | protected com.google.gerrit.util.cli.CmdLineParser.MyParser myParser; |
| 865 | |
| 866 | public JsonOutputOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super T> setter) { |
| 867 | super(parser, option, setter); |
| 868 | myParser = (com.google.gerrit.util.cli.CmdLineParser.MyParser) owner; |
| 869 | } |
| 870 | |
| 871 | @Override |
| 872 | public int parseArguments(org.kohsuke.args4j.spi.Parameters params) throws CmdLineException { |
| 873 | new Parameters(params, myParser).callParameters("--format", "json"); |
| 874 | setter.addValue(true); |
| 875 | return 0; // we didn't consume any additional args |
| 876 | } |
| 877 | |
| 878 | @Override |
| 879 | public String getDefaultMetaVariable() { |
| 880 | ... |
| 881 | } |
| 882 | } |
| 883 | |
Zac Livingston | 99a1ad1 | 2017-11-21 12:13:29 -0700 | [diff] [blame] | 884 | @RequiresOptions("--format") |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 885 | @Option( |
| 886 | name = "--special", |
| 887 | usage = "ouptut results using json", |
| 888 | handler = JsonOutputOptionHandler.class |
| 889 | ) |
| 890 | boolean json; |
| 891 | ---- |
| 892 | |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 893 | [[query_attributes]] |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 894 | === Change Attributes === |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 895 | |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 896 | ==== ChangePluginDefinedInfoFactory |
| 897 | |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 898 | Plugins can provide additional attributes to be returned from the Get Change and |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 899 | Query Change APIs by implementing the `ChangePluginDefinedInfoFactory` interface |
| 900 | and adding it to the `DynamicSet` in the plugin module's `configure()` method. |
| 901 | The new attribute(s) will be output under a `plugin` attribute in the change |
| 902 | output. This can be further controlled by registering a class containing @Option |
| 903 | declarations as a `DynamicBean`, annotated with the HTTP/SSH commands on |
| 904 | which the options should be available. |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 905 | |
Dave Borowitz | 4cf5828 | 2019-03-14 08:00:57 -0700 | [diff] [blame] | 906 | The example below shows a plugin that adds two attributes (`exampleName` and |
| 907 | `changeValue`), to the change query output, when the query command is provided |
| 908 | the `--myplugin-name--all` option. |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 909 | |
| 910 | [source, java] |
| 911 | ---- |
| 912 | public class Module extends AbstractModule { |
| 913 | @Override |
| 914 | protected void configure() { |
Dave Borowitz | 10745e1 | 2019-03-13 14:24:00 -0700 | [diff] [blame] | 915 | // Register attribute factory. |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 916 | DynamicSet.bind(binder(), ChangePluginDefinedInfoFactory.class) |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 917 | .to(AttributeFactory.class); |
Dave Borowitz | 10745e1 | 2019-03-13 14:24:00 -0700 | [diff] [blame] | 918 | |
| 919 | // Register options for GET /changes/X/change and /changes/X/detail. |
| 920 | bind(DynamicBean.class) |
| 921 | .annotatedWith(Exports.named(GetChange.class)) |
| 922 | .to(MyChangeOptions.class); |
| 923 | |
| 924 | // Register options for GET /changes/?q=... |
| 925 | bind(DynamicBean.class) |
| 926 | .annotatedWith(Exports.named(QueryChanges.class)) |
| 927 | .to(MyChangeOptions.class); |
| 928 | |
| 929 | // Register options for ssh gerrit query. |
| 930 | bind(DynamicBean.class) |
| 931 | .annotatedWith(Exports.named(Query.class)) |
| 932 | .to(MyChangeOptions.class); |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 936 | public class MyChangeOptions implements DynamicBean { |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 937 | @Option(name = "--all", usage = "Include plugin output") |
| 938 | public boolean all = false; |
| 939 | } |
| 940 | |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 941 | public class AttributeFactory implements ChangePluginDefinedInfoFactory { |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 942 | protected MyChangeOptions options; |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 943 | |
| 944 | public class PluginAttribute extends PluginDefinedInfo { |
| 945 | public String exampleName; |
| 946 | public String changeValue; |
| 947 | |
| 948 | public PluginAttribute(ChangeData c) { |
| 949 | this.exampleName = "Attribute Example"; |
| 950 | this.changeValue = Integer.toString(c.getId().get()); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | @Override |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 955 | public Map<Change.Id, PluginDefinedInfo> createPluginDefinedInfos( |
| 956 | Collection<ChangeData> cds, BeanProvider bp, String plugin) { |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 957 | if (options == null) { |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 958 | options = (MyChangeOptions) bp.getDynamicBean(plugin); |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 959 | } |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 960 | Map<Change.Id, PluginDefinedInfo> out = new HashMap<>(); |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 961 | if (options.all) { |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 962 | cds.forEach(cd -> out.put(cd.getId(), new PluginAttribute(cd))); |
| 963 | return out; |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 964 | } |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 965 | return ImmutableMap.of(); |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | ---- |
| 969 | |
| 970 | Example |
| 971 | ---- |
| 972 | |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 973 | ssh -p 29418 localhost gerrit query --myplugin-name--all "change:1" --format json |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 974 | |
| 975 | Output: |
| 976 | |
| 977 | { |
| 978 | "url" : "http://localhost:8080/1", |
| 979 | "plugins" : [ |
| 980 | { |
| 981 | "name" : "myplugin-name", |
| 982 | "exampleName" : "Attribute Example", |
| 983 | "changeValue" : "1" |
| 984 | } |
| 985 | ], |
| 986 | ... |
| 987 | } |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 988 | |
| 989 | curl http://localhost:8080/changes/1?myplugin-name--all |
| 990 | |
| 991 | Output: |
| 992 | |
| 993 | { |
| 994 | "_number": 1, |
| 995 | ... |
| 996 | "plugins": [ |
| 997 | { |
| 998 | "name": "myplugin-name", |
| 999 | "example_name": "Attribute Example", |
| 1000 | "change_value": "1" |
| 1001 | } |
| 1002 | ], |
| 1003 | ... |
| 1004 | } |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 1005 | ---- |
| 1006 | |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 1007 | Runtime exceptions generated by the implementors of ChangePluginDefinedInfoFactory |
| 1008 | are encapsulated in PluginDefinedInfo objects which are part of SSH/REST query output. |
| 1009 | |
| 1010 | ==== ChangeAttributeFactory |
| 1011 | |
| 1012 | Alternatively, there is also `ChangeAttributeFactory` which takes in one single |
| 1013 | `ChangeData` at a time. `ChangePluginDefinedInfoFactory` should be preferred |
| 1014 | over this as it handles many changes at once which also decreases the round-trip |
| 1015 | time for queries resulting in performance increase for bulk queries. |
| 1016 | |
| 1017 | Implementors of the `ChangePluginDefinedInfoFactory` and `ChangeAttributeFactory` |
| 1018 | interfaces should check whether they need to contribute to the |
| 1019 | link:#change-etag-computation[change ETag computation] to prevent callers using |
| 1020 | ETags from potentially seeing outdated plugin attributes. |
Edwin Kempin | 96915b7 | 2019-07-22 14:57:40 +0200 | [diff] [blame] | 1021 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1022 | [[simple-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1023 | == Simple Configuration in `gerrit.config` |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1024 | |
| 1025 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 1026 | If a plugin needs global configuration, this configuration should be |
| 1027 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 1028 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1029 | This approach of storing the plugin configuration is only suitable for |
| 1030 | plugins that have a simple configuration that only consists of |
| 1031 | key-value pairs. With this approach it is not possible to have |
| 1032 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1033 | configuration need to store their configuration in their |
| 1034 | link:#configuration[own configuration file] where they can make use of |
| 1035 | subsections. On the other hand storing the plugin configuration in a |
| 1036 | 'plugin' subsection in the `gerrit.config` file has the advantage that |
| 1037 | administrators have all configuration parameters in one file, instead |
| 1038 | of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1039 | |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1040 | To avoid conflicts with other plugins, it is recommended that plugins |
| 1041 | only use the `plugin` subsection with their own name. For example the |
| 1042 | `helloworld` plugin should store its configuration in the |
| 1043 | `plugin.helloworld` subsection: |
| 1044 | |
| 1045 | ---- |
| 1046 | [plugin "helloworld"] |
| 1047 | language = Latin |
| 1048 | ---- |
| 1049 | |
Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 1050 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1051 | plugin can easily access its configuration and there is no need for a |
| 1052 | plugin to parse the `gerrit.config` file on its own: |
| 1053 | |
| 1054 | [source,java] |
| 1055 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1056 | @Inject |
| 1057 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1058 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1059 | [...] |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1060 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 1061 | String language = cfg.getFromGerritConfig("helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1062 | .getString("language", "English"); |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1063 | ---- |
| 1064 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1065 | [[configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1066 | == Configuration in own config file |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1067 | |
| 1068 | Plugins can store their configuration in an own configuration file. |
| 1069 | This makes sense if the plugin configuration is rather complex and |
| 1070 | requires the usage of subsections. Plugins that have a simple |
| 1071 | key-value pair configuration can store their configuration in a |
| 1072 | link:#simple-configuration[`plugin` subsection of the `gerrit.config` |
| 1073 | file]. |
| 1074 | |
| 1075 | The plugin configuration file must be named after the plugin and must |
| 1076 | be located in the `etc` folder of the review site. For example a |
| 1077 | configuration file for a `default-reviewer` plugin could look like |
| 1078 | this: |
| 1079 | |
| 1080 | .$site_path/etc/default-reviewer.config |
| 1081 | ---- |
| 1082 | [branch "refs/heads/master"] |
| 1083 | reviewer = Project Owners |
| 1084 | reviewer = john.doe@example.com |
| 1085 | [match "file:^.*\.txt"] |
| 1086 | reviewer = My Info Developers |
| 1087 | ---- |
| 1088 | |
David Pursehouse | 5b47bc4 | 2016-07-22 11:00:25 +0900 | [diff] [blame] | 1089 | Plugins that have sensitive configuration settings can store those settings in |
| 1090 | an own secure configuration file. The plugin's secure configuration file must be |
| 1091 | named after the plugin and must be located in the `etc` folder of the review |
| 1092 | site. For example a secure configuration file for a `default-reviewer` plugin |
| 1093 | could look like this: |
| 1094 | |
| 1095 | .$site_path/etc/default-reviewer.secure.config |
| 1096 | ---- |
| 1097 | [auth] |
| 1098 | password = secret |
| 1099 | ---- |
| 1100 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1101 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 1102 | plugin can easily access its configuration: |
| 1103 | |
| 1104 | [source,java] |
| 1105 | ---- |
| 1106 | @Inject |
| 1107 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 1108 | |
| 1109 | [...] |
| 1110 | |
| 1111 | String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer") |
| 1112 | .getStringList("branch", "refs/heads/master", "reviewer"); |
David Pursehouse | 5b47bc4 | 2016-07-22 11:00:25 +0900 | [diff] [blame] | 1113 | String password = cfg.getGlobalPluginConfig("default-reviewer") |
| 1114 | .getString("auth", null, "password"); |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1115 | ---- |
| 1116 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1117 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1118 | [[simple-project-specific-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1119 | == Simple Project Specific Configuration in `project.config` |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1120 | |
| 1121 | In Gerrit, project specific configuration is stored in the project's |
| 1122 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 1123 | needs configuration on project level (e.g. to enable its functionality |
| 1124 | only for certain projects), this configuration should be stored in a |
| 1125 | `plugin` subsection in the project's `project.config` file. |
| 1126 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1127 | This approach of storing the plugin configuration is only suitable for |
| 1128 | plugins that have a simple configuration that only consists of |
| 1129 | key-value pairs. With this approach it is not possible to have |
| 1130 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1131 | configuration need to store their configuration in their |
| 1132 | link:#project-specific-configuration[own configuration file] where they |
| 1133 | can make use of subsections. On the other hand storing the plugin |
| 1134 | configuration in a 'plugin' subsection in the `project.config` file has |
| 1135 | the advantage that project owners have all configuration parameters in |
| 1136 | one file, instead of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1137 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1138 | To avoid conflicts with other plugins, it is recommended that plugins |
| 1139 | only use the `plugin` subsection with their own name. For example the |
| 1140 | `helloworld` plugin should store its configuration in the |
| 1141 | `plugin.helloworld` subsection: |
| 1142 | |
| 1143 | ---- |
| 1144 | [plugin "helloworld"] |
| 1145 | enabled = true |
| 1146 | ---- |
| 1147 | |
| 1148 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 1149 | plugin can easily access its project specific configuration and there |
| 1150 | is no need for a plugin to parse the `project.config` file on its own: |
| 1151 | |
| 1152 | [source,java] |
| 1153 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1154 | @Inject |
| 1155 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1156 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1157 | [...] |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1158 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 1159 | boolean enabled = cfg.getFromProjectConfig(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1160 | .getBoolean("enabled", false); |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1161 | ---- |
| 1162 | |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1163 | It is also possible to get missing configuration parameters inherited |
| 1164 | from the parent projects: |
| 1165 | |
| 1166 | [source,java] |
| 1167 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1168 | @Inject |
| 1169 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1170 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1171 | [...] |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1172 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 1173 | boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1174 | .getBoolean("enabled", false); |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1175 | ---- |
| 1176 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1177 | Project owners can edit the project configuration by fetching the |
| 1178 | `refs/meta/config` branch, editing the `project.config` file and |
| 1179 | pushing the commit back. |
| 1180 | |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 1181 | Plugin configuration values that are stored in the `project.config` |
| 1182 | file can be exposed in the ProjectInfoScreen to allow project owners |
| 1183 | to see and edit them from the UI. |
| 1184 | |
| 1185 | For this an instance of `ProjectConfigEntry` needs to be bound for each |
| 1186 | parameter. The export name must be a valid Git variable name. The |
| 1187 | variable name is case-insensitive, allows only alphanumeric characters |
| 1188 | and '-', and must start with an alphabetic character. |
| 1189 | |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 1190 | The example below shows how the parameters `plugin.helloworld.enabled` |
| 1191 | and `plugin.helloworld.language` are bound to be editable from the |
David Pursehouse | a1d633b | 2014-05-02 17:21:02 +0900 | [diff] [blame] | 1192 | Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting" |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 1193 | is provided as display name and the default value is set to `true`. |
| 1194 | For the parameter `plugin.helloworld.language` "Preferred Language" |
| 1195 | is provided as display name and "en" is set as default value. |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 1196 | |
| 1197 | [source,java] |
| 1198 | ---- |
| 1199 | class Module extends AbstractModule { |
| 1200 | @Override |
| 1201 | protected void configure() { |
| 1202 | bind(ProjectConfigEntry.class) |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 1203 | .annotatedWith(Exports.named("enabled")) |
| 1204 | .toInstance(new ProjectConfigEntry("Enable Greeting", true)); |
| 1205 | bind(ProjectConfigEntry.class) |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 1206 | .annotatedWith(Exports.named("language")) |
| 1207 | .toInstance(new ProjectConfigEntry("Preferred Language", "en")); |
| 1208 | } |
| 1209 | } |
| 1210 | ---- |
| 1211 | |
Edwin Kempin | b64d397 | 2013-11-17 18:55:48 +0100 | [diff] [blame] | 1212 | By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins |
| 1213 | can be notified when this configuration parameter is updated on a |
| 1214 | project. |
| 1215 | |
Janice Agustin | e5a9d01 | 2015-08-24 09:05:56 -0400 | [diff] [blame] | 1216 | [[configuring-groups]] |
| 1217 | === Referencing groups in `project.config` |
| 1218 | |
| 1219 | Plugins can refer to groups so that when they are renamed, the project |
| 1220 | config will also be updated in this section. The proper format to use is |
Hugo Arès | 532e0a3 | 2017-06-16 09:31:08 -0400 | [diff] [blame] | 1221 | the same as for any other group reference in the `project.config`, as shown below. |
Janice Agustin | e5a9d01 | 2015-08-24 09:05:56 -0400 | [diff] [blame] | 1222 | |
| 1223 | ---- |
Hugo Arès | 532e0a3 | 2017-06-16 09:31:08 -0400 | [diff] [blame] | 1224 | group group_name |
Janice Agustin | e5a9d01 | 2015-08-24 09:05:56 -0400 | [diff] [blame] | 1225 | ---- |
| 1226 | |
Hugo Arès | 532e0a3 | 2017-06-16 09:31:08 -0400 | [diff] [blame] | 1227 | The file `groups` must also contains the mapping of the group name and its UUID, |
| 1228 | refer to link:config-project-config.html#file-groups[file groups] |
| 1229 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1230 | [[project-specific-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1231 | == Project Specific Configuration in own config file |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1232 | |
| 1233 | Plugins can store their project specific configuration in an own |
| 1234 | configuration file in the projects `refs/meta/config` branch. |
| 1235 | This makes sense if the plugins project specific configuration is |
| 1236 | rather complex and requires the usage of subsections. Plugins that |
| 1237 | have a simple key-value pair configuration can store their project |
| 1238 | specific configuration in a link:#simple-project-specific-configuration[ |
| 1239 | `plugin` subsection of the `project.config` file]. |
| 1240 | |
| 1241 | The plugin configuration file in the `refs/meta/config` branch must be |
| 1242 | named after the plugin. For example a configuration file for a |
| 1243 | `default-reviewer` plugin could look like this: |
| 1244 | |
| 1245 | .default-reviewer.config |
| 1246 | ---- |
| 1247 | [branch "refs/heads/master"] |
| 1248 | reviewer = Project Owners |
| 1249 | reviewer = john.doe@example.com |
| 1250 | [match "file:^.*\.txt"] |
| 1251 | reviewer = My Info Developers |
| 1252 | ---- |
| 1253 | |
| 1254 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 1255 | plugin can easily access its project specific configuration: |
| 1256 | |
| 1257 | [source,java] |
| 1258 | ---- |
| 1259 | @Inject |
| 1260 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 1261 | |
| 1262 | [...] |
| 1263 | |
| 1264 | String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer") |
| 1265 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 1266 | ---- |
| 1267 | |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 1268 | It is also possible to get missing configuration parameters inherited |
| 1269 | from the parent projects: |
| 1270 | |
| 1271 | [source,java] |
| 1272 | ---- |
| 1273 | @Inject |
| 1274 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 1275 | |
| 1276 | [...] |
| 1277 | |
David Ostrovsky | 468e4c3 | 2014-03-22 06:05:35 -0700 | [diff] [blame] | 1278 | String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer") |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 1279 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 1280 | ---- |
| 1281 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1282 | Project owners can edit the project configuration by fetching the |
| 1283 | `refs/meta/config` branch, editing the `<plugin-name>.config` file and |
| 1284 | pushing the commit back. |
| 1285 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1286 | == React on changes in project configuration |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1287 | |
| 1288 | If a plugin wants to react on changes in the project configuration, it |
| 1289 | can implement a `GitReferenceUpdatedListener` and filter on events for |
| 1290 | the `refs/meta/config` branch: |
| 1291 | |
| 1292 | [source,java] |
| 1293 | ---- |
| 1294 | public class MyListener implements GitReferenceUpdatedListener { |
| 1295 | |
| 1296 | private final MetaDataUpdate.Server metaDataUpdateFactory; |
| 1297 | |
| 1298 | @Inject |
| 1299 | MyListener(MetaDataUpdate.Server metaDataUpdateFactory) { |
| 1300 | this.metaDataUpdateFactory = metaDataUpdateFactory; |
| 1301 | } |
| 1302 | |
| 1303 | @Override |
| 1304 | public void onGitReferenceUpdated(Event event) { |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1305 | if (event.getRefName().equals(RefNames.REFS_CONFIG)) { |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1306 | Project.NameKey p = new Project.NameKey(event.getProjectName()); |
| 1307 | try { |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1308 | ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId()); |
| 1309 | ProjectConfig newCfg = parseConfig(p, event.getNewObjectId()); |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1310 | |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1311 | if (oldCfg != null && newCfg != null |
| 1312 | && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) { |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1313 | // submit type has changed |
| 1314 | ... |
| 1315 | } |
| 1316 | } catch (IOException | ConfigInvalidException e) { |
| 1317 | ... |
| 1318 | } |
| 1319 | } |
| 1320 | } |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1321 | |
| 1322 | private ProjectConfig parseConfig(Project.NameKey p, String idStr) |
| 1323 | throws IOException, ConfigInvalidException, RepositoryNotFoundException { |
| 1324 | ObjectId id = ObjectId.fromString(idStr); |
| 1325 | if (ObjectId.zeroId().equals(id)) { |
| 1326 | return null; |
| 1327 | } |
| 1328 | return ProjectConfig.read(metaDataUpdateFactory.create(p), id); |
| 1329 | } |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1330 | } |
| 1331 | ---- |
| 1332 | |
Fabio Ponciroli | 214b896 | 2020-05-07 13:22:00 +0200 | [diff] [blame] | 1333 | == Trace Event origin |
| 1334 | |
| 1335 | When plugins are installed in a multi-master setups it can be useful to know |
| 1336 | the Gerrit `instanceId` of the server that has generated an Event. |
| 1337 | |
| 1338 | E.g. A plugin that sends an instance message for every comment on a change may |
| 1339 | want to react only if the event is generated on the local Gerrit master, for |
| 1340 | avoiding duplicating the notifications. |
| 1341 | |
| 1342 | If link:config-gerrit.html[instanceId] is set, each Event will contain its |
| 1343 | origin in the `instanceId` field. |
| 1344 | |
| 1345 | Here and example of ref-updated JSON event payload with `instanceId`: |
| 1346 | |
| 1347 | [source,json] |
| 1348 | --- |
| 1349 | { |
| 1350 | "submitter": { |
| 1351 | "name": "Administrator", |
| 1352 | "email": "admin@example.com", |
| 1353 | "username": "admin" |
| 1354 | }, |
| 1355 | "refUpdate": { |
| 1356 | "oldRev": "a69fc95c7aad5ad41c618d31548b8af835d2959a", |
| 1357 | "newRev": "31da6556d638a74e5370b62f83e8007f94abb7c6", |
| 1358 | "refName": "refs/changes/01/1/meta", |
| 1359 | "project": "test" |
| 1360 | }, |
| 1361 | "type": "ref-updated", |
| 1362 | "eventCreatedOn": 1588849085, |
| 1363 | "instanceId": "instance1" |
| 1364 | } |
| 1365 | --- |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1366 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1367 | [[capabilities]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1368 | == Plugin Owned Capabilities |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1369 | |
| 1370 | Plugins may provide their own capabilities and restrict usage of SSH |
Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 1371 | commands or `UiAction` to the users who are granted those capabilities. |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1372 | |
| 1373 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 1374 | abstract class: |
| 1375 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1376 | [source,java] |
| 1377 | ---- |
| 1378 | public class PrintHelloCapability extends CapabilityDefinition { |
| 1379 | @Override |
| 1380 | public String getDescription() { |
| 1381 | return "Print Hello"; |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1382 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1383 | } |
| 1384 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1385 | |
Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 1386 | If no Guice modules are declared in the manifest, capability may |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1387 | use auto-registration by providing an `@Export` annotation: |
| 1388 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1389 | [source,java] |
| 1390 | ---- |
| 1391 | @Export("printHello") |
| 1392 | public class PrintHelloCapability extends CapabilityDefinition { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1393 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1394 | } |
| 1395 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1396 | |
| 1397 | Otherwise the capability must be bound in a plugin module: |
| 1398 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1399 | [source,java] |
| 1400 | ---- |
| 1401 | public class HelloWorldModule extends AbstractModule { |
| 1402 | @Override |
| 1403 | protected void configure() { |
| 1404 | bind(CapabilityDefinition.class) |
| 1405 | .annotatedWith(Exports.named("printHello")) |
| 1406 | .to(PrintHelloCapability.class); |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1407 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1408 | } |
| 1409 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1410 | |
| 1411 | 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] | 1412 | 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] | 1413 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 1414 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1415 | [source,java] |
| 1416 | ---- |
| 1417 | @RequiresCapability("printHello") |
| 1418 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 1419 | public final class PrintHelloWorldCommand extends SshCommand { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1420 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1421 | } |
| 1422 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1423 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1424 | Or with `UiAction`: |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1425 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1426 | [source,java] |
| 1427 | ---- |
| 1428 | @RequiresCapability("printHello") |
| 1429 | public class SayHelloAction extends UiAction<RevisionResource> |
| 1430 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1431 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1432 | } |
| 1433 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1434 | |
| 1435 | Capability scope was introduced to differentiate between plugin-owned |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 1436 | capabilities and core capabilities. Per default the scope of the |
| 1437 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 1438 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1439 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 1440 | capability is assumed to be that plugin. |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 1441 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1442 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 1443 | (and thus is outside of a plugin) the scope is the core server and will use |
| 1444 | the `GlobalCapability` known to Gerrit Code Review server. |
| 1445 | |
| 1446 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 1447 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 1448 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1449 | [source,java] |
| 1450 | ---- |
| 1451 | @RequiresCapability(value = "administrateServer", scope = |
| 1452 | CapabilityScope.CORE) |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1453 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1454 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1455 | |
Edwin Kempin | 79ec493 | 2020-12-01 15:51:29 +0100 | [diff] [blame^] | 1456 | [post_review_extensions] |
| 1457 | == Post Review Extensions |
| 1458 | |
| 1459 | By implementing the `com.google.gerrit.server.restapi.change.OnPostReview` |
| 1460 | interface plugins can extend the change message that is being posted when the |
| 1461 | [post review](rest-api-changes.html#set-review) REST endpoint is invoked. |
| 1462 | |
| 1463 | This is useful if certain approvals have a special meaning (e.g. custom logic |
| 1464 | that is implemented in Prolog submit rules, signal for triggering an action |
| 1465 | like running CI etc.), as it allows the plugin to tell users about this meaning |
| 1466 | in the change message. This makes the effect of a given approval more |
| 1467 | transparent to the user. |
| 1468 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1469 | [[ui_extension]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1470 | == UI Extension |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1471 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1472 | [[panels]] |
| 1473 | === Panels |
| 1474 | |
David Ostrovsky | 7163dac | 2017-07-29 06:49:38 +0200 | [diff] [blame] | 1475 | UI plugins can contribute panels to Gerrit screens. |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1476 | |
| 1477 | Gerrit screens define extension points where plugins can add GWT |
| 1478 | panels with custom controls: |
| 1479 | |
| 1480 | * Change Screen: |
Edwin Kempin | 2a8c515 | 2015-07-08 14:28:57 +0200 | [diff] [blame] | 1481 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER`: |
| 1482 | + |
| 1483 | Panel will be shown in the header bar to the right of the change |
| 1484 | status. |
| 1485 | |
Edwin Kempin | 745021e | 2015-07-09 13:09:44 +0200 | [diff] [blame] | 1486 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_BUTTONS`: |
| 1487 | + |
| 1488 | Panel will be shown in the header bar on the right side of the buttons. |
| 1489 | |
Edwin Kempin | cbc9525 | 2015-07-09 11:37:53 +0200 | [diff] [blame] | 1490 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HEADER_RIGHT_OF_POP_DOWNS`: |
| 1491 | + |
| 1492 | Panel will be shown in the header bar on the right side of the pop down |
| 1493 | buttons. |
| 1494 | |
Khai Do | 675afc0 | 2016-07-28 16:30:37 -0700 | [diff] [blame] | 1495 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK`: |
| 1496 | + |
| 1497 | Panel will be shown below the commit info block. |
| 1498 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1499 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK`: |
| 1500 | + |
| 1501 | Panel will be shown below the change info block. |
| 1502 | |
Khai Do | 76c830c | 2016-07-28 16:35:45 -0700 | [diff] [blame] | 1503 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_RELATED_INFO_BLOCK`: |
| 1504 | + |
| 1505 | Panel will be shown below the related info block. |
| 1506 | |
Khai Do | 83940ba | 2016-09-20 15:15:45 +0200 | [diff] [blame] | 1507 | ** `GerritUiExtensionPoint.CHANGE_SCREEN_HISTORY_RIGHT_OF_BUTTONS`: |
| 1508 | + |
| 1509 | Panel will be shown in the history bar on the right side of the buttons. |
| 1510 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1511 | ** The following parameters are provided: |
Edwin Kempin | 5d683cc | 2015-07-10 15:47:14 +0200 | [diff] [blame] | 1512 | *** `GerritUiExtensionPoint.Key.CHANGE_INFO`: |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1513 | + |
Edwin Kempin | 5d683cc | 2015-07-10 15:47:14 +0200 | [diff] [blame] | 1514 | The link:rest-api-changes.html#change-info[ChangeInfo] entity for the |
| 1515 | current change. |
David Ostrovsky | 916ae0c | 2016-03-15 17:05:41 +0100 | [diff] [blame] | 1516 | + |
| 1517 | The link:rest-api-changes.html#revision-info[RevisionInfo] entity for |
| 1518 | the current patch set. |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1519 | |
Edwin Kempin | 88b947a | 2015-07-08 09:03:56 +0200 | [diff] [blame] | 1520 | * Project Info Screen: |
| 1521 | ** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_TOP`: |
| 1522 | + |
| 1523 | Panel will be shown at the top of the screen. |
| 1524 | |
| 1525 | ** `GerritUiExtensionPoint.PROJECT_INFO_SCREEN_BOTTOM`: |
| 1526 | + |
| 1527 | Panel will be shown at the bottom of the screen. |
| 1528 | |
| 1529 | ** The following parameters are provided: |
| 1530 | *** `GerritUiExtensionPoint.Key.PROJECT_NAME`: |
| 1531 | + |
| 1532 | The name of the project. |
| 1533 | |
Edwin Kempin | 241d9db | 2015-07-08 13:53:50 +0200 | [diff] [blame] | 1534 | * User Password Screen: |
| 1535 | ** `GerritUiExtensionPoint.PASSWORD_SCREEN_BOTTOM`: |
| 1536 | + |
| 1537 | Panel will be shown at the bottom of the screen. |
| 1538 | |
Edwin Kempin | 30c6f47 | 2015-07-09 14:27:52 +0200 | [diff] [blame] | 1539 | ** The following parameters are provided: |
| 1540 | *** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`: |
| 1541 | + |
| 1542 | The link:rest-api-accounts.html#account-info[AccountInfo] entity for |
| 1543 | the current user. |
| 1544 | |
Edwin Kempin | 1cd95f9 | 2015-07-14 08:27:20 +0200 | [diff] [blame] | 1545 | * User Preferences Screen: |
| 1546 | ** `GerritUiExtensionPoint.PREFERENCES_SCREEN_BOTTOM`: |
| 1547 | + |
| 1548 | Panel will be shown at the bottom of the screen. |
| 1549 | |
| 1550 | ** The following parameters are provided: |
| 1551 | *** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`: |
| 1552 | + |
| 1553 | The link:rest-api-accounts.html#account-info[AccountInfo] entity for |
| 1554 | the current user. |
| 1555 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1556 | * User Profile Screen: |
| 1557 | ** `GerritUiExtensionPoint.PROFILE_SCREEN_BOTTOM`: |
| 1558 | + |
| 1559 | Panel will be shown at the bottom of the screen below the grid with the |
| 1560 | profile data. |
| 1561 | |
Edwin Kempin | 30c6f47 | 2015-07-09 14:27:52 +0200 | [diff] [blame] | 1562 | ** The following parameters are provided: |
| 1563 | *** `GerritUiExtensionPoint.Key.ACCOUNT_INFO`: |
| 1564 | + |
| 1565 | The link:rest-api-accounts.html#account-info[AccountInfo] entity for |
| 1566 | the current user. |
| 1567 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1568 | Example panel: |
| 1569 | [source,java] |
| 1570 | ---- |
| 1571 | public class MyPlugin extends PluginEntryPoint { |
| 1572 | @Override |
| 1573 | public void onPluginLoad() { |
| 1574 | Plugin.get().panel(GerritUiExtensionPoint.CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK, |
Zac Livingston | e7f3d1a | 2017-03-01 12:47:37 -0700 | [diff] [blame] | 1575 | "my_panel_name", |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1576 | new Panel.EntryPoint() { |
| 1577 | @Override |
| 1578 | public void onLoad(Panel panel) { |
| 1579 | panel.setWidget(new InlineLabel("My Panel for change " |
| 1580 | + panel.getInt(GerritUiExtensionPoint.Key.CHANGE_ID, -1)); |
| 1581 | } |
| 1582 | }); |
| 1583 | } |
| 1584 | } |
| 1585 | ---- |
| 1586 | |
Zac Livingston | e7f3d1a | 2017-03-01 12:47:37 -0700 | [diff] [blame] | 1587 | Change Screen panel ordering may be specified in the |
| 1588 | project config. Values may be either "plugin name" or |
| 1589 | "plugin name"."panel name". |
| 1590 | Panels not specified in the config will be added |
| 1591 | to the end in load order. Panels specified in the config that |
| 1592 | are not found will be ignored. |
| 1593 | |
| 1594 | Example config: |
| 1595 | ---- |
| 1596 | [extension-panels "CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK"] |
| 1597 | panel = helloworld.change_id |
| 1598 | panel = myotherplugin |
| 1599 | panel = myplugin.my_panel_name |
| 1600 | ---- |
| 1601 | |
| 1602 | |
| 1603 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1604 | [[actions]] |
| 1605 | === Actions |
| 1606 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1607 | Plugins can contribute UI actions on core Gerrit pages. This is useful |
| 1608 | for workflow customization or exposing plugin functionality through the |
| 1609 | UI in addition to SSH commands and the REST API. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1610 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1611 | For instance a plugin to integrate Jira with Gerrit changes may |
| 1612 | contribute a "File bug" button to allow filing a bug from the change |
| 1613 | page or plugins to integrate continuous integration systems may |
| 1614 | contribute a "Schedule" button to allow a CI build to be scheduled |
| 1615 | manually from the patch set panel. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1616 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1617 | Two different places on core Gerrit pages are supported: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1618 | |
| 1619 | * Change screen |
| 1620 | * Project info screen |
| 1621 | |
| 1622 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 1623 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1624 | [source,java] |
| 1625 | ---- |
| 1626 | @RequiresCapability("printHello") |
| 1627 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 1628 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 1629 | static class Input { |
| 1630 | boolean french; |
| 1631 | String message; |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1632 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1633 | |
| 1634 | private Provider<CurrentUser> user; |
| 1635 | |
| 1636 | @Inject |
| 1637 | HelloWorldAction(Provider<CurrentUser> user) { |
| 1638 | this.user = user; |
| 1639 | } |
| 1640 | |
| 1641 | @Override |
| 1642 | public String apply(RevisionResource rev, Input input) { |
| 1643 | final String greeting = input.french |
| 1644 | ? "Bonjour" |
| 1645 | : "Hello"; |
| 1646 | return String.format("%s %s from change %s, patch set %d!", |
| 1647 | greeting, |
| 1648 | Strings.isNullOrEmpty(input.message) |
| 1649 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 1650 | : input.message, |
| 1651 | rev.getChange().getId().toString(), |
| 1652 | rev.getPatchSet().getPatchSetId()); |
| 1653 | } |
| 1654 | |
| 1655 | @Override |
| 1656 | public Description getDescription( |
| 1657 | RevisionResource resource) { |
| 1658 | return new Description() |
| 1659 | .setLabel("Say hello") |
| 1660 | .setTitle("Say hello in different languages"); |
| 1661 | } |
| 1662 | } |
| 1663 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1664 | |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1665 | Sometimes plugins may want to be able to change the state of a patch set or |
| 1666 | change in the `UiAction.apply()` method and reflect these changes on the core |
| 1667 | UI. For example a buildbot plugin which exposes a 'Schedule' button on the |
| 1668 | patch set panel may want to disable that button after the build was scheduled |
| 1669 | and update the tooltip of that button. But because of Gerrit's caching |
| 1670 | strategy the following must be taken into consideration. |
| 1671 | |
| 1672 | The browser is allowed to cache the `UiAction` information until something on |
| 1673 | the change is modified. More accurately the change row needs to be modified in |
| 1674 | the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or |
| 1675 | the +refs/meta/config+ of the project or any parents needs to change to a new |
| 1676 | SHA-1. The ETag SHA-1 computation code can be found in the |
| 1677 | `ChangeResource.getETag()` method. |
| 1678 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1679 | 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] | 1680 | |
| 1681 | [source,java] |
| 1682 | ---- |
| 1683 | @Override |
| 1684 | public Object apply(RevisionResource rcrs, Input in) { |
| 1685 | // schedule a build |
| 1686 | [...] |
| 1687 | // update change |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1688 | try (BatchUpdate bu = batchUpdateFactory.create( |
Dave Borowitz | ada289c | 2018-12-18 13:24:14 -0800 | [diff] [blame] | 1689 | project.getNameKey(), user, TimeUtil.nowTs())) { |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1690 | bu.addOp(change.getId(), new BatchUpdate.Op() { |
| 1691 | @Override |
Dave Borowitz | b91cf22 | 2017-03-10 13:11:59 -0500 | [diff] [blame] | 1692 | public boolean updateChange(ChangeContext ctx) { |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1693 | return true; |
| 1694 | } |
| 1695 | }); |
| 1696 | bu.execute(); |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1697 | } |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1698 | [...] |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1699 | } |
| 1700 | ---- |
| 1701 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1702 | `UiAction` must be bound in a plugin module: |
| 1703 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1704 | [source,java] |
| 1705 | ---- |
| 1706 | public class Module extends AbstractModule { |
| 1707 | @Override |
| 1708 | protected void configure() { |
| 1709 | install(new RestApiModule() { |
| 1710 | @Override |
| 1711 | protected void configure() { |
| 1712 | post(REVISION_KIND, "say-hello") |
| 1713 | .to(HelloWorldAction.class); |
| 1714 | } |
| 1715 | }); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1716 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1717 | } |
| 1718 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1719 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1720 | The module above must be declared in the `pom.xml` for Maven driven |
| 1721 | plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1722 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1723 | [source,xml] |
| 1724 | ---- |
| 1725 | <manifestEntries> |
| 1726 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 1727 | </manifestEntries> |
| 1728 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1729 | |
David Ostrovsky | fdbfcad | 2016-11-15 06:35:29 -0800 | [diff] [blame] | 1730 | or in the `BUILD` configuration file for Bazel driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1731 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1732 | [source,python] |
| 1733 | ---- |
| 1734 | manifest_entries = [ |
| 1735 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 1736 | ] |
| 1737 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1738 | |
| 1739 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 1740 | combined with the JavaScript API. This would display a small popup near the |
| 1741 | activation button to gather additional input from the user. The JS file is |
| 1742 | typically put in the `static` folder within the plugin's directory: |
| 1743 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1744 | [source,javascript] |
| 1745 | ---- |
| 1746 | Gerrit.install(function(self) { |
| 1747 | function onSayHello(c) { |
| 1748 | var f = c.textfield(); |
| 1749 | var t = c.checkbox(); |
| 1750 | var b = c.button('Say hello', {onclick: function(){ |
| 1751 | c.call( |
| 1752 | {message: f.value, french: t.checked}, |
| 1753 | function(r) { |
| 1754 | c.hide(); |
| 1755 | window.alert(r); |
| 1756 | c.refresh(); |
| 1757 | }); |
| 1758 | }}); |
| 1759 | c.popup(c.div( |
| 1760 | c.prependLabel('Greeting message', f), |
| 1761 | c.br(), |
| 1762 | c.label(t, 'french'), |
| 1763 | c.br(), |
| 1764 | b)); |
| 1765 | f.focus(); |
| 1766 | } |
| 1767 | self.onAction('revision', 'say-hello', onSayHello); |
| 1768 | }); |
| 1769 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1770 | |
| 1771 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 1772 | an HTTP Module: |
| 1773 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1774 | [source,java] |
| 1775 | ---- |
Dave Borowitz | 20f4e8a | 2019-03-13 14:19:15 -0700 | [diff] [blame] | 1776 | public class HttpModule extends ServletModule { |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1777 | @Override |
| 1778 | protected void configureServlets() { |
| 1779 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1780 | .toInstance(new JavaScriptPlugin("hello.js")); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1781 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1782 | } |
| 1783 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1784 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1785 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1786 | driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1787 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1788 | [source,xml] |
| 1789 | ---- |
| 1790 | <manifestEntries> |
| 1791 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 1792 | </manifestEntries> |
| 1793 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1794 | |
David Ostrovsky | fdbfcad | 2016-11-15 06:35:29 -0800 | [diff] [blame] | 1795 | or in the `BUILD` configuration file for Bazel driven plugins |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1796 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1797 | [source,python] |
| 1798 | ---- |
| 1799 | manifest_entries = [ |
| 1800 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 1801 | ] |
| 1802 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1803 | |
| 1804 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 1805 | capability check is done during the `UiAction` gathering, so the plugin author |
| 1806 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 1807 | case. |
| 1808 | |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 1809 | The following prerequisites must be met, to satisfy the capability check: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1810 | |
| 1811 | * user is authenticated |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1812 | * 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] | 1813 | * user is a member of a group which has the required capability |
| 1814 | |
| 1815 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 1816 | combined with JavaScript API (its own JavaScript function is provided), |
| 1817 | then a popup dialog is normally opened to gather additional user input. |
| 1818 | A new button is placed on the popup dialog to actually send the request. |
| 1819 | |
| 1820 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 1821 | can be accessed from any REST client, i. e.: |
| 1822 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 1823 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1824 | curl -X POST -H "Content-Type: application/json" \ |
| 1825 | -d '{message: "François", french: true}' \ |
Han-Wen Nienhuys | 84d830b | 2017-02-15 16:36:04 +0100 | [diff] [blame] | 1826 | --user joe:secret \ |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1827 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
| 1828 | "Bonjour François from change 1, patch set 1!" |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 1829 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1830 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1831 | 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] | 1832 | particularly useful for `DELETE` requests: |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1833 | |
| 1834 | [source,java] |
| 1835 | ---- |
| 1836 | public class Module extends AbstractModule { |
| 1837 | @Override |
| 1838 | protected void configure() { |
| 1839 | install(new RestApiModule() { |
| 1840 | @Override |
| 1841 | protected void configure() { |
| 1842 | delete(PROJECT_KIND) |
| 1843 | .to(DeleteProject.class); |
| 1844 | } |
| 1845 | }); |
| 1846 | } |
| 1847 | } |
| 1848 | ---- |
| 1849 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1850 | For a `UiAction` bound this way, a JS API function can be provided. |
| 1851 | |
| 1852 | Currently only one restriction exists: per plugin only one `UiAction` |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1853 | can be bound per resource without view name. To define a JS function |
| 1854 | for the `UiAction`, "/" must be used as the name: |
| 1855 | |
| 1856 | [source,javascript] |
| 1857 | ---- |
| 1858 | Gerrit.install(function(self) { |
| 1859 | function onDeleteProject(c) { |
| 1860 | [...] |
| 1861 | } |
| 1862 | self.onAction('project', '/', onDeleteProject); |
| 1863 | }); |
| 1864 | ---- |
| 1865 | |
Dave Borowitz | f1790ce | 2016-11-14 12:26:12 -0800 | [diff] [blame] | 1866 | |
| 1867 | [[action-visitor]] |
| 1868 | === Action Visitors |
| 1869 | |
| 1870 | In addition to providing new actions, plugins can have fine-grained control |
| 1871 | over the link:rest-api-changes.html#action-info[ActionInfo] map, modifying or |
| 1872 | removing existing actions, including those contributed by core. |
| 1873 | |
| 1874 | Visitors are provided the link:rest-api-changes.html#action-info[ActionInfo], |
| 1875 | which is mutable, along with copies of the |
| 1876 | link:rest-api-changes.html#change-info[ChangeInfo] and |
| 1877 | link:rest-api-changes.html#revision-info[RevisionInfo]. They can modify the |
| 1878 | action, or return `false` to exclude it from the resulting map. |
| 1879 | |
| 1880 | These operations only affect the action buttons that are displayed in the UI; |
| 1881 | the underlying REST API endpoints are not affected. Multiple plugins may |
| 1882 | implement the visitor interface, but the order in which they are run is |
| 1883 | undefined. |
| 1884 | |
| 1885 | For example, to exclude "Cherry-Pick" only from certain projects, and rename |
| 1886 | "Abandon": |
| 1887 | |
| 1888 | [source,java] |
| 1889 | ---- |
| 1890 | public class MyActionVisitor implements ActionVisitor { |
| 1891 | @Override |
| 1892 | public boolean visit(String name, ActionInfo actionInfo, |
| 1893 | ChangeInfo changeInfo) { |
| 1894 | if (name.equals("abandon")) { |
| 1895 | actionInfo.label = "Drop"; |
| 1896 | } |
| 1897 | return true; |
| 1898 | } |
| 1899 | |
| 1900 | @Override |
| 1901 | public boolean visit(String name, ActionInfo actionInfo, |
| 1902 | ChangeInfo changeInfo, RevisionInfo revisionInfo) { |
| 1903 | if (project.startsWith("some-team/") && name.equals("cherrypick")) { |
| 1904 | return false; |
| 1905 | } |
| 1906 | return true; |
| 1907 | } |
| 1908 | } |
| 1909 | ---- |
| 1910 | |
| 1911 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1912 | [[top-menu-extensions]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1913 | == Top Menu Extensions |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1914 | |
| 1915 | Plugins can contribute items to Gerrit's top menu. |
| 1916 | |
| 1917 | A single top menu extension can have multiple elements and will be put as |
| 1918 | the last element in Gerrit's top menu. |
| 1919 | |
| 1920 | Plugins define the top menu entries by implementing `TopMenu` interface: |
| 1921 | |
| 1922 | [source,java] |
| 1923 | ---- |
| 1924 | public class MyTopMenuExtension implements TopMenu { |
| 1925 | |
| 1926 | @Override |
| 1927 | public List<MenuEntry> getEntries() { |
| 1928 | return Lists.newArrayList( |
| 1929 | new MenuEntry("Top Menu Entry", Lists.newArrayList( |
| 1930 | new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1931 | } |
| 1932 | } |
| 1933 | ---- |
| 1934 | |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1935 | Plugins can also add additional menu items to Gerrit's top menu entries |
| 1936 | by defining a `MenuEntry` that has the same name as a Gerrit top menu |
| 1937 | entry: |
| 1938 | |
| 1939 | [source,java] |
| 1940 | ---- |
| 1941 | public class MyTopMenuExtension implements TopMenu { |
| 1942 | |
| 1943 | @Override |
| 1944 | public List<MenuEntry> getEntries() { |
| 1945 | return Lists.newArrayList( |
Dariusz Luksza | 2d3afab | 2013-10-01 11:07:13 +0200 | [diff] [blame] | 1946 | new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList( |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1947 | new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/")))); |
| 1948 | } |
| 1949 | } |
| 1950 | ---- |
| 1951 | |
Dariusz Luksza | e8de74f | 2014-06-04 19:39:20 +0200 | [diff] [blame] | 1952 | `MenuItems` that are bound for the `MenuEntry` with the name |
| 1953 | `GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder |
| 1954 | which is automatically replaced by the actual project name. |
| 1955 | |
| 1956 | E.g. plugins may register an link:#http[HTTP Servlet] to handle project |
| 1957 | specific requests and add an menu item for this: |
| 1958 | |
| 1959 | [source,java] |
| 1960 | --- |
| 1961 | new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}"); |
| 1962 | --- |
| 1963 | |
| 1964 | This also enables plugins to provide menu items for project aware |
| 1965 | screens: |
| 1966 | |
| 1967 | [source,java] |
| 1968 | --- |
| 1969 | new MenuItem("My Screen", "/x/my-screen/for/${projectName}"); |
| 1970 | --- |
| 1971 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1972 | If no Guice modules are declared in the manifest, the top menu extension may use |
| 1973 | auto-registration by providing an `@Listen` annotation: |
| 1974 | |
| 1975 | [source,java] |
| 1976 | ---- |
| 1977 | @Listen |
| 1978 | public class MyTopMenuExtension implements TopMenu { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1979 | [...] |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1980 | } |
| 1981 | ---- |
| 1982 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1983 | Otherwise the top menu extension must be bound in the plugin module used |
| 1984 | for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF): |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1985 | |
| 1986 | [source,java] |
| 1987 | ---- |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1988 | package com.googlesource.gerrit.plugins.helloworld; |
| 1989 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1990 | public class HelloWorldModule extends AbstractModule { |
| 1991 | @Override |
| 1992 | protected void configure() { |
| 1993 | DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class); |
| 1994 | } |
| 1995 | } |
| 1996 | ---- |
| 1997 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1998 | [source,manifest] |
| 1999 | ---- |
| 2000 | Gerrit-ApiType: plugin |
| 2001 | Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule |
| 2002 | ---- |
| 2003 | |
Edwin Kempin | b2e926a | 2013-11-11 16:38:30 +0100 | [diff] [blame] | 2004 | It is also possible to show some menu entries only if the user has a |
| 2005 | certain capability: |
| 2006 | |
| 2007 | [source,java] |
| 2008 | ---- |
| 2009 | public class MyTopMenuExtension implements TopMenu { |
| 2010 | private final String pluginName; |
| 2011 | private final Provider<CurrentUser> userProvider; |
| 2012 | private final List<MenuEntry> menuEntries; |
| 2013 | |
| 2014 | @Inject |
| 2015 | public MyTopMenuExtension(@PluginName String pluginName, |
| 2016 | Provider<CurrentUser> userProvider) { |
| 2017 | this.pluginName = pluginName; |
| 2018 | this.userProvider = userProvider; |
| 2019 | menuEntries = new ArrayList<TopMenu.MenuEntry>(); |
| 2020 | |
| 2021 | // add menu entry that is only visible to users with a certain capability |
| 2022 | if (canSeeMenuEntry()) { |
| 2023 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 2024 | .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 2025 | } |
| 2026 | |
| 2027 | // add menu entry that is visible to all users (even anonymous users) |
| 2028 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 2029 | .singletonList(new MenuItem("Documentation", "/plugins/myplugin/")))); |
| 2030 | } |
| 2031 | |
| 2032 | private boolean canSeeMenuEntry() { |
| 2033 | if (userProvider.get().isIdentifiedUser()) { |
| 2034 | CapabilityControl ctl = userProvider.get().getCapabilities(); |
| 2035 | return ctl.canPerform(pluginName + "-" + MyCapability.ID) |
| 2036 | || ctl.canAdministrateServer(); |
| 2037 | } else { |
| 2038 | return false; |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | @Override |
| 2043 | public List<MenuEntry> getEntries() { |
| 2044 | return menuEntries; |
| 2045 | } |
| 2046 | } |
| 2047 | ---- |
| 2048 | |
Dave Borowitz | f1790ce | 2016-11-14 12:26:12 -0800 | [diff] [blame] | 2049 | |
Edwin Kempin | 289f1a0 | 2014-02-04 16:08:25 +0100 | [diff] [blame] | 2050 | [[settings-screen]] |
| 2051 | == Plugin Settings Screen |
| 2052 | |
| 2053 | If a plugin implements a screen for administrating its settings that is |
| 2054 | available under "#/x/<plugin-name>/settings" it is automatically linked |
| 2055 | from the plugin list screen. |
| 2056 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2057 | [[http]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2058 | == HTTP Servlets |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2059 | |
| 2060 | Plugins or extensions may register additional HTTP servlets, and |
| 2061 | wrap them with HTTP filters. |
| 2062 | |
| 2063 | Servlets may use auto-registration to declare the URL they handle: |
| 2064 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2065 | [source,java] |
| 2066 | ---- |
| 2067 | import com.google.gerrit.extensions.annotations.Export; |
| 2068 | import com.google.inject.Singleton; |
| 2069 | import javax.servlet.http.HttpServlet; |
| 2070 | import javax.servlet.http.HttpServletRequest; |
| 2071 | import javax.servlet.http.HttpServletResponse; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2072 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2073 | @Export("/print") |
| 2074 | @Singleton |
| 2075 | class HelloServlet extends HttpServlet { |
| 2076 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 2077 | res.setContentType("text/plain"); |
| 2078 | res.setCharacterEncoding("UTF-8"); |
| 2079 | res.getWriter().write("Hello"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2080 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2081 | } |
| 2082 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2083 | |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 2084 | The auto registration only works for standard servlet mappings like |
Jonathan Nieder | 5758f18 | 2015-03-30 11:28:55 -0700 | [diff] [blame] | 2085 | `/foo` or `+/foo/*+`. Regex style bindings must use a Guice ServletModule |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 2086 | to register the HTTP servlets and declare it explicitly in the manifest |
| 2087 | with the `Gerrit-HttpModule` attribute: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2088 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2089 | [source,java] |
| 2090 | ---- |
| 2091 | import com.google.inject.servlet.ServletModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2092 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2093 | class MyWebUrls extends ServletModule { |
| 2094 | protected void configureServlets() { |
| 2095 | serve("/print").with(HelloServlet.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2096 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2097 | } |
| 2098 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2099 | |
| 2100 | For a plugin installed as name `helloworld`, the servlet implemented |
| 2101 | by HelloServlet class will be available to users as: |
| 2102 | |
| 2103 | ---- |
| 2104 | $ curl http://review.example.com/plugins/helloworld/print |
| 2105 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2106 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2107 | [[data-directory]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2108 | == Data Directory |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 2109 | |
Dave Borowitz | 9e15875 | 2015-02-24 10:17:04 -0800 | [diff] [blame] | 2110 | Plugins can request a data directory with a `@PluginData` Path (or File, |
| 2111 | deprecated) dependency. A data directory will be created automatically |
| 2112 | by the server in `$site_path/data/$plugin_name` and passed to the |
| 2113 | plugin. |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 2114 | |
| 2115 | Plugins can use this to store any data they want. |
| 2116 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2117 | [source,java] |
| 2118 | ---- |
| 2119 | @Inject |
Dave Borowitz | 9e15875 | 2015-02-24 10:17:04 -0800 | [diff] [blame] | 2120 | MyType(@PluginData java.nio.file.Path myDir) { |
| 2121 | this.in = Files.newInputStream(myDir.resolve("my.config")); |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 2122 | } |
| 2123 | ---- |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 2124 | |
Dariusz Luksza | ebab92a | 2014-09-10 11:14:19 +0200 | [diff] [blame] | 2125 | [[secure-store]] |
| 2126 | == SecureStore |
| 2127 | |
| 2128 | SecureStore allows to change the way Gerrit stores sensitive data like |
| 2129 | passwords. |
| 2130 | |
| 2131 | In order to replace the default SecureStore (no-op) implementation, |
| 2132 | a class that extends `com.google.gerrit.server.securestore.SecureStore` |
| 2133 | needs to be provided (with dependencies) in a separate jar file. Then |
| 2134 | link:pgm-SwitchSecureStore.html[SwitchSecureStore] must be run to |
| 2135 | switch implementations. |
| 2136 | |
| 2137 | The SecureStore implementation is instantiated using a Guice injector |
| 2138 | which binds the `File` annotated with the `@SitePath` annotation. |
| 2139 | This means that a SecureStore implementation class can get access to |
| 2140 | the `site_path` like in the following example: |
| 2141 | |
| 2142 | [source,java] |
| 2143 | ---- |
| 2144 | @Inject |
| 2145 | MySecureStore(@SitePath java.io.File sitePath) { |
| 2146 | // your code |
| 2147 | } |
| 2148 | ---- |
| 2149 | |
| 2150 | No Guice bindings or modules are required. Gerrit will automatically |
| 2151 | discover and bind the implementation. |
| 2152 | |
Thomas Draebing | c4e5c07 | 2020-07-10 11:44:47 +0200 | [diff] [blame] | 2153 | [[gerrit-replica]] |
| 2154 | == Gerrit Replica |
| 2155 | |
| 2156 | Gerrit can be run as a read-only replica. Some plugins may need to know |
| 2157 | whether Gerrit is run as a primary- or a replica instance. For that purpose |
| 2158 | Gerrit exposes the `@GerritIsReplica` annotation. A boolean annotated with |
| 2159 | this annotation will indicate whether Gerrit is run as a replica. |
| 2160 | |
Michael Ochmann | 2461265 | 2016-02-12 17:26:18 +0100 | [diff] [blame] | 2161 | [[accountcreation]] |
| 2162 | == Account Creation |
| 2163 | |
| 2164 | Plugins can hook into the |
| 2165 | link:rest-api-accounts.html#create-account[account creation] REST API and |
| 2166 | inject additional external identifiers for an account that represents a user |
| 2167 | in some external user store. For that, an implementation of the extension |
Han-Wen Nienhuys | a77b2e5 | 2017-11-11 11:30:05 +0100 | [diff] [blame] | 2168 | point `com.google.gerrit.server.account.AccountExternalIdCreator` |
Michael Ochmann | 2461265 | 2016-02-12 17:26:18 +0100 | [diff] [blame] | 2169 | must be registered. |
| 2170 | |
| 2171 | [source,java] |
| 2172 | ---- |
| 2173 | class MyExternalIdCreator implements AccountExternalIdCreator { |
| 2174 | @Override |
| 2175 | public List<AccountExternalId> create(Account.Id id, String username, |
| 2176 | String email) { |
| 2177 | // your code |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | bind(AccountExternalIdCreator.class) |
| 2182 | .annotatedWith(UniqueAnnotations.create()) |
| 2183 | .to(MyExternalIdCreator.class); |
| 2184 | } |
| 2185 | ---- |
| 2186 | |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2187 | [[download-commands]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2188 | == Download Commands |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2189 | |
Edwin Kempin | eafde88 | 2015-05-11 15:40:44 +0200 | [diff] [blame] | 2190 | Gerrit offers commands for downloading changes and cloning projects |
| 2191 | using different download schemes (e.g. for downloading via different |
| 2192 | network protocols). Plugins can contribute download schemes, download |
| 2193 | commands and clone commands by implementing |
| 2194 | `com.google.gerrit.extensions.config.DownloadScheme`, |
| 2195 | `com.google.gerrit.extensions.config.DownloadCommand` and |
| 2196 | `com.google.gerrit.extensions.config.CloneCommand`. |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2197 | |
Edwin Kempin | eafde88 | 2015-05-11 15:40:44 +0200 | [diff] [blame] | 2198 | The download schemes, download commands and clone commands which are |
| 2199 | used most often are provided by the Gerrit core plugin |
| 2200 | `download-commands`. |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2201 | |
Edwin Kempin | 78279ba | 2015-05-22 15:22:41 +0200 | [diff] [blame] | 2202 | [[included-in]] |
| 2203 | == Included In |
| 2204 | |
| 2205 | For merged changes the link:user-review-ui.html#included-in[Included In] |
| 2206 | drop-down panel shows the branches and tags in which the change is |
| 2207 | included. |
| 2208 | |
| 2209 | Plugins can add additional systems in which the change can be included |
| 2210 | by implementing `com.google.gerrit.extensions.config.ExternalIncludedIn`, |
| 2211 | e.g. a plugin can provide a list of servers on which the change was |
| 2212 | deployed. |
| 2213 | |
David Pursehouse | 38a27c3e | 2018-07-06 22:34:18 +0900 | [diff] [blame] | 2214 | [[change-report-formatting]] |
| 2215 | == Change Report Formatting |
| 2216 | |
| 2217 | When a change is pushed for review from the command line, Gerrit reports |
| 2218 | the change(s) received with their URL and subject. |
| 2219 | |
| 2220 | By implementing the |
| 2221 | `com.google.gerrit.server.git.ChangeReportFormatter` interface, a plugin |
| 2222 | may change the formatting of the report. |
| 2223 | |
David Pursehouse | e2f25be | 2018-11-26 17:56:07 +0900 | [diff] [blame] | 2224 | [[url-formatting]] |
| 2225 | == URL Formatting |
| 2226 | |
| 2227 | URLs to various parts of Gerrit are usually formed by adding suffixes to |
| 2228 | the canonical web URL. |
| 2229 | |
| 2230 | By implementing the |
| 2231 | `com.google.gerrit.server.config.UrlFormatter` interface, a plugin may |
| 2232 | change the format of the URL. |
| 2233 | |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2234 | [[links-to-external-tools]] |
| 2235 | == Links To External Tools |
| 2236 | |
| 2237 | Gerrit has extension points that enables development of a |
| 2238 | light-weight plugin that links commits to external |
| 2239 | tools (GitBlit, CGit, company specific resources etc). |
| 2240 | |
| 2241 | PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI. |
| 2242 | |
| 2243 | [source, java] |
| 2244 | ---- |
| 2245 | import com.google.gerrit.extensions.annotations.Listen; |
| 2246 | import com.google.gerrit.extensions.webui.PatchSetWebLink;; |
Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 2247 | import com.google.gerrit.extensions.webui.WebLinkTarget; |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2248 | |
| 2249 | @Listen |
| 2250 | public class MyWeblinkPlugin implements PatchSetWebLink { |
| 2251 | |
| 2252 | private String name = "MyLink"; |
| 2253 | private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s"; |
Sven Selberg | 5548420 | 2014-06-26 08:48:51 +0200 | [diff] [blame] | 2254 | private String imageUrl = "http://placehold.it/16x16.gif"; |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2255 | |
| 2256 | @Override |
Gal Paikin | 57c9d58 | 2020-11-27 14:35:22 +0100 | [diff] [blame] | 2257 | public WebLinkInfo getPatchSetWebLink(String projectName, String commit, |
| 2258 | String subject, String branchName) { |
Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 2259 | return new WebLinkInfo(name, |
| 2260 | imageUrl, |
| 2261 | String.format(placeHolderUrlProjectCommit, project, commit), |
| 2262 | WebLinkTarget.BLANK); |
Edwin Kempin | ceeed6b | 2014-09-11 17:07:33 +0200 | [diff] [blame] | 2263 | } |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2264 | } |
| 2265 | ---- |
| 2266 | |
David Pursehouse | 58b8d76 | 2016-12-09 11:12:27 +0900 | [diff] [blame] | 2267 | ParentWebLinks will appear to the right of the SHA1 of the parent |
| 2268 | revisions in the UI. The implementation should in most use cases direct |
| 2269 | to the same external service as PatchSetWebLink; it is provided as a |
| 2270 | separate interface because not all users want to have links for the |
| 2271 | parent revisions. |
| 2272 | |
Edwin Kempin | b3696c8 | 2014-09-11 09:41:42 +0200 | [diff] [blame] | 2273 | FileWebLinks will appear in the side-by-side diff screen on the right |
| 2274 | side of the patch selection on each side. |
| 2275 | |
Edwin Kempin | 8cdce50 | 2014-12-06 10:55:38 +0100 | [diff] [blame] | 2276 | DiffWebLinks will appear in the side-by-side and unified diff screen in |
| 2277 | the header next to the navigation icons. |
| 2278 | |
Edwin Kempin | ea00475 | 2014-04-11 15:56:02 +0200 | [diff] [blame] | 2279 | ProjectWebLinks will appear in the project list in the |
| 2280 | `Repository Browser` column. |
| 2281 | |
Edwin Kempin | 0f697bd | 2014-09-10 18:23:29 +0200 | [diff] [blame] | 2282 | BranchWebLinks will appear in the branch list in the last column. |
| 2283 | |
Edwin Kempin | f82b812 | 2016-06-03 09:20:16 +0200 | [diff] [blame] | 2284 | FileHistoryWebLinks will appear on the access rights screen. |
| 2285 | |
Paladox none | 34da15c | 2017-07-01 14:49:10 +0000 | [diff] [blame] | 2286 | TagWebLinks will appear in the tag list in the last column. |
| 2287 | |
Dave Borowitz | d0c01fd | 2017-06-06 10:47:08 -0400 | [diff] [blame] | 2288 | If a `get*WebLink` implementation returns `null`, the link will be omitted. This |
| 2289 | allows the plugin to selectively "enable" itself on a per-project/branch/file |
| 2290 | basis. |
| 2291 | |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2292 | [[lfs-extension]] |
| 2293 | == LFS Storage Plugins |
| 2294 | |
David Pursehouse | 2463c54 | 2016-08-02 16:04:58 +0900 | [diff] [blame] | 2295 | Gerrit provides an extension point that enables development of |
| 2296 | link:https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md[ |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2297 | LFS (Large File Storage),role=external,window=_blank] storage plugins. Gerrit core exposes the default LFS |
David Pursehouse | 2463c54 | 2016-08-02 16:04:58 +0900 | [diff] [blame] | 2298 | protocol endpoint `<project-name>/info/lfs/objects/batch` and forwards the requests |
| 2299 | to the configured link:config-gerrit.html#lfs[lfs.plugin] plugin which implements |
| 2300 | the LFS protocol. By exposing the default LFS endpoint, the git-lfs client can be |
| 2301 | used without any configuration. |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2302 | |
| 2303 | [source, java] |
| 2304 | ---- |
| 2305 | /** Provide an LFS protocol implementation */ |
| 2306 | import org.eclipse.jgit.lfs.server.LargeFileRepository; |
| 2307 | import org.eclipse.jgit.lfs.server.LfsProtocolServlet; |
| 2308 | |
| 2309 | @Singleton |
| 2310 | public class LfsApiServlet extends LfsProtocolServlet { |
| 2311 | private static final long serialVersionUID = 1L; |
| 2312 | |
| 2313 | private final S3LargeFileRepository repository; |
| 2314 | |
| 2315 | @Inject |
| 2316 | LfsApiServlet(S3LargeFileRepository repository) { |
| 2317 | this.repository = repository; |
| 2318 | } |
| 2319 | |
| 2320 | @Override |
| 2321 | protected LargeFileRepository getLargeFileRepository() { |
| 2322 | return repository; |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | /** Register the LfsApiServlet to listen on the default LFS protocol endpoint */ |
| 2327 | import static com.google.gerrit.httpd.plugins.LfsPluginServlet.URL_REGEX; |
| 2328 | |
Dave Borowitz | 20f4e8a | 2019-03-13 14:19:15 -0700 | [diff] [blame] | 2329 | import com.google.inject.servlet.ServletModule; |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2330 | |
Dave Borowitz | 20f4e8a | 2019-03-13 14:19:15 -0700 | [diff] [blame] | 2331 | public class HttpModule extends ServletModule { |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2332 | |
| 2333 | @Override |
| 2334 | protected void configureServlets() { |
| 2335 | serveRegex(URL_REGEX).with(LfsApiServlet.class); |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | /** Provide an implementation of the LargeFileRepository */ |
| 2340 | import org.eclipse.jgit.lfs.server.s3.S3Repository; |
| 2341 | |
| 2342 | public class S3LargeFileRepository extends S3Repository { |
| 2343 | ... |
| 2344 | } |
| 2345 | ---- |
| 2346 | |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2347 | [[metrics]] |
| 2348 | == Metrics |
| 2349 | |
| 2350 | === Metrics Reporting |
| 2351 | |
| 2352 | To send Gerrit's metrics data to an external reporting backend, a plugin can |
| 2353 | get a `MetricRegistry` injected and register an instance of a class that |
| 2354 | implements the `Reporter` interface from link:http://metrics.dropwizard.io/[ |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2355 | DropWizard Metrics,role=external,window=_blank]. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2356 | |
| 2357 | Metric reporting plugin implementations are provided for |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2358 | link:https://gerrit.googlesource.com/plugins/metrics-reporter-jmx/[JMX,role=external,window=_blank], |
| 2359 | link:https://gerrit.googlesource.com/plugins/metrics-reporter-elasticsearch/[Elastic Search,role=external,window=_blank], |
| 2360 | and link:https://gerrit.googlesource.com/plugins/metrics-reporter-graphite/[Graphite,role=external,window=_blank]. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2361 | |
| 2362 | There is also a working example of reporting metrics to the console in the |
Eryk Szymanski | da073b1 | 2017-09-11 14:27:57 +0200 | [diff] [blame] | 2363 | link:https://gerrit.googlesource.com/plugins/cookbook-plugin/+/master/src/main/java/com/googlesource/gerrit/plugins/cookbook/ConsoleMetricReporter.java[ |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2364 | cookbook plugin,role=external,window=_blank]. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2365 | |
| 2366 | === Providing own metrics |
| 2367 | |
| 2368 | Plugins may provide metrics to be dispatched to external reporting services by |
| 2369 | getting a `MetricMaker` injected and creating instances of specific types of |
| 2370 | metric: |
| 2371 | |
| 2372 | * Counter |
| 2373 | + |
| 2374 | Metric whose value increments during the life of the process. |
| 2375 | |
| 2376 | * Timer |
| 2377 | + |
| 2378 | Metric recording time spent on an operation. |
| 2379 | |
| 2380 | * Histogram |
| 2381 | + |
| 2382 | Metric recording statistical distribution (rate) of values. |
| 2383 | |
David Pursehouse | 48d05ea | 2017-02-03 19:05:29 +0900 | [diff] [blame] | 2384 | Note that metrics cannot be recorded from plugin init steps that |
| 2385 | are run during site initialization. |
| 2386 | |
David Pursehouse | c3bbd56 | 2017-02-06 20:25:29 +0900 | [diff] [blame] | 2387 | By default, plugin metrics are recorded under |
| 2388 | `plugins/${plugin-name}/${metric-name}`. This can be changed by |
| 2389 | setting `plugins.${plugin-name}.metricsPrefix` in the `gerrit.config` |
| 2390 | file. For example: |
| 2391 | |
| 2392 | ---- |
| 2393 | [plugin "my-plugin"] |
| 2394 | metricsPrefix = my-metrics |
| 2395 | ---- |
| 2396 | |
| 2397 | will cause the metrics to be recorded under `my-metrics/${metric-name}`. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2398 | |
| 2399 | See the replication metrics in the |
| 2400 | link:https://gerrit.googlesource.com/plugins/replication/+/master/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationMetrics.java[ |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2401 | replication plugin,role=external,window=_blank] for an example of usage. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2402 | |
Edwin Kempin | da17bc3 | 2016-06-14 11:50:58 +0200 | [diff] [blame] | 2403 | [[account-patch-review-store]] |
| 2404 | == AccountPatchReviewStore |
| 2405 | |
| 2406 | The AccountPatchReviewStore is used to store reviewed flags on changes. |
| 2407 | A reviewed flag is a tuple of (patch set ID, file, account ID) and |
| 2408 | records whether the user has reviewed a file in a patch set. Each user |
| 2409 | can easily have thousands of reviewed flags and the number of reviewed |
| 2410 | flags is growing without bound. The store must be able handle this data |
| 2411 | volume efficiently. |
| 2412 | |
| 2413 | Gerrit implements this extension point, but plugins may bind another |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 2414 | implementation, e.g. one that supports cluster setup with multiple |
| 2415 | primary Gerrit nodes handling write operations. |
Edwin Kempin | da17bc3 | 2016-06-14 11:50:58 +0200 | [diff] [blame] | 2416 | |
| 2417 | ---- |
| 2418 | DynamicItem.bind(binder(), AccountPatchReviewStore.class) |
| 2419 | .to(MultiMasterAccountPatchReviewStore.class); |
| 2420 | |
| 2421 | ... |
| 2422 | |
| 2423 | public class MultiMasterAccountPatchReviewStore |
| 2424 | implements AccountPatchReviewStore { |
| 2425 | ... |
| 2426 | } |
| 2427 | ---- |
| 2428 | |
Dave Borowitz | f1790ce | 2016-11-14 12:26:12 -0800 | [diff] [blame] | 2429 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2430 | [[documentation]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2431 | == Documentation |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2432 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2433 | 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] | 2434 | `+/Documentation/*+` or `+/static/*+`, the core Gerrit server will |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2435 | automatically export these resources over HTTP from the plugin JAR. |
| 2436 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2437 | Static resources under the `static/` directory in the JAR will be |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 2438 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 2439 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2440 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2441 | Documentation files under the `Documentation/` directory in the JAR |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 2442 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 2443 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 2444 | attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2445 | |
Christian Aistleitner | 040cf82 | 2015-03-26 21:09:09 +0100 | [diff] [blame] | 2446 | Documentation may be written in the Markdown flavor |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2447 | link:https://github.com/vsch/flexmark-java[flexmark-java,role=external,window=_blank] |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2448 | if the file name ends with `.md`. Gerrit will automatically convert |
| 2449 | Markdown to HTML if accessed with extension `.html`. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2450 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2451 | [[macros]] |
Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 2452 | Within the Markdown documentation files macros can be used that allow |
| 2453 | to write documentation with reasonably accurate examples that adjust |
| 2454 | automatically based on the installation. |
| 2455 | |
| 2456 | The following macros are supported: |
| 2457 | |
| 2458 | [width="40%",options="header"] |
| 2459 | |=================================================== |
| 2460 | |Macro | Replacement |
| 2461 | |@PLUGIN@ | name of the plugin |
| 2462 | |@URL@ | Gerrit Web URL |
| 2463 | |@SSH_HOST@ | SSH Host |
| 2464 | |@SSH_PORT@ | SSH Port |
| 2465 | |=================================================== |
| 2466 | |
| 2467 | The macros will be replaced when the documentation files are rendered |
| 2468 | from Markdown to HTML. |
| 2469 | |
| 2470 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 2471 | even if there is an expansion for `KEEP` in the future. |
| 2472 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2473 | [[auto-index]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2474 | === Automatic Index |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2475 | |
| 2476 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 2477 | redirect clients to the plugin's `/Documentation/index.html`. |
| 2478 | Requests for `/Documentation/` (bare directory) will also redirect |
| 2479 | to `/Documentation/index.html`. |
| 2480 | |
| 2481 | If neither resource `Documentation/index.html` or |
| 2482 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
| 2483 | automatically generate an index page for the plugin's documentation |
| 2484 | tree by scanning every `*.md` and `*.html` file in the Documentation/ |
| 2485 | directory. |
| 2486 | |
| 2487 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 2488 | header of the file and extract the first level one title. This |
| 2489 | title text will be used as display text for a link to the HTML |
| 2490 | version of the page. |
| 2491 | |
| 2492 | For any discovered HTML (`*.html`) file, Gerrit will use the name |
| 2493 | of the file, minus the `*.html` extension, as the link text. Any |
| 2494 | hyphens in the file name will be replaced with spaces. |
| 2495 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2496 | If a discovered file is named `about.md` or `about.html`, its |
| 2497 | content will be inserted in an 'About' section at the top of the |
| 2498 | auto-generated index page. If both `about.md` and `about.html` |
| 2499 | exist, only the first discovered file will be used. |
| 2500 | |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2501 | If a discovered file name beings with `cmd-` it will be clustered |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2502 | into a 'Commands' section of the generated index page. |
| 2503 | |
David Pursehouse | fe52915 | 2013-08-14 16:35:06 +0900 | [diff] [blame] | 2504 | If a discovered file name beings with `servlet-` it will be clustered |
| 2505 | into a 'Servlets' section of the generated index page. |
| 2506 | |
| 2507 | If a discovered file name beings with `rest-api-` it will be clustered |
| 2508 | into a 'REST APIs' section of the generated index page. |
| 2509 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2510 | All other files are clustered under a 'Documentation' section. |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2511 | |
| 2512 | Some optional information from the manifest is extracted and |
| 2513 | displayed as part of the index page, if present in the manifest: |
| 2514 | |
| 2515 | [width="40%",options="header"] |
| 2516 | |=================================================== |
| 2517 | |Field | Source Attribute |
| 2518 | |Name | Implementation-Title |
| 2519 | |Vendor | Implementation-Vendor |
| 2520 | |Version | Implementation-Version |
| 2521 | |URL | Implementation-URL |
| 2522 | |API Version | Gerrit-ApiVersion |
| 2523 | |=================================================== |
| 2524 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2525 | [[deployment]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2526 | == Deployment |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2527 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 2528 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 2529 | server using the link:cmd-plugin-install.html[plugin install] command. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2530 | |
Viktar Donich | 15ef24c | 2017-12-07 16:04:38 -0800 | [diff] [blame] | 2531 | Web UI plugins distributed as a single `.js` file (or `.html` file for |
David Pursehouse | 00c7081 | 2017-07-31 10:57:26 +0100 | [diff] [blame] | 2532 | Polygerrit) can be deployed without the overhead of JAR packaging. For |
| 2533 | more information refer to link:cmd-plugin-install.html[plugin install] |
| 2534 | command. |
Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame] | 2535 | |
David Pursehouse | 75021fe | 2017-07-31 23:07:04 +0200 | [diff] [blame] | 2536 | Plugins can also be copied directly into the server's directory at |
| 2537 | `$site_path/plugins/$name.(jar|js|html)`. For Web UI plugins, the name |
| 2538 | of the file, minus the `.js` or `.html` extension, will be used as the |
| 2539 | plugin name. For JAR plugins, the value of the `Gerrit-PluginName` |
| 2540 | manifest attribute will be used, if provided, otherwise the name of |
| 2541 | the file, minus the `.jar` extension, will be used. |
| 2542 | |
| 2543 | For Web UI plugins, the plugin version is derived from the filename. |
| 2544 | If the filename contains one or more hyphens, the version is taken |
| 2545 | from the portion following the last hyphen. For example if the plugin |
| 2546 | filename is `my-plugin-1.0.js` the version will be `1.0`. For JAR |
| 2547 | plugins, the version is taken from the `Version` attribute in the |
| 2548 | manifest. |
| 2549 | |
| 2550 | Unless disabled, servers periodically scan the `$site_path/plugins` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2551 | directory for updated plugins. The time can be adjusted by |
| 2552 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2553 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 2554 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 2555 | command can be used. |
| 2556 | |
Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 2557 | Disabled plugins can be re-enabled using the |
| 2558 | link:cmd-plugin-enable.html[plugin enable] command. |
| 2559 | |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2560 | [[reviewer-suggestion]] |
| 2561 | == Reviewer Suggestion Plugins |
| 2562 | |
| 2563 | Gerrit provides an extension point that enables Plugins to rank |
| 2564 | the list of reviewer suggestion a user receives upon clicking "Add Reviewer" on |
| 2565 | the change screen. |
David Pursehouse | c2233a4 | 2018-02-07 10:31:05 +0900 | [diff] [blame] | 2566 | |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2567 | Gerrit supports both a default suggestion that appears when the user has not yet |
| 2568 | typed anything and a filtered suggestion that is shown as the user starts |
| 2569 | typing. |
David Pursehouse | c2233a4 | 2018-02-07 10:31:05 +0900 | [diff] [blame] | 2570 | |
| 2571 | Plugins receive a candidate list and can return a `Set` of suggested reviewers |
| 2572 | containing the `Account.Id` and a score for each reviewer. The candidate list is |
| 2573 | non-binding and plugins can choose to return reviewers not initially contained in |
| 2574 | the candidate list. |
| 2575 | |
| 2576 | Server administrators can configure the overall weight of each plugin by setting |
| 2577 | the `addreviewer.pluginName-exportName.weight` value in `gerrit.config`. |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2578 | |
| 2579 | [source, java] |
| 2580 | ---- |
Patrick Hiesel | 2514e41 | 2016-10-13 09:34:44 +0200 | [diff] [blame] | 2581 | import com.google.gerrit.common.Nullable; |
David Ostrovsky | b03a6e9 | 2019-05-26 14:11:47 +0200 | [diff] [blame] | 2582 | import com.google.gerrit.entities.Account; |
| 2583 | import com.google.gerrit.entities.Change; |
| 2584 | import com.google.gerrit.entities.Project; |
Patrick Hiesel | 2514e41 | 2016-10-13 09:34:44 +0200 | [diff] [blame] | 2585 | import com.google.gerrit.extensions.annotations.ExtensionPoint; |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2586 | |
| 2587 | import java.util.Set; |
| 2588 | |
| 2589 | public class MyPlugin implements ReviewerSuggestion { |
Patrick Hiesel | 2514e41 | 2016-10-13 09:34:44 +0200 | [diff] [blame] | 2590 | public Set<SuggestedReviewer> suggestReviewers(Project.NameKey project, |
| 2591 | @Nullable Change.Id changeId, @Nullable String query, |
| 2592 | Set<Account.Id> candidates) { |
| 2593 | Set<SuggestedReviewer> suggestions = new HashSet<>(); |
| 2594 | // Implement your ranking logic here |
| 2595 | return suggestions; |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | ---- |
| 2599 | |
| 2600 | |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2601 | [[mail-filter]] |
| 2602 | == Mail Filter Plugins |
| 2603 | |
| 2604 | Gerrit provides an extension point that enables Plugins to discard incoming |
| 2605 | messages and prevent further processing by Gerrit. |
| 2606 | |
David Pursehouse | 4b06775 | 2017-03-03 15:54:53 +0900 | [diff] [blame] | 2607 | This can be used to implement spam checks, signature validations or organization |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2608 | specific checks like IP filters. |
| 2609 | |
| 2610 | [source, java] |
| 2611 | ---- |
| 2612 | import com.google.gerrit.extensions.annotations.ExtensionPoint; |
Han-Wen Nienhuys | 50dd94e | 2017-11-14 18:04:28 +0100 | [diff] [blame] | 2613 | import com.google.gerrit.mail.MailMessage; |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2614 | |
| 2615 | public class MyPlugin implements MailFilter { |
maximeg | 92419ab | 2018-02-16 10:27:25 +0100 | [diff] [blame] | 2616 | public boolean shouldProcessMessage(MailMessage message) { |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2617 | // Implement your filter logic here |
| 2618 | return true; |
| 2619 | } |
| 2620 | } |
| 2621 | ---- |
| 2622 | |
David Causse | 01c1340 | 2019-04-09 14:19:28 +0200 | [diff] [blame] | 2623 | [[ssh-command-creation-interception]] |
| 2624 | == SSH Command Creation Interception |
David Pursehouse | 9cd079f | 2018-02-28 21:36:27 +0900 | [diff] [blame] | 2625 | |
| 2626 | Gerrit provides an extension point that allows a plugin to intercept |
| 2627 | creation of SSH commands and override the functionality with its own |
| 2628 | implementation. |
| 2629 | |
| 2630 | [source, java] |
| 2631 | ---- |
| 2632 | import com.google.gerrit.sshd.SshCreateCommandInterceptor; |
| 2633 | |
| 2634 | class MyCommandInterceptor implements SshCreateCommandInterceptor { |
| 2635 | @Override |
| 2636 | public String intercept(String in) { |
| 2637 | return pluginName + " mycommand"; |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2638 | ---- |
| 2639 | |
David Causse | 01c1340 | 2019-04-09 14:19:28 +0200 | [diff] [blame] | 2640 | [[ssh-command-execution-interception]] |
| 2641 | == SSH Command Execution Interception |
| 2642 | Gerrit provides an extension point that enables plugins to check and |
| 2643 | prevent an SSH command from being run. |
| 2644 | |
| 2645 | [source, java] |
| 2646 | ---- |
| 2647 | import com.google.gerrit.sshd.SshExecuteCommandInterceptor; |
| 2648 | |
| 2649 | @Singleton |
| 2650 | public class SshExecuteCommandInterceptorImpl implements SshExecuteCommandInterceptor { |
| 2651 | private final Provider<SshSession> sessionProvider; |
| 2652 | |
| 2653 | @Inject |
| 2654 | SshExecuteCommandInterceptorImpl(Provider<SshSession> sessionProvider) { |
| 2655 | this.sessionProvider = sessionProvider; |
| 2656 | } |
| 2657 | |
| 2658 | @Override |
| 2659 | public boolean accept(String command, List<String> arguments) { |
| 2660 | if (command.startsWith("gerrit") && !"10.1.2.3".equals(sessionProvider.get().getRemoteAddressAsString())) { |
| 2661 | return false; |
| 2662 | } |
| 2663 | return true; |
| 2664 | } |
| 2665 | } |
| 2666 | ---- |
| 2667 | |
| 2668 | And then declare it in your SSH module: |
| 2669 | [source, java] |
| 2670 | ---- |
| 2671 | DynamicSet.bind(binder(), SshExecuteCommandInterceptor.class).to(SshExecuteCommandInterceptorImpl.class); |
| 2672 | ---- |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2673 | |
| 2674 | [[pre-submit-evaluator]] |
| 2675 | == Pre-submit Validation Plugins |
| 2676 | |
| 2677 | Gerrit provides an extension point that enables plugins to prevent a change |
| 2678 | from being submitted. |
| 2679 | |
| 2680 | [IMPORTANT] |
| 2681 | This extension point **must NOT** be used for long or slow operations, like |
| 2682 | calling external programs or content, running unit tests... |
| 2683 | Slow operations will hurt the whole Gerrit instance. |
| 2684 | |
| 2685 | This can be used to implement custom rules that changes have to match to become |
| 2686 | submittable. A more concrete example: the Prolog rules engine can be |
| 2687 | implemented using this. |
| 2688 | |
| 2689 | Gerrit calls the plugins once per change and caches the results. Although it is |
| 2690 | possible to predict when this interface will be triggered, this should not be |
| 2691 | considered as a feature. Plugins should only rely on the internal state of the |
| 2692 | ChangeData, not on external values like date and time, remote content or |
| 2693 | randomness. |
| 2694 | |
| 2695 | Plugins are expected to support rules inheritance themselves, providing ways |
| 2696 | to configure it and handling the logic behind it. |
| 2697 | Please note that no inheritance is sometimes better than badly handled |
| 2698 | inheritance: mis-communication and strange behaviors caused by inheritance |
| 2699 | may and will confuse the users. Each plugins is responsible for handling the |
| 2700 | project hierarchy and taking wise actions. Gerrit does not enforce it. |
| 2701 | |
| 2702 | Once Gerrit has gathered every plugins' SubmitRecords, it stores them. |
| 2703 | |
| 2704 | Plugins accept or reject a given change using `SubmitRecord.Status`. |
| 2705 | If a change is ready to be submitted, `OK`. If it is not ready and requires |
| 2706 | modifications, `NOT_READY`. Other statuses are available for particular cases. |
| 2707 | A change can be submitted if all the plugins accept the change. |
| 2708 | |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2709 | Plugins may also decide not to vote on a given change by returning an |
| 2710 | `Optional.empty()` (ie: the plugin is not enabled for this repository). |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2711 | |
| 2712 | If a plugin decides not to vote, it's name will not be displayed in the UI and |
| 2713 | it will not be recoded in the database. |
| 2714 | |
| 2715 | .Gerrit's Pre-submit handling with three plugins |
| 2716 | [width="50%",cols="^m,^m,^m,^m",frame="topbot",options="header"] |
| 2717 | |======================================================= |
| 2718 | | Plugin A | Plugin B | Plugin C | Final decision |
| 2719 | | OK | OK | OK | OK |
| 2720 | | OK | OK | / | OK |
| 2721 | | OK | OK | RULE_ERROR | NOT_READY |
| 2722 | | OK | NOT_READY | OK | NOT_READY |
| 2723 | | NOT_READY | OK | OK | NOT_READY |
| 2724 | |======================================================= |
| 2725 | |
| 2726 | |
| 2727 | This makes composing plugins really easy. |
| 2728 | |
| 2729 | - If a plugin places a veto on a change, it can't be submitted. |
| 2730 | - If a plugin isn't enabled for a project (or isn't needed for this change), |
| 2731 | it returns an empty collection. |
| 2732 | - If all the plugins answer `OK`, the change can be submitted. |
| 2733 | |
| 2734 | |
| 2735 | A more rare case, but worth documenting: if there are no installed plugins, |
| 2736 | the labels will be compared to the rules defined in the project's config, |
| 2737 | and the permission system will be used to allow or deny a submit request. |
| 2738 | |
| 2739 | Some rules are defined internally to provide a common base ground (and sanity): |
| 2740 | changes that are marked as WIP or that are closed (abandoned, merged) can't be merged. |
| 2741 | |
| 2742 | |
| 2743 | [source, java] |
| 2744 | ---- |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2745 | import java.util.Optional; |
Andrew Allen | 6887c73 | 2020-09-12 00:42:11 +0000 | [diff] [blame] | 2746 | import com.google.gerrit.entities.SubmitRecord; |
| 2747 | import com.google.gerrit.entities.SubmitRecord.Status; |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2748 | import com.google.gerrit.server.query.change.ChangeData; |
| 2749 | import com.google.gerrit.server.rules.SubmitRule; |
| 2750 | |
| 2751 | public class MyPluginRules implements SubmitRule { |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2752 | public Optional<SubmitRecord> evaluate(ChangeData changeData) { |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2753 | // Implement your submitability logic here |
| 2754 | |
| 2755 | // Assuming we want to prevent this change from being submitted: |
Edwin Kempin | f5d3e94 | 2019-07-22 15:23:47 +0200 | [diff] [blame] | 2756 | SubmitRecord record = new SubmitRecord(); |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2757 | record.status = Status.NOT_READY; |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2758 | return Optional.of(record); |
David Pursehouse | 9cd079f | 2018-02-28 21:36:27 +0900 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | ---- |
| 2762 | |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2763 | Don't forget to register your class! |
| 2764 | |
| 2765 | [source, java] |
| 2766 | ---- |
| 2767 | import com.google.gerrit.extensions.annotations.Exports; |
| 2768 | import com.google.inject.AbstractModule; |
| 2769 | |
| 2770 | public class MyPluginModule extends AbstractModule { |
| 2771 | @Override |
| 2772 | protected void configure() { |
| 2773 | bind(SubmitRule.class).annotatedWith(Exports.named("myPlugin")).to(MyPluginRules.class); |
| 2774 | } |
| 2775 | } |
| 2776 | ---- |
David Pursehouse | 9cd079f | 2018-02-28 21:36:27 +0900 | [diff] [blame] | 2777 | |
Maxime Guerreiro | 5ccf0b9 | 2018-05-15 16:28:22 +0000 | [diff] [blame] | 2778 | Plugin authors should also consider binding their SubmitRule using a `Gerrit-BatchModule`. |
| 2779 | See link:dev-plugins.html[Batch runtime] for more informations. |
| 2780 | |
Maxime Guerreiro | 97e891a | 2018-06-06 13:36:50 +0000 | [diff] [blame] | 2781 | |
| 2782 | The SubmitRule extension point allows you to write complex rules, but writing |
| 2783 | small self-contained rules should be preferred: doing so allows end users to |
| 2784 | compose several rules to form more complex submit checks. |
| 2785 | |
| 2786 | The `SubmitRequirement` class allows rules to communicate what the user needs |
| 2787 | to change in order to be compliant. These requirements should be kept once they |
| 2788 | are met, but marked as `OK`. If the requirements were not displayed, reviewers |
| 2789 | would need to use their precious time to manually check that they were met. |
| 2790 | |
Edwin Kempin | 96915b7 | 2019-07-22 14:57:40 +0200 | [diff] [blame] | 2791 | Implementors of the `SubmitRule` interface should check whether they need to |
| 2792 | contribute to the link:#change-etag-computation[change ETag computation] to |
| 2793 | prevent callers using ETags from potentially seeing outdated submittability |
| 2794 | information. |
| 2795 | |
| 2796 | [[change-etag-computation]] |
| 2797 | == Change ETag Computation |
| 2798 | |
| 2799 | By implementing the `com.google.gerrit.server.change.ChangeETagComputation` |
| 2800 | interface plugins can contribute a value to the change ETag computation. |
| 2801 | |
| 2802 | Plugins can affect the result of the get change / get change details REST |
| 2803 | endpoints by: |
| 2804 | |
| 2805 | * providing link:#query_attributes[plugin defined attributes] in |
| 2806 | link:rest-api-changes.html#change-info[ChangeInfo] |
| 2807 | * implementing a link:#pre-submit-evaluator[pre-submit evaluator] which affects |
| 2808 | the computation of `submittable` field in |
| 2809 | link:rest-api-changes.html#change-info[ChangeInfo] |
| 2810 | |
| 2811 | If the plugin defined part of link:rest-api-changes.html#change-info[ |
| 2812 | ChangeInfo] depends on plugin specific data, callers that use change ETags to |
| 2813 | avoid unneeded recomputations of ChangeInfos may see outdated plugin attributes |
| 2814 | and/or outdated submittable information, because a ChangeInfo is only reloaded |
| 2815 | if the change ETag changes. |
| 2816 | |
| 2817 | By implementating the `com.google.gerrit.server.change.ChangeETagComputation` |
| 2818 | interface plugins can contribute to the ETag computation and thus ensure that |
| 2819 | the change ETag changes when the plugin data was changed. This way it can be |
| 2820 | ensured that callers do not see outdated ChangeInfos. |
| 2821 | |
| 2822 | IMPORTANT: Change ETags are computed very frequently and the computation must |
| 2823 | be cheap. Take good care to not perform any expensive computations when |
| 2824 | implementing this. |
| 2825 | |
| 2826 | [source, java] |
| 2827 | ---- |
| 2828 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 2829 | |
| 2830 | import com.google.common.hash.Hasher; |
David Pursehouse | 0fb1fd1 | 2019-10-16 10:41:00 +0900 | [diff] [blame] | 2831 | import com.google.gerrit.entities.Change; |
| 2832 | import com.google.gerrit.entities.Project; |
Edwin Kempin | 96915b7 | 2019-07-22 14:57:40 +0200 | [diff] [blame] | 2833 | import com.google.gerrit.server.change.ChangeETagComputation; |
| 2834 | |
| 2835 | public class MyPluginChangeETagComputation implements ChangeETagComputation { |
| 2836 | public String getETag(Project.NameKey projectName, Change.Id changeId) { |
| 2837 | Hasher hasher = Hashing.murmur3_128().newHasher(); |
| 2838 | |
| 2839 | // Add hashes for all plugin-specific data that affects change infos. |
| 2840 | hasher.putString(sha1OfPluginSpecificChangeRef, UTF_8); |
| 2841 | |
| 2842 | return hasher.hash().toString(); |
| 2843 | } |
| 2844 | } |
| 2845 | ---- |
| 2846 | |
Edwin Kempin | ce8af4c | 2019-09-24 13:28:03 +0200 | [diff] [blame] | 2847 | [[exception-hook]] |
| 2848 | == ExceptionHook |
| 2849 | |
| 2850 | An `ExceptionHook` allows implementors to control how certain |
| 2851 | exceptions should be handled. |
| 2852 | |
| 2853 | This interface is intended to be implemented for multi-master setups to |
| 2854 | control the behavior for handling exceptions that are thrown by a lower |
| 2855 | layer that handles the consensus and synchronization between different |
| 2856 | server nodes. E.g. if an operation fails because consensus for a Git |
| 2857 | update could not be achieved (e.g. due to slow responding server nodes) |
| 2858 | this interface can be used to retry the request instead of failing it |
| 2859 | immediately. |
| 2860 | |
Edwin Kempin | 8f29a39 | 2019-10-31 11:48:31 +0100 | [diff] [blame] | 2861 | It also allows implementors to group exceptions that have the same |
| 2862 | cause into one metric bucket. |
| 2863 | |
Edwin Kempin | 7c9aa3e | 2019-09-26 11:50:03 +0200 | [diff] [blame] | 2864 | [[mail-soy-template-provider]] |
| 2865 | == MailSoyTemplateProvider |
| 2866 | |
| 2867 | This extension point allows to provide soy templates for registration |
| 2868 | so that they can be used for sending emails from a plugin. |
| 2869 | |
Patrick Hiesel | fc9817f | 2018-11-12 15:08:35 +0100 | [diff] [blame] | 2870 | [[quota-enforcer]] |
| 2871 | == Quota Enforcer |
| 2872 | |
| 2873 | Gerrit provides an extension point that allows a plugin to enforce quota. |
| 2874 | link:quota.html[This documentation page] has a list of all quota requests that |
| 2875 | Gerrit core issues. Plugins can choose to respond to all or just a subset of |
| 2876 | requests. Some implementations might want to keep track of user quota in buckets, |
| 2877 | others might just check against instance or project state to enforce limits on how |
| 2878 | many projects can be created or how large a repository can become. |
| 2879 | |
| 2880 | Checking against instance state can be racy for concurrent requests as the server does not |
| 2881 | refill tokens if the action fails in a later stage (e.g. database failure). If |
| 2882 | plugins want to guarantee an absolute maximum on a resource, they have to do their own |
| 2883 | book-keeping. |
| 2884 | |
| 2885 | [source, java] |
| 2886 | ---- |
| 2887 | import com.google.server.quota.QuotaEnforcer; |
| 2888 | |
| 2889 | class ProjectLimiter implements QuotaEnforcer { |
| 2890 | private final long maxNumberOfProjects = 100; |
| 2891 | @Override |
| 2892 | QuotaResponse requestTokens(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2893 | if (!"/projects/create".equals(quotaGroup)) { |
| 2894 | return QuotaResponse.noOp(); |
| 2895 | } |
| 2896 | // No deduction because we always check against the instance state (racy but fine for |
| 2897 | // this plugin) |
| 2898 | if (currentNumberOfProjects() + numTokens > maxNumberOfProjects) { |
| 2899 | return QuotaResponse.error("too many projects"); |
| 2900 | } |
| 2901 | return QuotaResponse.ok(); |
| 2902 | } |
| 2903 | |
| 2904 | @Override |
| 2905 | QuotaResponse dryRun(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2906 | // Since we are not keeping any state in this enforcer, we can simply call requestTokens(). |
| 2907 | return requestTokens(quotaGroup, ctx, numTokens); |
| 2908 | } |
| 2909 | |
| 2910 | void refill(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2911 | // No-op |
| 2912 | } |
| 2913 | } |
| 2914 | ---- |
| 2915 | |
| 2916 | [source, java] |
| 2917 | ---- |
| 2918 | import com.google.server.quota.QuotaEnforcer; |
| 2919 | |
| 2920 | class ApiQpsEnforcer implements QuotaEnforcer { |
| 2921 | // AutoRefillingPerUserBuckets is a imaginary bucket implementation that could be based on |
| 2922 | // a loading cache or a commonly used bucketing algorithm. |
| 2923 | private final AutoRefillingPerUserBuckets<CurrentUser, Long> buckets; |
| 2924 | @Override |
| 2925 | QuotaResponse requestTokens(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2926 | if (!quotaGroup.startsWith("/restapi/")) { |
| 2927 | return QuotaResponse.noOp(); |
| 2928 | } |
| 2929 | boolean success = buckets.deduct(ctx.user(), numTokens); |
| 2930 | if (!success) { |
| 2931 | return QuotaResponse.error("user sent too many qps, please wait for 5 minutes"); |
| 2932 | } |
| 2933 | return QuotaResponse.ok(); |
| 2934 | } |
| 2935 | |
| 2936 | @Override |
| 2937 | QuotaResponse dryRun(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2938 | if (!quotaGroup.startsWith("/restapi/")) { |
| 2939 | return QuotaResponse.noOp(); |
| 2940 | } |
| 2941 | boolean success = buckets.checkOnly(ctx.user(), numTokens); |
| 2942 | if (!success) { |
| 2943 | return QuotaResponse.error("user sent too many qps, please wait for 5 minutes"); |
| 2944 | } |
| 2945 | return QuotaResponse.ok(); |
| 2946 | } |
| 2947 | |
| 2948 | @Override |
| 2949 | void refill(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2950 | if (!quotaGroup.startsWith("/restapi/")) { |
| 2951 | return; |
| 2952 | } |
| 2953 | buckets.add(ctx.user(), numTokens); |
| 2954 | } |
| 2955 | } |
| 2956 | ---- |
| 2957 | |
Edwin Kempin | f3e6cc77 | 2019-05-27 17:33:16 +0200 | [diff] [blame] | 2958 | [[performance-logger]] |
| 2959 | == Performance Logger |
| 2960 | |
| 2961 | `com.google.gerrit.server.logging.PerformanceLogger` is an extension point that |
| 2962 | is invoked for all operations for which the execution time is measured. The |
| 2963 | invocation of the extension point does not happen immediately, but only at the |
| 2964 | end of a request (REST call, SSH call, git push). Implementors can write the |
| 2965 | execution times into a performance log for further analysis. |
| 2966 | |
Edwin Kempin | 4a3ac16 | 2019-07-04 08:35:58 +0200 | [diff] [blame] | 2967 | [[request-listener]] |
| 2968 | == Request Listener |
| 2969 | |
| 2970 | `com.google.gerrit.server.RequestListener` is an extension point that is |
| 2971 | invoked each time the server executes a request from a user. |
| 2972 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2973 | == SEE ALSO |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 2974 | |
| 2975 | * link:js-api.html[JavaScript API] |
| 2976 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 2977 | |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2978 | GERRIT |
| 2979 | ------ |
| 2980 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 2981 | |
| 2982 | SEARCHBOX |
| 2983 | --------- |