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