reading JSON values for multiple descriptors

31 views
Skip to first unread message

Nikhil Bhoski

unread,
Oct 29, 2018, 7:50:58 AM10/29/18
to Jenkins Developers
Hi 

I am following ui-samples plugin to create a dynamic drop down. i have as many classes as my dropdown list options. for each custom descriptor  i include respective config.jelly. My question is how can i access the values of custom config UI elements in my actual Action class .which extends Builder 


eg  
 

public class MyBuilder  extends Builder implements SimpleBuildStep{
@DataBoundConstructor
MyBuilder(Sting xyz){
 
this.xyz = xyz;
}


@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Builder>  { }

// i have followed ui-example and created custom descriptor and class which extends those as below 

 public static class Custom extends TypeList {
        private String runCustom;
        @DataBoundConstructor public Custom(String runAutomatic) {
            super("Custom");
            this.runCustom = runAutomatic;
        }
        @Extension
        public static final class DescriptorImpl extends TypetDescriptor {

@Override
public String getDisplayName() {
// TODO Auto-generated method stub
return "Custom";
}
  
    }


I need to access the data bound values from class Custom  in MyBuilder and vice versa . how can i do that 

I am also trying to read the Stapler request JSON object . but not finding a way that i could read the JSON form data in Action class 


Regards 
Nikhil 




Nikhil Bhoski

unread,
Oct 30, 2018, 1:05:56 AM10/30/18
to Jenkins Developers
Any Suggestions would be appreciated. i have been trying to get the Json form submitted using Stapler.getSubmittedForm(). but not sure how to get the current instance of stapler in the builder Action class . the main intention as described above is to read the config values of multiple descriptors.   

Jesse Glick

unread,
Oct 30, 2018, 10:09:52 AM10/30/18
to jenkin...@googlegroups.com
On Mon, Oct 29, 2018 at 7:51 AM Nikhil Bhoski <nikhil...@gmail.com> wrote:
> i have as many classes as my dropdown list options. for each custom descriptor i include respective config.jelly. My question is how can i access the values of custom config UI elements in my […] Builder

Your code snippet does not seem to make sense. Your `Builder` would
need to have a field of some `Describable` type (or a `List` thereof),
as in this example:

https://github.com/jenkinsci/ui-samples-plugin/blob/7d0fe2b32204a7fe0ec6adb6815e65d48b0dd54e/src/main/java/jenkins/plugins/ui_samples/HeteroList.java#L89-L97

Nikhil Bhoski

unread,
Oct 31, 2018, 3:36:06 AM10/31/18
to Jenkins Developers
Hi Jesse,

I am pasting actual code here (Sorry dont have git link for same). My actual issue is accessing the values that we are setting in the descriptors in my builders perform method as all my descriptors are public static nested classes. only option i have is instance of my describable (TypeList runTestTypes) which is in builder class but not sure how to map it to the values within perform method 

public class Hellomytool  extends Builder implements SimpleBuildStep{

private final String localmytool;
private final TypeList runTestTypes;
private static String WORK_SPACE = "WORKSPACE";
private static String customizedFields ;
    
@DataBoundConstructor
public Hellomytool(String localmytool,TypeList runTestTypes)
{
this.localmytool = localmytool;
this.runTestTypes = runTestTypes;
}

public String getLocalmytool(){

return this.localmytool;
}
public TypeList getRunTestTypes(){
return this.runTestTypes;
     }
@Extension
public static class Descriptorr extends BuildStepDescriptor<Builder>  {
@Override
public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends AbstractProject> jobType) {
//return mytoolItemProject.class.isAssignableFrom(jobType);
return true;
}
 
public boolean isMatrix(AbstractBuild<?, ?> build){ 
     return "hudson.matrix.MatrixBuild".equals(build.getClass().getName()); 
public DescriptorExtensionList <TypeList,Descriptor<TypeList>> getTypetDescriptor() {
        return Jenkins.getInstance().getDescriptorList(TypeList.class);
    }


//Overridden Method used to show the text under build dropdown
@Override
public String getDisplayName() {
return "Run mytool Tests";
}



@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
save();
return super.configure(req,formData);
}
public FormValidation doCheckLocalmytool(@QueryParameter String localmytool){
 
 
  return FormValidation.ok();
  }
 
public FormValidation doCheckCob(@QueryParameter boolean cob,
@QueryParameter String localmytool) {

return FormValidation.ok();
}



}
/*------------------------------------Descriptor Section -------------------*/
public static abstract class TypeList implements ExtensionPoint, Describable<TypeList> {
        protected String name;
        protected TypeList(String name) { this.name = name; }

       
public Descriptor<TypeList> getDescriptor() {
            return Jenkins.getInstance().getDescriptor(getClass());
        }
    }
public static abstract class TypetDescriptor extends Descriptor<TypeList> {
 
}

 
public static class Auto extends TypeList {
private boolean runAutomatic;
private final boolean junit;
private final boolean cob;

private final boolean tap;
@DataBoundConstructor
public Auto(boolean runAutomatic, boolean tap,boolean junit, boolean cob) {
super("Auto");
this.runAutomatic = runAutomatic;
this.tap = tap;
this.junit = junit;
this.cob = cob;
}

public boolean getTap(){
  
return this.tap; 
}
 
public boolean getJunit() {

return this.junit;
}

public boolean getCob() {

return this.cob;
}

@Extension
public static final class DescriptorImpl extends TypetDescriptor {
public boolean tap;

@Override
public String getDisplayName() {
return "Auto";
}

}
}

 
public static class Custom extends TypeList {
private String runCustom;

@DataBoundConstructor
public Custom(String runCustom) {
super("Custom");
this.runCustom = runCustom;
}

@Extension
public static final class DescriptorImpl extends TypetDescriptor {

@Override
public String getDisplayName() {
return "Custom";
}
}
}



@Override
public void perform(@Nonnull Run<?, ?> build,
@Nonnull FilePath workspace, @Nonnull Launcher launcher,
@Nonnull TaskListener listener) throws InterruptedException,
IOException {
boolean isError = false;
 
                // I  WANT values of class Auto / custom based on selection in UI here in this method 

Nikhil Bhoski

unread,
Oct 31, 2018, 3:51:15 AM10/31/18
to Jenkins Developers


On Tuesday, 30 October 2018 19:39:52 UTC+5:30, Jesse Glick wrote:

Ullrich Hafner

unread,
Oct 31, 2018, 5:49:25 AM10/31/18
to Jenkins Developers
Just use the field runTestTypes, if your UI is correctly mapped, it either contains a Custom or Auto instance. And you should not get any values from this object, you should call methods on it (tell don’t ask: https://martinfowler.com/bliki/TellDontAsk.html).


runTestTypes
runTestTypes runTestTypes

}
}


On Tuesday, 30 October 2018 19:39:52 UTC+5:30, Jesse Glick wrote:
On Mon, Oct 29, 2018 at 7:51 AM Nikhil Bhoski <nikhil...@gmail.com> wrote:
> i have as many classes as my dropdown list options. for each custom descriptor  i include respective config.jelly. My question is how can i access the values of custom config UI elements in my […] Builder

Your code snippet does not seem to make sense. Your `Builder` would
need to have a field of some `Describable` type (or a `List` thereof),
as in this example:

https://github.com/jenkinsci/ui-samples-plugin/blob/7d0fe2b32204a7fe0ec6adb6815e65d48b0dd54e/src/main/java/jenkins/plugins/ui_samples/HeteroList.java#L89-L97

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/a9cc0694-68f9-4465-8fa3-024feaa678b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Nikhil Bhoski

unread,
Oct 31, 2018, 7:44:50 AM10/31/18
to Jenkins Developers
Thanks Alot , Well explained . 
Reply all
Reply to author
Forward
0 new messages