Skip to content

Commit fd443e5

Browse files
authored
Extract lookup of repo/CG/WG data from APIs to validate.js (#64)
This allows more of the code to be tested, and is a more reasonable interface, as `validateRepo` doesn't need information related to all repositories.
1 parent 577decf commit fd443e5

File tree

5 files changed

+336
-182
lines changed

5 files changed

+336
-182
lines changed

lib/validator.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const mdMatch = (md, ref) => nlToSpace(httpToHttps(md.toLowerCase())).indexOf(nl
3939

4040
const fullName = r => r.owner.login + '/' + r.name;
4141

42-
function validateRepo(r, licenses, repoData, cgData, repoMap) {
42+
// Also potentially sets `r.prpreview` and `r.w3c`.
43+
function validateRepo(r, data, licenses) {
4344
const {contributing, contributingSw, license, licenseSw} = licenses;
4445

4546
const errors = [];
@@ -70,20 +71,12 @@ function validateRepo(r, licenses, repoData, cgData, repoMap) {
7071
let shouldBeRepoManaged = false;
7172
const hasRecTrack = {ashnazg: null, repotype: null, tr: null}; // TODO detect conflicting information (repo-type vs ash-nazg vs TR doc)
7273

73-
// is the repo associated with a CG in the CG monitor?
74-
const cg = cgData.data.find(cg => {
75-
return cg.repositories.includes('https://github.com/' + fullName(r)) ||
76-
cg.repositories.includes('https://github.com/' + fullName(r) + '/');
77-
});
78-
79-
// is the repo associated with a WG in the spec dashboard?
80-
const wgRepo = repoMap[fullName(r)];
81-
82-
if (wgRepo) {
83-
hasRecTrack.tr = wgRepo.some(x => x.recTrack);
74+
const {specs} = data;
75+
if (specs && specs.length) {
76+
hasRecTrack.tr = specs.some(s => s.recTrack);
8477
}
8578

86-
const ashRepo = repoData.find(x => x.owner.toLowerCase() === r.owner.login.toLowerCase() && x.name.toLowerCase() === r.name.toLowerCase());
79+
const {ashRepo} = data;
8780
if (ashRepo) {
8881
hasRecTrack.ashnazg = ashRepo.groups.some(g => g.groupType === "WG");
8982
}
@@ -123,6 +116,7 @@ function validateRepo(r, licenses, repoData, cgData, repoMap) {
123116
if (!conf.group && ["rec-track", "note", "cg-report"].includes(conf["repo-type"])) {
124117
reportError('incompletew3cjson', {error: "group"});
125118
} else {
119+
// Note that `data.groups` is unused here.
126120
groups = arrayify(conf.group).map(id => parseInt(id, 10));
127121
shouldBeRepoManaged = conf["repo-type"] && (conf["repo-type"] === 'rec-track' || conf["repo-type"] === 'cg-report');
128122
}
@@ -134,17 +128,7 @@ function validateRepo(r, licenses, repoData, cgData, repoMap) {
134128
}
135129
}
136130
} else {
137-
if (cg) {
138-
groups = [cg.id];
139-
} else if (r.owner.login === 'WICG') {
140-
groups = [80485];
141-
}
142-
if (wgRepo && wgRepo.length) {
143-
groups = groups.concat(wgRepo.map(x => x.group));
144-
}
145-
if (r.owner.login === 'WebAudio') {
146-
groups.push(46884);
147-
}
131+
groups = data.groups;
148132
reportError('now3cjson');
149133
}
150134
const recTrackStatus = hasRecTrack.tr || hasRecTrack.ashnazg || hasRecTrack.repo;
@@ -177,7 +161,6 @@ function validateRepo(r, licenses, repoData, cgData, repoMap) {
177161

178162
return {
179163
errors, groups,
180-
isAshRepo: !!ashRepo,
181164
hasRecTrack: !!recTrackStatus,
182165
};
183166
}

lib/w3cData.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-env node */
2+
3+
// Additional W3C data about a repo that isn't from the GitHub repo/API.
4+
5+
"use strict";
6+
7+
const fetch = require("node-fetch");
8+
const w3c = require("node-w3capi");
9+
10+
const config = require("../config.json");
11+
w3c.apiKey = config.w3capikey;
12+
13+
async function data() {
14+
const [ashRepos, cgData, repoMap, w3cgroups] = await Promise.all([
15+
fetch("https://labs.w3.org/hatchery/repo-manager/api/repos").then(r => r.json()),
16+
fetch("https://w3c.github.io/cg-monitor/report.json").then(r => r.json()),
17+
fetch("https://w3c.github.io/spec-dashboard/repo-map.json").then(r => r.json()),
18+
// https://github.com/w3c/node-w3capi/issues/41
19+
new Promise((resolve, reject) => {
20+
w3c.groups().fetch({embed: true}, (err, w3cgroups) => {
21+
if (err) {
22+
reject(err);
23+
} else {
24+
resolve(w3cgroups);
25+
}
26+
});
27+
})
28+
]);
29+
30+
function get(owner, name) {
31+
const fullName = `${owner}/${name}`;
32+
const ashRepo = ashRepos.find(x => {
33+
return x.owner.toLowerCase() === owner.toLowerCase() &&
34+
x.name.toLowerCase() === name.toLowerCase();
35+
}) || null;
36+
const cg = cgData.data.find(cg => {
37+
return cg.repositories.includes(`https://github.com/${fullName}`) ||
38+
cg.repositories.includes(`https://github.com/${fullName}/`);
39+
});
40+
const specs = repoMap[fullName] || [];
41+
42+
const groups = [];
43+
if (cg) {
44+
groups.push(cg.id);
45+
} else if (owner === 'WICG') {
46+
groups.push(80485);
47+
}
48+
for (const spec of specs) {
49+
groups.push(spec.group);
50+
}
51+
if (owner === 'WebAudio') {
52+
groups.push(46884);
53+
}
54+
55+
return {ashRepo, specs, groups};
56+
}
57+
58+
return {get, w3cgroups};
59+
}
60+
61+
module.exports = data;

0 commit comments

Comments
 (0)