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

@ -28,21 +28,18 @@ const sinon = __importStar(require("sinon"));
const apiClient = __importStar(require("./api-client"));
const feature_flags_1 = require("./feature-flags");
const logging_1 = require("./logging");
const repository_1 = require("./repository");
const testing_utils_1 = require("./testing-utils");
const util = __importStar(require("./util"));
const util_1 = require("./util");
(0, testing_utils_1.setupTests)(ava_1.default);
ava_1.default.beforeEach(() => {
(0, util_1.initializeEnvironment)(util_1.Mode.actions, "1.2.3");
sinon
.stub(util, "getRequiredEnvParam")
.withArgs("GITHUB_REPOSITORY")
.returns("github/example");
});
const testApiDetails = {
auth: "1234",
url: "https://github.com",
};
const testRepositoryNwo = (0, repository_1.parseRepositoryNwo)("github/example");
function mockHttpRequests(responseStatusCode, flags) {
// Passing an auth token is required, so we just use a dummy value
const client = github.getOctokit("123");
@ -73,7 +70,7 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const loggedMessages = [];
const featureFlags = new feature_flags_1.GitHubFeatureFlags(variant.gitHubVersion, testApiDetails, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
const featureFlags = new feature_flags_1.GitHubFeatureFlags(variant.gitHubVersion, testApiDetails, testRepositoryNwo, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
t.assert((await featureFlags.getDatabaseUploadsEnabled()) === false);
t.assert((await featureFlags.getMlPoweredQueriesEnabled()) === false);
t.assert((await featureFlags.getUploadsDomainEnabled()) === false);
@ -87,7 +84,7 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const loggedMessages = [];
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
mockHttpRequests(200, {});
t.assert((await featureFlags.getDatabaseUploadsEnabled()) === false);
t.assert((await featureFlags.getMlPoweredQueriesEnabled()) === false);
@ -106,7 +103,7 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
(0, ava_1.default)("Feature flags exception is propagated if the API request errors", async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, (0, logging_1.getRunnerLogger)(true));
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, logging_1.getRunnerLogger)(true));
mockHttpRequests(500, {});
await t.throwsAsync(async () => featureFlags.preloadFeatureFlags(), {
message: "Encountered an error while trying to load feature flags: Error: some error message",
@ -122,7 +119,7 @@ for (const featureFlag of FEATURE_FLAGS) {
(0, ava_1.default)(`Feature flag '${featureFlag}' is enabled if enabled in the API response`, async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, (0, logging_1.getRunnerLogger)(true));
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, logging_1.getRunnerLogger)(true));
const expectedFeatureFlags = {};
for (const f of FEATURE_FLAGS) {
expectedFeatureFlags[f] = false;