From d92d00978a4c8c5a4e2c5b37f49a42c9166aaf5d Mon Sep 17 00:00:00 2001 From: Christian Luijten Date: Thu, 14 Feb 2019 13:00:23 +0100 Subject: [PATCH 1/3] Throw UncheckedIOException instead of generic RuntimeException --- src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java index 83754792..7042458e 100644 --- a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java +++ b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java @@ -188,7 +188,7 @@ public Iterator asIterator(final String tailApiUrl, final Class type) try { url = root.getAPIUrl(tailApiUrl); } catch (IOException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } } @@ -240,7 +240,7 @@ private void fetch() { handleAPIError(e, connection); } } catch (IOException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } } From baf727acc99deb9ca840dd0fba0b914807bae56a Mon Sep 17 00:00:00 2001 From: Christian Luijten Date: Fri, 15 Feb 2019 12:52:20 +0100 Subject: [PATCH 2/3] Add missing import --- src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java index 7042458e..ebe80573 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.*; From f2b627010e45ebf444379c5e7d99644e8f8855ea Mon Sep 17 00:00:00 2001 From: Christian Luijten Date: Fri, 15 Feb 2019 21:15:22 +0100 Subject: [PATCH 3/3] Retrow wrapped IOException --- src/main/java/org/gitlab/api/GitlabAPI.java | 60 +++++++++---------- .../gitlab/api/http/GitlabHTTPRequestor.java | 20 ++++--- src/test/java/org/gitlab/api/GitlabAPIIT.java | 5 +- 3 files changed, 45 insertions(+), 40 deletions(-) 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 getUsers() { + public List getUsers() throws IOException { String tailUrl = GitlabUser.URL + PARAM_MAX_ITEMS_PER_PAGE; return retrieve().getAll(tailUrl, GitlabUser[].class); } @@ -485,7 +485,7 @@ public List getGroupsViaSudo(String username, Pagination pagination * @param group the target group * @return a list of projects for the group */ - public List getGroupProjects(GitlabGroup group) { + public List getGroupProjects(GitlabGroup group) throws IOException { return getGroupProjects(group.getId()); } @@ -495,7 +495,7 @@ public List getGroupProjects(GitlabGroup group) { * @param groupId the target group's id. * @return a list of projects for the group */ - public List getGroupProjects(Integer groupId) { + public List 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 getGroupProjects(Integer groupId) { * @param group The GitLab Group * @return The Group Members */ - public List getGroupMembers(GitlabGroup group) { + public List getGroupMembers(GitlabGroup group) throws IOException { return getGroupMembers(group.getId()); } @@ -516,7 +516,7 @@ public List getGroupMembers(GitlabGroup group) { * @param groupId The id of the GitLab Group * @return The Group Members */ - public List getGroupMembers(Integer groupId) { + public List 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 getAllProjects() { + public List 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 getProjects() { + public List getProjects() throws IOException { String tailUrl = GitlabProject.URL + PARAM_MAX_ITEMS_PER_PAGE; return retrieve().getAll(tailUrl, GitlabProject[].class); } @@ -943,7 +943,7 @@ public List getProjectsViaSudoWithPagination(GitlabUser user, Pag * * @return A list of gitlab namespace */ - public List getNamespaces() { + public List 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 getProjectJobs(GitlabProject project) { + public List getProjectJobs(GitlabProject project) throws IOException { return getProjectJobs(project.getId()); } @@ -977,7 +977,7 @@ public List getProjectJobs(GitlabProject project) { * @param projectId the project id * @return A list of project jobs */ - public List getProjectJobs(Integer projectId) { + public List 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 getProjectJobs(Integer projectId) { * @param pipelineId * @return A list of project jobs */ - public List getPipelineJobs(GitlabProject project, Integer pipelineId) { + public List getPipelineJobs(GitlabProject project, Integer pipelineId) throws IOException { return getPipelineJobs(project.getId(), pipelineId); } @@ -1001,7 +1001,7 @@ public List getPipelineJobs(GitlabProject project, Integer pipelineId * @param pipelineId * @return A list of project jobs */ - public List getPipelineJobs(Integer projectId, Integer pipelineId) { + public List 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 getMergeRequestsWithStatus(GitlabProject project return retrieve().getAll(tailUrl, GitlabMergeRequest[].class); } - public List getMergeRequests(Serializable projectId) { + public List 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 getMergeRequests(Serializable projectId, Pagination pagination) { + public List 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 getMergeRequests(GitlabProject project) { + public List 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 getMergeRequests(GitlabProject project, Pagination pagination) { + public List 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 getAllMergeRequests(GitlabProject project) { + public List 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 getNotes(GitlabMergeRequest mergeRequest) throws IOExcep return Arrays.asList(notes); } - public List getAllNotes(GitlabMergeRequest mergeRequest) { + public List 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 getBranches(Serializable projectId) { + public List 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 getBranches(GitlabProject project) { + public List 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 getIssues(GitlabProject project) { + public List getIssues(GitlabProject project) throws IOException { return getIssues(project.getId()); } - public List getIssues(Serializable projectId) { + public List 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 getIssues(GitlabProject project, GitlabMilestone milestone) { + public List 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 getIssues(GitlabGroup group, GitlabMilestone milestone) { + public List 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 getCommitComments(Integer projectId, String sha) thro * @param projectId * @return */ - public List getTags(Serializable projectId) { + public List 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 getTags(Serializable projectId) { * @param project * @return */ - public List getTags(GitlabProject project) { + public List 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 getAllAwards(GitlabMergeRequest mergeRequest) { + public List 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 getAllAwards(GitlabIssue issue) { + public List 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 getAllAwards(GitlabIssue issue, Integer noteId) { + public List 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 getPipelineTriggers(GitlabProject project) { + public List 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 ebe80573..b5cbed98 100644 --- a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java +++ b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java @@ -159,18 +159,22 @@ public T to(String tailAPIUrl, Class type, T instance) throws IOException } } - public List getAll(final String tailUrl, final Class type) { - List results = new ArrayList<>(); - Iterator iterator = asIterator(tailUrl, type); + public List getAll(final String tailUrl, final Class type) throws IOException { + try { + List results = new ArrayList<>(); + Iterator 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 Iterator asIterator(final String tailApiUrl, final Class type) { 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 gitlabNamespaces = api.getNamespaces(); assertThat(gitlabNamespaces, not(nullValue())); assertThat(gitlabNamespaces.isEmpty(), is(false));