From 3a3f957d816118427b31de7d48f65008d9ea8f39 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie Date: Mon, 1 Jul 2019 10:34:58 +0800 Subject: [PATCH] Allow user to edit a pipeline --- app/cmd/job.go | 2 +- app/cmd/job_edit.go | 75 ++++++++++++++++++++++++++++++++++++ app/cmd/plugin.go | 7 ++++ app/cmd/restart.go | 1 + client/job.go | 94 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 app/cmd/job_edit.go diff --git a/app/cmd/job.go b/app/cmd/job.go index a51790a..d151a09 100644 --- a/app/cmd/job.go +++ b/app/cmd/job.go @@ -21,7 +21,7 @@ func init() { rootCmd.AddCommand(jobCmd) jobCmd.PersistentFlags().StringVarP(&jobOption.Format, "output", "o", "json", "Format the output") jobCmd.PersistentFlags().StringVarP(&jobOption.Name, "name", "n", "", "Name of the job") - jobCmd.PersistentFlags().BoolVarP(&jobOption.History, "history", "", false, "Print the build history of job") + jobCmd.Flags().BoolVarP(&jobOption.History, "history", "", false, "Print the build history of job") } var jobCmd = &cobra.Command{ diff --git a/app/cmd/job_edit.go b/app/cmd/job_edit.go new file mode 100644 index 0000000..d598e1c --- /dev/null +++ b/app/cmd/job_edit.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "errors" + "fmt" + "log" + + "github.com/AlecAivazis/survey" + "github.com/linuxsuren/jenkins-cli/client" + "github.com/spf13/cobra" +) + +func init() { + jobCmd.AddCommand(jobEditCmd) +} + +var jobEditCmd = &cobra.Command{ + Use: "edit -n", + Short: "Edit the job of your Jenkins", + Long: `Edit the job of your Jenkins`, + Args: func(cmd *cobra.Command, args []string) error { + if jobOption.Name == "" { + return errors.New("requires job name") + } + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + var content string + var err error + if content, err = getPipeline(jobOption.Name); err != nil { + log.Fatal(err) + } + + prompt := &survey.Editor{ + Message: "Edit your pipeline script", + FileName: "*.sh", + Default: content, + AppendDefault: true, + } + + fmt.Println(content) + if err = survey.AskOne(prompt, &content); err != nil { + log.Fatal(err) + } + fmt.Println(content) + + jenkins := getCurrentJenkins() + jclient := &client.JobClient{} + jclient.URL = jenkins.URL + jclient.UserName = jenkins.UserName + jclient.Token = jenkins.Token + jclient.Proxy = jenkins.Proxy + jclient.ProxyAuth = jenkins.ProxyAuth + if err = jclient.UpdatePipeline(jobOption.Name, content); err != nil { + fmt.Println("update failed") + log.Fatal(err) + } + }, +} + +func getPipeline(name string) (script string, err error) { + jenkins := getCurrentJenkins() + jclient := &client.JobClient{} + jclient.URL = jenkins.URL + jclient.UserName = jenkins.UserName + jclient.Token = jenkins.Token + jclient.Proxy = jenkins.Proxy + jclient.ProxyAuth = jenkins.ProxyAuth + + var job *client.Pipeline + if job, err = jclient.GetPipeline(name); err == nil { + script = job.Script + } + return +} diff --git a/app/cmd/plugin.go b/app/cmd/plugin.go index a00a5bf..3d4d41f 100644 --- a/app/cmd/plugin.go +++ b/app/cmd/plugin.go @@ -50,6 +50,13 @@ var pluginCmd = &cobra.Command{ Use: "plugin", Short: "Manage the plugins of Jenkins", Long: `Manage the plugins of Jenkins`, + PreRun: func(cmd *cobra.Command, args []string) { + fmt.Printf("Inside subCmd PreRun with args: %v\n", args) + + if pluginOpt.Upload { + fmt.Println("sdfsdf") + } + }, Run: func(cmd *cobra.Command, args []string) { if pluginOpt.Upload { crumb, config := getCrumb() diff --git a/app/cmd/restart.go b/app/cmd/restart.go index 50828fd..7511a51 100644 --- a/app/cmd/restart.go +++ b/app/cmd/restart.go @@ -48,6 +48,7 @@ var restartCmd = &cobra.Command{ if response.StatusCode != 200 { if data, err := ioutil.ReadAll(response.Body); err == nil { fmt.Println(string(data)) + fmt.Println(response.StatusCode) } else { log.Fatal(err) } diff --git a/client/job.go b/client/job.go index aca76bf..9006956 100644 --- a/client/job.go +++ b/client/job.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "strconv" "strings" ) @@ -125,6 +126,94 @@ func (q *JobClient) GetJob(name string) (job *Job, err error) { return } +func (q *JobClient) UpdatePipeline(name, script string) (err error) { + jobItems := strings.Split(name, " ") + path := "" + for i, item := range jobItems { + if i == 0 { + path = fmt.Sprintf("job/%s", item) + } else { + path = fmt.Sprintf("%s/job/%s", path, item) + } + } + + api := fmt.Sprintf("%s/%s/wfapisu/update", q.URL, path) + var ( + req *http.Request + response *http.Response + ) + + fmt.Println(api) + formData := url.Values{"script": {script}} + 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") + // req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") + // 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 == 200 { + fmt.Println("updated") + } else { + fmt.Println("code", code) + log.Fatal(string(data)) + } + } else { + fmt.Println("request is error") + log.Fatal(err) + } + return +} + +func (q *JobClient) GetPipeline(name string) (pipeline *Pipeline, err error) { + jobItems := strings.Split(name, " ") + path := "" + for _, item := range jobItems { + path = fmt.Sprintf("%s/job/%s", path, item) + } + + api := fmt.Sprintf("%s/%s/wfapisu/script", q.URL, path) + var ( + req *http.Request + response *http.Response + ) + + req, err = http.NewRequest("GET", api, nil) + if err == nil { + q.AuthHandle(req) + } else { + return + } + + 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 == 200 { + pipeline = &Pipeline{} + err = json.Unmarshal(data, pipeline) + } else { + log.Fatal(string(data)) + } + } else { + log.Fatal(err) + } + return +} + func (q *JobClient) GetHistory(name string) (builds []JobBuild, err error) { var job *Job if job, err = q.GetJob(name); err == nil { @@ -213,3 +302,8 @@ type JobBuild struct { Number int URL string } + +type Pipeline struct { + Script string + Sandbox bool +} -- GitLab