Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw IOException instead of generic RuntimeException (breaking change) #341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/main/java/org/gitlab/api/GitlabAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public String getHost() {
return hostUrl;
}

public List<GitlabUser> getUsers() {
public List<GitlabUser> getUsers() throws IOException {
String tailUrl = GitlabUser.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabUser[].class);
}
Expand Down Expand Up @@ -485,7 +485,7 @@ public List<GitlabGroup> getGroupsViaSudo(String username, Pagination pagination
* @param group the target group
* @return a list of projects for the group
*/
public List<GitlabProject> getGroupProjects(GitlabGroup group) {
public List<GitlabProject> getGroupProjects(GitlabGroup group) throws IOException {
return getGroupProjects(group.getId());
}

Expand All @@ -495,7 +495,7 @@ public List<GitlabProject> getGroupProjects(GitlabGroup group) {
* @param groupId the target group's id.
* @return a list of projects for the group
*/
public List<GitlabProject> getGroupProjects(Integer groupId) {
public List<GitlabProject> getGroupProjects(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabProject.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
Expand All @@ -506,7 +506,7 @@ public List<GitlabProject> getGroupProjects(Integer groupId) {
* @param group The GitLab Group
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) {
public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) throws IOException {
return getGroupMembers(group.getId());
}

Expand All @@ -516,7 +516,7 @@ public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) {
* @param groupId The id of the GitLab Group
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(Integer groupId) {
public List<GitlabGroupMember> getGroupMembers(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabGroupMember.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabGroupMember[].class);
}
Expand Down Expand Up @@ -762,7 +762,7 @@ public void deleteGroup(Integer groupId) throws IOException {
*
* @return A list of gitlab projects
*/
public List<GitlabProject> getAllProjects() {
public List<GitlabProject> getAllProjects() throws IOException {
String tailUrl = GitlabProject.URL;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
Expand Down Expand Up @@ -808,7 +808,7 @@ public String getProjectJson(String namespace, String projectName) throws IOExce
*
* @return A list of gitlab projects
*/
public List<GitlabProject> getProjects() {
public List<GitlabProject> getProjects() throws IOException {
String tailUrl = GitlabProject.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
Expand Down Expand Up @@ -943,7 +943,7 @@ public List<GitlabProject> getProjectsViaSudoWithPagination(GitlabUser user, Pag
*
* @return A list of gitlab namespace
*/
public List<GitlabNamespace> getNamespaces() {
public List<GitlabNamespace> getNamespaces() throws IOException {
String tailUrl = GitlabNamespace.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabNamespace[].class);
}
Expand All @@ -967,7 +967,7 @@ public GitlabUpload uploadFile(GitlabProject project, File file) throws IOExcept
* @param project the project
* @return A list of project jobs
*/
public List<GitlabJob> getProjectJobs(GitlabProject project) {
public List<GitlabJob> getProjectJobs(GitlabProject project) throws IOException {
return getProjectJobs(project.getId());
}

Expand All @@ -977,7 +977,7 @@ public List<GitlabJob> getProjectJobs(GitlabProject project) {
* @param projectId the project id
* @return A list of project jobs
*/
public List<GitlabJob> getProjectJobs(Integer projectId) {
public List<GitlabJob> getProjectJobs(Integer projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabJob.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabJob[].class);
}
Expand All @@ -990,7 +990,7 @@ public List<GitlabJob> getProjectJobs(Integer projectId) {
* @param pipelineId
* @return A list of project jobs
*/
public List<GitlabJob> getPipelineJobs(GitlabProject project, Integer pipelineId) {
public List<GitlabJob> getPipelineJobs(GitlabProject project, Integer pipelineId) throws IOException {
return getPipelineJobs(project.getId(), pipelineId);
}

Expand All @@ -1001,7 +1001,7 @@ public List<GitlabJob> getPipelineJobs(GitlabProject project, Integer pipelineId
* @param pipelineId
* @return A list of project jobs
*/
public List<GitlabJob> getPipelineJobs(Integer projectId, Integer pipelineId) {
public List<GitlabJob> getPipelineJobs(Integer projectId, Integer pipelineId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabPipeline.URL + "/" + sanitizeId(pipelineId, "PipelineID") + GitlabJob.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabJob[].class);
}
Expand Down Expand Up @@ -1447,27 +1447,27 @@ public List<GitlabMergeRequest> getMergeRequestsWithStatus(GitlabProject project
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}

public List<GitlabMergeRequest> getMergeRequests(Serializable projectId) {
public List<GitlabMergeRequest> getMergeRequests(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabMergeRequest.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}

public List<GitlabMergeRequest> getMergeRequests(Serializable projectId, Pagination pagination) {
public List<GitlabMergeRequest> getMergeRequests(Serializable projectId, Pagination pagination) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabMergeRequest.URL + pagination.toString();
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}

public List<GitlabMergeRequest> getMergeRequests(GitlabProject project) {
public List<GitlabMergeRequest> getMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}

public List<GitlabMergeRequest> getMergeRequests(GitlabProject project, Pagination pagination) {
public List<GitlabMergeRequest> getMergeRequests(GitlabProject project, Pagination pagination) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL + pagination.toString();
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}

public List<GitlabMergeRequest> getAllMergeRequests(GitlabProject project) {
public List<GitlabMergeRequest> getAllMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
Expand Down Expand Up @@ -1676,7 +1676,7 @@ public List<GitlabNote> getNotes(GitlabMergeRequest mergeRequest) throws IOExcep
return Arrays.asList(notes);
}

public List<GitlabNote> getAllNotes(GitlabMergeRequest mergeRequest) {
public List<GitlabNote> getAllNotes(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getIid() +
GitlabNote.URL + PARAM_MAX_ITEMS_PER_PAGE;
Expand Down Expand Up @@ -2279,12 +2279,12 @@ public void deleteNote(GitlabMergeRequest mergeRequest, GitlabNote noteToDelete)
retrieve().method(DELETE).to(tailUrl, GitlabNote.class);
}

public List<GitlabBranch> getBranches(Serializable projectId) {
public List<GitlabBranch> getBranches(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabBranch.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabBranch[].class);
}

public List<GitlabBranch> getBranches(GitlabProject project) {
public List<GitlabBranch> getBranches(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabBranch[].class);
}
Expand Down Expand Up @@ -2452,23 +2452,23 @@ public void deleteProjectHook(GitlabProject project, String hookId) throws IOExc
retrieve().method(DELETE).to(tailUrl, Void.class);
}

public List<GitlabIssue> getIssues(GitlabProject project) {
public List<GitlabIssue> getIssues(GitlabProject project) throws IOException {
return getIssues(project.getId());
}

public List<GitlabIssue> getIssues(Serializable projectId) {
public List<GitlabIssue> getIssues(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabIssue.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabIssue[].class);
}

public List<GitlabIssue> getIssues(GitlabProject project, GitlabMilestone milestone) {
public List<GitlabIssue> getIssues(GitlabProject project, GitlabMilestone milestone) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(project.getId())
+ GitlabMilestone.URL + "/" + sanitizeMilestoneId(milestone.getId())
+ GitlabIssue.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabIssue[].class);
}

public List<GitlabIssue> getIssues(GitlabGroup group, GitlabMilestone milestone) {
public List<GitlabIssue> getIssues(GitlabGroup group, GitlabMilestone milestone) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + sanitizeGroupId(group.getId())
+ GitlabMilestone.URL + "/" + sanitizeMilestoneId(milestone.getId())
+ GitlabIssue.URL + PARAM_MAX_ITEMS_PER_PAGE;
Expand Down Expand Up @@ -3331,7 +3331,7 @@ public List<CommitComment> getCommitComments(Integer projectId, String sha) thro
* @param projectId
* @return
*/
public List<GitlabTag> getTags(Serializable projectId) {
public List<GitlabTag> getTags(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabTag.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabTag[].class);
}
Expand All @@ -3342,7 +3342,7 @@ public List<GitlabTag> getTags(Serializable projectId) {
* @param project
* @return
*/
public List<GitlabTag> getTags(GitlabProject project) {
public List<GitlabTag> getTags(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabTag.URL + PARAM_MAX_ITEMS_PER_PAGE;
return retrieve().getAll(tailUrl, GitlabTag[].class);
}
Expand Down Expand Up @@ -3431,7 +3431,7 @@ public void deleteTag(GitlabProject project, String tagName) throws IOException
*
* @param mergeRequest
*/
public List<GitlabAward> getAllAwards(GitlabMergeRequest mergeRequest) {
public List<GitlabAward> getAllAwards(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() + GitlabMergeRequest.URL + "/"
+ mergeRequest.getIid() + GitlabAward.URL + PARAM_MAX_ITEMS_PER_PAGE;

Expand Down Expand Up @@ -3486,7 +3486,7 @@ public void deleteAward(GitlabMergeRequest mergeRequest, GitlabAward award) thro
*
* @param issue
*/
public List<GitlabAward> getAllAwards(GitlabIssue issue) {
public List<GitlabAward> getAllAwards(GitlabIssue issue) throws IOException {
String tailUrl = GitlabProject.URL + "/" + issue.getProjectId() + GitlabIssue.URL + "/" + issue.getId()
+ GitlabAward.URL + PARAM_MAX_ITEMS_PER_PAGE;

Expand Down Expand Up @@ -3541,7 +3541,7 @@ public void deleteAward(GitlabIssue issue, GitlabAward award) throws IOException
* @param issue
* @param noteId
*/
public List<GitlabAward> getAllAwards(GitlabIssue issue, Integer noteId) {
public List<GitlabAward> getAllAwards(GitlabIssue issue, Integer noteId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + issue.getProjectId() + GitlabIssue.URL + "/" + issue.getId()
+ GitlabNote.URL + noteId + GitlabAward.URL + PARAM_MAX_ITEMS_PER_PAGE;

Expand Down Expand Up @@ -3739,7 +3739,7 @@ public GitlabBuildVariable updateBuildVariable(Integer projectId,
* @return list of build triggers
* @throws IllegalStateException if jobs are not enabled for the project
*/
public List<GitlabTrigger> getPipelineTriggers(GitlabProject project) {
public List<GitlabTrigger> getPipelineTriggers(GitlabProject project) throws IOException {
if (!project.isJobsEnabled()) {
// if the project has not allowed jobs, you will only get a 403 forbidden message which is
// not helpful.
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.net.*;
import java.util.*;
Expand Down Expand Up @@ -158,18 +159,22 @@ public <T> T to(String tailAPIUrl, Class<T> type, T instance) throws IOException
}
}

public <T> List<T> getAll(final String tailUrl, final Class<T[]> type) {
List<T> results = new ArrayList<>();
Iterator<T[]> iterator = asIterator(tailUrl, type);
public <T> List<T> getAll(final String tailUrl, final Class<T[]> type) throws IOException {
try {
List<T> results = new ArrayList<>();
Iterator<T[]> iterator = asIterator(tailUrl, type);

while (iterator.hasNext()) {
T[] requests = iterator.next();
while (iterator.hasNext()) {
T[] requests = iterator.next();

if (requests.length > 0) {
results.addAll(Arrays.asList(requests));
if (requests.length > 0) {
results.addAll(Arrays.asList(requests));
}
}
return results;
} catch (UncheckedIOException e) {
throw e.getCause();
}
return results;
}

public <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type) {
Expand All @@ -188,7 +193,7 @@ public <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type)
try {
url = root.getAPIUrl(tailApiUrl);
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
}

Expand Down Expand Up @@ -240,7 +245,7 @@ private void fetch() {
handleAPIError(e, connection);
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/gitlab/api/GitlabAPIIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -44,7 +45,7 @@ public void checkInvalidCredentials() throws Exception {
}

@Test
public void testAllProjects() {
public void testAllProjects() throws IOException {
assertThat(api.getAllProjects(), is(notNullValue()));
}

Expand Down Expand Up @@ -229,7 +230,7 @@ public void checkSearchProjects() throws Exception {
* There is at least one namespace for the user
*/
@Test
public void testGetNamespace() {
public void testGetNamespace() throws IOException {
final List<GitlabNamespace> gitlabNamespaces = api.getNamespaces();
assertThat(gitlabNamespaces, not(nullValue()));
assertThat(gitlabNamespaces.isEmpty(), is(false));
Expand Down