Merge pull request #3233 from github/mbg/getOptionalEnvVar

Add `getOptionalEnvVar` helper
This commit is contained in:
Michael B. Gale
2025-10-24 16:55:48 +01:00
committed by GitHub
6 changed files with 69 additions and 8 deletions
+9 -2
View File
@@ -89751,6 +89751,13 @@ function getRequiredEnvParam(paramName) {
}
return value;
}
function getOptionalEnvVar(paramName) {
const value = process.env[paramName];
if (value?.trim().length === 0) {
return void 0;
}
return value;
}
var HTTPError = class extends Error {
constructor(message, status) {
super(message);
@@ -95911,8 +95918,8 @@ async function postProcessSarifFiles(logger, features, checkoutPath, sarifPaths,
return { sarif, analysisKey, environment };
}
async function writePostProcessedFiles(logger, pathInput, uploadTarget, postProcessingResults) {
const outputPath = pathInput || process.env["CODEQL_ACTION_SARIF_DUMP_DIR" /* SARIF_DUMP_DIR */];
if (outputPath !== void 0 && outputPath.trim() !== "") {
const outputPath = pathInput || getOptionalEnvVar("CODEQL_ACTION_SARIF_DUMP_DIR" /* SARIF_DUMP_DIR */);
if (outputPath !== void 0) {
dumpSarifFile(
JSON.stringify(postProcessingResults.sarif),
outputPath,
+9 -2
View File
@@ -88330,6 +88330,13 @@ function getRequiredEnvParam(paramName) {
}
return value;
}
function getOptionalEnvVar(paramName) {
const value = process.env[paramName];
if (value?.trim().length === 0) {
return void 0;
}
return value;
}
var HTTPError = class extends Error {
constructor(message, status) {
super(message);
@@ -92725,8 +92732,8 @@ async function postProcessSarifFiles(logger, features, checkoutPath, sarifPaths,
return { sarif, analysisKey, environment };
}
async function writePostProcessedFiles(logger, pathInput, uploadTarget, postProcessingResults) {
const outputPath = pathInput || process.env["CODEQL_ACTION_SARIF_DUMP_DIR" /* SARIF_DUMP_DIR */];
if (outputPath !== void 0 && outputPath.trim() !== "") {
const outputPath = pathInput || getOptionalEnvVar("CODEQL_ACTION_SARIF_DUMP_DIR" /* SARIF_DUMP_DIR */);
if (outputPath !== void 0) {
dumpSarifFile(
JSON.stringify(postProcessingResults.sarif),
outputPath,
+9 -2
View File
@@ -88444,6 +88444,13 @@ function getRequiredEnvParam(paramName) {
}
return value;
}
function getOptionalEnvVar(paramName) {
const value = process.env[paramName];
if (value?.trim().length === 0) {
return void 0;
}
return value;
}
var HTTPError = class extends Error {
constructor(message, status) {
super(message);
@@ -93381,8 +93388,8 @@ async function postProcessSarifFiles(logger, features, checkoutPath, sarifPaths,
return { sarif, analysisKey, environment };
}
async function writePostProcessedFiles(logger, pathInput, uploadTarget, postProcessingResults) {
const outputPath = pathInput || process.env["CODEQL_ACTION_SARIF_DUMP_DIR" /* SARIF_DUMP_DIR */];
if (outputPath !== void 0 && outputPath.trim() !== "") {
const outputPath = pathInput || getOptionalEnvVar("CODEQL_ACTION_SARIF_DUMP_DIR" /* SARIF_DUMP_DIR */);
if (outputPath !== void 0) {
dumpSarifFile(
JSON.stringify(postProcessingResults.sarif),
outputPath,
+2 -2
View File
@@ -747,10 +747,10 @@ export async function writePostProcessedFiles(
postProcessingResults: PostProcessingResults,
) {
// If there's an explicit input, use that. Otherwise, use the value from the environment variable.
const outputPath = pathInput || process.env[EnvVar.SARIF_DUMP_DIR];
const outputPath = pathInput || util.getOptionalEnvVar(EnvVar.SARIF_DUMP_DIR);
// If we have a non-empty output path, write the SARIF file to it.
if (outputPath !== undefined && outputPath.trim() !== "") {
if (outputPath !== undefined) {
dumpSarifFile(
JSON.stringify(postProcessingResults.sarif),
outputPath,
+29
View File
@@ -252,6 +252,35 @@ test("allowed API versions", async (t) => {
);
});
test("getRequiredEnvParam - gets environment variables", (t) => {
process.env.SOME_UNIT_TEST_VAR = "foo";
const result = util.getRequiredEnvParam("SOME_UNIT_TEST_VAR");
t.is(result, "foo");
});
test("getRequiredEnvParam - throws if an environment variable isn't set", (t) => {
t.throws(() => util.getRequiredEnvParam("SOME_UNIT_TEST_VAR"));
});
test("getOptionalEnvVar - gets environment variables", (t) => {
process.env.SOME_UNIT_TEST_VAR = "foo";
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
t.is(result, "foo");
});
test("getOptionalEnvVar - gets undefined for empty environment variables", (t) => {
process.env.SOME_UNIT_TEST_VAR = "";
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
t.is(result, undefined);
});
test("getOptionalEnvVar - doesn't throw for undefined environment variables", (t) => {
t.notThrows(() => {
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
t.is(result, undefined);
});
});
test("doesDirectoryExist", async (t) => {
// Returns false if no file/dir of this name exists
t.false(util.doesDirectoryExist("non-existent-file.txt"));
+11
View File
@@ -673,6 +673,17 @@ export function getRequiredEnvParam(paramName: string): string {
return value;
}
/**
* Get an environment variable, but return `undefined` if it is not set or empty.
*/
export function getOptionalEnvVar(paramName: string): string | undefined {
const value = process.env[paramName];
if (value?.trim().length === 0) {
return undefined;
}
return value;
}
export class HTTPError extends Error {
public status: number;