Fix ks-controller panic due to missing nil checking in mutli-branch pipeline
Signed-off-by: rick <rick@jenkins-zh.cn>
This commit is contained in:
98
pkg/simple/client/devops/jenkins/internal/git.go
Normal file
98
pkg/simple/client/devops/jenkins/internal/git.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/beevik/etree"
|
||||
"k8s.io/klog"
|
||||
devopsv1alpha3 "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func AppendGitSourceToEtree(source *etree.Element, gitSource *devopsv1alpha3.GitSource) {
|
||||
if gitSource == nil {
|
||||
klog.Warning("please provide Git source when the sourceType is Git")
|
||||
return
|
||||
}
|
||||
source.CreateAttr("class", "jenkins.plugins.git.GitSCMSource")
|
||||
source.CreateAttr("plugin", "git")
|
||||
source.CreateElement("id").SetText(gitSource.ScmId)
|
||||
source.CreateElement("remote").SetText(gitSource.Url)
|
||||
if gitSource.CredentialId != "" {
|
||||
source.CreateElement("credentialsId").SetText(gitSource.CredentialId)
|
||||
}
|
||||
traits := source.CreateElement("traits")
|
||||
if gitSource.DiscoverBranches {
|
||||
traits.CreateElement("jenkins.plugins.git.traits.BranchDiscoveryTrait")
|
||||
}
|
||||
if gitSource.DiscoverTags {
|
||||
traits.CreateElement("jenkins.plugins.git.traits.TagDiscoveryTrait")
|
||||
}
|
||||
if gitSource.CloneOption != nil {
|
||||
cloneExtension := traits.CreateElement("jenkins.plugins.git.traits.CloneOptionTrait").CreateElement("extension")
|
||||
cloneExtension.CreateAttr("class", "hudson.plugins.git.extensions.impl.CloneOption")
|
||||
cloneExtension.CreateElement("shallow").SetText(strconv.FormatBool(gitSource.CloneOption.Shallow))
|
||||
cloneExtension.CreateElement("noTags").SetText(strconv.FormatBool(false))
|
||||
cloneExtension.CreateElement("honorRefspec").SetText(strconv.FormatBool(true))
|
||||
cloneExtension.CreateElement("reference")
|
||||
if gitSource.CloneOption.Timeout >= 0 {
|
||||
cloneExtension.CreateElement("timeout").SetText(strconv.Itoa(gitSource.CloneOption.Timeout))
|
||||
} else {
|
||||
cloneExtension.CreateElement("timeout").SetText(strconv.Itoa(10))
|
||||
}
|
||||
|
||||
if gitSource.CloneOption.Depth >= 0 {
|
||||
cloneExtension.CreateElement("depth").SetText(strconv.Itoa(gitSource.CloneOption.Depth))
|
||||
} else {
|
||||
cloneExtension.CreateElement("depth").SetText(strconv.Itoa(1))
|
||||
}
|
||||
}
|
||||
|
||||
if gitSource.RegexFilter != "" {
|
||||
regexTraits := traits.CreateElement("jenkins.scm.impl.trait.RegexSCMHeadFilterTrait")
|
||||
regexTraits.CreateAttr("plugin", "scm-api@2.4.0")
|
||||
regexTraits.CreateElement("regex").SetText(gitSource.RegexFilter)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetGitSourcefromEtree(source *etree.Element) *devopsv1alpha3.GitSource {
|
||||
var gitSource devopsv1alpha3.GitSource
|
||||
if credential := source.SelectElement("credentialsId"); credential != nil {
|
||||
gitSource.CredentialId = credential.Text()
|
||||
}
|
||||
if remote := source.SelectElement("remote"); remote != nil {
|
||||
gitSource.Url = remote.Text()
|
||||
}
|
||||
|
||||
traits := source.SelectElement("traits")
|
||||
if branchDiscoverTrait := traits.SelectElement(
|
||||
"jenkins.plugins.git.traits.BranchDiscoveryTrait"); branchDiscoverTrait != nil {
|
||||
gitSource.DiscoverBranches = true
|
||||
}
|
||||
if tagDiscoverTrait := traits.SelectElement(
|
||||
"jenkins.plugins.git.traits.TagDiscoveryTrait"); tagDiscoverTrait != nil {
|
||||
gitSource.DiscoverTags = true
|
||||
}
|
||||
if cloneTrait := traits.SelectElement(
|
||||
"jenkins.plugins.git.traits.CloneOptionTrait"); cloneTrait != nil {
|
||||
if cloneExtension := cloneTrait.SelectElement(
|
||||
"extension"); cloneExtension != nil {
|
||||
gitSource.CloneOption = &devopsv1alpha3.GitCloneOption{}
|
||||
if value, err := strconv.ParseBool(cloneExtension.SelectElement("shallow").Text()); err == nil {
|
||||
gitSource.CloneOption.Shallow = value
|
||||
}
|
||||
if value, err := strconv.ParseInt(cloneExtension.SelectElement("timeout").Text(), 10, 32); err == nil {
|
||||
gitSource.CloneOption.Timeout = int(value)
|
||||
}
|
||||
if value, err := strconv.ParseInt(cloneExtension.SelectElement("depth").Text(), 10, 32); err == nil {
|
||||
gitSource.CloneOption.Depth = int(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
if regexTrait := traits.SelectElement(
|
||||
"jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"); regexTrait != nil {
|
||||
if regex := regexTrait.SelectElement("regex"); regex != nil {
|
||||
gitSource.RegexFilter = regex.Text()
|
||||
}
|
||||
}
|
||||
return &gitSource
|
||||
}
|
||||
Reference in New Issue
Block a user