Skip to content

Commit

Permalink
Showing 6 changed files with 137 additions and 27 deletions.
30 changes: 17 additions & 13 deletions lib/config-utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/config-utils.js.map

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions lib/config-utils.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/config-utils.test.js.map

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions src/config-utils.test.ts
@@ -254,6 +254,58 @@ test("default queries are used", async t => {
});
});

test("Queries can be specified in config file", async t => {
return await util.withTmpDir(async tmpDir => {
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;

const inputFileContents = `
name: my config
queries:
- uses: ./foo`;

fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');

fs.mkdirSync(path.join(tmpDir, 'foo'));

const resolveQueriesArgs: {queries: string[], extraSearchPath: string | undefined}[] = [];
CodeQL.setCodeQL({
resolveQueries: async function(queries: string[], extraSearchPath: string | undefined) {
resolveQueriesArgs.push({queries, extraSearchPath});
// Return what we're given, just in the right format for a resolved query
// This way we can test by seeing which returned items are in the final
// configuration.
const dummyResolvedQueries = {};
queries.forEach(q => { dummyResolvedQueries[q] = {}; });
return {
byLanguage: {
'javascript': dummyResolvedQueries,
},
noDeclaredLanguage: {},
multipleDeclaredLanguages: {},
};
},
});

setInput('languages', 'javascript');

const config = await configUtils.initConfig();

// 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.regex(resolveQueriesArgs[1].queries[0], /.*\/foo$/);

// Now check that the end result contains the default queries and the query from config
t.deepEqual(config.queries['javascript'].length, 2);
t.regex(config.queries['javascript'][0], /javascript-code-scanning.qls$/);
t.regex(config.queries['javascript'][1], /.*\/foo$/);
});
});

test("Queries from config file can be overridden in workflow file", async t => {
return await util.withTmpDir(async tmpDir => {
process.env['RUNNER_TEMP'] = tmpDir;
35 changes: 23 additions & 12 deletions src/config-utils.ts
@@ -472,19 +472,34 @@ async function getLanguages(): Promise<string[]> {
}

/**
* Get the default config for when the user has not supplied one.
* Returns true if queries were provided in the workflow file
* (and thus added), otherwise false
*/
export async function getDefaultConfig(): Promise<Config> {
const languages = await getLanguages();
const queries = {};
await addDefaultQueries(languages, queries);
function addQueriesFromWorkflowIfRequired(
configFile: string,
languages: string[],
resultMap: { [language: string]: string[] }
): boolean {
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async query => {
await parseQueryUses('', languages, queries, query);
await parseQueryUses(configFile, languages, resultMap, query);
});
return true;
}

return false;
}

/**
* Get the default config for when the user has not supplied one.
*/
export async function getDefaultConfig(): Promise<Config> {
const languages = await getLanguages();
const queries = {};
await addDefaultQueries(languages, queries);
addQueriesFromWorkflowIfRequired('', languages, queries);

return {
languages: languages,
queries: queries,
@@ -545,12 +560,8 @@ async function loadConfig(configFile: string): Promise<Config> {

// If queries were provided using `with` in the action configuration,
// they should take precedence over the queries in the config file
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async query => {
await parseQueryUses(configFile, languages, queries, query);
});
} else if (QUERIES_PROPERTY in parsedYAML) {
const addedQueriesFromAction = addQueriesFromWorkflowIfRequired(configFile, languages, queries);
if (!addedQueriesFromAction && QUERIES_PROPERTY in parsedYAML) {
if (!(parsedYAML[QUERIES_PROPERTY] instanceof Array)) {
throw new Error(getQueriesInvalid(configFile));
}

0 comments on commit 7f19f91

Please sign in to comment.