-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyser.ts
147 lines (134 loc) · 4.32 KB
/
analyser.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
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
import {
detectManager,
installPackage,
runPackage
} from "./helpers/package.js";
import * as core from "@actions/core";
import { ActionInputs, getPath } from "./github/utils.js";
import { VMDAnalysis, VMDOutput } from "./types.js";
import { uploadOutputArtifact } from "./github/artifact.js";
import { commentOnPullRequest } from "./github/comments.js";
import { getCommentTemplate } from "./templates/commentTemplate.js";
import * as github from "@actions/github";
import {
compareAnalysisResults,
parseAnalysisOutput
} from "./helpers/parser.js";
import { runGroup } from "./helpers/group.js";
import { REPORT_PATH } from "./helpers/constants.js";
import { restoreCache, saveCache } from "./github/cache.js";
import {
CURRENT_BRANCH,
IS_PULL_REQUEST,
TARGET_BRANCH
} from "./github/context.js";
import fs from "node:fs";
export async function runVueMessDetector(input: ActionInputs): Promise<void> {
if (input.skipBots && github.context.payload.sender?.type === "Bot") {
core.warning("Bot detected, skipping analysis!");
return;
}
const pkg: string = input.packageManager || detectManager();
await runGroup(`Install Vue Mess Detector with ${pkg}`, () =>
runInstallGroup(pkg, input)
);
await runGroup("Run Vue Mess Detector", () => runAnalysisGroup(pkg, input));
await runGroup("Upload Vue Mess Detector Report", () =>
runUploadGroup(input)
);
}
export async function installVMD(
skipInstall: boolean,
pkgManager: string,
version: string
): Promise<void> {
if (skipInstall) {
core.info("Skipping installation of vue-mess-detector!");
return;
}
const stdout: string = await installPackage(
pkgManager,
`vue-mess-detector@${version}`
);
core.debug(stdout);
}
export const runVMD: (
pkgManager: string,
input: ActionInputs
) => Promise<string> = (pkgManager: string, input: ActionInputs) =>
runPackage(pkgManager, "vue-mess-detector", [
"analyze",
getPath(input),
...input.runArgs.split(" "),
"--output=json",
"--file-output=" + REPORT_PATH
]);
async function runInstallGroup(pkgManager: string, input: ActionInputs) {
await installVMD(input.skipInstall, pkgManager, input.version);
}
async function runAnalysisGroup(pkgManager: string, input: ActionInputs) {
const runOutput: string = await runVMD(pkgManager, input);
core.debug(runOutput);
}
export async function commentFullReport(
analysisOutput: VMDAnalysis,
input: ActionInputs
): Promise<void> {
const output: VMDOutput = {
fullAnalysis: analysisOutput
};
const artifact: number | undefined = await uploadOutputArtifact(REPORT_PATH);
const commentBody: string = getCommentTemplate(output, artifact);
await core.summary.addRaw(commentBody).write();
if (IS_PULL_REQUEST && input.commentsEnabled)
await commentOnPullRequest(commentBody);
}
async function generatedRelativeArtifact(newIssues: VMDOutput) {
const newIssuesContent: string = JSON.stringify(newIssues, null, 2);
fs.writeFileSync(REPORT_PATH, newIssuesContent, "utf-8");
return await uploadOutputArtifact(REPORT_PATH);
}
export async function handleResult(
input: ActionInputs,
analysisOutput: VMDAnalysis
): Promise<void> {
if (!IS_PULL_REQUEST) {
await saveCache(REPORT_PATH, CURRENT_BRANCH);
await commentFullReport(analysisOutput, input);
return;
}
if (!TARGET_BRANCH) {
core.warning("Could not find the target branch!");
await commentFullReport(analysisOutput, input);
return;
}
core.info(
`Comparing analysis results with base branch (${TARGET_BRANCH})...`
);
const oldAnalysis: VMDAnalysis | undefined = await restoreCache(
TARGET_BRANCH,
REPORT_PATH
);
if (!oldAnalysis) {
await commentFullReport(analysisOutput, input);
return;
}
const newIssues: VMDOutput = compareAnalysisResults(
oldAnalysis,
analysisOutput
);
const artifact: number | undefined =
await generatedRelativeArtifact(newIssues);
const newIssuesCommentBody: string = getCommentTemplate(newIssues, artifact);
await core.summary.addRaw(newIssuesCommentBody).write();
if (input.commentsEnabled) {
await commentOnPullRequest(newIssuesCommentBody);
}
}
async function runUploadGroup(input: ActionInputs) {
const analysisOutput: VMDAnalysis | undefined =
parseAnalysisOutput(REPORT_PATH);
if (analysisOutput !== undefined) {
await handleResult(input, analysisOutput);
}
}