mirror of
https://github.com/github/codeql-action.git
synced 2026-05-12 08:40:13 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 26812c842e | |||
| 6d90f4c71e | |||
| 37f3bfc967 | |||
| b30d90c496 | |||
| 503c5b9421 | |||
| c59e24e20a | |||
| 7cbb19ece7 |
Generated
+1026
-2123
File diff suppressed because it is too large
Load Diff
Generated
+2009
-3150
File diff suppressed because it is too large
Load Diff
Generated
+2355
-2388
File diff suppressed because it is too large
Load Diff
Generated
+3309
-3287
File diff suppressed because it is too large
Load Diff
@@ -363,7 +363,9 @@ async function run(startedAt: Date) {
|
|||||||
|
|
||||||
uploadResults = await postProcessAndUploadSarif(
|
uploadResults = await postProcessAndUploadSarif(
|
||||||
logger,
|
logger,
|
||||||
|
config.tempDir,
|
||||||
features,
|
features,
|
||||||
|
async () => codeql,
|
||||||
uploadKind,
|
uploadKind,
|
||||||
checkoutPath,
|
checkoutPath,
|
||||||
outputDir,
|
outputDir,
|
||||||
|
|||||||
@@ -602,6 +602,8 @@ async function testFailedSarifUpload(
|
|||||||
}
|
}
|
||||||
t.true(
|
t.true(
|
||||||
uploadFiles.calledOnceWith(
|
uploadFiles.calledOnceWith(
|
||||||
|
sinon.match.string,
|
||||||
|
codeqlObject,
|
||||||
sinon.match.string,
|
sinon.match.string,
|
||||||
sinon.match.string,
|
sinon.match.string,
|
||||||
category,
|
category,
|
||||||
|
|||||||
@@ -107,6 +107,8 @@ async function maybeUploadFailedSarif(
|
|||||||
|
|
||||||
logger.info(`Uploading failed SARIF file ${sarifFile}`);
|
logger.info(`Uploading failed SARIF file ${sarifFile}`);
|
||||||
const uploadResult = await uploadLib.uploadFiles(
|
const uploadResult = await uploadLib.uploadFiles(
|
||||||
|
config.tempDir,
|
||||||
|
codeql,
|
||||||
sarifFile,
|
sarifFile,
|
||||||
checkoutPath,
|
checkoutPath,
|
||||||
category,
|
category,
|
||||||
|
|||||||
+60
-42
@@ -11,8 +11,7 @@ import * as actionsUtil from "./actions-util";
|
|||||||
import * as analyses from "./analyses";
|
import * as analyses from "./analyses";
|
||||||
import * as api from "./api-client";
|
import * as api from "./api-client";
|
||||||
import { getGitHubVersion, wrapApiConfigurationError } from "./api-client";
|
import { getGitHubVersion, wrapApiConfigurationError } from "./api-client";
|
||||||
import { CodeQL, getCodeQL } from "./codeql";
|
import { type CodeQL } from "./codeql";
|
||||||
import { getConfig } from "./config-utils";
|
|
||||||
import { readDiffRangesJsonFile } from "./diff-informed-analysis-utils";
|
import { readDiffRangesJsonFile } from "./diff-informed-analysis-utils";
|
||||||
import { EnvVar } from "./environment";
|
import { EnvVar } from "./environment";
|
||||||
import { FeatureEnablement } from "./feature-flags";
|
import { FeatureEnablement } from "./feature-flags";
|
||||||
@@ -183,6 +182,44 @@ async function shouldDisableCombineSarifFiles(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialises a `CodeQL` instance that we can use to combine SARIF files.
|
||||||
|
*/
|
||||||
|
export async function minimalInitCodeQL(
|
||||||
|
logger: Logger,
|
||||||
|
gitHubVersion: GitHubVersion,
|
||||||
|
features: FeatureEnablement,
|
||||||
|
): Promise<CodeQL> {
|
||||||
|
logger.info(
|
||||||
|
"Initializing CodeQL since the 'init' Action was not called before this step.",
|
||||||
|
);
|
||||||
|
|
||||||
|
const apiDetails = {
|
||||||
|
auth: actionsUtil.getRequiredInput("token"),
|
||||||
|
externalRepoAuth: actionsUtil.getOptionalInput("external-repository-token"),
|
||||||
|
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
|
||||||
|
apiURL: getRequiredEnvParam("GITHUB_API_URL"),
|
||||||
|
};
|
||||||
|
|
||||||
|
const codeQLDefaultVersionInfo = await features.getDefaultCliVersion(
|
||||||
|
gitHubVersion.type,
|
||||||
|
);
|
||||||
|
|
||||||
|
const initCodeQLResult = await initCodeQL(
|
||||||
|
undefined, // There is no tools input on the upload action
|
||||||
|
apiDetails,
|
||||||
|
actionsUtil.getTemporaryDirectory(),
|
||||||
|
gitHubVersion.type,
|
||||||
|
codeQLDefaultVersionInfo,
|
||||||
|
features,
|
||||||
|
logger,
|
||||||
|
);
|
||||||
|
|
||||||
|
return initCodeQLResult.codeql;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CodeQLGetter = () => Promise<CodeQL>;
|
||||||
|
|
||||||
// Takes a list of paths to sarif files and combines them together using the
|
// Takes a list of paths to sarif files and combines them together using the
|
||||||
// CLI `github merge-results` command when all SARIF files are produced by
|
// CLI `github merge-results` command when all SARIF files are produced by
|
||||||
// CodeQL. Otherwise, it will fall back to combining the files in the action.
|
// CodeQL. Otherwise, it will fall back to combining the files in the action.
|
||||||
@@ -190,8 +227,10 @@ async function shouldDisableCombineSarifFiles(
|
|||||||
async function combineSarifFilesUsingCLI(
|
async function combineSarifFilesUsingCLI(
|
||||||
sarifFiles: string[],
|
sarifFiles: string[],
|
||||||
gitHubVersion: GitHubVersion,
|
gitHubVersion: GitHubVersion,
|
||||||
features: FeatureEnablement,
|
_features: FeatureEnablement,
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
|
getCodeQL: CodeQLGetter,
|
||||||
|
tempDir: string,
|
||||||
): Promise<SarifFile> {
|
): Promise<SarifFile> {
|
||||||
logger.info("Combining SARIF files using the CodeQL CLI");
|
logger.info("Combining SARIF files using the CodeQL CLI");
|
||||||
|
|
||||||
@@ -229,45 +268,10 @@ async function combineSarifFilesUsingCLI(
|
|||||||
return combineSarifFiles(sarifFiles, logger);
|
return combineSarifFiles(sarifFiles, logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize CodeQL, either by using the config file from the 'init' step,
|
// Obtain a `CodeQL` instance. For `analyze`, this is typically the instance that was used for running the queries.
|
||||||
// or by initializing it here.
|
// For `upload-sarif`, this either initialises a new instance or returns a previously initialised one if `getCodeQL`
|
||||||
let codeQL: CodeQL;
|
// is called more than once.
|
||||||
let tempDir: string = actionsUtil.getTemporaryDirectory();
|
const codeQL: CodeQL = await getCodeQL();
|
||||||
|
|
||||||
const config = await getConfig(tempDir, logger);
|
|
||||||
if (config !== undefined) {
|
|
||||||
codeQL = await getCodeQL(config.codeQLCmd);
|
|
||||||
tempDir = config.tempDir;
|
|
||||||
} else {
|
|
||||||
logger.info(
|
|
||||||
"Initializing CodeQL since the 'init' Action was not called before this step.",
|
|
||||||
);
|
|
||||||
|
|
||||||
const apiDetails = {
|
|
||||||
auth: actionsUtil.getRequiredInput("token"),
|
|
||||||
externalRepoAuth: actionsUtil.getOptionalInput(
|
|
||||||
"external-repository-token",
|
|
||||||
),
|
|
||||||
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
|
|
||||||
apiURL: getRequiredEnvParam("GITHUB_API_URL"),
|
|
||||||
};
|
|
||||||
|
|
||||||
const codeQLDefaultVersionInfo = await features.getDefaultCliVersion(
|
|
||||||
gitHubVersion.type,
|
|
||||||
);
|
|
||||||
|
|
||||||
const initCodeQLResult = await initCodeQL(
|
|
||||||
undefined, // There is no tools input on the upload action
|
|
||||||
apiDetails,
|
|
||||||
tempDir,
|
|
||||||
gitHubVersion.type,
|
|
||||||
codeQLDefaultVersionInfo,
|
|
||||||
features,
|
|
||||||
logger,
|
|
||||||
);
|
|
||||||
|
|
||||||
codeQL = initCodeQLResult.codeql;
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseTempDir = path.resolve(tempDir, "combined-sarif");
|
const baseTempDir = path.resolve(tempDir, "combined-sarif");
|
||||||
fs.mkdirSync(baseTempDir, { recursive: true });
|
fs.mkdirSync(baseTempDir, { recursive: true });
|
||||||
@@ -673,6 +677,8 @@ export interface PostProcessingResults {
|
|||||||
*
|
*
|
||||||
* @param logger The logger to use.
|
* @param logger The logger to use.
|
||||||
* @param features Information about enabled features.
|
* @param features Information about enabled features.
|
||||||
|
* @param getCodeQL A function to retrieve a `CodeQL` instance.
|
||||||
|
* @param tempPath A path to a temporary directory.
|
||||||
* @param checkoutPath The path where the repo was checked out at.
|
* @param checkoutPath The path where the repo was checked out at.
|
||||||
* @param sarifPaths The paths of the SARIF files to post-process.
|
* @param sarifPaths The paths of the SARIF files to post-process.
|
||||||
* @param category The analysis category.
|
* @param category The analysis category.
|
||||||
@@ -684,6 +690,8 @@ export interface PostProcessingResults {
|
|||||||
export async function postProcessSarifFiles(
|
export async function postProcessSarifFiles(
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
features: FeatureEnablement,
|
features: FeatureEnablement,
|
||||||
|
getCodeQL: CodeQLGetter,
|
||||||
|
tempPath: string,
|
||||||
checkoutPath: string,
|
checkoutPath: string,
|
||||||
sarifPaths: string[],
|
sarifPaths: string[],
|
||||||
category: string | undefined,
|
category: string | undefined,
|
||||||
@@ -708,6 +716,8 @@ export async function postProcessSarifFiles(
|
|||||||
gitHubVersion,
|
gitHubVersion,
|
||||||
features,
|
features,
|
||||||
logger,
|
logger,
|
||||||
|
getCodeQL,
|
||||||
|
tempPath,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const sarifPath = sarifPaths[0];
|
const sarifPath = sarifPaths[0];
|
||||||
@@ -768,6 +778,8 @@ export async function writePostProcessedFiles(
|
|||||||
* to.
|
* to.
|
||||||
*/
|
*/
|
||||||
export async function uploadFiles(
|
export async function uploadFiles(
|
||||||
|
tempDir: string,
|
||||||
|
codeql: CodeQL,
|
||||||
inputSarifPath: string,
|
inputSarifPath: string,
|
||||||
checkoutPath: string,
|
checkoutPath: string,
|
||||||
category: string | undefined,
|
category: string | undefined,
|
||||||
@@ -781,6 +793,8 @@ export async function uploadFiles(
|
|||||||
);
|
);
|
||||||
|
|
||||||
return uploadSpecifiedFiles(
|
return uploadSpecifiedFiles(
|
||||||
|
tempDir,
|
||||||
|
codeql,
|
||||||
sarifPaths,
|
sarifPaths,
|
||||||
checkoutPath,
|
checkoutPath,
|
||||||
category,
|
category,
|
||||||
@@ -794,6 +808,8 @@ export async function uploadFiles(
|
|||||||
* Uploads the given array of SARIF files.
|
* Uploads the given array of SARIF files.
|
||||||
*/
|
*/
|
||||||
async function uploadSpecifiedFiles(
|
async function uploadSpecifiedFiles(
|
||||||
|
tempDir: string,
|
||||||
|
codeql: CodeQL,
|
||||||
sarifPaths: string[],
|
sarifPaths: string[],
|
||||||
checkoutPath: string,
|
checkoutPath: string,
|
||||||
category: string | undefined,
|
category: string | undefined,
|
||||||
@@ -804,6 +820,8 @@ async function uploadSpecifiedFiles(
|
|||||||
const processingResults: PostProcessingResults = await postProcessSarifFiles(
|
const processingResults: PostProcessingResults = await postProcessSarifFiles(
|
||||||
logger,
|
logger,
|
||||||
features,
|
features,
|
||||||
|
async () => codeql,
|
||||||
|
tempDir,
|
||||||
checkoutPath,
|
checkoutPath,
|
||||||
sarifPaths,
|
sarifPaths,
|
||||||
category,
|
category,
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import * as core from "@actions/core";
|
import * as core from "@actions/core";
|
||||||
|
|
||||||
import * as actionsUtil from "./actions-util";
|
import * as actionsUtil from "./actions-util";
|
||||||
import { getActionVersion, getTemporaryDirectory } from "./actions-util";
|
|
||||||
import * as analyses from "./analyses";
|
import * as analyses from "./analyses";
|
||||||
import { getGitHubVersion } from "./api-client";
|
import { getGitHubVersion } from "./api-client";
|
||||||
|
import { getConfig } from "./config-utils";
|
||||||
import { initFeatures } from "./feature-flags";
|
import { initFeatures } from "./feature-flags";
|
||||||
import { Logger, getActionsLogger } from "./logging";
|
import { Logger, getActionsLogger } from "./logging";
|
||||||
import { getRepositoryNwo } from "./repository";
|
import { getRepositoryNwo } from "./repository";
|
||||||
@@ -17,7 +17,11 @@ import {
|
|||||||
isThirdPartyAnalysis,
|
isThirdPartyAnalysis,
|
||||||
} from "./status-report";
|
} from "./status-report";
|
||||||
import * as upload_lib from "./upload-lib";
|
import * as upload_lib from "./upload-lib";
|
||||||
import { postProcessAndUploadSarif } from "./upload-sarif";
|
import {
|
||||||
|
getOrInitCodeQL,
|
||||||
|
postProcessAndUploadSarif,
|
||||||
|
UploadSarifState,
|
||||||
|
} from "./upload-sarif";
|
||||||
import {
|
import {
|
||||||
ConfigurationError,
|
ConfigurationError,
|
||||||
checkActionVersion,
|
checkActionVersion,
|
||||||
@@ -59,12 +63,13 @@ async function run(startedAt: Date) {
|
|||||||
// possible, and only use safe functions outside.
|
// possible, and only use safe functions outside.
|
||||||
|
|
||||||
const logger = getActionsLogger();
|
const logger = getActionsLogger();
|
||||||
|
const state: UploadSarifState = { cachedCodeQL: undefined };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
initializeEnvironment(getActionVersion());
|
initializeEnvironment(actionsUtil.getActionVersion());
|
||||||
|
|
||||||
const gitHubVersion = await getGitHubVersion();
|
const gitHubVersion = await getGitHubVersion();
|
||||||
checkActionVersion(getActionVersion(), gitHubVersion);
|
checkActionVersion(actionsUtil.getActionVersion(), gitHubVersion);
|
||||||
|
|
||||||
// Make inputs accessible in the `post` step.
|
// Make inputs accessible in the `post` step.
|
||||||
actionsUtil.persistInputs();
|
actionsUtil.persistInputs();
|
||||||
@@ -73,7 +78,7 @@ async function run(startedAt: Date) {
|
|||||||
const features = initFeatures(
|
const features = initFeatures(
|
||||||
gitHubVersion,
|
gitHubVersion,
|
||||||
repositoryNwo,
|
repositoryNwo,
|
||||||
getTemporaryDirectory(),
|
actionsUtil.getTemporaryDirectory(),
|
||||||
logger,
|
logger,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -94,9 +99,20 @@ async function run(startedAt: Date) {
|
|||||||
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
|
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
|
||||||
const category = actionsUtil.getOptionalInput("category");
|
const category = actionsUtil.getOptionalInput("category");
|
||||||
|
|
||||||
|
// Determine the temporary directory to use. If we are able to read a `Config` from a previous CodeQL Action
|
||||||
|
// step in the job, then use the temporary directory configured there. Otherwise, use our default.
|
||||||
|
let tempDir: string = actionsUtil.getTemporaryDirectory();
|
||||||
|
|
||||||
|
const config = await getConfig(tempDir, logger);
|
||||||
|
if (config !== undefined) {
|
||||||
|
tempDir = config.tempDir;
|
||||||
|
}
|
||||||
|
|
||||||
const uploadResults = await postProcessAndUploadSarif(
|
const uploadResults = await postProcessAndUploadSarif(
|
||||||
logger,
|
logger,
|
||||||
|
tempDir,
|
||||||
features,
|
features,
|
||||||
|
() => getOrInitCodeQL(state, logger, gitHubVersion, features, config),
|
||||||
"always",
|
"always",
|
||||||
checkoutPath,
|
checkoutPath,
|
||||||
sarifPath,
|
sarifPath,
|
||||||
|
|||||||
@@ -5,15 +5,102 @@ import test, { ExecutionContext } from "ava";
|
|||||||
import * as sinon from "sinon";
|
import * as sinon from "sinon";
|
||||||
|
|
||||||
import { AnalysisKind, getAnalysisConfig } from "./analyses";
|
import { AnalysisKind, getAnalysisConfig } from "./analyses";
|
||||||
|
import { getCodeQLForTesting } from "./codeql";
|
||||||
|
import * as codeql from "./codeql";
|
||||||
import { getRunnerLogger } from "./logging";
|
import { getRunnerLogger } from "./logging";
|
||||||
import { createFeatures, setupTests } from "./testing-utils";
|
import { createFeatures, createTestConfig, setupTests } from "./testing-utils";
|
||||||
import { UploadResult } from "./upload-lib";
|
import { UploadResult } from "./upload-lib";
|
||||||
import * as uploadLib from "./upload-lib";
|
import * as uploadLib from "./upload-lib";
|
||||||
import { postProcessAndUploadSarif } from "./upload-sarif";
|
import {
|
||||||
|
getOrInitCodeQL,
|
||||||
|
postProcessAndUploadSarif,
|
||||||
|
UploadSarifState,
|
||||||
|
} from "./upload-sarif";
|
||||||
import * as util from "./util";
|
import * as util from "./util";
|
||||||
|
|
||||||
setupTests(test);
|
setupTests(test);
|
||||||
|
|
||||||
|
test("getOrInitCodeQL - gets cached CodeQL instance when available", async (t) => {
|
||||||
|
const cachedCodeQL = await getCodeQLForTesting();
|
||||||
|
const getCodeQL = sinon.stub(codeql, "getCodeQL").resolves(undefined);
|
||||||
|
const minimalInitCodeQL = sinon
|
||||||
|
.stub(uploadLib, "minimalInitCodeQL")
|
||||||
|
.resolves(undefined);
|
||||||
|
|
||||||
|
const result = await getOrInitCodeQL(
|
||||||
|
{ cachedCodeQL },
|
||||||
|
getRunnerLogger(true),
|
||||||
|
{ type: util.GitHubVariant.GHES, version: "3.0" },
|
||||||
|
createFeatures([]),
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Neither of the two functions to get a CodeQL instance were called.
|
||||||
|
t.true(getCodeQL.notCalled);
|
||||||
|
t.true(minimalInitCodeQL.notCalled);
|
||||||
|
|
||||||
|
// But we have an instance that refers to the same object as the one we put into the state.
|
||||||
|
t.truthy(result);
|
||||||
|
t.is(result, cachedCodeQL);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getOrInitCodeQL - uses minimalInitCodeQL when there's no config", async (t) => {
|
||||||
|
const newInstance = await getCodeQLForTesting();
|
||||||
|
const getCodeQL = sinon.stub(codeql, "getCodeQL").resolves(undefined);
|
||||||
|
const minimalInitCodeQL = sinon
|
||||||
|
.stub(uploadLib, "minimalInitCodeQL")
|
||||||
|
.resolves(newInstance);
|
||||||
|
|
||||||
|
const state: UploadSarifState = { cachedCodeQL: undefined };
|
||||||
|
const result = await getOrInitCodeQL(
|
||||||
|
state,
|
||||||
|
getRunnerLogger(true),
|
||||||
|
{ type: util.GitHubVariant.GHES, version: "3.0" },
|
||||||
|
createFeatures([]),
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check that the right function was called.
|
||||||
|
t.true(getCodeQL.notCalled);
|
||||||
|
t.true(minimalInitCodeQL.calledOnce);
|
||||||
|
|
||||||
|
// And that we received the instance that we expected.
|
||||||
|
t.truthy(result);
|
||||||
|
t.is(result, newInstance);
|
||||||
|
|
||||||
|
// And that it was cached.
|
||||||
|
t.is(state.cachedCodeQL, newInstance);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getOrInitCodeQL - uses getCodeQL when there's a config", async (t) => {
|
||||||
|
const newInstance = await getCodeQLForTesting();
|
||||||
|
const getCodeQL = sinon.stub(codeql, "getCodeQL").resolves(newInstance);
|
||||||
|
const minimalInitCodeQL = sinon
|
||||||
|
.stub(uploadLib, "minimalInitCodeQL")
|
||||||
|
.resolves(undefined);
|
||||||
|
const config = createTestConfig({});
|
||||||
|
|
||||||
|
const state: UploadSarifState = { cachedCodeQL: undefined };
|
||||||
|
const result = await getOrInitCodeQL(
|
||||||
|
state,
|
||||||
|
getRunnerLogger(true),
|
||||||
|
{ type: util.GitHubVariant.GHES, version: "3.0" },
|
||||||
|
createFeatures([]),
|
||||||
|
config,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check that the right function was called.
|
||||||
|
t.true(getCodeQL.calledOnce);
|
||||||
|
t.true(minimalInitCodeQL.notCalled);
|
||||||
|
|
||||||
|
// And that we received the instance that we expected.
|
||||||
|
t.truthy(result);
|
||||||
|
t.is(result, newInstance);
|
||||||
|
|
||||||
|
// And that it was cached.
|
||||||
|
t.is(state.cachedCodeQL, newInstance);
|
||||||
|
});
|
||||||
|
|
||||||
interface UploadSarifExpectedResult {
|
interface UploadSarifExpectedResult {
|
||||||
uploadResult?: UploadResult;
|
uploadResult?: UploadResult;
|
||||||
expectedFiles?: string[];
|
expectedFiles?: string[];
|
||||||
@@ -31,6 +118,8 @@ function mockPostProcessSarifFiles() {
|
|||||||
sinon.match.any,
|
sinon.match.any,
|
||||||
sinon.match.any,
|
sinon.match.any,
|
||||||
sinon.match.any,
|
sinon.match.any,
|
||||||
|
sinon.match.any,
|
||||||
|
sinon.match.any,
|
||||||
analysisConfig,
|
analysisConfig,
|
||||||
)
|
)
|
||||||
.resolves({ sarif: { runs: [] }, analysisKey: "", environment: "" });
|
.resolves({ sarif: { runs: [] }, analysisKey: "", environment: "" });
|
||||||
@@ -73,7 +162,9 @@ const postProcessAndUploadSarifMacro = test.macro({
|
|||||||
|
|
||||||
const actual = await postProcessAndUploadSarif(
|
const actual = await postProcessAndUploadSarif(
|
||||||
logger,
|
logger,
|
||||||
|
tempDir,
|
||||||
features,
|
features,
|
||||||
|
async () => getCodeQLForTesting(),
|
||||||
"always",
|
"always",
|
||||||
"",
|
"",
|
||||||
testPath,
|
testPath,
|
||||||
@@ -90,6 +181,8 @@ const postProcessAndUploadSarifMacro = test.macro({
|
|||||||
postProcessSarifFiles.calledWith(
|
postProcessSarifFiles.calledWith(
|
||||||
logger,
|
logger,
|
||||||
features,
|
features,
|
||||||
|
sinon.match.func,
|
||||||
|
tempDir,
|
||||||
sinon.match.any,
|
sinon.match.any,
|
||||||
analysisKindResult.expectedFiles?.map(toFullPath) ??
|
analysisKindResult.expectedFiles?.map(toFullPath) ??
|
||||||
fullSarifPaths,
|
fullSarifPaths,
|
||||||
@@ -221,7 +314,9 @@ test("postProcessAndUploadSarif doesn't upload if upload is disabled", async (t)
|
|||||||
|
|
||||||
const actual = await postProcessAndUploadSarif(
|
const actual = await postProcessAndUploadSarif(
|
||||||
logger,
|
logger,
|
||||||
|
tempDir,
|
||||||
features,
|
features,
|
||||||
|
() => getCodeQLForTesting(),
|
||||||
"never",
|
"never",
|
||||||
"",
|
"",
|
||||||
tempDir,
|
tempDir,
|
||||||
@@ -248,7 +343,9 @@ test("postProcessAndUploadSarif writes post-processed SARIF files if output dire
|
|||||||
const postProcessedOutPath = path.join(tempDir, "post-processed");
|
const postProcessedOutPath = path.join(tempDir, "post-processed");
|
||||||
const actual = await postProcessAndUploadSarif(
|
const actual = await postProcessAndUploadSarif(
|
||||||
logger,
|
logger,
|
||||||
|
tempDir,
|
||||||
features,
|
features,
|
||||||
|
() => getCodeQLForTesting(),
|
||||||
"never",
|
"never",
|
||||||
"",
|
"",
|
||||||
tempDir,
|
tempDir,
|
||||||
|
|||||||
+42
-1
@@ -1,20 +1,57 @@
|
|||||||
import { UploadKind } from "./actions-util";
|
import { UploadKind } from "./actions-util";
|
||||||
import * as analyses from "./analyses";
|
import * as analyses from "./analyses";
|
||||||
|
import type { CodeQL } from "./codeql";
|
||||||
|
import * as codeql from "./codeql";
|
||||||
|
import { Config } from "./config-utils";
|
||||||
import { FeatureEnablement } from "./feature-flags";
|
import { FeatureEnablement } from "./feature-flags";
|
||||||
import { Logger } from "./logging";
|
import { Logger } from "./logging";
|
||||||
import * as upload_lib from "./upload-lib";
|
import * as upload_lib from "./upload-lib";
|
||||||
import { unsafeEntriesInvariant } from "./util";
|
import { GitHubVersion, unsafeEntriesInvariant } from "./util";
|
||||||
|
|
||||||
|
export interface UploadSarifState {
|
||||||
|
/** The cached `CodeQL` instance, if any. */
|
||||||
|
cachedCodeQL: CodeQL | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
// Maps analysis kinds to SARIF IDs.
|
// Maps analysis kinds to SARIF IDs.
|
||||||
export type UploadSarifResults = Partial<
|
export type UploadSarifResults = Partial<
|
||||||
Record<analyses.AnalysisKind, upload_lib.UploadResult>
|
Record<analyses.AnalysisKind, upload_lib.UploadResult>
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
/** Get or initialise a `CodeQL` instance for use by the `upload-sarif` action. */
|
||||||
|
export async function getOrInitCodeQL(
|
||||||
|
actionState: UploadSarifState,
|
||||||
|
logger: Logger,
|
||||||
|
gitHubVersion: GitHubVersion,
|
||||||
|
features: FeatureEnablement,
|
||||||
|
config: Config | undefined,
|
||||||
|
): Promise<CodeQL> {
|
||||||
|
// Return the cached instance, if we have one.
|
||||||
|
if (actionState.cachedCodeQL !== undefined) return actionState.cachedCodeQL;
|
||||||
|
|
||||||
|
// If we have been able to load a `Config` from an earlier CodeQL Action step in the job,
|
||||||
|
// then use the CodeQL executable that we have used previously. Otherwise, initialise the
|
||||||
|
// CLI specifically for `upload-sarif`. Either way, we cache the instance.
|
||||||
|
if (config !== undefined) {
|
||||||
|
actionState.cachedCodeQL = await codeql.getCodeQL(config.codeQLCmd);
|
||||||
|
} else {
|
||||||
|
actionState.cachedCodeQL = await upload_lib.minimalInitCodeQL(
|
||||||
|
logger,
|
||||||
|
gitHubVersion,
|
||||||
|
features,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return actionState.cachedCodeQL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds SARIF files in `sarifPath`, post-processes them, and uploads them to the appropriate services.
|
* Finds SARIF files in `sarifPath`, post-processes them, and uploads them to the appropriate services.
|
||||||
*
|
*
|
||||||
* @param logger The logger to use.
|
* @param logger The logger to use.
|
||||||
|
* @param tempPath The path to the temporary directory.
|
||||||
* @param features Information about enabled features.
|
* @param features Information about enabled features.
|
||||||
|
* @param getCodeQL A function to retrieve a `CodeQL` instance.
|
||||||
* @param uploadKind The kind of upload that is requested.
|
* @param uploadKind The kind of upload that is requested.
|
||||||
* @param checkoutPath The path where the repository was checked out at.
|
* @param checkoutPath The path where the repository was checked out at.
|
||||||
* @param sarifPath The path to the file or directory to upload.
|
* @param sarifPath The path to the file or directory to upload.
|
||||||
@@ -25,7 +62,9 @@ export type UploadSarifResults = Partial<
|
|||||||
*/
|
*/
|
||||||
export async function postProcessAndUploadSarif(
|
export async function postProcessAndUploadSarif(
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
|
tempPath: string,
|
||||||
features: FeatureEnablement,
|
features: FeatureEnablement,
|
||||||
|
getCodeQL: upload_lib.CodeQLGetter,
|
||||||
uploadKind: UploadKind,
|
uploadKind: UploadKind,
|
||||||
checkoutPath: string,
|
checkoutPath: string,
|
||||||
sarifPath: string,
|
sarifPath: string,
|
||||||
@@ -45,6 +84,8 @@ export async function postProcessAndUploadSarif(
|
|||||||
const postProcessingResults = await upload_lib.postProcessSarifFiles(
|
const postProcessingResults = await upload_lib.postProcessSarifFiles(
|
||||||
logger,
|
logger,
|
||||||
features,
|
features,
|
||||||
|
getCodeQL,
|
||||||
|
tempPath,
|
||||||
checkoutPath,
|
checkoutPath,
|
||||||
sarifFiles,
|
sarifFiles,
|
||||||
category,
|
category,
|
||||||
|
|||||||
Reference in New Issue
Block a user