Compare commits

...

11 Commits

Author SHA1 Message Date
Kohsuke Kawaguchi
f6aea47c2c [maven-release-plugin] prepare release github-api-1.19 2012-04-06 08:25:43 -07:00
Kohsuke Kawaguchi
23069ac2fc Using map to enable lookup by name. 2012-04-06 08:24:41 -07:00
Kohsuke Kawaguchi
0b2fdc598b cleaning up 2012-04-06 08:22:44 -07:00
Frederik Fix
aac30b923c implement listing of branches 2012-04-05 16:21:24 +02:00
Kohsuke Kawaguchi
318caf6123 [maven-release-plugin] prepare for next development iteration 2012-03-08 12:51:59 -08:00
Kohsuke Kawaguchi
6898893ffb [maven-release-plugin] prepare release github-api-1.18 2012-03-08 12:51:54 -08:00
Kohsuke Kawaguchi
cb34f4e91f Merge branch 'pull-6' 2012-03-08 12:50:09 -08:00
Kohsuke Kawaguchi
0da4b320f1 As per Sun coding convention, using upper case letters for enum constants 2012-03-08 12:49:36 -08:00
Kohsuke Kawaguchi
facb5bd13e Added API to handle emails. 2012-03-08 12:47:11 -08:00
Kohsuke Kawaguchi
22bc768868 [maven-release-plugin] prepare for next development iteration 2012-02-12 09:30:37 -08:00
y.kokubo
fbcf3a17b4 implmented milestone api via v3 2012-01-13 14:36:00 +09:00
11 changed files with 323 additions and 14 deletions

View File

@@ -7,7 +7,7 @@
</parent>
<artifactId>github-api</artifactId>
<version>1.17</version>
<version>1.19</version>
<name>GitHub API for Java</name>
<url>http://github-api.kohsuke.org/</url>
<description>GitHub API for Java</description>

View File

@@ -0,0 +1,51 @@
package org.kohsuke.github;
/**
* A branch in a repository.
*
* @author Yusuke Kokubo
*/
public class GHBranch {
private GitHub root;
private GHRepository owner;
private String name;
private Commit commit;
public static class Commit {
String sha,url;
}
public GitHub getRoot() {
return root;
}
/**
* Repository that this branch is in.
*/
public GHRepository getOwner() {
return owner;
}
public String getName() {
return name;
}
/**
* The commit that this branch currently points to.
*/
public String getSHA1() {
return commit.sha;
}
@Override
public String toString() {
return "Branch:" + name + " in " + owner.getUrl();
}
/*package*/ GHBranch wrap(GHRepository repo) {
this.owner = repo;
this.root = repo.root;
return this;
}
}

View File

@@ -0,0 +1,73 @@
package org.kohsuke.github;
import java.util.Date;
import java.util.Locale;
/**
*
* @author Yusuke Kokubo
*
*/
public class GHMilestone {
GitHub root;
GHRepository owner;
GHUser creator;
private String state, due_on, title, url, created_at, description;
private int closed_issues, open_issues, number;
public GitHub getRoot() {
return root;
}
public GHRepository getOwner() {
return owner;
}
public GHUser getCreator() {
return creator;
}
public Date getDueOn() {
if (due_on == null) return null;
return GitHub.parseDate(due_on);
}
public String getTitle() {
return title;
}
public String getUrl() {
return url;
}
public Date getCreatedAt() {
return GitHub.parseDate(created_at);
}
public String getDescription() {
return description;
}
public int getClosedIssues() {
return closed_issues;
}
public int getOpenIssues() {
return open_issues;
}
public int getNumber() {
return number;
}
public GHMilestoneState getState() {
return Enum.valueOf(GHMilestoneState.class, state.toUpperCase(Locale.ENGLISH));
}
public GHMilestone wrap(GHRepository repo) {
this.owner = repo;
this.root = repo.root;
return this;
}
}

View File

@@ -0,0 +1,11 @@
package org.kohsuke.github;
/**
*
* @author Yusuke Kokubo
*
*/
public enum GHMilestoneState {
OPEN,
CLOSED
}

View File

@@ -0,0 +1,34 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Represents the account that's logging into GitHub.
*
* @author Kohsuke Kawaguchi
*/
public class GHMyself extends GHUser {
/**
* Returns the read-only list of e-mail addresses configured for you.
*
* This corresponds to the stuff you configure in https://github.com/settings/emails,
* and not to be confused with {@link #getEmail()} that shows your public e-mail address
* set in https://github.com/settings/profile
*
* @return
* Always non-null.
*/
public List<String> getEmails() throws IOException {
String[] addresses = root.retrieveWithAuth3("/user/emails",String[].class);
return Collections.unmodifiableList(Arrays.asList(addresses));
}
// public void addEmails(Collection<String> emails) throws IOException {
//// new Poster(root,ApiVersion.V3).withCredential().to("/user/emails");
// root.retrieveWithAuth3()
// }
}

View File

@@ -42,12 +42,14 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import static java.util.Arrays.*;
import static org.kohsuke.github.ApiVersion.V3;
@@ -68,6 +70,7 @@ public class GHRepository {
private boolean has_issues, has_wiki, fork, _private, has_downloads;
private int watchers,forks,open_issues;
private String created_at, pushed_at;
private Map<Integer,GHMilestone> milestones = new HashMap<Integer, GHMilestone>();
public String getDescription() {
return description;
@@ -458,6 +461,46 @@ public class GHRepository {
return this;
}
/**
* Gets branches by {@linkplain GHBranch#getName() their names}.
*/
public Map<String,GHBranch> getBranches() throws IOException {
Map<String,GHBranch> r = new TreeMap<String,GHBranch>();
for (GHBranch p : root.retrieve3("/repos/"+owner.login+"/"+name+"/branches", GHBranch[].class)) {
p.wrap(this);
r.put(p.getName(),p);
}
return r;
}
public Map<Integer, GHMilestone> getMilestones() throws IOException {
Map<Integer,GHMilestone> milestones = new TreeMap<Integer, GHMilestone>();
GHMilestone[] ms = root.retrieve3("/repos/"+owner.login+"/"+name+"/milestones", GHMilestone[].class);
for (GHMilestone m : ms) {
m.owner = this;
m.root = root;
milestones.put(m.getNumber(), m);
}
return milestones;
}
public GHMilestone getMilestone(int number) throws IOException {
GHMilestone m = milestones.get(number);
if (m == null) {
m = root.retrieve3("/repos/"+owner.login+"/"+name+"/milestones/"+number, GHMilestone.class);
m.owner = this;
m.root = root;
milestones.put(m.getNumber(), m);
}
return m;
}
public GHMilestone createMilestone(String title, String description) throws IOException {
return new Poster(root,V3).withCredential()
.with("title", title).with("description", description)
.to("/repos/"+owner.login+"/"+name+"/milestones", GHMilestone.class,"POST").wrap(this);
}
@Override
public String toString() {
return "Repository:"+owner.login+":"+name;

View File

@@ -49,6 +49,7 @@ import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
@@ -240,19 +241,16 @@ public class GitHub {
/**
* Gets the {@link GHUser} that represents yourself.
*/
public GHUser getMyself() throws IOException {
@WithBridgeMethods(GHUser.class)
public GHMyself getMyself() throws IOException {
requireCredential();
if (oauthAccessToken != null) {
GHUser u = retrieveWithAuth("/user/show", JsonUser.class).user;
u.root = this;
users.put(u.getLogin(), u);
return u;
} else {
return getUser(login);
}
GHMyself u = retrieveWithAuth("/user/show", JsonMyself.class).user;
u.root = this;
users.put(u.getLogin(), u);
return u;
}
/**

View File

@@ -0,0 +1,36 @@
/*
* The MIT License
*
* Copyright (c) 2011, Eric Maupin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
/**
* @author Eric Maupin
*/
class JsonBranch {
GHBranch branch;
GHBranch wrap(GHRepository r) {
return branch.wrap(r);
}
}

View File

@@ -0,0 +1,40 @@
/*
* The MIT License
*
* Copyright (c) 2011, Eric Maupin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import java.util.List;
/**
* @author Eric Maupin
*/
class JsonBranches {
List<GHBranch> branches;
public List<GHBranch> wrap(GHRepository owner) {
for (GHBranch branch : branches)
branch.wrap(owner);
return branches;
}
}

View File

@@ -0,0 +1,8 @@
package org.kohsuke.github;
/**
* @author Kohsuke Kawaguchi
*/
class JsonMyself {
GHMyself user;
}

View File

@@ -5,6 +5,7 @@ import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHEventInfo;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHOrganization.Permission;
@@ -14,7 +15,9 @@ import org.kohsuke.github.GitHub;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import java.util.List;
/**
* Unit test for simple App.
@@ -55,6 +58,13 @@ public class AppTest extends TestCase {
System.out.println(o);
}
public void testBranches() throws Exception {
GitHub gitHub = GitHub.connect();
Map<String,GHBranch> b =
gitHub.getUser("jenkinsci").getRepository("jenkins").getBranches();
System.out.println(b);
}
public void tryHook() throws Exception {
GitHub gitHub = GitHub.connect();
GHRepository r = gitHub.getMyself().getRepository("test2");
@@ -64,8 +74,8 @@ public class AppTest extends TestCase {
for (GHHook h : r.getHooks())
h.delete();
}
public void testApp() throws IOException {
public void testEventApi() throws Exception {
GitHub gitHub = GitHub.connect();
for (GHEventInfo ev : gitHub.getEvents()) {
System.out.println(ev);
@@ -75,6 +85,11 @@ public class AppTest extends TestCase {
System.out.println(pr.getPullRequest());
}
}
}
public void testApp() throws IOException {
GitHub gitHub = GitHub.connect();
System.out.println(gitHub.getMyself().getEmails());
// GHRepository r = gitHub.connect().getOrganization("jenkinsci").createRepository("kktest4", "Kohsuke's test", "http://kohsuke.org/", "Everyone", true);
// r.fork();