未验证 提交 7c3092fe 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Merge pull request #248 from LinuxSuRen/stop-last-build

Add support to stop the last build
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -18,8 +19,8 @@ func init() { ...@@ -18,8 +19,8 @@ func init() {
var jobCmd = &cobra.Command{ var jobCmd = &cobra.Command{
Use: "job", Use: "job",
Short: "Manage the job of your Jenkins", Short: i18n.T("Manage the job of your Jenkins"),
Long: `Manage the job of your Jenkins Long: i18n.T(`Manage the job of your Jenkins
Editing the pipeline job needs to install a plugin which is pipeline-restful-api Editing the pipeline job needs to install a plugin which is pipeline-restful-api
https://plugins.jenkins.io/pipeline-restful-api`, https://plugins.jenkins.io/pipeline-restful-api`),
} }
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
...@@ -27,15 +28,18 @@ var jobArtifactDownloadOption JobArtifactDownloadOption ...@@ -27,15 +28,18 @@ var jobArtifactDownloadOption JobArtifactDownloadOption
func init() { func init() {
jobArtifactCmd.AddCommand(jobArtifactDownloadCmd) jobArtifactCmd.AddCommand(jobArtifactDownloadCmd)
jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.ID, "id", "i", "", "ID of the job artifact") jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.ID, "id", "i", "",
jobArtifactDownloadCmd.Flags().BoolVarP(&jobArtifactDownloadOption.ShowProgress, "progress", "", true, "Whether show the progress") i18n.T("ID of the job artifact"))
jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.DownloadDir, "download-dir", "", "", "The directory which artifact will be downloaded") jobArtifactDownloadCmd.Flags().BoolVarP(&jobArtifactDownloadOption.ShowProgress, "progress", "", true,
i18n.T("Whether show the progress"))
jobArtifactDownloadCmd.Flags().StringVarP(&jobArtifactDownloadOption.DownloadDir, "download-dir", "", "",
i18n.T("The directory which artifact will be downloaded"))
} }
var jobArtifactDownloadCmd = &cobra.Command{ var jobArtifactDownloadCmd = &cobra.Command{
Use: "download <jobName> [buildID]", Use: "download <jobName> [buildID]",
Short: "Download the artifact of target job", Short: i18n.T("Download the artifact of target job"),
Long: `Download the artifact of target job`, Long: i18n.T(`Download the artifact of target job`),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
argLen := len(args) argLen := len(args)
......
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"log" "log"
"net/http" "net/http"
...@@ -32,8 +33,8 @@ func init() { ...@@ -32,8 +33,8 @@ func init() {
var jobBuildCmd = &cobra.Command{ var jobBuildCmd = &cobra.Command{
Use: "build <jobName>", Use: "build <jobName>",
Short: "Build the job of your Jenkins", Short: i18n.T("Build the job of your Jenkins"),
Long: `Build the job of your Jenkins`, Long: i18n.T("Build the job of your Jenkins"),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
name := args[0] name := args[0]
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -28,10 +28,10 @@ func init() { ...@@ -28,10 +28,10 @@ func init() {
var jobCreateCmd = &cobra.Command{ var jobCreateCmd = &cobra.Command{
Use: "create <jobName>", Use: "create <jobName>",
Short: "Create a job in your Jenkins", Short: i18n.T("Create a job in your Jenkins"),
Long: `Create a job in your Jenkins`, Long: i18n.T(`Create a job in your Jenkins`),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) (err error) {
jobName := args[0] jobName := args[0]
jclient := &client.JobClient{ jclient := &client.JobClient{
JenkinsCore: client.JenkinsCore{ JenkinsCore: client.JenkinsCore{
...@@ -41,7 +41,6 @@ var jobCreateCmd = &cobra.Command{ ...@@ -41,7 +41,6 @@ var jobCreateCmd = &cobra.Command{
getCurrentJenkinsAndClient(&(jclient.JenkinsCore)) getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
var createMode string var createMode string
var err error
if createMode, err = jobCreateOption.getCreateMode(jclient); err == nil { if createMode, err = jobCreateOption.getCreateMode(jclient); err == nil {
payload := client.CreateJobPayload{ payload := client.CreateJobPayload{
Name: jobName, Name: jobName,
...@@ -54,7 +53,7 @@ var jobCreateCmd = &cobra.Command{ ...@@ -54,7 +53,7 @@ var jobCreateCmd = &cobra.Command{
} }
err = jclient.Create(payload) err = jclient.Create(payload)
} }
helper.CheckErr(cmd, err) return
}, },
} }
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
...@@ -26,8 +27,8 @@ func init() { ...@@ -26,8 +27,8 @@ func init() {
var jobDeleteCmd = &cobra.Command{ var jobDeleteCmd = &cobra.Command{
Use: "delete <jobName>", Use: "delete <jobName>",
Short: "Delete a job in your Jenkins", Short: i18n.T("Delete a job in your Jenkins"),
Long: `Delete a job in your Jenkins`, Long: i18n.T("Delete a job in your Jenkins"),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
jobName := args[0] jobName := args[0]
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -25,22 +24,21 @@ var jobEditOption JobEditOption ...@@ -25,22 +24,21 @@ var jobEditOption JobEditOption
func init() { func init() {
jobCmd.AddCommand(jobEditCmd) jobCmd.AddCommand(jobEditCmd)
jobEditCmd.Flags().StringVarP(&jobEditOption.URL, "url", "", "", jobEditCmd.Flags().StringVarP(&jobEditOption.URL, "url", "", "",
"URL of the Jenkinsfile to files to use to replace pipeline") i18n.T("URL of the Jenkinsfile to files to use to replace pipeline"))
jobEditCmd.Flags().StringVarP(&jobEditOption.Filename, "filename", "f", "", jobEditCmd.Flags().StringVarP(&jobEditOption.Filename, "filename", "f", "",
"Filename to files to use to replace pipeline") i18n.T("Filename to files to use to replace pipeline"))
jobEditCmd.Flags().StringVarP(&jobEditOption.Script, "script", "s", "", jobEditCmd.Flags().StringVarP(&jobEditOption.Script, "script", "s", "",
"Script to use to replace pipeline. Use script first if you give filename at the meantime.") i18n.T("Script to use to replace pipeline. Use script first if you give filename at the meantime."))
} }
var jobEditCmd = &cobra.Command{ var jobEditCmd = &cobra.Command{
Use: "edit <jobName>", Use: "edit <jobName>",
Short: "Edit the job of your Jenkins", Short: i18n.T("Edit the job of your Jenkins"),
Long: `Edit the job of your Jenkins. We only support to edit the pipeline job.`, Long: i18n.T(`Edit the job of your Jenkins. We only support to edit the pipeline job.`),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) (err error) {
name := args[0] name := args[0]
var content string var content string
var err error
jclient := &client.JobClient{ jclient := &client.JobClient{
JenkinsCore: client.JenkinsCore{ JenkinsCore: client.JenkinsCore{
...@@ -52,7 +50,7 @@ var jobEditCmd = &cobra.Command{ ...@@ -52,7 +50,7 @@ var jobEditCmd = &cobra.Command{
if content, err = jobEditOption.getPipeline(jclient, name); err == nil { if content, err = jobEditOption.getPipeline(jclient, name); err == nil {
err = jclient.UpdatePipeline(name, content) err = jclient.UpdatePipeline(name, content)
} }
helper.CheckErr(cmd, err) return
}, },
} }
......
...@@ -3,7 +3,7 @@ package cmd ...@@ -3,7 +3,7 @@ package cmd
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/util" "github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -26,10 +26,10 @@ func init() { ...@@ -26,10 +26,10 @@ func init() {
var jobHistoryCmd = &cobra.Command{ var jobHistoryCmd = &cobra.Command{
Use: "history <jobName>", Use: "history <jobName>",
Short: "Print the history of job in your Jenkins", Short: i18n.T("Print the history of job in your Jenkins"),
Long: `Print the history of job in your Jenkins`, Long: i18n.T(`Print the history of job in your Jenkins`),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) (err error) {
jobName := args[0] jobName := args[0]
jClient := &client.JobClient{ jClient := &client.JobClient{
...@@ -39,7 +39,8 @@ var jobHistoryCmd = &cobra.Command{ ...@@ -39,7 +39,8 @@ var jobHistoryCmd = &cobra.Command{
} }
getCurrentJenkinsAndClient(&(jClient.JenkinsCore)) getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
builds, err := jClient.GetHistory(jobName) var builds []*client.JobBuild
builds, err = jClient.GetHistory(jobName)
if err == nil { if err == nil {
var data []byte var data []byte
data, err = jobHistoryOption.Output(builds) data, err = jobHistoryOption.Output(builds)
...@@ -47,7 +48,7 @@ var jobHistoryCmd = &cobra.Command{ ...@@ -47,7 +48,7 @@ var jobHistoryCmd = &cobra.Command{
cmd.Print(string(data)) cmd.Print(string(data))
} }
} }
helper.CheckErr(cmd, err) return
}, },
} }
......
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
...@@ -27,19 +28,16 @@ var jobInputOption JobInputOption ...@@ -27,19 +28,16 @@ var jobInputOption JobInputOption
func init() { func init() {
jobCmd.AddCommand(jobInputCmd) jobCmd.AddCommand(jobInputCmd)
jobInputCmd.Flags().StringVarP(&jobInputOption.Action, "action", "", "", "The action wether you want to process or abort.") jobInputCmd.Flags().StringVarP(&jobInputOption.Action, "action", "", "",
i18n.T("The action whether you want to process or abort."))
} }
var jobInputCmd = &cobra.Command{ var jobInputCmd = &cobra.Command{
Use: "input <jobName> [buildID]", Use: "input <jobName> [buildID]",
Short: "Input a job in your Jenkins", Short: i18n.T("Input a job in your Jenkins"),
Long: `Input a job in your Jenkins`, Long: i18n.T("Input a job in your Jenkins"),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Help()
return
}
jobName := args[0] jobName := args[0]
buildID := -1 buildID := -1
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/spf13/cobra"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -54,16 +53,12 @@ var _ = Describe("job input command", func() { ...@@ -54,16 +53,12 @@ var _ = Describe("job input command", func() {
rootCmd.SetArgs([]string{"job", "input"}) rootCmd.SetArgs([]string{"job", "input"})
jobInputCmd.SetHelpFunc(func(cmd *cobra.Command, _ []string) {
cmd.Print("help")
})
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
rootCmd.SetOutput(buf) rootCmd.SetOutput(buf)
_, err = rootCmd.ExecuteC() _, err = rootCmd.ExecuteC()
Expect(err).To(BeNil()) Expect(err).To(HaveOccurred())
Expect(buf.String()).To(Equal("help")) Expect(buf.String()).To(ContainSubstring("Error: requires at least 1 arg(s)"))
}) })
It("should success, abort without inputs", func() { It("should success, abort without inputs", func() {
......
package cmd package cmd
import ( import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"time" "time"
...@@ -24,16 +25,19 @@ var jobLogOption JobLogOption ...@@ -24,16 +25,19 @@ var jobLogOption JobLogOption
func init() { func init() {
jobCmd.AddCommand(jobLogCmd) jobCmd.AddCommand(jobLogCmd)
jobLogCmd.Flags().IntVarP(&jobLogOption.History, "history", "s", -1, "Specific build history of log") jobLogCmd.Flags().IntVarP(&jobLogOption.History, "history", "s", -1,
jobLogCmd.Flags().BoolVarP(&jobLogOption.Watch, "watch", "w", false, "Watch the job logs") i18n.T("Specific build history of log"))
jobLogCmd.Flags().IntVarP(&jobLogOption.Interval, "interval", "i", 1, "Interval of watch") jobLogCmd.Flags().BoolVarP(&jobLogOption.Watch, "watch", "w", false,
i18n.T("Watch the job logs"))
jobLogCmd.Flags().IntVarP(&jobLogOption.Interval, "interval", "i", 1,
i18n.T("Interval of watch"))
} }
var jobLogCmd = &cobra.Command{ var jobLogCmd = &cobra.Command{
Use: "log <jobName> [buildID]", Use: "log <jobName> [buildID]",
Short: "Print the job's log of your Jenkins", Short: i18n.T("Print the job's log of your Jenkins"),
Long: `Print the job's log of your Jenkins Long: i18n.T(`Print the job's log of your Jenkins
It'll print the log text of the last build if you don't give the build id.`, It'll print the log text of the last build if you don't give the build id.`),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
name := args[0] name := args[0]
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
...@@ -29,8 +30,8 @@ func init() { ...@@ -29,8 +30,8 @@ func init() {
var jobParamCmd = &cobra.Command{ var jobParamCmd = &cobra.Command{
Use: "param <jobName>", Use: "param <jobName>",
Short: "Get parameters of the job of your Jenkins", Short: i18n.T("Get parameters of the job of your Jenkins"),
Long: `Get parameters of the job of your Jenkins`, Long: i18n.T("Get parameters of the job of your Jenkins"),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
name := args[0] name := args[0]
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"strings" "strings"
...@@ -24,16 +25,18 @@ var jobSearchOption JobSearchOption ...@@ -24,16 +25,18 @@ var jobSearchOption JobSearchOption
func init() { func init() {
jobCmd.AddCommand(jobSearchCmd) jobCmd.AddCommand(jobSearchCmd)
jobSearchCmd.Flags().IntVarP(&jobSearchOption.Max, "max", "", 10, "The number of limitation to print") jobSearchCmd.Flags().IntVarP(&jobSearchOption.Max, "max", "", 10,
jobSearchCmd.Flags().BoolVarP(&jobSearchOption.PrintAll, "all", "", false, "Print all items if there's no keyword") i18n.T("The number of limitation to print"))
jobSearchCmd.Flags().BoolVarP(&jobSearchOption.PrintAll, "all", "", false,
i18n.T("Print all items if there's no keyword"))
jobSearchCmd.Flags().StringVarP(&jobSearchOption.Format, "output", "o", "json", jobSearchCmd.Flags().StringVarP(&jobSearchOption.Format, "output", "o", "json",
`Formats of the output which contain name, path`) i18n.T(`Formats of the output which contain name, path`))
} }
var jobSearchCmd = &cobra.Command{ var jobSearchCmd = &cobra.Command{
Use: "search [keyword]", Use: "search [keyword]",
Short: "Print the job of your Jenkins", Short: i18n.T("Print the job of your Jenkins"),
Long: `Print the job of your Jenkins`, Long: i18n.T(`Print the job of your Jenkins`),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !jobSearchOption.PrintAll && len(args) == 0 { if !jobSearchOption.PrintAll && len(args) == 0 {
cmd.Help() cmd.Help()
......
...@@ -2,13 +2,11 @@ package cmd ...@@ -2,13 +2,11 @@ package cmd
import ( import (
"fmt" "fmt"
"net/http" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"strconv"
"github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/http"
"strconv"
) )
// JobStopOption is the job stop option // JobStopOption is the job stop option
...@@ -26,18 +24,16 @@ func init() { ...@@ -26,18 +24,16 @@ func init() {
} }
var jobStopCmd = &cobra.Command{ var jobStopCmd = &cobra.Command{
Use: "stop <jobName> <buildNumber>", Use: "stop <jobName> [buildNumber]",
Short: "Stop a job build in your Jenkins", Short: i18n.T("Stop a job build in your Jenkins"),
Long: `Stop a job build in your Jenkins`, Long: i18n.T("Stop a job build in your Jenkins"),
Args: cobra.MinimumNArgs(2), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) (err error) {
var ( buildNum := -1
buildNum int if len(args) > 1 {
err error if buildNum, err = strconv.Atoi(args[1]); err != nil {
) return
if buildNum, err = strconv.Atoi(args[1]); err != nil { }
cmd.PrintErrln(err)
return
} }
jobName := args[0] jobName := args[0]
...@@ -52,7 +48,6 @@ var jobStopCmd = &cobra.Command{ ...@@ -52,7 +48,6 @@ var jobStopCmd = &cobra.Command{
} }
getCurrentJenkinsAndClient(&(jclient.JenkinsCore)) getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
err = jclient.StopJob(jobName, buildNum) return jclient.StopJob(jobName, buildNum)
helper.CheckErr(cmd, err)
}, },
} }
...@@ -70,7 +70,7 @@ var _ = Describe("job stop command", func() { ...@@ -70,7 +70,7 @@ var _ = Describe("job stop command", func() {
roundTripper.EXPECT(). roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil) RoundTrip(requestCrumb).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"job", "stop", jobName, "1", "-b", "true"}) rootCmd.SetArgs([]string{"job", "stop", jobName, "1", "-b"})
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
rootCmd.SetOutput(buf) rootCmd.SetOutput(buf)
...@@ -79,5 +79,59 @@ var _ = Describe("job stop command", func() { ...@@ -79,5 +79,59 @@ var _ = Describe("job stop command", func() {
Expect(buf.String()).To(Equal("")) Expect(buf.String()).To(Equal(""))
}) })
It("stop the last build, with batch mode", func() {
data, err := generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
Expect(err).To(BeNil())
jobName := "fakeJob"
request, _ := http.NewRequest("POST", fmt.Sprintf("http://localhost:8080/jenkins/job/%s/lastBuild/stop", jobName), nil)
request.Header.Add("CrumbRequestField", "Crumb")
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
response := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/crumbIssuer/api/json", nil)
requestCrumb.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
responseCrumb := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
Request: requestCrumb,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{"crumbRequestField":"CrumbRequestField","crumb":"Crumb"}
`)),
}
roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil)
rootCmd.SetArgs([]string{"job", "stop", jobName, "-b"})
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
_, err = rootCmd.ExecuteC()
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal(""))
})
It("stop the last build, with batch mode", func() {
jobName := "fakeJob"
rootCmd.SetArgs([]string{"job", "stop", jobName, "not-number", "-b"})
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
_, err := rootCmd.ExecuteC()
Expect(err).To(HaveOccurred())
Expect(buf.String()).To(ContainSubstring("Error: strconv.Atoi: parsing"))
})
}) })
}) })
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http" "net/http"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
...@@ -28,8 +29,8 @@ func init() { ...@@ -28,8 +29,8 @@ func init() {
var jobTypeCmd = &cobra.Command{ var jobTypeCmd = &cobra.Command{
Use: "type", Use: "type",
Short: "Print the types of job which in your Jenkins", Short: i18n.T("Print the types of job which in your Jenkins"),
Long: `Print the types of job which in your Jenkins`, Long: i18n.T("Print the types of job which in your Jenkins"),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
jclient := &client.JobClient{ jclient := &client.JobClient{
JenkinsCore: client.JenkinsCore{ JenkinsCore: client.JenkinsCore{
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"log" "log"
"os/exec" "os/exec"
"runtime" "runtime"
...@@ -22,15 +23,17 @@ var openOption OpenOption ...@@ -22,15 +23,17 @@ var openOption OpenOption
func init() { func init() {
rootCmd.AddCommand(openCmd) rootCmd.AddCommand(openCmd)
openCmd.Flags().StringVarP(&openOption.Name, "name", "n", "", "Open a specific Jenkins by name") openCmd.Flags().StringVarP(&openOption.Name, "name", "n", "",
openCmd.Flags().BoolVarP(&openOption.Config, "config", "c", false, "Open the configuration page of Jenkins") i18n.T("Open a specific Jenkins by name"))
openCmd.Flags().BoolVarP(&openOption.Config, "config", "c", false,
i18n.T("Open the configuration page of Jenkins"))
openOption.SetFlag(openCmd) openOption.SetFlag(openCmd)
} }
var openCmd = &cobra.Command{ var openCmd = &cobra.Command{
Use: "open [config name]", Use: "open [config name]",
Short: "Open your Jenkins with a browse", Short: i18n.T("Open your Jenkins with a browse"),
Long: `Open your Jenkins with a browse`, Long: i18n.T(`Open your Jenkins with a browse`),
Example: `jcli open -n <config name>`, Example: `jcli open -n <config name>`,
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
var jenkins *JenkinsServer var jenkins *JenkinsServer
......
...@@ -3,6 +3,7 @@ package cmd ...@@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jenkins-zh/jenkins-cli/app/helper" "github.com/jenkins-zh/jenkins-cli/app/helper"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -23,8 +24,8 @@ func init() { ...@@ -23,8 +24,8 @@ func init() {
var pluginCheckCmd = &cobra.Command{ var pluginCheckCmd = &cobra.Command{
Use: "check", Use: "check",
Short: "Check update center server", Short: i18n.T("Check update center server"),
Long: `Check update center server`, Long: i18n.T(`Check update center server`),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
jClient := &client.PluginManager{ jClient := &client.PluginManager{
JenkinsCore: client.JenkinsCore{ JenkinsCore: client.JenkinsCore{
......
...@@ -33,8 +33,8 @@ func init() { ...@@ -33,8 +33,8 @@ func init() {
var pluginInstallCmd = &cobra.Command{ var pluginInstallCmd = &cobra.Command{
Use: "install [pluginName]", Use: "install [pluginName]",
Short: i18n.T("Install the plugins"), Short: i18n.T("Install the plugins"),
Long: `Install the plugins Long: i18n.T(`Install the plugins
Allow you to install a plugin with or without the version`, Allow you to install a plugin with or without the version`),
Example: `jcli plugin install localization-zh-cn Example: `jcli plugin install localization-zh-cn
jcli plugin install localization-zh-cn@1.0.9 jcli plugin install localization-zh-cn@1.0.9
`, `,
......
...@@ -8,7 +8,7 @@ msgstr "" ...@@ -8,7 +8,7 @@ msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: EMAIL\n" "Report-Msgid-Bugs-To: EMAIL\n"
"POT-Creation-Date: 2019-11-21 13:00+0800\n" "POT-Creation-Date: 2019-11-21 13:00+0800\n"
"PO-Revision-Date: 2019-11-21 22:17+0800\n" "PO-Revision-Date: 2019-11-22 23:01+0800\n"
"Language-Team: \n" "Language-Team: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
...@@ -18,17 +18,45 @@ msgstr "" ...@@ -18,17 +18,45 @@ msgstr ""
"Plural-Forms: nplurals=1; plural=0;\n" "Plural-Forms: nplurals=1; plural=0;\n"
"Language: zh_CN\n" "Language: zh_CN\n"
#: app/cmd/job_type.go:32 app/cmd/job_type.go:33
msgid "Print the types of job which in your Jenkins"
msgstr ""
#: app/cmd/plugin_check.go:27 app/cmd/plugin_check.go:28
msgid "Check update center server"
msgstr ""
#: app/cmd/center_download.go:44
msgid "Download Jenkins from a mirror site. You can get more mirror sites from https://jenkins-zh.cn/tutorial/management/mirror/"
msgstr ""
#: app/cmd/job.go:22
msgid "Manage the job of your Jenkins"
msgstr ""
#: app/cmd/job_artifact_download.go:34
msgid "Whether show the progress"
msgstr "是否显示进度条"
#: app/cmd/job_edit.go:29
msgid "Filename to files to use to replace pipeline"
msgstr ""
#: app/cmd/plugin_download.go:36
msgid "Download the plugins"
msgstr "下载插件"
#: app/cmd/restart.go:29 app/cmd/restart.go:30 #: app/cmd/restart.go:29 app/cmd/restart.go:30
msgid "Restart your Jenkins" msgid "Restart your Jenkins"
msgstr "" msgstr "重启 Jenkins"
#: app/cmd/root.go:79 #: app/cmd/root.go:79
msgid "Select a Jenkins server for this time" msgid "Select a Jenkins server for this time"
msgstr "选择本次执行的 Jenkins" msgstr "选择本次执行的 Jenkins"
#: app/cmd/config_add.go:30 #: app/cmd/config_add.go:30
msgid "Proxy of the Jenkins" msgid "Proxy of the Jenkins"
msgstr "" msgstr "Jenkins 的代理"
#: app/cmd/plugin_upgrade.go:32 app/cmd/plugin_upgrade.go:33 #: app/cmd/plugin_upgrade.go:32 app/cmd/plugin_upgrade.go:33
msgid "Upgrade the specific plugin" msgid "Upgrade the specific plugin"
...@@ -39,8 +67,8 @@ msgid "Manage the config of jcli" ...@@ -39,8 +67,8 @@ msgid "Manage the config of jcli"
msgstr "管理 jcli 的配置" msgstr "管理 jcli 的配置"
#: app/cmd/config_generate.go:28 #: app/cmd/config_generate.go:28
msgid "Copy the output into clipboard" msgid "Copy the output into clipboard"
msgstr "拷贝输出到剪贴板" msgstr "拷贝输出到剪贴板"
#: app/cmd/root.go:77 #: app/cmd/root.go:77
msgid "An alternative config file" msgid "An alternative config file"
...@@ -50,9 +78,25 @@ msgstr "指定另外一个配置文件" ...@@ -50,9 +78,25 @@ msgstr "指定另外一个配置文件"
msgid "List all Jenkins config items" msgid "List all Jenkins config items"
msgstr "列出所有的 Jenkins 配置项" msgstr "列出所有的 Jenkins 配置项"
#: app/cmd/job_edit.go:31
msgid "Script to use to replace pipeline. Use script first if you give filename at the meantime."
msgstr ""
#: app/cmd/job_input.go:37 app/cmd/job_input.go:38
msgid "Input a job in your Jenkins"
msgstr ""
#: app/cmd/job_log.go:38
msgid "Print the job's log of your Jenkins"
msgstr "输出任务的日志"
#: app/cmd/config_generate.go:34 app/cmd/config_generate.go:35
msgid "Generate a sample config file for you"
msgstr "为你生成一份样本配置文件"
#: app/cmd/doc.go:30 app/cmd/doc.go:31 #: app/cmd/doc.go:30 app/cmd/doc.go:31
msgid "Generate document for all jcl commands" msgid "Generate document for all jcl commands"
msgstr "为所有的 jcli 命令生成文档" msgstr "为所有的 jcli 命令生成文档"
#: app/cmd/root.go:37 #: app/cmd/root.go:37
msgid "jcli is a tool which could help you with your multiple Jenkins" msgid "jcli is a tool which could help you with your multiple Jenkins"
...@@ -75,8 +119,8 @@ msgid "Print the version of Jenkins CLI" ...@@ -75,8 +119,8 @@ msgid "Print the version of Jenkins CLI"
msgstr "打印 Jenkins CLI 的版本" msgstr "打印 Jenkins CLI 的版本"
#: app/cmd/completion.go:14 app/cmd/completion.go:15 #: app/cmd/completion.go:14 app/cmd/completion.go:15
msgid "Genereate bash completion scripts" msgid "Genereate bash completion scripts"
msgstr "生成 bash 自动补全的脚本" msgstr "生成 bash 自动补全的脚本"
#: app/cmd/plugin_download.go:31 app/cmd/plugin_install.go:29 #: app/cmd/plugin_download.go:31 app/cmd/plugin_install.go:29
msgid "If you want to show the progress of download a plugin" msgid "If you want to show the progress of download a plugin"
...@@ -84,7 +128,7 @@ msgstr "你是否希望显示插件下载的进度" ...@@ -84,7 +128,7 @@ msgstr "你是否希望显示插件下载的进度"
#: app/cmd/plugin_download.go:37 #: app/cmd/plugin_download.go:37
msgid "Download the plugins which contain the target plugin and its dependencies" msgid "Download the plugins which contain the target plugin and its dependencies"
msgstr "" msgstr "下载插件及其依赖"
#: app/cmd/plugin_uninstall.go:26 app/cmd/plugin_uninstall.go:27 #: app/cmd/plugin_uninstall.go:26 app/cmd/plugin_uninstall.go:27
msgid "Uninstall the plugins" msgid "Uninstall the plugins"
...@@ -99,44 +143,32 @@ msgid "Whether skip the post command hook" ...@@ -99,44 +143,32 @@ msgid "Whether skip the post command hook"
msgstr "是否跳过后置命令钩子" msgstr "是否跳过后置命令钩子"
#: app/cmd/center_download.go:32 #: app/cmd/center_download.go:32
msgid "Version of the Jenkins which you want to download" msgid "Version of the Jenkins which you want to download"
msgstr "" msgstr "你希望下载的 Jenkins 版本"
#: app/cmd/config_add.go:22 #: app/cmd/config_add.go:22
msgid "Name of the Jenkins" msgid "Name of the Jenkins"
msgstr "" msgstr "Jenkins 的名称"
#: app/cmd/config_list.go:28 app/cmd/config_list.go:29
msgid "List all Jenkins config items"
msgstr ""
#: app/cmd/config_generate.go:34 app/cmd/config_generate.go:35
msgid "Generate a sample config file for you"
msgstr ""
#: app/cmd/config_add.go:28 #: app/cmd/config_add.go:28
msgid "Token of the Jenkins" msgid "Token of the Jenkins"
msgstr "" msgstr "Jenkins 的令牌"
#: app/cmd/config_add.go:32 #: app/cmd/config_add.go:32
msgid "ProxyAuth of the Jenkins" msgid "ProxyAuth of the Jenkins"
msgstr "" msgstr ""
#: app/cmd/root.go:36
msgid "jcli is a tool which could help you with your multiple Jenkins"
msgstr ""
#: app/cmd/center_watch.go:27 #: app/cmd/center_watch.go:27
msgid "The watch will be continue util Jenkins needs restart" msgid "The watch will be continue util Jenkins needs restart"
msgstr "" msgstr ""
#: app/cmd/config_add.go:26 #: app/cmd/config_add.go:26
msgid "UserName of the Jenkins" msgid "UserName of the Jenkins"
msgstr "" msgstr "Jenkins 的用户名"
#: app/cmd/config_edit.go:21 app/cmd/config_edit.go:22 #: app/cmd/config_edit.go:21 app/cmd/config_edit.go:22
msgid "Edit a Jenkins config" msgid "Edit a Jenkins config"
msgstr "" msgstr "编辑 Jenkins 配置"
#: app/cmd/plugin_download.go:25 #: app/cmd/plugin_download.go:25
msgid "If you want to skip download dependency of plugin" msgid "If you want to skip download dependency of plugin"
...@@ -144,7 +176,7 @@ msgstr "你是否想要跳过下载依赖插件" ...@@ -144,7 +176,7 @@ msgstr "你是否想要跳过下载依赖插件"
#: app/cmd/plugin_upload.go:45 #: app/cmd/plugin_upload.go:45
msgid "Password of remote plugin URL" msgid "Password of remote plugin URL"
msgstr "" msgstr "远程插件的秘密"
#: app/cmd/plugin_upload.go:58 #: app/cmd/plugin_upload.go:58
msgid "Upload a plugin to your Jenkins" msgid "Upload a plugin to your Jenkins"
...@@ -156,15 +188,15 @@ msgstr "管理你的 Jenkins 更新中心" ...@@ -156,15 +188,15 @@ msgstr "管理你的 Jenkins 更新中心"
#: app/cmd/job_artifact.go:33 app/cmd/job_artifact.go:34 #: app/cmd/job_artifact.go:33 app/cmd/job_artifact.go:34
msgid "Print the artifact list of target job" msgid "Print the artifact list of target job"
msgstr "" msgstr "输出目标任务的归档文件列表"
#: app/cmd/plugin_download.go:27 #: app/cmd/job_search.go:29
msgid "If you want to skip download optional dependency of plugin" msgid "The number of limitation to print"
msgstr "" msgstr "输出的数量限制"
#: app/cmd/plugin_download.go:36 #: app/cmd/open.go:29
msgid "Download the plugins" msgid "Open the configuration page of Jenkins"
msgstr "下载插件" msgstr ""
#: app/cmd/plugin_install.go:35 #: app/cmd/plugin_install.go:35
msgid "Install the plugins" msgid "Install the plugins"
...@@ -188,52 +220,20 @@ msgstr "你是否想要从镜像站点下载插件" ...@@ -188,52 +220,20 @@ msgstr "你是否想要从镜像站点下载插件"
#: app/cmd/plugin_list.go:35 app/cmd/plugin_list.go:36 #: app/cmd/plugin_list.go:35 app/cmd/plugin_list.go:36
msgid "Print all the plugins which are installed" msgid "Print all the plugins which are installed"
msgstr "" msgstr "输出已经安装的插件"
#: app/cmd/center_watch.go:29 #: app/cmd/center_watch.go:29
msgid "The watch will be continue util all Jenkins plugins installation is completed" msgid "The watch will be continue util all Jenkins plugins installation is completed"
msgstr "" msgstr ""
#: app/cmd/common.go:101 app/cmd/config_generate.go:26 #: app/cmd/common.go:101 app/cmd/config_generate.go:26
msgid "Interactive mode" msgid "Interactive mode"
msgstr "" msgstr "交互模式"
#: app/cmd/plugin_download.go:29 app/cmd/plugin_install.go:27
msgid "If you want to download plugin from a mirror site"
msgstr ""
#: app/cmd/plugin_upload.go:43 #: app/cmd/plugin_upload.go:43
msgid "User of remote plugin URL" msgid "User of remote plugin URL"
msgstr "" msgstr ""
#: app/cmd/plugin_upload.go:47
msgid "Remote Jenkins which will find from config list"
msgstr ""
#: app/cmd/plugin_upload.go:58
msgid "Upload a plugin to your Jenkins"
msgstr ""
#: app/cmd/root.go:76
msgid "An alternative config file"
msgstr ""
#: app/cmd/center.go:31 app/cmd/center.go:32
msgid "Manage your update center"
msgstr ""
#: app/cmd/center_download.go:36
msgid "If you want to show the download progress"
msgstr ""
#: app/cmd/completion.go:14 app/cmd/completion.go:15
msgid "Genereate bash completion scripts"
msgstr ""
#: app/cmd/config.go:32 app/cmd/config.go:33
msgid "Manage the config of jcli"
msgstr "管理 jcli 的配置信息"
#: app/cmd/plugin_upload.go:59 #: app/cmd/plugin_upload.go:59
msgid "Upload a plugin from local filesystem or remote URL to your Jenkins" msgid "Upload a plugin from local filesystem or remote URL to your Jenkins"
msgstr "上传来自本地文件系统或者远程 URL 的插件到你的 Jenkins" msgstr "上传来自本地文件系统或者远程 URL 的插件到你的 Jenkins"
...@@ -69,7 +69,13 @@ func (q *JobClient) BuildWithParams(jobName string, parameters []ParameterDefini ...@@ -69,7 +69,13 @@ func (q *JobClient) BuildWithParams(jobName string, parameters []ParameterDefini
// StopJob stops a job build // StopJob stops a job build
func (q *JobClient) StopJob(jobName string, num int) (err error) { func (q *JobClient) StopJob(jobName string, num int) (err error) {
path := ParseJobPath(jobName) path := ParseJobPath(jobName)
api := fmt.Sprintf("%s/%d/stop", path, num)
var api string
if num <= 0 {
api = fmt.Sprintf("%s/lastBuild/stop", path)
} else {
api = fmt.Sprintf("%s/%d/stop", path, num)
}
_, err = q.RequestWithoutData("POST", api, nil, nil, 200) _, err = q.RequestWithoutData("POST", api, nil, nil, 200)
return return
......
...@@ -219,6 +219,36 @@ var _ = Describe("job test", func() { ...@@ -219,6 +219,36 @@ var _ = Describe("job test", func() {
err := jobClient.StopJob(jobName, buildID) err := jobClient.StopJob(jobName, buildID)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
}) })
It("stop the last job build without a folder", func() {
jobName := "fakeJob"
buildID := -1
request, _ := http.NewRequest("POST", fmt.Sprintf("%s/job/%s/lastBuild/stop", jobClient.URL, jobName), nil)
request.Header.Add("CrumbRequestField", "Crumb")
response := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)
requestCrumb, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", jobClient.URL, "/crumbIssuer/api/json"), nil)
responseCrumb := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
Request: requestCrumb,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{"crumbRequestField":"CrumbRequestField","crumb":"Crumb"}
`)),
}
roundTripper.EXPECT().
RoundTrip(requestCrumb).Return(responseCrumb, nil)
err := jobClient.StopJob(jobName, buildID)
Expect(err).To(BeNil())
})
}) })
Context("GetJob", func() { Context("GetJob", func() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册