This repository was archived by the owner on Nov 18, 2024. It is now read-only.
generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patheslint.ts
99 lines (87 loc) · 2.81 KB
/
eslint.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
// import * as core from '@actions/core';
import { CLIEngine } from 'eslint';
import { getChangedFiles } from './fs';
import { Octokit, ActionData } from './types';
import { createCheck } from './api';
import { processLintResults } from './utils';
import { NAME, GITHUB_ANNOTATION_LIMIT } from './constants';
import {
getLintSummary,
getIgnoredFilesSummary,
getAnnotationSuggestions,
} from './utils/markdown';
export async function lintChangedFiles(
client: Octokit,
data: ActionData,
): Promise<void> {
const eslintConfig = {
extensions: data.eslint.extensions,
ignore: data.eslint.useEslintIgnore,
useEslintrc: data.eslint.useEslintrc,
rulePaths: data.eslint.rulePaths,
errorOnUnmatchedPattern: data.eslint.errorOnUnmatchedPattern,
fix: data.eslint.fix,
configFile: data.eslint.configFile,
};
console.log('[ESLINT] Run With Configuration ', eslintConfig);
const eslint = new CLIEngine(eslintConfig);
const updateCheck = await createCheck(client, data);
for await (const changed of getChangedFiles(client, data)) {
if (changed.length === 0) {
break;
}
const results = await eslint.executeOnFiles(changed);
const output = processLintResults(eslint, results, data);
if (output.annotations && output.annotations.length > 0) {
const annotations = [...(output.annotations || [])];
const batches: Array<typeof annotations> = [];
while (annotations.length > 0) {
batches.push(annotations.splice(0, GITHUB_ANNOTATION_LIMIT));
}
await Promise.all(
batches.map((batch) =>
updateCheck({
status: 'in_progress',
output: {
title: NAME,
summary: `${data.state.errorCount} error(s) found so far`,
annotations: data.reportSuggestions
? batch.map((annotation) => {
return {
...annotation,
message: `${
annotation.message
}\n\n${getAnnotationSuggestions(annotation)}`,
};
})
: batch,
},
}),
),
);
}
}
data.state.conclusion =
data.state.errorCount > 0 ||
!data.reportWarningsAsErrors ||
data.state.warningCount > 0
? 'failure'
: 'success';
await updateCheck({
conclusion: data.state.conclusion,
status: 'completed',
completed_at: new Date().toISOString(),
output: {
title: 'Checks Complete',
summary: getLintSummary(data) + getIgnoredFilesSummary(data, true),
},
});
// TODO
// await client.repos.createOrUpdateFileContents({
// owner: OWNER,
// repo: REPO,
// path: 'src/test.md',
// message: 'Commit Message',
// content: Buffer.from('Hello').toString('base64'),
// });
}