From e52aabc2c69e5cdf261289b0cb5d8d24d564c5d1 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 15 Feb 2020 20:59:00 +0800 Subject: [PATCH] Support search job by parent name --- app/cmd/job_search.go | 17 ++++++++++------- client/doc/README-zh.md | 10 +++++++++- client/doc/README.md | 10 +++++++++- client/job.go | 6 ++++-- client/job_test_common.go | 4 ++-- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/app/cmd/job_search.go b/app/cmd/job_search.go index 9e6da21..ddea17a 100644 --- a/app/cmd/job_search.go +++ b/app/cmd/job_search.go @@ -6,18 +6,18 @@ import ( "github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/client" "github.com/spf13/cobra" - "net/http" ) // JobSearchOption is the options of job search command type JobSearchOption struct { + CommonOption OutputOption + Name string + Type string + Parent string + Start int Limit int - Name string - Type string - - RoundTripper http.RoundTripper } var jobSearchOption JobSearchOption @@ -29,9 +29,11 @@ func init() { jobSearchCmd.Flags().IntVarP(&jobSearchOption.Limit, "limit", "", 50, i18n.T("The list of items limit")) jobSearchCmd.Flags().StringVarP(&jobSearchOption.Name, "name", "", "", - i18n.T("The name of plugin for search")) + i18n.T("The name of items for search")) jobSearchCmd.Flags().StringVarP(&jobSearchOption.Type, "type", "", "", - i18n.T("The type of plugin for search")) + i18n.T("The type of items for search")) + jobSearchCmd.Flags().StringVarP(&jobSearchOption.Parent, "parent", "", "", + i18n.T("The parent of items for search")) jobSearchOption.SetFlagWithHeaders(jobSearchCmd, "Name,DisplayName,Type,URL") healthCheckRegister.Register(getCmdPath(jobSearchCmd), &jobSearchOption) @@ -52,6 +54,7 @@ var jobSearchCmd = &cobra.Command{ JenkinsCore: client.JenkinsCore{ RoundTripper: jobSearchOption.RoundTripper, }, + Parent: jobSearchOption.Parent, } getCurrentJenkinsAndClient(&(jClient.JenkinsCore)) diff --git a/client/doc/README-zh.md b/client/doc/README-zh.md index 0092e3f..0e537bc 100644 --- a/client/doc/README-zh.md +++ b/client/doc/README-zh.md @@ -124,4 +124,12 @@ func BuildJob(jobBuild JobBuildOptions) (e error) { e = jobClient.BuildWithParams(jobBuild.JobName, params) return } -``` \ No newline at end of file +``` + +## 插件依赖 + +本 Jenkins 客户端的部分 API 需要依赖特定的插件,请参考下面的列表: + +| API | Plugin | +|---|---| +| Search Job | [pipeline-restful-api](https://github.com/jenkinsci/pipeline-restful-api-plugin) | diff --git a/client/doc/README.md b/client/doc/README.md index 13f4b74..cb5a9f5 100644 --- a/client/doc/README.md +++ b/client/doc/README.md @@ -124,4 +124,12 @@ func BuildJob(jobBuild JobBuildOptions) (e error) { e = jobClient.BuildWithParams(jobBuild.JobName, params) return } -``` \ No newline at end of file +``` + +## Plugin Dependencies + +Pay attention to the part of the Jenkins CLI, they need some special plugins. Please read through the following table: + +| API | Plugin | +|---|---| +| Search Job | [pipeline-restful-api](https://github.com/jenkinsci/pipeline-restful-api-plugin) | diff --git a/client/job.go b/client/job.go index 3bff4ea..f888c00 100644 --- a/client/job.go +++ b/client/job.go @@ -15,12 +15,14 @@ import ( // JobClient is client for operate jobs type JobClient struct { JenkinsCore + + Parent string } // Search find a set of jobs by name func (q *JobClient) Search(name, kind string, start, limit int) (items []JenkinsItem, err error) { - err = q.RequestWithData("GET", fmt.Sprintf("/items/list?name=%s&type=%s&start=%d&limit=%d", - name, kind, start, limit), + err = q.RequestWithData("GET", fmt.Sprintf("/items/list?name=%s&type=%s&start=%d&limit=%d&parent=%s", + name, kind, start, limit, q.Parent), nil, nil, 200, &items) return } diff --git a/client/job_test_common.go b/client/job_test_common.go index 5739f56..46820b8 100644 --- a/client/job_test_common.go +++ b/client/job_test_common.go @@ -188,7 +188,7 @@ func PrepareForJobLog(roundTripper *mhttp.MockRoundTripper, rootURL, jobName str // PrepareOneItem only for test func PrepareOneItem(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind, user, token string) { - request, _ := http.NewRequest("GET", fmt.Sprintf("%s/items/list?name=%s&type=%s&start=%d&limit=%d", + request, _ := http.NewRequest("GET", fmt.Sprintf("%s/items/list?name=%s&type=%s&start=%d&limit=%d&parent=", rootURL, name, kind, 0, 50), nil) response := &http.Response{ StatusCode: 200, @@ -204,7 +204,7 @@ func PrepareOneItem(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind, u // PrepareEmptyItems only for test func PrepareEmptyItems(roundTripper *mhttp.MockRoundTripper, rootURL, name, kind, user, token string) { - request, _ := http.NewRequest("GET", fmt.Sprintf("%s/items/list?name=%s&type=%s&start=%d&limit=%d", + request, _ := http.NewRequest("GET", fmt.Sprintf("%s/items/list?name=%s&type=%s&start=%d&limit=%d&parent=", rootURL, name, kind, 0, 50), nil) response := &http.Response{ StatusCode: 200, -- GitLab