Skip to content

Commit bcd278b

Browse files
committed
Added Optional<> support (#127).
1 parent 02f1b7f commit bcd278b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.URLEncoder;
55
import java.util.Date;
66
import java.util.List;
7+
import java.util.Optional;
78

89
import javax.ws.rs.core.Form;
910
import javax.ws.rs.core.GenericType;
@@ -194,6 +195,23 @@ public Commit getCommit(int projectId, String sha) throws GitLabApiException {
194195
return (response.readEntity(Commit.class));
195196
}
196197

198+
/**
199+
* Get a specific commit identified by the commit hash or name of a branch or tag as an Optional instance
200+
*
201+
* GET /projects/:id/repository/commits/:sha
202+
*
203+
* @param projectId the project ID that the commit belongs to
204+
* @param sha a commit hash or name of a branch or tag
205+
* @return the Commit for the specified project ID/sha pair as an Optional instance
206+
*/
207+
public Optional<Commit> getOptionalCommit(int projectId, String sha) {
208+
try {
209+
return (Optional.ofNullable(getCommit(projectId, sha)));
210+
} catch (GitLabApiException glae) {
211+
return (GitLabApi.createOptionalFromException(glae));
212+
}
213+
}
214+
197215
/**
198216
* Get the list of diffs of a commit in a project.
199217
*

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.util.List;
4+
import java.util.Optional;
45

56
import javax.ws.rs.core.Form;
67
import javax.ws.rs.core.GenericType;
@@ -138,6 +139,23 @@ public DeployKey getDeployKey(Integer projectId, Integer keyId) throws GitLabApi
138139
return (response.readEntity(DeployKey.class));
139140
}
140141

142+
/**
143+
* Get a single deploy key for the specified project as an Optional instance.
144+
*
145+
* GET /projects/:id/deploy_keys/:key_id
146+
*
147+
* @param projectId the ID of the project
148+
* @param keyId the ID of the deploy key to delete
149+
* @return the DeployKey for the specified project ID and key ID as an Optional instance
150+
*/
151+
public Optional<DeployKey> getOptionalDeployKey(Integer projectId, Integer keyId) {
152+
try {
153+
return (Optional.ofNullable(getDeployKey(projectId, keyId)));
154+
} catch (GitLabApiException glae) {
155+
return (GitLabApi.createOptionalFromException(glae));
156+
}
157+
}
158+
141159
/**
142160
* Creates a new deploy key for a project.
143161
*

0 commit comments

Comments
 (0)