Skip to content

Commit b9b6608

Browse files
committed
Add tests cases and comments flag
1 parent f43df7e commit b9b6608

11 files changed

+63
-22
lines changed

lib/args.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function buildYargs(args = null) {
3737
describe: 'File to write the metadata in',
3838
type: 'string'
3939
})
40+
.option('comments', {
41+
alias: 'c',
42+
demandOption: false,
43+
describe: 'Check for\'LGTM\' in comments',
44+
type: 'boolean'
45+
})
4046
.help()
4147
.alias('help', 'h')
4248
.argv;
@@ -47,8 +53,8 @@ const PR_RE = new RegExp(
4753
'([0-9]+)(?:/(?:files)?)?$');
4854

4955
function checkAndParseArgs(args) {
50-
const { owner = 'nodejs', repo = 'node', identifier, file } = args;
51-
const result = { owner, repo, file };
56+
const { owner = 'nodejs', repo = 'node', identifier, file, comments } = args;
57+
const result = { owner, repo, file, comments };
5258
if (!isNaN(identifier)) {
5359
result.prid = +identifier;
5460
} else {

lib/pr_checker.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const WEEKDAY_WAIT = 48;
1111
const WEEKEND_WAIT = 72;
1212

1313
const {
14-
REVIEW_SOURCES: { FROM_COMMENT }
14+
REVIEW_SOURCES: { FROM_COMMENT, FROM_REVIEW_COMMENT }
1515
} = require('./reviews');
1616
const {
1717
FIRST_TIME_CONTRIBUTOR, FIRST_TIMER
@@ -45,9 +45,9 @@ class PRChecker {
4545
);
4646
}
4747

48-
checkAll() {
48+
checkAll(comments = false) {
4949
const status = [
50-
this.checkReviews(),
50+
this.checkReviews(comments),
5151
this.checkCommitsAfterReview(),
5252
this.checkPRWait(new Date()),
5353
this.checkCI()
@@ -75,7 +75,7 @@ class PRChecker {
7575
return hint;
7676
}
7777

78-
checkReviews() {
78+
checkReviews(comments = false) {
7979
const {
8080
pr, logger, reviewers: { rejected, approved }
8181
} = this;
@@ -98,10 +98,13 @@ class PRChecker {
9898
let hint = this.getTSCHint(approved);
9999
logger.info(`Approvals: ${approved.length}${hint}`);
100100

101-
for (const { reviewer, review } of approved) {
102-
if (review.source === FROM_COMMENT) {
103-
logger.info(
104-
`${reviewer.getName()}) approved in via LGTM in comments`);
101+
if (comments) {
102+
for (const {reviewer, review} of approved) {
103+
if (review.source === FROM_COMMENT ||
104+
review.source === FROM_REVIEW_COMMENT) {
105+
logger.warn(
106+
`${reviewer.getName()}) approved in via LGTM in comments`);
107+
}
105108
}
106109
}
107110

lib/reviews.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ class ReviewAnalyzer {
5656
const map = new Map();
5757
const collaborators = this.collaborators;
5858
const list = this.reviews
59-
.filter((r) => r.state !== PENDING || this.isApprovedInComment(r))
59+
.filter((r) => r.state !== PENDING)
60+
.filter((r) => {
61+
if (r.state === COMMENTED) {
62+
return this.isApprovedInComment(r);
63+
} else {
64+
return true;
65+
}
66+
})
6067
.filter((r) => {
6168
return (isCollaborator(collaborators, r.author));
6269
}).sort((a, b) => {
@@ -84,7 +91,7 @@ class ReviewAnalyzer {
8491
case COMMENTED:
8592
map.set(
8693
login,
87-
new Review(r.state, r.publishedAt, r.bodyText, FROM_REVIEW_COMMENT)
94+
new Review(APPROVED, r.publishedAt, r.bodyText, FROM_REVIEW_COMMENT)
8895
);
8996
break;
9097
case DISMISSED:
@@ -104,7 +111,7 @@ class ReviewAnalyzer {
104111
updateMapByRawReviews(oldMap) {
105112
const comments = this.comments;
106113
const collaborators = this.collaborators;
107-
const withLgtm = comments.filter((c) => this.hasLGTM(c, 'bodyText'))
114+
const withLgtm = comments.filter((c) => this.hasLGTM(c))
108115
.filter((c) => {
109116
return (isCollaborator(collaborators, c.author));
110117
}).sort((a, b) => {
@@ -139,7 +146,7 @@ class ReviewAnalyzer {
139146
const collaborators = this.collaborators;
140147
for (const [ login, review ] of reviewers) {
141148
const reviewer = collaborators.get(login.toLowerCase());
142-
if (review.state === APPROVED || this.isApprovedInComment(review)) {
149+
if (review.state === APPROVED) {
143150
result.approved.push({reviewer, review});
144151
} else if (review.state === CHANGES_REQUESTED) {
145152
result.rejected.push({ reviewer, review });
@@ -153,16 +160,16 @@ class ReviewAnalyzer {
153160
* @returns {boolean}
154161
*/
155162
isApprovedInComment(review) {
156-
return review.state === COMMENTED && this.hasLGTM(review, 'ref');
163+
return review.state === COMMENTED && this.hasLGTM(review);
157164
}
158165

159166
/**
160167
* @param object
161168
* @param prop: string
162169
* @returns {boolean}
163170
*/
164-
hasLGTM(object, prop) {
165-
return LGTM_RE.test(object[prop].trim());
171+
hasLGTM(object) {
172+
return LGTM_RE.test(object.bodyText.trim());
166173
}
167174
}
168175

steps/metadata.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = async function getMetadata(argv, logger) {
3434
}, 'Generated metadata:');
3535

3636
const checker = new PRChecker(logger, data);
37-
const status = checker.checkAll();
37+
const status = checker.checkAll(argv.comments);
3838
return {
3939
status,
4040
request,

test/fixtures/README/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ For more information about the governance of the Node.js project, see
252252
**Foo User** <[email protected]> (she/her)
253253
* [Quo](https://github.com/quo) -
254254
**Quo User** <[email protected]> (she/her)
255+
* [Quux](https://github.com/quux) -
256+
**Quux User** <[email protected]> (he/him)
255257

256258
### Collaborator Emeriti
257259

test/fixtures/collaborators.json

+6
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@
2222
"name": "Quo User",
2323
"email": "[email protected]",
2424
"type": "COLLABORATOR"
25+
},
26+
{
27+
"login": "Quux",
28+
"name": "Quux User",
29+
"email": "[email protected]",
30+
"type": "COLLABORATOR"
2531
}
2632
]

test/fixtures/reviewers_approved.json

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
"source": "review"
1414
}
1515
},
16+
{
17+
"reviewer": {
18+
"login": "Quux",
19+
"name": "Quux User",
20+
"email": "[email protected]",
21+
"type": "COLLABORATOR"
22+
},
23+
"review": {
24+
"state": "APPROVED",
25+
"date": "2017-10-24T14:49:52Z",
26+
"ref": "LGTM",
27+
"source": "review_comment"
28+
}
29+
},
1630
{
1731
"reviewer": {
1832
"login": "Baz",

test/fixtures/reviews_approved.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"bodyText": "LGTM",
4040
"state": "COMMENTED",
4141
"author": {
42-
"login": "Baz"
42+
"login": "Quux"
4343
},
4444
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71817236",
4545
"publishedAt": "2017-10-24T14:49:52Z"

test/unit/args.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const parseArgs = require('../../lib/args');
44
const assert = require('assert');
55

66
const expected = {
7+
comments: false,
78
owner: `nodejs`,
89
repo: `node`,
910
prid: 16637,

test/unit/metadata_gen.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const expected = `PR-URL: https://github.com/nodejs/node/pull/16438
1919
Fixes: https://github.com/nodejs/node/issues/16437
2020
Refs: https://github.com/nodejs/node/pull/15148
2121
Reviewed-By: Foo User <[email protected]>
22+
Reviewed-By: Quux User <[email protected]>
2223
Reviewed-By: Baz User <[email protected]>
2324
Reviewed-By: Bar User <[email protected]>
2425
`;

test/unit/pr_checker.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ describe('PRChecker', () => {
8080

8181
const expectedLogs = {
8282
warn: [
83+
['Quux User(Quux)) approved in via LGTM in comments'],
84+
['Bar User(bar)) approved in via LGTM in comments'],
8385
['semver-major requires at least two TSC approvals']
8486
],
8587
info: [
8688
['Rejections: 0'],
87-
['Approvals: 3, 1 from TSC (bar)'],
88-
['Bar User(bar)) approved in via LGTM in comments']
89+
['Approvals: 4, 1 from TSC (bar)']
8990
],
9091
error: [],
9192
trace: []
@@ -100,7 +101,7 @@ describe('PRChecker', () => {
100101
collaborators
101102
});
102103

103-
const status = checker.checkReviews();
104+
const status = checker.checkReviews(true);
104105
assert(!status);
105106
assert.deepStrictEqual(logger.logs, expectedLogs);
106107
});

0 commit comments

Comments
 (0)