-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MDS-5293] Added feature flag support to Minespace, Core, and Core AP…
…I using Flagsmith (#2647) * component layout for home page. Search bar will require some changes so just remade in functional/TS format * actions, reducers, selectors, etc, for getting global mine alerts. Change route to point to new home page (will rename later) * Revert "component layout for home page. Search bar will require some changes so just remade in functional/TS format" This reverts commit 3ab20a7. * Revert "actions, reducers, selectors, etc, for getting global mine alerts. Change route to point to new home page (will rename later)" This reverts commit 7339823. * MDS-5293 Added support for flagsmith feature flags to core and minespace * Updated server / added provider * MDS-5293 Added backend support for feature flags * MDS-5293 Added feature flag documentation * MDS-5293 Fixed fe tests * Removed old feature flags * MDS-5293 Fixed pipeline Flagsmith props * Fixed cypress flagsmith envs * MDS-5293 Added flagsmith envs to env config --------- Co-authored-by: Tara Epp <[email protected]> Co-authored-by: simensma-fresh <[email protected]>
- Loading branch information
1 parent
24164bc
commit 3a6fa4d
Showing
75 changed files
with
1,018 additions
and
1,758 deletions.
There are no files selected for viewing
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
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,120 @@ | ||
# Feature Flags | ||
|
||
MDS Supports feature flags both for our frontend and backend apps using [Flagsmith](https://docs.flagsmith.com). | ||
|
||
## Frontend | ||
|
||
A couple of options exists to check for a feature flag in core-web and minespace-web. | ||
|
||
### Defining feature flag | ||
|
||
Feature flags for use in core / minespace have to be defined in the `Feature` enum located in `featureFlag.ts` in the common package. | ||
The values of this enum, must match the name of a feature flag defined in Flagsmith. | ||
|
||
```typescript | ||
export enum Feature { | ||
MAJOR_PROJECT_ARCHIVE_FILE = "major_project_archive_file", | ||
DOCUMENTS_REPLACE_FILE = "major_project_replace_file", | ||
MAJOR_PROJECT_ALL_DOCUMENTS = "major_project_all_documents", | ||
MAJOR_PROJECT_DECISION_PACKAGE = "major_project_decision_package", | ||
FLAGSMITH = "flagsmith", | ||
TSF_V2 = "tsf_v2", | ||
} | ||
``` | ||
|
||
### Using `useFeatureFlag` hook | ||
|
||
Preferred method if using feature flag in a functional React component. | ||
|
||
```typescript | ||
import { useFeatureFlag } from "@common/providers/featureFlags/useFeatureFlag"; | ||
import { Feature } from "@mds/common"; | ||
|
||
const ThisIsAReactComponent = () => { | ||
const { isFeatureEnabled } = useFeatureFlag(); | ||
|
||
return ( | ||
{isFeatureEnabled(Feature.TSF_V2)? <p>SuperSecretFeature</p>: <p>Sorry, you can't access this</p>} | ||
); | ||
}; | ||
``` | ||
|
||
### Using `withFeatureFlag` HOC | ||
|
||
Alternative method if using feature flag in a React component and you cannot use hooks (for example in class components). | ||
|
||
```typescript | ||
import withFeatureFlag from "@common/providers/featureFlags/withFeatureFlag"; | ||
import { Feature } from "@mds/common"; | ||
|
||
class ThisIsAReactComponent { | ||
render() { | ||
return ( | ||
{props.isFeatureEnabled(Feature.TSF_V2)? <p>SuperSecretFeature</p>: <p>Sorry, you can't access this</p>} | ||
); | ||
} | ||
}; | ||
|
||
export default withFeatureFlag(ThisIsAReactComponent); | ||
``` | ||
|
||
### Using FeatureFlagGuard | ||
Need to restrict a route based on a feature flag? | ||
|
||
You can use the `FeatureFlagGuard` and pass along the feature you want to check for. | ||
If it's not enabled, you get a nice little "you don't have access" notice. | ||
|
||
```typescript | ||
import { Feature } from "@mds/common"; | ||
import FeatureFlagGuard from "@/components/common/featureFlag.guard"; | ||
|
||
|
||
const DamsPage: React.FC<DamsPageProps> = (props) => { | ||
return ( | ||
<div> | ||
ALL THE DAMS | ||
</div> | ||
); | ||
}; | ||
|
||
const mapStateToProps = (state: RootState) => ({ | ||
initialValues: getDam(state), | ||
tsf: getTsf(state), | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch) => | ||
bindActionCreators( | ||
{ createDam, updateDam, fetchMineRecordById, storeTsf, storeDam, submit }, | ||
dispatch | ||
); | ||
|
||
export default compose( | ||
connect(mapStateToProps, mapDispatchToProps), | ||
)(withRouter(FeatureFlagGuard(Feature.TSF_V2)(DamsPage))); | ||
``` | ||
|
||
### Using FeatureFlag directly (discouraged) | ||
|
||
If you need access to a feature flag outside of a react context you can use `featureFlag.ts` directly. | ||
Please use the other methods above as far as you can. | ||
|
||
```typescript | ||
import { isFeatureEnabled } from @mds/common; | ||
import { Feature } from "@mds/common"; | ||
|
||
console.log(isFeatureEnabled(Feature.TSF_V2)); | ||
|
||
``` | ||
|
||
## Core API | ||
|
||
How to use: | ||
1. Define a feature flag in the `Feature` enum (feature_flag.py). The value of the enum must match a feature flag defined in Flagsmith. | ||
2. You can use the `is_feature_enabled` function to check if the given flag is enabled. | ||
|
||
```python | ||
from app.api.utils.feature_flag import is_feature_enabled, Feature | ||
|
||
if is_feature_enabled(Feature.TSF_V2): | ||
# Do something if TSF_V2 feature is enabled | ||
``` |
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
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
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
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,24 @@ | ||
from enum import Enum | ||
from flagsmith import Flagsmith | ||
from app.config import Config | ||
from flask import current_app | ||
|
||
|
||
class Feature(Enum): | ||
TSF_V2='tsf_v2' | ||
MAJOR_PROJECT_REPLACE_FILE='major_project_replace_file' | ||
|
||
def __str__(self): | ||
return self.value | ||
|
||
flagsmith = Flagsmith( | ||
environment_id = Config.FLAGSMITH_KEY, | ||
api = Config.FLAGSMITH_URL, | ||
) | ||
|
||
def is_feature_enabled(feature): | ||
try: | ||
return flagsmith.has_feature(feature) and flagsmith.feature_enabled(feature) | ||
except Exception as e: | ||
current_app.logger.error(f'Failed to look up feature flag for: {feature}. ' + str(e)) | ||
return False |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
cached-property==1.5.1 | ||
factory-boy==2.12.0 | ||
flagsmith==2.0.1 | ||
Flask==1.1.2 | ||
Flask-Caching==1.8.0 | ||
Flask-Cors==3.0.9 | ||
|
Oops, something went wrong.