-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
300 lines (272 loc) · 10.6 KB
/
index.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
299
300
#!/usr/bin/env node
'use strict';
/*
___ _ _ _____ _ _
/ _ \ | | | | / __ \ | | |
/ /_\ \ __| | __ _ _ __ | |_ ___ _ __ | / \/ |__ ___ ___| | _____ _ __
| _ |/ _` |/ _` | '_ \| __/ _ \ '__| | | | '_ \ / _ \/ __| |/ / _ \ '__|
| | | | (_| | (_| | |_) | || __/ | | \__/\ | | | __/ (__| < __/ |
\_| |_/\__,_|\__,_| .__/ \__\___|_| \____/_| |_|\___|\___|_|\_\___|_|
| |
|_|
*/
const axios = require('axios');
const issues = require('./doc/issues');
const version = require('./package.json').version;
// include submodules
const common = require('./lib/common.js');
const config = require('./lib/config.js');
const M000_PackageJson = require('./lib/M000_PackageJson.js');
const M100_IOPackageJson = require('./lib/M100_IOPackageJson.js');
const M250_Npm = require('./lib/M250_Npm.js');
const M300_Testing = require('./lib/M300_Testing.js');
const M400_Repository = require('./lib/M400_Repository.js');
const M500_Code = require('./lib/M500_Code.js');
const M600_Readme = require('./lib/M600_Readme.js');
const M700_License = require('./lib/M700_License.js');
const M800_Github = require('./lib/M800_Github.js');
const M900_GitNpmIgnore = require('./lib/M900_GitNpmIgnore.js');
// disable axios caching
axios.defaults.headers = {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
};
// Error ranges
// E0xx
// check package.json
//
// E100 - 249
// check io-package.json
//
// E250 - 299
// check npm and npmjs.org
//
// E3xx
// check testing
//
// E4xx
// check repositories
//
// E5xx
// check code
//
// E6xx
// check README file
//
// E7xx
// check license file
//
// E8xx
// check github repository
//
// E9xx
// check .gitignore file
function getGithubApiData(context) {
return new Promise((resolve, reject) => {
console.log('\ngetGithubApiData');
axios
.get(context.githubUrlApi, { cache: false })
.then((response) => {
context.githubApiData = response.data;
// console.log(`API Data: ${JSON.stringify(context.githubApiData)}`);
if (!context.branch) {
context.branch = context.githubApiData.default_branch; // main vs. master
console.log(`Branch was not defined by user - checking branch: ${context.branch}`);
}
context.githubUrl = `${context.githubUrlOriginal.replace('https://github.com', 'https://raw.githubusercontent.com')}/${context.branch}`;
console.log(`Original URL: ${context.githubUrlOriginal}`);
console.log(`raw: ${context.githubUrl}`);
console.log(`api: ${context.githubUrlApi}`);
resolve(context);
})
.catch((e) => {
context.errors.push(`[E000] FATAL: cannot access repository ${context.githubUrlApi}`);
reject(e);
}); // E0xx
});
}
function makeResponse(code, data) {
return {
statusCode: code || 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
'Access-Control-Allow-Credentials': true, // Required for cookies, authorization headers with HTTPS
},
body: typeof data === 'string' ? data : JSON.stringify(data),
};
}
function check(request, ctx, callback) {
// console.log('PROCESS: ' + JSON.stringify(request));
if (!request.queryStringParameters.url) {
return callback(null, makeResponse(500, { error: 'No github URL provided' }));
} else {
const context = { checks: [], errors: [], warnings: [] };
let githubUrl = request.queryStringParameters.url;
const githubBranch = request.queryStringParameters.branch;
githubUrl = githubUrl
.replace('http://', 'https://')
.replace('https://www.github.com', 'https://github.com')
.replace('https://raw.githubusercontent.com/', 'https://github.com/');
if (githubUrl.match(/\/$/)) {
githubUrl = githubUrl.substring(0, githubUrl.length - 1);
}
context.version = version;
config.initConfig(context);
context.githubUrlOriginal = githubUrl;
context.githubUrlApi = githubUrl.replace('https://github.com/', 'https://api.github.com/repos/');
context.branch = githubBranch || null;
getGithubApiData(context)
.then((context) => M800_Github.getCommitInfos(context))
.then((context) => M000_PackageJson.getPackageJson(context))
.then((context) => M100_IOPackageJson.getIOPackageJson(context))
.then((context) => M000_PackageJson.checkPackageJson(context))
.then((context) => M100_IOPackageJson.checkIOPackageJson(context))
.then((context) => M250_Npm.checkNpm(context))
.then((context) => M400_Repository.checkRepository(context))
.then((context) => M500_Code.checkCode(context))
.then((context) => M300_Testing.checkTests(context))
.then((context) => M800_Github.checkGithubRepo(context))
.then((context) => M600_Readme.checkReadme(context))
.then((context) => M700_License.checkLicenseFile(context))
.then((context) => M900_GitNpmIgnore.checkNpmIgnore(context))
.then((context) => M900_GitNpmIgnore.checkGitIgnore(context))
.then((context) => {
return callback(
null,
makeResponse(200, {
result: 'OK',
checks: context.checks,
errors: context.errors,
warnings: context.warnings,
version,
hasTravis: context.hasTravis,
lastCommitSha: context.lastCommitSha,
}),
);
})
.catch((err) => {
console.error(`GLOBAL ERROR: ${err.toString()}, ${JSON.stringify(err)}`);
context.errors.push(`[E999] GLOBAL ERROR: ${err.toString()}, ${JSON.stringify(err)}`);
return callback(
null,
makeResponse(200, {
result: 'Errors found',
checks: context.checks,
errors: context.errors,
issues,
warnings: context.warnings,
version,
hasTravis: context.hasTravis,
lastCommitSha: context.lastCommitSha,
error: `${err.request ? err.request.path : ''} ${err.message}`,
}),
);
});
}
}
function getText(text, lang) {
if (typeof text === 'object') {
if (text[lang]) {
return text[lang];
} else {
return text.en;
}
}
return text;
}
if (typeof module !== 'undefined' && module.parent) {
exports.handler = check;
} else {
let repoUrl = null;
let repoBranch = null;
// check options
if (process.argv.includes('-d')) {
process.argv.splice(process.argv.indexOf('-d'), 1);
common.setDebug(true);
}
if (process.argv.includes('--debug')) {
process.argv.splice(process.argv.indexOf('--debug'), 1);
common.setDebug(true);
}
if (process.argv.includes('--local')) {
process.argv.splice(process.argv.indexOf('--local'), 1);
common.setLocal(true);
}
// Get url from parameters if possible
if (process.argv.length > 2) {
repoUrl = process.argv[2];
if (!repoUrl.toLowerCase().includes('github.com')) {
repoUrl = `https://github.com/${repoUrl}`;
}
} else {
console.log('ERROR: No repository specified');
process.exit(1);
}
// Get branch from parameters if possible
if (process.argv.length > 3) {
repoBranch = process.argv[3];
}
console.log(`Checking repository ${repoUrl} (branch ${repoBranch})`);
check(
{
queryStringParameters: {
url: repoUrl,
branch: repoBranch,
},
},
null,
(err, data) => {
const context = JSON.parse(data.body);
console.log(context.result);
console.log('\n\n########## SUMMARY ##########');
if (context.errors.length) {
console.log('\n\nErrors:');
context.errors.sort().forEach((err) => {
const issue = err.substring(1, 5);
console.error(err);
if (issues[issue]) {
//if (issues[issue].title) {
// console.error(getText(issues[issue].title, 'en'));
//}
if (issues[issue].explanation) {
console.error(getText(issues[issue].explanation, 'en'));
}
if (issues[issue].resolving) {
console.error(getText(issues[issue].resolving, 'en'));
}
if (issues[issue].notes) {
console.error(getText(issues[issue].notes, 'en'));
}
}
});
} else {
console.log('\n\nNO errors encountered.');
}
if (context.warnings.length) {
console.log('\nWarnings:');
context.warnings.sort().forEach((err) => {
const issue = err.substring(1, 5);
console.warn(err);
if (issues[issue]) {
//if (issues[issue].title) {
// console.warn(getText(issues[issue].title, 'en'));
//}
if (issues[issue].explanation) {
console.warn(getText(issues[issue].explanation, 'en'));
}
if (issues[issue].resolving) {
console.warn(getText(issues[issue].resolving, 'en'));
}
if (issues[issue].notes) {
console.warn(getText(issues[issue].notes, 'en'));
}
}
});
} else {
console.log('\n\nNO warnings encountered.');
}
console.log(`\ncreated by repochecker ${context.version} based on commit ${context.lastCommitSha}`);
},
);
}