Skip to content

Commit 532a518

Browse files
author
Jose Constela
authored
Merge pull request #11 from tideflow-io/github_ci_service
GitHub ci service
2 parents 953b0ec + fbd2179 commit 532a518

File tree

8 files changed

+5358
-209
lines changed

8 files changed

+5358
-209
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"workbench.colorCustomizations": {}
3+
}

agent.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const io = require('socket.io-client')
33
const colors = require('colors')
44
const url = require('url')
55
const pretty = require('./helpers/pretty')
6-
const runner = require('./runner')
6+
const services = require('./services')
77
const os = require('os')
88

99
/**
@@ -34,18 +34,47 @@ module.exports.exec = (program) => {
3434

3535
const socket = io(`${URL}?token=${program.token}`)
3636

37-
// Execute command
38-
socket.on('tf.command', function (req) {
37+
socket.on('tf.githubCi.pullRequest', (req) => {
38+
if (!agent.authenticated) return
39+
q.push(services.githubCi.pullRequest(socket, 'tf.githubCi.pullRequest', req))
40+
})
41+
42+
socket.on('tf.githubCi.pullRequest.execution.finished', (req) => {
43+
if (!agent.authenticated) return
44+
q.push(services.githubCi.executionFinished(socket, 'tf.githubCi.pullRequest.execution.finished', req))
45+
})
46+
47+
socket.on('tf.githubCi.push', (req) => {
48+
if (!agent.authenticated) return
49+
q.push(services.githubCi.push(socket, 'tf.githubCi.push', req))
50+
})
51+
52+
socket.on('tf.githubCi.push.execution.finished', (req) => {
3953
if (!agent.authenticated) return
40-
q.push(runner.cmd(socket, 'tf.command', req))
54+
q.push(services.githubCi.executionFinished(socket, 'tf.githubCi.push.execution.finished', req))
4155
})
4256

43-
// Execute code
44-
socket.on('tf.code', function () {
57+
socket.on('tf.githubCi.test_cmd', (req) => {
4558
if (!agent.authenticated) return
46-
q.push(runner.code(socket, 'tf.code', req))
59+
q.push(services.githubCi.test_cmd(socket, 'tf.githubCi.test_cmd', req))
4760
})
4861

62+
socket.on('tf.githubCi.run_cmd', (req) => {
63+
if (!agent.authenticated) return
64+
q.push(services.githubCi.run_cmd(socket, 'tf.githubCi.run_cmd', req))
65+
})
66+
67+
// Execute command
68+
socket.on('tf.agent.execute', function (req) {
69+
if (!agent.authenticated) return
70+
q.push(services.agent.execute(socket, 'tf.agent.execute', req))
71+
})
72+
73+
socket.on('tf.agent.code_nodesfc', function(req) {
74+
if (!agent.authenticated) return
75+
q.push(services.agent.codeNodeSfc(socket, 'tf.agent.code_nodesfc', req))
76+
})
77+
4978
// Authorize agent
5079
socket.on('tf.authz', function (auth) {
5180
agent = Object.assign({authenticated: true}, agent, auth)

helpers/report.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,53 @@ module.exports.parseStd = parseStd
3737
* Reports stds outputed by the in-execution command.
3838
*
3939
* @param {Object} socket Socket's io socket that requested the execution
40-
* @param {String} topic Original message's topic
4140
* @param {Object} originalMessage Message that originally requested the execution
4241
* @param {String} stdout Command's stdout output
4342
* @param {String} stderr Command's stderr output
4443
*/
45-
const progress = (socket, topic, originalMessage, stdout, stderr) => {
44+
const progress = async (socket, originalMessage, stdout, stderr, date) => {
4645
console.log(' || STDOUT/ERR. Reporting...'.yellow)
4746

4847
const res = Object.assign(originalMessage, {
4948
stdout: parseStd(stdout),
50-
stderr: parseStd(stderr)
49+
stderr: parseStd(stderr),
50+
date: date || null
5151
})
5252

5353
// Make sure the agent doesn't make a report to the server, unless there's
5454
// an actual std output to be reported.
5555
if (!res.stdout && !res.stderr) { return }
5656

57-
socket.emit(`${topic}.progress`, res)
57+
await socket.emit('tf.notify.progress', res)
5858
}
5959

6060
module.exports.progress = progress
6161

62+
/**
63+
* Reports the final result of an execution, with
64+
* all stds as an array
65+
*
66+
* @param {Object} socket Socket's io socket that requested the execution
67+
* @param {Object} originalMessage Message that originally requested the execution
68+
* @param {object} result Command's stds and exit code output:
69+
*
70+
* {
71+
* stdLines: [{
72+
* output: string,
73+
* err: boolean,
74+
* date: date
75+
* }],
76+
* code: number
77+
* }
78+
*/
79+
const bulkResult = async (socket, originalMessage, result) => {
80+
console.log(' || BULK STDOUT/ERR. Reporting...'.yellow)
81+
const res = Object.assign(originalMessage, result)
82+
await socket.emit('tf.notify.finishBulk', res)
83+
}
84+
85+
module.exports.bulkResult = bulkResult
86+
6287
/**
6388
* Reports the final result of an execution.
6489
*
@@ -67,15 +92,13 @@ module.exports.progress = progress
6792
* @param {Object} originalMessage Message that originally requested the execution
6893
* @param {Object} res Object containing both stds
6994
*/
70-
const result = (socket, topic, originalMessage, res) => {
95+
const result = (socket, originalMessage, res) => {
7196
console.log(' || Command executed. Reporting...'.yellow)
7297

73-
const message = Object.assign(originalMessage, {
98+
socket.emit('tf.notify.finish', Object.assign(originalMessage, {
7499
stdout: parseStd(res.stdout),
75100
stderr: parseStd(res.stderr)
76-
})
77-
78-
socket.emit(`${topic}.res`, message)
101+
}))
79102
}
80103

81104
module.exports.result = result
@@ -87,10 +110,10 @@ module.exports.result = result
87110
* @param {Object} originalMessage Message that originally requested the execution
88111
* @param {Object} executionResult Catched exception
89112
*/
90-
const exception = (socket, topic, originalMessage, ex) => {
113+
const exception = (socket, originalMessage, ex) => {
91114
console.log(' || Command returned error'.red)
92115

93-
socket.emit(`${topic}.exception`, Object.assign(originalMessage, {ex}))
116+
socket.emit('tf.notify.exception', Object.assign(originalMessage, {ex}))
94117
}
95118

96119
module.exports.exception = exception

0 commit comments

Comments
 (0)