提交 9d07bf45 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Fix the conflict

---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Desktop (please complete the following information):**
- OS: [e.g. windows]
- Command Tool [e.g. cmd]
- Version [e.g. v0.0.16]
**Additional context**
Add any other context about the problem here.
\ No newline at end of file
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: enhancement
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.
\ No newline at end of file
<-- Comment:
Please read the description here carefully
1. The Comment area is part of the comment, please delete it after reading
2. The ISSUES title is named after the `[type] problem overview`
3. Areas with an asterisk (*) are required
-->
### ISSUES TYPE *
- [ ] bug
- [ ] document
- [ ] feature
- [ ] enhancement
- [ ] fix
### Description *
(Please describe the problem to be described as detailed as possible here for easy reading.)
### Error Information
......@@ -35,6 +35,8 @@ labels:
color: c2c2fc
- name: chore
color: c2c2fc
- name: no-changelog
color: c2c2fc
branches:
- name: master
protection:
......
......@@ -12,4 +12,6 @@
*.out
bin/
release/
\ No newline at end of file
release/
*.xml
......@@ -33,13 +33,15 @@ pipeline {
stage('Test') {
steps {
container('golang') {
sh label: 'go test', script: '''
go test ./util/... -v
go test ./client/... -v
go test ./app/... -v
'''
sh label: 'go test', script: 'make test'
}
}
}
}
post {
always {
junit allowEmptyResults: true, testResults: "*/**/*.xml"
}
}
}
\ No newline at end of file
......@@ -31,6 +31,11 @@ release: clean build-all
clean: ## Clean the generated artifacts
rm -rf bin release
test:
go test ./util/... -v -coverprofile bin/testcover-util.log
go test ./client/... -v -coverprofile bin/testcover-client.log
go test ./app/... -v -coverprofile bin/testcover-app.log
dep:
go get github.com/AlecAivazis/survey/v2
go get github.com/gosuri/uiprogress
......
......@@ -50,9 +50,15 @@ type JenkinsServer struct {
Description string `yaml:"description"`
}
type PreHook struct {
Path string `yaml:"path"`
Command string `yaml:"cmd"`
}
type Config struct {
Current string `yaml:"current"`
JenkinsServers []JenkinsServer `yaml:"jenkins_servers"`
PreHooks []PreHook `yaml:"preHooks"`
}
func setCurrentJenkins(name string) {
......
......@@ -40,7 +40,7 @@ var pluginUploadCmd = &cobra.Command{
Long: `Upload a plugin from local filesystem or remote URL to your Jenkins`,
Example: ` jcli plugin upload --remote https://server/sample.hpi
jcli plugin upload sample.hpi`,
PreRun: func(_ *cobra.Command, args []string) {
PreRun: func(cmd *cobra.Command, args []string) {
if pluginUploadOption.Remote != "" {
file, err := ioutil.TempFile(".", "jcli-plugin")
if err != nil {
......@@ -70,6 +70,8 @@ var pluginUploadCmd = &cobra.Command{
log.Fatal(err)
}
} else if len(args) == 0 {
executePreCmd(cmd, args)
path, _ := os.Getwd()
dirName := filepath.Base(path)
dirName = strings.Replace(dirName, "-plugin", "", -1)
......
......@@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/jenkins-zh/jenkins-cli/app"
"github.com/spf13/cobra"
......@@ -82,3 +84,43 @@ func getCurrentJenkinsFromOptionsOrDie() (jenkinsServer *JenkinsServer) {
}
return
}
func getCmdPath(cmd *cobra.Command) string {
current := cmd.Use
if cmd.HasParent() {
parentName := getCmdPath(cmd.Parent())
if parentName == "" {
return current
}
return fmt.Sprintf("%s.%s", parentName, current)
}
// don't need the name of root cmd
return ""
}
func executePreCmd(cmd *cobra.Command, _ []string) {
config := getConfig()
if config == nil {
log.Fatal("Cannot find config file")
return
}
path := getCmdPath(cmd)
for _, preHook := range config.PreHooks {
if path != preHook.Path {
continue
}
execute(preHook.Command)
}
}
func execute(command string) {
array := strings.Split(command, " ")
cmd := exec.Command(array[0], array[1:]...)
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
package cmd
import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
)
var _ = Describe("Root cmd test", func() {
var (
ctrl *gomock.Controller
rootCmd *cobra.Command
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
rootCmd = &cobra.Command{Use: "root"}
})
AfterEach(func() {
ctrl.Finish()
})
Context("PreHook test", func() {
It("only with root cmd", func() {
path := getCmdPath(rootCmd)
Expect(path).To(Equal(""))
})
It("one sub cmd", func() {
subCmd := &cobra.Command{
Use: "sub",
}
rootCmd.AddCommand(subCmd)
path := getCmdPath(subCmd)
Expect(path).To(Equal("sub"))
})
It("two sub cmds", func() {
sub1Cmd := &cobra.Command{
Use: "sub1",
}
sub2Cmd := &cobra.Command{
Use: "sub2",
}
rootCmd.AddCommand(sub1Cmd)
sub1Cmd.AddCommand(sub2Cmd)
path := getCmdPath(sub2Cmd)
Expect(path).To(Equal("sub1.sub2"))
})
})
})
package cmd
import (
"testing"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestApp(t *testing.T) {
RegisterFailHandler(Fail)
junitReporter := reporters.NewJUnitReporter("test-app.xml")
RunSpecsWithDefaultAndCustomReporters(t, "app/cmd", []Reporter{junitReporter})
}
package client
import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("PluginManager test", func() {
var (
ctrl *gomock.Controller
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
})
AfterEach(func() {
ctrl.Finish()
})
Context("basic function test", func() {
It("get install plugin query string", func() {
names := make([]string, 0)
Expect(getPluginsInstallQuery(names)).To(Equal(""))
names = append(names, "abc")
Expect(getPluginsInstallQuery(names)).To(Equal("plugin.abc="))
names = append(names, "def")
Expect(getPluginsInstallQuery(names)).To(Equal("plugin.abc=&plugin.def="))
names = append(names, "")
Expect(getPluginsInstallQuery(names)).To(Equal("plugin.abc=&plugin.def="))
})
})
})
......@@ -153,12 +153,20 @@ func (p *PluginManager) GetPlugins() (pluginList *InstalledPluginList, err error
return
}
func getPluginsInstallQuery(names []string) string {
pluginNames := make([]string, 0)
for _, name := range names {
if name == "" {
continue
}
pluginNames = append(pluginNames, fmt.Sprintf("plugin.%s=", name))
}
return strings.Join(pluginNames, "&")
}
// InstallPlugin install a plugin by name
func (p *PluginManager) InstallPlugin(names []string) (err error) {
for i, name := range names {
names[i] = fmt.Sprintf("plugin.%s", name)
}
api := fmt.Sprintf("%s/pluginManager/install?%s", p.URL, strings.Join(names, "=&"))
api := fmt.Sprintf("%s/pluginManager/install?%s", p.URL, getPluginsInstallQuery(names))
var (
req *http.Request
response *http.Response
......@@ -180,8 +188,22 @@ func (p *PluginManager) InstallPlugin(names []string) (err error) {
data, err = ioutil.ReadAll(response.Body)
if code == 200 {
fmt.Println("install succeed.")
} else if code == 400 {
if errMsg, ok := response.Header["X-Error"]; ok {
for _, msg := range errMsg {
fmt.Println(msg)
}
} else {
fmt.Println("Cannot found plugins", names)
}
} else {
log.Fatal(string(data))
fmt.Println(response.Header)
fmt.Println("status code", code)
if err == nil && p.Debug && len(data) > 0 {
ioutil.WriteFile("debug.html", data, 0664)
} else if err != nil {
log.Fatal(err)
}
}
} else {
log.Fatal(err)
......
package client
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestJenkinsClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "jenkins client test")
}
......@@ -3,11 +3,14 @@ package util
import (
"testing"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "jcli utils test")
junitReporter := reporters.NewJUnitReporter("test-utils.xml")
RunSpecsWithDefaultAndCustomReporters(t, "util", []Reporter{junitReporter})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册