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