This repository was archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.js
337 lines (288 loc) · 10 KB
/
main.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const path = require('path')
const cfg = require('../src/config.js')(path.resolve(__dirname, '../resources/config.json'))
// Modules
const projects = require('../src/projects.js')
const maven = require('../src/maven.js')
const gradle = require('../src/gradle.js')
const github = require('../src/github.js')(cfg.github)
const discord = require('../src/discord.js')(cfg.discord)
const log = require('../src/logger.js')
module.exports = {
start,
check,
update,
compile,
gatherResources,
upload,
finish,
/**
* This method returns the discord config used by this instance
*
* @return {Object} Config
*/
getConfig: () => cfg
}
/**
* This method starts the default lifecycle for all projects.
* It also returns a Promise that can signal when its done.
*
* @param {Boolean} logging Whether the internal activity should be logged.
* @return {Promise} A Promise that is resolved after all projects have finished their lifecycle
*/
function start (logging) {
return new Promise((resolve, reject) => {
log(logging, 'Loading Projects...')
projects.getProjects(logging).then(jobs => {
global.status.jobs = jobs.slice(0)
for (const index in jobs) {
updateStatus(jobs[index], 'Queued')
}
let i = -1
const nextJob = () => {
i++
if (!global.status.running || i >= jobs.length) {
resolve()
} else {
log(logging, '')
log(logging, 'Watching: ' + jobs[i].author + '/' + jobs[i].repo + ':' + jobs[i].branch)
const job = jobs[i]
// Project Lifecycle
check(job, logging)
.then(() => update(job, logging)
.then(() => compile(job, logging)
.then(() => gatherResources(job, logging)
.then(() => upload(job, logging)
.then(() => finish(job, logging)
.then(() => updateStatus(jobs[i], 'Finished'))
.then(nextJob, reject),
reject),
reject),
reject),
reject),
nextJob)
}
}
nextJob()
})
})
}
/**
* This method pulls the latest commit from github and
* checks if it diverges from the local records.
*
* @param {Object} job The currently handled Job Object
* @param {Boolean} logging Whether the internal activity should be logged
* @return {Promise} A promise that resolves when this activity finished
*/
function check (job, logging) {
if (!global.status.running) {
return Promise.reject(new Error('The operation has been cancelled'))
}
if (!projects.isValid(job, false)) {
return Promise.reject(new Error('Invalid Job!'))
}
updateStatus(job, 'Pulling Commits')
return new Promise((resolve, reject) => {
github.getLatestCommit(job, logging).then(commit => {
const timestamp = parseInt(commit.commit.committer.date.replace(/\D/g, ''))
if (commit.commit.message.toLowerCase().startsWith('[ci skip]')) {
reject(new Error('Skipping build...'))
return
}
job.commit = {
sha: commit.sha,
date: github.parseDate(commit.commit.committer.date),
timestamp: timestamp,
message: commit.commit.message,
author: commit.author.login,
avatar: commit.author.avatar_url
}
github.hasUpdate(job, timestamp, logging).then(id => {
job.id = id + 1
projects.clearWorkspace(job).then(resolve, reject)
}, reject)
}, reject)
})
}
/**
* This method updates a Projects build number and
* changes it's pom.xml file to include this version.
* It also clones the repository.
*
* @param {Object} job The currently handled Job Object
* @param {Boolean} logging Whether the internal activity should be logged
* @return {Promise} A promise that resolves when this activity finished
*/
function update (job, logging) {
if (!global.status.running) {
return Promise.reject(new Error('The operation has been cancelled'))
}
if (!projects.isValid(job, false)) {
return Promise.reject(new Error('Invalid Job!'))
}
updateStatus(job, 'Cloning Repository')
return new Promise((resolve, reject) => {
log(logging, 'Updating: ' + job.author + '/' + job.repo + ':' + job.branch + ' (' + job.id + ')')
github.clone(job, job.commit.sha, logging).then(() => {
const name = (job.options ? job.options.prefix : 'DEV') + ' - ' + job.id + ' (git ' + job.commit.sha.substr(0, 8) + ')'
log(logging, `-> Building using: ${job.options.buildTool === null ? 'maven' : job.options.buildTool}`)
if (!job.options.buildTool || job.options.buildTool === 'maven') {
maven.setVersion(job, name, true).then(resolve, reject)
} else {
gradle.setVersion(job, name).then(resolve, reject)
}
}, reject)
})
}
/**
* This method compiles the project using Maven or Gradle depending on job.config.buildTool.
* After completing, the job update will have the flag 'success',
* that is either true or false.
*
* @param {Object} job The currently handled Job Object
* @param {Boolean} logging Whether the internal activity should be logged
* @return {Promise} A promise that resolves when this activity finished
*/
function compile (job, logging) {
if (!global.status.running) {
return Promise.reject(new Error('The operation has been cancelled'))
}
if (!projects.isValid(job, false)) {
return Promise.reject(new Error('Invalid Job!'))
}
updateStatus(job, 'Compiling')
return new Promise((resolve) => {
if (!job.options.buildTool || job.options.buildTool === 'maven') {
log(logging, `Compiling using Maven: ${job.author}/${job.repo}:${job.branch} (${job.id})`)
maven.compile(job, cfg, logging)
.then(() => {
job.success = true
resolve()
})
.catch((err) => {
log(logging, err.stack)
job.success = false
resolve()
})
} else {
log(logging, `Compiling using Gradle: ${job.author}/${job.repo}:${job.branch} (${job.id})`)
gradle.compile(job, logging)
.then(() => {
job.success = true
resolve()
})
.catch((err) => {
log(logging, err.stack)
job.success = false
resolve()
})
}
})
}
/**
* This method pulls all resources from github, such as the license,
* all version tags and also relocates the exported .jar file to the main project folder.
*
* @param {Object} job The currently handled Job Object
* @param {Boolean} logging Whether the internal activity should be logged
* @return {Promise} A promise that resolves when this activity finished
*/
function gatherResources (job, logging) {
if (!global.status.running) {
return Promise.reject(new Error('The operation has been cancelled'))
}
if (!projects.isValid(job, true)) {
return Promise.reject(new Error('Invalid Job!'))
}
updateStatus(job, 'Fetching Resources')
return new Promise((resolve, reject) => {
log(logging, 'Gathering Resources: ' + job.author + '/' + job.repo + ':' + job.branch)
const promises = [
github.getLicense(job, logging),
github.getTags(job, logging)
]
if (job.options.buildTool === 'maven') {
promises.push(maven.relocate(job))
} else {
promises.push(gradle.relocate(job))
}
Promise.all(promises).then((values) => {
const license = values[0]
const tags = values[1]
job.license = {
name: license.license.name,
id: license.license.spdx_id,
url: license.download_url
}
job.tags = {}
for (const index in tags) {
job.tags[tags[index].name] = tags[index].commit.sha
}
resolve()
}, reject)
})
}
/**
* This method updates the builds.json file,
* generates a new index.html and badge.svg file for the project.
* It will also signal this Update to our Discord Webhook.
*
* @param {Object} job The currently handled Job Object
* @param {Boolean} logging Whether the internal activity should be logged
* @return {Promise} A promise that resolves when this activity finished
*/
function upload (job, logging) {
if (!global.status.running) {
return Promise.reject(new Error('The operation has been cancelled'))
}
if (!projects.isValid(job, true)) {
return Promise.reject(new Error('Invalid Job!'))
}
global.status.task[job.author + '/' + job.repo + '/' + job.branch] = 'Preparing Upload'
return new Promise((resolve, reject) => {
const promises = [
projects.addBuild(job, logging),
projects.generateHTML(job, logging),
projects.generateBadge(job, logging)
]
log(logging, 'Uploading: ' + job.author + '/' + job.repo + ':' + job.branch + ' (' + job.id + ')')
// Discord counts as a form of "logging" in this sense
if (logging) {
promises.push(discord.sendUpdate(job))
}
Promise.all(promises).then(resolve, reject)
})
}
/**
* This method will finish the lifecycle.
* It pushes all changed files to github.
* It will also clear the project file for the next iteration.
*
* @param {Object} job The currently handled Job Object
* @param {Boolean} logging Whether the internal activity should be logged
* @return {Promise} A promise that resolves when this activity finished
*/
function finish (job, logging) {
// Check if the program is still running
if (!global.status.running) {
return Promise.reject(new Error('The operation has been cancelled'))
}
// Check if the job is still valid
if (!projects.isValid(job, true)) {
return Promise.reject(new Error('Invalid Job!'))
}
updateStatus(job, 'Uploading')
return Promise.all([
github.pushChanges(job, logging),
projects.clearWorkspace(job)
])
}
/**
* This updates our global status variable for the given job.
*
* @param {Object} job The currently handled Job Object
* @param {String} status The new status message for this job
*/
function updateStatus (job, status) {
global.status.task[job.author + '/' + job.repo + '/' + job.branch] = status
}