This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
/
github_util.go
227 lines (198 loc) · 7.15 KB
/
github_util.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
/*
Copyright 2020 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 utils
import (
"context"
"fmt"
"github.com/google/go-github/github"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
"net/http"
)
/* Get the repository's information from from github message body: name, owner, html_url, and ref */
func getRepositoryInfo(body map[string]interface{}, repositoryEvent string) (string, string, string, string, error) {
ref := ""
if repositoryEvent == "push" {
// use SHA for ref
afterObj, ok := body["after"]
if !ok {
return "", "", "", "", fmt.Errorf("unable to find after for push webhook message")
}
ref, ok = afterObj.(string)
if !ok {
return "", "", "", "", fmt.Errorf("after for push webhook message (%s) is not a string but %T", afterObj, afterObj)
}
} else if repositoryEvent == "pull_request" {
// use pull_request.head.sha
prObj, ok := body["pull_request"]
if !ok {
return "", "", "", "", fmt.Errorf("unable to find pull_request in webhook message")
}
prMap, ok := prObj.(map[string]interface{})
if !ok {
return "", "", "", "", fmt.Errorf("pull_request in webhook message is of type %T, not map[string]interface{}", prObj)
}
headObj, ok := prMap["head"]
if !ok {
return "", "", "", "", fmt.Errorf("pull_request in webhook message does not contain head")
}
head, ok := headObj.(map[string]interface{})
if !ok {
return "", "", "", "", fmt.Errorf("pull_request head not map[string]interface{}, but is %T", headObj)
}
shaObj, ok := head["sha"]
if !ok {
return "", "", "", "", fmt.Errorf("pull_request.head.sha not found")
}
ref, ok = shaObj.(string)
if !ok {
return "", "", "", "", fmt.Errorf("pull_request merge_commit_sha in webhook message not a string: %v", shaObj)
}
}
repositoryObj, ok := body["repository"]
if !ok {
return "", "", "", "", fmt.Errorf("unable to find repository in webhook message")
}
repository, ok := repositoryObj.(map[string]interface{})
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository object not map[string]interface{}: %v", repositoryObj)
}
nameObj, ok := repository["name"]
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository name not found")
}
name, ok := nameObj.(string)
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository name not a string: %v", nameObj)
}
ownerMapObj, ok := repository["owner"]
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository owner not found")
}
ownerMap, ok := ownerMapObj.(map[string]interface{})
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository owner object not map[string]interface{}: %v", ownerMapObj)
}
ownerObj, ok := ownerMap["login"]
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository owner login not found")
}
owner, ok := ownerObj.(string)
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository owner login not string : %v", ownerObj)
}
htmlURLObj, ok := repository["html_url"]
if !ok {
return "", "", "", "", fmt.Errorf("webhook message repository html_url not found")
}
htmlURL, ok := htmlURLObj.(string)
if !ok {
return "", "", "", "", fmt.Errorf("webhook message html_url not string: %v", htmlURL)
}
return owner, name, htmlURL, ref, nil
}
/*
DownloadYAML Downloads a YAML file from a git repository.
header: HTTP header from webhook
bodyMap: HTTP message body from webhook
*/
func DownloadYAML(kubeClient *kubernetes.Clientset, header map[string][]string, bodyMap map[string]interface{}, fileName string) (map[string]interface{}, bool, error) {
hostHeader, isEnterprise := header[http.CanonicalHeaderKey("x-github-enterprise-host")]
var host string
if !isEnterprise {
host = "github.com"
} else {
host = hostHeader[0]
}
repositoryEvent := header["X-Github-Event"][0]
owner, name, htmlURL, ref, err := getRepositoryInfo(bodyMap, repositoryEvent)
if err != nil {
return nil, false, fmt.Errorf("unable to get repository owner, name, or html_url from webhook message: %v", err)
}
namespace := GetKabaneroNamespace()
user, token, err := GetGitHubSecret(kubeClient, namespace, htmlURL)
if err != nil {
return nil, false, fmt.Errorf("unable to get user/token secret for URL %s: %v", htmlURL, err)
}
githubURL := "https://" + host
bytes, found, err := DownloadFileFromGithub(owner, name, fileName, ref, githubURL, user, token, isEnterprise)
if err != nil {
return nil, found, err
}
retMap, err := YAMLToMap(bytes)
return retMap, found, err
}
// DownloadFileFromGithub Downloads a file and returns: bytes of the file, true if file exists, and any error
func DownloadFileFromGithub(owner, repository, fileName, ref, githubURL, user, token string, isEnterprise bool) ([]byte, bool, error) {
if klog.V(5) {
klog.Infof("downloadFileFromGithub %v, %v, %v, %v, %v, %v, %v", owner, repository, fileName, ref, githubURL, user, isEnterprise)
}
ctx := context.Background()
tp := github.BasicAuthTransport{
Username: user,
Password: token,
}
/*
tokenService := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tokenClient := oauth2.NewClient(ctx, tokenService)
*/
var err error
var client *github.Client
if isEnterprise {
githubURL = githubURL + "/api/v3"
client, err = github.NewEnterpriseClient(githubURL, githubURL, tp.Client())
if err != nil {
return nil, false, err
}
} else {
client = github.NewClient(tp.Client())
}
var options *github.RepositoryContentGetOptions
if ref != "" {
options = &github.RepositoryContentGetOptions{Ref: ref}
}
/*
rc, err := client.Repositories.DownloadContents(ctx, owner, repository, fileName, options)
if err != nil {
fmt.Printf("Error type: %T, value: %v\n", err, err)
return nil, false, err
}
defer rc.Close()
buf, err := ioutil.ReadAll(rc)
*/
fileContent, _, resp, err := client.Repositories.GetContents(ctx, owner, repository, fileName, options)
if resp.Response.StatusCode == 200 {
if fileContent != nil {
if fileContent.Content == nil {
return nil, true, fmt.Errorf("content for %v/%v/%v is nil", owner, repository, fileName)
}
content, err := fileContent.GetContent()
if err != nil {
klog.Infof("download File Form Github error %v", err)
} else {
klog.Infof("download File from Github: buffer %v", content)
}
return []byte(content), true, err
}
/* some other errors */
return nil, false, fmt.Errorf("unable to download %v/%v/%v: not a file", owner, repository, fileName)
} else if resp.Response.StatusCode == 400 {
/* does not exist */
return nil, false, nil
} else {
/* some other errors */
return nil, false, fmt.Errorf("unable to download %v/%v/%v, http error %v", owner, repository, fileName, resp.Response.Status)
}
}