Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Compare and Refs commands to API #30

Merged
merged 2 commits into from
Mar 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions src/main/java/org/kohsuke/github/GHCompare.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/kohsuke/github/GHRef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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;

public String getRef() {
return ref;
}

public URL getUrl() {
return GitHub.parseURL(url);
}

public GHObject getObject() {
return object;
}


public static class GHObject {
private String type, sha, url;

public String getType() {
return type;
}

public String getSha() {
return sha;
}

public URL getUrl() {
return GitHub.parseURL(url);
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,38 @@ 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 <tt>git log id1...id2</tt> 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);
}

/**
* 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. <tt>tags</tt> or <tt>commits</tt>
* @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.
*/
Expand Down