提交 d5a0ac35 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Add support to build a job with parameters

上级 070ad591
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"log"
"github.com/AlecAivazis/survey"
"github.com/linuxsuren/jenkins-cli/client" "github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -40,6 +43,43 @@ var jobBuildCmd = &cobra.Command{ ...@@ -40,6 +43,43 @@ var jobBuildCmd = &cobra.Command{
jclient.Proxy = jenkins.Proxy jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth 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), &paramDefs); err != nil {
log.Fatal(err)
}
}
hasParam = true
break
}
}
}
if hasParam {
jclient.BuildWithParams(jobOption.Name, paramDefs)
} else {
jclient.Build(jobOption.Name)
}
}, },
} }
...@@ -78,7 +78,7 @@ func (q *JobClient) Build(jobName string) (err error) { ...@@ -78,7 +78,7 @@ func (q *JobClient) Build(jobName string) (err error) {
code := response.StatusCode code := response.StatusCode
var data []byte var data []byte
data, err = ioutil.ReadAll(response.Body) data, err = ioutil.ReadAll(response.Body)
if code == 200 { if code == 201 { // Jenkins will send redirect by this api
fmt.Println("build successfully") fmt.Println("build successfully")
} else { } else {
log.Fatal(string(data)) log.Fatal(string(data))
...@@ -131,6 +131,56 @@ func (q *JobClient) GetBuild(jobName string, id int) (job *JobBuild, err error) ...@@ -131,6 +131,56 @@ func (q *JobClient) GetBuild(jobName string, id int) (job *JobBuild, err error)
return 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) { func (q *JobClient) GetJob(name string) (job *Job, err error) {
jobItems := strings.Split(name, " ") jobItems := strings.Split(name, " ")
path := "" path := ""
...@@ -339,6 +389,25 @@ type Job struct { ...@@ -339,6 +389,25 @@ type Job struct {
NextBuildNumber int NextBuildNumber int
URL string URL string
Buildable bool 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 { type SimpleJobBuild struct {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册