-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvalidator.js
231 lines (205 loc) · 8.36 KB
/
validator.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
/* eslint-env node */
"use strict";
// for some repos, having the w3c.json administrative file is felt as awkward
// we hard-code their equivalent here
const hardcodedRepoData = {
'w3c/markup-validator': {
'contacts': 'sideshowbarker',
'repo-type': 'tool'
},
'w3c/css-validator': {
'contacts': 'ylafon',
'repo-type': 'tool'
},
'w3c/respec': {
'contacts': 'marcoscaceres',
'repo-type': 'tool'
},
'w3c/respec-hljs': {
'contacts': 'marcoscaceres',
'repo-type': 'tool'
},
'w3c/webidl2.js': {
'contacts': 'marcoscaceres',
'repo-type': 'tool'
},
}
const hardcodedNotPermissiveLicensesGroups = [
83726, 34314, 87227, 35422, 76043
];
const hasIntersection = (a1, a2) => a1.find(x => a2.includes(x));
// extract from https://w3c.github.io/w3c.json.html with [...document.querySelectorAll('#repo-type + dd .value')].map(n => n.textContent)
const validRepoTypes = ['rec-track', 'note', 'cg-report', 'process', 'homepage', 'article', 'tool', 'project', 'others', 'workshop', 'tests', 'translation'];
const arrayify = x => Array.isArray(x) ? x : [x];
const nlToSpace = str => str.replace(/\n/g, " ").replace(/ {2}/g, " ").trim();
const httpToHttps = str => str.replace(/http:\/\/www.w3.org\//g, "https://www.w3.org/");
const mdMatch = (md, ref) => nlToSpace(httpToHttps(md.toLowerCase())).indexOf(nlToSpace(ref.toLowerCase())) !== -1;
const fullName = r => r.owner.login + '/' + r.name;
// Also potentially sets `r.prpreview` and `r.w3c`.
function validateRepo(r, data, licenses) {
const {contributing, contributingSw, license, licenseSw} = licenses;
const errors = [];
function reportError(type, details = null) {
errors.push([type, details]);
}
if (!r.readme) {
reportError('noreadme');
}
if (!r.codeofconduct) {
reportError('nocodeofconduct');
}
let shouldBeRepoManaged = false;
const hasRecTrack = {ashnazg: null, repotype: null, tr: null}; // TODO detect conflicting information (repo-type vs ash-nazg vs TR doc)
const {specs} = data;
if (specs && specs.length) {
hasRecTrack.tr = specs.some(s => s.recTrack);
}
const {ashRepo} = data;
if (ashRepo) {
hasRecTrack.ashnazg = ashRepo.groups.some(g => g.groupType === "WG");
}
if (r.prpreviewjson) {
try {
r.prpreview = JSON.parse(r.prpreviewjson.text);
} catch (e) {
//reportError('illformedprpreviewcjson');
}
}
let conf = null;
if (r.w3cjson) {
try {
conf = JSON.parse(r.w3cjson.text);
} catch (e) {
reportError('illformedw3cjson');
}
} else if (hardcodedRepoData[fullName(r)]) {
conf = hardcodedRepoData[fullName(r)];
}
let groups = [];
if (conf) {
r.w3c = conf;
// TODO: replace with JSON schema?
if (!conf["repo-type"]) {
reportError('incompletew3cjson', {error: "repo-type" + (Object.values(hasRecTrack).every(x => x === null) ? " (unknown)" : (hasRecTrack.tr || hasRecTrack.ashnazg ? " (rec-track)" : " (not rec-track)"))});
} else {
conf["repo-type"] = arrayify(conf["repo-type"]);
hasRecTrack.repotype = conf["repo-type"].includes('rec-track');
const unknownTypes = conf['repo-type'].filter(t => !validRepoTypes.includes(t));
if (unknownTypes.length) {
reportError('invalidw3cjson', {error: "unknown types: " + JSON.stringify(unknownTypes)});
}
}
if (!conf.group && conf["repo-type"] && hasIntersection(["rec-track", "note", "cg-report"], conf["repo-type"])) {
reportError('incompletew3cjson', {error: "group"});
} else {
// Note that `data.groups` is unused here.
groups = arrayify(conf.group).map(id => parseInt(id, 10));
shouldBeRepoManaged = conf["repo-type"] && hasIntersection(['rec-track', 'cg-report'], conf["repo-type"]);
}
if (!conf.contacts) {
reportError('incompletew3cjson', {error: "contacts"});
} else {
if (arrayify(conf.contacts).some(x => typeof x !== "string")) {
reportError('invalidw3cjson', {error: "invalid contacts: " + JSON.stringify(conf.contacts)});
}
}
} else {
groups = data.groups;
reportError('now3cjson');
}
if (conf && conf["repo-type"] && hasIntersection(["rec-track", "note", "cg-report"], conf["repo-type"])) {
if (!r.license || !r.license.text) {
reportError('nolicense');
} else {
if (!mdMatch(r.license.text, license) && !mdMatch(r.license.text, licenseSw)) {
reportError('invalidlicense', {error: "doesn't match SW or DOC license", license: r.license.text});
} else if (!groups.find(g => hardcodedNotPermissiveLicensesGroups.includes(g)) && conf["repo-type"].includes("rec-track") && !mdMatch(r.license.text, licenseSw)) {
reportError('invalidlicense', {error: "doesn't match chartered SW license", license: r.license.text});
}
}
if (!r.contributing || !r.contributing.text) {
reportError('nocontributing');
} else {
if (!mdMatch(r.contributing.text, contributing) && !mdMatch(r.contributing.text, contributingSw)) {
reportError('invalidcontributing', {error: "doesn't match SW or DOC contributing", contributing: r.contributing.text});
}
}
}
if (conf && conf["repo-type"] && hasIntersection(["rec-track", "note"], conf["repo-type"])) {
if (!r.autoPublish || !r.autoPublish.text) {
reportError('noautopublish');
}
}
if (r.travis && r.travis.text) {
reportError('usetravisci');
}
let defaultBranch;
if (!r.defaultBranch || !r.defaultBranch.name) {
// This ought to only happen for an empty repository.
reportError('nodefaultbranch');
} else {
defaultBranch = r.defaultBranch.name;
if (defaultBranch === "master") {
reportError('defaultbranchismaster');
}
}
const recTrackStatus = hasRecTrack.tr || hasRecTrack.ashnazg || hasRecTrack.repo;
shouldBeRepoManaged = shouldBeRepoManaged || recTrackStatus;
if (shouldBeRepoManaged) {
if (!ashRepo) {
reportError('noashnazg');
} else {
const ashGroups = ashRepo.groups.map(g => parseInt(g.w3cid, 10));
if (ashGroups.filter(x => groups.includes(x)).length !== ashGroups.length) {
reportError('inconsistentgroups', {ashnazgroups: ashGroups, error: JSON.stringify(groups) + ' vs ' + JSON.stringify(ashGroups)});
}
}
if (defaultBranch) {
const rule = r.branchProtectionRules
? r.branchProtectionRules.nodes.find(rule => rule.pattern === defaultBranch)
: null;
if (!rule) {
reportError('unprotectedbranch', {error: `${defaultBranch} branch is not protected`});
} else {
if (!rule.isAdminEnforced) {
reportError('unprotectedbranchforadmin', {error: `${defaultBranch} branch protection is not admin enforced`});
}
if (!(rule.requiredApprovingReviewCount > 0)) {
reportError('norequiredreview', {error: `${defaultBranch} branch review is not required`});
}
}
}
}
if (hasRecTrack.tr !== null && hasRecTrack.repotype !== null && hasRecTrack.tr !== hasRecTrack.repotype) {
reportError('inconsistentstatus', {error: "TR document: " + hasRecTrack.tr + ", vs repo: " + hasRecTrack.repotype});
}
if (hasRecTrack.tr !== null && hasRecTrack.ashnazg !== null && hasRecTrack.tr !== hasRecTrack.ashnazg) {
reportError('inconsistentstatus', {error: "TR document: " + hasRecTrack.tr + ", vs repo manager: " + hasRecTrack.ashnazg});
}
if (hasRecTrack.repotype !== null && hasRecTrack.ashnazg !== null && hasRecTrack.repotype !== hasRecTrack.ashnazg) {
reportError('inconsistentstatus', {error: "repo: " + hasRecTrack.repotype + ", vs repo manager: " + hasRecTrack.ashnazg});
}
return {
errors, groups,
hasRecTrack: !!recTrackStatus,
};
}
const ashnazgHookUrls = [
"https://labs.w3.org/hatchery/repo-manager/api/hook",
"https://labs.w3.org/repo-manager/api/hook"
];
function validateAshHooks(hooks) {
// Note: unlike `reportError` used above, here empty object literals are
// pushed instead of nulls. This is simply to match the original structure
// of report.json. TODO: change this to null, or add error details.
const errors = [];
const ashHooks = hooks.filter(h => ashnazgHookUrls.includes(h.config.url) && h.config.content_type === "json" && h.config.insecure_ssl === "0" && h.config.secret !== "");
if (ashHooks.length === 0) {
errors.push(['missingashnazghook', {}]);
}
if (ashHooks.length > 1) {
errors.push(['duplicateashnazghooks', {}]);
}
return errors;
}
module.exports = {validateRepo, validateAshHooks};