Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #140 from github/remove_some_shared_env
Remove some shared env
Robert authored and GitHub committed Aug 10, 2020

Unverified

No user is associated with the committer email.
2 parents 6e18b27 + eb4eda5 commit d5693a7
Showing 22 changed files with 90 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration-testing.yml
@@ -22,7 +22,7 @@ jobs:
env:
TEST_MODE: true
- run: |
cd "$CODEQL_ACTION_DATABASE_DIR"
cd "$RUNNER_TEMP/codeql_databases"
# List all directories as there will be precisely one directory per database
# but there may be other files in this directory such as query suites.
if [ "$(ls -d */ | wc -l)" != 6 ] || \
9 changes: 4 additions & 5 deletions lib/autobuild.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.js.map

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

8 changes: 8 additions & 0 deletions lib/codeql.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/codeql.js.map

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions lib/finalize-db.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/finalize-db.js.map

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

28 changes: 8 additions & 20 deletions lib/setup-tracer.js
2 changes: 1 addition & 1 deletion lib/setup-tracer.js.map

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions lib/shared-environment.js
2 changes: 1 addition & 1 deletion lib/shared-environment.js.map
3 changes: 1 addition & 2 deletions lib/upload-sarif.js
2 changes: 1 addition & 1 deletion lib/upload-sarif.js.map
34 changes: 10 additions & 24 deletions lib/util.js
2 changes: 1 addition & 1 deletion lib/util.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/autobuild.ts
@@ -1,7 +1,7 @@
import * as core from '@actions/core';

import { getCodeQL } from './codeql';
import * as sharedEnv from './shared-environment';
import { getCodeQL, isTracedLanguage } from './codeql';
import * as config_utils from './config-utils';
import * as util from './util';

interface AutobuildStatusReport extends util.StatusReportBase {
@@ -37,16 +37,17 @@ async function run() {
let language;
try {
util.prepareLocalRunEnvironment();
if (util.should_abort('autobuild', true) ||
!await util.sendStatusReport(await util.createStatusReportBase('autobuild', 'starting', startedAt), true)) {
if (!await util.sendStatusReport(await util.createStatusReportBase('autobuild', 'starting', startedAt), true)) {
return;
}

const config = await config_utils.getConfig();

// 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 = process.env[sharedEnv.CODEQL_ACTION_TRACED_LANGUAGES]?.split(',') || [];
const autobuildLanguages = config.languages.filter(isTracedLanguage);
language = autobuildLanguages[0];

if (!language) {
8 changes: 8 additions & 0 deletions src/codeql.ts
@@ -390,3 +390,11 @@ function getCodeQLForCmd(cmd: string): CodeQL {
}
};
}

export function isTracedLanguage(language: string): boolean {
return ['cpp', 'java', 'csharp'].includes(language);
}

export function isScannedLanguage(language: string): boolean {
return !isTracedLanguage(language);
}
18 changes: 8 additions & 10 deletions src/finalize-db.ts
@@ -3,7 +3,7 @@ import * as io from '@actions/io';
import * as fs from 'fs';
import * as path from 'path';

import { getCodeQL } from './codeql';
import { getCodeQL, isScannedLanguage } from './codeql';
import * as configUtils from './config-utils';
import * as sharedEnv from './shared-environment';
import * as upload_lib from './upload-lib';
@@ -56,11 +56,10 @@ async function sendStatusReport(
await util.sendStatusReport(statusReport);
}

async function createdDBForScannedLanguages(databaseFolder: string) {
const scannedLanguages = process.env[sharedEnv.CODEQL_ACTION_SCANNED_LANGUAGES];
if (scannedLanguages) {
const codeql = getCodeQL();
for (const language of scannedLanguages.split(',')) {
async function createdDBForScannedLanguages(databaseFolder: string, config: configUtils.Config) {
const codeql = getCodeQL();
for (const language of config.languages) {
if (isScannedLanguage(language)) {
core.startGroup('Extracting ' + language);
await codeql.extractScannedLanguage(path.join(databaseFolder, language), language);
core.endGroup();
@@ -69,7 +68,7 @@ async function createdDBForScannedLanguages(databaseFolder: string) {
}

async function finalizeDatabaseCreation(databaseFolder: string, config: configUtils.Config) {
await createdDBForScannedLanguages(databaseFolder);
await createdDBForScannedLanguages(databaseFolder, config);

const codeql = getCodeQL();
for (const language of config.languages) {
@@ -126,16 +125,15 @@ async function run() {
let uploadStats: upload_lib.UploadStatusReport | undefined = undefined;
try {
util.prepareLocalRunEnvironment();
if (util.should_abort('finish', true) ||
!await util.sendStatusReport(await util.createStatusReportBase('finish', 'starting', startedAt), true)) {
if (!await util.sendStatusReport(await util.createStatusReportBase('finish', 'starting', startedAt), true)) {
return;
}
const config = await configUtils.getConfig();

core.exportVariable(sharedEnv.ODASA_TRACER_CONFIGURATION, '');
delete process.env[sharedEnv.ODASA_TRACER_CONFIGURATION];

const databaseFolder = util.getRequiredEnvParam(sharedEnv.CODEQL_ACTION_DATABASE_DIR);
const databaseFolder = util.getCodeQLDatabasesDir();

const sarifFolder = core.getInput('output');
await io.mkdirP(sarifFolder);
34 changes: 10 additions & 24 deletions src/setup-tracer.ts
@@ -5,9 +5,8 @@ import * as fs from 'fs';
import * as path from 'path';

import * as analysisPaths from './analysis-paths';
import { CodeQL, setupCodeQL } from './codeql';
import { CodeQL, isTracedLanguage, setupCodeQL } from './codeql';
import * as configUtils from './config-utils';
import * as sharedEnv from './shared-environment';
import * as util from './util';

type TracerConfig = {
@@ -54,15 +53,15 @@ async function tracerConfig(
return info;
}

function concatTracerConfigs(configs: { [lang: string]: TracerConfig }): TracerConfig {
function concatTracerConfigs(configs: TracerConfig[]): TracerConfig {
// A tracer config is a map containing additional environment variables and a tracer 'spec' file.
// A tracer 'spec' file has the following format [log_file, number_of_blocks, blocks_text]

// Merge the environments
const env: { [key: string]: string; } = {};
let copyExecutables = false;
let envSize = 0;
for (let v of Object.values(configs)) {
for (const v of configs) {
for (let e of Object.entries(v.env)) {
const name = e[0];
const value = e[1];
@@ -178,8 +177,7 @@ async function run() {

try {
util.prepareLocalRunEnvironment();
if (util.should_abort('init', false) ||
!await util.sendStatusReport(await util.createStatusReportBase('init', 'starting', startedAt), true)) {
if (!await util.sendStatusReport(await util.createStatusReportBase('init', 'starting', startedAt), true)) {
return;
}

@@ -215,28 +213,24 @@ async function run() {
const codeqlRam = process.env['CODEQL_RAM'] || '6500';
core.exportVariable('CODEQL_RAM', codeqlRam);

const databaseFolder = path.resolve(util.getRequiredEnvParam('RUNNER_TEMP'), 'codeql_databases');
const databaseFolder = util.getCodeQLDatabasesDir();
await io.mkdirP(databaseFolder);

let tracedLanguages: { [key: string]: TracerConfig } = {};
let scannedLanguages: string[] = [];
let tracedLanguageConfigs: TracerConfig[] = [];
// TODO: replace this code once CodeQL supports multi-language tracing
for (let language of config.languages) {
const languageDatabase = path.join(databaseFolder, language);

// Init language database
await codeql.databaseInit(languageDatabase, language, sourceRoot);
// TODO: add better detection of 'traced languages' instead of using a hard coded list
if (['cpp', 'java', 'csharp'].includes(language)) {
if (isTracedLanguage(language)) {
const config: TracerConfig = await tracerConfig(codeql, languageDatabase);
tracedLanguages[language] = config;
} else {
scannedLanguages.push(language);
tracedLanguageConfigs.push(config);
}
}
const tracedLanguageKeys = Object.keys(tracedLanguages);
if (tracedLanguageKeys.length > 0) {
const mainTracerConfig = concatTracerConfigs(tracedLanguages);
if (tracedLanguageConfigs.length > 0) {
const mainTracerConfig = concatTracerConfigs(tracedLanguageConfigs);
if (mainTracerConfig.spec) {
for (let entry of Object.entries(mainTracerConfig.env)) {
core.exportVariable(entry[0], entry[1]);
@@ -260,13 +254,6 @@ async function run() {
}
}
}

core.exportVariable(sharedEnv.CODEQL_ACTION_SCANNED_LANGUAGES, scannedLanguages.join(','));
core.exportVariable(sharedEnv.CODEQL_ACTION_TRACED_LANGUAGES, tracedLanguageKeys.join(','));

// TODO: make this a "private" environment variable of the action
core.exportVariable(sharedEnv.CODEQL_ACTION_DATABASE_DIR, databaseFolder);

} catch (error) {
core.setFailed(error.message);
console.log(error);
@@ -279,7 +266,6 @@ async function run() {
return;
}
await sendSuccessStatusReport(startedAt, config);
core.exportVariable(sharedEnv.CODEQL_ACTION_INIT_COMPLETED, 'true');
}

run().catch(e => {
6 changes: 0 additions & 6 deletions src/shared-environment.ts
@@ -1,13 +1,7 @@
export const CODEQL_ACTION_DATABASE_DIR = 'CODEQL_ACTION_DATABASE_DIR';
export const CODEQL_ACTION_ANALYSIS_KEY = 'CODEQL_ACTION_ANALYSIS_KEY';
export const ODASA_TRACER_CONFIGURATION = 'ODASA_TRACER_CONFIGURATION';
export const CODEQL_ACTION_SCANNED_LANGUAGES = 'CODEQL_ACTION_SCANNED_LANGUAGES';
export const CODEQL_ACTION_TRACED_LANGUAGES = 'CODEQL_ACTION_TRACED_LANGUAGES';
// The time at which the first action (normally init) started executing.
// If a workflow invokes a different action without first invoking the init
// action (i.e. the upload action is being used by a third-party integrator)
// then this variable will be assigned the start time of the action invoked
// rather that the init action.
export const CODEQL_WORKFLOW_STARTED_AT = 'CODEQL_WORKFLOW_STARTED_AT';
// Populated when the init action completes successfully
export const CODEQL_ACTION_INIT_COMPLETED = 'CODEQL_ACTION_INIT_COMPLETED';
3 changes: 1 addition & 2 deletions src/upload-sarif.ts
@@ -16,8 +16,7 @@ async function sendSuccessStatusReport(startedAt: Date, uploadStats: upload_lib.

async function run() {
const startedAt = new Date();
if (util.should_abort('upload-sarif', false) ||
!await util.sendStatusReport(await util.createStatusReportBase('upload-sarif', 'starting', startedAt), true)) {
if (!await util.sendStatusReport(await util.createStatusReportBase('upload-sarif', 'starting', startedAt), true)) {
return;
}

38 changes: 11 additions & 27 deletions src/util.ts
@@ -27,31 +27,6 @@ export function isEnterprise(): boolean {
return getInstanceAPIURL() !== GITHUB_DOTCOM_API_URL;
}

/**
* Should the current action be aborted?
*
* This method should be called at the start of all CodeQL actions and they
* should abort cleanly if this returns true without failing the action.
* This method will call `core.setFailed` if necessary.
*/
export function should_abort(actionName: string, requireInitActionHasRun: boolean): boolean {

// Check that required aspects of the environment are present
const ref = process.env['GITHUB_REF'];
if (ref === undefined) {
core.setFailed('GITHUB_REF must be set.');
return true;
}

// If the init action is required, then check the it completed successfully.
if (requireInitActionHasRun && process.env[sharedEnv.CODEQL_ACTION_INIT_COMPLETED] === undefined) {
core.setFailed('The CodeQL ' + actionName + ' action cannot be used unless the CodeQL init action is run first. Aborting.');
return true;
}

return false;
}

/**
* Get an environment parameter, but throw an error if it is not set.
*/
@@ -144,7 +119,9 @@ async function getWorkflowPath(): Promise<string> {
* the github API, but after that the result will be cached.
*/
export async function getAnalysisKey(): Promise<string> {
let analysisKey = process.env[sharedEnv.CODEQL_ACTION_ANALYSIS_KEY];
const analysisKeyEnvVar = 'CODEQL_ACTION_ANALYSIS_KEY';

let analysisKey = process.env[analysisKeyEnvVar];
if (analysisKey !== undefined) {
return analysisKey;
}
@@ -153,7 +130,7 @@ export async function getAnalysisKey(): Promise<string> {
const jobName = getRequiredEnvParam('GITHUB_JOB');

analysisKey = workflowPath + ':' + jobName;
core.exportVariable(sharedEnv.CODEQL_ACTION_ANALYSIS_KEY, analysisKey);
core.exportVariable(analysisKeyEnvVar, analysisKey);
return analysisKey;
}

@@ -413,3 +390,10 @@ export function getThreadsFlag(): string {
}
return `--threads=${numThreads}`;
}

/**
* Get the directory where CodeQL databases should be placed.
*/
export function getCodeQLDatabasesDir() {
return path.resolve(getRequiredEnvParam('RUNNER_TEMP'), 'codeql_databases');
}

0 comments on commit d5693a7

Please sign in to comment.