gitlab4j-api icon indicating copy to clipboard operation
gitlab4j-api copied to clipboard

Group API - create/update Variable missing `variable_type` and `environment_scope`

Open mlk opened this issue 4 years ago • 1 comments

Looking at the HTTP API docs for [Group level variables|https://docs.gitlab.com/ee/api/group_level_variables.html] and it appears to support both a variable_type and a environment_scope. The two fields do not appear in the [Java API|https://javadoc.io/static/org.gitlab4j/gitlab4j-api/4.19.0/org/gitlab4j/api/GroupApi.html#createVariable-java.lang.Object-java.lang.String-java.lang.String-java.lang.Boolean-java.lang.Boolean-].

Is there a reason they are no included?

Thank you, Michael

mlk avatar Jan 18 '22 19:01 mlk

I have encountered the same problem and came up with the following workaround. It works for me.

    GitLabApi gitLabApi = GitLabApi.oauth2Login("yourUrl", "yourUsername", "yourPassword");
    GroupApi groupApi = gitLabApi.getGroupApi();

    org.gitlab4j.api.models.Group group = ... // define the group within which we will create variables
    Object groupIdOrPath = groupApi.getGroupIdOrPath(group);

    // build your variable
    Form variable = new GitLabApiForm()
            .withParam("key", "key_value", true)
            .withParam("value", "value", true)
            .withParam("variable_type", Variable.Type.ENV_VAR)
            .withParam("protected", false)
            .withParam("masked", false)
            .withParam("environment_scope", "*");

    // yeah, we need some reflection here
    Method method = AbstractApi.class.getDeclaredMethod(
            "post", Response.Status.class, jakarta.ws.rs.core.Form.class, Object[].class);
    method.setAccessible(true);

    // API call actually here
    Response response = (Response) method.invoke(groupApi, Response.Status.CREATED, variable,
            new Object[]{"groups", groupIdOrPath, "variables"});
    Variable created = response.readEntity(Variable.class);

However, it is not at all clear why this functionality is missing from the Group API. It seems to be just an omission.

nnsns avatar Jan 20 '24 14:01 nnsns