Skip to content

Commit a6fb4a7

Browse files
authored
Add MetatdataApi (#1025)
Fixes #992
1 parent 2d9a588 commit a6fb4a7

File tree

6 files changed

+201
-1
lines changed

6 files changed

+201
-1
lines changed

Diff for: src/main/java/org/gitlab4j/api/GitLabApi.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public String getApiNamespace() {
9999
private UserApi userApi;
100100
private WikisApi wikisApi;
101101
private KeysApi keysApi;
102+
private MetadataApi metadataApi;
102103

103104
/**
104105
* Get the GitLab4J shared Logger instance.
@@ -1463,7 +1464,7 @@ public ReleaseLinksApi getReleaseLinksApi() {
14631464

14641465
return releaseLinksApi;
14651466
}
1466-
1467+
14671468
/**
14681469
* Gets the ReleasesApi instance owned by this GitLabApi instance. The ReleasesApi is used
14691470
* to perform all release related API calls.
@@ -1739,6 +1740,21 @@ public KeysApi getKeysAPI() {
17391740
return keysApi;
17401741
}
17411742

1743+
/**
1744+
* Gets the MetadataApi instance owned by this GitlabApi instance. The MetadataApi is used to
1745+
* retrieve metadata information for this GitLab instance
1746+
*
1747+
* @return the MetadataApi instance owned by this GitlabApi instance
1748+
*/
1749+
public MetadataApi getMetadataApi() {
1750+
synchronized (this) {
1751+
if (metadataApi == null) {
1752+
metadataApi = new MetadataApi(this);
1753+
}
1754+
}
1755+
return metadataApi;
1756+
}
1757+
17421758

17431759
/**
17441760
* Create and return an Optional instance associated with a GitLabApiException.

Diff for: src/main/java/org/gitlab4j/api/MetadataApi.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.gitlab4j.api;
2+
3+
import javax.ws.rs.core.Response;
4+
import javax.ws.rs.core.Response.Status;
5+
import org.gitlab4j.api.models.Metadata;
6+
7+
/**
8+
* This class implements the client side API for the Gitlab metadata call.
9+
*
10+
* @see <a href="https://https://docs.gitlab.com/ee/api/metadata.html">Metadata API at Gitlab</a>
11+
*/
12+
public class MetadataApi extends AbstractApi {
13+
14+
public MetadataApi(GitLabApi gitLabApi) {
15+
super(gitLabApi);
16+
}
17+
18+
/**
19+
* Get Gitlab metadata
20+
*
21+
* <pre><code>Gitlab Endpoint: GET /metadata</code></pre>
22+
*
23+
* @return Gitlab metadata
24+
* @throws GitLabApiException if any exception occurs
25+
*/
26+
public Metadata getMetadata() throws GitLabApiException {
27+
Response response = get(Status.OK, null, "metadata");
28+
return (response.readEntity(Metadata.class));
29+
}
30+
31+
}

Diff for: src/main/java/org/gitlab4j/api/models/Metadata.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.gitlab4j.api.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import org.gitlab4j.api.utils.JacksonJson;
5+
6+
public class Metadata {
7+
8+
private String version;
9+
private String revision;
10+
private Kas kas;
11+
private Boolean enterprise;
12+
13+
public String getVersion() {
14+
return version;
15+
}
16+
17+
public void setVersion(String version) {
18+
this.version = version;
19+
}
20+
21+
public String getRevision() {
22+
return revision;
23+
}
24+
25+
public void setRevision(String revision) {
26+
this.revision = revision;
27+
}
28+
29+
public Kas getKas() {
30+
return kas;
31+
}
32+
33+
public void setKas(Kas kas) {
34+
this.kas = kas;
35+
}
36+
37+
public Boolean getEnterprise() {
38+
return enterprise;
39+
}
40+
41+
public void setEnterprise(Boolean enterprise) {
42+
this.enterprise = enterprise;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return (JacksonJson.toJsonString(this));
48+
}
49+
50+
private static class Kas {
51+
52+
private Boolean enabled;
53+
@JsonProperty("externalUrl")
54+
private String externalUrl;
55+
private String version;
56+
57+
public Boolean getEnabled() {
58+
return enabled;
59+
}
60+
61+
public void setEnabled(Boolean enabled) {
62+
this.enabled = enabled;
63+
}
64+
65+
public String getExternalUrl() {
66+
return externalUrl;
67+
}
68+
69+
public void setExternalUrl(String externalUrl) {
70+
this.externalUrl = externalUrl;
71+
}
72+
73+
public String getVersion() {
74+
return version;
75+
}
76+
77+
public void setVersion(String version) {
78+
this.version = version;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return (JacksonJson.toJsonString(this));
84+
}
85+
}
86+
87+
88+
89+
}

Diff for: src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import org.gitlab4j.api.models.MergeRequest;
8888
import org.gitlab4j.api.models.MergeRequestDiff;
8989
import org.gitlab4j.api.models.MergeRequestVersion;
90+
import org.gitlab4j.api.models.Metadata;
9091
import org.gitlab4j.api.models.Milestone;
9192
import org.gitlab4j.api.models.Note;
9293
import org.gitlab4j.api.models.NotificationSettings;
@@ -790,4 +791,10 @@ public void testProjectAccessToken() throws Exception {
790791
ProjectAccessToken token = unmarshalResource(ProjectAccessToken.class, "project-access-token.json");
791792
assertTrue(compareJson(token, "project-access-token.json"));
792793
}
794+
795+
@Test
796+
public void testMetadata() throws Exception {
797+
Metadata metadata = unmarshalResource(Metadata.class, "metadata.json");
798+
assertTrue(compareJson(metadata, "metadata.json"));
799+
}
793800
}

Diff for: src/test/java/org/gitlab4j/api/TestMetadataApi.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4+
5+
import org.gitlab4j.api.models.Metadata;
6+
import org.gitlab4j.api.models.Project;
7+
import org.gitlab4j.api.models.User;
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Disabled;
11+
import org.junit.jupiter.api.Tag;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
15+
@Tag("integration")
16+
@ExtendWith(SetupIntegrationTestExtension.class)
17+
@Disabled("Required Gitlab version not less then 15.6")
18+
public class TestMetadataApi extends AbstractIntegrationTest {
19+
20+
private static GitLabApi gitLabApi;
21+
private static Project testProject;
22+
private static User currentUser;
23+
24+
public TestMetadataApi() {
25+
super();
26+
}
27+
28+
@BeforeAll
29+
public static void setup() {
30+
gitLabApi = baseTestSetup();
31+
testProject = getTestProject();
32+
currentUser = getCurrentUser();
33+
}
34+
35+
@BeforeEach
36+
public void beforeMethod() {
37+
assumeTrue(gitLabApi != null);
38+
}
39+
40+
@Test
41+
public void testGetMetadata() throws GitLabApiException {
42+
Metadata metadata = gitLabApi.getMetadataApi().getMetadata();
43+
System.out.println("METADATA +\n" + metadata);
44+
}
45+
46+
47+
}

Diff for: src/test/resources/org/gitlab4j/api/metadata.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "15.11.13",
3+
"revision": "cc3748fcd2d",
4+
"kas": {
5+
"enabled": true,
6+
"externalUrl": "ws://820bd8a8b4f6/-/kubernetes-agent/",
7+
"version": "v15.11.0"
8+
},
9+
"enterprise": false
10+
}

0 commit comments

Comments
 (0)