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