-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhook.js
More file actions
83 lines (75 loc) · 1.9 KB
/
Copy pathhook.js
File metadata and controls
83 lines (75 loc) · 1.9 KB
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
var spawn = require('child_process').spawn
var githubhook = require('githubhook')
var PROJECT = {
repository: 'ExampleApp',
name: 'example',
path: '../example',
}
var DEPLOY_ARGS = {
'--name': PROJECT.name + '-container',
'-e': [
'host=0.0.0.0',
'port=80',
],
'-p': '80:80',
'-v': [
PROJECT.path + ':/app',
'/app/node_modules',
'wwwroot:/wwwroot',
]
}
var github = githubhook({
host: '0.0.0.0',
port: '9001',
path: '/',
})
github.on('pull_request:' + PROJECT.repository, function(ref, data) {
if (
data.pull_request &&
data.action === 'closed' &&
data.pull_request.merged === true &&
data.pull_request.base.ref === 'master'
) {
var args = unpackArgs(DEPLOY_ARGS)
console.log('running', './deploy.sh ' + [PROJECT.name, PROJECT.path, 'master', args].join(' '))
var child = spawn('bash',
['./deploy.sh', PROJECT.name, PROJECT.path, 'master']
.concat(args.split(' '))
.filter(function(e) { return e !== '' })
)
child.stdout.setEncoding('utf8')
child.stdout.on('data', function(data) {
console.log(data)
})
child.stderr.setEncoding('utf8')
child.stderr.on('data', function(error) {
console.log(error)
})
}
})
function unpackArgs(args) {
var strArgs = ''
for (var key in args) {
if (typeof args[key] === 'string') {
strArgs += [key, args[key]].join(' ') + ' '
} else if (args[key] instanceof Array) {
strArgs += args[key].cyclicZip([key]).reverse().join(' ') + ' '
} else {
console.error('Argument of unexpected type: ' + typeof args[key])
}
}
return strArgs
}
Array.prototype.cyclicZip = function(other) {
if (!(other instanceof Array)) {
console.error('Cannot zip Array with ' + (typeof other) + '.')
return
}
return [].concat.apply(
[],
this.map(function(e, i) {
return [e, other[i % other.length]]
})
)
}
github.listen()