mirror of
https://github.com/github/codeql-action.git
synced 2026-01-04 21:50:17 +08:00
89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
import { ConfigurationError } from "./util";
|
|
|
|
export enum AnalysisKind {
|
|
CodeScanning = "code-scanning",
|
|
CodeQuality = "code-quality",
|
|
}
|
|
|
|
// Exported for testing. A set of all known analysis kinds.
|
|
export const supportedAnalysisKinds = new Set(Object.values(AnalysisKind));
|
|
|
|
/**
|
|
* Parses a comma-separated string into a list of unique analysis kinds.
|
|
* Throws a configuration error if the input contains unknown analysis kinds
|
|
* or doesn't contain at least one element.
|
|
*
|
|
* @param input The comma-separated string to parse.
|
|
* @returns The array of unique analysis kinds that were parsed from the input string.
|
|
*/
|
|
export async function parseAnalysisKinds(
|
|
input: string,
|
|
): Promise<AnalysisKind[]> {
|
|
const components = input.split(",");
|
|
|
|
if (components.length < 1) {
|
|
throw new ConfigurationError(
|
|
"At least one analysis kind must be configured.",
|
|
);
|
|
}
|
|
|
|
for (const component of components) {
|
|
if (!supportedAnalysisKinds.has(component as AnalysisKind)) {
|
|
throw new ConfigurationError(`Unknown analysis kind: ${component}`);
|
|
}
|
|
}
|
|
|
|
// Return all unique elements.
|
|
return Array.from(
|
|
new Set(components.map((component) => component as AnalysisKind)),
|
|
);
|
|
}
|
|
|
|
/** The queries to use for Code Quality analyses. */
|
|
export const codeQualityQueries: string[] = ["code-quality"];
|
|
|
|
// Enumerates API endpoints that accept SARIF files.
|
|
export enum SARIF_UPLOAD_ENDPOINT {
|
|
CODE_SCANNING = "PUT /repos/:owner/:repo/code-scanning/analysis",
|
|
CODE_QUALITY = "PUT /repos/:owner/:repo/code-quality/analysis",
|
|
}
|
|
|
|
// Represents configurations for different analysis kinds.
|
|
export interface AnalysisConfig {
|
|
/** The analysis kind the configuration is for. */
|
|
kind: AnalysisKind;
|
|
/** A display friendly name for logs. */
|
|
name: string;
|
|
/** The API endpoint to upload SARIF files to. */
|
|
target: SARIF_UPLOAD_ENDPOINT;
|
|
/** The file extension for SARIF files generated by this kind of analysis. */
|
|
sarifExtension: string;
|
|
/** A predicate on filenames to decide whether a SARIF file
|
|
* belongs to this kind of analysis. */
|
|
sarifPredicate: (name: string) => boolean;
|
|
/** A prefix for environment variables used to track the uniqueness of SARIF uploads. */
|
|
sentinelPrefix: string;
|
|
}
|
|
|
|
// Represents the Code Scanning analysis configuration.
|
|
export const CodeScanning: AnalysisConfig = {
|
|
kind: AnalysisKind.CodeScanning,
|
|
name: "code scanning",
|
|
target: SARIF_UPLOAD_ENDPOINT.CODE_SCANNING,
|
|
sarifExtension: ".sarif",
|
|
sarifPredicate: (name) =>
|
|
name.endsWith(CodeScanning.sarifExtension) &&
|
|
!CodeQuality.sarifPredicate(name),
|
|
sentinelPrefix: "CODEQL_UPLOAD_SARIF_",
|
|
};
|
|
|
|
// Represents the Code Quality analysis configuration.
|
|
export const CodeQuality: AnalysisConfig = {
|
|
kind: AnalysisKind.CodeQuality,
|
|
name: "code quality",
|
|
target: SARIF_UPLOAD_ENDPOINT.CODE_QUALITY,
|
|
sarifExtension: ".quality.sarif",
|
|
sarifPredicate: (name) => name.endsWith(CodeQuality.sarifExtension),
|
|
sentinelPrefix: "CODEQL_UPLOAD_QUALITY_SARIF_",
|
|
};
|