-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
195 lines (159 loc) · 5.06 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
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
type githubRepository struct {
ID int `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
}
type githubCommit struct {
ID string `json:"id"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
URL string `json:"url"`
}
type githubPush struct {
Ref string `json:"ref"`
Repository githubRepository `json:"repository"`
HeadCommit githubCommit `json:"head_commit"`
}
func main() {
serverCI := http.NewServeMux()
serverCI.HandleFunc("/", handleCI)
http.ListenAndServe(":3000", serverCI)
}
func handleCI(w http.ResponseWriter, r *http.Request) {
fmt.Printf("\n-->Request: %v\n\n", r)
if strings.HasSuffix(r.URL.Path, "/badge") {
generateBadge(w, r)
} else if strings.HasSuffix(r.URL.Path, "/output") {
readOutput(w, r)
} else {
if r.URL.Path == "/" {
runTestsAndBuild(w, r)
}
}
}
func runTestsAndBuild(w http.ResponseWriter, r *http.Request) {
fmt.Printf("r: %v\n", r.RequestURI)
push, err := proccessGithubPayload(r)
if err == nil {
if isMiniCi(push) {
fmt.Printf("File .mini-ci.yml found. Lets build this app!\n")
//1 - create the commit folder
createCommitFolder(push)
//2 - then run the docker image
execDocker(push)
}
} else {
fmt.Printf("Err: %v\n", err)
}
}
//proccessGithubPayload unmarshal the github payload and returns the relevant
//data in a proper structure
func proccessGithubPayload(r *http.Request) (githubPush, error) {
x, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var push githubPush
err := json.Unmarshal(x, &push)
return push, err
}
//isMiniCi verify if we need to run the CI in this push
//if the project has a file named .mini-ci.yml then we need run the CI
func isMiniCi(push githubPush) bool {
urlYml := "https://api.github.com/repos/" + push.Repository.FullName + "/contents/.mini-ci.yml?ref=" + push.HeadCommit.ID
resp, err := http.Get(urlYml)
if err == nil {
if resp.StatusCode == 200 {
return true
}
}
return false
}
//createCommitFolder creates the folder that represents the commit
//this folder will retain the output returned by the tests
func createCommitFolder(push githubPush) {
//we gonna use this directory to save the output returned by the tests
dir := "./repositories/" + push.Repository.FullName + "/" + push.HeadCommit.ID + "/"
os.MkdirAll(dir, os.ModeDir|0775)
}
func execDocker(push githubPush) {
envApp := "APP=" + push.Repository.FullName
envCommit := "COMMIT=" + push.HeadCommit.ID
fmt.Printf("Run the Docker image\n")
fmt.Printf("docker run --rm -e '%v' -e '%v' -t minici:dev \n", envApp, envCommit)
//run the docker image
out, err := exec.Command("docker", "run", "--rm", "-e", envApp, "-e", envCommit, "-t", "minici:dev").Output()
if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("Write the output to file\n")
//write the output to file
fileOut := "./repositories/" + push.Repository.FullName + "/" + push.HeadCommit.ID + "/output"
ioutil.WriteFile(fileOut, out, 0644)
fmt.Printf("%s", string(out))
writeBuildStatus(push, string(out))
}
func writeBuildStatus(push githubPush, cmdOutput string) {
//split the output in lines
lines := strings.Split(cmdOutput, "\n")
//the last line contain the exit code, we need to get len(-2) because the output comes with an \r
exitCode := lines[len(lines)-2]
exitCode = strings.Replace(exitCode, "\r", "", -1)
//create the folder that will contain the build result file
parts := strings.Split(push.Ref, "/")
dir1 := parts[0]
dir2 := parts[1]
fileName := parts[2]
os.MkdirAll("./repositories/"+push.Repository.FullName+"/"+dir1+"/"+dir2+"/", os.ModeDir|0775)
//file that will store the build result
buildResultFile := "./repositories/" + push.Repository.FullName + "/" + dir1 + "/" + dir2 + "/" + fileName
//exitCode = 0 [sucess]
//exitCode = 1 [failed]
if exitCode == "0" {
ioutil.WriteFile(buildResultFile, []byte("success"), 0644)
} else {
ioutil.WriteFile(buildResultFile, []byte("failed"), 0644)
}
}
func readOutput(w http.ResponseWriter, r *http.Request) {
result, err := ioutil.ReadFile("." + r.URL.Path)
if err == nil {
w.Write([]byte(result))
} else {
w.Write([]byte(err.Error()))
}
}
func generateBadge(w http.ResponseWriter, r *http.Request) {
result, err := ioutil.ReadFile("." + r.URL.Path[:len(r.URL.Path)-6])
strResult := strings.Replace(string(result), "\n", "", -1)
if err == nil {
if strResult == "success" {
writeBadge(w, "badges/pass.png")
} else if strResult == "failed" {
writeBadge(w, "badges/fail.png")
} else {
w.Write([]byte("Error obtaining build status"))
}
} else {
w.Write([]byte(err.Error()))
}
}
func writeBadge(w http.ResponseWriter, filename string) {
const layout = "Mon, 02 Jan 2006 15:04:05 GMT"
w.Header().Add("content-type", "image/png")
w.Header().Set("cache-control", "no-cache, private")
w.Header().Set("date", time.Now().UTC().Format(layout))
w.Header().Set("expires", "Fri, 01 Jan 1984 00:00:00 GMT")
icon, _ := ioutil.ReadFile(filename)
w.Write(icon)
}