Skip to content

Commit de1e8aa

Browse files
committed
rules: stricter PR-URL: check
Only allow GitHub pull request URLs.
1 parent 2523fb9 commit de1e8aa

File tree

2 files changed

+84
-25
lines changed

2 files changed

+84
-25
lines changed

lib/rules/pr-url.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const id = 'pr-url'
4+
const prUrl = /^https:\/\/github\.com\/\w+\/\w+\/pull\/\d+$/
45

56
module.exports = {
67
id: id
@@ -22,33 +23,42 @@ module.exports = {
2223
})
2324
return
2425
}
25-
if (context.prUrl) {
26-
if (context.prUrl[0] === '#') {
27-
// see nodejs/node#7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
28-
// for an example
29-
var line = -1
30-
var column = -1
31-
for (var i = 0; i < context.body.length; i++) {
32-
const l = context.body[i]
33-
if (~l.indexOf('PR-URL') && ~l.indexOf(context.prUrl)) {
34-
line = i
35-
column = l.indexOf(context.prUrl)
36-
}
37-
}
38-
context.report({
39-
id: id
40-
, message: 'PR-URL must be a url, not a pr number.'
41-
, string: context.prUrl
42-
, line: line
43-
, column: column
44-
, level: 'fail'
45-
})
26+
var line = -1
27+
var column = -1
28+
for (var i = 0; i < context.body.length; i++) {
29+
const l = context.body[i]
30+
if (~l.indexOf('PR-URL') && ~l.indexOf(context.prUrl)) {
31+
line = i
32+
column = l.indexOf(context.prUrl)
4633
}
34+
}
35+
if (context.prUrl[0] === '#') {
36+
// see nodejs/node#7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
37+
// for an example
38+
context.report({
39+
id: id
40+
, message: 'PR-URL must be a URL, not a pull request number.'
41+
, string: context.prUrl
42+
, line: line
43+
, column: column
44+
, level: 'fail'
45+
})
46+
} else if (!prUrl.test(context.prUrl)) {
47+
context.report({
48+
id: id
49+
, message: 'PR-URL must be a GitHub pull request URL.'
50+
, string: context.prUrl
51+
, line: line
52+
, column: column
53+
, level: 'fail'
54+
})
4755
} else {
4856
context.report({
4957
id: id
50-
, message: 'PR-URL is valid'
58+
, message: 'PR-URL is valid.'
5159
, string: context.prUrl
60+
, line: line
61+
, column: column
5262
, level: 'pass'
5363
})
5464
}

test/rules/pr-url.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
const test = require('tap').test
44
const Rule = require('../../lib/rules/pr-url')
55
const MISSING_PR_URL = 'Commit must have a PR-URL.'
6-
const INVALID_PR_URL = 'PR-URL must be a url, not a pr number.'
6+
const INVALID_PR_URL = 'PR-URL must be a GitHub pull request URL.'
7+
const NUMERIC_PR_URL = 'PR-URL must be a URL, not a pull request number.'
8+
const VALID_PR_URL = 'PR-URL is valid.'
79

810
test('rule: pr-url', (t) => {
911
t.test('missing', (tt) => {
@@ -24,7 +26,8 @@ test('rule: pr-url', (t) => {
2426
Rule.validate(context)
2527
})
2628

27-
t.test('invalid', (tt) => {
29+
30+
t.test('invalid numeric', (tt) => {
2831
tt.plan(7)
2932
const context = {
3033
prUrl: '#1234'
@@ -35,7 +38,7 @@ test('rule: pr-url', (t) => {
3538
, report: (opts) => {
3639
tt.pass('called report')
3740
tt.equal(opts.id, 'pr-url', 'id')
38-
tt.equal(opts.message, INVALID_PR_URL, 'message')
41+
tt.equal(opts.message, NUMERIC_PR_URL, 'message')
3942
tt.equal(opts.string, '#1234', 'string')
4043
tt.equal(opts.line, 1, 'line')
4144
tt.equal(opts.column, 8, 'column')
@@ -46,5 +49,51 @@ test('rule: pr-url', (t) => {
4649
Rule.validate(context)
4750
})
4851

52+
t.test('invalid', (tt) => {
53+
tt.plan(7)
54+
const url = 'https://github.com/nodejs/node/issues/1234'
55+
const context = {
56+
prUrl: url
57+
, body: [
58+
''
59+
, `PR-URL: ${url}`
60+
]
61+
, report: (opts) => {
62+
tt.pass('called report')
63+
tt.equal(opts.id, 'pr-url', 'id')
64+
tt.equal(opts.message, INVALID_PR_URL, 'message')
65+
tt.equal(opts.string, url, 'string')
66+
tt.equal(opts.line, 1, 'line')
67+
tt.equal(opts.column, 8, 'column')
68+
tt.equal(opts.level, 'fail', 'level')
69+
}
70+
}
71+
72+
Rule.validate(context)
73+
})
74+
75+
t.test('valid', (tt) => {
76+
tt.plan(7)
77+
const url = 'https://github.com/nodejs/node/pull/1234'
78+
const context = {
79+
prUrl: url
80+
, body: [
81+
''
82+
, `PR-URL: ${url}`
83+
]
84+
, report: (opts) => {
85+
tt.pass('called report')
86+
tt.equal(opts.id, 'pr-url', 'id')
87+
tt.equal(opts.message, VALID_PR_URL, 'message')
88+
tt.equal(opts.string, url, 'string')
89+
tt.equal(opts.line, 1, 'line')
90+
tt.equal(opts.column, 8, 'column')
91+
tt.equal(opts.level, 'pass', 'level')
92+
}
93+
}
94+
95+
Rule.validate(context)
96+
})
97+
4998
t.end()
5099
})

0 commit comments

Comments
 (0)