This repository was archived by the owner on Nov 17, 2022. It is now read-only.
forked from goto-bus-stop/standard-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (150 loc) · 4.44 KB
/
Copy pathindex.js
File metadata and controls
178 lines (150 loc) · 4.44 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const fetch = require('node-fetch')
const { promisify } = require('util')
const { CLIEngine } = require('eslint')
const core = require('@actions/core')
const github = require('@actions/github')
const resolve = require('resolve')
const {
GITHUB_REPOSITORY,
GITHUB_SHA,
GITHUB_TOKEN,
GITHUB_WORKSPACE
} = process.env
const CHECK_NAME = 'Standard'
main().catch((err) => {
core.setFailed(err.message)
process.exit(1)
})
async function publishResults (results) {
const annotations = []
for (const result of results.results) {
annotations.push(...toAnnotations(result))
}
const headers = {
'content-type': 'application/json',
accept: 'application/vnd.github.antiope-preview+json',
authorization: `Bearer ${GITHUB_TOKEN}`,
'user-agent': 'standard-action'
}
const check = {
name: CHECK_NAME,
head_sha: GITHUB_SHA,
status: 'completed',
started_at: new Date(),
conclusion: results.errorCount > 0 ? 'failure' : 'success',
output: {
title: CHECK_NAME,
summary: `${results.errorCount} error(s), ${results.warningCount} warning(s) found`,
annotations
}
}
const response = await fetch(`https://api.github.com/repos/${GITHUB_REPOSITORY}/check-runs`, {
method: 'POST',
headers,
body: JSON.stringify(check)
})
if (response.status !== 201) {
// eh
const err = await response.json()
throw err
}
function toAnnotations ({ filePath, messages }) {
const path = filePath.substr(GITHUB_WORKSPACE.length + 1)
return messages.map(({ line, severity, ruleId, message }) => {
const annotationLevel = {
1: 'warning',
2: 'failure'
}[severity]
return {
path,
start_line: line,
end_line: line,
annotation_level: annotationLevel,
message: `[${ruleId}] ${message}`
}
})
}
}
function printResults (results, formatStyle) {
const formatter = CLIEngine.getFormatter(formatStyle)
console.log(formatter(results.results, {}))
}
function getPrNumber () {
const pullRequest = github.context.payload.pull_request
if (!pullRequest) {
return undefined
}
return pullRequest.number
}
async function getChangedFiles (
client,
prNumber
) {
const listFilesResponse = await client.pulls.listFiles({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
})
const changedFiles = listFilesResponse.data.map(f => f.filename)
const changedJsFiles = changedFiles.filter(filename => filename.endsWith('.js'))
return changedJsFiles
}
function loadLinter (name) {
let linterPath
try {
linterPath = resolve.sync(name, { basedir: process.cwd() })
} catch (err) {
if (name === 'standard') {
linterPath = 'standard' // use our bundled standard version
} else {
throw new Error(`Linter '${name}' not found, perhaps you need a 'run: npm install' step before this one?`)
}
}
let linter
try {
linter = require(linterPath)
} catch (err) {
throw new Error(`Linter '${name}' not found, perhaps you need a 'run: npm install' step before this one?`)
}
if (!linter.lintFiles) {
throw new Error(`Module '${name}' is not a standard-compatible linter.`)
}
return linter
}
async function main () {
const formatStyle = core.getInput('formatter')
const linterName = core.getInput('linter')
const useAnnotations = core.getInput('annotate')
const client = new github.GitHub(process.env.GITHUB_TOKEN)
const prNumber = getPrNumber()
const changedFiles = await getChangedFiles(client, prNumber)
console.log('changedFiles', changedFiles)
if (changedFiles.length === 0) {
console.log('no .js files were changed, exiting successfully')
process.exit(0)
}
if (useAnnotations === 'true' && !process.env.GITHUB_TOKEN) {
throw new Error(`when using annotate: true, you must set
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
in your action config.`)
}
const linter = loadLinter(linterName)
const lintFiles = promisify(linter.lintFiles.bind(linter))
const results = await lintFiles(changedFiles, {
cwd: GITHUB_WORKSPACE
})
printResults(results, formatStyle)
if (useAnnotations === 'true') {
try {
await publishResults(results)
} catch (err) {
console.error(err)
core.setFailed(err.message)
}
}
if (results.errorCount > 0) {
core.setFailed(`${results.errorCount} error(s), ${results.warningCount} warning(s) found`)
process.exit(1)
}
}