Skip to content

Commit

Permalink
Showing 15 changed files with 113 additions and 100 deletions.
5 changes: 4 additions & 1 deletion lib/analyze-action.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/analyze-action.js.map

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

4 changes: 1 addition & 3 deletions lib/analyze.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/analyze.js.map

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

29 changes: 7 additions & 22 deletions lib/autobuild-action.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/autobuild-action.js.map
30 changes: 18 additions & 12 deletions lib/autobuild.js
2 changes: 1 addition & 1 deletion lib/autobuild.js.map
24 changes: 18 additions & 6 deletions lib/runner.js
2 changes: 1 addition & 1 deletion lib/runner.js.map
7 changes: 5 additions & 2 deletions src/analyze-action.ts
@@ -1,6 +1,7 @@
import * as core from '@actions/core';

import { AnalysisStatusReport, runAnalyze } from './analyze';
import { getConfig } from './config-utils';
import { getActionsLogger } from './logging';
import { parseRepositoryNwo } from './repository';
import * as util from './util';
@@ -29,6 +30,8 @@ async function run() {
if (!await util.sendStatusReport(await util.createStatusReportBase('finish', 'starting', startedAt), true)) {
return;
}
const logger = getActionsLogger();
const config = await getConfig(util.getRequiredEnvParam('RUNNER_TEMP'), logger);
stats = await runAnalyze(
parseRepositoryNwo(util.getRequiredEnvParam('GITHUB_REPOSITORY')),
await util.getCommitOid(),
@@ -43,8 +46,8 @@ async function run() {
core.getInput('upload') === 'true',
'actions',
core.getInput('output'),
util.getRequiredEnvParam('RUNNER_TEMP'),
getActionsLogger());
config,
logger);

} catch (error) {
core.setFailed(error.message);
4 changes: 1 addition & 3 deletions src/analyze.ts
@@ -130,11 +130,9 @@ export async function runAnalyze(
doUpload: boolean,
mode: util.Mode,
outputDir: string,
tempDir: string,
config: configUtils.Config,
logger: Logger): Promise<AnalysisStatusReport> {

const config = await configUtils.getConfig(tempDir, logger);

// Delete the tracer config env var to avoid tracing ourselves
delete process.env[sharedEnv.ODASA_TRACER_CONFIGURATION];

36 changes: 8 additions & 28 deletions src/autobuild-action.ts
@@ -1,8 +1,8 @@
import * as core from '@actions/core';

import { getCodeQL } from './codeql';
import { determineAutobuildLanguage, runAutobuild } from './autobuild';
import * as config_utils from './config-utils';
import { isTracedLanguage } from './languages';
import { Language } from './languages';
import { getActionsLogger } from './logging';
import * as util from './util';

@@ -37,47 +37,27 @@ async function sendCompletedStatusReport(
async function run() {
const logger = getActionsLogger();
const startedAt = new Date();
let language;
let language: Language | undefined = undefined;
try {
util.prepareLocalRunEnvironment();
if (!await util.sendStatusReport(await util.createStatusReportBase('autobuild', 'starting', startedAt), true)) {
return;
}

const config = await config_utils.getConfig(util.getRequiredEnvParam('RUNNER_TEMP'), logger);

// Attempt to find a language to autobuild
// We want pick the dominant language in the repo from the ones we're able to build
// The languages are sorted in order specified by user or by lines of code if we got
// them from the GitHub API, so try to build the first language on the list.
const autobuildLanguages = config.languages.filter(isTracedLanguage);
language = autobuildLanguages[0];

if (!language) {
core.info("None of the languages in this project require extra build steps");
return;
}

core.debug(`Detected dominant traced language: ${language}`);

if (autobuildLanguages.length > 1) {
core.warning(`We will only automatically build ${language} code. If you wish to scan ${autobuildLanguages.slice(1).join(' and ')}, you must replace this block with custom build steps.`);
language = determineAutobuildLanguage(config, logger);
if (language !== undefined) {
await runAutobuild(language, config, logger);
}

core.startGroup(`Attempting to automatically build ${language} code`);
const codeQL = getCodeQL(config.codeQLCmd);
await codeQL.runAutobuild(language);

core.endGroup();

} catch (error) {
core.setFailed("We were unable to automatically build your code. Please replace the call to the autobuild action with your custom build steps. " + error.message);
console.log(error);
await sendCompletedStatusReport(startedAt, [language], language, error);
await sendCompletedStatusReport(startedAt, language ? [language] : [], language, error);
return;
}

await sendCompletedStatusReport(startedAt, [language]);
await sendCompletedStatusReport(startedAt, language ? [language] : []);
}

run().catch(e => {
34 changes: 27 additions & 7 deletions src/autobuild.ts
@@ -3,16 +3,36 @@ import * as config_utils from './config-utils';
import { isTracedLanguage, Language } from './languages';
import { Logger } from './logging';

export async function runAutobuild(
language: Language,
tmpDir: string,
logger: Logger) {
export function determineAutobuildLanguage(
config: config_utils.Config,
logger: Logger
): Language | undefined {

// Attempt to find a language to autobuild
// We want pick the dominant language in the repo from the ones we're able to build
// The languages are sorted in order specified by user or by lines of code if we got
// them from the GitHub API, so try to build the first language on the list.
const autobuildLanguages = config.languages.filter(isTracedLanguage);
const language = autobuildLanguages[0];

if (!language) {
logger.info("None of the languages in this project require extra build steps");
return undefined;
}

if (!isTracedLanguage(language)) {
throw new Error(`Cannot build "${language}" as it is not a traced language`);
logger.debug(`Detected dominant traced language: ${language}`);

if (autobuildLanguages.length > 1) {
logger.warning(`We will only automatically build ${language} code. If you wish to scan ${autobuildLanguages.slice(1).join(' and ')}, you must replace this call with custom build steps.`);
}

const config = await config_utils.getConfig(tmpDir, logger);
return language;
}

export async function runAutobuild(
language: Language,
config: config_utils.Config,
logger: Logger) {

logger.startGroup(`Attempting to automatically build ${language} code`);
const codeQL = getCodeQL(config.codeQLCmd);
30 changes: 19 additions & 11 deletions src/runner.ts
@@ -5,10 +5,11 @@ import * as os from 'os';
import * as path from 'path';

import { runAnalyze } from './analyze';
import { runAutobuild } from './autobuild';
import { determineAutobuildLanguage, runAutobuild } from './autobuild';
import { CodeQL, getCodeQL } from './codeql';
import { getConfig} from './config-utils';
import { initCodeQL, initConfig, runInit } from './init';
import { parseLanguage } from './languages';
import { Language, parseLanguage } from './languages';
import { getRunnerLogger } from './logging';
import { parseRepositoryNwo } from './repository';
import * as upload_lib from './upload-lib';
@@ -170,18 +171,24 @@ interface AutobuildArgs {
program
.command('autobuild')
.description('Attempts to automatically build code')
.requiredOption('--language <language>', 'The language to build')
.option('--language <language>', 'The language to build. By default will try to detect the dominant language.')
.option('--temp-dir <dir>', 'Directory to use for temporary files. By default will use current working directory.')
.action(async (cmd: AutobuildArgs) => {
try {
const language = parseLanguage(cmd.language);
if (language === undefined) {
throw new Error(`"${cmd.language}" is not a recognised language`);
const config = await getConfig(getTempDir(cmd.tempDir), logger);
let language: Language | undefined = undefined;
if (cmd.language !== undefined) {
language = parseLanguage(cmd.language);
if (language === undefined || !config.languages.includes(language)) {
throw new Error(`"${cmd.language}" is not a recognised language. ` +
`Known languages in this project are ${config.languages.join(', ')}.`);
}
} else {
language = determineAutobuildLanguage(config, logger);
}
if (language !== undefined) {
await runAutobuild(language, config, logger);
}
await runAutobuild(
language,
getTempDir(cmd.tempDir),
logger);
} catch (e) {
logger.error('Autobuild failed');
logger.error(e);
@@ -217,6 +224,7 @@ program
try {
const tempDir = getTempDir(cmd.tempDir);
const outputDir = cmd.outputDir || path.join(tempDir, 'codeql-sarif');
const config = await getConfig(getTempDir(cmd.tempDir), logger);
await runAnalyze(
parseRepositoryNwo(cmd.repository),
cmd.commit,
@@ -231,7 +239,7 @@ program
cmd.upload,
'runner',
outputDir,
tempDir,
config,
logger);
} catch (e) {
logger.error('Upload failed');

0 comments on commit a542021

Please sign in to comment.