Skip to content

Commit dd037f8

Browse files
Use promise-based version of W3C API library (#238)
in places where we already use promises, at least
1 parent 67ca3e7 commit dd037f8

File tree

3 files changed

+46
-67
lines changed

3 files changed

+46
-67
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"express": "4.15.3",
5858
"express-session": "1.15.3",
5959
"express-winston": "2.4.0",
60-
"node-w3capi": "1.10.1",
60+
"node-w3capi": "^1.11.0",
6161
"nodemailer": "6.4.16",
6262
"nodemailer-mock-transport": "1.3.0",
6363
"object-assign": "4.1.1",

pr-check.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ let store, log;
1515
async function findW3CUserFromGithub(user) {
1616
log.info("Looking for github user with id " + user.ghID + " in W3C API");
1717
try {
18-
// can't easily promisify w3c API :(
19-
let w3cuser = await new Promise((res, rej) => w3c.user({type: 'github', id: user.ghID}).fetch((err, u) => { if (err) return rej(err); return res(u);}));
18+
let w3cuser = await w3c.user({type: 'github', id: user.ghID}).fetch();
2019
log.info(JSON.stringify(w3cuser, null, 2));
2120
await doAsync(store).mergeOnUser(user.username, {
2221
w3cid: w3cuser.id,
@@ -180,14 +179,11 @@ function prChecker(config, argLog, argStore, GH, mailer) {
180179

181180
let groups = [];
182181

183-
for (g of repoGroups) {
182+
for (let g of repoGroups) {
184183
// get group type and shortname
185184
try {
186-
await new Promise((res, rej) => w3c.group(g).fetch((err, group) => {
187-
if (err) return rej(err);
188-
groups.push({id: g, type: types[group.type], shortname: group.shortname});
189-
res();
190-
}));
185+
const group = await w3c.group(g).fetch();
186+
groups.push({id: g, type: types[group.type], shortname: group.shortname});
191187
} catch (err) {
192188
return cb(err);
193189
}
@@ -208,7 +204,7 @@ function prChecker(config, argLog, argStore, GH, mailer) {
208204
if (pr.repoId) {
209205
let nplc;
210206
try {
211-
nplc = await new Promise((res, rej) => w3c.nplc({repoId: pr.repoId, pr: pr.num}).fetch((err, n) => { if (err) return rej(err); return res(n);}));
207+
nplc = await w3c.nplc({repoId: pr.repoId, pr: pr.num}).fetch();
212208
} catch (err) {
213209
// Non-participant licensing contribution doesn't exist
214210
pr.contribStatus[username] = "not in group";

w3c-ipr.js

+40-57
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,45 @@
1+
const doAsync = require('doasync'); // rather than utils.promisy to get "free" support for object methods
2+
13
const fromUrlToId = url => url.replace(/.*\//, "");
24

35
module.exports = async function iprcheck(w3c, w3cprofileid, name, w3cgroups, store, cb) {
4-
return Promise.all(
5-
w3cgroups.map(
6-
({id, type, shortname}) => new Promise((res, rej) => {
7-
store.getGroup(id, function(err, group) {
8-
if (err) return rej(err);
9-
w3c.user(w3cprofileid)
10-
.participations()
11-
.fetch({embed: true},
12-
function(err, participations) {
13-
if (err) return rej(err);
14-
for (var i = 0 ; i < participations.length; i++) {
15-
var p = participations[i];
16-
var org = p._links.organization;
17-
var affiliation = p.individual ? {id: w3cprofileid, name: name} : {id: fromUrlToId(org.href), name: org.title};
18-
if (p._links.group.href === `https://api.w3.org/groups/${type}/${shortname}`) {
19-
return res({ok: true, affiliation: affiliation});
20-
}
21-
}
22-
// If we reach here,
23-
// the user is not participating directly in the group
24-
// For non WGs, game over
25-
if (group.groupType != "WG") {
26-
return res({ok: false});
27-
}
28-
// For WGs, we check if the user is affiliated
29-
// with an organization that is participating
30-
w3c.group({type, shortname})
31-
.participations()
32-
.fetch({embed: true}, function(err, participations) {
33-
if (err) return rej(err);
34-
var orgids = participations
35-
.filter(p => !p.individual)
36-
.map(p => fromUrlToId(p._links.organization.href));
37-
w3c.user(w3cprofileid)
38-
.affiliations()
39-
.fetch(function(err, affiliations) {
40-
if (err) return rej(err);
41-
var affids = affiliations.map(a => a ? fromUrlToId(a.href) : undefined);
42-
var intersection = orgids.filter(id => affids.includes(id));
43-
if (intersection.length) {
44-
var affiliationId = intersection[0];
45-
var affiliationName = affiliations.find(a => a.href == "https://api.w3.org/affiliations/" + affiliationId).title;
46-
return res({ok: true, affiliation: {id: affiliationId, name: affiliationName}});
47-
}
48-
return res({ok: false});
49-
});
50-
});
51-
});
52-
})
53-
})))
54-
.then(results => {
55-
for(let res of results) {
56-
if (res.ok) {
57-
return {affiliation: res.affiliation, ok: true};
58-
}
6+
for (let {id, type, shortname} of w3cgroups) {
7+
const group = await doAsync(store).getGroup(id);
8+
const participations = await w3c.user(w3cprofileid)
9+
.participations()
10+
.fetch({embed: true});
11+
if (!participations) return {affiliation: null, ok: false};
12+
for (let p of participations) {
13+
const org = p._links.organization;
14+
const affiliation = p.individual ? {id: w3cprofileid, name: name} : {id: fromUrlToId(org.href), name: org.title};
15+
if (p._links.group.href === `https://api.w3.org/groups/${type}/${shortname}`) {
16+
return {ok: true, affiliation: affiliation};
5917
}
60-
return {affiliation: null, ok: false};
61-
});
18+
}
19+
// If we reach here,
20+
// the user is not participating directly in the group
21+
// For non WGs, game over
22+
if (group.groupType != "WG") {
23+
continue;
24+
}
25+
// For WGs, we check if the user is affiliated
26+
// with an organization that is participating
27+
const orgParticipations = await w3c.group({type, shortname})
28+
.participations()
29+
.fetch({embed: true});
30+
const orgids = orgParticipations
31+
.filter(p => !p.individual)
32+
.map(p => fromUrlToId(p._links.organization.href));
33+
const affiliations = await w3c.user(w3cprofileid)
34+
.affiliations()
35+
.fetch();
36+
const affids = affiliations.map(a => a ? fromUrlToId(a.href) : undefined);
37+
const intersection = orgids.filter(id => affids.includes(id));
38+
if (intersection.length) {
39+
const affiliationId = intersection[0];
40+
const affiliationName = affiliations.find(a => a.href == "https://api.w3.org/affiliations/" + affiliationId).title;
41+
return {ok: true, affiliation: {id: affiliationId, name: affiliationName}};
42+
}
43+
}
44+
return {affiliation: null, ok: false};
6245
};

0 commit comments

Comments
 (0)