adding event support

This commit is contained in:
Kohsuke Kawaguchi
2012-01-08 16:53:20 -08:00
parent ac1dd9a0ee
commit 4a507a5625
4 changed files with 88 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
package org.kohsuke.github;
import org.codehaus.jackson.node.ObjectNode;
import java.io.IOException;
import java.util.Date;
/**
* Represents an event.
*
* @author Kohsuke Kawaguchi
*/
public class GHEventInfo {
private GitHub root;
private ObjectNode payload;
private String created_at;
private String type;
// these are all shallow objects
private GHRepository repo;
private GHUser actor;
private GHOrganization org;
public GHEvent getType() {
for (GHEvent e : GHEvent.values()) {
if (e.name().replace("_","").equalsIgnoreCase(type))
return e;
}
return null; // unknown event type
}
/*package*/ GHEventInfo wrapUp(GitHub root) {
this.root = root;
return this;
}
public Date getCreatedAt() {
return GitHub.parseDate(created_at);
}
/**
* Repository where the change was made.
*/
public GHRepository getRepository() throws IOException {
return root.getRepository(repo.getName());
}
public GHUser getActor() throws IOException {
return root.getUser(actor.getLogin());
}
public GHOrganization getOrganization() throws IOException {
return (org==null || org.getLogin()==null) ? null : root.getOrganization(org.getLogin());
}
}

View File

@@ -84,6 +84,9 @@ public class GHRepository {
return html_url;
}
/**
* String of the form "owner/reponame"
*/
public String getName() {
return name;
}

View File

@@ -44,8 +44,10 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
@@ -280,9 +282,29 @@ public class GitHub {
return o;
}
/**
* Gets the repository object from 'user/reponame' string that GitHub calls as "repository name"
*
* @see GHRepository#getName()
*/
public GHRepository getRepository(String name) throws IOException {
String[] tokens = name.split("/");
return getUser(tokens[0]).getRepository(tokens[1]);
}
public Map<String, GHOrganization> getMyOrganizations() throws IOException {
return retrieveWithAuth("/organizations",JsonOrganizations.class).wrap(this);
}
/**
* Public events visible to you. Equivalent of what's displayed on https://github.com/
*/
public List<GHEventInfo> getEvents() throws IOException {
// TODO: pagenation
GHEventInfo[] events = retrieve3("/events", GHEventInfo[].class);
for (GHEventInfo e : events)
e.wrapUp(this);
return Arrays.asList(events);
}
/**