diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index c544351d..f584002b 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -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); } @@ -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()); } @@ -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); } @@ -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()); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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()); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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; @@ -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); } @@ -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; @@ -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); } @@ -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); } @@ -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; @@ -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; @@ -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; @@ -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. diff --git a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java index 83754792..b5cbed98 100644 --- a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java +++ b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java @@ -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.*; @@ -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) { @@ -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); } } @@ -240,7 +245,7 @@ private void fetch() { handleAPIError(e, connection); } } catch (IOException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } } diff --git a/src/test/java/org/gitlab/api/GitlabAPIIT.java b/src/test/java/org/gitlab/api/GitlabAPIIT.java index f8d2fc62..ade06e54 100644 --- a/src/test/java/org/gitlab/api/GitlabAPIIT.java +++ b/src/test/java/org/gitlab/api/GitlabAPIIT.java @@ -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; @@ -44,7 +45,7 @@ public void checkInvalidCredentials() throws Exception { } @Test - public void testAllProjects() { + public void testAllProjects() throws IOException { assertThat(api.getAllProjects(), is(notNullValue())); } @@ -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));