-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathfixes-url.js
96 lines (93 loc) · 2.38 KB
/
fixes-url.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
const id = 'fixes-url'
const github = new RegExp('^https://github\\.com/[\\w-]+/[\\w-]+/' +
'(issues|pull)/\\d+(#issuecomment-\\d+|#discussion_r\\d+)?/?$'
)
export default {
id,
meta: {
description: 'enforce format of Fixes URLs',
recommended: true
},
defaults: {},
options: {},
validate: (context, rule) => {
const parsed = context.toJSON()
if (!Array.isArray(parsed.fixes) || !parsed.fixes.length) {
context.report({
id,
message: 'skipping fixes-url',
string: '',
level: 'skip'
})
return
}
// Allow GitHub issues with optional comment.
// GitHub pull requests must reference a comment or discussion.
for (const url of parsed.fixes) {
const match = github.exec(url)
if (url[0] === '#') {
// See nodejs/node#2aa376914b621018c5784104b82c13e78ee51307
// for an example
const { line, column } = findLineAndColumn(context.body, url)
context.report({
id,
message: 'Fixes must be a URL, not an issue number.',
string: url,
line,
column,
level: 'fail'
})
} else if (match) {
if (match[1] === 'pull' && match[2] === undefined) {
const { line, column } = findLineAndColumn(context.body, url)
context.report({
id,
message: 'Pull request URL must reference a comment or discussion.',
string: url,
line,
column,
level: 'fail'
})
} else {
const { line, column } = findLineAndColumn(context.body, url)
context.report({
id,
message: 'Valid fixes URL.',
string: url,
line,
column,
level: 'pass'
})
}
} else {
const { line, column } = findLineAndColumn(context.body, url)
context.report({
id,
message: 'Fixes must be a GitHub URL.',
string: url,
line,
column,
level: 'fail'
})
}
}
}
}
function findLineAndColumn (body, str) {
for (let i = 0; i < body.length; i++) {
const l = body[i]
if (~l.indexOf('Fixes')) {
const idx = l.indexOf(str)
if (idx !== -1) {
return {
line: i,
column: idx
}
}
}
}
return {
line: -1,
column: -1
}
}