Skip to content

Commit 583a49b

Browse files
authored
Merge pull request hub4j#1329 from ecxia/add_gitcommit_class
Add gitcommit class
2 parents 93eaa7c + 3408011 commit 583a49b

5 files changed

Lines changed: 465 additions & 137 deletions

File tree

src/main/java/org/kohsuke/github/GHCommit.java

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.kohsuke.github;
22

3-
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
43
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
54

65
import java.io.IOException;
@@ -34,94 +33,60 @@ public class GHCommit {
3433
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
3534
"UWF_UNWRITTEN_FIELD" },
3635
justification = "JSON API")
37-
public static class ShortInfo {
38-
private GHAuthor author;
39-
private GHAuthor committer;
36+
public static class ShortInfo extends GitCommit {
4037

41-
private String message;
42-
43-
private int comment_count;
44-
45-
private GHVerification verification;
46-
47-
static class Tree {
48-
String sha;
49-
}
50-
51-
private Tree tree;
52-
53-
/**
54-
* Gets author.
55-
*
56-
* @return the author
57-
*/
58-
@WithBridgeMethods(value = GHAuthor.class, castRequired = true)
59-
public GitUser getAuthor() {
60-
return author;
61-
}
38+
private int comment_count = -1;
6239

6340
/**
64-
* Gets authored date.
41+
* Gets comment count.
6542
*
66-
* @return the authored date
43+
* @return the comment count
6744
*/
68-
public Date getAuthoredDate() {
69-
return author.getDate();
45+
public int getCommentCount() throws GHException {
46+
if (comment_count < 0) {
47+
throw new GHException("Not available on this endpoint.");
48+
}
49+
return comment_count;
7050
}
7151

7252
/**
73-
* Gets committer.
74-
*
75-
* @return the committer
53+
* Creates instance of {@link GHCommit.ShortInfo}.
7654
*/
77-
@WithBridgeMethods(value = GHAuthor.class, castRequired = true)
78-
public GitUser getCommitter() {
79-
return committer;
80-
}
55+
public ShortInfo() {
56+
// Empty constructor required for Jackson binding
57+
};
8158

82-
/**
83-
* Gets commit date.
84-
*
85-
* @return the commit date
86-
*/
87-
public Date getCommitDate() {
88-
return committer.getDate();
59+
ShortInfo(GitCommit commit) {
60+
// Inherited copy constructor, used for bridge method from {@link GitCommit},
61+
// which is used in {@link GHContentUpdateResponse}) to {@link GHCommit}.
62+
super(commit);
8963
}
9064

91-
/**
92-
* Gets message.
93-
*
94-
* @return Commit message.
95-
*/
96-
public String getMessage() {
97-
return message;
98-
}
99-
100-
/**
101-
* Gets comment count.
102-
*
103-
* @return the comment count
104-
*/
105-
public int getCommentCount() {
106-
return comment_count;
65+
@Override
66+
public List<String> getParentSHA1s() {
67+
List<String> shortInfoParents = super.getParentSHA1s();
68+
if (shortInfoParents == null) {
69+
throw new GHException("Not available on this endpoint. Try calling getParentSHA1s from outer class.");
70+
}
71+
return shortInfoParents;
10772
}
10873

109-
/**
110-
* Gets Verification Status.
111-
*
112-
* @return the Verification status
113-
*/
114-
public GHVerification getVerification() {
115-
return verification;
116-
}
11774
}
11875

11976
/**
12077
* The type GHAuthor.
12178
*
12279
* @deprecated Use {@link GitUser} instead.
12380
*/
81+
@Deprecated
12482
public static class GHAuthor extends GitUser {
83+
public GHAuthor() {
84+
super();
85+
}
86+
87+
public GHAuthor(GitUser user) {
88+
super(user);
89+
}
12590
}
12691

12792
/**
@@ -263,6 +228,27 @@ static class User {
263228
List<Parent> parents;
264229
User author, committer;
265230

231+
/**
232+
* Creates an instance of {@link GHCommit}.
233+
*/
234+
public GHCommit() {
235+
// empty constructor needed for Jackson binding
236+
}
237+
238+
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "acceptable")
239+
GHCommit(ShortInfo shortInfo) {
240+
// Constructs a (relatively sparse) GHCommit from a GitCommit. Used for
241+
// bridge method from {@link GitCommit}, which is used in
242+
// {@link GHContentUpdateResponse}) to {@link GHCommit}.
243+
commit = shortInfo;
244+
245+
owner = commit.getOwner();
246+
html_url = commit.getHtmlUrl();
247+
sha = commit.getSha();
248+
url = commit.getUrl();
249+
parents = commit.getParents();
250+
}
251+
266252
/**
267253
* Gets commit short info.
268254
*
@@ -330,7 +316,7 @@ public int getLinesDeleted() throws IOException {
330316
* on error
331317
*/
332318
public GHTree getTree() throws IOException {
333-
return owner.getTree(getCommitShortInfo().tree.sha);
319+
return owner.getTree(getCommitShortInfo().getTreeSHA1());
334320
}
335321

336322
/**
@@ -379,7 +365,7 @@ public List<File> getFiles() throws IOException {
379365
* @return SHA1 of parent commit objects.
380366
*/
381367
public List<String> getParentSHA1s() {
382-
if (parents == null)
368+
if (parents == null || parents.size() == 0)
383369
return Collections.emptyList();
384370
return new AbstractList<String>() {
385371
@Override
@@ -596,4 +582,5 @@ GHCommit wrapUp(GHRepository owner) {
596582
this.owner = owner;
597583
return this;
598584
}
585+
599586
}

src/main/java/org/kohsuke/github/GHContentUpdateResponse.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.kohsuke.github;
22

3+
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
34
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
45

56
/**
67
* The response that is returned when updating repository content.
78
*/
89
public class GHContentUpdateResponse {
910
private GHContent content;
10-
private GHCommit commit;
11+
private GitCommit commit;
1112

1213
/**
1314
* Gets content.
@@ -25,7 +26,14 @@ public GHContent getContent() {
2526
* @return the commit
2627
*/
2728
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
28-
public GHCommit getCommit() {
29+
@WithBridgeMethods(value = GHCommit.class, adapterMethod = "gitCommitToGHCommit")
30+
public GitCommit getCommit() {
2931
return commit;
3032
}
33+
34+
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "bridge method of getCommit")
35+
private Object gitCommitToGHCommit(GitCommit commit, Class targetType) {
36+
return new GHCommit(new GHCommit.ShortInfo(commit));
37+
}
38+
3139
}

0 commit comments

Comments
 (0)