-
Notifications
You must be signed in to change notification settings - Fork 113
/
reviews.js
184 lines (173 loc) · 4.67 KB
/
reviews.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
'use strict';
const {
PENDING, COMMENTED, APPROVED, CHANGES_REQUESTED, DISMISSED
} = require('./review_state');
const { isCollaborator } = require('./collaborators');
const { ascending } = require('./utils');
const LGTM_RE = /^lgtm\W?$/i;
const FROM_REVIEW = 'review';
const FROM_COMMENT = 'comment';
const FROM_REVIEW_COMMENT = 'review_comment';
class Review {
/**
* @param {string} state
* @param {string} date // ISO date string
* @param {string} ref
* @param {string} source
*/
constructor(state, date, ref, source) {
this.state = state;
this.date = date;
this.ref = ref;
this.source = source;
}
}
/**
* @typedef {Object} GHReview
* @property {string} bodyText
* @property {string} state
* @property {{login: string}} author
* @property {string} url
* @property {string} publishedAt
*
* @typedef {Object} GHComment
* @property {string} bodyText
* @property {{login: string}} author
* @property {string} publishedAt
*
*/
class ReviewAnalyzer {
/**
* @param {PRData} data
*/
constructor(data) {
const { reviews, comments, collaborators } = data;
this.reviews = reviews;
this.comments = comments;
this.collaborators = collaborators;
}
/**
* @returns {Map<string, Review>}
*/
mapByGithubReviews() {
const map = new Map();
const collaborators = this.collaborators;
const list = this.reviews
.filter((r) => r.state !== PENDING)
.filter((r) => {
if (r.state === COMMENTED) {
return this.isApprovedInComment(r);
} else {
return true;
}
})
.filter((r) => {
return (isCollaborator(collaborators, r.author));
}).sort((a, b) => {
return ascending(a.publishedAt, b.publishedAt);
});
for (const r of list) {
const login = r.author.login.toLowerCase();
const entry = map.get(login);
if (!entry) { // initialize
map.set(
login,
new Review(r.state, r.publishedAt, r.url, FROM_REVIEW)
);
}
switch (r.state) {
case APPROVED:
case CHANGES_REQUESTED:
// Overwrite previous reviews, whether initalized or not
map.set(
login,
new Review(r.state, r.publishedAt, r.url, FROM_REVIEW)
);
break;
case COMMENTED:
map.set(
login,
new Review(APPROVED, r.publishedAt, r.bodyText, FROM_REVIEW_COMMENT)
);
break;
case DISMISSED:
// TODO: check the state of the dismissed review?
map.delete(login);
break;
}
}
return map;
}
// TODO: count -1 ...? But they should just make it explicit
/**
* @param {Map<string, Review>} oldMap
* @returns {Map<string, Review>}
*/
updateMapByRawReviews(oldMap) {
const comments = this.comments;
const collaborators = this.collaborators;
const withLgtm = comments.filter((c) => this.hasLGTM(c))
.filter((c) => {
return (isCollaborator(collaborators, c.author));
}).sort((a, b) => {
return ascending(a.publishedAt, b.publishedAt);
});
for (const c of withLgtm) {
const login = c.author.login.toLowerCase();
const entry = oldMap.get(login);
if (!entry || entry.date < c.publishedAt) {
oldMap.set(
login,
// no url, have to use bodyText for refs
new Review(APPROVED, c.publishedAt, c.bodyText, FROM_COMMENT)
);
}
}
return oldMap;
}
/**
* @typedef {{reviwewer: Collaborator, review: Review}[]} ReviewerList
* @returns {{approved: ReviewerList, requestedChanges: ReviewerList}}
*/
getReviewers() {
const ghReviews = this.mapByGithubReviews();
const reviewers = this.updateMapByRawReviews(ghReviews);
const result = {
approved: [],
requestedChanges: []
};
const collaborators = this.collaborators;
for (const [ login, review ] of reviewers) {
const reviewer = collaborators.get(login.toLowerCase());
if (review.state === APPROVED) {
result.approved.push({ reviewer, review });
} else if (review.state === CHANGES_REQUESTED) {
result.requestedChanges.push({ reviewer, review });
}
}
return result;
}
/**
* @param review
* @returns {boolean}
*/
isApprovedInComment(review) {
return review.state === COMMENTED && this.hasLGTM(review);
}
/**
* @param object
* @param prop: string
* @returns {boolean}
*/
hasLGTM(object) {
return LGTM_RE.test(object.bodyText.trim());
}
}
const REVIEW_SOURCES = {
FROM_COMMENT, FROM_REVIEW, FROM_REVIEW_COMMENT
};
module.exports = {
ReviewAnalyzer,
Review,
REVIEW_SOURCES
};