Explicitly pass repository to feature flags constructor

As suggested in review: The `GITHUB_REPOSITORY` environment variable is
only available on Actions. Passing it in explicitly avoids potentially
crashing if this code is called from the runner.
This commit is contained in:
Henry Mercer 2021-12-15 17:03:43 +00:00
parent 621e0794ac
commit 5e87034b3b
9 changed files with 31 additions and 33 deletions

View file

@ -101,9 +101,14 @@ async function run() {
actionsUtil.getOptionalInput("ram") || process.env["CODEQL_RAM"]
);
const repositoryNwo = parseRepositoryNwo(
util.getRequiredEnvParam("GITHUB_REPOSITORY")
);
const featureFlags = new GitHubFeatureFlags(
config.gitHubVersion,
apiDetails,
repositoryNwo,
logger
);
void featureFlags.preloadFeatureFlags();
@ -182,9 +187,6 @@ async function run() {
logger.info("Not uploading results");
}
const repositoryNwo = parseRepositoryNwo(
util.getRequiredEnvParam("GITHUB_REPOSITORY")
);
// Possibly upload the database bundles for remote queries
await uploadDatabases(
repositoryNwo,

View file

@ -6,6 +6,7 @@ import * as apiClient from "./api-client";
import { GitHubApiDetails } from "./api-client";
import { GitHubFeatureFlags } from "./feature-flags";
import { getRunnerLogger } from "./logging";
import { parseRepositoryNwo } from "./repository";
import {
getRecordingLogger,
LoggedMessage,
@ -25,11 +26,6 @@ setupTests(test);
test.beforeEach(() => {
initializeEnvironment(Mode.actions, "1.2.3");
sinon
.stub(util, "getRequiredEnvParam")
.withArgs("GITHUB_REPOSITORY")
.returns("github/example");
});
const testApiDetails: GitHubApiDetails = {
@ -37,6 +33,8 @@ const testApiDetails: GitHubApiDetails = {
url: "https://github.com",
};
const testRepositoryNwo = parseRepositoryNwo("github/example");
function mockHttpRequests(
responseStatusCode: number,
flags: { [flagName: string]: boolean }
@ -83,6 +81,7 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
const featureFlags = new GitHubFeatureFlags(
variant.gitHubVersion,
testApiDetails,
testRepositoryNwo,
getRecordingLogger(loggedMessages)
);
@ -110,6 +109,7 @@ test("Feature flags are disabled if they're not returned in API response", async
const featureFlags = new GitHubFeatureFlags(
{ type: GitHubVariant.DOTCOM },
testApiDetails,
testRepositoryNwo,
getRecordingLogger(loggedMessages)
);
@ -143,6 +143,7 @@ test("Feature flags exception is propagated if the API request errors", async (t
const featureFlags = new GitHubFeatureFlags(
{ type: GitHubVariant.DOTCOM },
testApiDetails,
testRepositoryNwo,
getRunnerLogger(true)
);
@ -169,6 +170,7 @@ for (const featureFlag of FEATURE_FLAGS) {
const featureFlags = new GitHubFeatureFlags(
{ type: GitHubVariant.DOTCOM },
testApiDetails,
testRepositoryNwo,
getRunnerLogger(true)
);

View file

@ -1,6 +1,6 @@
import { getApiClient, GitHubApiDetails } from "./api-client";
import { Logger } from "./logging";
import { parseRepositoryNwo } from "./repository";
import { RepositoryNwo } from "./repository";
import * as util from "./util";
export interface FeatureFlags {
@ -23,6 +23,7 @@ export class GitHubFeatureFlags implements FeatureFlags {
constructor(
private gitHubVersion: util.GitHubVersion,
private apiDetails: GitHubApiDetails,
private repositoryNwo: RepositoryNwo,
private logger: Logger
) {}
@ -62,15 +63,12 @@ export class GitHubFeatureFlags implements FeatureFlags {
return {};
}
const client = getApiClient(this.apiDetails);
const repositoryNwo = parseRepositoryNwo(
util.getRequiredEnvParam("GITHUB_REPOSITORY")
);
try {
const response = await client.request(
"GET /repos/:owner/:repo/code-scanning/codeql-action/features",
{
owner: repositoryNwo.owner,
repo: repositoryNwo.repo,
owner: this.repositoryNwo.owner,
repo: this.repositoryNwo.repo,
}
);
return response.data;