From c87026e090c32085ef5bcab48a828c85c04defe9 Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 27 Jan 2021 19:40:32 +0800 Subject: [PATCH 1/6] Add support to delete job history --- app/cmd/job_history.go | 9 ++++++++- app/cmd/root.go | 2 +- client/job.go | 8 ++++++++ go.mod | 2 +- go.sum | 6 ++---- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/cmd/job_history.go b/app/cmd/job_history.go index 2bfd2dde..63480813 100644 --- a/app/cmd/job_history.go +++ b/app/cmd/job_history.go @@ -13,6 +13,7 @@ import ( type JobHistoryOption struct { cobra_ext.OutputOption + Delete int RoundTripper http.RoundTripper } @@ -21,10 +22,11 @@ var jobHistoryOption JobHistoryOption func init() { jobCmd.AddCommand(jobHistoryCmd) jobHistoryOption.SetFlagWithHeaders(jobHistoryCmd, "DisplayName,Building,Result") + jobHistoryCmd.Flags().IntVarP(&jobHistoryOption.Delete, "delete", "d", -1, "Delete a history item") } var jobHistoryCmd = &cobra.Command{ - Use: "history ", + Use: "history", Short: i18n.T("Print the history of job in your Jenkins"), Long: i18n.T(`Print the history of job in your Jenkins`), Args: cobra.MinimumNArgs(1), @@ -38,6 +40,11 @@ var jobHistoryCmd = &cobra.Command{ } getCurrentJenkinsAndClientOrDie(&(jClient.JenkinsCore)) + if jobHistoryOption.Delete != -1 { + err = jClient.DeleteHistory(jobName, jobHistoryOption.Delete) + return + } + var builds []*client.JobBuild builds, err = jClient.GetHistory(jobName) if err == nil { diff --git a/app/cmd/root.go b/app/cmd/root.go index cc296e20..4208151c 100644 --- a/app/cmd/root.go +++ b/app/cmd/root.go @@ -211,7 +211,7 @@ func init() { rootCmd.SetOut(os.Stdout) - goPlugin.LoadPlugins(rootCmd) + goPlugin.LoadPlugins(rootCmd, "jenkins-zh", "jcli-plugins") if rootOptions.GetGitHubClient() == nil { rootOptions.SetGitHubClient(github.NewClient(nil)) diff --git a/client/job.go b/client/job.go index 6c005947..57b760a0 100644 --- a/client/job.go +++ b/client/job.go @@ -281,6 +281,14 @@ func (q *JobClient) GetHistory(name string) (builds []*JobBuild, err error) { return } +// DeleteHistory returns the build history of a job +func (q *JobClient) DeleteHistory(jobName string, num int) (err error) { + path := ParseJobPath(jobName) + api := fmt.Sprintf("%s/%d/doDelete", path, num) + _, err = q.RequestWithoutData(http.MethodPost, api, nil, nil, 200) + return +} + // Log get the log of a job func (q *JobClient) Log(jobName string, history int, start int64) (jobLog JobLog, err error) { path := ParseJobPath(jobName) diff --git a/go.mod b/go.mod index 37e70961..a430082a 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/jenkins-zh/jenkins-formulas v0.0.5 github.com/linuxsuren/cobra-extension v0.0.10 github.com/linuxsuren/go-cli-alias v0.0.4 - github.com/linuxsuren/go-cli-plugin v0.0.3 + github.com/linuxsuren/go-cli-plugin v0.0.4 github.com/linuxsuren/http-downloader v0.0.10 github.com/magiconair/properties v1.8.4 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index bd61d030..796c3889 100644 --- a/go.sum +++ b/go.sum @@ -190,8 +190,6 @@ github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/linuxsuren/cobra-extension v0.0.1 h1:e288zsm1iWUaAOjfiw42hiWJsrrXZnJ0vKApWPyTfkQ= -github.com/linuxsuren/cobra-extension v0.0.1/go.mod h1:UqHi31q7Dj+46nzcVWNZz5Z1JcHBJcmCyPAZRoM8LXo= github.com/linuxsuren/cobra-extension v0.0.3 h1:06tdJNYM4Wcx6WZPDnz/WLuP2jVdVZ9RLuwlUHIblmg= github.com/linuxsuren/cobra-extension v0.0.3/go.mod h1:UqHi31q7Dj+46nzcVWNZz5Z1JcHBJcmCyPAZRoM8LXo= github.com/linuxsuren/cobra-extension v0.0.6/go.mod h1:qcEJv7BbL0UpK6MbrTESP/nKf1+z1wQdMAnE1NBl3QQ= @@ -200,8 +198,8 @@ github.com/linuxsuren/cobra-extension v0.0.10/go.mod h1:nDsXgvm0lSWVV+byAEfwhIGF github.com/linuxsuren/go-cli-alias v0.0.4 h1:+otarDOeSZzzbTCr9CllAOQCQNpaf/HI41iDvWWUo/w= github.com/linuxsuren/go-cli-alias v0.0.4/go.mod h1:dfwOx8H0iVpdS9gtLC80GCC4cvDvzWejxjxXn7sFWUs= github.com/linuxsuren/go-cli-plugin v0.0.1/go.mod h1:uyO09KK8otYfDV5LVTfcWX2UbAi3kEz3PrkdfIxnDlg= -github.com/linuxsuren/go-cli-plugin v0.0.3 h1:thb6d4I6CaPOKxGBFsltBEsQYGkHGnJCZXjjYPpzc9g= -github.com/linuxsuren/go-cli-plugin v0.0.3/go.mod h1:42kKvBOmdnVjxU73XF8nllAat1YiVrOE176Dn9qeTSg= +github.com/linuxsuren/go-cli-plugin v0.0.4 h1:73q0OOVJh9+9kt9GgAb9hUFIH2ZvmFODpO2QLN9rnnY= +github.com/linuxsuren/go-cli-plugin v0.0.4/go.mod h1:N0fu8YFzVKucXgytFATEB3icJgZg3iTN7AUw0qsstxw= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec h1:N6xw+W4IvgOV0cmhE7iUWKHNZqMUXlT3in+1ORmqIGo= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI= github.com/linuxsuren/http-downloader v0.0.6/go.mod h1:xxgh2OE7WGL9TwDE9L8Gh7Lqq9fFPuHbh5tofUitEfE= From 38ebb0c06877fbc1453efba0e7d4d7b5d863d35f Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 27 Jan 2021 20:20:31 +0800 Subject: [PATCH 2/6] Add e2e tests for jcli job history -d --- e2e/job_test.go | 20 ++++++++++++++++++++ e2e/setup_test.go | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/e2e/job_test.go b/e2e/job_test.go index d0e8f13f..ba3a4e8d 100644 --- a/e2e/job_test.go +++ b/e2e/job_test.go @@ -2,8 +2,11 @@ package e2e import ( "fmt" + "math" + "math/rand" "os/exec" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -12,7 +15,24 @@ func TestListJobType(t *testing.T) { cmd := exec.Command("jcli", "job", "type", "--url", GetJenkinsURL()) fmt.Println(cmd.String()) data, err := cmd.CombinedOutput() + fmt.Println(string(data)) + assert.Nil(t, err) + + rand.Seed(math.MaxInt8) + name := fmt.Sprintf("%d", rand.Int()) + cmd = exec.Command("jcli", "job", "create", name, "--type", "hudson.model.FreeStyleProject", "--url", GetJenkinsURL(), "--logger-level", "debug") + data, err = cmd.CombinedOutput() + fmt.Println(string(data)) assert.Nil(t, err) + cmd = exec.Command("jcli", "job", "build", name, "-b", "--url", GetJenkinsURL()) + data, err = cmd.CombinedOutput() fmt.Println(string(data)) + assert.Nil(t, err) + time.Sleep(time.Second * 6) + + cmd = exec.Command("jcli", "job", "history", name, "-d", "1", "--url", GetJenkinsURL()) + data, err = cmd.CombinedOutput() + fmt.Println(string(data)) + assert.Nil(t, err) } diff --git a/e2e/setup_test.go b/e2e/setup_test.go index 0b80dade..646f35b8 100644 --- a/e2e/setup_test.go +++ b/e2e/setup_test.go @@ -40,7 +40,7 @@ func TestMain(m *testing.M) { jenkinsURL = fmt.Sprintf("http://localhost:%d", port) cmd := exec.Command("jcli", "center", "start", "--random-web-dir", "--setup-wizard=false", - "--port", fmt.Sprintf("%d", port), "--version", version, "--thread", "10") + "--port", fmt.Sprintf("%d", port), "--version", version, "--thread", "10", "--clean-home") fmt.Println(cmd.String()) cmdStderrPipe, _ := cmd.StderrPipe() if err = cmd.Start(); err != nil { From 47c570acd832d922ceabc0ae6cd60a9d23638865 Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 27 Jan 2021 20:22:52 +0800 Subject: [PATCH 3/6] Add gosec check with GitHub action --- .github/workflows/pull-request.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 784a04b0..832606df 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -75,6 +75,18 @@ jobs: uses: Jerome1337/golint-action@v1.0.2 with: golint-path: util/... + Security: + name: Security + runs-on: ubuntu-latest + env: + GO111MODULE: on + steps: + - name: Checkout Source + uses: actions/checkout@v2 + - name: Run Gosec Security Scanner + uses: securego/gosec@master + with: + args: '-exclude=G402,G204,G304,G110 app/cmd/common' # - name: SonarCloud Scan # uses: LinuxSuRen/sonarcloud-github-action@master From e5bab59b8ce3b3d1021f8a5781686f2531d47f0c Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 27 Jan 2021 20:27:16 +0800 Subject: [PATCH 4/6] Cancel the previous workflow --- .github/workflows/pull-request.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 832606df..40d5943e 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -10,6 +10,10 @@ jobs: name: Build runs-on: ubuntu-20.04 steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ secrets.GITHUB_TOKEN_SECRETS }} - name: Set up Go 1.15 uses: actions/setup-go@v2.1.3 with: From 8f8f15e5656f3ec1ea2f0d0792592065d00585d7 Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 27 Jan 2021 20:29:16 +0800 Subject: [PATCH 5/6] Cancel the previous workflow --- .github/workflows/int-test-darwin.yaml | 5 ++++- .github/workflows/int-test-ubuntu.yaml | 5 ++++- app/cmd/common/common.go | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/int-test-darwin.yaml b/.github/workflows/int-test-darwin.yaml index e1ea0a90..e4773fbf 100644 --- a/.github/workflows/int-test-darwin.yaml +++ b/.github/workflows/int-test-darwin.yaml @@ -10,7 +10,10 @@ jobs: name: Build runs-on: macOS-latest steps: - + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ secrets.GITHUB_TOKEN_SECRETS }} - name: Set up Go 1.15 uses: actions/setup-go@v2.1.3 with: diff --git a/.github/workflows/int-test-ubuntu.yaml b/.github/workflows/int-test-ubuntu.yaml index 1efe19dc..dd01a0bd 100644 --- a/.github/workflows/int-test-ubuntu.yaml +++ b/.github/workflows/int-test-ubuntu.yaml @@ -10,7 +10,10 @@ jobs: name: Build runs-on: ubuntu-20.04 steps: - + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.7.0 + with: + access_token: ${{ secrets.GITHUB_TOKEN_SECRETS }} - name: Set up Go 1.15 uses: actions/setup-go@v2.1.3 with: diff --git a/app/cmd/common/common.go b/app/cmd/common/common.go index 0b52031c..1d077d10 100644 --- a/app/cmd/common/common.go +++ b/app/cmd/common/common.go @@ -55,7 +55,7 @@ func (b *BatchOption) Confirm(message string) bool { prompt = &survey.Confirm{ Message: message, } - survey.AskOne(prompt, &confirm, survey.WithStdio(b.Stdio.In, b.Stdio.Out, b.Stdio.Err)) + _ = survey.AskOne(prompt, &confirm, survey.WithStdio(b.Stdio.In, b.Stdio.Out, b.Stdio.Err)) return confirm } From bb950152e6f1cc20b7a6db2d8c48900002064c47 Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 27 Jan 2021 20:31:56 +0800 Subject: [PATCH 6/6] Remove the action step of cancel previous workflow --- .github/workflows/int-test-darwin.yaml | 4 ---- .github/workflows/int-test-ubuntu.yaml | 4 ---- .github/workflows/pull-request.yaml | 4 ---- 3 files changed, 12 deletions(-) diff --git a/.github/workflows/int-test-darwin.yaml b/.github/workflows/int-test-darwin.yaml index e4773fbf..ebb80185 100644 --- a/.github/workflows/int-test-darwin.yaml +++ b/.github/workflows/int-test-darwin.yaml @@ -10,10 +10,6 @@ jobs: name: Build runs-on: macOS-latest steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.7.0 - with: - access_token: ${{ secrets.GITHUB_TOKEN_SECRETS }} - name: Set up Go 1.15 uses: actions/setup-go@v2.1.3 with: diff --git a/.github/workflows/int-test-ubuntu.yaml b/.github/workflows/int-test-ubuntu.yaml index dd01a0bd..402a6b0d 100644 --- a/.github/workflows/int-test-ubuntu.yaml +++ b/.github/workflows/int-test-ubuntu.yaml @@ -10,10 +10,6 @@ jobs: name: Build runs-on: ubuntu-20.04 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.7.0 - with: - access_token: ${{ secrets.GITHUB_TOKEN_SECRETS }} - name: Set up Go 1.15 uses: actions/setup-go@v2.1.3 with: diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 40d5943e..832606df 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -10,10 +10,6 @@ jobs: name: Build runs-on: ubuntu-20.04 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.7.0 - with: - access_token: ${{ secrets.GITHUB_TOKEN_SECRETS }} - name: Set up Go 1.15 uses: actions/setup-go@v2.1.3 with: