Add Bitbucket trigger for seed job

This commit is contained in:
Riccardo Biraghi 2020-02-05 16:17:49 +00:00
parent c16ca15521
commit 0f5cd0334d
6 changed files with 121 additions and 8 deletions

4
go.mod
View File

@ -20,11 +20,11 @@ require (
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.3.0
go.uber.org/zap v1.9.1
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83 // indirect
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 // indirect
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20191004055002-72853e10c5a3 // indirect
golang.org/x/tools v0.0.0-20200205141839-4abfd4a1628e // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
k8s.io/api v0.0.0-20190612125737-db0771252981

9
go.sum
View File

@ -419,6 +419,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83 h1:mgAKeshyNqWKdENOnQsg+8dRTwZFIwFaO3HNl52sweA=
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@ -426,6 +428,9 @@ golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f h1:hX65Cu3JDlGH3uEdK7I99Ii+9kjD6mvnnpfLdEAH0x4=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 h1:0IiAsCRByjO2QjX7ZPkw5oU9x+n1YqRL802rjC0c3Aw=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -507,7 +512,11 @@ golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190408170212-12dd9f86f350/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191004055002-72853e10c5a3 h1:2AmBLzhAfXj+2HCW09VCkJtHIYgHTIPcTeYqgP7Bwt0=
golang.org/x/tools v0.0.0-20191004055002-72853e10c5a3/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200205141839-4abfd4a1628e h1:JdEzLb04S2BEPrWqUDsgo7JXSq8pQlzIHbrBQN5o7TY=
golang.org/x/tools v0.0.0-20200205141839-4abfd4a1628e/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=

View File

@ -552,6 +552,10 @@ type SeedJob struct {
// +optional
JenkinsCredentialType JenkinsCredentialType `json:"credentialType,omitempty"`
// BitbucketPushTrigger is used for Bitbucket web hooks
// +optional
BitbucketPushTrigger bool `json:"bitbucketPushTrigger"`
// GitHubPushTrigger is used for GitHub web hooks
// +optional
GitHubPushTrigger bool `json:"githubPushTrigger"`

View File

@ -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,

View File

@ -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 {

View File

@ -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)
})