-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
118 lines (101 loc) · 4.16 KB
/
index.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
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as Octokit from '@octokit/rest'
import * as fs from 'fs'
import * as glob from 'glob'
import { Configuration, Linter } from 'tslint'
type LEVEL = 'notice' | 'warning' | 'failure'
enum CONCLUSION {
FAILURE = 'failure',
SUCCESS = 'success',
}
enum MODE {
ALL = 'all',
COMMIT = 'commit',
}
const LINTER = 'Linter'
const main = async () => {
const { repo: { owner, repo }, sha: head_sha } = github.context
try {
const lintFile = core.getInput('lintFile', { required: true }) // lintFile
const token = core.getInput('token', { required: true }) // github token
const pattern = core.getInput('pattern') // file pattern
const mode = core.getInput('mode') || MODE.COMMIT // ALL || COMMIT (default)
if (mode && ![MODE.ALL, MODE.COMMIT].includes(mode as MODE)) throw new Error('Bad Request: target parameter is not valid (all, commit).')
if (mode === MODE.ALL && !pattern) throw new Error('Bad Request: all target must need pattern parameter.')
const gitToolkit: Octokit = new github.GitHub(token)
const check = await gitToolkit.checks.create({ owner, repo, name: LINTER, head_sha, status: 'in_progress' })
const linter = new Linter({ fix: false, formatter: 'json' })
let fileList
if (mode === MODE.ALL) {
fileList = glob.sync(pattern, { dot: true, ignore: ['./node_modules/**'] })
} else {
const { data: prData } = await gitToolkit.search.issuesAndPullRequests({ q: `sha:${head_sha}` })
if (!prData.items.length) { // if pull_request is not exist = first pr
const { data: commit } = await gitToolkit.repos.getCommit({ owner, repo, ref: head_sha })
fileList = commit.files.map((d) => {
return d.filename && new RegExp(/\.ts$/g).test(d.filename) ? d.filename : ''
})
} else { // if pull_request is exist
const { data: prInfo } = await gitToolkit.pulls.listFiles({ owner, repo, pull_number: prData.items[0].number })
fileList = prInfo.map((d) => {
return d.filename && new RegExp(/\.ts$/g).test(d.filename) ? d.filename : ''
})
}
}
for (let i = 0; i < fileList.length; i++) {
const filename = fileList[i]
if (!filename) continue
if (!fs.existsSync(filename)) continue
const inFileContents = fs.readFileSync(filename, 'utf8')
const configuration = Configuration.findConfiguration(lintFile, filename).results
linter.lint(filename, inFileContents, configuration)
}
const lintResult = linter.getResult()
if (!lintResult.failures.length) {
await gitToolkit.checks.update({
owner,
repo,
check_run_id: check.data.id,
name: LINTER,
status: 'completed',
conclusion: CONCLUSION.SUCCESS,
output: {
title: 'Tslint Check Report',
summary: `0 errors\n0 warnings`,
},
})
}
const annotations: Octokit.ChecksCreateParamsOutputAnnotations[] = []
for (let failure of lintResult.failures) {
const level = { 'warning': 'warning', 'error': 'failure', 'off': 'notice' }[failure.getRuleSeverity()] || 'notice'
annotations.push({
path: failure.getFileName(),
annotation_level: level as LEVEL,
title: 'tsLint Checker',
message: `${failure.getRuleName()}: ${failure.getFailure()}`,
start_line: failure.getStartPosition().getLineAndCharacter().line + 1,
end_line: failure.getEndPosition().getLineAndCharacter().line + 1,
})
if (annotations.length === 50 || annotations.length === lintResult.failures.length) {
await gitToolkit.checks.update({
owner,
repo,
check_run_id: check.data.id,
name: LINTER,
status: 'completed',
conclusion: lintResult.errorCount ? CONCLUSION.FAILURE : CONCLUSION.SUCCESS,
output: {
title: 'Tslint Check Report',
summary: `${lintResult.errorCount} errors\n${lintResult.warningCount} warnings`,
annotations,
},
})
annotations.length = 0
}
}
} catch (err) {
core.setFailed(`Action failed with error: ${err}`)
}
}
main()