Skip to content

Commit ebc2eb0

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

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.Date;
2727
import java.util.List;
28+
import java.util.Optional;
2829

2930
import javax.ws.rs.core.GenericType;
3031
import javax.ws.rs.core.Response;
@@ -147,6 +148,23 @@ public Issue getIssue(Integer projectId, Integer issueId) throws GitLabApiExcept
147148
return (response.readEntity(Issue.class));
148149
}
149150

151+
/**
152+
* Get a single project issue as an Optional instance.
153+
*
154+
* GET /projects/:id/issues/:issue_iid
155+
*
156+
* @param projectId the project ID to get the issue for
157+
* @param issueId the internal ID of a project's issue
158+
* @return the specified Issue as an Optional instance
159+
*/
160+
public Optional<Issue> getOptionalIssue(Integer projectId, Integer issueId) {
161+
try {
162+
return (Optional.ofNullable(getIssue(projectId, issueId)));
163+
} catch (GitLabApiException glae) {
164+
return (GitLabApi.createOptionalFromException(glae));
165+
}
166+
}
167+
150168
/**
151169
* Create an issue for the project.
152170
*
@@ -462,13 +480,13 @@ public TimeStats resetSpentTime(Integer projectId, Integer issueIid) throws GitL
462480
}
463481

464482
/**
465-
* Get time tracking stats
483+
* Get time tracking stats.
466484
*
467485
* GET /projects/:id/issues/:issue_iid/time_stats
468486
*
469487
* @param projectId the project ID that owns the issue
470488
* @param issueIid the internal ID of a project's issue
471-
* @return a TimeSTats instance
489+
* @return a TimeStats instance
472490
* @throws GitLabApiException if any exception occurs
473491
*/
474492
public TimeStats getTimeTrackingStats(Integer projectId, Integer issueIid) throws GitLabApiException {
@@ -484,4 +502,21 @@ public TimeStats getTimeTrackingStats(Integer projectId, Integer issueIid) throw
484502
Response response = get(Response.Status.OK, new GitLabApiForm().asMap(), "projects", projectId, "issues", issueIid, "time_stats");
485503
return (response.readEntity(TimeStats.class));
486504
}
505+
506+
/**
507+
* Get time tracking stats as an Optional instance
508+
*
509+
* GET /projects/:id/issues/:issue_iid/time_stats
510+
*
511+
* @param projectId the project ID that owns the issue
512+
* @param issueIid the internal ID of a project's issue
513+
* @return a TimeStats as an Optional instance
514+
*/
515+
public Optional<TimeStats> getOptionalTimeTrackingStats(Integer projectId, Integer issueIid) {
516+
try {
517+
return (Optional.ofNullable(getTimeTrackingStats(projectId, issueIid)));
518+
} catch (GitLabApiException glae) {
519+
return (GitLabApi.createOptionalFromException(glae));
520+
}
521+
}
487522
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.file.Files;
77
import java.nio.file.StandardCopyOption;
88
import java.util.List;
9+
import java.util.Optional;
910

1011
import javax.ws.rs.core.Form;
1112
import javax.ws.rs.core.GenericType;
@@ -134,6 +135,23 @@ public Job getJob(int projectId, int jobId) throws GitLabApiException {
134135
return (response.readEntity(Job.class));
135136
}
136137

138+
/**
139+
* Get single job in a project as an Optional instance.
140+
*
141+
* GET /projects/:id/jobs/:job_id
142+
*
143+
* @param projectId the project ID to get the specified job for
144+
* @param jobId the job ID to get
145+
* @return a single job for the specified project ID as an Optional intance
146+
*/
147+
public Optional<Job> getOptionalJob(int projectId, int jobId) {
148+
try {
149+
return (Optional.ofNullable(getJob(projectId, jobId)));
150+
} catch (GitLabApiException glae) {
151+
return (GitLabApi.createOptionalFromException(glae));
152+
}
153+
}
154+
137155
/**
138156
* Download the artifacts file from the given reference name and job provided the job finished successfully.
139157
* The file will be saved to the specified directory. If the file already exists in the directory it will

0 commit comments

Comments
 (0)