forked from scottgonzalez/node-github-notifier
/
notifier.js
134 lines (116 loc) · 2.77 KB
/
notifier.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
var http = require( "http" ),
querystring = require( "querystring" ),
util = require( "util" ),
EventEmitter2 = require( "eventemitter2" ).EventEmitter2;
function Notifier() {
var notifier = this;
this.server = http.createServer(function( request, response ) {
var data = "";
request.setEncoding( "utf8" );
request.on( "data", function( chunk ) {
data += chunk;
});
request.on( "end", function() {
try {
data = querystring.parse( data );
data = JSON.parse( data.payload );
} catch( error ) {
// Invalid data, stop processing
response.writeHead( 400 );
response.end();
notifier.emit( "error", error );
return;
}
// Accept the request and close the connection
if ( notifier.process( data ) ) {
// Payload parsing successful
response.writeHead( 202 );
} else {
// Unrecognized payload
response.writeHead( 400 );
}
response.end();
});
});
EventEmitter2.call( this, {
wildcard: true,
delimiter: "/"
});
}
util.inherits( Notifier, EventEmitter2 );
Notifier.prototype.listen = function() {
this.server.listen.apply( this.server, arguments );
};
Notifier.services = [
/* github */
function ( raw ) {
if ( raw.ref )
{
return false;
}
var refParts = raw.ref.split( "/" ),
type = refParts[ 1 ],
owner = raw.repository.owner.name,
repo = raw.repository.name,
data = {
commit: raw.after,
owner: owner,
repo: repo,
raw: raw
},
eventName = owner + "/" + repo + "/" + raw.ref.substr( 5 );
if ( type === "heads" ) {
// Handle namespaced branches
data.branch = refParts.slice( 2 ).join( "/" );
} else if ( type === "tags" ) {
data.tag = refParts[ 2 ];
}
this.emit( eventName, data );
return true;
},
/* bitbucket */
function ( raw ) {
if ( raw.canon_url !== 'https://bitbucket.org' || 'undefined' === raw.repository ) {
return false;
}
var commit,
i,
repoParts = raw.repository.absolute_url.split( "/" ),
owner = repoParts[ 1 ],
repo = repoParts[ 2 ],
data = {
commit: null,
owner: owner,
repo: repo,
raw: raw
},
nodes = [];
for( i = 0; i < raw.commits.length; i++ ) {
commit = raw.commits[ i ];
if ( ( commit.branches && commit.branches.length )
|| null != commit.branch) {
if ( commit.branch != null)
{
data.branch = commit.branch
}
data.commit = commit.node;
eventName = owner + "/" + repo + "/" + commit.node.substr( 5 );
this.emit( eventName, data );
}
}
return true;
}
]
Notifier.prototype.process = function( raw ) {
var i = Notifier.services.length - 1;
for ( ; i >= 0; i-- ) {
if ( Notifier.services[ i ].call( this, raw ) ) {
return true;
}
}
return false;
};
exports.Notifier = Notifier;
exports.createServer = function() {
return new Notifier();
};