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 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 8 | For JavaScript plugin development, consult with |
| 9 | link:pg-plugin-dev.html[JavaScript Plugin Development] guide. |
Viktar Donich | 5055e8d | 2017-11-09 13:02:42 -0800 | [diff] [blame] | 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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 123 | Gerrit-BatchModule: tld.example.project.CoreModuleClassName |
Maxime Guerreiro | 5ccf0b9 | 2018-05-15 16:28:22 +0000 | [diff] [blame] | 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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [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 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 281 | public class MyInitStep implements InitStep { |
| 282 | private final String pluginName; |
| 283 | private final ConsoleUI ui; |
| 284 | private final AllProjectsConfig allProjectsConfig; |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 285 | |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 286 | @Inject |
| 287 | public MyInitStep(@PluginName String pluginName, ConsoleUI ui, |
| 288 | AllProjectsConfig allProjectsConfig) { |
| 289 | this.pluginName = pluginName; |
| 290 | this.ui = ui; |
| 291 | this.allProjectsConfig = allProjectsConfig; |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 292 | } |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 293 | |
| 294 | @Override |
| 295 | public void run() throws Exception { |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void postRun() throws Exception { |
| 300 | ui.message("\n"); |
| 301 | ui.header(pluginName + " Integration"); |
| 302 | boolean enabled = ui.yesno(true, "By default enabled for all projects"); |
| 303 | Config cfg = allProjectsConfig.load().getConfig(); |
| 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 | } |
Edwin Kempin | d4cfac1 | 2013-11-27 11:22:34 +0100 | [diff] [blame] | 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 | |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 709 | [source,java] |
| 710 | ---- |
| 711 | bind(ChangeOperatorFactory.class) |
| 712 | .annotatedWith(Exports.named("sample")) |
| 713 | .to(SampleOperator.class); |
| 714 | ---- |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 715 | |
| 716 | If this is registered in the `myplugin` plugin, then the resulting |
| 717 | operator will be named `sample_myplugin`. |
| 718 | |
| 719 | The search operator itself is implemented by ensuring that the |
| 720 | `create()` method of the class implementing the |
| 721 | `ChangeQueryBuilder.ChangeOperatorFactory` interface returns a |
| 722 | `Predicate<ChangeData>`. Here is a sample operator factory |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 723 | definition which creates a `MyPredicate`: |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 724 | |
| 725 | [source,java] |
| 726 | ---- |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 727 | public class SampleOperator |
| 728 | implements ChangeQueryBuilder.ChangeOperatorFactory { |
Dave Borowitz | b53581b | 2019-04-08 08:25:32 -0700 | [diff] [blame] | 729 | public static class MyPredicate extends PostFilterPredicate<ChangeData> { |
Martin Fick | 5f622291 | 2015-11-12 14:52:50 -0700 | [diff] [blame] | 730 | ... |
| 731 | } |
| 732 | |
| 733 | @Override |
| 734 | public Predicate<ChangeData> create(ChangeQueryBuilder builder, String value) |
| 735 | throws QueryParseException { |
| 736 | return new MyPredicate(value); |
| 737 | } |
| 738 | } |
| 739 | ---- |
| 740 | |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 741 | [[search_operands]] |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 742 | == Search Operands |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 743 | |
| 744 | Plugins can define new search operands to extend change searching. |
| 745 | Plugin methods implementing search operands (returning a |
| 746 | `Predicate<ChangeData>`), must be defined on a class implementing |
| 747 | one of the `ChangeQueryBuilder.ChangeOperandsFactory` interfaces |
Martin Fick | 08534c9 | 2020-08-05 18:09:41 -0500 | [diff] [blame] | 748 | (.e.g., ChangeQueryBuilder.ChangeHasOperandFactory or |
| 749 | ChangeQueryBuilder.ChangeIsOperandFactory). The specific |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 750 | `ChangeOperandFactory` class must also be bound to the `DynamicSet` from |
| 751 | a module's `configure()` method in the plugin. |
| 752 | |
| 753 | The new operand, when used in a search would appear as: |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 754 | `operatorName:operandName_pluginName` |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 755 | |
| 756 | A sample `ChangeHasOperandFactory` class implementing, and registering, a |
| 757 | new `has:sample_pluginName` operand is shown below: |
| 758 | |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 759 | [source, java] |
| 760 | ---- |
| 761 | public class SampleHasOperand implements ChangeHasOperandFactory { |
| 762 | public static class Module extends AbstractModule { |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 763 | @Override |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 764 | protected void configure() { |
| 765 | bind(ChangeHasOperandFactory.class) |
| 766 | .annotatedWith(Exports.named("sample") |
| 767 | .to(SampleHasOperand.class); |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 768 | } |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public Predicate<ChangeData> create(ChangeQueryBuilder builder) |
| 773 | throws QueryParseException { |
| 774 | return new HasSamplePredicate(); |
| 775 | } |
| 776 | } |
| 777 | ---- |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 778 | |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 779 | [[command_options]] |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 780 | == Command Options |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 781 | |
| 782 | Plugins can provide additional options for each of the gerrit ssh and the |
| 783 | REST API commands by implementing the DynamicBean interface and registering |
| 784 | it to a command class name in the plugin module's `configure()` method. The |
| 785 | plugin's name will be prepended to the name of each @Option annotation found |
| 786 | on the DynamicBean object provided by the plugin. The example below shows a |
| 787 | plugin that adds an option to log a value from the gerrit 'ban-commits' |
| 788 | ssh command. |
| 789 | |
| 790 | [source, java] |
| 791 | ---- |
| 792 | public class SshModule extends AbstractModule { |
Edwin Kempin | 8860e8e | 2018-05-08 11:19:37 +0200 | [diff] [blame] | 793 | private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 794 | |
| 795 | @Override |
| 796 | protected void configure() { |
| 797 | bind(DynamicOptions.DynamicBean.class) |
| 798 | .annotatedWith(Exports.named( |
| 799 | com.google.gerrit.sshd.commands.BanCommitCommand.class)) |
| 800 | .to(BanOptions.class); |
| 801 | } |
| 802 | |
| 803 | public static class BanOptions implements DynamicOptions.DynamicBean { |
| 804 | @Option(name = "--log", aliases = { "-l" }, usage = "Say Hello in the Log") |
| 805 | private void parse(String arg) { |
Edwin Kempin | 8860e8e | 2018-05-08 11:19:37 +0200 | [diff] [blame] | 806 | logger.atSevere().log("Say Hello in the Log %s", arg); |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 807 | } |
| 808 | } |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 809 | } |
Zac Livingston | 4f083a8 | 2016-05-20 12:38:43 -0600 | [diff] [blame] | 810 | ---- |
Craig Chapel | dba4e89 | 2016-11-14 09:25:17 -0700 | [diff] [blame] | 811 | |
Prudhvi Akhil Alahari | 86f0223 | 2020-10-30 23:51:33 +0530 | [diff] [blame] | 812 | To provide additional Guice bindings for options to a command in another classloader, bind a |
| 813 | ModulesClassNamesProvider which provides the name of your Modules needed for your DynamicBean |
| 814 | in the other classLoader. |
| 815 | |
| 816 | Do this by binding to the name of the command you are going to bind to and providing an |
| 817 | Iterable of Module names to instantiate and add to the Injector used to instantiate the |
| 818 | DynamicBean in the other classLoader. This interface supports running LifecycleListeners |
| 819 | which are defined by the Modules being provided. The duration of the lifecycle starts when |
| 820 | a ssh or http request starts and ends when the request completes. |
| 821 | |
| 822 | [source, java] |
| 823 | ---- |
Nasser Grainawi | 00ea3ef | 2021-08-25 09:41:28 -0600 | [diff] [blame] | 824 | bind(DynamicOptions.DynamicBean.class) |
| 825 | .annotatedWith(Exports.named( |
| 826 | "com.google.gerrit.plugins.otherplugin.command")) |
| 827 | .to(MyOptionsModulesClassNamesProvider.class); |
Prudhvi Akhil Alahari | 86f0223 | 2020-10-30 23:51:33 +0530 | [diff] [blame] | 828 | |
Nasser Grainawi | 00ea3ef | 2021-08-25 09:41:28 -0600 | [diff] [blame] | 829 | static class MyOptionsModulesClassNamesProvider implements DynamicOptions.ModulesClassNamesProvider { |
| 830 | @Override |
| 831 | public String getClassName() { |
| 832 | return "com.googlesource.gerrit.plugins.myplugin.CommandOptions"; |
Prudhvi Akhil Alahari | 86f0223 | 2020-10-30 23:51:33 +0530 | [diff] [blame] | 833 | } |
Nasser Grainawi | 00ea3ef | 2021-08-25 09:41:28 -0600 | [diff] [blame] | 834 | @Override |
| 835 | public Iterable<String> getModulesClassNames()() { |
| 836 | return "com.googlesource.gerrit.plugins.myplugin.MyOptionsModule"; |
| 837 | } |
| 838 | } |
Prudhvi Akhil Alahari | 86f0223 | 2020-10-30 23:51:33 +0530 | [diff] [blame] | 839 | ---- |
| 840 | |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 841 | === Calling Command Options === |
| 842 | |
| 843 | Within an OptionHandler, during the processing of an option, plugins can |
| 844 | provide and call extra parameters on the current command during parsing |
| 845 | simulating as if they had been passed from the command line originally. |
| 846 | |
| 847 | To call additional parameters from within an option handler, instantiate |
| 848 | the com.google.gerrit.util.cli.CmdLineParser.Parameters class with the |
| 849 | existing parameters, and then call callParameters() with the additional |
| 850 | parameters to be parsed. OptionHandlers may optionally pass this class to |
| 851 | other methods which may then both parse/consume more parameters and call |
| 852 | additional parameters. |
| 853 | |
Zac Livingston | 99a1ad1 | 2017-11-21 12:13:29 -0700 | [diff] [blame] | 854 | When calling command options not provided by your plugin, there is always |
| 855 | a risk that the options may not exist, perhaps because the options being |
| 856 | called are to be provided by another plugin, and said plugin is not |
| 857 | currently installed. To protect againt this situation, it is possible to |
| 858 | define an option as being dependent on other options using the |
| 859 | @RequiresOptions() annotation. If the required options are not all not |
| 860 | currently present, then the dependent option will not be available or |
| 861 | visible in the help. |
| 862 | |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 863 | The example below shows a plugin that adds a "--special" option (perhaps |
Zac Livingston | 99a1ad1 | 2017-11-21 12:13:29 -0700 | [diff] [blame] | 864 | for use with the Query command) that calls (and requires) the |
| 865 | "--format json" option. |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 866 | |
| 867 | [source, java] |
| 868 | ---- |
| 869 | public class JsonOutputOptionHandler<T> extends OptionHandler<T> { |
| 870 | protected com.google.gerrit.util.cli.CmdLineParser.MyParser myParser; |
| 871 | |
| 872 | public JsonOutputOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super T> setter) { |
| 873 | super(parser, option, setter); |
| 874 | myParser = (com.google.gerrit.util.cli.CmdLineParser.MyParser) owner; |
| 875 | } |
| 876 | |
| 877 | @Override |
| 878 | public int parseArguments(org.kohsuke.args4j.spi.Parameters params) throws CmdLineException { |
| 879 | new Parameters(params, myParser).callParameters("--format", "json"); |
| 880 | setter.addValue(true); |
| 881 | return 0; // we didn't consume any additional args |
| 882 | } |
| 883 | |
| 884 | @Override |
| 885 | public String getDefaultMetaVariable() { |
| 886 | ... |
| 887 | } |
| 888 | } |
| 889 | |
Zac Livingston | 99a1ad1 | 2017-11-21 12:13:29 -0700 | [diff] [blame] | 890 | @RequiresOptions("--format") |
Martin Fick | 57b553a | 2017-11-02 15:31:28 -0600 | [diff] [blame] | 891 | @Option( |
| 892 | name = "--special", |
| 893 | usage = "ouptut results using json", |
| 894 | handler = JsonOutputOptionHandler.class |
| 895 | ) |
| 896 | boolean json; |
| 897 | ---- |
| 898 | |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 899 | [[query_attributes]] |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 900 | == Change Attributes |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 901 | |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 902 | ==== ChangePluginDefinedInfoFactory |
| 903 | |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 904 | 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] | 905 | Query Change APIs by implementing the `ChangePluginDefinedInfoFactory` interface |
| 906 | and adding it to the `DynamicSet` in the plugin module's `configure()` method. |
| 907 | The new attribute(s) will be output under a `plugin` attribute in the change |
| 908 | output. This can be further controlled by registering a class containing @Option |
| 909 | declarations as a `DynamicBean`, annotated with the HTTP/SSH commands on |
| 910 | which the options should be available. |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 911 | |
Dave Borowitz | 4cf5828 | 2019-03-14 08:00:57 -0700 | [diff] [blame] | 912 | The example below shows a plugin that adds two attributes (`exampleName` and |
| 913 | `changeValue`), to the change query output, when the query command is provided |
| 914 | the `--myplugin-name--all` option. |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 915 | |
| 916 | [source, java] |
| 917 | ---- |
| 918 | public class Module extends AbstractModule { |
| 919 | @Override |
| 920 | protected void configure() { |
Dave Borowitz | 10745e1 | 2019-03-13 14:24:00 -0700 | [diff] [blame] | 921 | // Register attribute factory. |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 922 | DynamicSet.bind(binder(), ChangePluginDefinedInfoFactory.class) |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 923 | .to(AttributeFactory.class); |
Dave Borowitz | 10745e1 | 2019-03-13 14:24:00 -0700 | [diff] [blame] | 924 | |
| 925 | // Register options for GET /changes/X/change and /changes/X/detail. |
| 926 | bind(DynamicBean.class) |
| 927 | .annotatedWith(Exports.named(GetChange.class)) |
| 928 | .to(MyChangeOptions.class); |
| 929 | |
| 930 | // Register options for GET /changes/?q=... |
| 931 | bind(DynamicBean.class) |
| 932 | .annotatedWith(Exports.named(QueryChanges.class)) |
| 933 | .to(MyChangeOptions.class); |
| 934 | |
| 935 | // Register options for ssh gerrit query. |
| 936 | bind(DynamicBean.class) |
| 937 | .annotatedWith(Exports.named(Query.class)) |
| 938 | .to(MyChangeOptions.class); |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 942 | public class MyChangeOptions implements DynamicBean { |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 943 | @Option(name = "--all", usage = "Include plugin output") |
| 944 | public boolean all = false; |
| 945 | } |
| 946 | |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 947 | public class AttributeFactory implements ChangePluginDefinedInfoFactory { |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 948 | protected MyChangeOptions options; |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 949 | |
| 950 | public class PluginAttribute extends PluginDefinedInfo { |
| 951 | public String exampleName; |
| 952 | public String changeValue; |
| 953 | |
| 954 | public PluginAttribute(ChangeData c) { |
| 955 | this.exampleName = "Attribute Example"; |
| 956 | this.changeValue = Integer.toString(c.getId().get()); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | @Override |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 961 | public Map<Change.Id, PluginDefinedInfo> createPluginDefinedInfos( |
| 962 | Collection<ChangeData> cds, BeanProvider bp, String plugin) { |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 963 | if (options == null) { |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 964 | options = (MyChangeOptions) bp.getDynamicBean(plugin); |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 965 | } |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 966 | Map<Change.Id, PluginDefinedInfo> out = new HashMap<>(); |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 967 | if (options.all) { |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 968 | cds.forEach(cd -> out.put(cd.getId(), new PluginAttribute(cd))); |
| 969 | return out; |
Nasser Grainawi | ba5c740 | 2018-10-05 10:35:18 -0600 | [diff] [blame] | 970 | } |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 971 | return ImmutableMap.of(); |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | ---- |
| 975 | |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 976 | Example: |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 977 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 978 | $ 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] | 979 | { |
| 980 | "url" : "http://localhost:8080/1", |
| 981 | "plugins" : [ |
| 982 | { |
| 983 | "name" : "myplugin-name", |
| 984 | "exampleName" : "Attribute Example", |
| 985 | "changeValue" : "1" |
| 986 | } |
| 987 | ], |
| 988 | ... |
| 989 | } |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 990 | |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 991 | $ curl http://localhost:8080/changes/1?myplugin-name--all |
Dave Borowitz | 9c6ffc1 | 2019-03-14 08:09:48 -0700 | [diff] [blame] | 992 | { |
| 993 | "_number": 1, |
| 994 | ... |
| 995 | "plugins": [ |
| 996 | { |
| 997 | "name": "myplugin-name", |
| 998 | "example_name": "Attribute Example", |
| 999 | "change_value": "1" |
| 1000 | } |
| 1001 | ], |
| 1002 | ... |
| 1003 | } |
Zac Livingston | cffb2459 | 2016-11-13 09:08:08 -0700 | [diff] [blame] | 1004 | ---- |
| 1005 | |
Adithya Chakilam | 0ddd3d2 | 2020-09-03 14:46:17 -0500 | [diff] [blame] | 1006 | Runtime exceptions generated by the implementors of ChangePluginDefinedInfoFactory |
| 1007 | are encapsulated in PluginDefinedInfo objects which are part of SSH/REST query output. |
| 1008 | |
Adithya Chakilam | 260a910 | 2020-10-22 16:05:53 -0500 | [diff] [blame] | 1009 | Implementors of the `ChangePluginDefinedInfoFactory` interface should check whether |
| 1010 | they need to contribute to the link:#change-etag-computation[change ETag computation] |
| 1011 | to prevent callers using ETags from potentially seeing outdated plugin attributes. |
Edwin Kempin | 96915b7 | 2019-07-22 14:57:40 +0200 | [diff] [blame] | 1012 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1013 | [[simple-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1014 | == Simple Configuration in `gerrit.config` |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1015 | |
| 1016 | In Gerrit, global configuration is stored in the `gerrit.config` file. |
| 1017 | If a plugin needs global configuration, this configuration should be |
| 1018 | stored in a `plugin` subsection in the `gerrit.config` file. |
| 1019 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1020 | This approach of storing the plugin configuration is only suitable for |
| 1021 | plugins that have a simple configuration that only consists of |
| 1022 | key-value pairs. With this approach it is not possible to have |
| 1023 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1024 | configuration need to store their configuration in their |
| 1025 | link:#configuration[own configuration file] where they can make use of |
| 1026 | subsections. On the other hand storing the plugin configuration in a |
| 1027 | 'plugin' subsection in the `gerrit.config` file has the advantage that |
| 1028 | administrators have all configuration parameters in one file, instead |
| 1029 | of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1030 | |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1031 | To avoid conflicts with other plugins, it is recommended that plugins |
| 1032 | only use the `plugin` subsection with their own name. For example the |
| 1033 | `helloworld` plugin should store its configuration in the |
| 1034 | `plugin.helloworld` subsection: |
| 1035 | |
| 1036 | ---- |
| 1037 | [plugin "helloworld"] |
| 1038 | language = Latin |
| 1039 | ---- |
| 1040 | |
Sasa Zivkov | acdf533 | 2013-09-20 14:05:15 +0200 | [diff] [blame] | 1041 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1042 | plugin can easily access its configuration and there is no need for a |
| 1043 | plugin to parse the `gerrit.config` file on its own: |
| 1044 | |
| 1045 | [source,java] |
| 1046 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1047 | @Inject |
| 1048 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1049 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1050 | [...] |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1051 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 1052 | String language = cfg.getFromGerritConfig("helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1053 | .getString("language", "English"); |
Edwin Kempin | f7bfff8 | 2013-09-17 13:34:20 +0200 | [diff] [blame] | 1054 | ---- |
| 1055 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1056 | [[configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1057 | == Configuration in own config file |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1058 | |
| 1059 | Plugins can store their configuration in an own configuration file. |
| 1060 | This makes sense if the plugin configuration is rather complex and |
| 1061 | requires the usage of subsections. Plugins that have a simple |
| 1062 | key-value pair configuration can store their configuration in a |
| 1063 | link:#simple-configuration[`plugin` subsection of the `gerrit.config` |
| 1064 | file]. |
| 1065 | |
| 1066 | The plugin configuration file must be named after the plugin and must |
| 1067 | be located in the `etc` folder of the review site. For example a |
| 1068 | configuration file for a `default-reviewer` plugin could look like |
| 1069 | this: |
| 1070 | |
| 1071 | .$site_path/etc/default-reviewer.config |
| 1072 | ---- |
| 1073 | [branch "refs/heads/master"] |
| 1074 | reviewer = Project Owners |
| 1075 | reviewer = john.doe@example.com |
| 1076 | [match "file:^.*\.txt"] |
| 1077 | reviewer = My Info Developers |
| 1078 | ---- |
| 1079 | |
David Pursehouse | 5b47bc4 | 2016-07-22 11:00:25 +0900 | [diff] [blame] | 1080 | Plugins that have sensitive configuration settings can store those settings in |
| 1081 | an own secure configuration file. The plugin's secure configuration file must be |
| 1082 | named after the plugin and must be located in the `etc` folder of the review |
| 1083 | site. For example a secure configuration file for a `default-reviewer` plugin |
| 1084 | could look like this: |
| 1085 | |
| 1086 | .$site_path/etc/default-reviewer.secure.config |
| 1087 | ---- |
| 1088 | [auth] |
| 1089 | password = secret |
| 1090 | ---- |
| 1091 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1092 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 1093 | plugin can easily access its configuration: |
| 1094 | |
| 1095 | [source,java] |
| 1096 | ---- |
| 1097 | @Inject |
| 1098 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 1099 | |
| 1100 | [...] |
| 1101 | |
| 1102 | String[] reviewers = cfg.getGlobalPluginConfig("default-reviewer") |
| 1103 | .getStringList("branch", "refs/heads/master", "reviewer"); |
David Pursehouse | 5b47bc4 | 2016-07-22 11:00:25 +0900 | [diff] [blame] | 1104 | String password = cfg.getGlobalPluginConfig("default-reviewer") |
| 1105 | .getString("auth", null, "password"); |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1106 | ---- |
| 1107 | |
Edwin Kempin | 78ca094 | 2013-10-30 11:24:06 +0100 | [diff] [blame] | 1108 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1109 | [[simple-project-specific-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1110 | == Simple Project Specific Configuration in `project.config` |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1111 | |
| 1112 | In Gerrit, project specific configuration is stored in the project's |
| 1113 | `project.config` file on the `refs/meta/config` branch. If a plugin |
| 1114 | needs configuration on project level (e.g. to enable its functionality |
| 1115 | only for certain projects), this configuration should be stored in a |
| 1116 | `plugin` subsection in the project's `project.config` file. |
| 1117 | |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1118 | This approach of storing the plugin configuration is only suitable for |
| 1119 | plugins that have a simple configuration that only consists of |
| 1120 | key-value pairs. With this approach it is not possible to have |
| 1121 | subsections in the plugin configuration. Plugins that require a complex |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1122 | configuration need to store their configuration in their |
| 1123 | link:#project-specific-configuration[own configuration file] where they |
| 1124 | can make use of subsections. On the other hand storing the plugin |
| 1125 | configuration in a 'plugin' subsection in the `project.config` file has |
| 1126 | the advantage that project owners have all configuration parameters in |
| 1127 | one file, instead of having one configuration file per plugin. |
Edwin Kempin | c9b6860 | 2013-10-30 09:32:43 +0100 | [diff] [blame] | 1128 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1129 | To avoid conflicts with other plugins, it is recommended that plugins |
| 1130 | only use the `plugin` subsection with their own name. For example the |
| 1131 | `helloworld` plugin should store its configuration in the |
| 1132 | `plugin.helloworld` subsection: |
| 1133 | |
| 1134 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1135 | [plugin "helloworld"] |
| 1136 | enabled = true |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1137 | ---- |
| 1138 | |
| 1139 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 1140 | plugin can easily access its project specific configuration and there |
| 1141 | is no need for a plugin to parse the `project.config` file on its own: |
| 1142 | |
| 1143 | [source,java] |
| 1144 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1145 | @Inject |
| 1146 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1147 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1148 | [...] |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1149 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 1150 | boolean enabled = cfg.getFromProjectConfig(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1151 | .getBoolean("enabled", false); |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1152 | ---- |
| 1153 | |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1154 | It is also possible to get missing configuration parameters inherited |
| 1155 | from the parent projects: |
| 1156 | |
| 1157 | [source,java] |
| 1158 | ---- |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1159 | @Inject |
| 1160 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1161 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1162 | [...] |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1163 | |
Edwin Kempin | 122622d | 2013-10-29 16:45:44 +0100 | [diff] [blame] | 1164 | boolean enabled = cfg.getFromProjectConfigWithInheritance(project, "helloworld") |
David Pursehouse | 529ec25 | 2013-09-27 13:45:14 +0900 | [diff] [blame] | 1165 | .getBoolean("enabled", false); |
Edwin Kempin | ca7ad8e | 2013-09-16 16:43:05 +0200 | [diff] [blame] | 1166 | ---- |
| 1167 | |
Edwin Kempin | 7b2f4cc | 2013-08-26 15:44:19 +0200 | [diff] [blame] | 1168 | Project owners can edit the project configuration by fetching the |
| 1169 | `refs/meta/config` branch, editing the `project.config` file and |
| 1170 | pushing the commit back. |
| 1171 | |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 1172 | Plugin configuration values that are stored in the `project.config` |
| 1173 | file can be exposed in the ProjectInfoScreen to allow project owners |
| 1174 | to see and edit them from the UI. |
| 1175 | |
| 1176 | For this an instance of `ProjectConfigEntry` needs to be bound for each |
| 1177 | parameter. The export name must be a valid Git variable name. The |
| 1178 | variable name is case-insensitive, allows only alphanumeric characters |
| 1179 | and '-', and must start with an alphabetic character. |
| 1180 | |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 1181 | The example below shows how the parameters `plugin.helloworld.enabled` |
| 1182 | and `plugin.helloworld.language` are bound to be editable from the |
David Pursehouse | a1d633b | 2014-05-02 17:21:02 +0900 | [diff] [blame] | 1183 | Web UI. For the parameter `plugin.helloworld.enabled` "Enable Greeting" |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 1184 | is provided as display name and the default value is set to `true`. |
| 1185 | For the parameter `plugin.helloworld.language` "Preferred Language" |
| 1186 | is provided as display name and "en" is set as default value. |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 1187 | |
| 1188 | [source,java] |
| 1189 | ---- |
| 1190 | class Module extends AbstractModule { |
| 1191 | @Override |
| 1192 | protected void configure() { |
| 1193 | bind(ProjectConfigEntry.class) |
Edwin Kempin | a6c1c45 | 2013-11-28 16:55:22 +0100 | [diff] [blame] | 1194 | .annotatedWith(Exports.named("enabled")) |
| 1195 | .toInstance(new ProjectConfigEntry("Enable Greeting", true)); |
| 1196 | bind(ProjectConfigEntry.class) |
Edwin Kempin | 9ce4f55 | 2013-11-15 16:00:00 +0100 | [diff] [blame] | 1197 | .annotatedWith(Exports.named("language")) |
| 1198 | .toInstance(new ProjectConfigEntry("Preferred Language", "en")); |
| 1199 | } |
| 1200 | } |
| 1201 | ---- |
| 1202 | |
Edwin Kempin | b64d397 | 2013-11-17 18:55:48 +0100 | [diff] [blame] | 1203 | By overwriting the `onUpdate` method of `ProjectConfigEntry` plugins |
| 1204 | can be notified when this configuration parameter is updated on a |
| 1205 | project. |
| 1206 | |
Janice Agustin | e5a9d01 | 2015-08-24 09:05:56 -0400 | [diff] [blame] | 1207 | [[configuring-groups]] |
| 1208 | === Referencing groups in `project.config` |
| 1209 | |
| 1210 | Plugins can refer to groups so that when they are renamed, the project |
| 1211 | 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] | 1212 | 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] | 1213 | |
| 1214 | ---- |
Hugo Arès | 532e0a3 | 2017-06-16 09:31:08 -0400 | [diff] [blame] | 1215 | group group_name |
Janice Agustin | e5a9d01 | 2015-08-24 09:05:56 -0400 | [diff] [blame] | 1216 | ---- |
| 1217 | |
Hugo Arès | 532e0a3 | 2017-06-16 09:31:08 -0400 | [diff] [blame] | 1218 | The file `groups` must also contains the mapping of the group name and its UUID, |
| 1219 | refer to link:config-project-config.html#file-groups[file groups] |
| 1220 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1221 | [[project-specific-configuration]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1222 | == Project Specific Configuration in own config file |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1223 | |
| 1224 | Plugins can store their project specific configuration in an own |
| 1225 | configuration file in the projects `refs/meta/config` branch. |
| 1226 | This makes sense if the plugins project specific configuration is |
| 1227 | rather complex and requires the usage of subsections. Plugins that |
| 1228 | have a simple key-value pair configuration can store their project |
| 1229 | specific configuration in a link:#simple-project-specific-configuration[ |
| 1230 | `plugin` subsection of the `project.config` file]. |
| 1231 | |
| 1232 | The plugin configuration file in the `refs/meta/config` branch must be |
| 1233 | named after the plugin. For example a configuration file for a |
| 1234 | `default-reviewer` plugin could look like this: |
| 1235 | |
| 1236 | .default-reviewer.config |
| 1237 | ---- |
| 1238 | [branch "refs/heads/master"] |
| 1239 | reviewer = Project Owners |
| 1240 | reviewer = john.doe@example.com |
| 1241 | [match "file:^.*\.txt"] |
| 1242 | reviewer = My Info Developers |
| 1243 | ---- |
| 1244 | |
| 1245 | Via the `com.google.gerrit.server.config.PluginConfigFactory` class a |
| 1246 | plugin can easily access its project specific configuration: |
| 1247 | |
| 1248 | [source,java] |
| 1249 | ---- |
| 1250 | @Inject |
| 1251 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 1252 | |
| 1253 | [...] |
| 1254 | |
| 1255 | String[] reviewers = cfg.getProjectPluginConfig(project, "default-reviewer") |
| 1256 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 1257 | ---- |
| 1258 | |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 1259 | It is also possible to get missing configuration parameters inherited |
| 1260 | from the parent projects: |
| 1261 | |
| 1262 | [source,java] |
| 1263 | ---- |
| 1264 | @Inject |
| 1265 | private com.google.gerrit.server.config.PluginConfigFactory cfg; |
| 1266 | |
| 1267 | [...] |
| 1268 | |
David Ostrovsky | 468e4c3 | 2014-03-22 06:05:35 -0700 | [diff] [blame] | 1269 | String[] reviewers = cfg.getProjectPluginConfigWithInheritance(project, "default-reviewer") |
Edwin Kempin | 762da38 | 2013-10-30 14:50:01 +0100 | [diff] [blame] | 1270 | .getStringList("branch", "refs/heads/master", "reviewer"); |
| 1271 | ---- |
| 1272 | |
Edwin Kempin | 705f284 | 2013-10-30 14:25:31 +0100 | [diff] [blame] | 1273 | Project owners can edit the project configuration by fetching the |
| 1274 | `refs/meta/config` branch, editing the `<plugin-name>.config` file and |
| 1275 | pushing the commit back. |
| 1276 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1277 | == React on changes in project configuration |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1278 | |
| 1279 | If a plugin wants to react on changes in the project configuration, it |
| 1280 | can implement a `GitReferenceUpdatedListener` and filter on events for |
| 1281 | the `refs/meta/config` branch: |
| 1282 | |
| 1283 | [source,java] |
| 1284 | ---- |
| 1285 | public class MyListener implements GitReferenceUpdatedListener { |
| 1286 | |
| 1287 | private final MetaDataUpdate.Server metaDataUpdateFactory; |
| 1288 | |
| 1289 | @Inject |
| 1290 | MyListener(MetaDataUpdate.Server metaDataUpdateFactory) { |
| 1291 | this.metaDataUpdateFactory = metaDataUpdateFactory; |
| 1292 | } |
| 1293 | |
| 1294 | @Override |
| 1295 | public void onGitReferenceUpdated(Event event) { |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1296 | if (event.getRefName().equals(RefNames.REFS_CONFIG)) { |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1297 | Project.NameKey p = new Project.NameKey(event.getProjectName()); |
| 1298 | try { |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1299 | ProjectConfig oldCfg = parseConfig(p, event.getOldObjectId()); |
| 1300 | ProjectConfig newCfg = parseConfig(p, event.getNewObjectId()); |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1301 | |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1302 | if (oldCfg != null && newCfg != null |
| 1303 | && !oldCfg.getProject().getSubmitType().equals(newCfg.getProject().getSubmitType())) { |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1304 | // submit type has changed |
| 1305 | ... |
| 1306 | } |
| 1307 | } catch (IOException | ConfigInvalidException e) { |
| 1308 | ... |
| 1309 | } |
| 1310 | } |
| 1311 | } |
Edwin Kempin | a951ba5 | 2014-01-03 14:07:28 +0100 | [diff] [blame] | 1312 | |
| 1313 | private ProjectConfig parseConfig(Project.NameKey p, String idStr) |
| 1314 | throws IOException, ConfigInvalidException, RepositoryNotFoundException { |
| 1315 | ObjectId id = ObjectId.fromString(idStr); |
| 1316 | if (ObjectId.zeroId().equals(id)) { |
| 1317 | return null; |
| 1318 | } |
| 1319 | return ProjectConfig.read(metaDataUpdateFactory.create(p), id); |
| 1320 | } |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1321 | } |
| 1322 | ---- |
| 1323 | |
Fabio Ponciroli | 214b896 | 2020-05-07 13:22:00 +0200 | [diff] [blame] | 1324 | == Trace Event origin |
| 1325 | |
| 1326 | When plugins are installed in a multi-master setups it can be useful to know |
| 1327 | the Gerrit `instanceId` of the server that has generated an Event. |
| 1328 | |
| 1329 | E.g. A plugin that sends an instance message for every comment on a change may |
| 1330 | want to react only if the event is generated on the local Gerrit master, for |
| 1331 | avoiding duplicating the notifications. |
| 1332 | |
| 1333 | If link:config-gerrit.html[instanceId] is set, each Event will contain its |
| 1334 | origin in the `instanceId` field. |
| 1335 | |
| 1336 | Here and example of ref-updated JSON event payload with `instanceId`: |
| 1337 | |
| 1338 | [source,json] |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1339 | ---- |
Fabio Ponciroli | 214b896 | 2020-05-07 13:22:00 +0200 | [diff] [blame] | 1340 | { |
| 1341 | "submitter": { |
| 1342 | "name": "Administrator", |
| 1343 | "email": "admin@example.com", |
| 1344 | "username": "admin" |
| 1345 | }, |
| 1346 | "refUpdate": { |
| 1347 | "oldRev": "a69fc95c7aad5ad41c618d31548b8af835d2959a", |
| 1348 | "newRev": "31da6556d638a74e5370b62f83e8007f94abb7c6", |
| 1349 | "refName": "refs/changes/01/1/meta", |
| 1350 | "project": "test" |
| 1351 | }, |
| 1352 | "type": "ref-updated", |
| 1353 | "eventCreatedOn": 1588849085, |
| 1354 | "instanceId": "instance1" |
| 1355 | } |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1356 | ---- |
Edwin Kempin | a46b6c9 | 2013-12-04 21:05:24 +0100 | [diff] [blame] | 1357 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1358 | [[capabilities]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1359 | == Plugin Owned Capabilities |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1360 | |
| 1361 | Plugins may provide their own capabilities and restrict usage of SSH |
Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 1362 | commands or `UiAction` to the users who are granted those capabilities. |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1363 | |
| 1364 | Plugins define the capabilities by overriding the `CapabilityDefinition` |
| 1365 | abstract class: |
| 1366 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1367 | [source,java] |
| 1368 | ---- |
| 1369 | public class PrintHelloCapability extends CapabilityDefinition { |
| 1370 | @Override |
| 1371 | public String getDescription() { |
| 1372 | return "Print Hello"; |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1373 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1374 | } |
| 1375 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1376 | |
Dariusz Luksza | 112d93a | 2014-06-01 16:52:23 +0200 | [diff] [blame] | 1377 | If no Guice modules are declared in the manifest, capability may |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1378 | use auto-registration by providing an `@Export` annotation: |
| 1379 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1380 | [source,java] |
| 1381 | ---- |
| 1382 | @Export("printHello") |
| 1383 | public class PrintHelloCapability extends CapabilityDefinition { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1384 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1385 | } |
| 1386 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1387 | |
| 1388 | Otherwise the capability must be bound in a plugin module: |
| 1389 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1390 | [source,java] |
| 1391 | ---- |
| 1392 | public class HelloWorldModule extends AbstractModule { |
| 1393 | @Override |
| 1394 | protected void configure() { |
| 1395 | bind(CapabilityDefinition.class) |
| 1396 | .annotatedWith(Exports.named("printHello")) |
| 1397 | .to(PrintHelloCapability.class); |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1398 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1399 | } |
| 1400 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1401 | |
| 1402 | 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] | 1403 | 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] | 1404 | this capability in the usual way, using the `RequiresCapability` annotation: |
| 1405 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1406 | [source,java] |
| 1407 | ---- |
| 1408 | @RequiresCapability("printHello") |
| 1409 | @CommandMetaData(name="print", description="Print greeting in different languages") |
| 1410 | public final class PrintHelloWorldCommand extends SshCommand { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1411 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1412 | } |
| 1413 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1414 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1415 | Or with `UiAction`: |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1416 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1417 | [source,java] |
| 1418 | ---- |
| 1419 | @RequiresCapability("printHello") |
| 1420 | public class SayHelloAction extends UiAction<RevisionResource> |
| 1421 | implements RestModifyView<RevisionResource, SayHelloAction.Input> { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1422 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1423 | } |
| 1424 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1425 | |
| 1426 | Capability scope was introduced to differentiate between plugin-owned |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 1427 | capabilities and core capabilities. Per default the scope of the |
| 1428 | `@RequiresCapability` annotation is `CapabilityScope.CONTEXT`, that means: |
| 1429 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1430 | * when `@RequiresCapability` is used within a plugin the scope of the |
| 1431 | capability is assumed to be that plugin. |
David Pursehouse | bf05334 | 2013-09-05 14:55:29 +0900 | [diff] [blame] | 1432 | |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1433 | * If `@RequiresCapability` is used within the core Gerrit Code Review server |
| 1434 | (and thus is outside of a plugin) the scope is the core server and will use |
| 1435 | the `GlobalCapability` known to Gerrit Code Review server. |
| 1436 | |
| 1437 | If a plugin needs to use a core capability name (e.g. "administrateServer") |
| 1438 | this can be specified by setting `scope = CapabilityScope.CORE`: |
| 1439 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1440 | [source,java] |
| 1441 | ---- |
| 1442 | @RequiresCapability(value = "administrateServer", scope = |
| 1443 | CapabilityScope.CORE) |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1444 | [...] |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1445 | ---- |
David Ostrovsky | 7066cc0 | 2013-06-15 14:46:23 +0200 | [diff] [blame] | 1446 | |
Edwin Kempin | 79ec493 | 2020-12-01 15:51:29 +0100 | [diff] [blame] | 1447 | [post_review_extensions] |
| 1448 | == Post Review Extensions |
| 1449 | |
| 1450 | By implementing the `com.google.gerrit.server.restapi.change.OnPostReview` |
| 1451 | interface plugins can extend the change message that is being posted when the |
David Ostrovsky | ce35159 | 2021-05-03 21:14:05 +0200 | [diff] [blame] | 1452 | link:rest-api-changes.html#set-review[post review] REST endpoint is invoked. |
Edwin Kempin | 79ec493 | 2020-12-01 15:51:29 +0100 | [diff] [blame] | 1453 | |
| 1454 | This is useful if certain approvals have a special meaning (e.g. custom logic |
| 1455 | that is implemented in Prolog submit rules, signal for triggering an action |
| 1456 | like running CI etc.), as it allows the plugin to tell users about this meaning |
| 1457 | in the change message. This makes the effect of a given approval more |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 1458 | transparent to the user. |
Zac Livingston | e7f3d1a | 2017-03-01 12:47:37 -0700 | [diff] [blame] | 1459 | |
David Ostrovsky | ce35159 | 2021-05-03 21:14:05 +0200 | [diff] [blame] | 1460 | [[ui_extension]] |
| 1461 | == UI Extension |
Zac Livingston | e7f3d1a | 2017-03-01 12:47:37 -0700 | [diff] [blame] | 1462 | |
Edwin Kempin | 52f79ac | 2015-07-07 16:37:50 +0200 | [diff] [blame] | 1463 | [[actions]] |
| 1464 | === Actions |
| 1465 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1466 | Plugins can contribute UI actions on core Gerrit pages. This is useful |
| 1467 | for workflow customization or exposing plugin functionality through the |
| 1468 | UI in addition to SSH commands and the REST API. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1469 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1470 | For instance a plugin to integrate Jira with Gerrit changes may |
| 1471 | contribute a "File bug" button to allow filing a bug from the change |
| 1472 | page or plugins to integrate continuous integration systems may |
| 1473 | contribute a "Schedule" button to allow a CI build to be scheduled |
| 1474 | manually from the patch set panel. |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1475 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1476 | Two different places on core Gerrit pages are supported: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1477 | |
| 1478 | * Change screen |
| 1479 | * Project info screen |
| 1480 | |
| 1481 | Plugins contribute UI actions by implementing the `UiAction` interface: |
| 1482 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1483 | [source,java] |
| 1484 | ---- |
| 1485 | @RequiresCapability("printHello") |
| 1486 | class HelloWorldAction implements UiAction<RevisionResource>, |
| 1487 | RestModifyView<RevisionResource, HelloWorldAction.Input> { |
| 1488 | static class Input { |
| 1489 | boolean french; |
| 1490 | String message; |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1491 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1492 | |
| 1493 | private Provider<CurrentUser> user; |
| 1494 | |
| 1495 | @Inject |
| 1496 | HelloWorldAction(Provider<CurrentUser> user) { |
| 1497 | this.user = user; |
| 1498 | } |
| 1499 | |
| 1500 | @Override |
| 1501 | public String apply(RevisionResource rev, Input input) { |
| 1502 | final String greeting = input.french |
| 1503 | ? "Bonjour" |
| 1504 | : "Hello"; |
| 1505 | return String.format("%s %s from change %s, patch set %d!", |
| 1506 | greeting, |
| 1507 | Strings.isNullOrEmpty(input.message) |
| 1508 | ? Objects.firstNonNull(user.get().getUserName(), "world") |
| 1509 | : input.message, |
| 1510 | rev.getChange().getId().toString(), |
| 1511 | rev.getPatchSet().getPatchSetId()); |
| 1512 | } |
| 1513 | |
| 1514 | @Override |
| 1515 | public Description getDescription( |
| 1516 | RevisionResource resource) { |
| 1517 | return new Description() |
| 1518 | .setLabel("Say hello") |
| 1519 | .setTitle("Say hello in different languages"); |
| 1520 | } |
| 1521 | } |
| 1522 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1523 | |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1524 | Sometimes plugins may want to be able to change the state of a patch set or |
| 1525 | change in the `UiAction.apply()` method and reflect these changes on the core |
| 1526 | UI. For example a buildbot plugin which exposes a 'Schedule' button on the |
| 1527 | patch set panel may want to disable that button after the build was scheduled |
| 1528 | and update the tooltip of that button. But because of Gerrit's caching |
| 1529 | strategy the following must be taken into consideration. |
| 1530 | |
| 1531 | The browser is allowed to cache the `UiAction` information until something on |
| 1532 | the change is modified. More accurately the change row needs to be modified in |
| 1533 | the database to have a more recent `lastUpdatedOn` or a new `rowVersion`, or |
| 1534 | the +refs/meta/config+ of the project or any parents needs to change to a new |
| 1535 | SHA-1. The ETag SHA-1 computation code can be found in the |
| 1536 | `ChangeResource.getETag()` method. |
| 1537 | |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1538 | 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] | 1539 | |
| 1540 | [source,java] |
| 1541 | ---- |
| 1542 | @Override |
| 1543 | public Object apply(RevisionResource rcrs, Input in) { |
| 1544 | // schedule a build |
| 1545 | [...] |
| 1546 | // update change |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1547 | try (BatchUpdate bu = batchUpdateFactory.create( |
Dave Borowitz | ada289c | 2018-12-18 13:24:14 -0800 | [diff] [blame] | 1548 | project.getNameKey(), user, TimeUtil.nowTs())) { |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1549 | bu.addOp(change.getId(), new BatchUpdate.Op() { |
| 1550 | @Override |
Dave Borowitz | b91cf22 | 2017-03-10 13:11:59 -0500 | [diff] [blame] | 1551 | public boolean updateChange(ChangeContext ctx) { |
Edwin Kempin | e2d06b0 | 2016-02-17 18:34:17 +0100 | [diff] [blame] | 1552 | return true; |
| 1553 | } |
| 1554 | }); |
| 1555 | bu.execute(); |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1556 | } |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1557 | [...] |
David Ostrovsky | 450eefe | 2013-10-21 21:18:11 +0200 | [diff] [blame] | 1558 | } |
| 1559 | ---- |
| 1560 | |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1561 | `UiAction` must be bound in a plugin module: |
| 1562 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1563 | [source,java] |
| 1564 | ---- |
| 1565 | public class Module extends AbstractModule { |
| 1566 | @Override |
| 1567 | protected void configure() { |
| 1568 | install(new RestApiModule() { |
| 1569 | @Override |
| 1570 | protected void configure() { |
| 1571 | post(REVISION_KIND, "say-hello") |
| 1572 | .to(HelloWorldAction.class); |
| 1573 | } |
| 1574 | }); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1575 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1576 | } |
| 1577 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1578 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1579 | The module above must be declared in the `pom.xml` for Maven driven |
| 1580 | plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1581 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1582 | [source,xml] |
| 1583 | ---- |
| 1584 | <manifestEntries> |
| 1585 | <Gerrit-Module>com.googlesource.gerrit.plugins.cookbook.Module</Gerrit-Module> |
| 1586 | </manifestEntries> |
| 1587 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1588 | |
David Ostrovsky | fdbfcad | 2016-11-15 06:35:29 -0800 | [diff] [blame] | 1589 | or in the `BUILD` configuration file for Bazel driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1590 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1591 | [source,python] |
| 1592 | ---- |
| 1593 | manifest_entries = [ |
| 1594 | 'Gerrit-Module: com.googlesource.gerrit.plugins.cookbook.Module', |
| 1595 | ] |
| 1596 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1597 | |
| 1598 | In some use cases more user input must be gathered, for that `UiAction` can be |
| 1599 | combined with the JavaScript API. This would display a small popup near the |
| 1600 | activation button to gather additional input from the user. The JS file is |
| 1601 | typically put in the `static` folder within the plugin's directory: |
| 1602 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1603 | [source,javascript] |
| 1604 | ---- |
| 1605 | Gerrit.install(function(self) { |
| 1606 | function onSayHello(c) { |
| 1607 | var f = c.textfield(); |
| 1608 | var t = c.checkbox(); |
| 1609 | var b = c.button('Say hello', {onclick: function(){ |
| 1610 | c.call( |
| 1611 | {message: f.value, french: t.checked}, |
| 1612 | function(r) { |
| 1613 | c.hide(); |
| 1614 | window.alert(r); |
| 1615 | c.refresh(); |
| 1616 | }); |
| 1617 | }}); |
| 1618 | c.popup(c.div( |
| 1619 | c.prependLabel('Greeting message', f), |
| 1620 | c.br(), |
| 1621 | c.label(t, 'french'), |
| 1622 | c.br(), |
| 1623 | b)); |
| 1624 | f.focus(); |
| 1625 | } |
| 1626 | self.onAction('revision', 'say-hello', onSayHello); |
| 1627 | }); |
| 1628 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1629 | |
| 1630 | The JS module must be exposed as a `WebUiPlugin` and bound as |
| 1631 | an HTTP Module: |
| 1632 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1633 | [source,java] |
| 1634 | ---- |
Dave Borowitz | 20f4e8a | 2019-03-13 14:19:15 -0700 | [diff] [blame] | 1635 | public class HttpModule extends ServletModule { |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1636 | @Override |
| 1637 | protected void configureServlets() { |
| 1638 | DynamicSet.bind(binder(), WebUiPlugin.class) |
| 1639 | .toInstance(new JavaScriptPlugin("hello.js")); |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1640 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1641 | } |
| 1642 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1643 | |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1644 | The HTTP module above must be declared in the `pom.xml` for Maven |
| 1645 | driven plugins: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1646 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1647 | [source,xml] |
| 1648 | ---- |
| 1649 | <manifestEntries> |
| 1650 | <Gerrit-HttpModule>com.googlesource.gerrit.plugins.cookbook.HttpModule</Gerrit-HttpModule> |
| 1651 | </manifestEntries> |
| 1652 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1653 | |
David Ostrovsky | fdbfcad | 2016-11-15 06:35:29 -0800 | [diff] [blame] | 1654 | or in the `BUILD` configuration file for Bazel driven plugins |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1655 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1656 | [source,python] |
| 1657 | ---- |
| 1658 | manifest_entries = [ |
| 1659 | 'Gerrit-HttpModule: com.googlesource.gerrit.plugins.cookbook.HttpModule', |
| 1660 | ] |
| 1661 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1662 | |
| 1663 | If `UiAction` is annotated with the `@RequiresCapability` annotation, then the |
| 1664 | capability check is done during the `UiAction` gathering, so the plugin author |
| 1665 | doesn't have to set `UiAction.Description.setVisible()` explicitly in this |
| 1666 | case. |
| 1667 | |
David Pursehouse | a61ee50 | 2016-09-06 16:27:09 +0900 | [diff] [blame] | 1668 | The following prerequisites must be met, to satisfy the capability check: |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1669 | |
| 1670 | * user is authenticated |
Edwin Kempin | 7afa73c | 2013-11-08 07:48:47 +0100 | [diff] [blame] | 1671 | * 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] | 1672 | * user is a member of a group which has the required capability |
| 1673 | |
| 1674 | The `apply` method is called when the button is clicked. If `UiAction` is |
| 1675 | combined with JavaScript API (its own JavaScript function is provided), |
| 1676 | then a popup dialog is normally opened to gather additional user input. |
| 1677 | A new button is placed on the popup dialog to actually send the request. |
| 1678 | |
| 1679 | Every `UiAction` exposes a REST API endpoint. The endpoint from the example above |
| 1680 | can be accessed from any REST client, i. e.: |
| 1681 | |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 1682 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1683 | $ curl -X POST -H "Content-Type: application/json" \ |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1684 | -d '{message: "François", french: true}' \ |
Han-Wen Nienhuys | 84d830b | 2017-02-15 16:36:04 +0100 | [diff] [blame] | 1685 | --user joe:secret \ |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1686 | http://host:port/a/changes/1/revisions/1/cookbook~say-hello |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1687 | "Bonjour François from change 1, patch set 1!" |
Michael Ochmann | b99feab | 2016-07-06 14:10:22 +0200 | [diff] [blame] | 1688 | ---- |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 1689 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1690 | 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] | 1691 | particularly useful for `DELETE` requests: |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1692 | |
| 1693 | [source,java] |
| 1694 | ---- |
| 1695 | public class Module extends AbstractModule { |
| 1696 | @Override |
| 1697 | protected void configure() { |
| 1698 | install(new RestApiModule() { |
| 1699 | @Override |
| 1700 | protected void configure() { |
| 1701 | delete(PROJECT_KIND) |
| 1702 | .to(DeleteProject.class); |
| 1703 | } |
| 1704 | }); |
| 1705 | } |
| 1706 | } |
| 1707 | ---- |
| 1708 | |
David Pursehouse | 4224582 | 2013-09-24 09:48:20 +0900 | [diff] [blame] | 1709 | For a `UiAction` bound this way, a JS API function can be provided. |
| 1710 | |
| 1711 | Currently only one restriction exists: per plugin only one `UiAction` |
David Ostrovsky | c6d19ed | 2013-09-20 21:30:18 +0200 | [diff] [blame] | 1712 | can be bound per resource without view name. To define a JS function |
| 1713 | for the `UiAction`, "/" must be used as the name: |
| 1714 | |
| 1715 | [source,javascript] |
| 1716 | ---- |
| 1717 | Gerrit.install(function(self) { |
| 1718 | function onDeleteProject(c) { |
| 1719 | [...] |
| 1720 | } |
| 1721 | self.onAction('project', '/', onDeleteProject); |
| 1722 | }); |
| 1723 | ---- |
| 1724 | |
Dave Borowitz | f1790ce | 2016-11-14 12:26:12 -0800 | [diff] [blame] | 1725 | |
| 1726 | [[action-visitor]] |
| 1727 | === Action Visitors |
| 1728 | |
| 1729 | In addition to providing new actions, plugins can have fine-grained control |
| 1730 | over the link:rest-api-changes.html#action-info[ActionInfo] map, modifying or |
| 1731 | removing existing actions, including those contributed by core. |
| 1732 | |
| 1733 | Visitors are provided the link:rest-api-changes.html#action-info[ActionInfo], |
| 1734 | which is mutable, along with copies of the |
| 1735 | link:rest-api-changes.html#change-info[ChangeInfo] and |
| 1736 | link:rest-api-changes.html#revision-info[RevisionInfo]. They can modify the |
| 1737 | action, or return `false` to exclude it from the resulting map. |
| 1738 | |
| 1739 | These operations only affect the action buttons that are displayed in the UI; |
| 1740 | the underlying REST API endpoints are not affected. Multiple plugins may |
| 1741 | implement the visitor interface, but the order in which they are run is |
| 1742 | undefined. |
| 1743 | |
| 1744 | For example, to exclude "Cherry-Pick" only from certain projects, and rename |
| 1745 | "Abandon": |
| 1746 | |
| 1747 | [source,java] |
| 1748 | ---- |
| 1749 | public class MyActionVisitor implements ActionVisitor { |
| 1750 | @Override |
| 1751 | public boolean visit(String name, ActionInfo actionInfo, |
| 1752 | ChangeInfo changeInfo) { |
| 1753 | if (name.equals("abandon")) { |
| 1754 | actionInfo.label = "Drop"; |
| 1755 | } |
| 1756 | return true; |
| 1757 | } |
| 1758 | |
| 1759 | @Override |
| 1760 | public boolean visit(String name, ActionInfo actionInfo, |
| 1761 | ChangeInfo changeInfo, RevisionInfo revisionInfo) { |
| 1762 | if (project.startsWith("some-team/") && name.equals("cherrypick")) { |
| 1763 | return false; |
| 1764 | } |
| 1765 | return true; |
| 1766 | } |
| 1767 | } |
| 1768 | ---- |
| 1769 | |
| 1770 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1771 | [[top-menu-extensions]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1772 | == Top Menu Extensions |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1773 | |
| 1774 | Plugins can contribute items to Gerrit's top menu. |
| 1775 | |
| 1776 | A single top menu extension can have multiple elements and will be put as |
| 1777 | the last element in Gerrit's top menu. |
| 1778 | |
| 1779 | Plugins define the top menu entries by implementing `TopMenu` interface: |
| 1780 | |
| 1781 | [source,java] |
| 1782 | ---- |
| 1783 | public class MyTopMenuExtension implements TopMenu { |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1784 | @Override |
| 1785 | public List<MenuEntry> getEntries() { |
| 1786 | return Lists.newArrayList( |
| 1787 | new MenuEntry("Top Menu Entry", Lists.newArrayList( |
| 1788 | new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1789 | } |
| 1790 | } |
| 1791 | ---- |
| 1792 | |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1793 | Plugins can also add additional menu items to Gerrit's top menu entries |
| 1794 | by defining a `MenuEntry` that has the same name as a Gerrit top menu |
| 1795 | entry: |
| 1796 | |
| 1797 | [source,java] |
| 1798 | ---- |
| 1799 | public class MyTopMenuExtension implements TopMenu { |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1800 | @Override |
| 1801 | public List<MenuEntry> getEntries() { |
| 1802 | return Lists.newArrayList( |
Dariusz Luksza | 2d3afab | 2013-10-01 11:07:13 +0200 | [diff] [blame] | 1803 | new MenuEntry(GerritTopMenu.PROJECTS, Lists.newArrayList( |
Edwin Kempin | 77f2324 | 2013-09-30 14:53:20 +0200 | [diff] [blame] | 1804 | new MenuItem("Browse Repositories", "https://gerrit.googlesource.com/")))); |
| 1805 | } |
| 1806 | } |
| 1807 | ---- |
| 1808 | |
Dariusz Luksza | e8de74f | 2014-06-04 19:39:20 +0200 | [diff] [blame] | 1809 | `MenuItems` that are bound for the `MenuEntry` with the name |
| 1810 | `GerritTopMenu.PROJECTS` can contain a `${projectName}` placeholder |
| 1811 | which is automatically replaced by the actual project name. |
| 1812 | |
| 1813 | E.g. plugins may register an link:#http[HTTP Servlet] to handle project |
| 1814 | specific requests and add an menu item for this: |
| 1815 | |
| 1816 | [source,java] |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1817 | ---- |
| 1818 | new MenuItem("My Screen", "/plugins/myplugin/project/${projectName}"); |
| 1819 | ---- |
Dariusz Luksza | e8de74f | 2014-06-04 19:39:20 +0200 | [diff] [blame] | 1820 | |
| 1821 | This also enables plugins to provide menu items for project aware |
| 1822 | screens: |
| 1823 | |
| 1824 | [source,java] |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 1825 | ---- |
| 1826 | new MenuItem("My Screen", "/x/my-screen/for/${projectName}"); |
| 1827 | ---- |
Dariusz Luksza | e8de74f | 2014-06-04 19:39:20 +0200 | [diff] [blame] | 1828 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1829 | If no Guice modules are declared in the manifest, the top menu extension may use |
| 1830 | auto-registration by providing an `@Listen` annotation: |
| 1831 | |
| 1832 | [source,java] |
| 1833 | ---- |
| 1834 | @Listen |
| 1835 | public class MyTopMenuExtension implements TopMenu { |
David Pursehouse | d128c89 | 2013-10-22 21:52:21 +0900 | [diff] [blame] | 1836 | [...] |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1837 | } |
| 1838 | ---- |
| 1839 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1840 | Otherwise the top menu extension must be bound in the plugin module used |
| 1841 | for the Gerrit system injector (Gerrit-Module entry in MANIFEST.MF): |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1842 | |
| 1843 | [source,java] |
| 1844 | ---- |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1845 | package com.googlesource.gerrit.plugins.helloworld; |
| 1846 | |
Dariusz Luksza | 589ba00aa | 2013-05-07 17:21:23 +0200 | [diff] [blame] | 1847 | public class HelloWorldModule extends AbstractModule { |
| 1848 | @Override |
| 1849 | protected void configure() { |
| 1850 | DynamicSet.bind(binder(), TopMenu.class).to(MyTopMenuExtension.class); |
| 1851 | } |
| 1852 | } |
| 1853 | ---- |
| 1854 | |
Luca Milanesio | cb23040 | 2013-10-11 08:49:56 +0100 | [diff] [blame] | 1855 | [source,manifest] |
| 1856 | ---- |
| 1857 | Gerrit-ApiType: plugin |
| 1858 | Gerrit-Module: com.googlesource.gerrit.plugins.helloworld.HelloWorldModule |
| 1859 | ---- |
| 1860 | |
Edwin Kempin | b2e926a | 2013-11-11 16:38:30 +0100 | [diff] [blame] | 1861 | It is also possible to show some menu entries only if the user has a |
| 1862 | certain capability: |
| 1863 | |
| 1864 | [source,java] |
| 1865 | ---- |
| 1866 | public class MyTopMenuExtension implements TopMenu { |
| 1867 | private final String pluginName; |
| 1868 | private final Provider<CurrentUser> userProvider; |
| 1869 | private final List<MenuEntry> menuEntries; |
| 1870 | |
| 1871 | @Inject |
| 1872 | public MyTopMenuExtension(@PluginName String pluginName, |
| 1873 | Provider<CurrentUser> userProvider) { |
| 1874 | this.pluginName = pluginName; |
| 1875 | this.userProvider = userProvider; |
| 1876 | menuEntries = new ArrayList<TopMenu.MenuEntry>(); |
| 1877 | |
| 1878 | // add menu entry that is only visible to users with a certain capability |
| 1879 | if (canSeeMenuEntry()) { |
| 1880 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1881 | .singletonList(new MenuItem("Gerrit", "http://gerrit.googlecode.com/")))); |
| 1882 | } |
| 1883 | |
| 1884 | // add menu entry that is visible to all users (even anonymous users) |
| 1885 | menuEntries.add(new MenuEntry("Top Menu Entry", Collections |
| 1886 | .singletonList(new MenuItem("Documentation", "/plugins/myplugin/")))); |
| 1887 | } |
| 1888 | |
| 1889 | private boolean canSeeMenuEntry() { |
| 1890 | if (userProvider.get().isIdentifiedUser()) { |
| 1891 | CapabilityControl ctl = userProvider.get().getCapabilities(); |
| 1892 | return ctl.canPerform(pluginName + "-" + MyCapability.ID) |
| 1893 | || ctl.canAdministrateServer(); |
| 1894 | } else { |
| 1895 | return false; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | @Override |
| 1900 | public List<MenuEntry> getEntries() { |
| 1901 | return menuEntries; |
| 1902 | } |
| 1903 | } |
| 1904 | ---- |
| 1905 | |
Dave Borowitz | f1790ce | 2016-11-14 12:26:12 -0800 | [diff] [blame] | 1906 | |
Edwin Kempin | 289f1a0 | 2014-02-04 16:08:25 +0100 | [diff] [blame] | 1907 | [[settings-screen]] |
| 1908 | == Plugin Settings Screen |
| 1909 | |
| 1910 | If a plugin implements a screen for administrating its settings that is |
| 1911 | available under "#/x/<plugin-name>/settings" it is automatically linked |
| 1912 | from the plugin list screen. |
| 1913 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1914 | [[http]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1915 | == HTTP Servlets |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1916 | |
| 1917 | Plugins or extensions may register additional HTTP servlets, and |
| 1918 | wrap them with HTTP filters. |
| 1919 | |
| 1920 | Servlets may use auto-registration to declare the URL they handle: |
| 1921 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1922 | [source,java] |
| 1923 | ---- |
| 1924 | import com.google.gerrit.extensions.annotations.Export; |
| 1925 | import com.google.inject.Singleton; |
| 1926 | import javax.servlet.http.HttpServlet; |
| 1927 | import javax.servlet.http.HttpServletRequest; |
| 1928 | import javax.servlet.http.HttpServletResponse; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1929 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1930 | @Export("/print") |
| 1931 | @Singleton |
| 1932 | class HelloServlet extends HttpServlet { |
| 1933 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 1934 | res.setContentType("text/plain"); |
| 1935 | res.setCharacterEncoding("UTF-8"); |
| 1936 | res.getWriter().write("Hello"); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1937 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1938 | } |
| 1939 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1940 | |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1941 | The auto registration only works for standard servlet mappings like |
Jonathan Nieder | 5758f18 | 2015-03-30 11:28:55 -0700 | [diff] [blame] | 1942 | `/foo` or `+/foo/*+`. Regex style bindings must use a Guice ServletModule |
Edwin Kempin | 8aa650f | 2012-07-18 11:25:48 +0200 | [diff] [blame] | 1943 | to register the HTTP servlets and declare it explicitly in the manifest |
| 1944 | with the `Gerrit-HttpModule` attribute: |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1945 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1946 | [source,java] |
| 1947 | ---- |
| 1948 | import com.google.inject.servlet.ServletModule; |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1949 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1950 | class MyWebUrls extends ServletModule { |
| 1951 | protected void configureServlets() { |
| 1952 | serve("/print").with(HelloServlet.class); |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1953 | } |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1954 | } |
| 1955 | ---- |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 1956 | |
| 1957 | For a plugin installed as name `helloworld`, the servlet implemented |
| 1958 | by HelloServlet class will be available to users as: |
| 1959 | |
| 1960 | ---- |
| 1961 | $ curl http://review.example.com/plugins/helloworld/print |
| 1962 | ---- |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 1963 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 1964 | [[data-directory]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 1965 | == Data Directory |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1966 | |
Dave Borowitz | 9e15875 | 2015-02-24 10:17:04 -0800 | [diff] [blame] | 1967 | Plugins can request a data directory with a `@PluginData` Path (or File, |
| 1968 | deprecated) dependency. A data directory will be created automatically |
| 1969 | by the server in `$site_path/data/$plugin_name` and passed to the |
| 1970 | plugin. |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1971 | |
| 1972 | Plugins can use this to store any data they want. |
| 1973 | |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1974 | [source,java] |
| 1975 | ---- |
| 1976 | @Inject |
Dave Borowitz | 9e15875 | 2015-02-24 10:17:04 -0800 | [diff] [blame] | 1977 | MyType(@PluginData java.nio.file.Path myDir) { |
| 1978 | this.in = Files.newInputStream(myDir.resolve("my.config")); |
David Pursehouse | 68153d7 | 2013-09-04 10:09:17 +0900 | [diff] [blame] | 1979 | } |
| 1980 | ---- |
Edwin Kempin | 41f6391 | 2012-07-17 12:33:55 +0200 | [diff] [blame] | 1981 | |
Dariusz Luksza | ebab92a | 2014-09-10 11:14:19 +0200 | [diff] [blame] | 1982 | [[secure-store]] |
| 1983 | == SecureStore |
| 1984 | |
| 1985 | SecureStore allows to change the way Gerrit stores sensitive data like |
| 1986 | passwords. |
| 1987 | |
| 1988 | In order to replace the default SecureStore (no-op) implementation, |
| 1989 | a class that extends `com.google.gerrit.server.securestore.SecureStore` |
| 1990 | needs to be provided (with dependencies) in a separate jar file. Then |
| 1991 | link:pgm-SwitchSecureStore.html[SwitchSecureStore] must be run to |
| 1992 | switch implementations. |
| 1993 | |
| 1994 | The SecureStore implementation is instantiated using a Guice injector |
| 1995 | which binds the `File` annotated with the `@SitePath` annotation. |
| 1996 | This means that a SecureStore implementation class can get access to |
| 1997 | the `site_path` like in the following example: |
| 1998 | |
| 1999 | [source,java] |
| 2000 | ---- |
| 2001 | @Inject |
| 2002 | MySecureStore(@SitePath java.io.File sitePath) { |
| 2003 | // your code |
| 2004 | } |
| 2005 | ---- |
| 2006 | |
| 2007 | No Guice bindings or modules are required. Gerrit will automatically |
| 2008 | discover and bind the implementation. |
| 2009 | |
Thomas Draebing | c4e5c07 | 2020-07-10 11:44:47 +0200 | [diff] [blame] | 2010 | [[gerrit-replica]] |
| 2011 | == Gerrit Replica |
| 2012 | |
| 2013 | Gerrit can be run as a read-only replica. Some plugins may need to know |
| 2014 | whether Gerrit is run as a primary- or a replica instance. For that purpose |
| 2015 | Gerrit exposes the `@GerritIsReplica` annotation. A boolean annotated with |
| 2016 | this annotation will indicate whether Gerrit is run as a replica. |
| 2017 | |
Michael Ochmann | 2461265 | 2016-02-12 17:26:18 +0100 | [diff] [blame] | 2018 | [[accountcreation]] |
| 2019 | == Account Creation |
| 2020 | |
| 2021 | Plugins can hook into the |
| 2022 | link:rest-api-accounts.html#create-account[account creation] REST API and |
| 2023 | inject additional external identifiers for an account that represents a user |
| 2024 | 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] | 2025 | point `com.google.gerrit.server.account.AccountExternalIdCreator` |
Michael Ochmann | 2461265 | 2016-02-12 17:26:18 +0100 | [diff] [blame] | 2026 | must be registered. |
| 2027 | |
| 2028 | [source,java] |
| 2029 | ---- |
| 2030 | class MyExternalIdCreator implements AccountExternalIdCreator { |
| 2031 | @Override |
| 2032 | public List<AccountExternalId> create(Account.Id id, String username, |
| 2033 | String email) { |
| 2034 | // your code |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | bind(AccountExternalIdCreator.class) |
| 2039 | .annotatedWith(UniqueAnnotations.create()) |
| 2040 | .to(MyExternalIdCreator.class); |
Michael Ochmann | 2461265 | 2016-02-12 17:26:18 +0100 | [diff] [blame] | 2041 | ---- |
| 2042 | |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2043 | [[download-commands]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2044 | == Download Commands |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2045 | |
Edwin Kempin | eafde88 | 2015-05-11 15:40:44 +0200 | [diff] [blame] | 2046 | Gerrit offers commands for downloading changes and cloning projects |
| 2047 | using different download schemes (e.g. for downloading via different |
| 2048 | network protocols). Plugins can contribute download schemes, download |
| 2049 | commands and clone commands by implementing |
| 2050 | `com.google.gerrit.extensions.config.DownloadScheme`, |
| 2051 | `com.google.gerrit.extensions.config.DownloadCommand` and |
| 2052 | `com.google.gerrit.extensions.config.CloneCommand`. |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2053 | |
Edwin Kempin | eafde88 | 2015-05-11 15:40:44 +0200 | [diff] [blame] | 2054 | The download schemes, download commands and clone commands which are |
| 2055 | used most often are provided by the Gerrit core plugin |
| 2056 | `download-commands`. |
Edwin Kempin | ea62148 | 2013-10-16 12:58:24 +0200 | [diff] [blame] | 2057 | |
Edwin Kempin | 78279ba | 2015-05-22 15:22:41 +0200 | [diff] [blame] | 2058 | [[included-in]] |
| 2059 | == Included In |
| 2060 | |
| 2061 | For merged changes the link:user-review-ui.html#included-in[Included In] |
| 2062 | drop-down panel shows the branches and tags in which the change is |
| 2063 | included. |
| 2064 | |
| 2065 | Plugins can add additional systems in which the change can be included |
| 2066 | by implementing `com.google.gerrit.extensions.config.ExternalIncludedIn`, |
| 2067 | e.g. a plugin can provide a list of servers on which the change was |
| 2068 | deployed. |
| 2069 | |
David Pursehouse | 38a27c3e | 2018-07-06 22:34:18 +0900 | [diff] [blame] | 2070 | [[change-report-formatting]] |
| 2071 | == Change Report Formatting |
| 2072 | |
| 2073 | When a change is pushed for review from the command line, Gerrit reports |
| 2074 | the change(s) received with their URL and subject. |
| 2075 | |
| 2076 | By implementing the |
| 2077 | `com.google.gerrit.server.git.ChangeReportFormatter` interface, a plugin |
| 2078 | may change the formatting of the report. |
| 2079 | |
David Pursehouse | e2f25be | 2018-11-26 17:56:07 +0900 | [diff] [blame] | 2080 | [[url-formatting]] |
| 2081 | == URL Formatting |
| 2082 | |
| 2083 | URLs to various parts of Gerrit are usually formed by adding suffixes to |
| 2084 | the canonical web URL. |
| 2085 | |
| 2086 | By implementing the |
| 2087 | `com.google.gerrit.server.config.UrlFormatter` interface, a plugin may |
| 2088 | change the format of the URL. |
| 2089 | |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2090 | [[links-to-external-tools]] |
| 2091 | == Links To External Tools |
| 2092 | |
| 2093 | Gerrit has extension points that enables development of a |
| 2094 | light-weight plugin that links commits to external |
| 2095 | tools (GitBlit, CGit, company specific resources etc). |
| 2096 | |
Han-Wen Nienhuys | 37a1cab | 2021-04-01 12:46:00 +0200 | [diff] [blame] | 2097 | PatchSetWebLinks will appear to the right of the commit-SHA-1 in the UI. |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2098 | |
| 2099 | [source, java] |
| 2100 | ---- |
| 2101 | import com.google.gerrit.extensions.annotations.Listen; |
| 2102 | import com.google.gerrit.extensions.webui.PatchSetWebLink;; |
Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 2103 | import com.google.gerrit.extensions.webui.WebLinkTarget; |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2104 | |
| 2105 | @Listen |
| 2106 | public class MyWeblinkPlugin implements PatchSetWebLink { |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2107 | private String name = "MyLink"; |
| 2108 | private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s"; |
Sven Selberg | 5548420 | 2014-06-26 08:48:51 +0200 | [diff] [blame] | 2109 | private String imageUrl = "http://placehold.it/16x16.gif"; |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2110 | |
| 2111 | @Override |
Gal Paikin | 57c9d58 | 2020-11-27 14:35:22 +0100 | [diff] [blame] | 2112 | public WebLinkInfo getPatchSetWebLink(String projectName, String commit, |
Gal Paikin | bdcba69 | 2021-01-07 15:52:00 +0100 | [diff] [blame] | 2113 | String commitMessage, String branchName) { |
Sven Selberg | a85e64d | 2014-09-24 10:52:21 +0200 | [diff] [blame] | 2114 | return new WebLinkInfo(name, |
| 2115 | imageUrl, |
| 2116 | String.format(placeHolderUrlProjectCommit, project, commit), |
| 2117 | WebLinkTarget.BLANK); |
Edwin Kempin | ceeed6b | 2014-09-11 17:07:33 +0200 | [diff] [blame] | 2118 | } |
Sven Selberg | ae1a10c | 2014-02-14 14:24:29 +0100 | [diff] [blame] | 2119 | } |
| 2120 | ---- |
| 2121 | |
Han-Wen Nienhuys | 37a1cab | 2021-04-01 12:46:00 +0200 | [diff] [blame] | 2122 | ParentWebLinks will appear to the right of the SHA-1 of the parent |
David Pursehouse | 58b8d76 | 2016-12-09 11:12:27 +0900 | [diff] [blame] | 2123 | revisions in the UI. The implementation should in most use cases direct |
| 2124 | to the same external service as PatchSetWebLink; it is provided as a |
| 2125 | separate interface because not all users want to have links for the |
| 2126 | parent revisions. |
| 2127 | |
Edwin Kempin | b3696c8 | 2014-09-11 09:41:42 +0200 | [diff] [blame] | 2128 | FileWebLinks will appear in the side-by-side diff screen on the right |
| 2129 | side of the patch selection on each side. |
| 2130 | |
Edwin Kempin | 8cdce50 | 2014-12-06 10:55:38 +0100 | [diff] [blame] | 2131 | DiffWebLinks will appear in the side-by-side and unified diff screen in |
| 2132 | the header next to the navigation icons. |
| 2133 | |
Edwin Kempin | ea00475 | 2014-04-11 15:56:02 +0200 | [diff] [blame] | 2134 | ProjectWebLinks will appear in the project list in the |
| 2135 | `Repository Browser` column. |
| 2136 | |
Edwin Kempin | 0f697bd | 2014-09-10 18:23:29 +0200 | [diff] [blame] | 2137 | BranchWebLinks will appear in the branch list in the last column. |
| 2138 | |
Edwin Kempin | f82b812 | 2016-06-03 09:20:16 +0200 | [diff] [blame] | 2139 | FileHistoryWebLinks will appear on the access rights screen. |
| 2140 | |
Paladox none | 34da15c | 2017-07-01 14:49:10 +0000 | [diff] [blame] | 2141 | TagWebLinks will appear in the tag list in the last column. |
| 2142 | |
Dave Borowitz | d0c01fd | 2017-06-06 10:47:08 -0400 | [diff] [blame] | 2143 | If a `get*WebLink` implementation returns `null`, the link will be omitted. This |
| 2144 | allows the plugin to selectively "enable" itself on a per-project/branch/file |
| 2145 | basis. |
| 2146 | |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2147 | [[lfs-extension]] |
| 2148 | == LFS Storage Plugins |
| 2149 | |
David Pursehouse | 2463c54 | 2016-08-02 16:04:58 +0900 | [diff] [blame] | 2150 | Gerrit provides an extension point that enables development of |
| 2151 | 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] | 2152 | 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] | 2153 | protocol endpoint `<project-name>/info/lfs/objects/batch` and forwards the requests |
| 2154 | to the configured link:config-gerrit.html#lfs[lfs.plugin] plugin which implements |
| 2155 | the LFS protocol. By exposing the default LFS endpoint, the git-lfs client can be |
| 2156 | used without any configuration. |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2157 | |
| 2158 | [source, java] |
| 2159 | ---- |
| 2160 | /** Provide an LFS protocol implementation */ |
| 2161 | import org.eclipse.jgit.lfs.server.LargeFileRepository; |
| 2162 | import org.eclipse.jgit.lfs.server.LfsProtocolServlet; |
| 2163 | |
| 2164 | @Singleton |
| 2165 | public class LfsApiServlet extends LfsProtocolServlet { |
| 2166 | private static final long serialVersionUID = 1L; |
| 2167 | |
| 2168 | private final S3LargeFileRepository repository; |
| 2169 | |
| 2170 | @Inject |
| 2171 | LfsApiServlet(S3LargeFileRepository repository) { |
| 2172 | this.repository = repository; |
| 2173 | } |
| 2174 | |
| 2175 | @Override |
| 2176 | protected LargeFileRepository getLargeFileRepository() { |
| 2177 | return repository; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | /** Register the LfsApiServlet to listen on the default LFS protocol endpoint */ |
| 2182 | import static com.google.gerrit.httpd.plugins.LfsPluginServlet.URL_REGEX; |
| 2183 | |
Dave Borowitz | 20f4e8a | 2019-03-13 14:19:15 -0700 | [diff] [blame] | 2184 | import com.google.inject.servlet.ServletModule; |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2185 | |
Dave Borowitz | 20f4e8a | 2019-03-13 14:19:15 -0700 | [diff] [blame] | 2186 | public class HttpModule extends ServletModule { |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2187 | @Override |
| 2188 | protected void configureServlets() { |
| 2189 | serveRegex(URL_REGEX).with(LfsApiServlet.class); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | /** Provide an implementation of the LargeFileRepository */ |
| 2194 | import org.eclipse.jgit.lfs.server.s3.S3Repository; |
| 2195 | |
| 2196 | public class S3LargeFileRepository extends S3Repository { |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 2197 | ... |
Saša Živkov | ca7a67e | 2015-12-01 14:25:10 +0100 | [diff] [blame] | 2198 | } |
| 2199 | ---- |
| 2200 | |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2201 | [[metrics]] |
| 2202 | == Metrics |
| 2203 | |
| 2204 | === Metrics Reporting |
| 2205 | |
| 2206 | To send Gerrit's metrics data to an external reporting backend, a plugin can |
| 2207 | get a `MetricRegistry` injected and register an instance of a class that |
| 2208 | implements the `Reporter` interface from link:http://metrics.dropwizard.io/[ |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2209 | DropWizard Metrics,role=external,window=_blank]. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2210 | |
| 2211 | Metric reporting plugin implementations are provided for |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2212 | link:https://gerrit.googlesource.com/plugins/metrics-reporter-jmx/[JMX,role=external,window=_blank], |
| 2213 | link:https://gerrit.googlesource.com/plugins/metrics-reporter-elasticsearch/[Elastic Search,role=external,window=_blank], |
| 2214 | 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] | 2215 | |
| 2216 | 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] | 2217 | 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] | 2218 | cookbook plugin,role=external,window=_blank]. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2219 | |
| 2220 | === Providing own metrics |
| 2221 | |
| 2222 | Plugins may provide metrics to be dispatched to external reporting services by |
| 2223 | getting a `MetricMaker` injected and creating instances of specific types of |
| 2224 | metric: |
| 2225 | |
| 2226 | * Counter |
| 2227 | + |
| 2228 | Metric whose value increments during the life of the process. |
| 2229 | |
| 2230 | * Timer |
| 2231 | + |
| 2232 | Metric recording time spent on an operation. |
| 2233 | |
| 2234 | * Histogram |
| 2235 | + |
| 2236 | Metric recording statistical distribution (rate) of values. |
| 2237 | |
David Pursehouse | 48d05ea | 2017-02-03 19:05:29 +0900 | [diff] [blame] | 2238 | Note that metrics cannot be recorded from plugin init steps that |
| 2239 | are run during site initialization. |
| 2240 | |
David Pursehouse | c3bbd56 | 2017-02-06 20:25:29 +0900 | [diff] [blame] | 2241 | By default, plugin metrics are recorded under |
| 2242 | `plugins/${plugin-name}/${metric-name}`. This can be changed by |
| 2243 | setting `plugins.${plugin-name}.metricsPrefix` in the `gerrit.config` |
| 2244 | file. For example: |
| 2245 | |
| 2246 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 2247 | [plugin "my-plugin"] |
| 2248 | metricsPrefix = my-metrics |
David Pursehouse | c3bbd56 | 2017-02-06 20:25:29 +0900 | [diff] [blame] | 2249 | ---- |
| 2250 | |
| 2251 | will cause the metrics to be recorded under `my-metrics/${metric-name}`. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2252 | |
| 2253 | See the replication metrics in the |
| 2254 | 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] | 2255 | replication plugin,role=external,window=_blank] for an example of usage. |
David Pursehouse | 8ad1173 | 2016-08-29 15:00:14 +0900 | [diff] [blame] | 2256 | |
Edwin Kempin | da17bc3 | 2016-06-14 11:50:58 +0200 | [diff] [blame] | 2257 | [[account-patch-review-store]] |
| 2258 | == AccountPatchReviewStore |
| 2259 | |
| 2260 | The AccountPatchReviewStore is used to store reviewed flags on changes. |
| 2261 | A reviewed flag is a tuple of (patch set ID, file, account ID) and |
| 2262 | records whether the user has reviewed a file in a patch set. Each user |
| 2263 | can easily have thousands of reviewed flags and the number of reviewed |
| 2264 | flags is growing without bound. The store must be able handle this data |
| 2265 | volume efficiently. |
| 2266 | |
| 2267 | Gerrit implements this extension point, but plugins may bind another |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 2268 | implementation, e.g. one that supports cluster setup with multiple |
| 2269 | primary Gerrit nodes handling write operations. |
Edwin Kempin | da17bc3 | 2016-06-14 11:50:58 +0200 | [diff] [blame] | 2270 | |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 2271 | [source,java] |
Edwin Kempin | da17bc3 | 2016-06-14 11:50:58 +0200 | [diff] [blame] | 2272 | ---- |
| 2273 | DynamicItem.bind(binder(), AccountPatchReviewStore.class) |
| 2274 | .to(MultiMasterAccountPatchReviewStore.class); |
| 2275 | |
| 2276 | ... |
| 2277 | |
| 2278 | public class MultiMasterAccountPatchReviewStore |
| 2279 | implements AccountPatchReviewStore { |
| 2280 | ... |
| 2281 | } |
| 2282 | ---- |
| 2283 | |
Dave Borowitz | f1790ce | 2016-11-14 12:26:12 -0800 | [diff] [blame] | 2284 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2285 | [[documentation]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2286 | == Documentation |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2287 | |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2288 | 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] | 2289 | `+/Documentation/*+` or `+/static/*+`, the core Gerrit server will |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2290 | automatically export these resources over HTTP from the plugin JAR. |
| 2291 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2292 | Static resources under the `static/` directory in the JAR will be |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 2293 | available as `/plugins/helloworld/static/resource`. This prefix is |
| 2294 | configurable by setting the `Gerrit-HttpStaticPrefix` attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2295 | |
David Pursehouse | 6853b5a | 2013-07-10 11:38:03 +0900 | [diff] [blame] | 2296 | Documentation files under the `Documentation/` directory in the JAR |
Dave Borowitz | b893ac8 | 2013-03-27 10:03:55 -0400 | [diff] [blame] | 2297 | will be available as `/plugins/helloworld/Documentation/resource`. This |
| 2298 | prefix is configurable by setting the `Gerrit-HttpDocumentationPrefix` |
| 2299 | attribute. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2300 | |
Christian Aistleitner | 040cf82 | 2015-03-26 21:09:09 +0100 | [diff] [blame] | 2301 | Documentation may be written in the Markdown flavor |
Marian Harbach | 3425337 | 2019-12-10 18:01:31 +0100 | [diff] [blame] | 2302 | 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] | 2303 | if the file name ends with `.md`. Gerrit will automatically convert |
| 2304 | Markdown to HTML if accessed with extension `.html`. |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2305 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2306 | [[macros]] |
Edwin Kempin | c78777d | 2012-07-16 15:55:11 +0200 | [diff] [blame] | 2307 | Within the Markdown documentation files macros can be used that allow |
| 2308 | to write documentation with reasonably accurate examples that adjust |
| 2309 | automatically based on the installation. |
| 2310 | |
| 2311 | The following macros are supported: |
| 2312 | |
| 2313 | [width="40%",options="header"] |
| 2314 | |=================================================== |
| 2315 | |Macro | Replacement |
| 2316 | |@PLUGIN@ | name of the plugin |
| 2317 | |@URL@ | Gerrit Web URL |
| 2318 | |@SSH_HOST@ | SSH Host |
| 2319 | |@SSH_PORT@ | SSH Port |
| 2320 | |=================================================== |
| 2321 | |
| 2322 | The macros will be replaced when the documentation files are rendered |
| 2323 | from Markdown to HTML. |
| 2324 | |
| 2325 | Macros that start with `\` such as `\@KEEP@` will render as `@KEEP@` |
| 2326 | even if there is an expansion for `KEEP` in the future. |
| 2327 | |
Luca Milanesio | a3d41da | 2020-12-29 14:29:03 +0000 | [diff] [blame] | 2328 | Documentation should typically contain the following content: |
| 2329 | |
Luca Milanesio | 718a0a5 | 2020-12-29 22:19:12 +0000 | [diff] [blame] | 2330 | [width="100%",options="header"] |
Luca Milanesio | a3d41da | 2020-12-29 14:29:03 +0000 | [diff] [blame] | 2331 | |=================================================== |
Luca Milanesio | 718a0a5 | 2020-12-29 22:19:12 +0000 | [diff] [blame] | 2332 | |File | Content |
| 2333 | |`README.md` | Home page of the plugin when browsing its source code on Git |
| 2334 | |`LICENSE` | Open-source license |
| 2335 | |`resources/Documentation/about.md` | Overview of the plugin and its purpose |
| 2336 | |`resources/Documentation/config.md` | Plugin configuration settings and sample configs |
| 2337 | |`resources/Documentation/build.md` | How to build the plugin |
| 2338 | |`resources/Documentation/cmd-<command>.md` | SSH commands |
| 2339 | |`resources/Documentation/rest-api-<api>.md` | REST API |
| 2340 | |`resources/Documentation/servlet-<servlet>.md` | HTTP Servlets |
Luca Milanesio | a3d41da | 2020-12-29 14:29:03 +0000 | [diff] [blame] | 2341 | |=================================================== |
| 2342 | |
| 2343 | The documentation under resources/Documentation may contain macro that |
| 2344 | will be included and expanded by Gerrit once the plugin is loaded. |
| 2345 | |
| 2346 | The files in the root directory are not included in the plugin package |
| 2347 | and must not have any macro for expansion. It may also collect |
| 2348 | additional information that would make the plugin more discoverable, such as |
| 2349 | a more user-friendly description of its use-cases. |
| 2350 | |
| 2351 | The documentation can also include images that can help understanding more |
| 2352 | visually how the plugin can interact with the other Gerrit components. |
| 2353 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2354 | [[auto-index]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2355 | === Automatic Index |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2356 | |
| 2357 | If a plugin does not handle its `/` URL itself, Gerrit will |
| 2358 | redirect clients to the plugin's `/Documentation/index.html`. |
| 2359 | Requests for `/Documentation/` (bare directory) will also redirect |
| 2360 | to `/Documentation/index.html`. |
| 2361 | |
| 2362 | If neither resource `Documentation/index.html` or |
| 2363 | `Documentation/index.md` exists in the plugin JAR, Gerrit will |
Edwin Kempin | a9c529f | 2021-06-17 09:34:24 +0200 | [diff] [blame] | 2364 | automatically generate an index page. |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2365 | |
Edwin Kempin | a9c529f | 2021-06-17 09:34:24 +0200 | [diff] [blame] | 2366 | The generated index page contains 3 sections: |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2367 | |
Edwin Kempin | a9c529f | 2021-06-17 09:34:24 +0200 | [diff] [blame] | 2368 | 1. Manifest section |
| 2369 | + |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2370 | Some optional information from the manifest is extracted and |
| 2371 | displayed as part of the index page, if present in the manifest: |
Edwin Kempin | a9c529f | 2021-06-17 09:34:24 +0200 | [diff] [blame] | 2372 | + |
Shawn O. Pearce | 795167c | 2012-05-12 11:20:18 -0700 | [diff] [blame] | 2373 | [width="40%",options="header"] |
| 2374 | |=================================================== |
| 2375 | |Field | Source Attribute |
| 2376 | |Name | Implementation-Title |
| 2377 | |Vendor | Implementation-Vendor |
| 2378 | |Version | Implementation-Version |
| 2379 | |URL | Implementation-URL |
| 2380 | |API Version | Gerrit-ApiVersion |
| 2381 | |=================================================== |
| 2382 | |
Edwin Kempin | a9c529f | 2021-06-17 09:34:24 +0200 | [diff] [blame] | 2383 | 2. About section |
| 2384 | + |
| 2385 | If an `about.md` or `about.html` file exists, its content will be inserted in an |
| 2386 | 'About' section. |
| 2387 | + |
| 2388 | If both `about.md` and `about.html` exist, only the first discovered file will |
| 2389 | be used. |
| 2390 | |
| 2391 | 3. TOC section |
| 2392 | + |
| 2393 | If a `toc.md` or `toc.html` file exists, its content will be inserted in a |
| 2394 | 'Documentation' section. |
| 2395 | + |
| 2396 | `toc.md` or `toc.html` is a manually maintained index of the documentation pages |
| 2397 | that exist in the plugin. Having a manually maintained index has the advantage |
| 2398 | that you can group the documentation pages by topic and sort them by importance. |
| 2399 | + |
| 2400 | If both `toc.md` and `toc.html` exist, only the first discovered file will |
| 2401 | be used. |
| 2402 | + |
| 2403 | If no `toc` file is present the TOC section is automatically generated by |
| 2404 | scanning every `\*.md` and `*.html` file in the `Documentation/` directory. |
| 2405 | + |
| 2406 | For any discovered Markdown (`*.md`) file, Gerrit will parse the |
| 2407 | header of the file and extract the first level one title. This |
| 2408 | title text will be used as display text for a link to the HTML |
| 2409 | version of the page. |
| 2410 | + |
| 2411 | For any discovered HTML (`\*.html`) file, Gerrit will use the name |
| 2412 | of the file, minus the `*.html` extension, as the link text. Any |
| 2413 | hyphens in the file name will be replaced with spaces. |
| 2414 | + |
| 2415 | If a discovered file name beings with `cmd-` it will be clustered |
| 2416 | into a 'Commands' section of the generated index page. |
| 2417 | + |
| 2418 | If a discovered file name beings with `servlet-` it will be clustered |
| 2419 | into a 'Servlets' section of the generated index page. |
| 2420 | + |
| 2421 | If a discovered file name beings with `rest-api-` it will be clustered |
| 2422 | into a 'REST APIs' section of the generated index page. |
| 2423 | + |
| 2424 | All other files are clustered under a 'Documentation' section. |
| 2425 | |
Edwin Kempin | f5a7733 | 2012-07-18 11:17:53 +0200 | [diff] [blame] | 2426 | [[deployment]] |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2427 | == Deployment |
Nasser Grainawi | e033b26 | 2012-05-09 17:54:21 -0700 | [diff] [blame] | 2428 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 2429 | Compiled plugins and extensions can be deployed to a running Gerrit |
| 2430 | server using the link:cmd-plugin-install.html[plugin install] command. |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2431 | |
Leopold Schabel | a46aac2 | 2021-03-15 00:05:28 +0100 | [diff] [blame] | 2432 | Web UI plugins distributed as a single `.js` file can be deployed without the |
| 2433 | overhead of JAR packaging. For more information refer to |
| 2434 | link:cmd-plugin-install.html[plugin install] command. |
Dariusz Luksza | 357a242 | 2012-11-12 06:16:26 +0100 | [diff] [blame] | 2435 | |
David Pursehouse | 75021fe | 2017-07-31 23:07:04 +0200 | [diff] [blame] | 2436 | Plugins can also be copied directly into the server's directory at |
Leopold Schabel | a46aac2 | 2021-03-15 00:05:28 +0100 | [diff] [blame] | 2437 | `$site_path/plugins/$name.(jar|js)`. For Web UI plugins, the name |
| 2438 | of the file, minus the `.js` extension, will be used as the |
David Pursehouse | 75021fe | 2017-07-31 23:07:04 +0200 | [diff] [blame] | 2439 | plugin name. For JAR plugins, the value of the `Gerrit-PluginName` |
| 2440 | manifest attribute will be used, if provided, otherwise the name of |
| 2441 | the file, minus the `.jar` extension, will be used. |
| 2442 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 2443 | For JAR plugins, the version is taken from the `Version` attribute in the |
David Pursehouse | 75021fe | 2017-07-31 23:07:04 +0200 | [diff] [blame] | 2444 | manifest. |
| 2445 | |
| 2446 | Unless disabled, servers periodically scan the `$site_path/plugins` |
Shawn O. Pearce | da4919a | 2012-05-10 16:54:28 -0700 | [diff] [blame] | 2447 | directory for updated plugins. The time can be adjusted by |
| 2448 | link:config-gerrit.html#plugins.checkFrequency[plugins.checkFrequency]. |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2449 | |
Edwin Kempin | f729574 | 2012-07-16 15:03:46 +0200 | [diff] [blame] | 2450 | For disabling plugins the link:cmd-plugin-remove.html[plugin remove] |
| 2451 | command can be used. |
| 2452 | |
Brad Larson | d5e87c3 | 2012-07-11 12:18:49 -0500 | [diff] [blame] | 2453 | Disabled plugins can be re-enabled using the |
| 2454 | link:cmd-plugin-enable.html[plugin enable] command. |
| 2455 | |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2456 | [[reviewer-suggestion]] |
| 2457 | == Reviewer Suggestion Plugins |
| 2458 | |
| 2459 | Gerrit provides an extension point that enables Plugins to rank |
| 2460 | the list of reviewer suggestion a user receives upon clicking "Add Reviewer" on |
| 2461 | the change screen. |
David Pursehouse | c2233a4 | 2018-02-07 10:31:05 +0900 | [diff] [blame] | 2462 | |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2463 | Gerrit supports both a default suggestion that appears when the user has not yet |
| 2464 | typed anything and a filtered suggestion that is shown as the user starts |
| 2465 | typing. |
David Pursehouse | c2233a4 | 2018-02-07 10:31:05 +0900 | [diff] [blame] | 2466 | |
| 2467 | Plugins receive a candidate list and can return a `Set` of suggested reviewers |
| 2468 | containing the `Account.Id` and a score for each reviewer. The candidate list is |
| 2469 | non-binding and plugins can choose to return reviewers not initially contained in |
| 2470 | the candidate list. |
| 2471 | |
| 2472 | Server administrators can configure the overall weight of each plugin by setting |
| 2473 | the `addreviewer.pluginName-exportName.weight` value in `gerrit.config`. |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2474 | |
| 2475 | [source, java] |
| 2476 | ---- |
Patrick Hiesel | 2514e41 | 2016-10-13 09:34:44 +0200 | [diff] [blame] | 2477 | import com.google.gerrit.common.Nullable; |
David Ostrovsky | b03a6e9 | 2019-05-26 14:11:47 +0200 | [diff] [blame] | 2478 | import com.google.gerrit.entities.Account; |
| 2479 | import com.google.gerrit.entities.Change; |
| 2480 | import com.google.gerrit.entities.Project; |
Patrick Hiesel | 2514e41 | 2016-10-13 09:34:44 +0200 | [diff] [blame] | 2481 | import com.google.gerrit.extensions.annotations.ExtensionPoint; |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2482 | |
| 2483 | import java.util.Set; |
| 2484 | |
| 2485 | public class MyPlugin implements ReviewerSuggestion { |
Patrick Hiesel | 2514e41 | 2016-10-13 09:34:44 +0200 | [diff] [blame] | 2486 | public Set<SuggestedReviewer> suggestReviewers(Project.NameKey project, |
| 2487 | @Nullable Change.Id changeId, @Nullable String query, |
| 2488 | Set<Account.Id> candidates) { |
| 2489 | Set<SuggestedReviewer> suggestions = new HashSet<>(); |
| 2490 | // Implement your ranking logic here |
| 2491 | return suggestions; |
Patrick Hiesel | 87880b0 | 2016-05-03 18:15:08 +0200 | [diff] [blame] | 2492 | } |
| 2493 | } |
| 2494 | ---- |
| 2495 | |
| 2496 | |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2497 | [[mail-filter]] |
| 2498 | == Mail Filter Plugins |
| 2499 | |
| 2500 | Gerrit provides an extension point that enables Plugins to discard incoming |
| 2501 | messages and prevent further processing by Gerrit. |
| 2502 | |
David Pursehouse | 4b06775 | 2017-03-03 15:54:53 +0900 | [diff] [blame] | 2503 | This can be used to implement spam checks, signature validations or organization |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2504 | specific checks like IP filters. |
| 2505 | |
| 2506 | [source, java] |
| 2507 | ---- |
| 2508 | import com.google.gerrit.extensions.annotations.ExtensionPoint; |
Han-Wen Nienhuys | 50dd94e | 2017-11-14 18:04:28 +0100 | [diff] [blame] | 2509 | import com.google.gerrit.mail.MailMessage; |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2510 | |
| 2511 | public class MyPlugin implements MailFilter { |
maximeg | 92419ab | 2018-02-16 10:27:25 +0100 | [diff] [blame] | 2512 | public boolean shouldProcessMessage(MailMessage message) { |
Patrick Hiesel | 30c7618 | 2017-01-20 11:46:43 +0100 | [diff] [blame] | 2513 | // Implement your filter logic here |
| 2514 | return true; |
| 2515 | } |
| 2516 | } |
| 2517 | ---- |
| 2518 | |
frankborden2@gmail.com | 66d410b | 2021-08-26 10:27:33 +0200 | [diff] [blame] | 2519 | |
| 2520 | [[account-tag]] |
| 2521 | == Account Tag Plugins |
| 2522 | |
| 2523 | Gerrit provides an extension point that enables Plugins to supply additional |
| 2524 | tags on an account. |
| 2525 | |
| 2526 | [source, java] |
| 2527 | ---- |
| 2528 | import com.google.gerrit.entities.Account; |
| 2529 | import com.google.gerrit.server.account.AccountTagProvider; |
| 2530 | import java.util.List; |
| 2531 | |
| 2532 | public class MyPlugin implements AccountTagProvider { |
| 2533 | public List<String> getTags(Account.Id id) { |
| 2534 | // Implement your logic here |
| 2535 | } |
| 2536 | } |
| 2537 | ---- |
| 2538 | |
David Causse | 01c1340 | 2019-04-09 14:19:28 +0200 | [diff] [blame] | 2539 | [[ssh-command-creation-interception]] |
| 2540 | == SSH Command Creation Interception |
David Pursehouse | 9cd079f | 2018-02-28 21:36:27 +0900 | [diff] [blame] | 2541 | |
| 2542 | Gerrit provides an extension point that allows a plugin to intercept |
| 2543 | creation of SSH commands and override the functionality with its own |
| 2544 | implementation. |
| 2545 | |
| 2546 | [source, java] |
| 2547 | ---- |
| 2548 | import com.google.gerrit.sshd.SshCreateCommandInterceptor; |
| 2549 | |
| 2550 | class MyCommandInterceptor implements SshCreateCommandInterceptor { |
| 2551 | @Override |
| 2552 | public String intercept(String in) { |
| 2553 | return pluginName + " mycommand"; |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 2554 | } |
| 2555 | } |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2556 | ---- |
| 2557 | |
David Causse | 01c1340 | 2019-04-09 14:19:28 +0200 | [diff] [blame] | 2558 | [[ssh-command-execution-interception]] |
| 2559 | == SSH Command Execution Interception |
| 2560 | Gerrit provides an extension point that enables plugins to check and |
| 2561 | prevent an SSH command from being run. |
| 2562 | |
| 2563 | [source, java] |
| 2564 | ---- |
| 2565 | import com.google.gerrit.sshd.SshExecuteCommandInterceptor; |
| 2566 | |
| 2567 | @Singleton |
| 2568 | public class SshExecuteCommandInterceptorImpl implements SshExecuteCommandInterceptor { |
| 2569 | private final Provider<SshSession> sessionProvider; |
| 2570 | |
| 2571 | @Inject |
| 2572 | SshExecuteCommandInterceptorImpl(Provider<SshSession> sessionProvider) { |
| 2573 | this.sessionProvider = sessionProvider; |
| 2574 | } |
| 2575 | |
| 2576 | @Override |
| 2577 | public boolean accept(String command, List<String> arguments) { |
| 2578 | if (command.startsWith("gerrit") && !"10.1.2.3".equals(sessionProvider.get().getRemoteAddressAsString())) { |
| 2579 | return false; |
| 2580 | } |
| 2581 | return true; |
| 2582 | } |
| 2583 | } |
| 2584 | ---- |
| 2585 | |
| 2586 | And then declare it in your SSH module: |
| 2587 | [source, java] |
| 2588 | ---- |
Nasser Grainawi | eea6039 | 2021-08-23 09:05:10 -0600 | [diff] [blame] | 2589 | DynamicSet.bind(binder(), SshExecuteCommandInterceptor.class) |
| 2590 | .to(SshExecuteCommandInterceptorImpl.class); |
David Causse | 01c1340 | 2019-04-09 14:19:28 +0200 | [diff] [blame] | 2591 | ---- |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2592 | |
| 2593 | [[pre-submit-evaluator]] |
| 2594 | == Pre-submit Validation Plugins |
| 2595 | |
| 2596 | Gerrit provides an extension point that enables plugins to prevent a change |
| 2597 | from being submitted. |
| 2598 | |
| 2599 | [IMPORTANT] |
| 2600 | This extension point **must NOT** be used for long or slow operations, like |
| 2601 | calling external programs or content, running unit tests... |
| 2602 | Slow operations will hurt the whole Gerrit instance. |
| 2603 | |
| 2604 | This can be used to implement custom rules that changes have to match to become |
| 2605 | submittable. A more concrete example: the Prolog rules engine can be |
| 2606 | implemented using this. |
| 2607 | |
| 2608 | Gerrit calls the plugins once per change and caches the results. Although it is |
| 2609 | possible to predict when this interface will be triggered, this should not be |
| 2610 | considered as a feature. Plugins should only rely on the internal state of the |
| 2611 | ChangeData, not on external values like date and time, remote content or |
| 2612 | randomness. |
| 2613 | |
| 2614 | Plugins are expected to support rules inheritance themselves, providing ways |
| 2615 | to configure it and handling the logic behind it. |
| 2616 | Please note that no inheritance is sometimes better than badly handled |
| 2617 | inheritance: mis-communication and strange behaviors caused by inheritance |
| 2618 | may and will confuse the users. Each plugins is responsible for handling the |
| 2619 | project hierarchy and taking wise actions. Gerrit does not enforce it. |
| 2620 | |
| 2621 | Once Gerrit has gathered every plugins' SubmitRecords, it stores them. |
| 2622 | |
| 2623 | Plugins accept or reject a given change using `SubmitRecord.Status`. |
| 2624 | If a change is ready to be submitted, `OK`. If it is not ready and requires |
| 2625 | modifications, `NOT_READY`. Other statuses are available for particular cases. |
| 2626 | A change can be submitted if all the plugins accept the change. |
| 2627 | |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2628 | Plugins may also decide not to vote on a given change by returning an |
| 2629 | `Optional.empty()` (ie: the plugin is not enabled for this repository). |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2630 | |
| 2631 | If a plugin decides not to vote, it's name will not be displayed in the UI and |
| 2632 | it will not be recoded in the database. |
| 2633 | |
| 2634 | .Gerrit's Pre-submit handling with three plugins |
| 2635 | [width="50%",cols="^m,^m,^m,^m",frame="topbot",options="header"] |
| 2636 | |======================================================= |
| 2637 | | Plugin A | Plugin B | Plugin C | Final decision |
| 2638 | | OK | OK | OK | OK |
| 2639 | | OK | OK | / | OK |
| 2640 | | OK | OK | RULE_ERROR | NOT_READY |
| 2641 | | OK | NOT_READY | OK | NOT_READY |
| 2642 | | NOT_READY | OK | OK | NOT_READY |
| 2643 | |======================================================= |
| 2644 | |
| 2645 | |
| 2646 | This makes composing plugins really easy. |
| 2647 | |
| 2648 | - If a plugin places a veto on a change, it can't be submitted. |
| 2649 | - If a plugin isn't enabled for a project (or isn't needed for this change), |
| 2650 | it returns an empty collection. |
| 2651 | - If all the plugins answer `OK`, the change can be submitted. |
| 2652 | |
| 2653 | |
| 2654 | A more rare case, but worth documenting: if there are no installed plugins, |
| 2655 | the labels will be compared to the rules defined in the project's config, |
| 2656 | and the permission system will be used to allow or deny a submit request. |
| 2657 | |
| 2658 | Some rules are defined internally to provide a common base ground (and sanity): |
| 2659 | changes that are marked as WIP or that are closed (abandoned, merged) can't be merged. |
| 2660 | |
| 2661 | |
| 2662 | [source, java] |
| 2663 | ---- |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2664 | import java.util.Optional; |
Andrew Allen | 6887c73 | 2020-09-12 00:42:11 +0000 | [diff] [blame] | 2665 | import com.google.gerrit.entities.SubmitRecord; |
| 2666 | import com.google.gerrit.entities.SubmitRecord.Status; |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2667 | import com.google.gerrit.server.query.change.ChangeData; |
| 2668 | import com.google.gerrit.server.rules.SubmitRule; |
| 2669 | |
| 2670 | public class MyPluginRules implements SubmitRule { |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2671 | public Optional<SubmitRecord> evaluate(ChangeData changeData) { |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2672 | // Implement your submitability logic here |
| 2673 | |
| 2674 | // Assuming we want to prevent this change from being submitted: |
Edwin Kempin | f5d3e94 | 2019-07-22 15:23:47 +0200 | [diff] [blame] | 2675 | SubmitRecord record = new SubmitRecord(); |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2676 | record.status = Status.NOT_READY; |
Patrick Hiesel | 7ab1b18 | 2019-08-13 15:29:27 +0200 | [diff] [blame] | 2677 | return Optional.of(record); |
David Pursehouse | 9cd079f | 2018-02-28 21:36:27 +0900 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | ---- |
| 2681 | |
Maxime Guerreiro | a63cc6e | 2018-02-28 14:31:55 +0100 | [diff] [blame] | 2682 | Don't forget to register your class! |
| 2683 | |
| 2684 | [source, java] |
| 2685 | ---- |
| 2686 | import com.google.gerrit.extensions.annotations.Exports; |
| 2687 | import com.google.inject.AbstractModule; |
| 2688 | |
| 2689 | public class MyPluginModule extends AbstractModule { |
| 2690 | @Override |
| 2691 | protected void configure() { |
| 2692 | bind(SubmitRule.class).annotatedWith(Exports.named("myPlugin")).to(MyPluginRules.class); |
| 2693 | } |
| 2694 | } |
| 2695 | ---- |
David Pursehouse | 9cd079f | 2018-02-28 21:36:27 +0900 | [diff] [blame] | 2696 | |
Maxime Guerreiro | 5ccf0b9 | 2018-05-15 16:28:22 +0000 | [diff] [blame] | 2697 | Plugin authors should also consider binding their SubmitRule using a `Gerrit-BatchModule`. |
| 2698 | See link:dev-plugins.html[Batch runtime] for more informations. |
| 2699 | |
Maxime Guerreiro | 97e891a | 2018-06-06 13:36:50 +0000 | [diff] [blame] | 2700 | |
| 2701 | The SubmitRule extension point allows you to write complex rules, but writing |
| 2702 | small self-contained rules should be preferred: doing so allows end users to |
| 2703 | compose several rules to form more complex submit checks. |
| 2704 | |
| 2705 | The `SubmitRequirement` class allows rules to communicate what the user needs |
| 2706 | to change in order to be compliant. These requirements should be kept once they |
| 2707 | are met, but marked as `OK`. If the requirements were not displayed, reviewers |
| 2708 | would need to use their precious time to manually check that they were met. |
| 2709 | |
Edwin Kempin | 96915b7 | 2019-07-22 14:57:40 +0200 | [diff] [blame] | 2710 | Implementors of the `SubmitRule` interface should check whether they need to |
| 2711 | contribute to the link:#change-etag-computation[change ETag computation] to |
| 2712 | prevent callers using ETags from potentially seeing outdated submittability |
| 2713 | information. |
| 2714 | |
| 2715 | [[change-etag-computation]] |
| 2716 | == Change ETag Computation |
| 2717 | |
| 2718 | By implementing the `com.google.gerrit.server.change.ChangeETagComputation` |
| 2719 | interface plugins can contribute a value to the change ETag computation. |
| 2720 | |
| 2721 | Plugins can affect the result of the get change / get change details REST |
| 2722 | endpoints by: |
| 2723 | |
| 2724 | * providing link:#query_attributes[plugin defined attributes] in |
| 2725 | link:rest-api-changes.html#change-info[ChangeInfo] |
| 2726 | * implementing a link:#pre-submit-evaluator[pre-submit evaluator] which affects |
| 2727 | the computation of `submittable` field in |
| 2728 | link:rest-api-changes.html#change-info[ChangeInfo] |
| 2729 | |
| 2730 | If the plugin defined part of link:rest-api-changes.html#change-info[ |
| 2731 | ChangeInfo] depends on plugin specific data, callers that use change ETags to |
| 2732 | avoid unneeded recomputations of ChangeInfos may see outdated plugin attributes |
| 2733 | and/or outdated submittable information, because a ChangeInfo is only reloaded |
| 2734 | if the change ETag changes. |
| 2735 | |
| 2736 | By implementating the `com.google.gerrit.server.change.ChangeETagComputation` |
| 2737 | interface plugins can contribute to the ETag computation and thus ensure that |
| 2738 | the change ETag changes when the plugin data was changed. This way it can be |
| 2739 | ensured that callers do not see outdated ChangeInfos. |
| 2740 | |
| 2741 | IMPORTANT: Change ETags are computed very frequently and the computation must |
| 2742 | be cheap. Take good care to not perform any expensive computations when |
| 2743 | implementing this. |
| 2744 | |
| 2745 | [source, java] |
| 2746 | ---- |
| 2747 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 2748 | |
| 2749 | import com.google.common.hash.Hasher; |
David Pursehouse | 0fb1fd1 | 2019-10-16 10:41:00 +0900 | [diff] [blame] | 2750 | import com.google.gerrit.entities.Change; |
| 2751 | import com.google.gerrit.entities.Project; |
Edwin Kempin | 96915b7 | 2019-07-22 14:57:40 +0200 | [diff] [blame] | 2752 | import com.google.gerrit.server.change.ChangeETagComputation; |
| 2753 | |
| 2754 | public class MyPluginChangeETagComputation implements ChangeETagComputation { |
| 2755 | public String getETag(Project.NameKey projectName, Change.Id changeId) { |
| 2756 | Hasher hasher = Hashing.murmur3_128().newHasher(); |
| 2757 | |
| 2758 | // Add hashes for all plugin-specific data that affects change infos. |
| 2759 | hasher.putString(sha1OfPluginSpecificChangeRef, UTF_8); |
| 2760 | |
| 2761 | return hasher.hash().toString(); |
| 2762 | } |
| 2763 | } |
| 2764 | ---- |
| 2765 | |
Edwin Kempin | ce8af4c | 2019-09-24 13:28:03 +0200 | [diff] [blame] | 2766 | [[exception-hook]] |
| 2767 | == ExceptionHook |
| 2768 | |
| 2769 | An `ExceptionHook` allows implementors to control how certain |
| 2770 | exceptions should be handled. |
| 2771 | |
| 2772 | This interface is intended to be implemented for multi-master setups to |
| 2773 | control the behavior for handling exceptions that are thrown by a lower |
| 2774 | layer that handles the consensus and synchronization between different |
| 2775 | server nodes. E.g. if an operation fails because consensus for a Git |
| 2776 | update could not be achieved (e.g. due to slow responding server nodes) |
| 2777 | this interface can be used to retry the request instead of failing it |
| 2778 | immediately. |
| 2779 | |
Edwin Kempin | 8f29a39 | 2019-10-31 11:48:31 +0100 | [diff] [blame] | 2780 | It also allows implementors to group exceptions that have the same |
| 2781 | cause into one metric bucket. |
| 2782 | |
Edwin Kempin | 7c9aa3e | 2019-09-26 11:50:03 +0200 | [diff] [blame] | 2783 | [[mail-soy-template-provider]] |
| 2784 | == MailSoyTemplateProvider |
| 2785 | |
| 2786 | This extension point allows to provide soy templates for registration |
| 2787 | so that they can be used for sending emails from a plugin. |
| 2788 | |
Patrick Hiesel | fc9817f | 2018-11-12 15:08:35 +0100 | [diff] [blame] | 2789 | [[quota-enforcer]] |
| 2790 | == Quota Enforcer |
| 2791 | |
| 2792 | Gerrit provides an extension point that allows a plugin to enforce quota. |
| 2793 | link:quota.html[This documentation page] has a list of all quota requests that |
| 2794 | Gerrit core issues. Plugins can choose to respond to all or just a subset of |
| 2795 | requests. Some implementations might want to keep track of user quota in buckets, |
| 2796 | others might just check against instance or project state to enforce limits on how |
| 2797 | many projects can be created or how large a repository can become. |
| 2798 | |
| 2799 | Checking against instance state can be racy for concurrent requests as the server does not |
| 2800 | refill tokens if the action fails in a later stage (e.g. database failure). If |
| 2801 | plugins want to guarantee an absolute maximum on a resource, they have to do their own |
| 2802 | book-keeping. |
| 2803 | |
| 2804 | [source, java] |
| 2805 | ---- |
| 2806 | import com.google.server.quota.QuotaEnforcer; |
| 2807 | |
| 2808 | class ProjectLimiter implements QuotaEnforcer { |
| 2809 | private final long maxNumberOfProjects = 100; |
| 2810 | @Override |
| 2811 | QuotaResponse requestTokens(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2812 | if (!"/projects/create".equals(quotaGroup)) { |
| 2813 | return QuotaResponse.noOp(); |
| 2814 | } |
| 2815 | // No deduction because we always check against the instance state (racy but fine for |
| 2816 | // this plugin) |
| 2817 | if (currentNumberOfProjects() + numTokens > maxNumberOfProjects) { |
| 2818 | return QuotaResponse.error("too many projects"); |
| 2819 | } |
| 2820 | return QuotaResponse.ok(); |
| 2821 | } |
| 2822 | |
| 2823 | @Override |
| 2824 | QuotaResponse dryRun(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2825 | // Since we are not keeping any state in this enforcer, we can simply call requestTokens(). |
| 2826 | return requestTokens(quotaGroup, ctx, numTokens); |
| 2827 | } |
| 2828 | |
| 2829 | void refill(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2830 | // No-op |
| 2831 | } |
| 2832 | } |
| 2833 | ---- |
| 2834 | |
| 2835 | [source, java] |
| 2836 | ---- |
| 2837 | import com.google.server.quota.QuotaEnforcer; |
| 2838 | |
| 2839 | class ApiQpsEnforcer implements QuotaEnforcer { |
| 2840 | // AutoRefillingPerUserBuckets is a imaginary bucket implementation that could be based on |
| 2841 | // a loading cache or a commonly used bucketing algorithm. |
| 2842 | private final AutoRefillingPerUserBuckets<CurrentUser, Long> buckets; |
| 2843 | @Override |
| 2844 | QuotaResponse requestTokens(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2845 | if (!quotaGroup.startsWith("/restapi/")) { |
| 2846 | return QuotaResponse.noOp(); |
| 2847 | } |
| 2848 | boolean success = buckets.deduct(ctx.user(), numTokens); |
| 2849 | if (!success) { |
| 2850 | return QuotaResponse.error("user sent too many qps, please wait for 5 minutes"); |
| 2851 | } |
| 2852 | return QuotaResponse.ok(); |
| 2853 | } |
| 2854 | |
| 2855 | @Override |
| 2856 | QuotaResponse dryRun(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2857 | if (!quotaGroup.startsWith("/restapi/")) { |
| 2858 | return QuotaResponse.noOp(); |
| 2859 | } |
| 2860 | boolean success = buckets.checkOnly(ctx.user(), numTokens); |
| 2861 | if (!success) { |
| 2862 | return QuotaResponse.error("user sent too many qps, please wait for 5 minutes"); |
| 2863 | } |
| 2864 | return QuotaResponse.ok(); |
| 2865 | } |
| 2866 | |
| 2867 | @Override |
| 2868 | void refill(String quotaGroup, QuotaRequestContext ctx, long numTokens) { |
| 2869 | if (!quotaGroup.startsWith("/restapi/")) { |
| 2870 | return; |
| 2871 | } |
| 2872 | buckets.add(ctx.user(), numTokens); |
| 2873 | } |
| 2874 | } |
| 2875 | ---- |
| 2876 | |
Edwin Kempin | f3e6cc77 | 2019-05-27 17:33:16 +0200 | [diff] [blame] | 2877 | [[performance-logger]] |
| 2878 | == Performance Logger |
| 2879 | |
| 2880 | `com.google.gerrit.server.logging.PerformanceLogger` is an extension point that |
| 2881 | is invoked for all operations for which the execution time is measured. The |
| 2882 | invocation of the extension point does not happen immediately, but only at the |
| 2883 | end of a request (REST call, SSH call, git push). Implementors can write the |
| 2884 | execution times into a performance log for further analysis. |
| 2885 | |
Edwin Kempin | 4a3ac16 | 2019-07-04 08:35:58 +0200 | [diff] [blame] | 2886 | [[request-listener]] |
| 2887 | == Request Listener |
| 2888 | |
| 2889 | `com.google.gerrit.server.RequestListener` is an extension point that is |
| 2890 | invoked each time the server executes a request from a user. |
| 2891 | |
Yuxuan 'fishy' Wang | 61698b1 | 2013-12-20 12:55:51 -0800 | [diff] [blame] | 2892 | == SEE ALSO |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 2893 | |
Ben Rohlfs | da0a62b | 2021-04-26 17:02:19 +0200 | [diff] [blame] | 2894 | * link:pg-plugin-dev.html[JavaScript Plugin API] |
David Ostrovsky | f86bae5 | 2013-09-01 09:10:39 +0200 | [diff] [blame] | 2895 | * link:dev-rest-api.html[REST API Developers' Notes] |
| 2896 | |
Deniz Türkoglu | eb78b60 | 2012-05-07 14:02:36 -0700 | [diff] [blame] | 2897 | GERRIT |
| 2898 | ------ |
| 2899 | Part of link:index.html[Gerrit Code Review] |
Yuxuan 'fishy' Wang | 99cb68d | 2013-10-31 17:26:00 -0700 | [diff] [blame] | 2900 | |
| 2901 | SEARCHBOX |
| 2902 | --------- |