Hi all
So … I have written a plugin that adds a little functionality which is only applicable to repositories in one very specific project. The plugin adds a new web-item to the ‘bitbucket.repository.nav’ section but I’d like it to only be visible for repositories in that one project.
I can’t see any existing conditions (com.atlassian.bitbucket.web.conditions) that might help me here, so the question is : Can I achieve this through some other mechanism in the plugin or do I need to write my own condition to enable this?
Any help, as ever, greatly appreciated.
1 Like
You need to write your own condition class that implements com.atlassian.plugin.web.Condition
. The shouldDisplay(Map<String, Object> context){}
implementation can analyse the context of the request. For example
String path = ((HttpServletRequest) map.get("request")).getServletPath();
gives you the current path which you can extract the repo slug from. If it’s your repo, return true
to display the web-item otherwise false
.
Let me know how it goes
Thanks for the answer. I went ahead and created my own condition, that seems to work ok. Here’s the code if anyone else is interested:
public class IsHotfixRepositoryCondition implements Condition {
private String projectKey;
@Override
public void init(Map<String, String> map) throws PluginParseException {
projectKey = map.get("projectkey");
if (StringUtils.isBlank(projectKey)) {
throw new PluginParseException("<param name=\"projectkey\">{project-key}</param> is required.");
}
}
@Override
public boolean shouldDisplay(Map<String, Object> context) {
// Get repository from contexts
Repository repository = (Repository) context.get("repository");
if (repository!=null ) {
if (repository.getProject().getKey().toLowerCase().equals(projectKey.toLowerCase())) {
return true;
}
}
return false;
}
}
And the corresponding atlassian-plugin.xml looks like this:
<web-item key="repository-hotfix-tab" name="Repository hotfix tab" section="bitbucket.repository.nav" weight="50">
<condition class="com.walnut.bitbucket.plugins.hotfix.utils.conditions.IsHotfixRepositoryCondition">
<param name="projectkey">hot</param>
</condition>
<label key="repository.plugin.tab">Hotfix Info</label>
<link>/plugins/servlet/hotfix/${repository.project.key}/${repository.slug}</link>
</web-item>
The only tricky bit was getting the dependencies satisfied and for that I had to create a small ‘ComponentImports’ class as described towards the end of this thread : https://community.atlassian.com/t5/Answers-Developer-Questions/Issue-with-including-conditions-to-web-items/qaq-p/493048
Thanks for the help …