This repository was archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathservice.js
230 lines (193 loc) · 5.18 KB
/
service.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
var fs = require( "fs" ),
semver = require( "semver" ),
Step = require( "step" ),
config = require( "./config" ),
logger = require( "./logger" ),
Manifest = require( "./manifest" ),
suites = require( "./suites" ),
blacklist = require( "./blacklist" );
Manifest.suites = suites;
Manifest.blacklist = blacklist;
function extend( a, b ) {
for ( var prop in b ) {
a[ prop ] = b[ prop ];
}
}
function Repo() {
this.userId = this.service + "/" + this.userName;
this.id = this.userId + "/" + this.repoName;
this.path = config.repoDir + "/" + this.id;
}
// manifest
extend( Repo.prototype, {
getManifest: function( tag, file, fn ) {
var repo = this;
this._getManifest( tag, file, function( error, manifest ) {
if ( error ) {
return fn( error );
}
try {
manifest = JSON.parse( manifest );
} catch( error ) {
logger.log( "Invalid JSON in manifest", repo.id, tag, file );
repo.informInvalidJson({ tag: tag, file: file });
return fn( null, null );
}
fn( null, manifest );
});
},
validateManifest: function( manifest, version, prefix, filename ) {
var errors = Manifest.validate( manifest, version, prefix, filename );
if ( errors.length ) {
logger.log( "Manifest errors:", this.id, version, filename, errors );
}
return errors;
}
});
// versions
extend( Repo.prototype, {
getVersionTags: function( fn ) {
var repo = this;
Step(
function() {
repo.getTags( this );
},
function( error, tags ) {
if ( error ) {
if ( /Repository not found/.test( error.message ) ) {
repo.informRepoNotFound();
return fn( null, [] );
}
return fn( error );
}
return tags.filter(function( version ) {
// we allow a v prefix, but nothing else
if ( version.charAt( 0 ) === "v" ) {
version = version.substring( 1 );
}
// tag is not a clean version number
if ( semver.clean( version ) !== version ) {
return false;
}
return semver.valid( version );
});
},
fn
);
},
getRelease: function( tag, fn ) {
this.validateVersion( tag, function( error, manifests ) {
if ( error ) {
return fn( error );
}
if ( !manifests ) {
return fn( null, null );
}
fn( null, {
tag: tag,
manifests: manifests
});
});
},
validateVersion: function( tag, fn ) {
var repo = this;
Step(
// get list of manifest files
function() {
repo.getManifestFiles( tag, this );
},
// get all manifest files
function( error, files ) {
if ( error ) {
return fn( error );
}
if ( !files.length ) {
logger.log( "No manifest files for", repo.id, tag );
repo.informMissingManifset({ tag: tag });
return fn( null, null );
}
logger.log( "Found manifest files for", repo.id, tag, files );
this.parallel()( null, files );
var group = this.group();
files.forEach(function( file ) {
repo.getManifest( tag, file, group() );
});
},
// validate manifests
function( error, files, manifests ) {
var i, l, errors,
mappedManifests = {};
if ( error ) {
return fn( error );
}
// if any manifest is invalid, then the whole version is invalid
if ( manifests.some(function( manifest ) {
return !manifest;
})) {
return fn( null, null );
}
for ( i = 0, l = manifests.length; i < l; i++ ) {
errors = repo.validateManifest( manifests[ i ], tag,
suites[ repo.id ], files[ i ] );
if ( errors.length ) {
repo.informInvalidManifest({
tag: tag,
file: files[ i ],
errors: errors
});
return fn( null, null );
}
mappedManifests[ files[ i ] ] = manifests[ i ];
}
fn( null, mappedManifests );
}
);
}
});
// Status notifications
extend( Repo.prototype, {
inform: function( msg ) {
fs.appendFile( config.errorLog, (new Date()).toGMTString() + " " + msg + "\n" );
},
informMissingManifset: function( data ) {
this.inform( this.id + " " + data.tag + " has no manifest file(s)." );
},
informInvalidJson: function( data ) {
this.inform( this.id + " " + data.tag + " " + data.file + " is invalid JSON." );
},
informInvalidManifest: function( data ) {
this.inform( this.id + " " + data.tag + " " + data.file + " has the following errors: " + data.errors );
},
informOtherOwner: function( data ) {
this.inform( this.id + " " + data.tag + " cannot publish " + data.name + " which is owned by " + data.owner );
},
informRepoNotFound: function() {
this.inform( this.id + " repo not found on remote server." );
},
informSuccess: function( data ) {
this.inform( this.id + " SUCCESSFULLY ADDED " + data.name + " v" + data.version + "!" );
}
});
var services = {};
module.exports = {
Repo: Repo,
register: function( service, ServiceRepo ) {
ServiceRepo.prototype.service = service;
services[ service ] = ServiceRepo;
},
getRepoById: function( id ) {
var parts = id.split( "/" );
return new services[ parts[ 0 ] ]( parts[ 1 ], parts[ 2 ] );
},
getRepoByHook: function( data ) {
var service, parsed;
for ( service in services ) {
parsed = services[ service ].test( data );
if ( parsed ) {
return new services[ service ]( parsed );
}
}
return null;
}
};
require( "./service/github" );