Skip to content

Commit 95300fd

Browse files
authored
fix: prevent duplicate and self-refs (#478)
1 parent de6d1e2 commit 95300fd

6 files changed

+93
-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
@@ -32,7 +32,7 @@ class MetadataGenerator {
3232

3333
const parser = new LinkParser(owner, repo, op);
3434
const fixes = parser.getFixes();
35-
const refs = parser.getRefs();
35+
const refs = parser.getRefs().filter(f => f !== prUrl);
3636
const altPrUrl = parser.getAltPrUrl();
3737

3838
const meta = [];

test/fixtures/data.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const firstTimerPrivatePR = readJSON('first_timer_pr_with_private_email.json');
7777
const semverMajorPR = readJSON('semver_major_pr.json');
7878
const fixAndRefPR = readJSON('pr_with_fixes_and_refs.json');
7979
const fixCrossPR = readJSON('pr_with_fixes_cross.json');
80+
const duplicateRefPR = readJSON('pr_with_duplicate_refs.json');
81+
const selfRefPR = readJSON('pr_with_self_ref.json');
8082
const backportPR = readJSON('pr_with_backport.json');
8183
const conflictingPR = readJSON('conflicting_pr.json');
8284
const emptyProfilePR = readJSON('empty_profile_pr.json');
@@ -137,5 +139,7 @@ module.exports = {
137139
readmeNoCollaboratorE,
138140
readmeUnordered,
139141
closedPR,
140-
mergedPR
142+
mergedPR,
143+
selfRefPR,
144+
duplicateRefPR
141145
};
+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

+41-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,29 +40,39 @@ 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
52+
Reviewed-By: Foo User <[email protected]>
53+
Reviewed-By: Quux User <[email protected]>
54+
Reviewed-By: Baz User <[email protected]>
55+
Reviewed-By: Bar User <[email protected]>
56+
`;
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
3861
Reviewed-By: Foo User <[email protected]>
3962
Reviewed-By: Quux User <[email protected]>
4063
Reviewed-By: Baz User <[email protected]>
4164
Reviewed-By: Bar User <[email protected]>
4265
`;
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-
`;
75+
5576
const skipRefsExpected = `PR-URL: https://github.com/nodejs/node/pull/16438
5677
Reviewed-By: Foo User <[email protected]>
5778
Reviewed-By: Quux User <[email protected]>
@@ -65,6 +86,16 @@ describe('MetadataGenerator', () => {
6586
assert.strictEqual(expected, results);
6687
});
6788

89+
it('should prevent duplicate references', () => {
90+
const results = new MetadataGenerator(duplicateRefData).getMetadata();
91+
assert.strictEqual(duplicateRefExpected, results);
92+
});
93+
94+
it('should prevent self-referential references', () => {
95+
const results = new MetadataGenerator(selfRefData).getMetadata();
96+
assert.strictEqual(selfRefExpected, results);
97+
});
98+
6899
it('should handle cross-owner and cross-repo fixes properly', () => {
69100
const results = new MetadataGenerator(crossData).getMetadata();
70101
assert.strictEqual(crossExpected, results);

0 commit comments

Comments
 (0)