Unverified Commit eb72d135 authored by Greg Messner's avatar Greg Messner Committed by GitHub
Browse files

Update application settings (#471)

parent 870cf651
......@@ -107,7 +107,7 @@ public class ApplicationSettingsApi extends AbstractApi {
* @return the populated ApplicationSettings instance
* @throws GitLabApiException if any error occurs
*/
private final ApplicationSettings parseApplicationSettings(JsonNode root) throws GitLabApiException {
public static final ApplicationSettings parseApplicationSettings(JsonNode root) throws GitLabApiException {
ApplicationSettings appSettings = new ApplicationSettings();
......@@ -122,7 +122,8 @@ public class ApplicationSettingsApi extends AbstractApi {
case "created_at":
try {
appSettings.setCreatedAt(ISO8601.toDate(root.path(fieldName).asText()));
String value = root.path(fieldName).asText();
appSettings.setCreatedAt(ISO8601.toDate(value));
} catch (ParseException pe) {
throw new GitLabApiException(pe);
}
......@@ -130,7 +131,8 @@ public class ApplicationSettingsApi extends AbstractApi {
case "updated_at":
try {
appSettings.setUpdatedAt(ISO8601.toDate(root.path(fieldName).asText()));
String value = root.path(fieldName).asText();
appSettings.setUpdatedAt(ISO8601.toDate(value));
} catch (ParseException pe) {
throw new GitLabApiException(pe);
}
......
......@@ -6,6 +6,7 @@ import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
/**
* This enum provides constants and value validation for the available GitLab application settings.
* See <a href="https://docs.gitlab.com/ce/api/settings.html#list-of-settings-that-can-be-accessed-via-api-calls">
......@@ -87,7 +88,7 @@ public enum Setting {
* is required to apply changes.
*/
//TODO according to documentation : string or array of strings => How to do this?
ASSET_PROXY_WHITELIST(String.class),
ASSET_PROXY_WHITELIST(new Class<?>[]{String.class, String[].class}),
/**
* By default, we write to the authorized_keys file to support Git over SSH
......@@ -166,7 +167,7 @@ public enum Setting {
* Default project creation protection. Can take: 0 (No one), 1 (Maintainers)
* or 2 (Developers + Maintainers)
*/
DEFAULT_PROJECT_CREATION(String.class),
DEFAULT_PROJECT_CREATION(Integer.class),
/**
* What visibility level new projects receive. Can take private, internal and
......@@ -666,11 +667,11 @@ public enum Setting {
PASSWORD_AUTHENTICATION_ENABLED_FOR_WEB(Boolean.class),
/**
* Path of the group that is allowed to toggle the performance bar.
* ID of the group that is allowed to toggle the performance bar.
* @deprecated Use {@link #PERFORMANCE_BAR_ALLOWED_GROUP_PATH} instead.
*/
@Deprecated
PERFORMANCE_BAR_ALLOWED_GROUP_ID(String.class),
PERFORMANCE_BAR_ALLOWED_GROUP_ID(Integer.class),
/**
* Path of the group that is allowed to toggle the performance bar.
......@@ -697,7 +698,7 @@ public enum Setting {
/**
* Interval multiplier used by endpoints that perform polling. Set to 0 to disable polling.
* The documentation liusts this as a decimal, but it is a String in the JSON.
* The documentation lists this as a decimal, but it is a String in the JSON.
*/
POLLING_INTERVAL_MULTIPLIER(String.class),
......@@ -1038,12 +1039,13 @@ public enum Setting {
private static JacksonJsonEnumHelper<Setting> enumHelper = new JacksonJsonEnumHelper<>(Setting.class);
private Class<?> type;
private Class<?>[] types;
private Setting(Class<?> type) {
this.type = type;
}
public final Class<?> getType() {
return (type);
private Setting(Class<?>[] types) {
this.types = types;
}
@JsonCreator
......@@ -1068,8 +1070,24 @@ public enum Setting {
* @param value the value to validate
* @return true if the value is of the correct type or null
*/
public boolean isValid(Object value) {
return (value == null || value.getClass() == type);
public final boolean isValid(Object value) {
if (value == null) {
return (true);
}
Class<?> valueType = value.getClass();
if (type != null) {
return (valueType == type);
}
for (Class<?> type : types) {
if (valueType == type) {
return (true);
}
}
return (false);
}
/**
......@@ -1085,8 +1103,13 @@ public enum Setting {
return;
}
StringBuilder shouldBe = new StringBuilder(types[0].getSimpleName());
for (int i = 1; i < types.length; i++) {
shouldBe.append(" | ").append(types[i].getSimpleName());
}
String errorMsg = String.format("'%s' value is of incorrect type, is %s, should be %s",
toValue(), value.getClass().getSimpleName(), getType().getSimpleName());
toValue(), value.getClass().getSimpleName(), shouldBe.toString());
throw new GitLabApiException(errorMsg);
}
}
......@@ -25,6 +25,11 @@ public class JsonUtils {
jacksonJson.getObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
}
static JsonNode readTreeFromMap(Map<String, Object> map) throws JsonParseException, JsonMappingException, IOException {
String jsonString = jacksonJson.getObjectMapper().writeValueAsString(map);
return (jacksonJson.readTree(jsonString));
}
static JsonNode readTreeFromString(String jsonString) throws JsonParseException, JsonMappingException, IOException {
return (jacksonJson.readTree(jsonString));
}
......@@ -116,11 +121,15 @@ public class JsonUtils {
}
static void sortedDump(final JsonNode node) throws JsonProcessingException {
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
System.err.println(jacksonJson.getObjectMapper().writeValueAsString(obj));
System.err.println(sortedJsonString(node));
System.err.flush();
}
static String sortedJsonString(final JsonNode node) throws JsonProcessingException {
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
return (jacksonJson.getObjectMapper().writeValueAsString(obj));
}
static String readResource(String filename) throws IOException {
InputStreamReader reader = new InputStreamReader(GitLabApi.class.getResourceAsStream(filename));
......
......@@ -24,6 +24,7 @@
package org.gitlab4j.api;
import static org.gitlab4j.api.JsonUtils.compareJson;
import static org.gitlab4j.api.JsonUtils.readTreeFromResource;
import static org.gitlab4j.api.JsonUtils.unmarshalResource;
import static org.gitlab4j.api.JsonUtils.unmarshalResourceList;
import static org.gitlab4j.api.JsonUtils.unmarshalResourceMap;
......@@ -34,6 +35,7 @@ import java.util.Map;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Application;
import org.gitlab4j.api.models.ApplicationSettings;
import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.AwardEmoji;
import org.gitlab4j.api.models.Badge;
......@@ -53,7 +55,6 @@ import org.gitlab4j.api.models.Epic;
import org.gitlab4j.api.models.EpicIssue;
import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.ExportStatus;
import org.gitlab4j.api.models.ProjectFetches;
import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.HealthCheckInfo;
......@@ -76,6 +77,7 @@ import org.gitlab4j.api.models.PackageFile;
import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectFetches;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.ProtectedBranch;
......@@ -100,6 +102,8 @@ import org.gitlab4j.api.services.JiraService;
import org.gitlab4j.api.services.SlackService;
import org.junit.Test;
import com.fasterxml.jackson.databind.JsonNode;
public class TestGitLabApiBeans {
@Test
......@@ -418,6 +422,13 @@ public class TestGitLabApiBeans {
assertTrue(compareJson(runnerDetail, "runner-detail.json"));
}
@Test
public void testSettings() throws Exception {
JsonNode json = readTreeFromResource("application-settings.json");
ApplicationSettings applicationSettings = ApplicationSettingsApi.parseApplicationSettings(json);
assertTrue(compareJson(applicationSettings.getSettings(), "application-settings.json"));
}
@Test
public void testAllRunners() throws Exception {
List<Runner> allRunners = unmarshalResourceList(Runner.class, "all-runners.json");
......
{
"default_projects_limit": 100000,
"signup_enabled": false,
"password_authentication_enabled_for_web": true,
"gravatar_enabled": true,
"sign_in_text": "",
"home_page_url": "",
"default_branch_protection": 2,
"restricted_visibility_levels": [],
"max_attachment_size": 10,
"session_expire_delay": 10080,
"default_project_visibility": "internal",
"default_snippet_visibility": "private",
"default_group_visibility": "private",
"outbound_local_requests_whitelist": [],
"domain_whitelist": [],
"domain_blacklist_enabled" : false,
"domain_blacklist" : [],
"external_authorization_service_enabled": true,
"external_authorization_service_url": "https://authorize.me",
"external_authorization_service_default_label": "default",
"external_authorization_service_timeout": 0.5,
"user_oauth_applications": true,
"after_sign_out_path": "",
"container_registry_token_expire_delay": 5,
"repository_storages": ["default"],
"plantuml_enabled": false,
"terminal_max_session_time": 0,
"polling_interval_multiplier": "1.0",
"rsa_key_restriction": 0,
"dsa_key_restriction": 0,
"ecdsa_key_restriction": 0,
"ed25519_key_restriction": 0,
"first_day_of_week": 0,
"enforce_terms": true,
"terms": "Hello world!",
"performance_bar_allowed_group_id": 42,
"instance_statistics_visibility_private": false,
"user_show_add_ssh_key_message": true,
"file_template_project_id": 1,
"local_markdown_version": 0,
"asset_proxy_enabled": true,
"asset_proxy_url": "https://assets.example.com",
"asset_proxy_whitelist": ["example.com", "*.example.com", "your-instance.com"],
"geo_node_allowed_ips": "0.0.0.0/0, ::/0",
"allow_local_requests_from_hooks_and_services": true,
"allow_local_requests_from_web_hooks_and_services": true,
"allow_local_requests_from_system_hooks": false
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment