Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins-cli
提交
1e15baa9
J
jenkins-cli
项目概览
LinuxSuRen
/
jenkins-cli
与 Fork 源项目一致
Fork自
Jenkins 中文社区 / jenkins-cli
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins-cli
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1e15baa9
编写于
9月 07, 2019
作者:
LinuxSuRen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support to search all jobs
上级
fc819728
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
81 addition
and
12 deletion
+81
-12
app/cmd/job_search.go
app/cmd/job_search.go
+12
-3
app/cmd/job_search_test.go
app/cmd/job_search_test.go
+63
-3
client/job.go
client/job.go
+2
-2
client/job_test.go
client/job_test.go
+4
-4
未找到文件。
app/cmd/job_search.go
浏览文件 @
1e15baa9
...
...
@@ -8,8 +8,11 @@ import (
"github.com/spf13/cobra"
)
// JobSearchOption is the options of job search command
type
JobSearchOption
struct
{
OutputOption
PrintAll
bool
Max
int
RoundTripper
http
.
RoundTripper
}
...
...
@@ -18,19 +21,25 @@ var jobSearchOption JobSearchOption
func
init
()
{
jobCmd
.
AddCommand
(
jobSearchCmd
)
jobSearchCmd
.
Flags
()
.
IntVarP
(
&
jobSearchOption
.
Max
,
"max"
,
""
,
10
,
"The number of limitation to print"
)
jobSearchCmd
.
Flags
()
.
BoolVarP
(
&
jobSearchOption
.
PrintAll
,
"all"
,
""
,
false
,
"Print all items if there's no keyword"
)
jobSearchCmd
.
Flags
()
.
StringVarP
(
&
jobSearchOption
.
Format
,
"output"
,
"o"
,
"json"
,
"Format the output"
)
}
var
jobSearchCmd
=
&
cobra
.
Command
{
Use
:
"search
<keyword>
"
,
Use
:
"search
[keyword]
"
,
Short
:
"Print the job of your Jenkins"
,
Long
:
`Print the job of your Jenkins`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
len
(
args
)
==
0
{
if
!
jobSearchOption
.
PrintAll
&&
len
(
args
)
==
0
{
cmd
.
Help
()
return
}
if
jobSearchOption
.
PrintAll
&&
len
(
args
)
==
0
{
args
=
[]
string
{
""
}
}
keyword
:=
args
[
0
]
jclient
:=
&
client
.
JobClient
{
...
...
@@ -40,7 +49,7 @@ var jobSearchCmd = &cobra.Command{
}
getCurrentJenkinsAndClient
(
&
(
jclient
.
JenkinsCore
))
if
status
,
err
:=
jclient
.
Search
(
keyword
);
err
==
nil
{
if
status
,
err
:=
jclient
.
Search
(
keyword
,
jobSearchOption
.
Max
);
err
==
nil
{
var
data
[]
byte
if
data
,
err
=
Format
(
status
,
queueOption
.
Format
);
err
==
nil
{
cmd
.
Println
(
string
(
data
))
...
...
app/cmd/job_search_test.go
浏览文件 @
1e15baa9
...
...
@@ -10,6 +10,7 @@ import (
"github.com/golang/mock/gomock"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
"github.com/spf13/cobra"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)
...
...
@@ -22,7 +23,6 @@ var _ = Describe("job search command", func() {
BeforeEach
(
func
()
{
ctrl
=
gomock
.
NewController
(
GinkgoT
())
roundTripper
=
mhttp
.
NewMockRoundTripper
(
ctrl
)
jobSearchOption
.
RoundTripper
=
roundTripper
rootCmd
.
SetArgs
([]
string
{})
rootOptions
.
Jenkins
=
""
...
...
@@ -36,7 +36,33 @@ var _ = Describe("job search command", func() {
ctrl
.
Finish
()
})
Context
(
"basic cases"
,
func
()
{
Context
(
"no need roundtripper"
,
func
()
{
It
(
"search without any options"
,
func
()
{
data
,
err
:=
generateSampleConfig
()
Expect
(
err
)
.
To
(
BeNil
())
err
=
ioutil
.
WriteFile
(
rootOptions
.
ConfigFile
,
data
,
0664
)
Expect
(
err
)
.
To
(
BeNil
())
jobSearchCmd
.
SetHelpFunc
(
func
(
cmd
*
cobra
.
Command
,
_
[]
string
)
{
cmd
.
Print
(
"help"
)
})
rootCmd
.
SetArgs
([]
string
{
"job"
,
"search"
})
buf
:=
new
(
bytes
.
Buffer
)
rootCmd
.
SetOutput
(
buf
)
_
,
err
=
rootCmd
.
ExecuteC
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
buf
.
String
())
.
To
(
Equal
(
"help"
))
})
})
Context
(
"basic cases, need roundtrippper"
,
func
()
{
BeforeEach
(
func
()
{
roundTripper
=
mhttp
.
NewMockRoundTripper
(
ctrl
)
jobSearchOption
.
RoundTripper
=
roundTripper
})
It
(
"should success, search with one result item"
,
func
()
{
data
,
err
:=
generateSampleConfig
()
Expect
(
err
)
.
To
(
BeNil
())
...
...
@@ -45,7 +71,7 @@ var _ = Describe("job search command", func() {
keyword
:=
"fake"
request
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"http://localhost:8080/jenkins/search/suggest?query=%s"
,
keyword
),
nil
)
request
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"http://localhost:8080/jenkins/search/suggest?query=%s
&max=10
"
,
keyword
),
nil
)
request
.
SetBasicAuth
(
"admin"
,
"111e3a2f0231198855dceaff96f20540a9"
)
response
:=
&
http
.
Response
{
StatusCode
:
200
,
...
...
@@ -70,6 +96,40 @@ var _ = Describe("job search command", func() {
}
]
}
`
))
})
It
(
"should success, search without keyword"
,
func
()
{
data
,
err
:=
generateSampleConfig
()
Expect
(
err
)
.
To
(
BeNil
())
err
=
ioutil
.
WriteFile
(
rootOptions
.
ConfigFile
,
data
,
0664
)
Expect
(
err
)
.
To
(
BeNil
())
request
,
_
:=
http
.
NewRequest
(
"GET"
,
"http://localhost:8080/jenkins/search/suggest?query=&max=10"
,
nil
)
request
.
SetBasicAuth
(
"admin"
,
"111e3a2f0231198855dceaff96f20540a9"
)
response
:=
&
http
.
Response
{
StatusCode
:
200
,
Proto
:
"HTTP/1.1"
,
Request
:
request
,
Body
:
ioutil
.
NopCloser
(
bytes
.
NewBufferString
(
`{"suggestions": [{"name": "fake"}]}`
)),
}
roundTripper
.
EXPECT
()
.
RoundTrip
(
request
)
.
Return
(
response
,
nil
)
rootCmd
.
SetArgs
([]
string
{
"job"
,
"search"
,
"--all"
})
buf
:=
new
(
bytes
.
Buffer
)
rootCmd
.
SetOutput
(
buf
)
_
,
err
=
rootCmd
.
ExecuteC
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
buf
.
String
())
.
To
(
Equal
(
`{
"Suggestions": [
{
"Name": "fake"
}
]
}
`
))
})
})
...
...
client/job.go
浏览文件 @
1e15baa9
...
...
@@ -19,8 +19,8 @@ type JobClient struct {
}
// Search find a set of jobs by name
func
(
q
*
JobClient
)
Search
(
keyword
string
)
(
status
*
SearchResult
,
err
error
)
{
err
=
q
.
RequestWithData
(
"GET"
,
fmt
.
Sprintf
(
"/search/suggest?query=%s
"
,
keyword
),
nil
,
nil
,
200
,
&
status
)
func
(
q
*
JobClient
)
Search
(
keyword
string
,
max
int
)
(
status
*
SearchResult
,
err
error
)
{
err
=
q
.
RequestWithData
(
"GET"
,
fmt
.
Sprintf
(
"/search/suggest?query=%s
&max=%d"
,
keyword
,
max
),
nil
,
nil
,
200
,
&
status
)
return
}
...
...
client/job_test.go
浏览文件 @
1e15baa9
...
...
@@ -36,7 +36,7 @@ var _ = Describe("job test", func() {
It
(
"basic case with one result item"
,
func
()
{
keyword
:=
"fake"
request
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"%s%s%s"
,
jobClient
.
URL
,
"/search/suggest?query="
,
keyword
),
nil
)
request
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"%s%s%s
&max=1
"
,
jobClient
.
URL
,
"/search/suggest?query="
,
keyword
),
nil
)
response
:=
&
http
.
Response
{
StatusCode
:
200
,
Proto
:
"HTTP/1.1"
,
...
...
@@ -46,7 +46,7 @@ var _ = Describe("job test", func() {
roundTripper
.
EXPECT
()
.
RoundTrip
(
request
)
.
Return
(
response
,
nil
)
result
,
err
:=
jobClient
.
Search
(
keyword
)
result
,
err
:=
jobClient
.
Search
(
keyword
,
1
)
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
result
)
.
NotTo
(
BeNil
())
Expect
(
len
(
result
.
Suggestions
))
.
To
(
Equal
(
1
))
...
...
@@ -56,7 +56,7 @@ var _ = Describe("job test", func() {
It
(
"basic case without any result items"
,
func
()
{
keyword
:=
"fake"
request
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"%s%s%s"
,
jobClient
.
URL
,
"/search/suggest?query="
,
keyword
),
nil
)
request
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"%s%s%s
&max=1
"
,
jobClient
.
URL
,
"/search/suggest?query="
,
keyword
),
nil
)
response
:=
&
http
.
Response
{
StatusCode
:
200
,
Proto
:
"HTTP/1.1"
,
...
...
@@ -66,7 +66,7 @@ var _ = Describe("job test", func() {
roundTripper
.
EXPECT
()
.
RoundTrip
(
request
)
.
Return
(
response
,
nil
)
result
,
err
:=
jobClient
.
Search
(
keyword
)
result
,
err
:=
jobClient
.
Search
(
keyword
,
1
)
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
result
)
.
NotTo
(
BeNil
())
Expect
(
len
(
result
.
Suggestions
))
.
To
(
Equal
(
0
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录