Skip to content

Commit b96fd36

Browse files
ISibboIgmessner
authored andcommitted
Get more information from gitlab (#186)
* Load forks of a project with Pager * Load comments of a commit with Pager * Load merge request changes * Load merge request participants. * Method to request repository contributors * getMergeRequestChanges method needs list of diffs * Fixed getFile method does not handle spaces in path correctly
1 parent 3210ee1 commit b96fd36

File tree

9 files changed

+110
-7
lines changed

9 files changed

+110
-7
lines changed

src/main/java/org/gitlab4j/api/AbstractApi.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,23 @@ protected int getDefaultPerPage() {
7676
protected GitLabApiClient getApiClient() {
7777
return (gitLabApi.getApiClient());
7878
}
79-
79+
80+
/**
81+
* Encode a string to be used as in-path argument for a gitlab api request.
82+
*
83+
* Standard URL encoding changes spaces to plus signs, but for arguments that are part of the path,
84+
* like the :file_path in a "Get raw file" request, gitlab expects spaces to be encoded with %20.
85+
*
86+
* @param s the string to encode
87+
* @return encoded version of s with spaces encoded as %2F
88+
* @throws GitLabApiException if encoding throws an exception
89+
*/
8090
protected String urlEncode(String s) throws GitLabApiException {
8191
try {
8292
String encoded = URLEncoder.encode(s, "UTF-8");
93+
// Since the encode method encodes plus signs as %2B,
94+
// we can simply replace the encoded spaces with the correct encoding here
95+
encoded = encoded.replace("+", "%20");
8396
encoded = encoded.replace(".", "%2E");
8497
encoded = encoded.replace("-", "%2D");
8598
encoded = encoded.replace("_", "%5F");

src/main/java/org/gitlab4j/api/CommitsApi.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,21 @@ public List<Comment> getComments(int projectId, String sha) throws GitLabApiExce
302302
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "comments");
303303
return (response.readEntity(new GenericType<List<Comment>>() {}));
304304
}
305+
306+
/**
307+
* Get a Pager of the comments of a commit in a project.
308+
*
309+
* GET /projects/:id/repository/commits/:sha/comments
310+
*
311+
* @param projectId the project ID that the commit belongs to
312+
* @param sha a commit hash or name of a branch or tag
313+
* @param itemsPerPage the number of Comment instances that will be fetched per page
314+
* @return a List of Comment instances for the specified project ID/sha pair
315+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
316+
*/
317+
public Pager<Comment> getComments(int projectId, String sha, int itemsPerPage) throws GitLabApiException {
318+
return new Pager<Comment>(this, Comment.class, itemsPerPage, null, "projects", projectId, "repository", "commits", sha, "comments");
319+
}
305320

306321
/**
307322
* Add a comment to a commit. In order to post a comment in a particular line of a particular file,

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public String getApiNamespace() {
3737
}
3838
}
3939

40-
// Used to keep track of GitLabApiExceptions on calls that return Optionsl<?>
40+
// Used to keep track of GitLabApiExceptions on calls that return Optional<?>
4141
private static final Map<Optional<?>, GitLabApiException> optionalExceptionMap =
4242
Collections.synchronizedMap(new WeakHashMap<Optional<?>, GitLabApiException>());
4343

src/main/java/org/gitlab4j/api/MergeRequestApi.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.gitlab4j.api.GitLabApi.ApiVersion;
1111
import org.gitlab4j.api.models.Commit;
1212
import org.gitlab4j.api.models.MergeRequest;
13+
import org.gitlab4j.api.models.Participant;
1314

1415
/**
1516
* This class implements the client side API for the GitLab merge request calls.
@@ -577,4 +578,34 @@ public MergeRequest unapproveMergeRequest(Integer projectId, Integer mergeReques
577578
Response response = post(Response.Status.OK, (Form)null, "projects", projectId, "merge_requests", mergeRequestIid, "unapprove");
578579
return (response.readEntity(MergeRequest.class));
579580
}
581+
582+
/**
583+
* Get merge request with changes information.
584+
*
585+
* GET /projects/:id/merge_requests/:merge_request_iid/changes
586+
*
587+
* @param projectId the project ID to get the merge requests for
588+
* @param mergeRequestIid the IID of the merge request to get
589+
* @return a merge request including its changes
590+
* @throws GitLabApiException if any exception occurs
591+
*/
592+
public MergeRequest getMergeRequestChanges(Integer projectId, Integer mergeRequestIid) throws GitLabApiException {
593+
Response response = get(Response.Status.OK, null, "projects", projectId, "merge_requests", mergeRequestIid, "changes");
594+
return (response.readEntity(MergeRequest.class));
595+
}
596+
597+
/**
598+
* Get participants of merge request.
599+
*
600+
* GET /projects/:id/merge_requests/:merge_request_iid/participants
601+
*
602+
* @param projectId the project ID to get the merge requests for
603+
* @param mergeRequestIid the IID of the merge request to get
604+
* @param itemsPerPage the number of Participant instances that will be fetched per page
605+
* @return all participants for the specified merge request
606+
* @throws GitLabApiException if any exception occurs
607+
*/
608+
public Pager<Participant> getParticipants(Integer projectId, Integer mergeRequestIid, int itemsPerPage) throws GitLabApiException {
609+
return new Pager<Participant>(this, Participant.class, itemsPerPage, null, "projects", projectId, "merge_requests", mergeRequestIid, "participants");
610+
}
580611
}

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ public void deleteSnippet(Integer projectId, Integer snippetId) throws GitLabApi
18111811
delete(Response.Status.NO_CONTENT, null, "projects", projectId, "snippets", snippetId);
18121812
}
18131813

1814-
/*
1814+
/**
18151815
* Get the raw project snippet as plain text.
18161816
*
18171817
* GET /projects/:id/snippets/:snippet_id/raw
@@ -1826,7 +1826,7 @@ public String getRawSnippetContent(Integer projectId, Integer snippetId) throws
18261826
return (response.readEntity(String.class));
18271827
}
18281828

1829-
/*
1829+
/**
18301830
* Get the raw project snippet plain text as an Optional instance.
18311831
*
18321832
* GET /projects/:id/snippets/:snippet_id/raw
@@ -2054,4 +2054,18 @@ public void deletePushRules(Integer projectId) throws GitLabApiException {
20542054

20552055
delete(Response.Status.OK, null, "projects", projectId, "push_rule");
20562056
}
2057+
2058+
/**
2059+
* Get a Pager of projects that were forked from the specified project.
2060+
*
2061+
* GET /projects/:id/forks
2062+
*
2063+
* @param projectId the ID of the project
2064+
* @param itemsPerPage the number of Project instances that will be fetched per page
2065+
* @return a Pager of projects
2066+
* @throws GitLabApiException if any exception occurs
2067+
*/
2068+
public Pager<Project> getForks(Integer projectId, int itemsPerPage) throws GitLabApiException {
2069+
return new Pager<Project>(this, Project.class, itemsPerPage, null, "projects", projectId, "forks");
2070+
}
20572071
}

src/main/java/org/gitlab4j/api/RepositoryApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.gitlab4j.api.GitLabApi.ApiVersion;
1818
import org.gitlab4j.api.models.Branch;
1919
import org.gitlab4j.api.models.CompareResults;
20+
import org.gitlab4j.api.models.Contributor;
2021
import org.gitlab4j.api.models.Tag;
2122
import org.gitlab4j.api.models.TreeItem;
2223
import org.gitlab4j.api.utils.FileUtils;
@@ -498,4 +499,19 @@ public CompareResults compare(String projectPath, String from, String to) throws
498499
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectPath, "repository", "compare");
499500
return (response.readEntity(CompareResults.class));
500501
}
502+
503+
/**
504+
* Get a Pager of contributors from a project.
505+
*
506+
* GET /projects/:id/repository/contributors
507+
*
508+
* @param projectId the project to get the list of contributors for
509+
* @param itemsPerPage the number of Project instances that will be fetched per page
510+
* @return the list of contributors for the specified project ID
511+
*
512+
* @throws GitLabApiException if any exception occurs
513+
*/
514+
public Pager<Contributor> getContributors(Integer projectId, int itemsPerPage) throws GitLabApiException {
515+
return new Pager<Contributor>(this, Contributor.class, itemsPerPage, null, "projects", projectId, "repository", "contributors");
516+
}
501517
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.gitlab4j.api.models;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement
6+
public class Contributor extends AbstractUser {
7+
}

src/main/java/org/gitlab4j/api/models/MergeRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MergeRequest {
1919
private Integer approvalsBeforeMerge;
2020
private Assignee assignee;
2121
private Author author;
22-
private Diff changes;
22+
private List<Diff> changes;
2323
private Date createdAt;
2424
private String description;
2525
private Integer downvotes;
@@ -80,11 +80,11 @@ public void setAuthor(Author author) {
8080
this.author = author;
8181
}
8282

83-
public Diff getChanges() {
83+
public List<Diff> getChanges() {
8484
return changes;
8585
}
8686

87-
public void setChanges(Diff changes) {
87+
public void setChanges(List<Diff> changes) {
8888
this.changes = changes;
8989
}
9090

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.gitlab4j.api.models;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement
6+
public class Participant extends AbstractUser {
7+
}

0 commit comments

Comments
 (0)