/
git.go
292 lines (254 loc) · 8.41 KB
/
git.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"k8s.io/klog"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
var (
client *github.Client
ctx = context.Background()
)
func getRef(baseBranch, commitBranch string) (ref *github.Reference, err error) {
if ref, _, err = client.Git.GetRef(ctx, owner, repo, "refs/heads/"+commitBranch); err == nil {
return ref, nil
}
var baseRef *github.Reference
if baseRef, _, err = client.Git.GetRef(ctx, owner, repo, "refs/heads/"+baseBranch); err != nil {
return nil, err
}
newRef := &github.Reference{Ref: github.String("refs/heads/" + commitBranch), Object: &github.GitObject{SHA: baseRef.Object.SHA}}
ref, _, err = client.Git.CreateRef(ctx, owner, repo, newRef)
return ref, err
}
func getTree(ref *github.Reference, sourceFiles string) (tree *github.Tree, err error) {
var entries []*github.TreeEntry
// Load each file into the tree.
for _, fileArg := range strings.Split(sourceFiles, ",") {
file, content, err := getFileContent(fileArg)
if err != nil {
return nil, err
}
entries = append(entries, &github.TreeEntry{Path: github.String(file), Type: github.String("blob"), Content: github.String(string(content)), Mode: github.String("100644")})
}
tree, _, err = client.Git.CreateTree(ctx, owner, repo, *ref.Object.SHA, entries)
return tree, err
}
func getFileContent(fileArg string) (targetName string, b []byte, err error) {
var localFile string
files := strings.Split(fileArg, ":")
switch {
case len(files) < 1:
return "", nil, errors.New("empty files parameter")
case len(files) == 1:
localFile = files[0]
targetName = files[0]
default:
localFile = files[0]
targetName = files[1]
}
b, err = ioutil.ReadFile(localFile)
return targetName, b, err
}
func pushCommit(ref *github.Reference, tree *github.Tree, commitMessage *string) (err error) {
parent, _, err := client.Repositories.GetCommit(ctx, owner, repo, *ref.Object.SHA)
if err != nil {
return err
}
// This is not always populated, but is needed.
parent.Commit.SHA = parent.SHA
// Create the commit using the tree.
date := time.Now()
author := &github.CommitAuthor{Date: &date, Name: &authorName, Email: &authorEmail}
commit := &github.Commit{Author: author, Message: commitMessage, Tree: tree, Parents: []*github.Commit{parent.Commit}}
newCommit, _, err := client.Git.CreateCommit(ctx, owner, repo, commit)
if err != nil {
return err
}
// Attach the commit to the master branch.
ref.Object.SHA = newCommit.SHA
_, _, err = client.Git.UpdateRef(ctx, owner, repo, ref, false)
return err
}
func createPR(prSubject, prDescription, prBranch, commitBranch *string) (err error) {
newPR := &github.NewPullRequest{
Title: prSubject,
Head: commitBranch,
Base: prBranch,
Body: prDescription,
MaintainerCanModify: github.Bool(true),
}
pr, _, err := client.PullRequests.Create(ctx, owner, repo, newPR)
if err != nil {
return err
}
fmt.Printf("PR created: %s\n", pr.GetHTMLURL())
_, _, err = client.PullRequests.Merge(ctx, owner, repo, *pr.Number, "Merged!", &github.PullRequestOptions{})
if err != nil {
return err
}
_, err = client.Git.DeleteRef(ctx, owner, repo, fmt.Sprintf("refs/heads/%s", *commitBranch))
if err != nil {
return err
}
return nil
}
func handleGetBuild(w http.ResponseWriter, req *http.Request) error {
params := req.URL.Query()
project := params.Get("project")
commit := params.Get("commit")
if project == "" || commit == "" {
return fmt.Errorf("project or commit param is missing")
}
if commit != "" {
content, err := sess.Download(myBucket, fmt.Sprintf("%s/%s/build.yaml", project, commit))
if err != nil {
return fmt.Errorf("failed to get %s/%s/build.yaml from the bucket", project, commit)
}
var build Build
err = json.Unmarshal(content, &build)
if err != nil {
return fmt.Errorf("failed to unmarshal the build.yaml content to Build structure")
}
content, err = sess.Download(myBucket, fmt.Sprintf("%s/%s/%s", project, commit, build.Artifacts[0]))
if err != nil {
return fmt.Errorf("failed to get %s/%s/%s from the bucket", project, commit, build.Artifacts[0])
}
contentType := http.DetectContentType(content)
size := strconv.FormatInt(int64(len(content)), 10)
//Send the headers
w.Header().Set("Content-Disposition", "attachment; filename="+build.Artifacts[0])
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", size)
io.Copy(w, bytes.NewReader(content))
}
return nil
}
func getFromFile(keyname string, req *http.Request) ([]byte, string, error) {
var buf bytes.Buffer
// in your case formFile would be fileupload
formFile, header, err := req.FormFile(keyname)
if err != nil {
return nil, "", fmt.Errorf("failed to get the %s from formFile", keyname)
}
defer formFile.Close()
io.Copy(&buf, formFile)
defer buf.Reset()
return buf.Bytes(), header.Filename, nil
}
func handlePostBuild(req *http.Request) error {
klog.Infoln("Entry: handlePostBuild")
klog.Infoln("Parsing ParseMultipartForm")
if err := req.ParseMultipartForm(32 << 20); err != nil {
klog.Errorf("failed to ParseMultipartForm")
return fmt.Errorf("failed to ParseMultipartForm")
}
for key, _ := range req.Form {
klog.Infof("%s = %s\n", key, req.Form.Get(key))
}
if req.Form.Get("source") == "" || req.Form.Get("commit") == "" || req.Form.Get("project") == "" {
return fmt.Errorf("source, commit and project are the must have form params")
}
params := req.URL.Query()
dryrun := params.Get("dryrun")
var files []string
for fl := range req.MultipartForm.File {
content, filename, err := getFromFile(fl, req)
if err != nil {
return fmt.Errorf("failed to get the file content: %v", err)
}
klog.Infof("file found in the request: %s and the actual filename is %s", fl, filename)
files = append(files, filename)
if dryrun != "" {
continue
}
err = sess.Upload(myBucket, fmt.Sprintf("%s/%s/%s", req.Form.Get("project"), req.Form.Get("commit"), filename), content)
if err != nil {
return fmt.Errorf("failed to upload the file: %s to S3 object store", fl)
}
}
b := Build{
req.Form.Get("source"),
req.Form.Get("commit"),
files,
req.Form.Get("project"),
}
if dryrun != "" {
klog.Infof("Build: %v", b)
return nil
}
tmpfs, err := ioutil.TempFile("", "build")
if err != nil {
return fmt.Errorf("failed to create a tempfile: %v", err)
}
defer os.Remove(tmpfs.Name())
bytes, err := json.MarshalIndent(b, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal build struct: %v", err)
}
// Uploading the build.yaml to the bucket
err = sess.Upload(myBucket, fmt.Sprintf("%s/%s/%s", req.Form.Get("project"), req.Form.Get("commit"), "build.yaml"), bytes)
if err != nil {
return fmt.Errorf("failed to upload the content to S3 object store")
}
_, err = tmpfs.Write(bytes)
if err != nil {
return fmt.Errorf("failed to write build content to formFile: %v", err)
}
if err := tmpfs.Close(); err != nil {
return fmt.Errorf("failed to close the formFile: %v", err)
}
file := fmt.Sprintf("%s:%s/build.yaml", tmpfs.Name(), b.Project)
commitMessage := fmt.Sprintf("%s commit %s", b.Project, b.Commit)
commitBranch := strconv.FormatInt(time.Now().Unix(),10)
baseBranch := "master"
prSubject := fmt.Sprintf("Update %s build", b.Project)
prDescription := "This is an automated PR via build-bot"
ref, err := getRef(baseBranch, commitBranch)
if err != nil {
return err
}
tree, err := getTree(ref, file)
if err != nil {
return err
}
if err := pushCommit(ref, tree, &commitMessage); err != nil {
return fmt.Errorf("Unable to create the commit: %s\n", err)
}
if err := createPR(&prSubject, &prDescription, &baseBranch, &commitBranch); err != nil {
return fmt.Errorf("Error while creating the pull request: %s", err)
}
klog.Infoln("Exit: handlePostBuild")
return nil
}
func gitInit() error {
var token string
// override the token set in the GITHUB_AUTH_TOKEN env if any
if tkn := os.Getenv("GITHUB_AUTH_TOKEN"); tkn != "" {
token = tkn
} else {
if tokenPath == "" {
return fmt.Errorf("github token is missing, either --github-token-path or GITHUB_AUTH_TOKEN env is missing")
}
secret, err := ioutil.ReadFile(tokenPath)
if err != nil {
return err
}
token = string(bytes.TrimSpace(secret))
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
return nil
}