Merge branch 'main' into ghae-endpoint

This commit is contained in:
Chris Gavin 2021-02-16 10:17:25 +00:00 committed by GitHub
commit 3c63623824
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 125 additions and 64 deletions

View file

@ -45,6 +45,13 @@ export function getRequiredEnvParam(paramName: string): string {
return value;
}
export function getTemporaryDirectory(): string {
const value = process.env["CODEQL_ACTION_TEMP"];
return value !== undefined && value !== ""
? value
: getRequiredEnvParam("RUNNER_TEMP");
}
/**
* Ensures all required environment variables are set in the context of a local run.
*/

View file

@ -19,7 +19,7 @@ test("emptyPaths", async (t) => {
tempDir: tmpDir,
toolCacheDir: tmpDir,
codeQLCmd: "",
gitHubVersion: { type: "dotcom" } as util.GitHubVersion,
gitHubVersion: { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion,
};
analysisPaths.includeAndExcludeAnalysisPaths(config);
t.is(process.env["LGTM_INDEX_INCLUDE"], undefined);
@ -39,7 +39,7 @@ test("nonEmptyPaths", async (t) => {
tempDir: tmpDir,
toolCacheDir: tmpDir,
codeQLCmd: "",
gitHubVersion: { type: "dotcom" } as util.GitHubVersion,
gitHubVersion: { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion,
};
analysisPaths.includeAndExcludeAnalysisPaths(config);
t.is(process.env["LGTM_INDEX_INCLUDE"], "path1\npath2");
@ -63,7 +63,7 @@ test("exclude temp dir", async (t) => {
tempDir,
toolCacheDir,
codeQLCmd: "",
gitHubVersion: { type: "dotcom" } as util.GitHubVersion,
gitHubVersion: { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion,
};
analysisPaths.includeAndExcludeAnalysisPaths(config);
t.is(process.env["LGTM_INDEX_INCLUDE"], undefined);

View file

@ -63,10 +63,7 @@ async function run() {
return;
}
const logger = getActionsLogger();
config = await getConfig(
actionsUtil.getRequiredEnvParam("RUNNER_TEMP"),
logger
);
config = await getConfig(actionsUtil.getTemporaryDirectory(), logger);
if (config === undefined) {
throw new Error(
"Config file could not be found at expected location. Has the 'init' action been called?"

View file

@ -34,7 +34,9 @@ test("status report fields", async (t) => {
tempDir: tmpDir,
toolCacheDir: tmpDir,
codeQLCmd: "",
gitHubVersion: { type: "dotcom" } as util.GitHubVersion,
gitHubVersion: {
type: util.GitHubVariant.DOTCOM,
} as util.GitHubVersion,
};
fs.mkdirSync(util.getCodeQLDatabasePath(config.tempDir, language), {
recursive: true,

View file

@ -57,7 +57,7 @@ async function run() {
}
const config = await config_utils.getConfig(
actionsUtil.getRequiredEnvParam("RUNNER_TEMP"),
actionsUtil.getTemporaryDirectory(),
logger
);
if (config === undefined) {

View file

@ -21,7 +21,7 @@ const sampleApiDetails = {
url: "https://github.example.com",
};
const gitHubVersion = { type: "dotcom" } as util.GitHubVersion;
const gitHubVersion = { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion;
// Returns the filepath of the newly-created file
function createConfigFile(inputFileContents: string, tmpDir: string): string {

View file

@ -95,7 +95,7 @@ export interface Config {
codeQLCmd: string;
/**
* Version of GHES that we have determined that we are talking to, or undefined
* if talking to github.com.
* if talking to github.com or GitHub AE.
*/
gitHubVersion: GitHubVersion;
}

View file

@ -126,7 +126,7 @@ async function run() {
const initCodeQLResult = await initCodeQL(
actionsUtil.getOptionalInput("tools"),
apiDetails,
actionsUtil.getRequiredEnvParam("RUNNER_TEMP"),
actionsUtil.getTemporaryDirectory(),
actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"),
"actions",
logger
@ -139,7 +139,7 @@ async function run() {
actionsUtil.getOptionalInput("queries"),
actionsUtil.getOptionalInput("config-file"),
parseRepositoryNwo(actionsUtil.getRequiredEnvParam("GITHUB_REPOSITORY")),
actionsUtil.getRequiredEnvParam("RUNNER_TEMP"),
actionsUtil.getTemporaryDirectory(),
actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"),
codeql,
actionsUtil.getRequiredEnvParam("GITHUB_WORKSPACE"),

View file

@ -26,7 +26,7 @@ function getTestConfig(tmpDir: string): configUtils.Config {
tempDir: tmpDir,
toolCacheDir: tmpDir,
codeQLCmd: "",
gitHubVersion: { type: "dotcom" } as util.GitHubVersion,
gitHubVersion: { type: util.GitHubVariant.DOTCOM } as util.GitHubVersion,
};
}

View file

@ -6,7 +6,7 @@ import test from "ava";
import { getRunnerLogger } from "./logging";
import { setupTests } from "./testing-utils";
import * as uploadLib from "./upload-lib";
import { GitHubVersion, withTmpDir } from "./util";
import { GitHubVersion, GitHubVariant, withTmpDir } from "./util";
setupTests(test);
@ -26,12 +26,12 @@ test("validateSarifFileSchema - invalid", (t) => {
test("validate correct payload used per version", async (t) => {
const newVersions: GitHubVersion[] = [
{ type: "dotcom" },
{ type: "ghes", version: "3.1.0" },
{ type: GitHubVariant.DOTCOM },
{ type: GitHubVariant.GHES, version: "3.1.0" },
];
const oldVersions: GitHubVersion[] = [
{ type: "ghes", version: "2.22.1" },
{ type: "ghes", version: "3.0.0" },
{ type: GitHubVariant.GHES, version: "2.22.1" },
{ type: GitHubVariant.GHES, version: "3.0.0" },
];
const allVersions = newVersions.concat(oldVersions);

View file

@ -241,7 +241,7 @@ export function buildPayload(
// This behaviour can be made the default when support for GHES 3.0 is discontinued.
if (
gitHubVersion.type === "dotcom" ||
gitHubVersion.type !== util.GitHubVariant.GHES ||
semver.satisfies(gitHubVersion.version, `>=3.1`)
) {
if (

View file

@ -221,19 +221,26 @@ test("getGitHubVersion", async (t) => {
auth: "",
url: "https://github.com",
});
t.deepEqual("dotcom", v.type);
t.deepEqual(util.GitHubVariant.DOTCOM, v.type);
mockGetMetaVersionHeader("2.0");
const v2 = await util.getGitHubVersion({
auth: "",
url: "https://ghe.example.com",
});
t.deepEqual({ type: "ghes", version: "2.0" }, v2);
t.deepEqual({ type: util.GitHubVariant.GHES, version: "2.0" }, v2);
mockGetMetaVersionHeader("GitHub AE");
const ghae = await util.getGitHubVersion({
auth: "",
url: "https://example.githubenterprise.com",
});
t.deepEqual({ type: util.GitHubVariant.GHAE }, ghae);
mockGetMetaVersionHeader(undefined);
const v3 = await util.getGitHubVersion({
auth: "",
url: "https://ghe.example.com",
});
t.deepEqual({ type: "dotcom" }, v3);
t.deepEqual({ type: util.GitHubVariant.DOTCOM }, v3);
});

View file

@ -219,16 +219,22 @@ const CODEQL_ACTION_WARNED_ABOUT_VERSION_ENV_VAR =
"CODEQL_ACTION_WARNED_ABOUT_VERSION";
let hasBeenWarnedAboutVersion = false;
export enum GitHubVariant {
DOTCOM,
GHES,
GHAE,
}
export type GitHubVersion =
| { type: "dotcom" }
| { type: "ghes"; version: string };
| { type: GitHubVariant.DOTCOM }
| { type: GitHubVariant.GHAE }
| { type: GitHubVariant.GHES; version: string };
export async function getGitHubVersion(
apiDetails: GitHubApiDetails
): Promise<GitHubVersion> {
// We can avoid making an API request in the standard dotcom case
if (parseGithubUrl(apiDetails.url) === GITHUB_DOTCOM_URL) {
return { type: "dotcom" };
return { type: GitHubVariant.DOTCOM };
}
// Doesn't strictly have to be the meta endpoint as we're only
@ -239,11 +245,15 @@ export async function getGitHubVersion(
// This happens on dotcom, although we expect to have already returned in that
// case. This can also serve as a fallback in cases we haven't foreseen.
if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === undefined) {
return { type: "dotcom" };
return { type: GitHubVariant.DOTCOM };
}
if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === "GitHub AE") {
return { type: GitHubVariant.GHAE };
}
const version = response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] as string;
return { type: "ghes", version };
return { type: GitHubVariant.GHES, version };
}
export function checkGitHubVersionInRange(
@ -251,7 +261,7 @@ export function checkGitHubVersionInRange(
mode: Mode,
logger: Logger
) {
if (hasBeenWarnedAboutVersion || version.type !== "ghes") {
if (hasBeenWarnedAboutVersion || version.type !== GitHubVariant.GHES) {
return;
}