diff --git a/pom.xml b/pom.xml index 4eb2d73fae..ebc3110a3a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.36 + 1.37 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java @@ -25,6 +25,10 @@ + + UTF-8 + + diff --git a/src/main/java/org/kohsuke/github/GHCompare.java b/src/main/java/org/kohsuke/github/GHCompare.java new file mode 100644 index 0000000000..31d5a07bd5 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHCompare.java @@ -0,0 +1,156 @@ +package org.kohsuke.github; + +import java.net.URL; +import java.util.Date; + +/** + * The model user for comparing 2 commits in the GitHub API. + * + * @author Michael Clarke + */ +public class GHCompare { + + private String url, html_url, permalink_url, diff_url, patch_url; + public Status status; + private int ahead_by, behind_by, total_commits; + private Commit base_commit, merge_base_commit; + private Commit[] commits; + private GHCommit.File[] files; + + private GHRepository owner; + + public URL getUrl() { + return GitHub.parseURL(url); + } + + public URL getHtmlUrl() { + return GitHub.parseURL(html_url); + } + + public URL getPermalinkUrl() { + return GitHub.parseURL(permalink_url); + } + + public URL getDiffUrl() { + return GitHub.parseURL(diff_url); + } + + public URL getPatchUrl() { + return GitHub.parseURL(patch_url); + } + + public Status getStatus() { + return status; + } + + public int getAheadBy() { + return ahead_by; + } + + public int getBehindBy() { + return behind_by; + } + + public int getTotalCommits() { + return total_commits; + } + + public Commit getBaseCommit() { + return base_commit; + } + + public Commit getMergeBaseCommit() { + return merge_base_commit; + } + + public Commit[] getCommits() { + return commits; + } + + + public GHCompare wrap(GHRepository owner) { + this.owner = owner; + for (Commit commit : commits) { + commit.wrapUp(owner); + } + merge_base_commit.wrapUp(owner); + base_commit.wrapUp(owner); + return this; + } + + /** + * Compare commits had a child commit element with additional details we want to capture. + * This extenstion of GHCommit provides that. + */ + public static class Commit extends GHCommit { + + private InnerCommit commit; + + public InnerCommit getCommit() { + return commit; + } + } + + + public static class InnerCommit { + private String url, sha, message; + private User author, committer; + private Tree tree; + + public String getUrl() { + return url; + } + + public String getSha() { + return sha; + } + + public String getMessage() { + return message; + } + + public User getAuthor() { + return author; + } + + public User getCommitter() { + return committer; + } + + public Tree getTree() { + return tree; + } + } + + public static class Tree { + private String url, sha; + + public String getUrl() { + return url; + } + + public String getSha() { + return sha; + } + } + + public static class User { + private String name, email, date; + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public Date getDate() { + return GitHub.parseDate(date); + } + } + + public static enum Status { + behind, ahead, identical; + } +} diff --git a/src/main/java/org/kohsuke/github/GHRef.java b/src/main/java/org/kohsuke/github/GHRef.java new file mode 100644 index 0000000000..9d19c7e57c --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHRef.java @@ -0,0 +1,61 @@ +package org.kohsuke.github; + +import java.net.URL; + +/** + * Provides information on a Git ref from GitHub. + * + * @author Michael Clarke + */ +public class GHRef { + + private String ref, url; + private GHObject object; + + /** + * Name of the ref, such as "refs/tags/abc" + */ + public String getRef() { + return ref; + } + + /** + * The API URL of this tag, such as https://api.github.com/repos/jenkinsci/jenkins/git/refs/tags/1.312 + */ + public URL getUrl() { + return GitHub.parseURL(url); + } + + /** + * The object that this ref points to. + */ + public GHObject getObject() { + return object; + } + + + public static class GHObject { + private String type, sha, url; + + /** + * Type of the object, such as "commit" + */ + public String getType() { + return type; + } + + /** + * SHA1 of this object. + */ + public String getSha() { + return sha; + } + + /** + * API URL to this Git data, such as https://api.github.com/repos/jenkinsci/jenkins/git/commits/b72322675eb0114363a9a86e9ad5a170d1d07ac0 + */ + public URL getUrl() { + return GitHub.parseURL(url); + } + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 703ff2378f..5302413c3a 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -393,6 +393,46 @@ public GHHook getHook(int id) throws IOException { return root.retrieve().to(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this); } + /** + * Gets a comparison between 2 points in the repository. This would be similar + * to calling git log id1...id2 against a local repository. + * @param id1 an identifier for the first point to compare from, this can be a sha1 ID (for a commit, tag etc) or a direct tag name + * @param id2 an identifier for the second point to compare to. Can be the same as the first point. + * @return the comparison output + * @throws IOException on failure communicating with GitHub + */ + public GHCompare getCompare(String id1, String id2) throws IOException { + GHCompare compare = root.retrieve().to(String.format("/repos/%s/%s/compare/%s...%s", owner.login, name, id1, id2), GHCompare.class); + return compare.wrap(this); + } + + public GHCompare getCompare(GHCommit id1, GHCommit id2) throws IOException { + return getCompare(id1.getSHA1(),id2.getSHA1()); + } + + public GHCompare getCompare(GHBranch id1, GHBranch id2) throws IOException { + return getCompare(id1.getName(),id2.getName()); + } + + /** + * Retrieves all refs for the github repository. + * @return an array of GHRef elements coresponding with the refs in the remote repository. + * @throws IOException on failure communicating with GitHub + */ + public GHRef[] getRefs() throws IOException { + return root.retrieve().to(String.format("/repos/%s/%s/git/refs", owner.login, name), GHRef[].class); + } + + /** + * Retrienved all refs of the given type for the current GitHub repository. + * @param refType the type of reg to search for e.g. tags or commits + * @return an array of all refs matching the request type + * @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested + */ + public GHRef[] getRefs(String refType) throws IOException { + return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class); + } + /** * Gets a commit object in this repository. */ diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index ffcccbf1e7..efe3dc4ae1 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -188,7 +188,7 @@ public void testCommitComment() throws Exception { } public void testCreateCommitComment() throws Exception { - GHCommit commit = gitHub.getUser("johnou").getRepository("sandbox-ant").getCommit("8ae38db0ea5837313ab5f39d43a6f73de3bd9000"); + GHCommit commit = gitHub.getUser("kohsuke").getRepository("sandbox-ant").getCommit("8ae38db0ea5837313ab5f39d43a6f73de3bd9000"); GHCommitComment c = commit.createComment("[testing](http://kohsuse.org/)"); System.out.println(c); c.update("updated text");