Skip to content

Commit e75ae6a

Browse files
committed
fix: prevent duplicate and self-refs
1 parent fd566e2 commit e75ae6a

6 files changed

+92
-18
lines changed

lib/links.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ class LinkParser {
3232
}
3333

3434
getRefsUrlsFromArray(arr) {
35-
const result = [];
35+
const result = new Set();
3636
for (const item of arr) {
3737
const m = item.match(REF_RE);
3838
if (!m) continue;
3939
const ref = m[1];
4040
const url = this.getUrlFromOP(ref);
41-
if (url) result.push(url);
41+
if (url) result.add(url);
4242
}
43-
return result;
43+
return Array.from(result);
4444
}
4545

4646
getPRUrlsFromArray(arr) {
47-
const result = [];
47+
const result = new Set();
4848
for (const item of arr) {
4949
const m = item.match(PR_RE);
5050
if (!m) continue;
5151
const prUrl = m[1];
5252
const url = this.getUrlFromOP(prUrl);
53-
if (url) result.push(url);
53+
if (url) result.add(url);
5454
}
55-
return result;
55+
return Array.from(result);
5656
}
5757

5858
// Do this so we can reliably get the correct url.

lib/metadata_gen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MetadataGenerator {
3131

3232
const parser = new LinkParser(owner, repo, op);
3333
const fixes = parser.getFixes();
34-
const refs = parser.getRefs();
34+
const refs = parser.getRefs().filter(f => f !== prUrl);
3535
const altPrUrl = parser.getAltPrUrl();
3636
const meta = [
3737
...fixes.map((fix) => `Fixes: ${fix}`),

test/fixtures/data.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ const firstTimerPrivatePR = readJSON('first_timer_pr_with_private_email.json');
7878
const semverMajorPR = readJSON('semver_major_pr.json');
7979
const fixAndRefPR = readJSON('pr_with_fixes_and_refs.json');
8080
const fixCrossPR = readJSON('pr_with_fixes_cross.json');
81+
const duplicateRefPR = readJSON('pr_with_duplicate_refs.json');
82+
const selfRefPR = readJSON('pr_with_self_ref.json');
8183
const backportPR = readJSON('pr_with_backport.json');
8284
const conflictingPR = readJSON('conflicting_pr.json');
8385
const emptyProfilePR = readJSON('empty_profile_pr.json');
@@ -139,5 +141,7 @@ module.exports = {
139141
readmeNoCollaboratorE,
140142
readmeUnordered,
141143
closedPR,
142-
mergedPR
144+
mergedPR,
145+
selfRefPR,
146+
duplicateRefPR
143147
};
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"createdAt": "2017-10-24T11:13:43Z",
3+
"url": "https://github.com/nodejs/node/pull/16438",
4+
"bodyHTML":
5+
"<p>Refs: <a href=\"https://github.com/nodejs/node/pull/16439\" class=\"issue-link js-issue-link\" data-error-text=\"Failed to load issue title\" data-id=\"271243248\" data-permission-text=\"Issue title is private\" data-url=\"https://github.com/nodejs/node/pull/16439\">#16439</a>\nRefs: <a href=\"https://github.com/nodejs/node/pull/16439\" class=\"issue-link js-issue-link\" data-error-text=\"Failed to load issue title\" data-id=\"271243248\" data-permission-text=\"Issue title is private\" data-url=\"https://github.com/nodejs/node/pull/16439\">#16439</a></p>",
6+
"bodyText": "Refs: https://github.com/nodejs/node/pull/16439\nRefs: https://github.com/nodejs/node/pull/16439",
7+
"labels": {
8+
"nodes": [
9+
{
10+
"name": "build"
11+
},
12+
{
13+
"name": "npm"
14+
},
15+
{
16+
"name": "python"
17+
}
18+
]
19+
}
20+
}

test/fixtures/pr_with_self_ref.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"createdAt": "2017-10-24T11:13:43Z",
3+
"url": "https://github.com/nodejs/node/pull/16438",
4+
"bodyHTML":
5+
"<p>Refs: <a href=\"https://github.com/nodejs/node/pull/16438\" class=\"issue-link js-issue-link\" data-error-text=\"Failed to load issue title\" data-id=\"271243248\" data-permission-text=\"Issue title is private\" data-url=\"https://github.com/nodejs/node/pull/16438\">#16438</a></p>",
6+
"bodyText": "Refs: https://github.com/nodejs/node/pull/16438",
7+
"labels": {
8+
"nodes": [
9+
{
10+
"name": "build"
11+
},
12+
{
13+
"name": "npm"
14+
},
15+
{
16+
"name": "python"
17+
}
18+
]
19+
}
20+
}

test/unit/metadata_gen.test.js

+40-10
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
const MetadataGenerator = require('../../lib/metadata_gen');
44
const {
55
fixAndRefPR,
6+
selfRefPR,
7+
duplicateRefPR,
68
fixCrossPR,
79
backportPR,
810
allGreenReviewers
911
} = require('../fixtures/data');
1012

1113
const assert = require('assert');
14+
1215
const data = {
1316
owner: 'nodejs',
1417
repo: 'node',
1518
pr: fixAndRefPR,
1619
reviewers: allGreenReviewers
1720
};
18-
const crossData = Object.assign({}, data, { pr: fixCrossPR });
21+
const expected = `PR-URL: https://github.com/nodejs/node/pull/16438
22+
Fixes: https://github.com/nodejs/node/issues/16437
23+
Refs: https://github.com/nodejs/node/pull/15148
24+
Reviewed-By: Foo User <[email protected]>
25+
Reviewed-By: Quux User <[email protected]>
26+
Reviewed-By: Baz User <[email protected]>
27+
Reviewed-By: Bar User <[email protected]>
28+
`;
29+
1930
const backportArgv = {
2031
argv: {
2132
owner: 'nodejs',
@@ -29,36 +40,55 @@ const backportArgv = {
2940
backport: true
3041
}
3142
};
32-
3343
const backportData = Object.assign({}, data, { pr: backportPR }, backportArgv);
44+
const backportExpected = `PR-URL: https://github.com/nodejs/node/pull/29995
45+
Backport-PR-URL: https://github.com/nodejs/node/pull/30072
46+
Fixes: https://github.com/nodejs/build/issues/1961
47+
Refs: https://github.com/nodejs/node/commit/53ca0b9ae145c430842bf78e553e3b6cbd2823aa#commitcomment-35494896
48+
`;
3449

35-
const expected = `PR-URL: https://github.com/nodejs/node/pull/16438
36-
Fixes: https://github.com/nodejs/node/issues/16437
37-
Refs: https://github.com/nodejs/node/pull/15148
50+
const selfRefData = Object.assign({}, data, { pr: selfRefPR });
51+
const selfRefExpected = `PR-URL: https://github.com/nodejs/node/pull/16438
3852
Reviewed-By: Foo User <[email protected]>
3953
Reviewed-By: Quux User <[email protected]>
4054
Reviewed-By: Baz User <[email protected]>
4155
Reviewed-By: Bar User <[email protected]>
4256
`;
57+
58+
const duplicateRefData = Object.assign({}, data, { pr: duplicateRefPR });
59+
const duplicateRefExpected = `PR-URL: https://github.com/nodejs/node/pull/16438
60+
Refs: https://github.com/nodejs/node/pull/16439
61+
Reviewed-By: Foo User <[email protected]>
62+
Reviewed-By: Quux User <[email protected]>
63+
Reviewed-By: Baz User <[email protected]>
64+
Reviewed-By: Bar User <[email protected]>
65+
`;
66+
67+
const crossData = Object.assign({}, data, { pr: fixCrossPR });
4368
const crossExpected = `PR-URL: https://github.com/nodejs/node/pull/16438
4469
Fixes: https://github.com/joyeecheung/node-core-utils/issues/123
4570
Reviewed-By: Foo User <[email protected]>
4671
Reviewed-By: Quux User <[email protected]>
4772
Reviewed-By: Baz User <[email protected]>
4873
Reviewed-By: Bar User <[email protected]>
4974
`;
50-
const backportExpected = `PR-URL: https://github.com/nodejs/node/pull/29995
51-
Backport-PR-URL: https://github.com/nodejs/node/pull/30072
52-
Fixes: https://github.com/nodejs/build/issues/1961
53-
Refs: https://github.com/nodejs/node/commit/53ca0b9ae145c430842bf78e553e3b6cbd2823aa#commitcomment-35494896
54-
`;
5575

5676
describe('MetadataGenerator', () => {
5777
it('should generate metadata properly', () => {
5878
const results = new MetadataGenerator(data).getMetadata();
5979
assert.strictEqual(expected, results);
6080
});
6181

82+
it('should prevent duplicate references', () => {
83+
const results = new MetadataGenerator(duplicateRefData).getMetadata();
84+
assert.strictEqual(duplicateRefExpected, results);
85+
});
86+
87+
it('should prevent self-referential references', () => {
88+
const results = new MetadataGenerator(selfRefData).getMetadata();
89+
assert.strictEqual(selfRefExpected, results);
90+
});
91+
6292
it('should handle cross-owner and cross-repo fixes properly', () => {
6393
const results = new MetadataGenerator(crossData).getMetadata();
6494
assert.strictEqual(crossExpected, results);

0 commit comments

Comments
 (0)