@@ -19,18 +19,10 @@ const (
1919 ciType = server .CIServerType ("jenkins" )
2020)
2121
22- type jenkinsJobName string
23-
2422type jobOptions struct {
2523 Jenkins jenkinsOption `mapstructure:"jenkins"`
2624 ci.CIConfig `mapstructure:",squash"`
2725 JobName jenkinsJobName `mapstructure:"jobName"`
28- // SCM scm.SCMInfo `mapstructure:"scm"`
29- // Pipeline pipeline `mapstructure:"pipeline"`
30-
31- // used in package
32- // CIFileConfig *cifile.CIFileConfig `mapstructure:"ci"`
33- // ProjectRepo *git.RepoInfo `mapstructure:"projectRepo"`
3426}
3527
3628func newJobOptions (options configmanager.RawOptions ) (* jobOptions , error ) {
@@ -41,38 +33,18 @@ func newJobOptions(options configmanager.RawOptions) (*jobOptions, error) {
4133 return & opts , nil
4234}
4335
44- func (n jenkinsJobName ) getJobName () string {
45- jobNameStr := string (n )
46- if strings .Contains (jobNameStr , "/" ) {
47- return strings .Split (jobNameStr , "/" )[1 ]
48- }
49- return jobNameStr
50- }
51-
52- func (n jenkinsJobName ) getJobFolder () string {
53- jobNameStr := string (n )
54- if strings .Contains (jobNameStr , "/" ) {
55- return strings .Split (jobNameStr , "/" )[0 ]
36+ func (j * jobOptions ) install (jenkinsClient jenkins.JenkinsAPI , secretToken string ) error {
37+ // 1. install jenkins plugins
38+ pipelinePlugins := j .extractPlugins ()
39+ if err := ensurePluginInstalled (jenkinsClient , pipelinePlugins ); err != nil {
40+ return err
5641 }
57- return ""
58- }
59-
60- func (n jenkinsJobName ) checkValid () error {
61- jobNameStr := string (n )
62- if strings .Contains (jobNameStr , "/" ) {
63- strs := strings .Split (jobNameStr , "/" )
64- if len (strs ) != 2 || len (strs [0 ]) == 0 || len (strs [1 ]) == 0 {
65- return fmt .Errorf ("jenkins jobName illegal: %s" , n )
66- }
42+ // 2. config plugins by casc
43+ if err := configPlugins (jenkinsClient , pipelinePlugins ); err != nil {
44+ return err
6745 }
68- return nil
69- }
70-
71- func (j * jobOptions ) extractPlugins () []step.StepConfigAPI {
72- stepConfigs := step .ExtractValidStepConfig (j .Pipeline )
73- // add repo plugin for repoInfo
74- stepConfigs = append (stepConfigs , step .GetRepoStepConfig (j .ProjectRepo )... )
75- return stepConfigs
46+ // 3. create or update jenkins job
47+ return j .createOrUpdateJob (jenkinsClient , secretToken )
7648}
7749
7850func (j * jobOptions ) remove (jenkinsClient jenkins.JenkinsAPI ) error {
@@ -120,16 +92,39 @@ func (j *jobOptions) createOrUpdateJob(jenkinsClient jenkins.JenkinsAPI, secretT
12092 return nil
12193}
12294
123- func (j * jobOptions ) install (jenkinsClient jenkins.JenkinsAPI , secretToken string ) error {
124- // 1. install jenkins plugins
125- pipelinePlugins := j .extractPlugins ()
126- if err := ensurePluginInstalled (jenkinsClient , pipelinePlugins ); err != nil {
127- return err
95+ func (j * jobOptions ) extractPlugins () []step.StepConfigAPI {
96+ stepConfigs := step .ExtractValidStepConfig (j .Pipeline )
97+ // add repo plugin for repoInfo
98+ stepConfigs = append (stepConfigs , step .GetRepoStepConfig (j .ProjectRepo )... )
99+ return stepConfigs
100+ }
101+
102+ // jenkins jobName, can be like folder/jobName or jobName
103+ type jenkinsJobName string
104+
105+ func (n jenkinsJobName ) getJobName () string {
106+ jobNameStr := string (n )
107+ if strings .Contains (jobNameStr , "/" ) {
108+ return strings .Split (jobNameStr , "/" )[1 ]
128109 }
129- // 2. config plugins by casc
130- if err := configPlugins (jenkinsClient , pipelinePlugins ); err != nil {
131- return err
110+ return jobNameStr
111+ }
112+
113+ func (n jenkinsJobName ) getJobFolder () string {
114+ jobNameStr := string (n )
115+ if strings .Contains (jobNameStr , "/" ) {
116+ return strings .Split (jobNameStr , "/" )[0 ]
132117 }
133- // 3. create or update jenkins job
134- return j .createOrUpdateJob (jenkinsClient , secretToken )
118+ return ""
119+ }
120+
121+ func (n jenkinsJobName ) checkValid () error {
122+ jobNameStr := string (n )
123+ if strings .Contains (jobNameStr , "/" ) {
124+ strs := strings .Split (jobNameStr , "/" )
125+ if len (strs ) != 2 || len (strs [0 ]) == 0 || len (strs [1 ]) == 0 {
126+ return fmt .Errorf ("jenkins jobName illegal: %s" , n )
127+ }
128+ }
129+ return nil
135130}
0 commit comments