1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat: prevent adding flags to archived project (#7811)

This commit is contained in:
Mateusz Kwasniewski 2024-08-09 09:00:19 +02:00 committed by GitHub
parent e8d682c034
commit bde81b940c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 70 additions and 17 deletions

View File

@ -1256,7 +1256,12 @@ class FeatureToggleService {
await this.validateName(value.name);
await this.validateFeatureFlagNameAgainstPattern(value.name, projectId);
const projectExists = await this.projectStore.hasProject(projectId);
let projectExists: boolean;
if (this.flagResolver.isEnabled('archiveProjects')) {
projectExists = await this.projectStore.hasActiveProject(projectId);
} else {
projectExists = await this.projectStore.hasProject(projectId);
}
if (await this.projectStore.isFeatureLimitReached(projectId)) {
throw new InvalidOperationError(
@ -1322,7 +1327,9 @@ class FeatureToggleService {
return createdToggle;
}
throw new NotFoundError(`Project with id ${projectId} does not exist`);
throw new NotFoundError(
`Active project with id ${projectId} does not exist`,
);
}
async checkFeatureFlagNamesAgainstProjectPattern(

View File

@ -16,7 +16,12 @@ import {
TEST_AUDIT_USER,
} from '../../../types';
import EnvironmentService from '../../project-environments/environment-service';
import { ForbiddenError, PatternError, PermissionError } from '../../../error';
import {
ForbiddenError,
NotFoundError,
PatternError,
PermissionError,
} from '../../../error';
import type { ISegmentService } from '../../segment/segment-service-interface';
import { createFeatureToggleService, createSegmentService } from '../..';
import { insertLastSeenAt } from '../../../../test/e2e/helpers/test-helper';
@ -41,7 +46,9 @@ const mockConstraints = (): IConstraint[] => {
const irrelevantDate = new Date();
beforeAll(async () => {
const config = createTestConfig();
const config = createTestConfig({
experimental: { flags: { archiveProjects: true } },
});
db = await dbInit(
'feature_toggle_service_v2_service_serial',
config.getLogger,
@ -744,3 +751,25 @@ test('Should return "default" for stickiness when creating a flexibleRollout str
strategies: [{ parameters: { stickiness: 'default' } }],
});
});
test('Should not allow to add flags to archived projects', async () => {
const project = await stores.projectStore.create({
id: 'archivedProject',
name: 'archivedProject',
});
await stores.projectStore.archive(project.id);
await expect(
service.createFeatureToggle(
project.id,
{
name: 'irrelevant',
},
TEST_AUDIT_USER,
),
).rejects.toEqual(
new NotFoundError(
`Active project with id archivedProject does not exist`,
),
);
});

View File

@ -67,6 +67,8 @@ export interface IProjectApplicationsSearchParams {
export interface IProjectStore extends Store<IProject, string> {
hasProject(id: string): Promise<boolean>;
hasActiveProject(id: string): Promise<boolean>;
updateHealth(healthUpdate: IProjectHealthUpdate): Promise<void>;
create(project: IProjectInsert): Promise<IProject>;

View File

@ -102,15 +102,6 @@ class ProjectStore implements IProjectStore {
destroy(): void {}
async exists(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
[id],
);
const { present } = result.rows[0];
return present;
}
async isFeatureLimitReached(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1
@ -252,6 +243,15 @@ class ProjectStore implements IProjectStore {
.then(this.mapRow);
}
async exists(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
[id],
);
const { present } = result.rows[0];
return present;
}
async hasProject(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
@ -261,6 +261,15 @@ class ProjectStore implements IProjectStore {
return present;
}
async hasActiveProject(id: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ? and archived_at IS NULL) AS present`,
[id],
);
const { present } = result.rows[0];
return present;
}
async updateHealth(healthUpdate: IProjectHealthUpdate): Promise<void> {
await this.db(TABLE).where({ id: healthUpdate.id }).update({
health: healthUpdate.health,

View File

@ -108,10 +108,6 @@ export default class FakeProjectStore implements IProjectStore {
return this.projects.length;
}
async exists(key: string): Promise<boolean> {
return this.projects.some((project) => project.id === key);
}
async get(key: string): Promise<IProject> {
const project = this.projects.find((p) => p.id === key);
if (project) {
@ -129,10 +125,20 @@ export default class FakeProjectStore implements IProjectStore {
return Promise.resolve(0);
}
async exists(key: string): Promise<boolean> {
return this.projects.some((project) => project.id === key);
}
async hasProject(id: string): Promise<boolean> {
return this.exists(id);
}
async hasActiveProject(id: string): Promise<boolean> {
return this.projects.some(
(project) => project.id === id && project.archivedAt === null,
);
}
async importProjects(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
projects: IProjectInsert[],