Skip to content

Commit 1a57cb7

Browse files
committed
feat: improve job history info
1 parent 7f1c402 commit 1a57cb7

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

app/cmd/job_history.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ var jobHistoryOption JobHistoryOption
2121

2222
func init() {
2323
jobCmd.AddCommand(jobHistoryCmd)
24-
jobHistoryOption.SetFlagWithHeaders(jobHistoryCmd, "DisplayName,Building,Result")
24+
jobHistoryOption.SetFlagWithHeaders(jobHistoryCmd, "ID,DisplayName,Description,Building,Result")
2525
jobHistoryCmd.Flags().IntVarP(&jobHistoryOption.Delete, "delete", "d", -1, "Delete a history item")
26+
27+
jobHistoryCmd.AddCommand(createJobHistoryEditCmd())
2628
}
2729

2830
var jobHistoryCmd = &cobra.Command{

app/cmd/job_history_edit.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"github.com/jenkins-zh/jenkins-cli/client"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
type jobHistoryEdit struct {
9+
displayName string
10+
description string
11+
id int
12+
}
13+
14+
func createJobHistoryEditCmd() (cmd *cobra.Command) {
15+
opt := &jobHistoryEdit{}
16+
cmd = &cobra.Command{
17+
Use: "edit",
18+
Short: "Edit job history",
19+
RunE: opt.RunE,
20+
Args: cobra.MinimumNArgs(1),
21+
}
22+
23+
flags := cmd.Flags()
24+
flags.StringVarP(&opt.displayName, "displayName", "d", "", "Display name of target job history")
25+
flags.StringVarP(&opt.description, "description", "m", "", "Description of target job history")
26+
flags.IntVarP(&opt.id, "id", "i", -1, "ID of job history")
27+
return
28+
}
29+
30+
func (o *jobHistoryEdit) RunE(cmd *cobra.Command, args []string) (err error) {
31+
jobName := args[0]
32+
33+
jClient := &client.JobClient{
34+
JenkinsCore: client.JenkinsCore{
35+
RoundTripper: jobHistoryOption.RoundTripper,
36+
},
37+
}
38+
getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
39+
40+
err = jClient.EditBuild(jobName, o.id, o.displayName, o.description)
41+
return
42+
}

client/job.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,32 @@ func (q *JobClient) GetBuild(jobName string, id int) (job *JobBuild, err error)
9999
api = fmt.Sprintf("%s/%d/api/json", path, id)
100100
}
101101

102-
err = q.RequestWithData("GET", api, nil, nil, 200, &job)
102+
err = q.RequestWithData(http.MethodGet, api, nil, nil, 200, &job)
103+
return
104+
}
105+
106+
func (q *JobClient) EditBuild(jobName string, buildID int, displayName, description string) (err error) {
107+
path := ParseJobPath(jobName)
108+
var api string
109+
if buildID == -1 {
110+
err = fmt.Errorf("build id is required")
111+
return
112+
} else {
113+
api = fmt.Sprintf("%s/%d/configSubmit", path, buildID)
114+
}
115+
116+
formData := url.Values{}
117+
formData.Add("displayName", displayName)
118+
formData.Add("description", description)
119+
formData.Add("Submit", "")
120+
formData.Add("core:apply", "")
121+
formData.Add("json", fmt.Sprintf(`{"displayName":"%s","description":"%s","Submit":"","core:apply":"}`, displayName, description))
122+
payload := strings.NewReader(formData.Encode())
123+
124+
header := map[string]string{
125+
"Content-Type": "application/x-www-form-urlencoded",
126+
}
127+
_, err = q.RequestWithoutData(http.MethodPost, api, header, payload, http.StatusFound)
103128
return
104129
}
105130

0 commit comments

Comments
 (0)