-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathautobuild-action.ts
149 lines (135 loc) · 4.17 KB
/
autobuild-action.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
148
149
import * as core from "@actions/core";
import {
getActionVersion,
getOptionalInput,
getTemporaryDirectory,
} from "./actions-util";
import { getGitHubVersion } from "./api-client";
import { determineAutobuildLanguages, runAutobuild } from "./autobuild";
import { getCodeQL } from "./codeql";
import { Config, getConfig } from "./config-utils";
import { EnvVar } from "./environment";
import { Language } from "./languages";
import { Logger, getActionsLogger } from "./logging";
import {
StatusReportBase,
getActionsStatus,
createStatusReportBase,
sendStatusReport,
ActionName,
} from "./status-report";
import { endTracingForCluster } from "./tracer-config";
import {
checkActionVersion,
checkDiskUsage,
checkGitHubVersionInRange,
getErrorMessage,
initializeEnvironment,
wrapError,
} from "./util";
interface AutobuildStatusReport extends StatusReportBase {
/** Comma-separated set of languages being auto-built. */
autobuild_languages: string;
/** Language that failed autobuilding (or undefined if all languages succeeded). */
autobuild_failure?: string;
}
async function sendCompletedStatusReport(
config: Config | undefined,
logger: Logger,
startedAt: Date,
allLanguages: string[],
failingLanguage?: string,
cause?: Error,
) {
initializeEnvironment(getActionVersion());
const status = getActionsStatus(cause, failingLanguage);
const statusReportBase = await createStatusReportBase(
ActionName.Autobuild,
status,
startedAt,
config,
await checkDiskUsage(logger),
logger,
cause?.message,
cause?.stack,
);
if (statusReportBase !== undefined) {
const statusReport: AutobuildStatusReport = {
...statusReportBase,
autobuild_languages: allLanguages.join(","),
autobuild_failure: failingLanguage,
};
await sendStatusReport(statusReport);
}
}
async function run() {
const startedAt = new Date();
const logger = getActionsLogger();
let config: Config | undefined;
let currentLanguage: Language | undefined;
let languages: Language[] | undefined;
try {
const statusReportBase = await createStatusReportBase(
ActionName.Autobuild,
"starting",
startedAt,
config,
await checkDiskUsage(logger),
logger,
);
if (statusReportBase !== undefined) {
await sendStatusReport(statusReportBase);
}
const gitHubVersion = await getGitHubVersion();
checkGitHubVersionInRange(gitHubVersion, logger);
checkActionVersion(getActionVersion(), gitHubVersion);
config = await getConfig(getTemporaryDirectory(), logger);
if (config === undefined) {
throw new Error(
"Config file could not be found at expected location. Has the 'init' action been called?",
);
}
const codeql = await getCodeQL(config.codeQLCmd);
languages = await determineAutobuildLanguages(codeql, config, logger);
if (languages !== undefined) {
const workingDirectory = getOptionalInput("working-directory");
if (workingDirectory) {
logger.info(
`Changing autobuilder working directory to ${workingDirectory}`,
);
process.chdir(workingDirectory);
}
for (const language of languages) {
currentLanguage = language;
await runAutobuild(config, language, logger);
}
}
// End tracing early to avoid tracing analyze. This improves the performance and reliability of
// the analyze step.
await endTracingForCluster(codeql, config, logger);
} catch (unwrappedError) {
const error = wrapError(unwrappedError);
core.setFailed(
`We were unable to automatically build your code. Please replace the call to the autobuild action with your custom build steps. ${error.message}`,
);
await sendCompletedStatusReport(
config,
logger,
startedAt,
languages ?? [],
currentLanguage,
error,
);
return;
}
core.exportVariable(EnvVar.AUTOBUILD_DID_COMPLETE_SUCCESSFULLY, "true");
await sendCompletedStatusReport(config, logger, startedAt, languages ?? []);
}
async function runWrapper() {
try {
await run();
} catch (error) {
core.setFailed(`autobuild action failed. ${getErrorMessage(error)}`);
}
}
void runWrapper();