|
| 1 | +package com.dkaedv.glghproxy; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.Serializable; |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.net.URLEncoder; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.gitlab.api.GitlabAPI; |
| 11 | +import org.gitlab.api.Pagination; |
| 12 | +import org.gitlab.api.models.GitlabCommit; |
| 13 | +import org.gitlab.api.models.GitlabMergeRequest; |
| 14 | +import org.gitlab.api.models.GitlabProject; |
| 15 | + |
| 16 | +/** |
| 17 | + * Helper class for fixing bugs in GitlabAPI until they are fixed upstream. |
| 18 | + */ |
| 19 | +public class GitlabAPIExt { |
| 20 | + private GitlabAPI api; |
| 21 | + |
| 22 | + public GitlabAPIExt(GitlabAPI api) { |
| 23 | + this.api = api; |
| 24 | + } |
| 25 | + |
| 26 | + public List<GitlabCommit> getCommits(GitlabMergeRequest mergeRequest) throws IOException { |
| 27 | + return getCommits(mergeRequest, new Pagination()); |
| 28 | + } |
| 29 | + |
| 30 | + public List<GitlabCommit> getCommits(GitlabMergeRequest mergeRequest, Pagination pagination) throws IOException { |
| 31 | + Integer projectId = mergeRequest.getProjectId(); |
| 32 | + |
| 33 | + String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + |
| 34 | + GitlabMergeRequest.URL + "/" + mergeRequest.getIid() + |
| 35 | + GitlabCommit.URL + pagination.toString(); |
| 36 | + |
| 37 | + GitlabCommit[] commits = api.retrieve().to(tailUrl, GitlabCommit[].class); |
| 38 | + return Arrays.asList(commits); |
| 39 | + } |
| 40 | + |
| 41 | + private String sanitizeProjectId(Serializable projectId) { |
| 42 | + return sanitizeId(projectId, "projectId"); |
| 43 | + } |
| 44 | + |
| 45 | + private String sanitizeId(Serializable id, String parameterName) { |
| 46 | + if (!(id instanceof String) && !(id instanceof Number)) { |
| 47 | + throw new IllegalArgumentException(parameterName + " needs to be of type String or Number"); |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + return URLEncoder.encode(String.valueOf(id), "UTF-8"); |
| 52 | + } catch (UnsupportedEncodingException e) { |
| 53 | + throw new RuntimeException((e)); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments