Skip to content

Commit 7783c72

Browse files
authored
Add ErrorTrackingApi (#1295)
1 parent 06b1f9e commit 7783c72

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
5+
import jakarta.ws.rs.core.GenericType;
6+
import jakarta.ws.rs.core.Response;
7+
8+
import org.gitlab4j.api.models.ErrorTrackingClientKey;
9+
10+
/**
11+
* This class provides an entry point to the GitLab API error tracking.
12+
* <a href="https://docs.gitlab.com/api/error_tracking/">GitLab Error tracking API Documentation</a>
13+
*/
14+
public class ErrorTrackingApi extends AbstractApi {
15+
16+
public ErrorTrackingApi(GitLabApi gitLabApi) {
17+
super(gitLabApi);
18+
}
19+
20+
/**
21+
* Creates an integrated error tracking client key for a specified project.
22+
*
23+
* <pre><code>GitLab Endpoint: POST /projects/:id/error_tracking/client_keys</code></pre>
24+
*
25+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
26+
* @return the created ErrorTrackingClientKey
27+
* @throws GitLabApiException if any exception occurs
28+
*/
29+
public ErrorTrackingClientKey createClientKey(Object projectIdOrPath) throws GitLabApiException {
30+
GitLabApiForm formData = new GitLabApiForm();
31+
Response response = post(
32+
Response.Status.CREATED,
33+
formData,
34+
"projects",
35+
getProjectIdOrPath(projectIdOrPath),
36+
"error_tracking",
37+
"client_keys");
38+
return (response.readEntity(ErrorTrackingClientKey.class));
39+
}
40+
41+
/**
42+
* Lists all integrated error tracking client keys for a specified project.
43+
*
44+
* <pre><code>GitLab Endpoint: GET /projects/:id/error_tracking/client_keys</code></pre>
45+
*
46+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
47+
* @return a list of ErrorTrackingClientKey
48+
* @throws GitLabApiException if any exception occurs
49+
*/
50+
public List<ErrorTrackingClientKey> getClientKeys(Object projectIdOrPath) throws GitLabApiException {
51+
Response response = get(
52+
Response.Status.OK,
53+
null,
54+
"projects",
55+
getProjectIdOrPath(projectIdOrPath),
56+
"error_tracking",
57+
"client_keys");
58+
return (response.readEntity(new GenericType<>() {}));
59+
}
60+
}

gitlab4j-api/src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public String getApiNamespace() {
103103
private WikisApi wikisApi;
104104
private KeysApi keysApi;
105105
private MetadataApi metadataApi;
106+
private ErrorTrackingApi errorTrackingApi;
106107

107108
/**
108109
* Get the GitLab4J shared Logger instance.
@@ -1897,6 +1898,23 @@ public MetadataApi getMetadataApi() {
18971898
return metadataApi;
18981899
}
18991900

1901+
/**
1902+
* Gets the ErrorTrackingApi instance owned by this GitLabApi instance.
1903+
*
1904+
* @return the ErrorTrackingApi instance owned by this GitLabApi instance
1905+
*/
1906+
public ErrorTrackingApi getErrorTrackingApi() {
1907+
if (errorTrackingApi == null) {
1908+
synchronized (this) {
1909+
if (errorTrackingApi == null) {
1910+
errorTrackingApi = new ErrorTrackingApi(this);
1911+
}
1912+
}
1913+
}
1914+
1915+
return errorTrackingApi;
1916+
}
1917+
19001918
/**
19011919
* Create and return an Optional instance associated with a GitLabApiException.
19021920
*
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
5+
6+
import java.util.List;
7+
8+
import org.gitlab4j.api.models.ErrorTrackingClientKey;
9+
import org.gitlab4j.api.models.Project;
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Tag;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
16+
/**
17+
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
18+
* <p>
19+
* TEST_NAMESPACE
20+
* TEST_PROJECT_NAME
21+
* TEST_HOST_URL
22+
* TEST_PRIVATE_TOKEN
23+
* <p>
24+
* If any of the above are NULL, all tests in this class will be skipped.
25+
*/
26+
@Tag("integration")
27+
@ExtendWith(SetupIntegrationTestExtension.class)
28+
@org.junit.jupiter.api.Disabled(
29+
"Integration tests are disabled, see https://github.com/gitlab4j/gitlab4j-api/issues/1165")
30+
public class TestErrorTrackingApi extends AbstractIntegrationTest {
31+
32+
private static GitLabApi gitLabApi;
33+
private static Project testProject;
34+
35+
@BeforeAll
36+
public static void setup() {
37+
gitLabApi = baseTestSetup();
38+
testProject = getTestProject();
39+
}
40+
41+
@BeforeEach
42+
public void beforeMethod() {
43+
assumeTrue(gitLabApi != null);
44+
assumeTrue(testProject != null);
45+
}
46+
47+
@Test
48+
public void testCreateClientKey() throws GitLabApiException {
49+
assertNotNull(testProject);
50+
Long projectId = testProject.getId();
51+
ErrorTrackingClientKey errorTrackingClientKey =
52+
gitLabApi.getErrorTrackingApi().createClientKey(projectId);
53+
assertNotNull(errorTrackingClientKey);
54+
}
55+
56+
@Test
57+
public void testGetClientKeys() throws GitLabApiException {
58+
assertNotNull(testProject);
59+
Long projectId = testProject.getId();
60+
ErrorTrackingClientKey errorTrackingClientKey =
61+
gitLabApi.getErrorTrackingApi().createClientKey(projectId);
62+
List<ErrorTrackingClientKey> clientKeys =
63+
gitLabApi.getErrorTrackingApi().getClientKeys(projectId);
64+
assertEquals(errorTrackingClientKey, clientKeys.get(0));
65+
}
66+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.io.Serializable;
4+
5+
import org.gitlab4j.models.utils.JacksonJson;
6+
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
public class ErrorTrackingClientKey implements Serializable {
10+
private static final long serialVersionUID = 1L;
11+
12+
private Long id;
13+
private Boolean active;
14+
15+
@JsonProperty("public_key")
16+
private String publicKey;
17+
18+
@JsonProperty("sentry_dsn")
19+
private String sentryDsn;
20+
21+
public String getPublicKey() {
22+
return publicKey;
23+
}
24+
25+
public void setPublicKey(String publicKey) {
26+
this.publicKey = publicKey;
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public Boolean getActive() {
38+
return active;
39+
}
40+
41+
public void setActive(Boolean active) {
42+
this.active = active;
43+
}
44+
45+
public String getSentryDsn() {
46+
return sentryDsn;
47+
}
48+
49+
public void setSentryDsn(String sentryDsn) {
50+
this.sentryDsn = sentryDsn;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return (JacksonJson.toJsonString(this));
56+
}
57+
}

gitlab4j-models/src/test/java/org/gitlab4j/models/TestGitLabApiBeans.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,4 +820,11 @@ public void testWebhook() throws Exception {
820820
GroupHook groupHook = unmarshalResource(GroupHook.class, "group-hook.json");
821821
assertTrue(compareJson(groupHook, "group-hook.json"));
822822
}
823+
824+
@Test
825+
public void testErrorTrackingClientKey() throws Exception {
826+
ErrorTrackingClientKey errorTrackingClientKey =
827+
unmarshalResource(ErrorTrackingClientKey.class, "error-tracking-client-key.json");
828+
assertTrue(compareJson(errorTrackingClientKey, "error-tracking-client-key.json"));
829+
}
823830
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": 3,
3+
"active": true,
4+
"public_key": "glet_0ff98b1d849c083f76d0bc545ed053a3",
5+
"sentry_dsn": "https://[email protected]/errortracking/api/v1/projects/5"
6+
}

0 commit comments

Comments
 (0)