Skip to content

Commit ec18994

Browse files
committed
Mods for Java 8 and for new getOptionalXyz() methods (#127).
1 parent 8d0ddc1 commit ec18994

File tree

9 files changed

+404
-16
lines changed

9 files changed

+404
-16
lines changed

pom.xml

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>org.gitlab4j</groupId>
66
<artifactId>gitlab4j-api</artifactId>
77
<packaging>jar</packaging>
8-
<version>4.7.18-SNAPSHOT</version>
8+
<version>4.8.0-SNAPSHOT</version>
99
<name>GitLab API Java Client</name>
1010
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API.</description>
1111
<url>https://github.com/gmessner/gitlab4j-api</url>
@@ -38,7 +38,8 @@
3838
</developers>
3939

4040
<properties>
41-
<jdk.version>1.7</jdk.version>
41+
<java.source.version>1.8</java.source.version>
42+
<java.target.version>1.8</java.target.version>
4243
<jersey.version>2.26</jersey.version>
4344
<jackson.version>2.9.3</jackson.version>
4445
<javaServlet.version>3.1.0</javaServlet.version>
@@ -60,15 +61,15 @@
6061
<plugin>
6162
<groupId>org.apache.maven.plugins</groupId>
6263
<artifactId>maven-release-plugin</artifactId>
63-
<version>2.5.1</version>
64+
<version>2.5.3</version>
6465
<configuration>
6566
<goals>deploy</goals>
6667
</configuration>
6768
</plugin>
6869

6970
<plugin>
7071
<artifactId>maven-scm-plugin</artifactId>
71-
<version>1.9.2</version>
72+
<version>1.9.5</version>
7273
<configuration>
7374
<tag>${project.artifactId}-${project.version}</tag>
7475
</configuration>
@@ -77,17 +78,27 @@
7778
<plugin>
7879
<groupId>org.apache.maven.plugins</groupId>
7980
<artifactId>maven-compiler-plugin</artifactId>
80-
<version>3.2</version>
81+
<version>3.7.0</version>
8182
<configuration>
82-
<source>${jdk.version}</source>
83-
<target>${jdk.version}</target>
83+
<source>${java.source.version}</source>
84+
<target>${java.target.version}</target>
8485
</configuration>
86+
<executions>
87+
<execution>
88+
<id>default-compile</id>
89+
<configuration>
90+
<excludes>
91+
<exclude>**/module-info.java</exclude>
92+
</excludes>
93+
</configuration>
94+
</execution>
95+
</executions>
8596
</plugin>
8697

87-
<plugin>
98+
<plugin>
8899
<groupId>org.apache.maven.plugins</groupId>
89100
<artifactId>maven-gpg-plugin</artifactId>
90-
<version>1.5</version>
101+
<version>1.6</version>
91102
<executions>
92103
<execution>
93104
<id>sign-artifacts</id>
@@ -102,7 +113,7 @@
102113
<plugin>
103114
<groupId>org.apache.maven.plugins</groupId>
104115
<artifactId>maven-source-plugin</artifactId>
105-
<version>2.4</version>
116+
<version>3.0.0</version>
106117
<executions>
107118
<execution>
108119
<id>attach-sources</id>
@@ -116,7 +127,7 @@
116127
<plugin>
117128
<groupId>org.apache.maven.plugins</groupId>
118129
<artifactId>maven-javadoc-plugin</artifactId>
119-
<version>2.10.1</version>
130+
<version>3.0.0</version>
120131
<executions>
121132
<execution>
122133
<id>attach-javadocs</id>
@@ -130,7 +141,7 @@
130141
<plugin>
131142
<groupId>org.jacoco</groupId>
132143
<artifactId>jacoco-maven-plugin</artifactId>
133-
<version>0.7.9</version>
144+
<version>0.8.0</version>
134145
<executions>
135146
<execution>
136147
<id>default-prepare-agent</id>
@@ -173,7 +184,7 @@
173184
<plugin>
174185
<groupId>org.apache.maven.plugins</groupId>
175186
<artifactId>maven-surefire-plugin</artifactId>
176-
<version>2.12.2</version>
187+
<version>2.19.1</version>
177188
<configuration>
178189
<systemPropertyVariables>
179190
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
@@ -184,6 +195,7 @@
184195
</build>
185196

186197
<dependencies>
198+
<!-- jaxb-api and javax.activation added for Java 9 compatibility -->
187199
<dependency>
188200
<groupId>javax.xml.bind</groupId>
189201
<artifactId>jaxb-api</artifactId>
@@ -194,6 +206,7 @@
194206
<artifactId>javax.activation</artifactId>
195207
<version>1.2.0</version>
196208
</dependency>
209+
197210
<dependency>
198211
<groupId>com.fasterxml.jackson.jaxrs</groupId>
199212
<artifactId>jackson-jaxrs-json-provider</artifactId>

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.gitlab4j.api;
22

3+
import java.util.Collections;
34
import java.util.Map;
5+
import java.util.Optional;
6+
import java.util.WeakHashMap;
47

58
import javax.ws.rs.core.Response;
69

@@ -28,6 +31,10 @@ public String getApiNamespace() {
2831
}
2932
}
3033

34+
// Used to keep track of GitLabApiExceptions on calls that return Optionsl<?>
35+
private static final Map<Optional<?>, GitLabApiException> optionalExceptionMap =
36+
Collections.synchronizedMap(new WeakHashMap<Optional<?>, GitLabApiException>());
37+
3138
GitLabApiClient apiClient;
3239
private ApiVersion apiVersion;
3340
private String gitLabServerUrl;
@@ -55,7 +62,6 @@ public String getApiNamespace() {
5562
private NotesApi notesApi;
5663
private EventsApi eventsApi;
5764

58-
5965
/**
6066
* Create a new GitLabApi instance that is logically a duplicate of this instance, with the exception off sudo state.
6167
*
@@ -993,4 +999,46 @@ public UserApi getUserApi() {
993999

9941000
return (userApi);
9951001
}
1002+
1003+
/**
1004+
* Create and return an Optional instance associated with a GitLabApiException.
1005+
*
1006+
* @param optional the Optional instance to use as the key for the exception
1007+
* @param glae the GitLabApiException that was the result of a call to the GitLab API
1008+
*/
1009+
protected static final <T> Optional<T> createOptionalFromException(GitLabApiException glae) {
1010+
Optional<T> optional = Optional.empty();
1011+
optionalExceptionMap.put(optional, glae);
1012+
return (optional);
1013+
}
1014+
1015+
/**
1016+
* Get the exception associated with the provided Optional instance, or null if no exception is
1017+
* associated with the Optional instance.
1018+
*
1019+
* @param optional the Optional instance to get the exception for
1020+
* @return the exception associated with the provided Optional instance, or null if no exception is
1021+
* associated with the Optional instance
1022+
*/
1023+
public static final GitLabApiException getOptionalException(Optional<?> optional) {
1024+
return (optionalExceptionMap.get(optional));
1025+
}
1026+
1027+
/**
1028+
* Return the Optional instances contained value, if present, otherwise throw the exception that is
1029+
* associated with the Optional instance.
1030+
*
1031+
* @param optional the Optional instance to get the value for
1032+
* @return the value of the Optional instance if no exception is associated with it
1033+
* @throws GitLabApiException if there was an exception associated with the Optional instance
1034+
*/
1035+
public static final <T> T orElseThrow(Optional<T> optional) throws GitLabApiException {
1036+
1037+
GitLabApiException glea = getOptionalException(optional);
1038+
if (glea != null) {
1039+
throw (glea);
1040+
}
1041+
1042+
return (optional.get());
1043+
}
9961044
}

src/main/java/org/gitlab4j/api/GroupApi.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Date;
44
import java.util.List;
5+
import java.util.Optional;
56

67
import javax.ws.rs.core.Form;
78
import javax.ws.rs.core.GenericType;
@@ -12,7 +13,6 @@
1213
import org.gitlab4j.api.models.Group;
1314
import org.gitlab4j.api.models.Member;
1415
import org.gitlab4j.api.models.Project;
15-
import org.gitlab4j.api.models.User;
1616
import org.gitlab4j.api.models.Visibility;
1717

1818
/**
@@ -281,6 +281,18 @@ public Group getGroup(Integer groupId) throws GitLabApiException {
281281
return getGroup(groupId.toString());
282282
}
283283

284+
/**
285+
* Get all details of a group as an Optional instance.
286+
*
287+
* GET /groups/:id
288+
*
289+
* @param groupId the group ID to get
290+
* @return the Group for the specified group ID as an Optional instance
291+
*/
292+
public Optional<Group> getOptionalGroup(Integer groupId) {
293+
return (getOptionalGroup(groupId.toString()));
294+
}
295+
284296
/**
285297
* Get all details of a group.
286298
*
@@ -295,6 +307,22 @@ public Group getGroup(String groupPath) throws GitLabApiException {
295307
return (response.readEntity(Group.class));
296308
}
297309

310+
/**
311+
* Get all details of a group as an Optional instance.
312+
*
313+
* GET /groups/:id
314+
*
315+
* @param groupPath the path of the group to get details for
316+
* @return the Group for the specified group path as an Optional instance
317+
*/
318+
public Optional<Group> getOptionalGroup(String groupPath) {
319+
try {
320+
return (Optional.ofNullable(getGroup(groupPath)));
321+
} catch (GitLabApiException glae) {
322+
return (GitLabApi.createOptionalFromException(glae));
323+
}
324+
}
325+
298326
/**
299327
* Creates a new project group. Available only for users who can create groups.
300328
*
@@ -543,6 +571,23 @@ public Member getMember(int groupId, int userId) throws GitLabApiException {
543571
return (response.readEntity(new GenericType<Member>() {}));
544572
}
545573

574+
/**
575+
* Get a group member viewable by the authenticated user as an Optional instance.
576+
*
577+
* GET /groups/:id/members/:id
578+
*
579+
* @param groupId the group ID to get the member for
580+
* @param userId the member ID of the member to get
581+
* @return a member viewable by the authenticated user as an Optional instance
582+
*/
583+
public Optional<Member> getOptionalMember(int groupId, int userId) throws GitLabApiException {
584+
try {
585+
return (Optional.ofNullable(getMember(groupId, userId)));
586+
} catch (GitLabApiException glae) {
587+
return (GitLabApi.createOptionalFromException(glae));
588+
}
589+
}
590+
546591
/**
547592
* Adds a user to the list of group members.
548593
*

0 commit comments

Comments
 (0)