forked from kabanero-io/kabanero-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
246 lines (195 loc) · 6.34 KB
/
github.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright 2019 IBM Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"errors"
"github.com/google/go-github/github"
ghw "gopkg.in/go-playground/webhooks.v3/github"
"io/ioutil"
"k8s.io/client-go/dynamic"
"k8s.io/klog"
"net/http"
"os"
"strings"
)
// Event A GitHub Event
type Event string
// EventHandler Handler for an event
type EventHandler func(*GitHubListener, *http.Request) (interface{}, error)
// GitHubListener Event listener for GitHub events
type GitHubListener struct {
DynamicClient dynamic.Interface
Client *github.Client
Events map[Event]EventHandler
}
var (
// ErrInvalidHTTPMethod Only support the POST HTTP method for webhooks
ErrInvalidHTTPMethod = errors.New("invalid HTTP method; must use HTTP POST")
// ErrMissingGitHubEventHeader X-GitHub-Event header is required for GH payloads
ErrMissingGitHubEventHeader = errors.New("missing X-GitHub-Event header")
// ErrInvalidPayload Payload was invalid
ErrInvalidPayload = errors.New("unable to parse event payload")
// ErrUnknownEvent Event is either unknown or unsupported
ErrUnknownEvent = errors.New("unknown or unsupported event")
// ErrConnection Unable to connect to GH/GHE
ErrConnection = errors.New("connection to GitHub failed")
// ErrUserNameNotFound The GH_USER environment variable is required for the GitHub listener
ErrUserNameNotFound = errors.New("GH_USER environment variable is required")
// ErrTokenNotFound The GH_TOKEN environment variable is required for the GitHub listener
ErrTokenNotFound = errors.New("GH_TOKEN environment variable is required")
)
const (
// PushEvent GH Push event
PushEvent Event = "push"
)
// NewGitHubEventListener Create a new GitHubListener
func NewGitHubEventListener(dynamicClient dynamic.Interface) (*GitHubListener, error) {
// TODO: Read GH URL for each event.
githubURL := strings.TrimSpace(os.Getenv("GH_URL"))
// TODO: Switch to Secret and clean this up later.
username := strings.TrimSpace(os.Getenv("GH_USER"))
token := strings.TrimSpace(os.Getenv("GH_TOKEN"))
var err error
username, token, _, err = getURLAPIToken(dynamicClient, webhookNamespace, githubURL)
if err != nil {
return nil, err
}
if username == "" {
return nil, ErrUserNameNotFound
}
if token == "" {
return nil, ErrTokenNotFound
}
tp := github.BasicAuthTransport{
Username: username,
Password: token,
}
var client *github.Client
if githubURL == "" {
client = github.NewClient(tp.Client())
} else {
client, err = github.NewEnterpriseClient(githubURL, githubURL, tp.Client())
}
if err != nil {
return nil, err
}
listener := new(GitHubListener)
listener.DynamicClient = dynamicClient
listener.Client = client
listener.Events = make(map[Event]EventHandler)
listener.Events[PushEvent] = (*GitHubListener).handlePushEvent
return listener, nil
}
// ParseEvent Parse GH and GHE events.
func (listener *GitHubListener) ParseEvent(r *http.Request) (interface{}, error) {
if r.Method != http.MethodPost {
return nil, ErrInvalidHTTPMethod
}
event := Event(r.Header.Get("X-GitHub-Event"))
if event == "" {
return nil, ErrMissingGitHubEventHeader
}
if eventHandler := listener.Events[event]; eventHandler != nil {
return eventHandler(listener, r)
}
return nil, ErrUnknownEvent
}
// GetFile Retrieve file contents from a GitHub repository
func (listener *GitHubListener) GetFile(ghURL, owner, repo, path string) (string, error) {
// TODO: Clean this up.
username := strings.TrimSpace(os.Getenv("GH_USER"))
token := strings.TrimSpace(os.Getenv("GH_TOKEN"))
if username == "" {
return "", ErrUserNameNotFound
}
if token == "" {
return "", ErrTokenNotFound
}
tp := github.BasicAuthTransport{
Username: username,
Password: token,
}
var client *github.Client
var err error
if ghURL == "" {
client = github.NewClient(tp.Client())
} else {
client, err = github.NewEnterpriseClient("https://api."+ghURL, ghURL, tp.Client())
}
rc, err := client.Repositories.DownloadContents(context.Background(), owner, repo, path, nil)
if err != nil {
return "", err
}
defer rc.Close()
if buf, err := ioutil.ReadAll(rc); err == nil {
return string(buf), nil
}
return "", err
}
func readPayload(r *http.Request) (string, error) {
payload, err := ioutil.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
return "", ErrInvalidPayload
}
return string(payload), nil
}
func getPayload(data string, eventPayload interface{}) error {
return json.Unmarshal([]byte(data), eventPayload)
}
func (listener *GitHubListener) handlePushEvent(r *http.Request) (interface{}, error) {
data, err := readPayload(r)
if err != nil {
return nil, err
}
var payload ghw.PushPayload
err = getPayload(data, &payload)
if err != nil {
klog.Error(err)
return nil, err
}
ghURL := getGitHubURL(payload.Repository.HTMLURL)
owner, repo := getOwnerAndRepo(payload.Repository.SSHURL)
appsodyConfig, err := gitHubListener.GetFile(ghURL, owner, repo, ".appsody-config.yaml")
if err == nil {
klog.Infof("Appsody information from repo: %s", appsodyConfig)
} else {
klog.Error(err)
}
branch := payload.Ref
branch = branch[strings.LastIndex(branch, "/"):]
// Get appsody information
appsodyConfig = appsodyConfig[len("stack:")+1:]
sep := strings.Index(appsodyConfig, ":")
collectionID := appsodyConfig[:sep]
collectionVersion := appsodyConfig[sep+1:]
event, err := NewGitHubRepositoryEvent("Push", branch, collectionID, collectionVersion, "", data)
if err != nil {
return nil, err
}
return event, nil
}
func getGitHubURL(url string) string {
// Strip off `https://`
url = url[8:]
i := strings.Index(url, "/")
return url[:i+1]
}
func getOwnerAndRepo(gitURL string) (string, string) {
// Strip off the Git URL and .git suffix, leaving owner/repo
gitURL = gitURL[strings.Index(gitURL, ":")+1:]
gitURL = gitURL[:len(gitURL)-4]
ownerAndRepo := strings.Split(gitURL, "/")
return ownerAndRepo[0], ownerAndRepo[1]
}