This repository has been archived by the owner on Jul 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
57 lines (52 loc) · 1.82 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
const standard = require('standard')
module.exports = robot => {
robot.on('push', async context => {
let exclude
const linterItems = {}
const push = context.payload
const compare = await context.github.repos.compareCommits(context.repo({
base: push.before,
head: push.after
}))
const branch = push.ref.replace('refs/heads/', '')
// Checks for a config file
let config = context.config('linter.yml')
// Adds properties to a LinterItem object to be passed to standard.lintText()
if (config) {
for (const property in config) {
if (property === 'exclude') {
exclude = config[property]
} else {
linterItems[property] = config[property]
}
}
}
return Promise.all(compare.data.files.map(async file => {
if (!exclude.includes(file.filename)) {
const content = await context.github.repos.getContent(context.repo({
path: file.filename,
ref: branch
}))
const text = Buffer.from(content.data.content, 'base64').toString()
Object.assign(linterItems, {cwd: '', fix: true, filename: file.filename})
standard.lintText(text, linterItems, (err, results) => {
if (err) {
throw new Error(err)
}
return Promise.all(results.results.map(result => {
if (result.output) {
// Checks that we have a fixed version and the file isn't part of the exclude list
context.github.repos.updateFile(context.repo({
path: file.filename,
message: `Fix lint errors for ${file.filename}`,
content: Buffer.from(result.output).toString('base64'),
sha: content.data.sha,
branch
}))
}
}))
})
}
}))
})
}