Can Gitlab feature flags be modified by code with unleash java?

127 Views Asked by At

I'm working with Gitlab Feature Flags and to use them in my Java project I'm using unleash-java. The problem is that in this library it seems that I can't change from my code the values of the feature flags, but just from Gitlab. Is there a way to do it? Maybe using another library?

1

There are 1 best solutions below

0
On

As per GitLab's documentation, answer to your question is Yes. It is possible to update the GitLab feature flag https://docs.gitlab.com/ee/api/feature_flags.html#update-a-feature-flag

To get a feature flag use

GET https://gitlab.example.com/api/v4/projects/<projectId>/feature_flags/test-feature-flag

You have to pass your Git Access token in the header named PRIVATE-TOKEN

and response will be like below

{
    "name": "test-feature-flag",
    "description": "This is a test feature flag to check the API",
    "active": true,
    "version": "new_version_flag",
    "created_at": "2023-12-14T16:52:34.191Z",
    "updated_at": "2023-12-14T16:52:34.191Z",
    "scopes": [],
    "strategies": [
        {
            "id": 6825,
            "name": "default",
            "parameters": {},
            "scopes": [
                {
                    "id": 7572,
                    "environment_scope": "*"
                }
            ]
        }
    ]
}

Now as you can see this feature flag is active now. You can disable it using below end point

PUT https://gitlab.example.com/api/v4/projects/<projectId>/feature_flags/test-feature-flag

and the json body to update required field. For e.g. I passed below to toggle the state.

{
"active": false
}

And it worked perfectly.

Note: If you are using unleash in your application to read the feature flag, then keep in mind that the feature flag's updates will be reflected only after the cache is expired.

Cache can be configured by setting fetchTogglesInterval Below is a Java UnleashConfig bean

@Bean
  public UnleashConfig unleashConfig() {
    return UnleashConfig.builder().appName(unleashConfig.getAppName())
        .instanceId(unleashConfig.getInstanceId()).unleashAPI(unleashConfig.getApiUrl())
        .fetchTogglesInterval(unleashConfig.getCacheInterval()).disableMetrics().build();
  }