-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (153 loc) · 4.96 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"net/http"
"flag"
"net/url"
"os"
"github.com/yosida95/golang-jenkins"
"gopkg.in/go-playground/webhooks.v5/github"
)
var port = flag.String("port", "6686", "port on which application will run")
var secret = flag.String("secret", "", "github webhook secret")
var endpoint = flag.String("endpoint", "/github-webhook/", "github webhook endpoint to configure")
var jenkinsUrl = flag.String("jenkinsUrl", "", "jenkins base URL")
var jenkinsUsername = flag.String("jenkinsUsername", "", "jenkins username whom REST api token is associated")
var jenkinsApiToken = flag.String("jenkinsApiToken", "", "jenkins REST API token")
var jenkinsJobToTrigger = flag.String("jenkinsJobToTrigger", "", "jenkins job to trigger")
const usage = `Github webhook parser.
Usage:
githubwebhookparser [commands|flags]
The commands & flags are:
help prints help
--port port on which application will run (default: 6686)
--secret github webhook secret
--endpoint github webhook endpoint to configure (default: /github-webhook/)
--jenkinsUrl jenkins base URL
--jenkinsUsername jenkins username whom REST api token is associated
--jenkinsApiToken jenkins REST API token
--jenkinsJobToTrigger jenkins job to trigger
Examples:
# prints help:
githubwebhookparser help
# sample usage
githubwebhookparser --port 6686 --secret MyGitHubSuperSecretSecrect --endpoint /github-webhook/
--jenkinsUrl http://jenkins.example.com/ --jenkinsUsername admin --jenkinsApiToken mytoken
--jenkinsJobToTrigger webhook-test
`
// Jenkins parameter Definitions
var actions = []gojenkins.Action{{
ParameterDefinitions: []gojenkins.ParameterDefinition{
{
Name: "REF",
},
{
Name: "REF_TYPE",
},
{
Name: "REPOSITORY",
},
{
Name: "SENDER",
},
},
}}
func usageExit(rc int) {
fmt.Println(usage)
os.Exit(rc)
}
func validate() {
if *port == "" || *endpoint == "" || *secret == "" {
usageExit(1)
}
if *jenkinsJobToTrigger != "" && (*jenkinsUrl == "" || *jenkinsUsername == "" || *jenkinsApiToken == "") {
usageExit(1)
}
}
func main() {
flag.Usage = func() { usageExit(0) }
flag.Parse()
args := flag.Args()
if len(args) > 0 {
switch args[0] {
case "help":
usageExit(0)
return
}
}
validate()
// Github authentication
hook, _ := github.New(github.Options.Secret(*secret))
// Webhook handler
http.HandleFunc(*endpoint, func(w http.ResponseWriter, r *http.Request) {
payload, err := hook.Parse(r, github.CreateEvent, github.DeleteEvent, github.PushEvent)
if err != nil {
if err == github.ErrEventNotFound {
fmt.Println("E! Specified payload not found")
fmt.Println(err)
}
}
switch payload.(type) {
case github.CreatePayload:
if *jenkinsJobToTrigger != "" {
payload := payload.(github.CreatePayload)
job := gojenkins.Job{
Name: *jenkinsJobToTrigger,
Actions: actions,
}
logPayload(payload.Ref, payload.RefType, payload.Repository.Name, payload.Sender.Login, "CREATE")
checkError(jenkins().Build(job, withParams(payload.Ref, payload.RefType, payload.Repository.Name, payload.Sender.Login, "CREATE")))
}
case github.DeletePayload:
if *jenkinsJobToTrigger != "" {
payload := payload.(github.DeletePayload)
job := gojenkins.Job{
Name: *jenkinsJobToTrigger,
Actions: actions,
}
logPayload(payload.Ref, payload.RefType, payload.Repository.Name, payload.Sender.Login, "DELETE")
checkError(jenkins().Build(job, withParams(payload.Ref, payload.RefType, payload.Repository.Name, payload.Sender.Login, "DELETE")))
}
case github.PushPayload:
if *jenkinsJobToTrigger != "" {
payload := payload.(github.PushPayload)
job := gojenkins.Job{
Name: *jenkinsJobToTrigger,
Actions: actions,
}
logPayload(payload.Ref, "-", payload.Repository.Name, payload.Sender.Login, "PUSH")
checkError(jenkins().Build(job, withParams(payload.Ref, "", payload.Repository.Name, payload.Sender.Login, "PUSH")))
}
}
})
fmt.Printf("I! Starting the server on port %s\n", *port)
http.ListenAndServe(":"+*port, nil)
}
func jenkins() *gojenkins.Jenkins {
auth := &gojenkins.Auth{
Username: *jenkinsUsername,
ApiToken: *jenkinsApiToken,
}
return gojenkins.NewJenkins(auth, *jenkinsUrl)
}
func withParams(ref, refType, repository, sender, event string) url.Values {
params := url.Values{}
params.Set("REF", ref)
params.Set("REF_TYPE", refType)
params.Set("REPOSITORY", repository)
params.Set("SENDER", sender)
params.Set("GITHUB_EVENT", event)
return params
}
func checkError(err error) {
if err != nil {
fmt.Printf("E! Error occured while executing job %s\n", *jenkinsJobToTrigger)
fmt.Println(err)
} else {
fmt.Printf("I! Successfully executed job %s\n", *jenkinsJobToTrigger)
}
}
func logPayload(ref, refType, repository, sender, event string) {
fmt.Printf("REF: %s, REF_TYPE: %s, REPOSITORY: %s, SENDER: %s, GITHUB_EVENT: %s\n",
ref, refType, repository, sender, event)
}