mirror of
https://github.com/jenkinsci/kubernetes-operator.git
synced 2026-10-20 02:38:43 +02:00
Add Bitbucket trigger for seed job
This commit is contained in:
@@ -70,6 +70,9 @@ import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
|
||||
{{ if .GitHubPushTrigger }}
|
||||
import com.cloudbees.jenkins.GitHubPushTrigger;
|
||||
{{ end }}
|
||||
{{ if .BitbucketPushTrigger }}
|
||||
import com.cloudbees.jenkins.plugins.BitBucketTrigger;
|
||||
{{ end }}
|
||||
import hudson.model.FreeStyleProject;
|
||||
import hudson.model.labels.LabelAtom;
|
||||
import hudson.plugins.git.BranchSpec;
|
||||
@@ -128,6 +131,10 @@ jobRef.addTrigger(new SCMTrigger("{{ .PollSCM }}"))
|
||||
jobRef.addTrigger(new GitHubPushTrigger())
|
||||
{{ end }}
|
||||
|
||||
{{ if .BitbucketPushTrigger }}
|
||||
jobRef.addTrigger(new BitBucketTrigger())
|
||||
{{ end }}
|
||||
|
||||
{{ if .BuildPeriodically }}
|
||||
jobRef.addTrigger(new TimerTrigger("{{ .BuildPeriodically }}"))
|
||||
{{ end}}
|
||||
@@ -470,6 +477,7 @@ func seedJobCreatingGroovyScript(seedJob v1alpha2.SeedJob) (string, error) {
|
||||
Targets string
|
||||
RepositoryBranch string
|
||||
RepositoryURL string
|
||||
BitbucketPushTrigger bool
|
||||
GitHubPushTrigger bool
|
||||
BuildPeriodically string
|
||||
PollSCM string
|
||||
@@ -485,6 +493,7 @@ func seedJobCreatingGroovyScript(seedJob v1alpha2.SeedJob) (string, error) {
|
||||
Targets: seedJob.Targets,
|
||||
RepositoryBranch: seedJob.RepositoryBranch,
|
||||
RepositoryURL: seedJob.RepositoryURL,
|
||||
BitbucketPushTrigger: seedJob.BitbucketPushTrigger,
|
||||
GitHubPushTrigger: seedJob.GitHubPushTrigger,
|
||||
BuildPeriodically: seedJob.BuildPeriodically,
|
||||
PollSCM: seedJob.PollSCM,
|
||||
|
||||
@@ -103,6 +103,14 @@ func (s *SeedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if seedJob.BitbucketPushTrigger {
|
||||
if msg := s.validateBitbucketPushTrigger(jenkins); len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
@@ -119,24 +127,39 @@ func (s *SeedJobs) validateSchedule(job v1alpha2.SeedJob, str string, key string
|
||||
|
||||
func (s *SeedJobs) validateGitHubPushTrigger(jenkins v1alpha2.Jenkins) []string {
|
||||
var messages []string
|
||||
if err := checkPluginExists(jenkins, "github"); err != nil {
|
||||
return append(messages, fmt.Sprintf("githubPushTrigger cannot be enabled: %s", err))
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
func (s *SeedJobs) validateBitbucketPushTrigger(jenkins v1alpha2.Jenkins) []string {
|
||||
var messages []string
|
||||
if err := checkPluginExists(jenkins, "bitbucket"); err != nil {
|
||||
return append(messages, fmt.Sprintf("bitbucketPushTrigger cannot be enabled: %s", err))
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
func checkPluginExists(jenkins v1alpha2.Jenkins, name string) error {
|
||||
exists := false
|
||||
for _, plugin := range jenkins.Spec.Master.BasePlugins {
|
||||
if plugin.Name == "github" {
|
||||
if plugin.Name == name {
|
||||
exists = true
|
||||
}
|
||||
}
|
||||
|
||||
userExists := false
|
||||
for _, plugin := range jenkins.Spec.Master.Plugins {
|
||||
if plugin.Name == "github" {
|
||||
if plugin.Name == name {
|
||||
userExists = true
|
||||
}
|
||||
}
|
||||
|
||||
if !exists && !userExists {
|
||||
messages = append(messages, "githubPushTrigger is set. This function requires `github` plugin installed in .Spec.Master.Plugins because seed jobs Push Trigger function needs it")
|
||||
return fmt.Errorf("`%s` plugin not installed", name)
|
||||
}
|
||||
return messages
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SeedJobs) validateIfIDIsUnique(seedJobs []v1alpha2.SeedJob) []string {
|
||||
|
||||
@@ -645,9 +645,9 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, result, []string{"seedJob `example` githubPushTrigger is set. This function requires `github` plugin installed in .Spec.Master.Plugins because seed jobs Push Trigger function needs it"})
|
||||
assert.Equal(t, result, []string{"seedJob `example` githubPushTrigger cannot be enabled: `github` plugin not installed"})
|
||||
})
|
||||
t.Run("Invalid with set githubPushTrigger and not installed github plugin", func(t *testing.T) {
|
||||
t.Run("Valid with set githubPushTrigger and installed github plugin", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
Spec: v1alpha2.JenkinsSpec{
|
||||
SeedJobs: []v1alpha2.SeedJob{
|
||||
@@ -680,6 +680,74 @@ func TestValidateSeedJobs(t *testing.T) {
|
||||
seedJobs := New(nil, config, logf.ZapLogger(false))
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
t.Run("Invalid with set bitbucketPushTrigger and not installed bitbucket plugin", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
Spec: v1alpha2.JenkinsSpec{
|
||||
SeedJobs: []v1alpha2.SeedJob{
|
||||
{
|
||||
ID: "example",
|
||||
CredentialID: "jenkins-operator-e2e",
|
||||
JenkinsCredentialType: v1alpha2.NoJenkinsCredentialCredentialType,
|
||||
Targets: "cicd/jobs/*.jenkins",
|
||||
RepositoryBranch: "master",
|
||||
RepositoryURL: "https://github.com/jenkinsci/kubernetes-operator.git",
|
||||
BitbucketPushTrigger: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fakeClient := fake.NewFakeClient()
|
||||
|
||||
config := configuration.Configuration{
|
||||
Client: fakeClient,
|
||||
ClientSet: kubernetes.Clientset{},
|
||||
Notifications: nil,
|
||||
}
|
||||
|
||||
seedJobs := New(nil, config, logf.ZapLogger(false))
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, result, []string{"seedJob `example` bitbucketPushTrigger cannot be enabled: `bitbucket` plugin not installed"})
|
||||
})
|
||||
t.Run("Valid with set bitbucketPushTrigger and installed Bitbucket plugin", func(t *testing.T) {
|
||||
jenkins := v1alpha2.Jenkins{
|
||||
Spec: v1alpha2.JenkinsSpec{
|
||||
SeedJobs: []v1alpha2.SeedJob{
|
||||
{
|
||||
ID: "example",
|
||||
CredentialID: "jenkins-operator-e2e",
|
||||
JenkinsCredentialType: v1alpha2.NoJenkinsCredentialCredentialType,
|
||||
Targets: "cicd/jobs/*.jenkins",
|
||||
RepositoryBranch: "master",
|
||||
RepositoryURL: "https://github.com/jenkinsci/kubernetes-operator.git",
|
||||
BitbucketPushTrigger: true,
|
||||
},
|
||||
},
|
||||
Master: v1alpha2.JenkinsMaster{
|
||||
Plugins: []v1alpha2.Plugin{
|
||||
{Name: "bitbucket", Version: "latest"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fakeClient := fake.NewFakeClient()
|
||||
|
||||
config := configuration.Configuration{
|
||||
Client: fakeClient,
|
||||
ClientSet: kubernetes.Clientset{},
|
||||
Notifications: nil,
|
||||
}
|
||||
|
||||
seedJobs := New(nil, config, logf.ZapLogger(false))
|
||||
result, err := seedJobs.ValidateSeedJobs(jenkins)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user