This repository was archived by the owner on Dec 4, 2023. It is now read-only.
generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.ts
121 lines (101 loc) · 2.81 KB
/
main.ts
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
import * as path from 'path'
import * as core from '@actions/core'
import {exec} from '@actions/exec'
import * as github from '@actions/github'
import {getChangedFiles} from './getChangedFiles'
async function run(): Promise<void> {
try {
const {owner, repo} = github.context.repo
const dryRun = core.getInput('dry')
const token = core.getInput('GITHUB_TOKEN')
if (!token) {
core.debug(`NO GITHUB_TOKEN`)
}
const octokit = new github.GitHub(token)
const changedFiles = await getChangedFiles(octokit)
core.debug(changedFiles.join(', '))
if (!changedFiles.length) {
return
}
let results: any = []
let eslintOutput = ''
let eslintError = ''
try {
await exec(
'node',
[
path.join(process.cwd(), 'node_modules/eslint/bin/eslint'),
`--fix-dry-run`,
'--format',
'json',
...changedFiles
],
{
silent: true,
listeners: {
stdout: (data: Buffer) => {
eslintOutput += data.toString()
},
stderr: (data: Buffer) => {
eslintError += data.toString()
}
}
}
)
} catch {}
core.debug(`error running eslint?: ${eslintError}`)
try {
results = JSON.parse(eslintOutput.replace(/\\"/g, '\\"'))
const stylish = require('eslint/lib/formatters/stylish')
// log to console so github action problem matchers can work on output
console.log(stylish(results)) // eslint-disable-line no-console
if (results.find(({errorCount}: any) => errorCount > 0)) {
core.setFailed('eslint completed with errors')
}
} catch (err) {
core.debug(eslintOutput)
core.setFailed(err.message)
}
if (dryRun) {
return
}
for (const result of results) {
const filePath = result.filePath.replace(
`${process.env.GITHUB_WORKSPACE}/`,
''
)
let file
if (result.output) {
try {
core.debug(`getContents: ${filePath}`)
const {data} = await octokit.repos.getContents({
owner,
repo,
path: filePath,
ref: process.env.GITHUB_HEAD_REF
})
file = data
} catch (err) {
core.debug(err.message)
}
if (!file || Array.isArray(file)) {
return
}
// Commit eslint fixes
octokit.repos.createOrUpdateFile({
owner,
repo,
path: filePath,
sha: file.sha,
message: 'style(): Auto eslint fix',
content: Buffer.from(result.output).toString('base64'),
branch: process.env.GITHUB_HEAD_REF
})
}
}
} catch (error) {
core.debug(error.stack)
core.setFailed(error.message)
}
}
run()