refactor: remove unnecessary config. (#5)

This commit is contained in:
Bo-Yi Wu
2017-01-16 10:42:55 +08:00
committed by GitHub
parent 2211e06f29
commit 788418c56f
4 changed files with 17 additions and 137 deletions
-8
View File
@@ -46,14 +46,6 @@ docker run --rm \
-e PLUGIN_USERNAME=xxxxxxx \
-e PLUGIN_TOKEN=xxxxxxx \
-e PLUGIN_JOB=xxxxxxx \
-e DRONE_REPO_OWNER=appleboy \
-e DRONE_REPO_NAME=go-hello \
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
-e DRONE_COMMIT_BRANCH=master \
-e DRONE_COMMIT_AUTHOR=appleboy \
-e DRONE_BUILD_NUMBER=1 \
-e DRONE_BUILD_STATUS=success \
-e DRONE_BUILD_LINK=http://github.com/appleboy/go-hello \
-v $(pwd):$(pwd) \
-w $(pwd) \
appleboy/drone-jenkins
+5 -74
View File
@@ -36,60 +36,7 @@ func main() {
cli.StringSliceFlag{
Name: "job",
Usage: "jenkins job",
EnvVar: "PLUGIN_JOB",
},
cli.StringFlag{
Name: "repo.owner",
Usage: "repository owner",
EnvVar: "DRONE_REPO_OWNER",
},
cli.StringFlag{
Name: "repo.name",
Usage: "repository name",
EnvVar: "DRONE_REPO_NAME",
},
cli.StringFlag{
Name: "commit.sha",
Usage: "git commit sha",
EnvVar: "DRONE_COMMIT_SHA",
},
cli.StringFlag{
Name: "commit.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "commit.author",
Usage: "git author name",
EnvVar: "DRONE_COMMIT_AUTHOR",
},
cli.StringFlag{
Name: "commit.message",
Usage: "commit message",
EnvVar: "DRONE_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "build.event",
Value: "push",
Usage: "build event",
EnvVar: "DRONE_BUILD_EVENT",
},
cli.IntFlag{
Name: "build.number",
Usage: "build number",
EnvVar: "DRONE_BUILD_NUMBER",
},
cli.StringFlag{
Name: "build.status",
Usage: "build status",
Value: "success",
EnvVar: "DRONE_BUILD_STATUS",
},
cli.StringFlag{
Name: "build.link",
Usage: "build link",
EnvVar: "DRONE_BUILD_LINK",
EnvVar: "PLUGIN_JOB,JENKINS_JOB",
},
cli.StringFlag{
Name: "env-file",
@@ -106,26 +53,10 @@ func run(c *cli.Context) error {
}
plugin := Plugin{
Repo: Repo{
Owner: c.String("repo.owner"),
Name: c.String("repo.name"),
},
Build: Build{
Number: c.Int("build.number"),
Event: c.String("build.event"),
Status: c.String("build.status"),
Commit: c.String("commit.sha"),
Branch: c.String("commit.branch"),
Author: c.String("commit.author"),
Message: c.String("commit.message"),
Link: c.String("build.link"),
},
Config: Config{
BaseURL: c.String("base.url"),
Username: c.String("username"),
Token: c.String("token"),
Job: c.StringSlice("job"),
},
BaseURL: c.String("base.url"),
Username: c.String("username"),
Token: c.String("token"),
Job: c.StringSlice("job"),
}
return plugin.Exec()
+7 -32
View File
@@ -7,38 +7,13 @@ import (
)
type (
// Repo information.
Repo struct {
Owner string
Name string
}
// Build information.
Build struct {
Event string
Number int
Commit string
Message string
Branch string
Author string
Status string
Link string
}
// Config for the plugin.
Config struct {
// Plugin values.
Plugin struct {
BaseURL string
Username string
Token string
Job []string
}
// Plugin values.
Plugin struct {
Repo Repo
Build Build
Config Config
}
)
func trimElement(keys []string) []string {
@@ -58,19 +33,19 @@ func trimElement(keys []string) []string {
// Exec executes the plugin.
func (p Plugin) Exec() error {
if len(p.Config.BaseURL) == 0 || len(p.Config.Username) == 0 || len(p.Config.Token) == 0 {
if len(p.BaseURL) == 0 || len(p.Username) == 0 || len(p.Token) == 0 {
log.Println("missing jenkins config")
return errors.New("missing jenkins config")
}
auth := &Auth{
Username: p.Config.Username,
Token: p.Config.Token,
Username: p.Username,
Token: p.Token,
}
jenkins := NewJenkins(auth, p.Config.BaseURL)
jenkins := NewJenkins(auth, p.BaseURL)
for _, value := range trimElement(p.Config.Job) {
for _, value := range trimElement(p.Job) {
jenkins.trigger(value, nil)
}
+5 -23
View File
@@ -16,9 +16,7 @@ func TestMissingConfig(t *testing.T) {
func TestMissingJenkinsConfig(t *testing.T) {
plugin := Plugin{
Config: Config{
BaseURL: "http://example.com",
},
BaseURL: "http://example.com",
}
err := plugin.Exec()
@@ -28,26 +26,10 @@ func TestMissingJenkinsConfig(t *testing.T) {
func TestPluginTriggerBuild(t *testing.T) {
plugin := Plugin{
Repo: Repo{
Name: "go-hello",
Owner: "appleboy",
},
Build: Build{
Number: 101,
Status: "success",
Link: "https://github.com/appleboy/go-hello",
Author: "Bo-Yi Wu",
Branch: "master",
Message: "update by drone line plugin.",
Commit: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
},
Config: Config{
BaseURL: "http://example.com",
Username: "foo",
Token: "bar",
Job: []string{"drone-jenkins"},
},
BaseURL: "http://example.com",
Username: "foo",
Token: "bar",
Job: []string{"drone-jenkins"},
}
err := plugin.Exec()