diff --git a/app/cmd/job_build.go b/app/cmd/job_build.go index c8ae9afa14fdbe931abd3b785ea3c90ab2fa7848..32e05375b3c9541646e4fc716d391a05675dbc93 100644 --- a/app/cmd/job_build.go +++ b/app/cmd/job_build.go @@ -1,8 +1,11 @@ package cmd import ( + "encoding/json" "fmt" + "log" + "github.com/AlecAivazis/survey" "github.com/linuxsuren/jenkins-cli/client" "github.com/spf13/cobra" ) @@ -40,6 +43,43 @@ var jobBuildCmd = &cobra.Command{ jclient.Proxy = jenkins.Proxy jclient.ProxyAuth = jenkins.ProxyAuth - jclient.Build(jobOption.Name) + paramDefs := []client.ParameterDefinition{} + hasParam := false + if job, err := jclient.GetJob(jobOption.Name); err == nil { + fmt.Println(job.Property) + if len(job.Property) != 0 { + for _, pro := range job.Property { + if len(pro.ParameterDefinitions) == 0 { + continue + } + + if data, err := json.MarshalIndent(pro.ParameterDefinitions, "", " "); err == nil { + content := string(data) + prompt := &survey.Editor{ + Message: "Edit your pipeline script", + FileName: "*.sh", + Default: content, + AppendDefault: true, + } + + if err = survey.AskOne(prompt, &content); err != nil { + log.Fatal(err) + } + + if err = json.Unmarshal([]byte(content), ¶mDefs); err != nil { + log.Fatal(err) + } + } + hasParam = true + break + } + } + } + + if hasParam { + jclient.BuildWithParams(jobOption.Name, paramDefs) + } else { + jclient.Build(jobOption.Name) + } }, } diff --git a/client/job.go b/client/job.go index 7db6b205aa87782ac9b18cc4e7ad92ea2eacd791..eacde4ab5512f1f8278a3726dd2f06b6affb50ff 100644 --- a/client/job.go +++ b/client/job.go @@ -78,7 +78,7 @@ func (q *JobClient) Build(jobName string) (err error) { code := response.StatusCode var data []byte data, err = ioutil.ReadAll(response.Body) - if code == 200 { + if code == 201 { // Jenkins will send redirect by this api fmt.Println("build successfully") } else { log.Fatal(string(data)) @@ -131,6 +131,56 @@ func (q *JobClient) GetBuild(jobName string, id int) (job *JobBuild, err error) return } +func (q *JobClient) BuildWithParams(jobName string, parameters []ParameterDefinition) (err error) { + jobItems := strings.Split(jobName, " ") + path := "" + for _, item := range jobItems { + path = fmt.Sprintf("%s/job/%s", path, item) + } + + api := fmt.Sprintf("%s/%s/build", q.URL, path) + var ( + req *http.Request + response *http.Response + ) + + var paramJSON []byte + + if len(parameters) == 1 { + paramJSON, err = json.Marshal(parameters[0]) + } else { + paramJSON, err = json.Marshal(parameters) + } + + formData := url.Values{"json": {fmt.Sprintf("{\"parameter\": %s}", string(paramJSON))}} + payload := strings.NewReader(formData.Encode()) + req, err = http.NewRequest("POST", api, payload) + if err == nil { + q.AuthHandle(req) + } else { + return + } + + if err = q.CrumbHandle(req); err != nil { + log.Fatal(err) + } + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + client := q.GetClient() + if response, err = client.Do(req); err == nil { + code := response.StatusCode + var data []byte + data, err = ioutil.ReadAll(response.Body) + if code == 201 { // Jenkins will send redirect by this api + fmt.Println("build successfully") + } else { + log.Fatal(string(data)) + } + } else { + log.Fatal(err) + } + return +} + func (q *JobClient) GetJob(name string) (job *Job, err error) { jobItems := strings.Split(name, " ") path := "" @@ -339,6 +389,25 @@ type Job struct { NextBuildNumber int URL string Buildable bool + + Property []ParametersDefinitionProperty +} + +type ParametersDefinitionProperty struct { + ParameterDefinitions []ParameterDefinition +} + +type ParameterDefinition struct { + Description string + Name string `json:"name"` + Type string + Value string `json:"value"` + DefaultParameterValue DefaultParameterValue +} + +type DefaultParameterValue struct { + Description string + Value string } type SimpleJobBuild struct {