- Add new configuration Parameter

- Write test to check it is read from configuration
- Update documentation
This commit is contained in:
Tugdual Grall 2023-03-18 06:37:28 +00:00
parent fb75ebd750
commit ee44252240
16 changed files with 1644 additions and 31 deletions

View file

@ -105,6 +105,7 @@ test("load empty config", async (t) => {
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -176,6 +177,7 @@ test("loading config saves config", async (t) => {
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -214,6 +216,7 @@ test("load input outside of workspace", async (t) => {
undefined,
"../input",
undefined,
undefined,
false,
false,
"",
@ -254,6 +257,7 @@ test("load non-local input with invalid repo syntax", async (t) => {
undefined,
configFile,
undefined,
undefined,
false,
false,
"",
@ -295,6 +299,7 @@ test("load non-existent input", async (t) => {
undefined,
configFile,
undefined,
undefined,
false,
false,
"",
@ -402,6 +407,7 @@ test("load non-empty input", async (t) => {
undefined,
configFilePath,
undefined,
undefined,
false,
false,
"my-artifact",
@ -473,6 +479,7 @@ test("Default queries are used", async (t) => {
undefined,
configFilePath,
undefined,
undefined,
false,
false,
"",
@ -552,6 +559,7 @@ test("Queries can be specified in config file", async (t) => {
undefined,
configFilePath,
undefined,
undefined,
false,
false,
"",
@ -630,6 +638,7 @@ test("Queries from config file can be overridden in workflow file", async (t) =>
undefined,
configFilePath,
undefined,
undefined,
false,
false,
"",
@ -706,6 +715,7 @@ test("Queries in workflow file can be used in tandem with the 'disable default q
undefined,
configFilePath,
undefined,
undefined,
false,
false,
"",
@ -773,6 +783,7 @@ test("Multiple queries can be specified in workflow file, no config file require
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -861,6 +872,7 @@ test("Queries in workflow file can be added to the set of queries without overri
undefined,
configFilePath,
undefined,
undefined,
false,
false,
"",
@ -913,6 +925,91 @@ test("Queries in workflow file can be added to the set of queries without overri
});
});
test("Queries can be specified in configuration, same as file", async (t) => {
return await util.withTmpDir(async (tmpDir) => {
const inputFileContents = `
name: my config
queries:
- uses: ./foo
packs:
javascript:
- a/b@1.2.3
python:
- c/d@1.2.3
`;
fs.mkdirSync(path.join(tmpDir, "foo"));
const resolveQueriesArgs: Array<{
queries: string[];
extraSearchPath: string | undefined;
}> = [];
const codeQL = setCodeQL({
async resolveQueries(
queries: string[],
extraSearchPath: string | undefined
) {
resolveQueriesArgs.push({ queries, extraSearchPath });
return queriesToResolvedQueryForm(queries);
},
async packDownload(): Promise<PackDownloadOutput> {
return { packs: [] };
},
});
// Only JS, python packs will be ignored
const languages = "javascript";
const config = await configUtils.initConfig(
languages,
undefined,
undefined,
undefined,
undefined,
undefined,
inputFileContents,
false,
false,
"",
"",
{ owner: "github", repo: "example " },
tmpDir,
codeQL,
tmpDir,
gitHubVersion,
sampleApiDetails,
createFeatures([]),
getRunnerLogger(true)
);
// Check resolveQueries was called correctly
// It'll be called once for the default queries
// and once for `./foo` from the config file.
t.deepEqual(resolveQueriesArgs.length, 2);
t.deepEqual(resolveQueriesArgs[1].queries.length, 1);
t.true(resolveQueriesArgs[1].queries[0].endsWith(`${path.sep}foo`));
t.deepEqual(config.packs as unknown, {
[Language.javascript]: ["a/b@1.2.3"],
});
// Now check that the end result contains the default queries and the query from config
t.deepEqual(config.queries["javascript"].builtin.length, 1);
t.deepEqual(config.queries["javascript"].custom.length, 1);
t.true(
config.queries["javascript"].builtin[0].endsWith(
"javascript-code-scanning.qls"
)
);
t.true(
config.queries["javascript"].custom[0].queries[0].endsWith(
`${path.sep}foo`
)
);
});
});
test("Invalid queries in workflow file handled correctly", async (t) => {
return await util.withTmpDir(async (tmpDir) => {
const queries = "foo/bar@v1@v3";
@ -943,6 +1040,7 @@ test("Invalid queries in workflow file handled correctly", async (t) => {
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -1015,6 +1113,7 @@ test("API client used when reading remote config", async (t) => {
undefined,
configFile,
undefined,
undefined,
false,
false,
"",
@ -1046,6 +1145,7 @@ test("Remote config handles the case where a directory is provided", async (t) =
undefined,
repoReference,
undefined,
undefined,
false,
false,
"",
@ -1085,6 +1185,7 @@ test("Invalid format of remote config handled correctly", async (t) => {
undefined,
repoReference,
undefined,
undefined,
false,
false,
"",
@ -1128,6 +1229,7 @@ test("No detected languages", async (t) => {
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -1160,6 +1262,7 @@ test("Unknown languages", async (t) => {
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -1217,6 +1320,7 @@ test("Config specifies packages", async (t) => {
undefined,
configFile,
undefined,
undefined,
false,
false,
"",
@ -1278,6 +1382,7 @@ test("Config specifies packages for multiple languages", async (t) => {
undefined,
configFile,
undefined,
undefined,
false,
false,
"",
@ -1350,6 +1455,7 @@ function doInvalidInputTest(
undefined,
configFile,
undefined,
undefined,
false,
false,
"",
@ -1934,6 +2040,7 @@ const mlPoweredQueriesMacro = test.macro({
undefined,
undefined,
undefined,
undefined,
false,
false,
"",
@ -2650,3 +2757,5 @@ const mockRepositoryNwo = parseRepositoryNwo("owner/repo");
t.deepEqual(mockRequest.called, args.expectedApiCall);
});
});

View file

@ -1690,6 +1690,7 @@ export async function initConfig(
registriesInput: string | undefined,
configFile: string | undefined,
dbLocation: string | undefined,
configuration: string | undefined,
trapCachingEnabled: boolean,
debugMode: boolean,
debugArtifactName: string,
@ -1705,6 +1706,15 @@ export async function initConfig(
): Promise<Config> {
let config: Config;
// if configuration is set, it takes precedence over configFile
if (configuration) {
const configFileToCreate = path.resolve(workspacePath, "user-config-from-action.yml");
fs.writeFileSync(configFileToCreate, configuration);
configFile = configFileToCreate;
logger.debug(`Using configuration from action input: ${configFile}`);
}
// If no config file was provided create an empty one
if (!configFile) {
logger.debug("No configuration file was provided");
@ -1748,6 +1758,7 @@ export async function initConfig(
);
}
// When using the codescanning config in the CLI, pack downloads
// happen in the CLI during the `database init` command, so no need
// to download them here.

View file

@ -256,6 +256,7 @@ async function run() {
registriesInput,
getOptionalInput("config-file"),
getOptionalInput("db-location"),
getOptionalInput("configuration"),
getTrapCachingEnabled(),
// Debug mode is enabled if:
// - The `init` Action is passed `debug: true`.

View file

@ -58,6 +58,7 @@ export async function initConfig(
registriesInput: string | undefined,
configFile: string | undefined,
dbLocation: string | undefined,
queryFilters: string | undefined,
trapCachingEnabled: boolean,
debugMode: boolean,
debugArtifactName: string,
@ -79,6 +80,7 @@ export async function initConfig(
registriesInput,
configFile,
dbLocation,
queryFilters,
trapCachingEnabled,
debugMode,
debugArtifactName,
@ -108,7 +110,6 @@ export async function runInit(
logger: Logger
): Promise<TracerConfig | undefined> {
fs.mkdirSync(config.dbLocation, { recursive: true });
try {
if (await codeQlVersionAbove(codeql, CODEQL_VERSION_NEW_TRACING)) {
// When parsing the codeql config in the CLI, we have not yet created the qlconfig file.