codeql-action/src/upload-sarif-action.ts
Andrew Eisenberg 3708898bf2 Add environment variables to signal feature and version to the CLI
This PR ensures environment variables are set before any invocation of
the CLI.  Here is a list of vars that are set:

https://github.com/github/codeql-coreql-team/issues/1124#issuecomment-852463521

This ensures the CLI knows the features and versions of the containing
actions/runner.

Additionally:

- Fix the user agent so that it more closely aligns with user agent
  spec
- Refactor environment variable initialization so that it all happens in
  one place and call.
- Move Mode, getRequiredEnvParam, setMode, getMode out of actions-util
  and into util. actions-util is meant for utils only called by the
  action, not the runner.

The `prepareLocalRunEnvironment()` method is most likely deprecated and
should be removed. I originally added it because I had a way of working
where I would run the action from my local machine to test out changes,
but this was always a little flaky. So, I no longer use this way of
working. I will probably remove it soon.
2021-06-02 11:06:02 -07:00

91 lines
2.1 KiB
TypeScript

import * as core from "@actions/core";
import * as actionsUtil from "./actions-util";
import { getActionsLogger } from "./logging";
import * as upload_lib from "./upload-lib";
import {
getGitHubVersion,
getRequiredEnvParam,
initializeEnvironment,
Mode,
} from "./util";
// eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json");
interface UploadSarifStatusReport
extends actionsUtil.StatusReportBase,
upload_lib.UploadStatusReport {}
async function sendSuccessStatusReport(
startedAt: Date,
uploadStats: upload_lib.UploadStatusReport
) {
const statusReportBase = await actionsUtil.createStatusReportBase(
"upload-sarif",
"success",
startedAt
);
const statusReport: UploadSarifStatusReport = {
...statusReportBase,
...uploadStats,
};
await actionsUtil.sendStatusReport(statusReport);
}
async function run() {
initializeEnvironment(Mode.actions, pkg.version);
const startedAt = new Date();
if (
!(await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
"upload-sarif",
"starting",
startedAt
)
))
) {
return;
}
try {
const apiDetails = {
auth: actionsUtil.getRequiredInput("token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
};
const gitHubVersion = await getGitHubVersion(apiDetails);
const uploadStats = await upload_lib.uploadFromActions(
actionsUtil.getRequiredInput("sarif_file"),
gitHubVersion,
apiDetails,
getActionsLogger()
);
await sendSuccessStatusReport(startedAt, uploadStats);
} catch (error) {
core.setFailed(error.message);
console.log(error);
await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
"upload-sarif",
"failure",
startedAt,
error.message,
error.stack
)
);
return;
}
}
async function runWrapper() {
try {
await run();
} catch (error) {
core.setFailed(`codeql/upload-sarif action failed: ${error}`);
console.log(error);
}
}
void runWrapper();