-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
plugin.go
71 lines (58 loc) · 1.13 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"errors"
"log"
"net/url"
"strings"
)
type (
// Plugin values.
Plugin struct {
BaseURL string
Username string
Token string
Job []string
Insecure bool
Parameters []string
}
)
func trimElement(keys []string) []string {
newKeys := []string{}
for _, value := range keys {
value = strings.Trim(value, " ")
if len(value) == 0 {
continue
}
newKeys = append(newKeys, value)
}
return newKeys
}
// Exec executes the plugin.
func (p Plugin) Exec() error {
if len(p.BaseURL) == 0 || len(p.Username) == 0 || len(p.Token) == 0 {
return errors.New("missing jenkins config")
}
jobs := trimElement(p.Job)
if len(jobs) == 0 {
return errors.New("missing jenkins job")
}
auth := &Auth{
Username: p.Username,
Token: p.Token,
}
jenkins := NewJenkins(auth, p.BaseURL, p.Insecure)
params := url.Values{}
for _, v := range p.Parameters {
kv := strings.Split(v, "=")
if len(kv) == 2 {
params.Add(kv[0], kv[1])
}
}
for _, v := range jobs {
if err := jenkins.trigger(v, params); err != nil {
return err
}
log.Printf("trigger job %s success", v)
}
return nil
}