Skip to content

Commit

Permalink
Add MetatdataApi (#1025)
Browse files Browse the repository at this point in the history
Fixes #992
  • Loading branch information
DoubleDPro authored Sep 5, 2023
1 parent 2d9a588 commit a6fb4a7
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/java/org/gitlab4j/api/GitLabApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public String getApiNamespace() {
private UserApi userApi;
private WikisApi wikisApi;
private KeysApi keysApi;
private MetadataApi metadataApi;

/**
* Get the GitLab4J shared Logger instance.
Expand Down Expand Up @@ -1463,7 +1464,7 @@ public ReleaseLinksApi getReleaseLinksApi() {

return releaseLinksApi;
}

/**
* Gets the ReleasesApi instance owned by this GitLabApi instance. The ReleasesApi is used
* to perform all release related API calls.
Expand Down Expand Up @@ -1739,6 +1740,21 @@ public KeysApi getKeysAPI() {
return keysApi;
}

/**
* Gets the MetadataApi instance owned by this GitlabApi instance. The MetadataApi is used to
* retrieve metadata information for this GitLab instance
*
* @return the MetadataApi instance owned by this GitlabApi instance
*/
public MetadataApi getMetadataApi() {
synchronized (this) {
if (metadataApi == null) {
metadataApi = new MetadataApi(this);
}
}
return metadataApi;
}


/**
* Create and return an Optional instance associated with a GitLabApiException.
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/gitlab4j/api/MetadataApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.gitlab4j.api;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gitlab4j.api.models.Metadata;

/**
* This class implements the client side API for the Gitlab metadata call.
*
* @see <a href="https://https://docs.gitlab.com/ee/api/metadata.html">Metadata API at Gitlab</a>
*/
public class MetadataApi extends AbstractApi {

public MetadataApi(GitLabApi gitLabApi) {
super(gitLabApi);
}

/**
* Get Gitlab metadata
*
* <pre><code>Gitlab Endpoint: GET /metadata</code></pre>
*
* @return Gitlab metadata
* @throws GitLabApiException if any exception occurs
*/
public Metadata getMetadata() throws GitLabApiException {
Response response = get(Status.OK, null, "metadata");
return (response.readEntity(Metadata.class));
}

}
89 changes: 89 additions & 0 deletions src/main/java/org/gitlab4j/api/models/Metadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.gitlab4j.api.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.gitlab4j.api.utils.JacksonJson;

public class Metadata {

private String version;
private String revision;
private Kas kas;
private Boolean enterprise;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getRevision() {
return revision;
}

public void setRevision(String revision) {
this.revision = revision;
}

public Kas getKas() {
return kas;
}

public void setKas(Kas kas) {
this.kas = kas;
}

public Boolean getEnterprise() {
return enterprise;
}

public void setEnterprise(Boolean enterprise) {
this.enterprise = enterprise;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}

private static class Kas {

private Boolean enabled;
@JsonProperty("externalUrl")
private String externalUrl;
private String version;

public Boolean getEnabled() {
return enabled;
}

public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}

public String getExternalUrl() {
return externalUrl;
}

public void setExternalUrl(String externalUrl) {
this.externalUrl = externalUrl;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}



}
7 changes: 7 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestDiff;
import org.gitlab4j.api.models.MergeRequestVersion;
import org.gitlab4j.api.models.Metadata;
import org.gitlab4j.api.models.Milestone;
import org.gitlab4j.api.models.Note;
import org.gitlab4j.api.models.NotificationSettings;
Expand Down Expand Up @@ -790,4 +791,10 @@ public void testProjectAccessToken() throws Exception {
ProjectAccessToken token = unmarshalResource(ProjectAccessToken.class, "project-access-token.json");
assertTrue(compareJson(token, "project-access-token.json"));
}

@Test
public void testMetadata() throws Exception {
Metadata metadata = unmarshalResource(Metadata.class, "metadata.json");
assertTrue(compareJson(metadata, "metadata.json"));
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/gitlab4j/api/TestMetadataApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.gitlab4j.api;

import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.gitlab4j.api.models.Metadata;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
@Disabled("Required Gitlab version not less then 15.6")
public class TestMetadataApi extends AbstractIntegrationTest {

private static GitLabApi gitLabApi;
private static Project testProject;
private static User currentUser;

public TestMetadataApi() {
super();
}

@BeforeAll
public static void setup() {
gitLabApi = baseTestSetup();
testProject = getTestProject();
currentUser = getCurrentUser();
}

@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}

@Test
public void testGetMetadata() throws GitLabApiException {
Metadata metadata = gitLabApi.getMetadataApi().getMetadata();
System.out.println("METADATA +\n" + metadata);
}


}
10 changes: 10 additions & 0 deletions src/test/resources/org/gitlab4j/api/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "15.11.13",
"revision": "cc3748fcd2d",
"kas": {
"enabled": true,
"externalUrl": "ws://820bd8a8b4f6/-/kubernetes-agent/",
"version": "v15.11.0"
},
"enterprise": false
}

0 comments on commit a6fb4a7

Please sign in to comment.