-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.ts
More file actions
89 lines (72 loc) · 1.91 KB
/
main.ts
File metadata and controls
89 lines (72 loc) · 1.91 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
import {
setFailed,
info,
error,
warning,
getInput,
startGroup,
endGroup,
} from '@actions/core';
import { context } from '@actions/github';
import { existsSync, readFileSync } from 'node:fs';
import { PRCommentManager } from './pr-comment-manager';
import { PRCommentFormatter } from './pr-comment-formatter';
import { decodeHtmlEntities } from './utils';
import { parseResultsFile } from './parse-results';
const args = process.argv.slice(2);
const file = args[0];
if (!existsSync(file)) {
setFailed('Results file does not exist.');
}
const fileContents = existsSync(file)
? readFileSync(file, { encoding: 'utf8' })
: '';
const fileResultsMap = parseResultsFile(fileContents);
for (const [fileName, results] of fileResultsMap) {
if (results.length > 0 && process.env.STRICT === 'true') {
process.exitCode = 1;
}
for (const result of results) {
if (result.type === 'ERROR') {
process.exitCode = 1;
}
const func =
result.type === 'ERROR' || process.env.STRICT === 'true'
? error
: warning;
func(decodeHtmlEntities(result.message), {
title: result.code,
file: fileName,
startLine: result.line,
startColumn: result.column,
});
}
}
async function postPRComment() {
startGroup('Posting PR comment');
try {
const token = getInput('repo-token') || process.env.INPUT_REPO_TOKEN;
const prNumber = context.payload.pull_request?.number;
if (!token) {
info('No GitHub token provided, skipping PR comment');
return;
}
if (!prNumber) {
info('Not a pull request, skipping PR comment');
return;
}
const manager = new PRCommentManager({
token,
owner: context.repo.owner,
repo: context.repo.repo,
prNumber,
});
const formatter = new PRCommentFormatter(fileResultsMap);
const commentBody = formatter.getComment();
await manager.postComment(commentBody);
} catch (err) {
error(`Failed to post PR comment: ${err}`);
}
endGroup();
}
postPRComment();