How to create editable parameter based on conditions of another parameter? - jenkins

For Jenkins pipeline, I've a parameter say Repository and second parameter say Branch.
Based on Repository value, I want to auto-populate, value for Branch.
Let's say -
if Repository is BobRepo then for Branch value BobBranch is auto populated
if Repository is AdamRepo then for Branch value AdamBranch is auto populated
This can be achieved using Active Choice Reactive Reference Parameter.
But if user provides, some unknown value to Repository like UnknownRepo then he should be allowed to type in value in Branch parameter which is not possible with Active Choice Reactive Reference Parameter
Can you please help how to achieve editable parameter when conditions don't match?

Actually you can achieve that using the Active Choice Reactive Reference Parameter.
All you need is to set the Branch parameter to be of Choice Type Formatted HTML and use the following template that is provided in the official documentation:
return "<input name=\"value\" value=\"${ReactiveRefParam}\" class=\"setting-input\" type=\"text\">"
This code template will create an input parameter with the propagated value that was defined in the value attribute ,and it enables the user to edit the propagated value.
In your case it will look something like:
def branch = ''
if (Repository == 'BobRepo') {
branch = 'BobBranch'
} else if (Repository == 'AdamRepo') {
branch = 'AdamBranch'
}
return "<input name='value' value='${branch}' class='setting-input' type='text'>"
If you want the input field to be read only on certain values, then it is also possible using the readonly attribute of the HTML input with some logic.
In addition you can also add HTML styles to input as you wish.
Here is an example that prevents edits when a branch is populated and the input width is changed to a custom value:
def branch = ''
if (Repository == 'BobRepo') {
branch = 'BobBranch'
} else if (Repository == 'AdamRepo') {
branch = 'AdamBranch'
}
return "<input name='value' value='${Repository}' style='width: 200px;' class='setting-input' type='text' ${branch ? 'readonly' : ''}>"

Related

Is there any way to merge fields without adding the doneMatch into the field?

We need to merge two fields into one. In the config, there is a "doneMatch" special string, and this seems to get appended to the merged field. Why is this needed, and is there a way to not have it also appended to the target field?
For example, I have:
src.fieldA = "City"
src.fieldB = "State"
I want to merge these 2 fields into target.fieldA as "City: State". However, what I end up with is "City: State##DONE##" I can change the config file so that it uses a different doneMatch but it can't be null or empty.. so if I changed it to ";", then the resulting field would be "City: State;". I have to have an end done char/string for some reason. What is this used for? If I am synching the fields with newer updates, is it going to detect the previous ##DONE## in the target.fieldA and think it's already done the merge, so would not make any new changes?
Can someone send me more info on this feature?
I have updated the code for v9.0.1 that changes the way that the FieldMerge works. It no longer uses doneMatch and instead requires that all 3 fields are different and then skips if it has already been done.
if (source.Fields.Contains(config.sourceField1) && source.Fields.Contains(config.sourceField2))
{
var val1 = source.Fields[config.sourceField1].Value != null ? source.Fields[config.sourceField1].Value.ToString() : string.Empty;
var val2 = source.Fields[config.sourceField2].Value != null ? source.Fields[config.sourceField2].Value.ToString() : string.Empty;
var valT = target.Fields[config.targetField].Value != null ? target.Fields[config.targetField].Value.ToString() : string.Empty;
var newValT = string.Format(config.formatExpression, val1, val2);
if (valT.Equals(newValT))
{
Trace.WriteLine(string.Format(" [SKIP] field already merged {0}:{1}+{2} to {3}:{4}", source.Id, config.sourceField1, config.sourceField2, target.Id, config.targetField));
} else
{
target.Fields[config.targetField].Value = string.Format(config.formatExpression, val1, val2) + config.doneMatch;
Trace.WriteLine(string.Format(" [UPDATE] field merged {0}:{1}+{2} to {3}:{4}", source.Id, config.sourceField1, config.sourceField2, target.Id, config.targetField));
}
}
https://github.com/nkdAgility/azure-devops-migration-tools/pull/529
Test the field merge with Azure Devops Migration tools, I could also reproduce this situation. The doneMatch field is required in the configuration.json file(##Done## by default).
There seems to be no way to avoid adding donematch to the target field. Since I am not the developer of this tool, I am not sure about the function of this field.
I would like to share a workaround to solve this issue.
Workaround:
You could try to set the " " to the doneMatch field. ( "doneMatch": " ")
For example:
"FieldMaps": [
{
"ObjectType": "VstsSyncMigrator.Engine.Configuration.FieldMap.FieldMergeMapConfig",
"WorkItemTypeName": "*",
"sourceField1": "System.Description",
"sourceField2": "Microsoft.VSTS.Common.AcceptanceCriteria",
"targetField": "System.Description",
"formatExpression": "{0} {1}",
"doneMatch": " "
}
Since the configuration file is a Json file, you could use " " to represent spaces.
Result:
is it going to detect the previous ##DONE## in the target.fieldA and
think it's already done the merge, so would not make any new changes
Based on my test, the ##Done## in the target field will not affect other operations. You can still operate on this field.
Update:
The above method could only work on field type: Text (multiple lines). If the field is other types, this method doesn't work.
You could create a new field(Text (multiple lines)). Then you could set the target field as the new field.
e.g.
"FieldMaps": [
{
"ObjectType": "VstsSyncMigrator.Engine.Configuration.FieldMap.FieldMergeMapConfig",
"WorkItemTypeName": "*",
"sourceField1": "Custom.test1",
"sourceField2": "Custom.test2",
"targetField": "Custom.test3",
"formatExpression": "{0} {1}",
"doneMatch": " "
}

How to have dependent parameters in JENKINS

Does Jenkins support (out of the box plugin) to have a way of defining dependent parameters. For ex: If I have three fields in a choice parameter for user to input, and if I select option A (considering it gives you a list of options) in the first field, then only other dependent fields should be shown to user to fill out(while doing the build), Similarly if user selects B , it should show rest of the other relevant options
Regards
You can do a basic if/then on a source parameter using an Active Choice Reactive Reference parameter. This does not solve the problem of not wanting parameters to display or not though.
How to do an if / then on a build parameter, setting another build parameter
============================================================================
You need the "Active Choices" plugin for starters.
1) The source parameter
Create a typical choice parameter - We'll call it "Choose", and give it the choices "val1", "val2", "val3", "val4"
2) The derived parameter
Create an "Active Choices Reactive Reference Parameter".
Name: What you want the parameter to be called / referenced as
Check "Groovy script"
The script:
if (Choose.equals("val1")) {
return "<input name=\"value\" value=\"Something because we chose val1\" class=\"setting-input\" type=\"text\">"
} else if (Choose.equals("val2")) {
return "<input name=\"value\" value=\"Something because we chose val2\" class=\"setting-input\" type=\"text\">"
} else if (Choose.equals("val3")) {
return "<input name=\"value\" value=\"Something because we chose val3\" class=\"setting-input\" type=\"text\">"
} else if (Choose.equals("val4")) {
return "<input name=\"value\" value=\"Something because we chose val4\" class=\"setting-input\" type=\"text\">"
}
Choice type: Formatted hidden HTML
-- or --
Formatted HTML - For testing so you can see the parameter being set
Referenced parameters: The name of the source parameter - "Choose"

how to return inputBox using groovy (jenkins)

I use the jenkins with active choices parameter
I need that groovy depends on my choice return area where I can write something
I try this
if (test_bench_UID.equals('user_spec')) { inputBox = "<input name='value' class='setting-input' type='text'>" return inputBox }
Where test_bench_UID is name active choices parameter, user_spec is parameter after choosing whom groovy should return input box
You have to add Active Reactive Reference Parameters, and select the choice type of the "Formatted HTML" field
,
don't forget to select the reference field!), and write such a script (
if(refParameter.equals("value")){
inputBox = "<input name='value' class='setting-input' type='text'>"
return inputBox
} else return "<b>else value will be show as text</b>"
You can use the variable by using ${value}.
In your case may be you can try ${user_spec} where ever you want to access it.
Hope it helps.

Building project with Active Choice Reactive Reference Parameter

I'm new to jenkins and groovy and I'm trying to create my own configuration which allows me to build my project with various parameters. To achieve that I use Active Choices Reactive Reference Parameter. As a Choice Type I set "Formatted HTML". It looks exactly as I want but unfortunately, no mater what, I cannot return parameters to build.
This is my groovy script:
if(useDefaultValues.equals("YES")) {
return "defaultName"
} else {
inputBox = "<input name='name' class='setting-input' type='text'>"
return inputBox
}
Can anyone help me with this please?
Update your Groovy script to something like this:
def defaultName = "default name"
if (useDefaultValues.equals("YES")) {
return "<b>${defaultName}</b><input type=\"hidden\" name=\"value\" value=\"${defaultName}\" />"
}
return "<input name=\"value\" class=\"setting-input\" type=\"text\">"
It's important that your input field uses name value - it does not change your parameter name, and if you named it name you will be able to access it as $name (if you use Groovy for instance).
It is also important that default value is passed as a hidden input field, otherwise parameter value is not set. This hidden input also has to use name value.
However there is one weird problem with HTML formatted input parameter - it always adds , in the end of the parameter value. So for instance if I pass lorem ipsum, when I read it with the parameter $name I will get lorem ipsum,. It looks like it treats it as a multiple parameters or something. To extract clean value from the parameter you can do something like (Groovy code):
name.split(',').first()
def defaultName = "default name"
if (useDefaultValues.equals("YES")) {
return "<input type=\"text\" name=\"value\" value=\"${defaultName}\" />"
}
return "<input name=\"value\" type=\"text\">"
Check "Omit value field" fixed comma problem.(comma issue)

How do I pass an Umbraco property directly to a macro

I have an umbraco macro written in Razor. And I have a node with two media picker properties. I want the macro to simply take the media picker property, grab the image and then it's crops and then a specific crop and display it in an image tag. I have the macro working fine but I can't work out how pass a property name to the macro. Basically to prevent me from just copying the macro twice and editing depending on the property name I require.
My macro code is as follows. All I want to do is parametrize the #Model.imageId
#{
var image = Model.MediaById(#Model.imageId);
if (image != null)
{
var crops = image.imageCropper.crops;
if (crops != null || crops.GetType().ToString() != "System.String")
{
<img src="#crops.Find("#name", "home-promo").url" width="217" height="163" />
} // if
}
}
If you define your Macro with a parameter imgId and call it like this:
<umbraco:Macro ID="Macro1" Alias="CropMacro" imgId="imageId" runat="server"></umbraco:Macro>
You're actually passing the name of the field "imageId". In the macro, you then may get the value of the Model's imageId property by using:
var image = Model.MediaById(Model.getProperty(Parameter.imgId).Value)
For an almost identical question & answer, as well as for how to do the same with helpers, see also here:
Umbraco razor template - Get Formatted Date From Field specified in parameter

Resources