Skip to content

Commit 7d1d6b1

Browse files
DoubleDProjmini
andauthored
Merge reguest api: fix methods for diff and version (#1004)
Changes MergeRequestApi: - added methods "getDiffs()" for GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/diffs - add methods "getDiffVersions()" for GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions - add method "getDiffVersion()" and "getOptionalDiffVersion()" for GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions/:version_id - deprecate "getMergeRequestDiffs()" and "getMergeRequestDiff()" Fixes #993 --------- Co-authored-by: Jeremie Bresson <[email protected]>
1 parent bd35108 commit 7d1d6b1

File tree

5 files changed

+249
-77
lines changed

5 files changed

+249
-77
lines changed

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

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
import org.gitlab4j.api.models.ApprovalRuleParams;
1616
import org.gitlab4j.api.models.ApprovalState;
1717
import org.gitlab4j.api.models.Commit;
18+
import org.gitlab4j.api.models.Diff;
1819
import org.gitlab4j.api.models.Issue;
1920
import org.gitlab4j.api.models.MergeRequest;
2021
import org.gitlab4j.api.models.MergeRequestDiff;
2122
import org.gitlab4j.api.models.MergeRequestFilter;
2223
import org.gitlab4j.api.models.MergeRequestParams;
24+
import org.gitlab4j.api.models.MergeRequestVersion;
2325
import org.gitlab4j.api.models.Participant;
2426
import org.gitlab4j.api.models.Pipeline;
2527

@@ -400,7 +402,9 @@ public Stream<Commit> getCommitsStream(Object projectIdOrPath, Long mergeRequest
400402
* @param mergeRequestIid the internal ID of the merge request
401403
* @return a List of merge request diff versions for the specified merge request
402404
* @throws GitLabApiException if any exception occurs
405+
* @deprecated use {@link #getDiffVersions(Object, Long)} instead
403406
*/
407+
@Deprecated
404408
public List<MergeRequestDiff> getMergeRequestDiffs(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException {
405409
return (getMergeRequestDiffs(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).all());
406410
}
@@ -415,7 +419,9 @@ public List<MergeRequestDiff> getMergeRequestDiffs(Object projectIdOrPath, Long
415419
* @param itemsPerPage the number of MergeRequest instances that will be fetched per page
416420
* @return a Pager of merge request diff versions for the specified merge request
417421
* @throws GitLabApiException if any exception occurs
422+
* @deprecated use {@link #getDiffVersions(Object, Long, int)} instead
418423
*/
424+
@Deprecated
419425
public Pager<MergeRequestDiff> getMergeRequestDiffs(Object projectIdOrPath, Long mergeRequestIid, int itemsPerPage) throws GitLabApiException {
420426
return (new Pager<MergeRequestDiff>(this, MergeRequestDiff.class, itemsPerPage, null,
421427
"projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "versions"));
@@ -430,7 +436,9 @@ public Pager<MergeRequestDiff> getMergeRequestDiffs(Object projectIdOrPath, Long
430436
* @param mergeRequestIid the internal ID of the merge request
431437
* @return a Stream of merge request diff versions for the specified merge request
432438
* @throws GitLabApiException if any exception occurs
439+
* @deprecated use {@link #getDiffVersionsStream(Object, Long)} instead
433440
*/
441+
@Deprecated
434442
public Stream<MergeRequestDiff> getMergeRequestDiffsStream(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException {
435443
return (getMergeRequestDiffs(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).stream());
436444
}
@@ -445,12 +453,12 @@ public Stream<MergeRequestDiff> getMergeRequestDiffsStream(Object projectIdOrPat
445453
* @param versionId the ID of the merge request diff version
446454
* @return a MergeRequestDiff instance for the specified MR diff version
447455
* @throws GitLabApiException if any exception occurs
456+
* @deprecated use {@link #getDiffVersion(Object, Long, Long)} instead
448457
*/
458+
@Deprecated
449459
public MergeRequestDiff getMergeRequestDiff(Object projectIdOrPath,
450460
Long mergeRequestIid, Long versionId) throws GitLabApiException {
451-
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath),
452-
"merge_requests", mergeRequestIid, "versions", versionId);
453-
return (response.readEntity(MergeRequestDiff.class));
461+
return getDiffVersion(projectIdOrPath, mergeRequestIid, versionId);
454462
}
455463

456464
/**
@@ -462,7 +470,9 @@ public MergeRequestDiff getMergeRequestDiff(Object projectIdOrPath,
462470
* @param mergeRequestIid the internal ID of the merge request
463471
* @param versionId the ID of the merge request diff version
464472
* @return the specified MergeRequestDiff as an Optional instance instance
473+
* @deprecated use {@link #getOptionalDiffVersion(Object, Long, Long)} instead
465474
*/
475+
@Deprecated
466476
public Optional<MergeRequestDiff> getOptionalMergeRequestDiff(
467477
Object projectIdOrPath, Long mergeRequestIid, Long versionId) {
468478
try {
@@ -472,6 +482,129 @@ public Optional<MergeRequestDiff> getOptionalMergeRequestDiff(
472482
}
473483
}
474484

485+
/**
486+
* Get a list of merge request diff versions.
487+
*
488+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions</code></pre>
489+
*
490+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
491+
* @param mergeRequestIid the internal ID of the merge request
492+
* @return a List of merge request diff versions for the specified merge request
493+
* @throws GitLabApiException if any exception occurs
494+
*/
495+
public List<MergeRequestVersion> getDiffVersions(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException {
496+
return (getDiffVersions(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).all());
497+
}
498+
499+
/**
500+
* Get a Pager of merge request diff versions.
501+
*
502+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions</code></pre>
503+
*
504+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
505+
* @param mergeRequestIid the internal ID of the merge request
506+
* @param itemsPerPage the number of MergeRequest instances that will be fetched per page
507+
* @return a Pager of merge request diff versions for the specified merge request
508+
* @throws GitLabApiException if any exception occurs
509+
*/
510+
public Pager<MergeRequestVersion> getDiffVersions(Object projectIdOrPath, Long mergeRequestIid, int itemsPerPage) throws GitLabApiException {
511+
return (new Pager<MergeRequestVersion>(this, MergeRequestVersion.class, itemsPerPage, null,
512+
"projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "versions"));
513+
}
514+
515+
/**
516+
* Get a Stream of merge request diff versions.
517+
*
518+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions</code></pre>
519+
*
520+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
521+
* @param mergeRequestIid the internal ID of the merge request
522+
* @return a Stream of merge request diff versions for the specified merge request
523+
* @throws GitLabApiException if any exception occurs
524+
*/
525+
public Stream<MergeRequestVersion> getDiffVersionsStream(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException {
526+
return (getDiffVersions(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).stream());
527+
}
528+
529+
/**
530+
* Get a single merge request diff version.
531+
*
532+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions/:version_id</code></pre>
533+
*
534+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
535+
* @param mergeRequestIid the internal ID of the merge request
536+
* @param versionId the ID of the merge request diff version
537+
* @return a MergeRequestVersion instance for the specified MR diff version
538+
* @throws GitLabApiException if any exception occurs
539+
*/
540+
public MergeRequestDiff getDiffVersion(Object projectIdOrPath, Long mergeRequestIid, Long versionId) throws GitLabApiException {
541+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath),
542+
"merge_requests", mergeRequestIid, "versions", versionId);
543+
return (response.readEntity(MergeRequestDiff.class));
544+
}
545+
546+
/**
547+
* Get a single merge request diff version as an Optional instance.
548+
*
549+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/versions/:version_id</code></pre>
550+
*
551+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
552+
* @param mergeRequestIid the internal ID of the merge request
553+
* @param versionId the ID of the merge request diff version
554+
* @return the specified MergeRequestVersion as an Optional instance instance
555+
*/
556+
public Optional<MergeRequestDiff> getOptionalDiffVersion(Object projectIdOrPath, Long mergeRequestIid, Long versionId) {
557+
try {
558+
return (Optional.ofNullable(getDiffVersion(projectIdOrPath, mergeRequestIid, versionId)));
559+
} catch (GitLabApiException glae) {
560+
return (GitLabApi.createOptionalFromException(glae));
561+
}
562+
}
563+
564+
/**
565+
* Get a list of merge request diffs.
566+
*
567+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/diffs</code></pre>
568+
*
569+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
570+
* @param mergeRequestIid the internal ID of the merge request
571+
* @return a List of merge request diffs for the specified merge request
572+
* @throws GitLabApiException if any exception occurs
573+
*/
574+
public List<Diff> getDiffs(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException {
575+
return (getDiffs(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).all());
576+
}
577+
578+
/**
579+
* Get a Pager of merge request diffs.
580+
*
581+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/diffs</code></pre>
582+
*
583+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
584+
* @param mergeRequestIid the internal ID of the merge request
585+
* @param itemsPerPage the number of Diff instances that will be fetched per page
586+
* @return a Pager of merge request diffs for the specified merge request
587+
* @throws GitLabApiException if any exception occurs
588+
*/
589+
public Pager<Diff> getDiffs(Object projectIdOrPath, Long mergeRequestIid, int itemsPerPage) throws GitLabApiException {
590+
return (new Pager<>(this, Diff.class, itemsPerPage, null,
591+
"projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "diffs"));
592+
}
593+
594+
/**
595+
* Get a Stream of merge request diffs.
596+
*
597+
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/diffs</code></pre>
598+
*
599+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
600+
* @param mergeRequestIid the internal ID of the merge request
601+
* @return a Stream of merge request diffs for the specified merge request
602+
* @throws GitLabApiException if any exception occurs
603+
*/
604+
public Stream<Diff> getDiffsStream(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException {
605+
return (getDiffs(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).stream());
606+
}
607+
475608
/**
476609
* Creates a merge request.
477610
*

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

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,14 @@
11
package org.gitlab4j.api.models;
22

3-
import java.util.Date;
43
import java.util.List;
54

65
import org.gitlab4j.api.utils.JacksonJson;
76

8-
public class MergeRequestDiff {
7+
public class MergeRequestDiff extends MergeRequestVersion {
98

10-
private Long id;
11-
private String headCommitSha;
12-
private String baseCommitSha;
13-
private String startCommitSha;
14-
private Date createdAt;
15-
private Long mergeRequestId;
16-
private String state;
17-
private String realSize;
189
private List<Commit> commits;
1910
private List<Diff> diffs;
2011

21-
public Long getId() {
22-
return id;
23-
}
24-
25-
public void setId(Long id) {
26-
this.id = id;
27-
}
28-
29-
public String getHeadCommitSha() {
30-
return headCommitSha;
31-
}
32-
33-
public void setHeadCommitSha(String headCommitSha) {
34-
this.headCommitSha = headCommitSha;
35-
}
36-
37-
public String getBaseCommitSha() {
38-
return baseCommitSha;
39-
}
40-
41-
public void setBaseCommitSha(String baseCommitSha) {
42-
this.baseCommitSha = baseCommitSha;
43-
}
44-
45-
public String getStartCommitSha() {
46-
return startCommitSha;
47-
}
48-
49-
public void setStartCommitSha(String startCommitSha) {
50-
this.startCommitSha = startCommitSha;
51-
}
52-
53-
public Date getCreatedAt() {
54-
return createdAt;
55-
}
56-
57-
public void setCreatedAt(Date createdAt) {
58-
this.createdAt = createdAt;
59-
}
60-
61-
public Long getMergeRequestId() {
62-
return mergeRequestId;
63-
}
64-
65-
public void setMergeRequestId(Long mergeRequestId) {
66-
this.mergeRequestId = mergeRequestId;
67-
}
68-
69-
public String getState() {
70-
return state;
71-
}
72-
73-
public void setState(String state) {
74-
this.state = state;
75-
}
76-
77-
public String getRealSize() {
78-
return realSize;
79-
}
80-
81-
public void setRealSize(String realSize) {
82-
this.realSize = realSize;
83-
}
84-
8512
public List<Commit> getCommits() {
8613
return commits;
8714
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.util.Date;
4+
5+
import org.gitlab4j.api.utils.JacksonJson;
6+
7+
public class MergeRequestVersion {
8+
9+
private Long id;
10+
private String headCommitSha;
11+
private String baseCommitSha;
12+
private String startCommitSha;
13+
private Date createdAt;
14+
private Long mergeRequestId;
15+
private String state;
16+
private String realSize;
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public String getHeadCommitSha() {
27+
return headCommitSha;
28+
}
29+
30+
public void setHeadCommitSha(String headCommitSha) {
31+
this.headCommitSha = headCommitSha;
32+
}
33+
34+
public String getBaseCommitSha() {
35+
return baseCommitSha;
36+
}
37+
38+
public void setBaseCommitSha(String baseCommitSha) {
39+
this.baseCommitSha = baseCommitSha;
40+
}
41+
42+
public String getStartCommitSha() {
43+
return startCommitSha;
44+
}
45+
46+
public void setStartCommitSha(String startCommitSha) {
47+
this.startCommitSha = startCommitSha;
48+
}
49+
50+
public Date getCreatedAt() {
51+
return createdAt;
52+
}
53+
54+
public void setCreatedAt(Date createdAt) {
55+
this.createdAt = createdAt;
56+
}
57+
58+
public Long getMergeRequestId() {
59+
return mergeRequestId;
60+
}
61+
62+
public void setMergeRequestId(Long mergeRequestId) {
63+
this.mergeRequestId = mergeRequestId;
64+
}
65+
66+
public String getState() {
67+
return state;
68+
}
69+
70+
public void setState(String state) {
71+
this.state = state;
72+
}
73+
74+
public String getRealSize() {
75+
return realSize;
76+
}
77+
78+
public void setRealSize(String realSize) {
79+
this.realSize = realSize;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return (JacksonJson.toJsonString(this));
85+
}
86+
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import org.gitlab4j.api.models.Member;
8686
import org.gitlab4j.api.models.MergeRequest;
8787
import org.gitlab4j.api.models.MergeRequestDiff;
88+
import org.gitlab4j.api.models.MergeRequestVersion;
8889
import org.gitlab4j.api.models.Milestone;
8990
import org.gitlab4j.api.models.Note;
9091
import org.gitlab4j.api.models.NotificationSettings;
@@ -656,6 +657,12 @@ public void testMergeRequestDiffs() throws Exception {
656657
assertTrue(compareJson(diffs, "merge-request-diffs.json"));
657658
}
658659

660+
@Test
661+
public void testMergeRequestVersions() throws Exception {
662+
List<MergeRequestVersion> versions = unmarshalResourceList(MergeRequestVersion.class, "merge-request-versions.json");
663+
assertTrue(compareJson(versions, "merge-request-diffs.json"));
664+
}
665+
659666
@Test
660667
public void testMilestone() throws Exception {
661668
Milestone milestone = unmarshalResource(Milestone.class, "milestone.json");

0 commit comments

Comments
 (0)