Search code examples
jenkins-pipelinejenkins-groovy

hudson.plugins.git.UserRemoteConfig contains null values in RefSpec, but Jenkins has actual values


If I run the following in the Jenkins Script console:

String jobName = "path/to/myJobName"
def workflowJob = Hudson.getInstance().getItemByFullName(jobName)
def currentRun = workflowJob.getLastBuild()

List<hudson.scm.SCM> scms = currentRun.getSCMs()
 
for (hudson.scm.SCM scm in scms) {
  String refspec = scm.userRemoteConfigs[0].refspec
  scm.getUserRemoteConfigs().each {
    println(it.toString())
  }
}

I get this output, most of the SCMs have "Null" in the refspec (but do contain the URL):

null => repo_url

While, if I look at the Pipeline Job in the Jenkins view I can see fully detailed refspecs, including the branch name:

Multiple Refspecs including branch and revision

I guess the question is, what's the right way to get these details from a workflowRun?

I've tried all the functions listed https://javadoc.jenkins.io/plugin/git/hudson/plugins/git/UserRemoteConfig.html and my Google-Foo has failed to find any "other approaches", I'm not sure if there's a way I can feed the WorkflowRun into the Git Plugin and get more details out from there.


Solution

  • I asked ChatGPT and got some useful input, basically modifying the above like seems to work:

    String jobName = "path/to/myJobName"
    def workflowJob = Hudson.getInstance().getItemByFullName(jobName)
    def currentRun = workflowJob.getLastBuild()
    
    List<hudson.scm.SCM> scms = currentRun.getSCMs()
     
    for (hudson.scm.SCM scm in scms) {
      def branches = scm.getBranches()
      branches.each { branch ->
          println(branch.name)
      }
    }