Home > front end >  Active choices reactive parameter doesn't show desired default branch
Active choices reactive parameter doesn't show desired default branch

Time:07-01

I want to use Jenkins' active choices reactive parameter to use Groovy script to show all branches in the repository.

I have the following code sample to get all branches of a repository and since that there are hundreds of branches, I want the default to be master branch.

Even though I specifically inserted the defaultBranch variable, it shows the first item as the default and not the branch I written to.

Code:

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import jenkins.model.Jenkins

def git_url ="url"

def getAllBranches(url, credentialID, activeChoice = false, defaultBranch = 'master') {
  def jenkinsCredentials = CredentialsProvider.lookupCredentials(
          com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey,
          Jenkins.instance
  );
  def key = jenkinsCredentials.findResult { it.id == credentialID ? it.privateKey : null }

  if( !key ) {
    return 'Error: credentials not found'
  }

  Process process = ['ssh-agent','bash','-c', "echo '"   key   "' | ssh-add - 2> /dev/null && git ls-remote -t -h "   url].execute()
  def out = new StringBuilder()
  def err = new StringBuilder()
  process.consumeProcessOutput( out, err )
  process.waitFor()
  if( err.size() > 0 ) return err
  if( out.size() > 0 ) {
      def branches = out.readLines().collect { it.split()[1].replaceAll('refs/heads/', '') }
      if( activeChoice ) {
        def defaultBranchIndex = branches.indexOf(defaultBranch)
        if( defaultBranchIndex >= 0 ) branches.set(defaultBranchIndex, defaultBranch   ':selected')
      }
      return branches
  }
}

return getAllBranches(git_url, "efa7bed9-56a0-42ac-8fa3-a68fe7700801")

CodePudding user response:

You have set default for activeChoice to false in the getAllBranches method and do not set while calling it, thus the if-branch that adds the :selected is never entered.

CodePudding user response:

I changed activeChoice method's value from false to true and it solved my issue:

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import jenkins.model.Jenkins

def git_url ="url"

def getAllBranches(url, credentialID, activeChoice = true, defaultBranch = 'master') {
  def jenkinsCredentials = CredentialsProvider.lookupCredentials(
          com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey,
          Jenkins.instance
  );
  def key = jenkinsCredentials.findResult { it.id == credentialID ? it.privateKey : null }

  if( !key ) {
    return 'Error: credentials not found'
  }

  Process process = ['ssh-agent','bash','-c', "echo '"   key   "' | ssh-add - 2> /dev/null && git ls-remote -t -h "   url].execute()
  def out = new StringBuilder()
  def err = new StringBuilder()
  process.consumeProcessOutput( out, err )
  process.waitFor()
  if( err.size() > 0 ) return err
  if( out.size() > 0 ) {
      def branches = out.readLines().collect { it.split()[1].replaceAll('refs/heads/', '') }
      if( activeChoice ) {
        def defaultBranchIndex = branches.indexOf(defaultBranch)
        if( defaultBranchIndex >= 0 ) branches.set(defaultBranchIndex, defaultBranch   ':selected')
      }
      return branches
  }
}

return getAllBranches(git_url, "efa7bed9-56a0-42ac-8fa3-a68fe7700801")
  • Related