Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Convert GitHub variant to an enum.
Chris Gavin committed Feb 15, 2021

Unverified

No user is associated with the committer email.
1 parent 0656b2c commit c9ca4ec
Showing 24 changed files with 67 additions and 52 deletions.
6 changes: 3 additions & 3 deletions lib/analysis-paths.test.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/analysis-paths.test.js.map

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

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

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

2 changes: 1 addition & 1 deletion lib/config-utils.test.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/config-utils.test.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/tracer-config.test.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/tracer-config.test.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/upload-lib.js
2 changes: 1 addition & 1 deletion lib/upload-lib.js.map
8 changes: 4 additions & 4 deletions lib/upload-lib.test.js
2 changes: 1 addition & 1 deletion lib/upload-lib.test.js.map
16 changes: 11 additions & 5 deletions lib/util.js
2 changes: 1 addition & 1 deletion lib/util.js.map
8 changes: 4 additions & 4 deletions lib/util.test.js
2 changes: 1 addition & 1 deletion lib/util.test.js.map
6 changes: 3 additions & 3 deletions src/analysis-paths.test.ts
@@ -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);
4 changes: 3 additions & 1 deletion src/analyze.test.ts
@@ -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,
2 changes: 1 addition & 1 deletion src/config-utils.test.ts
@@ -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 {
2 changes: 1 addition & 1 deletion src/tracer-config.test.ts
@@ -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,
};
}

10 changes: 5 additions & 5 deletions src/upload-lib.test.ts
@@ -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);

2 changes: 1 addition & 1 deletion src/upload-lib.ts
@@ -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 !== "ghes" ||
gitHubVersion.type !== util.GitHubVariant.GHES ||
semver.satisfies(gitHubVersion.version, `>=3.1`)
) {
if (
8 changes: 4 additions & 4 deletions src/util.test.ts
@@ -221,26 +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: "ghae" }, ghae);
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);
});
21 changes: 13 additions & 8 deletions src/util.ts
@@ -219,17 +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: "ghae" }
| { 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
@@ -240,23 +245,23 @@ 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: "ghae" };
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(
version: GitHubVersion,
mode: Mode,
logger: Logger
) {
if (hasBeenWarnedAboutVersion || version.type !== "ghes") {
if (hasBeenWarnedAboutVersion || version.type !== GitHubVariant.GHES) {
return;
}

0 comments on commit c9ca4ec

Please sign in to comment.