Skip to content

[PM-2282] Make feature flags type safe #8612

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

Merged
merged 13 commits into from
Apr 26, 2024
Merged

Conversation

Hinton
Copy link
Member

@Hinton Hinton commented Apr 4, 2024

Type of change

- [ ] Bug fix
- [ ] New feature development
- [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

Refactors the feature flags in ConfigService to be type safe. It also moves the default value to a centralized location rather than the caller defining it. This ensures consistency across the various places they are used.

Before you submit

  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team
  • Ensure that all UI additions follow WCAG AA requirements

@Hinton Hinton requested review from a team as code owners April 4, 2024 13:13
@Hinton Hinton marked this pull request as draft April 4, 2024 13:14
@github-actions github-actions bot added the needs-qa Marks a PR as requiring QA approval label Apr 4, 2024
Copy link
Contributor

github-actions bot commented Apr 4, 2024

Logo
Checkmarx One – Scan Summary & Detailse931223c-b01c-473e-881c-3995962525bf

No New Or Fixed Issues Found

@Hinton Hinton marked this pull request as ready for review April 4, 2024 15:23
// Replace this with a type safe lookup of the feature flag values in PM-2282
export type FeatureFlagValue = number | string | boolean;
// Map of feature flags to their value type. `string`, `number` and `boolean` are the only supported types.
export const FeatureFlagRuntimeTypes = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we technically don't need these for this PR, #8561 requires it if we want to introduce a default defaultValue

Copy link

codecov bot commented Apr 4, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 27.70%. Comparing base (fffef95) to head (1b18c78).

Files Patch % Lines
...platform/services/config/default-config.service.ts 25.00% 3 Missing ⚠️
...log/bulk-collection-assignment-dialog.component.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8612      +/-   ##
==========================================
+ Coverage   27.67%   27.70%   +0.02%     
==========================================
  Files        2369     2369              
  Lines       69049    69052       +3     
  Branches    12919    12919              
==========================================
+ Hits        19112    19128      +16     
+ Misses      48504    48491      -13     
  Partials     1433     1433              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Hinton Hinton removed the needs-qa Marks a PR as requiring QA approval label Apr 4, 2024
@Hinton Hinton requested a review from a team as a code owner April 4, 2024 16:59
Copy link
Contributor

@coroiu coroiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't really comment on libs/common/src/enums/feature-flag.enum.ts since it's my own code but added some comments in other parts

return this.serverConfig$.pipe(
map((serverConfig) => {
if (serverConfig?.featureStates == null || serverConfig.featureStates[key] == null) {
return defaultValue;
}

return serverConfig.featureStates[key] as T;
return serverConfig.featureStates[key] as FeatureFlagType<Flag>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 suggestion: Since we now have the types available during runtime, maybe we should add some guards here to verify that we actually get the types we expect?

Suggested change
return serverConfig.featureStates[key] as FeatureFlagType<Flag>;
const value = serverConfig.featureStates[key];
if (FeatureFlagRuntimeTypes[key] !== typeof value) { throw new Error("Unknown flag value, possibly undefined?"); } // or maybe return default value?
return value as FeatureFlagType<Flag>;

Or maybe this check should be done when fetching the server config, in which case serverConfig?.featureStates might already be fully type-safe by this point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was planning on using it in my other PR.

@Hinton Hinton requested review from a team as code owners April 5, 2024 12:39
@Hinton Hinton requested a review from r-tome April 5, 2024 12:39
@Hinton Hinton requested a review from MGibson1 April 15, 2024 13:00
audreyality
audreyality previously approved these changes Apr 18, 2024
Copy link
Member

@audreyality audreyality left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻 kudos!
🤔 non-blocking concern
⛏️ nitpick


export type DefaultFeatureFlagValueType = typeof DefaultFeatureFlagValue;

export type FeatureFlagType<Flag extends FeatureFlag> = DefaultFeatureFlagValueType[Flag];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type FeatureFlagType<Flag extends FeatureFlag> = DefaultFeatureFlagValueType[Flag];
/** gets the type of a `FeatureFlag` value. */
export type FeatureFlagType<Flag extends FeatureFlag> = DefaultFeatureFlagValueType[Flag];

Copy link
Member

@audreyality audreyality Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Since the default config service has test cases, they should be updated.

r-tome
r-tome previously approved these changes Apr 19, 2024
@Hinton Hinton dismissed stale reviews from r-tome and audreyality via 1b18c78 April 19, 2024 15:35
@Hinton Hinton merged commit 14b2eb9 into main Apr 26, 2024
63 checks passed
@Hinton Hinton deleted the ps/PM-2282-typesafe-feature-flags branch April 26, 2024 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants