Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Bitbucket How can I add a property at the application level and have it as the default for a custom repo hook

Anthony Lernihan September 16, 2016

I am developing a repo hook. In the setting page for the hook one of the value that is inputted is a regular expression. I want a default for this regular expression. The user can then accept or override this default for this repo. This default can change per Bitbucket server(installation) So I want for the system admin to able to change this value. 

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Adam
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 17, 2016

The 'override' aspect is straight-forward, but manual - you'll have to take a value from the repo level and if it's missing, take from the app level instead, and if that's missing handle it however you like (throw, or supply a default default perhaps? smile).

How can you accept and store a value at the app level?

One option is you can either add a form in the UI to the admin pages, and protect it so only admins can set it. You would add a web-item to a location like "atl.admin/admin-general-section" (though there are other options - if you append ?web.items&web.sections&web.panels to a URL, you can see all the locations that content and links can be added). You would have the web-item link to a servlet that you implement with the form, and store the value in your plugin (since you only have this value once per instance, one option would be to store it using PluginSettings API (you inject a PluginSettingsFactory into your constructor and call pluginSettingsFactory.createSettingsForKey("your plugin key") to get back a PluginSettings API you can use to store and retrieve values.

If you don't want to create a full UI for the setting, you can also accept values from configuration properties files. By injecting ApplicationPropertiesService and calling applicationPropertiesService.getPluginProperty("plugin.mycompany.somekey", "fallbackvalue") you can get values that a sysadmin has placed in the Bitbucket properties file in their home directory. Note that the key has to start with "plugin.".

 

How can you accept and store a value per-repo?

The hook SPI has a configuration form that you can implement to configure a repo and have settings stored in a Settings object for you. This Settings object is available when your hook runs, and you can write logic to coalesce it with the value you get at the application level.

 

Hope that helps,

Adam

Anthony Lernihan September 17, 2016

Thanks Adam, but will "@param config"  also contain application level properties. So I am calling my repo hook form using  soy and 

<view>com.atlassian.stash.repository.hook.ref.formContents</view>

So will the form have access to the application level properties. I can do your suggest on the Java side. Currently the form has only 

* @param config
* @param errors

Adam
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 18, 2016

I _believe_ you can use RepositoryHookService.setSettings(repo, hookKey, settings) to set defaults for a given repo that you could use in your form. But you'd have to do this proactively for every repo to have it there when the form is first viewed. I wouldn't recommend that if you can help it - might have some bad performance implications.

 

It might be better to just put a message in your form that the application-level default will be used if they don't provide one, and then do the merge of instance/repo-level config when the form is submitted or the hook is enabled without configuration (or even leave them separate and do it live when the hook is actually run). I understand that might not be the UX you want, but I'm not sure there's a good way to get it (happy to be corrected!)

Best of luck!

TAGS
AUG Leaders

Atlassian Community Events