-
-
Notifications
You must be signed in to change notification settings - Fork 720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Feature lifecycle controller #6788
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0cf9d6f
feat: feature lifecycle usage behind a flag
kwasniew 52b3ad5
feat: feature lifecycle usage behind a flag
kwasniew 3eefc26
feat: feature lifecycle usage behind a flag
kwasniew bd91b40
feat: feature lifecycle usage behind a flag
kwasniew 4bbc15c
feat: feature lifecycle usage behind a flag
kwasniew 8ebc6c4
feat: feature lifecycle controller and openapi
kwasniew 28028b3
Merge branch 'main' of github.com:Unleash/unleash into feature-lifecy…
kwasniew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/lib/features/feature-lifecycle/feature-lifecycle-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { FeatureLifecycleService } from './feature-lifecycle-service'; | ||
import { | ||
type IFlagResolver, | ||
type IUnleashConfig, | ||
type IUnleashServices, | ||
NONE, | ||
serializeDates, | ||
} from '../../types'; | ||
import type { OpenApiService } from '../../services'; | ||
import { | ||
createResponseSchema, | ||
featureLifecycleSchema, | ||
type FeatureLifecycleSchema, | ||
getStandardResponses, | ||
} from '../../openapi'; | ||
import Controller from '../../routes/controller'; | ||
import type { Request, Response } from 'express'; | ||
import { NotFoundError } from '../../error'; | ||
|
||
interface FeatureLifecycleParams { | ||
projectId: string; | ||
featureName: string; | ||
} | ||
|
||
const PATH = '/:projectId/features/:featureName/lifecycle'; | ||
|
||
export default class FeatureLifecycleController extends Controller { | ||
private featureLifecycleService: FeatureLifecycleService; | ||
|
||
private openApiService: OpenApiService; | ||
|
||
private flagResolver: IFlagResolver; | ||
|
||
constructor( | ||
config: IUnleashConfig, | ||
{ | ||
featureLifecycleService, | ||
openApiService, | ||
}: Pick<IUnleashServices, 'openApiService' | 'featureLifecycleService'>, | ||
) { | ||
super(config); | ||
this.featureLifecycleService = featureLifecycleService; | ||
this.openApiService = openApiService; | ||
this.flagResolver = config.flagResolver; | ||
|
||
this.route({ | ||
method: 'get', | ||
path: PATH, | ||
handler: this.getFeatureLifecycle, | ||
permission: NONE, | ||
middleware: [ | ||
openApiService.validPath({ | ||
tags: ['Unstable'], | ||
summary: 'Get feature lifecycle', | ||
description: | ||
'Information about the lifecycle stages of the feature.', | ||
operationId: 'getFeatureLifecycle', | ||
responses: { | ||
200: createResponseSchema('featureLifecycleSchema'), | ||
...getStandardResponses(401, 403, 404), | ||
}, | ||
}), | ||
], | ||
}); | ||
} | ||
|
||
async getFeatureLifecycle( | ||
req: Request<FeatureLifecycleParams, any, any, any>, | ||
res: Response<FeatureLifecycleSchema>, | ||
): Promise<void> { | ||
if (!this.flagResolver.isEnabled('featureLifecycle')) { | ||
throw new NotFoundError('Feature lifecycle is disabled.'); | ||
} | ||
const { featureName } = req.params; | ||
|
||
const result = | ||
await this.featureLifecycleService.getFeatureLifecycle(featureName); | ||
|
||
this.openApiService.respondWithValidation( | ||
200, | ||
res, | ||
featureLifecycleSchema.$id, | ||
serializeDates(result), | ||
); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/lib/features/feature-lifecycle/feature-lifecycle.e2e.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
import { | ||
type IUnleashTest, | ||
setupAppWithAuth, | ||
} from '../../../test/e2e/helpers/test-helper'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
|
||
let app: IUnleashTest; | ||
let db: ITestDb; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('feature_lifecycle', getLogger); | ||
app = await setupAppWithAuth( | ||
db.stores, | ||
{ | ||
experimental: { | ||
flags: { | ||
featureLifecycle: true, | ||
}, | ||
}, | ||
}, | ||
db.rawDatabase, | ||
); | ||
|
||
await app.request | ||
.post(`/auth/demo/login`) | ||
.send({ | ||
email: 'user@getunleash.io', | ||
}) | ||
.expect(200); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.destroy(); | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => {}); | ||
|
||
const getFeatureLifecycle = async (featureName: string, expectedCode = 200) => { | ||
return app.request | ||
.get(`/api/admin/projects/default/features/${featureName}/lifecycle`) | ||
.expect(expectedCode); | ||
}; | ||
|
||
test('should return lifecycle stages', async () => { | ||
await app.createFeature('my_feature_a'); | ||
|
||
const { body } = await getFeatureLifecycle('my_feature_a'); | ||
|
||
expect(body).toEqual([]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { FromSchema } from 'json-schema-to-ts'; | ||
|
||
export const featureLifecycleSchema = { | ||
$id: '#/components/schemas/featureLifecycleSchema', | ||
type: 'array', | ||
description: 'A list of lifecycle stages for a given feature', | ||
items: { | ||
additionalProperties: false, | ||
type: 'object', | ||
required: ['stage', 'enteredStageAt'], | ||
properties: { | ||
stage: { | ||
type: 'string', | ||
enum: ['initial', 'pre-live', 'live', 'completed', 'archived'], | ||
example: 'initial', | ||
description: | ||
'The name of the lifecycle stage that got recorded for a given feature', | ||
}, | ||
enteredStageAt: { | ||
type: 'string', | ||
format: 'date-time', | ||
example: '2023-01-28T16:21:39.975Z', | ||
description: 'The date when the feature entered a given stage', | ||
}, | ||
}, | ||
description: 'The lifecycle stage of the feature', | ||
}, | ||
components: { | ||
schemas: {}, | ||
}, | ||
} as const; | ||
|
||
export type FeatureLifecycleSchema = FromSchema<typeof featureLifecycleSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nothing returned yet since we have in-memory fake store