Fwd: Using StaplerRequest to configure a plugin?

41 views
Skip to first unread message

Guy Matz

unread,
Mar 7, 2018, 9:49:20 AM3/7/18
to Jenkins Developers
Is this an appropriate place to ask this kind of question?

---------- Forwarded message ----------
From: Guy Matz <guy...@gmail.com>
Date: Tue, Mar 6, 2018 at 5:34 PM
Subject: Using StaplerRequest to configure a plugin?
To: jenkins...@googlegroups.com


Hi!  Does anyone know how to automate the configuration of a plugin using a StaplerRequest object?  I'm trying to configure scm-sync-configuration with:

import net.sf.json.JSONObject
import org.kohsuke.stapler.StaplerRequest
import hudson.plugins.scm_sync_configuration.ScmSyncConfigurationPlugin

def params = [ 
                noUserCommitMessage: false,
                displayStatus: true,
                scm: 'hudson.plugins.scm_sync_configuration.scms.ScmSyncGitSCM',
                gitRepositoryUrl: 'g...@github.com:GroupM-mPlatform/ci.git',
                commitMessagePattern: '[message]',
                manualSynchronizationIncludes: 'asdfasdf'
]

def JSONObject formData = [ params ] as JSONObject

scm = new ScmSyncConfigurationPlugin().getInstance()
println("SCMs? ${scm.getScms()}")

StaplerRequest req = [
  getParameter: { name -> 
    // println(params[name])
    params[name]
  }
] as org.kohsuke.stapler.StaplerRequest 

scm.configure(req, formData)

It seems to almost work a little - uncommenting "println(params[name])" tells me that it's getting processed a bit - but then it bombs with:

java.lang.UnsupportedOperationException
at org.codehaus.groovy.runtime.ConvertedMap.invokeCustom(ConvertedMap.java:52)
at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:124)
at com.sun.proxy.$Proxy51.getParameterValues(Unknown Source)
at hudson.plugins.scm_sync_configuration.ScmSyncConfigurationPlugin.configure(ScmSyncConfigurationPlugin.java:218)
at hudson.plugins.scm_sync_configuration.ScmSyncConfigurationPlugin$configure.call(Unknown Source)

The source at line 218 of ScmSyncConfigurationPlugin is:
if(req.getParameterValues("manualSynchronizationIncludes") != null){ 
...
}

Any thoughts out there?  :-(

Daniel Beck

unread,
Mar 7, 2018, 10:58:04 AM3/7/18
to jenkin...@googlegroups.com

> On 7. Mar 2018, at 15:49, Guy Matz <guy...@gmail.com> wrote:
>
> Is this an appropriate place to ask this kind of question?

Looks like a user question but you're deep enough in the guts of Jenkins to qualify for this list IMO.

Regarding your specific approach, you're implementing the single method `getParameter`, so the call to `getParameterValues` fails.

Note that you're probably more successful if you're planning to work with https://github.com/jenkinsci/configuration-as-code-plugin/ to configure your Jenkins once it's available :-)

Vlad Gorbunov

unread,
Sep 11, 2019, 9:24:49 AM9/11/19
to Jenkins Developers
Try to use 

StaplerRequest req = org.kohsuke.stapler.Stapler.getCurrentRequest()


Reply all
Reply to author
Forward
0 new messages