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