主动选择反应参数不显示所需的默认分支

发布于 2025-02-12 00:56:02 字数 1547 浏览 2 评论 0原文

我想使用Jenkins的主动选择反应性参数使用Groovy脚本来显示存储库中的所有分支。

我有以下代码示例以获取存储库的所有分支,并且由于有数百个分支,我希望默认值为主分支。

即使我专门插入了DefaultBranch变量,它也将第一个项目显示为默认值,而不是我编写的分支。

代码:

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")

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路还长,别太狂 2025-02-19 00:56:02

您已经将activeChoice的默认值设置为falsegetAllBranches方法中,并且在调用它时未设置,因此如果 - 添加的分支:从未输入

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.

愁杀 2025-02-19 00:56:02

我将ActiveChoice方法的价值从false更改为true,它解决了我的问题:

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")

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文