Using plan defined variables from Bamboo plugin

I am developing a plugin and I need to replace a Bamboo user-defined variable (of a build plan) in a string with the actual value from the source code of the plugin.


I mean, replace:

"Hello, I'm ${bamboo.author}"

with:

"Hello, I'm Luis"

, supposing that author=Luis in this moment.

How can I achieve this?


Needless to say that I don't want to parse the string manually (looking for "author", "${" or whatever), since this is not an elegant option. I really think that the Bamboo API provides something to list defined variables of something like that.

Thx,

Luis

1 answer

More info:

I finally found some useful classes in com.atlassian.bamboo.variable; this is my code:

	private String replaceVars(String str, PlanKey plk)
{
String result = str;
VariableDefinitionManagerImpl vdm = new VariableDefinitionManagerImpl(new VariableDefinitionHibernateDao(),
new VariableDefinitionFactoryImpl());
List<VariableDefinition> variables = vdm.getGlobalVariables();
log.debug("Current plan " + plk.getKey());

if (variables.isEmpty())
{
return result;
}

for (VariableDefinition variable : variables)
{
log.debug("Variable " + variable.getKey());
Plan varPlan = variable.getPlan();
log.debug("Plan " + varPlan.getKey());

if (!isCurrentPlan(plk, varPlan.getPlanKey()))
{
continue;
}

VariableDefinitionImpl varImpl = (VariableDefinitionImpl) variable;
log.debug("VariableImpl " + varImpl.getKey() + "; Value: " + varImpl.getValue());
result = result.replaceAll(varImpl.getKey(), varImpl.getValue());
}
log.debug("selector result: " + str);
return result;
}

private boolean isCurrentPlan(PlanKey currentPlanKey, PlanKey plKey)
{
return (currentPlanKey.getKey().equals(plKey.getKey()));
}

The problem is that I get a null in com.atlassian.bamboo.variable.VariableDefinitionHibernateDao.findGlobalVariables(VariableDefinitionHibernateDao.java:33)

There is almost a variable defined in my current plan, so I don't know where the problem is, really.

Can somebody help me?

Thanks,
Luis

Suggest an answer

Log in or Join to answer
Community showcase
Renan Battaglin
Published May 18, 2017 in Bamboo

FAQ: How to Upgrade Bamboo Server

Bamboo 5.9 will no longer be supported after June 12, 2017. What does this mean? As part of our End of Life policy, Atlassian supports major versions for two years after the first major iteratio...

1,097 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot