Address review comments

This commit is contained in:
Angela P Wen 2022-08-11 13:45:26 +02:00
parent 3c4f458a1a
commit 65d6ee0c51
16 changed files with 126 additions and 107 deletions

View file

@ -876,7 +876,6 @@ export async function isAnalyzingDefaultBranch(): Promise<boolean> {
}
export async function printDebugLogs(config: Config) {
core.info("Debug mode is on. Printing CodeQL debug logs...");
for (const language of config.languages) {
const databaseDirectory = getCodeQLDatabasePath(config, language);
const logsDirectory = path.join(databaseDirectory, "log");

View file

@ -34,7 +34,7 @@ async function runWrapper() {
try {
await run(debugArtifacts.uploadSarifDebugArtifact);
} catch (error) {
core.setFailed(`analyze action cleanup failed: ${error}`);
core.setFailed(`analyze post-action step failed: ${error}`);
console.log(error);
}
}

View file

@ -116,9 +116,12 @@ export async function uploadLogsDebugArtifact(config: Config) {
/**
* If a database has not been finalized, we cannot run the `codeql database bundle`
* command in the CLI because it will return an error. Instead we directly zip
* all files in the database folder and upload it as an artifact.
* all files in the database folder and return the path.
*/
async function uploadPartialDatabaseBundle(config: Config, language: Language) {
async function createPartialDatabaseBundle(
config: Config,
language: Language
): Promise<string> {
const databasePath = getCodeQLDatabasePath(config, language);
const databaseBundlePath = path.resolve(
config.dbLocation,
@ -134,11 +137,24 @@ async function uploadPartialDatabaseBundle(config: Config, language: Language) {
const zip = new AdmZip();
zip.addLocalFolder(databasePath);
zip.writeZip(databaseBundlePath);
await uploadDebugArtifacts(
[databaseBundlePath],
config.dbLocation,
config.debugArtifactName
return databaseBundlePath;
}
/**
* Runs `codeql database bundle` command and returns the path.
*/
async function createDatabaseBundleCli(
config: Config,
language: Language
): Promise<string> {
// Otherwise run `codeql database bundle` command.
const databaseBundlePath = await bundleDb(
config,
language,
await getCodeQL(config.codeQLCmd),
`${config.debugDatabaseName}-${language}`
);
return databaseBundlePath;
}
export async function uploadDatabaseBundleDebugArtifact(
@ -146,26 +162,24 @@ export async function uploadDatabaseBundleDebugArtifact(
logger: Logger
) {
for (const language of config.languages) {
if (!dbIsFinalized(config, language, logger)) {
await uploadPartialDatabaseBundle(config, language);
continue;
}
try {
// Otherwise run `codeql database bundle` command.
const bundlePath = await bundleDb(
config,
language,
await getCodeQL(config.codeQLCmd),
`${config.debugDatabaseName}-${language}`
);
let databaseBundlePath;
if (!dbIsFinalized(config, language, logger)) {
databaseBundlePath = await createPartialDatabaseBundle(
config,
language
);
} else {
databaseBundlePath = await createDatabaseBundleCli(config, language);
}
await uploadDebugArtifacts(
[bundlePath],
[databaseBundlePath],
config.dbLocation,
config.debugArtifactName
);
} catch (error) {
core.info(
`Failed to upload database debug bundles for ${config.debugDatabaseName}-${language}: ${error}`
`Failed to upload database debug bundle for ${config.debugDatabaseName}-${language}: ${error}`
);
}
}

View file

@ -45,7 +45,7 @@ async function runWrapper() {
actionsUtil.printDebugLogs
);
} catch (error) {
core.setFailed(`init action cleanup failed: ${error}`);
core.setFailed(`init post-action step failed: ${error}`);
console.log(error);
}
}

View file

@ -442,46 +442,44 @@ for (const [
});
}
test("doesDirectoryExist", (t) => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "does-dir-exist-"));
test("doesDirectoryExist", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const topLevelFile = "top-level-test-file.txt";
fs.writeFileSync(topLevelFile, "");
fs.writeFileSync(`${tmpDir}/nested-test-file.txt`, "");
const topLevelFile = "top-level-test-file.txt";
fs.writeFileSync(topLevelFile, "");
fs.writeFileSync(`${tmpDir}/nested-test-file.txt`, "");
// Returns true if directory
t.true(util.doesDirectoryExist(tmpDir));
// Returns true if directory
t.true(util.doesDirectoryExist(tmpDir));
// Returns false if file
t.false(util.doesDirectoryExist(topLevelFile));
// Returns false if file
t.false(util.doesDirectoryExist(topLevelFile));
// Returns false if no file of this type exists
t.false(util.doesDirectoryExist("non-existent-file.txt"));
// Clean up test files.
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.unlinkSync(topLevelFile);
// Returns false if no file of this type exists
t.false(util.doesDirectoryExist("non-existent-file.txt"));
});
});
test("listFolder", (t) => {
// Returns empty if not a directory
t.deepEqual(util.listFolder("not-a-directory"), []);
test("listFolder", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
// Returns empty if not a directory
t.deepEqual(util.listFolder("not-a-directory"), []);
// Returns empty if directory is empty
const emptyTmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), "list-folder-empty-")
);
t.deepEqual(util.listFolder(emptyTmpDir), []);
fs.rmSync(emptyTmpDir, { recursive: true, force: true });
// Returns empty if directory is empty
const emptyTmpDir = fs.mkdtempSync(
path.join(os.tmpdir(), "list-folder-empty-")
);
t.deepEqual(util.listFolder(emptyTmpDir), []);
fs.rmSync(emptyTmpDir, { recursive: true, force: true });
// Returns all file names in directory
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "list-folder-"));
fs.writeFileSync(`${tmpDir}/test-file-1.txt`, "");
fs.writeFileSync(`${tmpDir}/test-file-2.txt`, "");
fs.writeFileSync(`${tmpDir}/test-file-3.txt`, "");
t.deepEqual(util.listFolder(tmpDir), [
`${tmpDir}/test-file-1.txt`,
`${tmpDir}/test-file-2.txt`,
`${tmpDir}/test-file-3.txt`,
]);
// Returns all file names in directory
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "list-folder-"));
fs.writeFileSync(`${tmpDir}/test-file-1.txt`, "");
fs.writeFileSync(`${tmpDir}/test-file-2.txt`, "");
fs.writeFileSync(`${tmpDir}/test-file-3.txt`, "");
t.deepEqual(util.listFolder(tmpDir), [
`${tmpDir}/test-file-1.txt`,
`${tmpDir}/test-file-2.txt`,
`${tmpDir}/test-file-3.txt`,
]);
});
});