Skip to content

Commit 4fb28c3

Browse files
committed
Fix getting merge requests originating from forked and deleted projects
See timols/java-gitlab-api#403 for the same fix upstream.
1 parent c5b5299 commit 4fb28c3

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

src/main/java/com/dkaedv/glghproxy/controller/ReposController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.web.bind.annotation.ResponseStatus;
4141

4242
import com.dkaedv.glghproxy.Constants;
43+
import com.dkaedv.glghproxy.GitlabAPIExt;
4344
import com.dkaedv.glghproxy.Utils;
4445
import com.dkaedv.glghproxy.converter.GitlabToGithubConverter;
4546
import com.dkaedv.glghproxy.githubentity.HookRequest;
@@ -167,7 +168,7 @@ public List<RepositoryCommit> getCommitsOnPullRequest(@PathVariable String names
167168
repo = OwnerFixup.fixupRepo(repo, treatOrgaAsOwner);
168169
GitlabAPI api = gitlab.connect(authorization);
169170
GitlabMergeRequest mergeRequest = findMergeRequestByProjectAndIid(namespace, repo, id, api);
170-
List<GitlabCommit> commits = api.getCommits(mergeRequest);
171+
List<GitlabCommit> commits = new GitlabAPIExt(api).getCommits(mergeRequest);
171172

172173
return GitlabToGithubConverter.convertCommits(commits, env);
173174
}

0 commit comments

Comments
 (0)