-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.golo
232 lines (173 loc) Β· 7.77 KB
/
main.golo
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
module jarvisci
import java.text.MessageFormat
import gololang.Errors
import spark.Spark
import org.eclipse.egit.github.core.client.GitHubClient
import org.eclipse.egit.github.core.client.GitHubRequest
import config
function getGitHubEvent = |request| -> request: headers("X-GitHub-Event")
function getRepository = |data| ->
DynamicObject()
: html_url(data: get("repository"): get("html_url"))
: ssh_url(data: get("repository"): get("ssh_url"))
: url(data: get("repository"): get("url"))
: full_name(data: get("repository"): get("full_name"))
: name(data: get("repository"): get("name"))
: ref(data: get("ref"))
: branchName(|this| -> this: ref(): split("/"): asList(): last())
function getGitHubClient = {
var gitHubClient = null
if config(): enterprise() {
gitHubClient = GitHubClient(config(): host(), config(): port(), config(): scheme())
} else {
gitHubClient = GitHubClient(config(): host())
}
gitHubClient: setOAuth2Token(config(): token())
return gitHubClient
}
function console = ->
DynamicObject()
: log(|this, txt, args...| -> println(MessageFormat.format(txt, args)))
function getExecutorHelper = ->
DynamicObject()
: shell(|this, cmd| {
let p = Runtime.getRuntime(): exec(cmd)
return p: waitFor()
})
: sh(|this, cmd, args...| {
let p = Runtime.getRuntime(): exec(MessageFormat.format(cmd, args))
return p: waitFor()
})
: tmp_dir(null)
: checkout(|this, branchName| -> this: shell("./checkout.sh " + this: tmp_dir() + " " + branchName))
: clone(|this, repo| -> this: shell("git clone " + repo: url() + ".git " + this: tmp_dir()))
: info(|this, txt, args...| -> console(): log("βΉοΈ " + txt, args))
: success(|this, txt, args...| -> console(): log("β
" + txt, args))
: fail(|this, txt, args...| -> console(): log("π " + txt, args))
: warning(|this, txt, args...| -> console(): log("β οΈ " + txt, args))
----
TODO:
- use EvaluationEnvironment to load config
- try to see how I can test my golo project
----
function main = |args| {
let executorHelper = getExecutorHelper()
let env = gololang.EvaluationEnvironment()
setPort(config(): http_port())
spark.Spark.get("/", |request, response| {
response: type("application/json")
return JSON.stringify(DynamicObject(): message("Hello from Golo-CI"))
})
spark.Spark.get("/golo_ci", |request, response| {
response: type("application/json")
return JSON.stringify(DynamicObject(): message("Hello from Golo-CI"))
})
# Add a wehook to GhitHub Enterprise
# http://zeiracorp.local:8888/golo_ci
spark.Spark.post("/golo_ci", |request, response| {
response: type("application/json")
let eventName = getGitHubEvent(request)
#console(): log("GitHub Event: {0}", eventName)
let data = JSON.parse(request: body())
let action = data: get("action")
let after = data: get("after")
let owner = data: get("repository"): get("owner"): get("name")
let repoName = data: get("repository"): get("name")
let statuses_url = "/repos/" + owner + "/" + repoName + "/statuses/" + after
let gitHubClient = getGitHubClient()
if eventName: equals("pull_request") {
console(): log("GitHub Event: {0} action: {1}", eventName, action)
# https://developer.github.com/guides/delivering-deployments/
if action: equals("closed") and data: get("pull_request"): get("merged") {
console(): log("π {0}", "A pull request was merged! A deployment should start now...")
}
}
if eventName: equals("push") {
console(): log("GitHub Event: {0}", eventName)
let repo = getRepository(data)
executorHelper: tmp_dir("clones/" + uuid() + "-" +repo: name() + "-" + repo: branchName())
executorHelper: repo(repo)
if executorHelper: clone(repo): equals(0) {
if executorHelper: checkout(repo: branchName()):equals(0) {
let displayError = |sourceError| ->
|error| ->
console(): log(
"π Error from {0}: {1}",
sourceError orIfNull "π",
error orIfNull "π΅"
)
let doNothingJustDisplay = |value| -> console(): log("{0}", value orIfNull "I'm fine π")
# Building closure.
let runCiGolo = |goloSourceCode| {
# Initialize and build
trying({
# TODO: run as a worker or a thread and kill it if to long -> need a queue
# Run `check` function in `ci.golo`
let results = fun(
"check",
env: anonymousModule(goloSourceCode)
)(executorHelper)
# Display results
console(): log("π results: {0}", JSON.stringify(results))
# console(): log("statuses_url: {0}", statuses_url)
# Change status depending of build result
# this is the ci.golo file that drive the status
# Jarvis-CI is just a "messenger"
let status = results?: status()
orIfNull "Warning: status is not defined"
# -> generate an Error "state is not included in the list"
let description = results?: description()
orIfNull "Warning: description (and probably status) is not defined"
let context = results?: context()
orIfNull "jarvis-ci"
trying({
let set_status = gitHubClient: post(statuses_url,
map[
["state", status],
["description", description],
["context", context]
],
java.lang.Object.class
)
#TODO: how to check conflicts? -> API ?
#println("++>> " + set_status)
let checkStatus = |status| {
if status: equals("pending") { return "πΆ" }
if status: equals("success") { return "π" }
if status: equals("failure") { return "π‘" } else {
return "π’"
}
}
return "πΌ GitHub status are passed: \n" +
" - status: " + checkStatus(status) + " " + status + "\n" +
" - description: " + description + "\n" +
" - context: " + context
}): either(doNothingJustDisplay ,displayError("[status]"))
#TODO - plan to remove the build, cleaning, ...
return "π― check() from ci.golo was executed"
}): either(doNothingJustDisplay ,displayError("[check()]"))
} # end of runCiGolo
# change status at the begining of checking
trying({
gitHubClient: post(statuses_url,
map[
["state", "pending"],
["description", "Jarvis-CI is checking..."],
["context", "jarvis-ci"]
],
java.lang.Object.class
)
return "π GitHub pending status are passed on πΏ " + repo: branchName()
}): either(doNothingJustDisplay ,displayError("[status:pending]"))
# Try loading ci.golo from the current branch
# and run ci if ok
trying({
return fileToText(executorHelper: tmp_dir()+"/ci.golo", "UTF-8")
})
: either(runCiGolo ,displayError)
} # end of checkout
} # end of clone
} # end of push
return JSON.stringify(DynamicObject(): message("Hello from Golo-CI"))
})
}