forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaborators.js
More file actions
113 lines (95 loc) · 2.91 KB
/
Copy pathcollaborators.js
File metadata and controls
113 lines (95 loc) · 2.91 KB
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
'use strict';
const TSC_TITLE = '### TSC (Technical Steering Committee)';
const TSCE_TITLE = '### TSC Emeriti';
const CL_TITLE = '### Collaborators';
const CLE_TITLE = '### Collaborator Emeriti';
const CONTACT_RE = /\* +\[(.+?)\]\(.+?\) +-\s\*\*(.+?)\*\* +<(.+?)>/mg;
const TSC = 'TSC';
const COLLABORATOR = 'COLLABORATOR';
class Collaborator {
constructor(login, name, email, type) {
this.login = login; // This is not lowercased
this.name = name;
this.email = email;
this.type = type;
}
isActor(actor) {
if (!actor || !actor.login) { // ghost
return false;
}
return actor.login.toLowerCase() === this.login.toLowerCase();
}
isTSC() {
return this.type === TSC;
}
getName() {
return `${this.name}(${this.login})`;
}
getContact() {
return `${this.name} <${this.email}>`;
}
}
Collaborator.TYPES = {
TSC, COLLABORATOR
};
function getCollaborators(readme, cli, owner, repo) {
// This is more or less taken from
// https://github.com/rvagg/iojs-tools/blob/master/pr-metadata/pr-metadata.js
const members = new Map();
let m;
const tscIndex = readme.indexOf(TSC_TITLE);
const tsceIndex = readme.indexOf(TSCE_TITLE);
const clIndex = readme.indexOf(CL_TITLE);
const cleIndex = readme.indexOf(CLE_TITLE);
if (tscIndex === -1) {
throw new Error(`Couldn't find ${TSC_TITLE} in the README`);
}
if (tsceIndex === -1) {
throw new Error(`Couldn't find ${TSCE_TITLE} in the README`);
}
if (clIndex === -1) {
throw new Error(`Couldn't find ${CL_TITLE} in the README`);
}
if (cleIndex === -1) {
throw new Error(`Couldn't find ${CLE_TITLE} in the README`);
}
if (!(tscIndex < tsceIndex &&
tsceIndex < clIndex &&
clIndex < cleIndex)) {
cli.warn('Contacts in the README is out of order, ' +
'analysis could go wrong.', { newline: true });
}
// We also assume that TSC & TSC Emeriti are also listed as collaborators
CONTACT_RE.lastIndex = tscIndex;
// eslint-disable-next-line no-cond-assign
while ((m = CONTACT_RE.exec(readme)) && CONTACT_RE.lastIndex < tsceIndex) {
const login = m[1].toLowerCase();
members.set(login, new Collaborator(m[1], m[2], m[3], TSC));
}
CONTACT_RE.lastIndex = clIndex;
// eslint-disable-next-line no-cond-assign
while ((m = CONTACT_RE.exec(readme)) &&
CONTACT_RE.lastIndex < cleIndex) {
const login = m[1].toLowerCase();
if (!members.get(login)) {
members.set(login, new Collaborator(m[1], m[2], m[3], COLLABORATOR));
}
}
if (!members.size) {
throw new Error('Could not find any collaborators');
}
return members;
}
/**
* @param {Map<string, Collaborator>} collaborators
* @param {{login?: string}} user
*/
function isCollaborator(collaborators, user) {
return (user && user.login && // could be a ghost
collaborators.get(user.login.toLowerCase()));
}
module.exports = {
getCollaborators,
Collaborator,
isCollaborator
};