This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·128 lines (116 loc) · 2.68 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
/* eslint-disable no-console */
const Confirm = require('prompt-confirm')
const { CLIEngine } = require('eslint')
const GitHubApi = require('github')
const Promise = require('bluebird')
const {
__,
assoc,
filter,
isEmpty,
keys,
match,
none,
not,
omit,
path,
pick,
pipe,
propOr,
reduce,
splitEvery,
toPairs,
unnest,
} = require('ramda')
const getLineMapFromPatchString = require('./patch-line-mapper.js')
const USERNAME = process.argv[2]
const TOKEN = process.argv[3]
const PR = process.argv[4]
const OWNER = PR.match(/^([^/]+)\//)[1]
const REPO = PR.match(/\/([^#]+)/)[1]
const PR_NUMBER = PR.match(/#(\d+)/)[1]
const github = new GitHubApi({
Promise,
headers: {
'user-agent': 'mc-linter',
},
})
github.authenticate({
type: 'basic',
username: USERNAME,
password: TOKEN,
})
const linter = new CLIEngine()
const messageBlacklist = [
'Unable to resolve path to module',
'should be listed in the project\'s dependencies',
]
console.log(`Fetching PR #${PR_NUMBER}`)
const groupMessages = (result, message) => {
let newMessage = propOr('', message.line, result)
newMessage += message.message + '\n'
return assoc(message.line, newMessage, result)
}
const matchBlacklist = ({ message }) =>
none(
pipe(
match(__, message),
isEmpty,
not
),
messageBlacklist
)
github.pullRequests.getFiles({
owner: OWNER,
repo: REPO,
number: PR_NUMBER,
per_page: 100,
})
.then(response => response.data)
.filter(file => file.filename.match(/\.js$/))
.filter(file => file.status !== 'renamed')
.map((file) => {
const linesChanged = getLineMapFromPatchString(file.patch)
return github.repos.getContent({
owner: OWNER,
repo: REPO,
ref: file.blob_url.match(/blob\/([a-zA-Z0-9]+)\//)[1],
path: file.filename,
})
.then(response => Buffer.from(response.data.content, 'base64').toString())
.then(linter.executeOnText.bind(linter))
.then(path(['results', 0, 'messages']))
.then(filter(matchBlacklist))
.then(reduce(groupMessages, {}))
.then(pick(keys(linesChanged)))
.then(toPairs)
.map(([line, message]) => ({
path: file.filename,
line,
position: Number(linesChanged[line]),
body: message,
}))
})
.then(unnest)
.tap(console.log)
.map(omit('line'))
.tap(comments => console.log(`${comments.length} comments`))
.then(splitEvery(50))
.each(comments =>
new Confirm('Send comments?')
.run()
.then((answer) => {
if (!answer) return
return github.pullRequests.createReview({
owner: OWNER,
repo: REPO,
number: PR_NUMBER,
body: 'MC-linter',
event: 'COMMENT',
comments,
})
.tap(() => console.log('Comments sent!'))
})
)
.catch(console.log)