/
gith.js
298 lines (247 loc) · 7.75 KB
/
gith.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
/*
* gith
* https://github.com/danheberden/gith
*
* Copyright (c) 2012 Dan Heberden
* Licensed under the MIT license.
*/
var http = require("http");
var EventEmitter2 = require( "eventemitter2" ).EventEmitter2;
var util = require("util");
var _ = require("lodash");
var querystring = require("querystring");
var filterSettings = function( settings, payload ) {
if ( !payload ) {
return false;
}
settings = settings || {};
// add the matches object for later
payload.matches = {};
// check all the things
var checksPassed = true;
[ 'repo', 'branch', 'file', 'tag' ].forEach( function( thing ) {
var wat = settings[ thing ];
// default to a passed state
var passed = true;
// was a filter specified? and is it not a wildcard?
if ( wat && wat !== '*' ) {
// checking, so default passed to false
passed = false;
// make an array of the thing or all the files
var checks = [].concat( thing === 'file' ? payload.files.all : payload[ thing ] );
// all the checks - did any of them pass?
passed = checks.some( function( check ) {
// direct match
if ( wat === check ) {
return true;
}
// if negated match (!string)
if ( _.isString( wat ) && wat[0] === "!" &&
wat.slice(1) !== check ) {
return true;
}
// regex?
if ( _.isRegExp( wat ) ) {
var match = check.match( wat );
// did it match? huh? did it?
if ( match ) {
// goddamn files being different
if ( thing === 'file' ) {
if ( !payload.matches.files ) {
payload.matches.files = {};
}
payload.matches.files[ check ] = match;
} else {
payload.matches[ thing ] = match;
}
return true;
}
}
});
// usr function?
if ( _.isFunction( wat ) ) {
passed = wat( payload[ thing ], payload );
}
// assign the final result of this 'thing' to checksPassed
checksPassed = passed && checksPassed;
}
});
return checksPassed;
};
// Used by exports.module.create's returned function to create
// new gith objects that hold settings and emit events
var Gith = function( eventaur, settings ) {
var gith = this;
this.settings = settings || {};
// make this bad boy an event emitter
EventEmitter2.call( this, {
delimiter: ':',
maxListeners: 0
});
// handle bound payloads
eventaur.on( 'payload', function( originalPayload ) {
// make a simpler payload
var payload = gith.simplifyPayload( originalPayload );
// bother doing anything?
if ( filterSettings( settings, payload ) ) {
// all the things
gith.emit( 'all', payload );
// did we do any branch work?
if ( originalPayload.created && originalPayload.forced && payload.branch ) {
gith.emit( 'branch:add', payload );
}
if ( originalPayload.deleted && originalPayload.forced && payload.branch ) {
gith.emit( 'branch:delete', payload );
}
// how about files?
if ( payload.files.added.length > 0 ) {
gith.emit( 'file:add', payload );
}
if ( payload.files.deleted.length > 0 ) {
gith.emit( 'file:delete', payload );
}
if ( payload.files.modified.length > 0 ) {
gith.emit( 'file:modify', payload );
}
if ( payload.files.all.length > 0 ) {
gith.emit( 'file:all', payload );
}
// tagging?
if ( payload.tag && originalPayload.created ) {
gith.emit( 'tag:add', payload );
}
if ( payload.tag && originalPayload.deleted ) {
gith.emit( 'tag:delete', payload );
}
}
});
};
// inherit the EventEmitter2 stuff
util.inherits( Gith, EventEmitter2 );
// expose the simpliyPayload method on gith()
Gith.prototype.simplifyPayload = function( payload ) {
payload = payload || {};
var branch = '';
var tag = '';
var rRef = /refs\/(tags|heads)\/(.*)$/;
// break out if it was a tag or branch and assign
var refMatches = ( payload.ref || "" ).match( rRef );
if ( refMatches ) {
if ( refMatches[1] === "heads" ) {
branch = refMatches[2];
}
if ( refMatches[1] === "tags" ) {
tag = refMatches[2];
}
}
// if branch wasn't found, use base_ref if available
if ( !branch && payload.base_ref ) {
branch = payload.base_ref.replace( rRef, '$2' );
}
var simpler = {
original: payload,
files: {
all: [],
added: [],
deleted: [],
modified: []
},
tag: tag,
branch: branch,
repo: payload.repository ? (payload.repository.owner.name + '/' + payload.repository.name) : null,
sha: payload.after,
time: payload.repository ? payload.repository.pushed_at : null,
urls: {
head: payload.head_commit ? payload.head_commit.url : '',
branch: '',
tag: '',
repo: payload.repository ? payload.repository.url : null,
compare: payload.compare
},
reset: !payload.created && payload.forced,
pusher: payload.pusher ? payload.pusher.name : null,
owner: (payload.repository && payload.repository.owner) ? payload.repository.owner.name : null
};
if ( branch ) {
simpler.urls.branch = simpler.urls.branch + '/tree/' + branch;
}
if ( tag ) {
simpler.urls.tag = simpler.urls.head;
}
// populate files for every commit
(payload.commits || []).forEach( function( commit ) {
// github label and simpler label ( make 'removed' deleted to be consistant )
_.each( { added: 'added', modified: 'modified', removed: 'deleted' }, function( s, g ) {
simpler.files[ s ] = simpler.files[ s ].concat( commit[ g ] );
simpler.files.all = simpler.files.all.concat( commit[ g ] );
});
});
return simpler;
// todo: use github api to find what files were removed if the
// head was reset? maybe?
};
// the listen method - this gets added/bound in
// module.exports.create, fyi
var listen = function( eventaur, port ) {
// are we changing ports?
if ( port ) {
this.port = port;
}
if ( !this.port ) {
throw new Error('.listen() requires a port to be set');
}
this.server = http.createServer( function( req, res ) {
var data = "";
if ( req.method === "POST" ) {
req.on( "data", function( chunk ) {
data += chunk;
});
}
req.on( "end", function() {
if ( /^payload=/.test( data ) ) {
var payload = JSON.parse( querystring.unescape(data.slice(8)) );
eventaur.emit( "payload", payload );
res.writeHead( 200, {
'Content-type': 'text/html'
});
}
res.end();
});
}).listen( port );
};
// make require('gith')( 9001 ) work if someone really wants to
module.exports = function( port ) {
return module.exports.create( port );
};
// make the preferred way of `require('gith').create( 9001 ) work
module.exports.create = function( port ) {
// make an event emitter to use for the hardcore stuff
var eventaur = new EventEmitter2({
delimter: ':',
maxListeners: 0
});
// return a function that
// a) holds its own server/port/whatever
// b) exposes a listen method
// c) is a function that returns a new Gith object
var ret = function( map ) {
// make a new Gith with a reference to this factory
return new Gith( eventaur, map );
};
// add the listen method to the function - bind to ret
// and send eventaur to it
ret.listen = listen.bind( ret, eventaur );
// expose ability to close http server
ret.close = function(){
this.server.close();
};
ret.payload = function( payload ) {
eventaur.emit( 'payload', payload );
};
// if create was sent port, call listen automatically
if ( port ) {
ret.listen( port );
}
// return the new function
return ret;
};