Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
8c0952a5
Commit
8c0952a5
authored
May 05, 2019
by
Greg Messner
Browse files
Initial commit (#340).
parent
c2e8d576
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/ApplicationSettingsApi.java
0 → 100644
View file @
8c0952a5
package
org.gitlab4j.api
;
import
java.text.ParseException
;
import
java.util.Iterator
;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.models.Setting
;
import
org.gitlab4j.api.models.ApplicationSettings
;
import
org.gitlab4j.api.utils.ISO8601
;
import
com.fasterxml.jackson.databind.JsonNode
;
/**
* This class implements the client side API for the GitLab Application Settings API.
* See <a href="https://docs.gitlab.com/ee/api/settings.html">Application Settings API at GitLab</a> for more information.
*/
public
class
ApplicationSettingsApi
extends
AbstractApi
{
public
ApplicationSettingsApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* Get the current application settings of the GitLab instance.
*
* <pre><code>GitLab Endpoint: GET /api/v4/application/settings</code></pre>
*
* @return an ApplicationSettings instance containing the current application settings of the GitLab instance.
* @throws GitLabApiException if any exception occurs
*/
public
ApplicationSettings
getApplicationSettings
()
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"application"
,
"settings"
);
JsonNode
root
=
response
.
readEntity
(
JsonNode
.
class
);
return
(
parseApplicationSettings
(
root
));
}
/**
* Update the application settings of the GitLab instance with the settings in the
* provided ApplicationSettings instance.
*
* <pre><code>GitLab Endpoint: PUT /api/v4/application/settings</code></pre>
*
* @param appSettings the ApplicationSettings instance holding the settings and values to update
* @return the updated application settings in an ApplicationSettings instance
* @throws GitLabApiException if any exception occurs
*/
public
ApplicationSettings
updateApplicationSettings
(
ApplicationSettings
appSettings
)
throws
GitLabApiException
{
if
(
appSettings
==
null
||
appSettings
.
getSettings
().
isEmpty
())
{
throw
new
GitLabApiException
(
"ApplicationSettings cannot be null or empty."
);
}
final
GitLabApiForm
form
=
new
GitLabApiForm
();
appSettings
.
getSettings
().
forEach
((
s
,
v
)
->
form
.
withParam
(
s
,
v
));
Response
response
=
put
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"application"
,
"settings"
);
JsonNode
root
=
response
.
readEntity
(
JsonNode
.
class
);
return
(
parseApplicationSettings
(
root
));
}
/**
* Update a single application setting of the GitLab instance with the provided settings and value.
*
* <pre><code>GitLab Endpoint: PUT /api/v4/application/settings</code></pre>
*
* @param setting the ApplicationSetting to update
* @param value the new value for the application setting
* @return the updated application settings in an ApplicationSettings instance
* @throws GitLabApiException if any exception occurs
*/
public
ApplicationSettings
updateApplicationSetting
(
Setting
setting
,
Object
value
)
throws
GitLabApiException
{
if
(
setting
==
null
)
{
throw
new
GitLabApiException
(
"setting cannot be null."
);
}
return
(
updateApplicationSetting
(
setting
.
toString
(),
value
));
}
/**
* Update a single application setting of the GitLab instance with the provided settings and value.
*
* <pre><code>GitLab Endpoint: PUT /api/v4/application/settings</code></pre>
*
* @param setting the ApplicationSetting to update
* @param value the new value for the application setting
* @return the updated application settings in an ApplicationSettings instance
* @throws GitLabApiException if any exception occurs
*/
public
ApplicationSettings
updateApplicationSetting
(
String
setting
,
Object
value
)
throws
GitLabApiException
{
if
(
setting
==
null
||
setting
.
trim
().
isEmpty
())
{
throw
new
GitLabApiException
(
"setting cannot be null or empty."
);
}
GitLabApiForm
form
=
new
GitLabApiForm
().
withParam
(
setting
,
value
);
Response
response
=
put
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"application"
,
"settings"
);
JsonNode
root
=
response
.
readEntity
(
JsonNode
.
class
);
return
(
parseApplicationSettings
(
root
));
}
/**
* Pareses the returned JSON and returns an ApplicationSettings instance.
*
* @param root the root JsonNode
* @return the populated ApplicationSettings instance
* @throws GitLabApiException if any error occurs
*/
private
final
ApplicationSettings
parseApplicationSettings
(
JsonNode
root
)
throws
GitLabApiException
{
ApplicationSettings
appSettings
=
new
ApplicationSettings
();
Iterator
<
String
>
fieldNames
=
root
.
fieldNames
();
while
(
fieldNames
.
hasNext
())
{
String
fieldName
=
fieldNames
.
next
();
switch
(
fieldName
)
{
case
"id"
:
appSettings
.
setId
(
root
.
path
(
fieldName
).
asInt
());
break
;
case
"created_at"
:
try
{
appSettings
.
setCreatedAt
(
ISO8601
.
toDate
(
root
.
path
(
fieldName
).
asText
()));
}
catch
(
ParseException
pe
)
{
throw
new
GitLabApiException
(
pe
);
}
break
;
case
"updated_at"
:
try
{
appSettings
.
setUpdatedAt
(
ISO8601
.
toDate
(
root
.
path
(
fieldName
).
asText
()));
}
catch
(
ParseException
pe
)
{
throw
new
GitLabApiException
(
pe
);
}
break
;
default
:
Setting
setting
=
Setting
.
forValue
(
fieldName
);
if
(
setting
!=
null
)
{
appSettings
.
addSetting
(
setting
,
root
.
path
(
fieldName
));
}
else
{
GitLabApi
.
getLogger
().
warning
(
String
.
format
(
"Unknown setting: %s, type: %s"
,
fieldName
,
root
.
path
(
fieldName
).
getClass
().
getSimpleName
()));
appSettings
.
addSetting
(
fieldName
,
root
.
path
(
fieldName
));
}
break
;
}
}
return
(
appSettings
);
}
}
src/main/java/org/gitlab4j/api/models/ApplicationSettings.java
0 → 100644
View file @
8c0952a5
package
org.gitlab4j.api.models
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.gitlab4j.api.GitLabApiException
;
import
org.gitlab4j.api.utils.JacksonJson
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.BooleanNode
;
import
com.fasterxml.jackson.databind.node.DoubleNode
;
import
com.fasterxml.jackson.databind.node.FloatNode
;
import
com.fasterxml.jackson.databind.node.IntNode
;
import
com.fasterxml.jackson.databind.node.NullNode
;
import
com.fasterxml.jackson.databind.node.TextNode
;
public
class
ApplicationSettings
{
private
Integer
id
;
private
Date
createdAt
;
private
Date
updatedAt
;
private
Map
<
String
,
Object
>
settings
=
new
HashMap
<>();
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
public
Date
getCreatedAt
()
{
return
createdAt
;
}
public
void
setCreatedAt
(
Date
createdAt
)
{
this
.
createdAt
=
createdAt
;
}
public
Date
getUpdatedAt
()
{
return
updatedAt
;
}
public
void
setUpdatedAt
(
Date
updatedAt
)
{
this
.
updatedAt
=
updatedAt
;
}
public
Map
<
String
,
Object
>
getSettings
()
{
return
settings
;
}
public
void
setSettings
(
Map
<
String
,
Object
>
settings
)
{
this
.
settings
=
settings
;
}
@JsonIgnore
public
Object
getSetting
(
Setting
setting
)
{
if
(
setting
==
null
)
{
return
(
null
);
}
String
name
=
setting
.
toString
();
return
(
settings
.
get
(
name
));
}
@JsonIgnore
public
Object
getSetting
(
String
setting
)
{
if
(
setting
==
null
)
{
return
(
null
);
}
return
(
settings
.
get
(
setting
));
}
public
Object
addSetting
(
String
setting
,
Object
value
)
throws
GitLabApiException
{
Setting
appSetting
=
Setting
.
forValue
(
setting
);
if
(
appSetting
!=
null
)
{
return
(
addSetting
(
appSetting
,
value
));
}
settings
.
put
(
setting
,
value
);
return
(
value
);
}
public
Object
addSetting
(
Setting
setting
,
Object
value
)
throws
GitLabApiException
{
if
(
value
instanceof
JsonNode
)
{
value
=
jsonNodeToValue
((
JsonNode
)
value
);
}
setting
.
validate
(
value
);
settings
.
put
(
setting
.
toString
(),
value
);
return
(
value
);
}
public
Object
removeSetting
(
Setting
setting
)
{
return
settings
.
remove
(
setting
.
toString
());
}
public
Object
removeSetting
(
String
setting
)
{
return
settings
.
remove
(
setting
);
}
public
void
clearSettings
()
{
settings
.
clear
();
}
private
Object
jsonNodeToValue
(
JsonNode
node
)
{
Object
value
=
node
;
if
(
node
instanceof
NullNode
)
{
value
=
null
;
}
else
if
(
node
instanceof
TextNode
)
{
value
=
node
.
asText
();
}
else
if
(
node
instanceof
BooleanNode
)
{
value
=
node
.
asBoolean
();
}
else
if
(
node
instanceof
IntNode
)
{
value
=
node
.
asInt
();
}
else
if
(
node
instanceof
FloatNode
)
{
value
=
(
float
)((
FloatNode
)
node
).
asDouble
();
}
else
if
(
node
instanceof
DoubleNode
)
{
value
=
(
float
)((
DoubleNode
)
node
).
asDouble
();
}
else
if
(
node
instanceof
ArrayNode
)
{
int
numItems
=
node
.
size
();
String
[]
values
=
new
String
[
numItems
];
for
(
int
i
=
0
;
i
<
numItems
;
i
++)
{
values
[
i
]
=
node
.
path
(
i
).
asText
();
}
value
=
values
;
}
return
(
value
);
}
@Override
public
String
toString
()
{
return
(
JacksonJson
.
toJsonString
(
this
));
}
}
src/main/java/org/gitlab4j/api/models/Setting.java
0 → 100644
View file @
8c0952a5
This diff is collapsed.
Click to expand it.
src/test/java/org/gitlab4j/api/TestApplicationSettingsApi.java
0 → 100644
View file @
8c0952a5
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package
org.gitlab4j.api
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assume
.
assumeTrue
;
import
static
org
.
gitlab4j
.
api
.
models
.
Setting
.
LOCAL_MARKDOWN_VERSION
;
import
org.gitlab4j.api.models.ApplicationSettings
;
import
org.junit.AfterClass
;
import
org.junit.Before
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.junit.experimental.categories.Category
;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
* <p>
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* <p>
* If any of the above are NULL, all tests in this class will be skipped.
*/
@Category
(
IntegrationTest
.
class
)
public
class
TestApplicationSettingsApi
extends
AbstractIntegrationTest
{
private
static
GitLabApi
gitLabApi
;
private
static
Object
savedLocalMarkdownVersion
;
private
static
boolean
fetchedApplicationSettings
;
public
TestApplicationSettingsApi
()
{
super
();
}
@BeforeClass
public
static
void
setup
()
{
// Must setup the connection to the GitLab test server
gitLabApi
=
baseTestSetup
();
if
(
gitLabApi
!=
null
)
{
try
{
ApplicationSettings
appSettings
=
gitLabApi
.
getApplicationSettingsApi
().
getApplicationSettings
();
savedLocalMarkdownVersion
=
appSettings
.
getSetting
(
LOCAL_MARKDOWN_VERSION
);
fetchedApplicationSettings
=
true
;
}
catch
(
Exception
ignore
)
{}
}
}
@AfterClass
public
static
void
teardown
()
{
if
(
fetchedApplicationSettings
)
{
try
{
gitLabApi
.
getApplicationSettingsApi
().
updateApplicationSetting
(
LOCAL_MARKDOWN_VERSION
,
savedLocalMarkdownVersion
);
}
catch
(
Exception
ignore
)
{}
}
}
@Before
public
void
beforeMethod
()
{
assumeTrue
(
fetchedApplicationSettings
);
}
@Test
public
void
testGetApplicationSettings
()
throws
GitLabApiException
{
ApplicationSettings
appSettings
=
gitLabApi
.
getApplicationSettingsApi
().
getApplicationSettings
();
assertNotNull
(
appSettings
);
}
@Test
public
void
testUpdateApplicationSetting
()
throws
GitLabApiException
{
int
newValue
=
(
savedLocalMarkdownVersion
!=
null
?
((
Integer
)
savedLocalMarkdownVersion
).
intValue
()
+
1234
:
1234
);
ApplicationSettings
appSettings
=
gitLabApi
.
getApplicationSettingsApi
().
updateApplicationSetting
(
LOCAL_MARKDOWN_VERSION
,
newValue
);
assertNotNull
(
appSettings
);
Object
updatedLocalMarkdownVersion
=
appSettings
.
getSetting
(
LOCAL_MARKDOWN_VERSION
);
assertEquals
(
newValue
,
updatedLocalMarkdownVersion
);
appSettings
=
gitLabApi
.
getApplicationSettingsApi
().
updateApplicationSetting
(
LOCAL_MARKDOWN_VERSION
,
savedLocalMarkdownVersion
);
updatedLocalMarkdownVersion
=
appSettings
.
getSetting
(
LOCAL_MARKDOWN_VERSION
);
assertEquals
(
savedLocalMarkdownVersion
,
updatedLocalMarkdownVersion
);
}
@Test
public
void
testUpdateApplicationSettings
()
throws
GitLabApiException
{
// Arrange
int
newValue
=
(
savedLocalMarkdownVersion
!=
null
?
((
Integer
)
savedLocalMarkdownVersion
).
intValue
()
+
123
:
123
);
ApplicationSettings
appSettings
=
new
ApplicationSettings
();
appSettings
.
addSetting
(
LOCAL_MARKDOWN_VERSION
,
newValue
);
// Act
ApplicationSettings
updatedAppSettings
=
gitLabApi
.
getApplicationSettingsApi
().
updateApplicationSettings
(
appSettings
);
// Assert
assertNotNull
(
updatedAppSettings
);
Object
updatedLocalMarkdownVersion
=
updatedAppSettings
.
getSetting
(
LOCAL_MARKDOWN_VERSION
);
assertEquals
(
newValue
,
updatedLocalMarkdownVersion
);
// Arrange
appSettings
=
new
ApplicationSettings
();
appSettings
.
addSetting
(
LOCAL_MARKDOWN_VERSION
,
savedLocalMarkdownVersion
);
// Act
updatedAppSettings
=
gitLabApi
.
getApplicationSettingsApi
().
updateApplicationSettings
(
appSettings
);
// Assert
updatedLocalMarkdownVersion
=
updatedAppSettings
.
getSetting
(
LOCAL_MARKDOWN_VERSION
);
assertEquals
(
savedLocalMarkdownVersion
,
updatedLocalMarkdownVersion
);
}
}
src/test/java/org/gitlab4j/api/TestApplications.java
→
src/test/java/org/gitlab4j/api/TestApplications
Api
.java
View file @
8c0952a5
File moved
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment