From 1bf3e025b8f058232529d732aa05e1f8ad853cfb Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 5 Sep 2012 19:30:18 -0700 Subject: [PATCH 01/10] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f8ac35a431..966a5854c9 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.32 + 1.33-SNAPSHOT GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java From 0e226a8f78dfa6586d0452c723004f598c482bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Br=C3=A1zdil?= Date: Thu, 6 Sep 2012 14:23:51 +0300 Subject: [PATCH 02/10] When using lazy population, this is not deprecated --- src/main/java/org/kohsuke/github/GHIssue.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java index 18f2594e3a..9d85a274aa 100644 --- a/src/main/java/org/kohsuke/github/GHIssue.java +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -211,7 +211,6 @@ public GHUser getAssignee() { /** * User who submitted the issue. */ - @Deprecated public GHUser getUser() { return user; } From e658a7fa6b0c0704ce706b3094b879df10c03116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Br=C3=A1zdil?= Date: Wed, 12 Sep 2012 18:04:59 +0200 Subject: [PATCH 03/10] send authentication header on all requests --- .../java/org/kohsuke/github/GHCommit.java | 1 - .../org/kohsuke/github/GHCommitComment.java | 4 ++-- src/main/java/org/kohsuke/github/GHHook.java | 2 +- src/main/java/org/kohsuke/github/GHIssue.java | 4 ++-- .../java/org/kohsuke/github/GHMyself.java | 4 ++-- .../org/kohsuke/github/GHOrganization.java | 12 +++++----- .../java/org/kohsuke/github/GHPerson.java | 2 +- .../java/org/kohsuke/github/GHRepository.java | 22 +++++++++---------- src/main/java/org/kohsuke/github/GHTeam.java | 12 +++++----- src/main/java/org/kohsuke/github/GHUser.java | 4 ++-- src/main/java/org/kohsuke/github/GitHub.java | 10 ++++----- .../java/org/kohsuke/github/Requester.java | 5 ++--- 12 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHCommit.java b/src/main/java/org/kohsuke/github/GHCommit.java index 8069066780..d7bb9492c7 100644 --- a/src/main/java/org/kohsuke/github/GHCommit.java +++ b/src/main/java/org/kohsuke/github/GHCommit.java @@ -225,7 +225,6 @@ public GHCommitComment createComment(String body, String path, Integer line, Int .with("path",path) .with("line",line) .with("position",position) - .withCredential() .to(String.format("/repos/%s/%s/commits/%s/comments",owner.getOwnerName(),owner.getName(),sha),GHCommitComment.class); return r.wrap(owner); } diff --git a/src/main/java/org/kohsuke/github/GHCommitComment.java b/src/main/java/org/kohsuke/github/GHCommitComment.java index 9ff07304eb..a5f23877f8 100644 --- a/src/main/java/org/kohsuke/github/GHCommitComment.java +++ b/src/main/java/org/kohsuke/github/GHCommitComment.java @@ -99,7 +99,7 @@ public GHCommit getCommit() throws IOException { public void update(String body) throws IOException { GHCommitComment r = new Requester(owner.root) .with("body", body) - .withCredential().method("PATCH").to(getApiTail(), GHCommitComment.class); + .method("PATCH").to(getApiTail(), GHCommitComment.class); this.body = body; } @@ -107,7 +107,7 @@ public void update(String body) throws IOException { * Deletes this comment. */ public void delete() throws IOException { - new Requester(owner.root).withCredential().method("DELETE").to(getApiTail()); + new Requester(owner.root).method("DELETE").to(getApiTail()); } private String getApiTail() { diff --git a/src/main/java/org/kohsuke/github/GHHook.java b/src/main/java/org/kohsuke/github/GHHook.java index 8d1e5da027..fa797db933 100644 --- a/src/main/java/org/kohsuke/github/GHHook.java +++ b/src/main/java/org/kohsuke/github/GHHook.java @@ -54,6 +54,6 @@ public int getId() { * Deletes this hook. */ public void delete() throws IOException { - new Requester(repository.root).withCredential().method("DELETE").to(String.format("/repos/%s/%s/hooks/%d", repository.getOwnerName(), repository.getName(), id)); + new Requester(repository.root).method("DELETE").to(String.format("/repos/%s/%s/hooks/%d", repository.getOwnerName(), repository.getName(), id)); } } diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java index 9d85a274aa..74cb7fedd1 100644 --- a/src/main/java/org/kohsuke/github/GHIssue.java +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -138,11 +138,11 @@ public URL getApiURL(){ * Updates the issue by adding a comment. */ public void comment(String message) throws IOException { - new Requester(root).withCredential().with("body",message).to(getApiRoute() + "/comments"); + new Requester(root).with("body",message).to(getApiRoute() + "/comments"); } private void edit(String key, Object value) throws IOException { - new Requester(root).withCredential()._with(key, value).method("PATCH").to(getApiRoute()); + new Requester(root)._with(key, value).method("PATCH").to(getApiRoute()); } /** diff --git a/src/main/java/org/kohsuke/github/GHMyself.java b/src/main/java/org/kohsuke/github/GHMyself.java index 2a16abcdad..31be58a707 100644 --- a/src/main/java/org/kohsuke/github/GHMyself.java +++ b/src/main/java/org/kohsuke/github/GHMyself.java @@ -22,7 +22,7 @@ public class GHMyself extends GHUser { * Always non-null. */ public List getEmails() throws IOException { - String[] addresses = root.retrieve().withCredential().to("/user/emails", String[].class); + String[] addresses = root.retrieve().to("/user/emails", String[].class); return Collections.unmodifiableList(Arrays.asList(addresses)); } @@ -33,7 +33,7 @@ public List getEmails() throws IOException { * Always non-null. */ public List getPublicKeys() throws IOException { - return Collections.unmodifiableList(Arrays.asList(root.retrieve().withCredential().to("/user/keys", GHKey[].class))); + return Collections.unmodifiableList(Arrays.asList(root.retrieve().to("/user/keys", GHKey[].class))); } // public void addEmails(Collection emails) throws IOException { diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index 8328c72235..188f26d930 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -33,7 +33,7 @@ public GHRepository createRepository(String name, String description, String hom public GHRepository createRepository(String name, String description, String homepage, GHTeam team, boolean isPublic) throws IOException { // such API doesn't exist, so fall back to HTML scraping - return new Requester(root).withCredential() + return new Requester(root) .with("name", name).with("description", description).with("homepage", homepage) .with("public", isPublic).with("team_id",team.getId()).to("/orgs/"+login+"/repos", GHRepository.class).wrap(root); } @@ -42,7 +42,7 @@ public GHRepository createRepository(String name, String description, String hom * Teams by their names. */ public Map getTeams() throws IOException { - GHTeam[] teams = root.retrieve().withCredential().to("/orgs/" + login + "/teams", GHTeam[].class); + GHTeam[] teams = root.retrieve().to("/orgs/" + login + "/teams", GHTeam[].class); Map r = new TreeMap(); for (GHTeam t : teams) { r.put(t.getName(),t.wrapUp(this)); @@ -54,7 +54,7 @@ public Map getTeams() throws IOException { * Publicizes the membership. */ public void publicize(GHUser u) throws IOException { - root.retrieve().withCredential().method("PUT").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); + root.retrieve().method("PUT").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); } /** @@ -64,7 +64,7 @@ public List getMembers() throws IOException { return new AbstractList() { // these are shallow objects with only some limited values filled out // TODO: it's better to allow objects to fill themselves in later when missing values are requested - final GHUser[] shallow = root.retrieve().withCredential().to("/orgs/" + login + "/members", GHUser[].class); + final GHUser[] shallow = root.retrieve().to("/orgs/" + login + "/members", GHUser[].class); @Override public GHUser get(int index) { @@ -86,7 +86,7 @@ public int size() { * Conceals the membership. */ public void conceal(GHUser u) throws IOException { - root.retrieve().withCredential().method("DELETE").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); + root.retrieve().method("DELETE").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); } public enum Permission { ADMIN, PUSH, PULL } @@ -95,7 +95,7 @@ public enum Permission { ADMIN, PUSH, PULL } * Creates a new team and assigns the repositories. */ public GHTeam createTeam(String name, Permission p, Collection repositories) throws IOException { - Requester post = new Requester(root).withCredential().with("name", name).with("permission", p.name().toLowerCase()); + Requester post = new Requester(root).with("name", name).with("permission", p.name().toLowerCase()); List repo_names = new ArrayList(); for (GHRepository r : repositories) { repo_names.add(r.getName()); diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java index 71c7c54c6f..7f65277f4f 100644 --- a/src/main/java/org/kohsuke/github/GHPerson.java +++ b/src/main/java/org/kohsuke/github/GHPerson.java @@ -96,7 +96,7 @@ public void remove() { */ public GHRepository getRepository(String name) throws IOException { try { - return root.retrieve().withCredential().to("/repos/" + login + '/' + name, GHRepository.class).wrap(root); + return root.retrieve().to("/repos/" + login + '/' + name, GHRepository.class).wrap(root); } catch (FileNotFoundException e) { return null; } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 291b616c3a..38a90dbe53 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -231,7 +231,7 @@ public Set getCollaboratorNames() throws IOException { * If this repository belongs to an organization, return a set of teams. */ public Set getTeams() throws IOException { - return Collections.unmodifiableSet(new HashSet(Arrays.asList(GHTeam.wrapUp(root.retrieve().withCredential().to("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login))))); + return Collections.unmodifiableSet(new HashSet(Arrays.asList(GHTeam.wrapUp(root.retrieve().to("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login))))); } public void addCollaborators(GHUser... users) throws IOException { @@ -253,7 +253,7 @@ public void removeCollaborators(Collection users) throws IOException { private void modifyCollaborators(Collection users, String method) throws IOException { verifyMine(); for (GHUser user : users) { - new Requester(root).withCredential().method(method).to("/repos/" + owner.login + "/" + name + "/collaborators/" + user.getLogin()); + new Requester(root).method(method).to("/repos/" + owner.login + "/" + name + "/collaborators/" + user.getLogin()); } } @@ -270,7 +270,7 @@ public void setEmailServiceHook(String address) throws IOException { } private void edit(String key, String value) throws IOException { - Requester requester = new Requester(root).withCredential(); + Requester requester = new Requester(root); if (!key.equals("name")) requester.with("name", name); // even when we don't change the name, we need to send it in requester.with(key, value).method("PATCH").to("/repos/" + owner.login + "/" + name); @@ -313,7 +313,7 @@ public void setHomepage(String value) throws IOException { * Deletes this repository. */ public void delete() throws IOException { - new Requester(root).withCredential().method("DELETE").to("/repos/" + owner.login + "/" + name); + new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name); } /** @@ -323,7 +323,7 @@ public void delete() throws IOException { * Newly forked repository that belong to you. */ public GHRepository fork() throws IOException { - return new Requester(root).withCredential().method("POST").to("/repos/" + owner.login + "/" + name + "/forks", GHRepository.class).wrap(root); + return new Requester(root).method("POST").to("/repos/" + owner.login + "/" + name + "/forks", GHRepository.class).wrap(root); } /** @@ -333,7 +333,7 @@ public GHRepository fork() throws IOException { * Newly forked repository that belong to you. */ public GHRepository forkTo(GHOrganization org) throws IOException { - new Requester(root).withCredential().to(String.format("/repos/%s/%s/forks?org=%s",owner.login,name,org.getLogin())); + new Requester(root).to(String.format("/repos/%s/%s/forks?org=%s",owner.login,name,org.getLogin())); // this API is asynchronous. we need to wait for a bit for (int i=0; i<10; i++) { @@ -352,7 +352,7 @@ public GHRepository forkTo(GHOrganization org) throws IOException { * Retrieves a specified pull request. */ public GHPullRequest getPullRequest(int i) throws IOException { - return root.retrieve().withCredential().to("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this); + return root.retrieve().to("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this); } /** @@ -386,14 +386,14 @@ protected void wrapUp(GHPullRequest[] page) { */ public List getHooks() throws IOException { List list = new ArrayList(Arrays.asList( - root.retrieve().withCredential().to(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class))); + root.retrieve().to(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class))); for (GHHook h : list) h.wrap(this); return list; } public GHHook getHook(int id) throws IOException { - return root.retrieve().withCredential().to(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this); + return root.retrieve().to(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this); } /** @@ -476,7 +476,6 @@ public GHCommitStatus getLastCommitStatus(String sha1) throws IOException { */ public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description) throws IOException { return new Requester(root) - .withCredential() .with("state",state.name().toLowerCase(Locale.ENGLISH)) .with("target_url", targetUrl) .with("description", description) @@ -505,7 +504,6 @@ public GHHook createHook(String name, Map config, Collection getMembers() throws IOException { - return new HashSet(Arrays.asList(GHUser.wrap(org.root.retrieve().withCredential().to(api("/members"), GHUser[].class), org.root))); + return new HashSet(Arrays.asList(GHUser.wrap(org.root.retrieve().to(api("/members"), GHUser[].class), org.root))); } public Map getRepositories() throws IOException { - GHRepository[] repos = org.root.retrieve().withCredential().to(api("/repos"), GHRepository[].class); + GHRepository[] repos = org.root.retrieve().to(api("/repos"), GHRepository[].class); Map m = new TreeMap(); for (GHRepository r : repos) { m.put(r.getName(),r.wrap(org.root)); @@ -62,22 +62,22 @@ public Map getRepositories() throws IOException { * Adds a member to the team. */ public void add(GHUser u) throws IOException { - org.root.retrieve().withCredential().method("PUT").to(api("/members/" + u.getLogin()), null); + org.root.retrieve().method("PUT").to(api("/members/" + u.getLogin()), null); } /** * Removes a member to the team. */ public void remove(GHUser u) throws IOException { - org.root.retrieve().withCredential().method("DELETE").to(api("/members/" + u.getLogin()), null); + org.root.retrieve().method("DELETE").to(api("/members/" + u.getLogin()), null); } public void add(GHRepository r) throws IOException { - org.root.retrieve().withCredential().method("PUT").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); + org.root.retrieve().method("PUT").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); } public void remove(GHRepository r) throws IOException { - org.root.retrieve().withCredential().method("DELETE").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); + org.root.retrieve().method("DELETE").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); } private String api(String tail) { diff --git a/src/main/java/org/kohsuke/github/GHUser.java b/src/main/java/org/kohsuke/github/GHUser.java index 329434f38c..41fe9a24bb 100644 --- a/src/main/java/org/kohsuke/github/GHUser.java +++ b/src/main/java/org/kohsuke/github/GHUser.java @@ -41,14 +41,14 @@ public class GHUser extends GHPerson { * Follow this user. */ public void follow() throws IOException { - new Requester(root).withCredential().method("PUT").to("/user/following/" + login); + new Requester(root).method("PUT").to("/user/following/" + login); } /** * Unfollow this user. */ public void unfollow() throws IOException { - new Requester(root).withCredential().method("DELETE").to("/user/following/" + login); + new Requester(root).method("DELETE").to("/user/following/" + login); } /** diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 886239820b..9e5e970ded 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -167,7 +167,7 @@ public static GitHub connectAnonymously() { * Gets the current rate limit. */ public GHRateLimit getRateLimit() throws IOException { - return retrieve().withCredential().to("/rate_limit", JsonRateLimit.class).rate; + return retrieve().to("/rate_limit", JsonRateLimit.class).rate; } /** @@ -177,7 +177,7 @@ public GHRateLimit getRateLimit() throws IOException { public GHMyself getMyself() throws IOException { requireCredential(); - GHMyself u = retrieve().withCredential().to("/user", GHMyself.class); + GHMyself u = retrieve().to("/user", GHMyself.class); u.root = this; users.put(u.getLogin(), u); @@ -237,7 +237,7 @@ public GHRepository getRepository(String name) throws IOException { * TODO: make this automatic. */ public Map getMyOrganizations() throws IOException { - GHOrganization[] orgs = retrieve().withCredential().to("/user/orgs", GHOrganization[].class); + GHOrganization[] orgs = retrieve().to("/user/orgs", GHOrganization[].class); Map r = new HashMap(); for (GHOrganization o : orgs) { // don't put 'o' into orgs because they are shallow @@ -277,7 +277,7 @@ public T parseEventPayload(Reader r, Class type) t * Newly created repository. */ public GHRepository createRepository(String name, String description, String homepage, boolean isPublic) throws IOException { - Requester requester = new Requester(this).withCredential() + Requester requester = new Requester(this) .with("name", name).with("description", description).with("homepage", homepage) .with("public", isPublic ? 1 : 0); return requester.method("POST").to("/user/repos", GHRepository.class).wrap(this); @@ -288,7 +288,7 @@ public GHRepository createRepository(String name, String description, String hom */ public boolean isCredentialValid() throws IOException { try { - retrieve().withCredential().to("/user", GHUser.class); + retrieve().to("/user", GHUser.class); return true; } catch (IOException e) { return false; diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index be8bb3ffd6..c6bf92639c 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -55,7 +55,6 @@ class Requester { private final GitHub root; private final List args = new ArrayList(); - private boolean authenticate; /** * Request method. @@ -79,10 +78,10 @@ private Entry(String key, Object value) { /** * Makes a request with authentication credential. */ + @Deprecated public Requester withCredential() { // keeping it inline with retrieveWithAuth not to enforce the check // root.requireCredential(); - authenticate = true; return this; } @@ -269,7 +268,7 @@ private HttpURLConnection setupConnection(URL url) throws IOException { // if the authentication is needed but no credential is given, try it anyway (so that some calls // that do work with anonymous access in the reduced form should still work.) // if OAuth token is present, it'll be set in the URL, so need to set the Authorization header - if (authenticate && root.encodedAuthorization!=null && root.oauthAccessToken == null) + if (root.encodedAuthorization!=null && root.oauthAccessToken == null) uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization); try { From aed888051e5d2c821e7a70ed804047f461fa29d4 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 15:36:32 -0700 Subject: [PATCH 04/10] added a method to retrieve a single issue --- src/main/java/org/kohsuke/github/GHRepository.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 38a90dbe53..50a184fc48 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -140,6 +140,10 @@ public GHUser getOwner() throws IOException { return root.getUser(owner.login); // because 'owner' isn't fully populated } + public GHIssue getIssue(int id) throws IOException { + return root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues/" + id, GHIssue.class).wrap(this); + } + public List getIssues(GHIssueState state) throws IOException { return Arrays.asList(GHIssue.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this)); } @@ -333,7 +337,7 @@ public GHRepository fork() throws IOException { * Newly forked repository that belong to you. */ public GHRepository forkTo(GHOrganization org) throws IOException { - new Requester(root).to(String.format("/repos/%s/%s/forks?org=%s",owner.login,name,org.getLogin())); + new Requester(root).to(String.format("/repos/%s/%s/forks?org=%s", owner.login, name, org.getLogin())); // this API is asynchronous. we need to wait for a bit for (int i=0; i<10; i++) { @@ -476,7 +480,7 @@ public GHCommitStatus getLastCommitStatus(String sha1) throws IOException { */ public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description) throws IOException { return new Requester(root) - .with("state",state.name().toLowerCase(Locale.ENGLISH)) + .with("state", state.name().toLowerCase(Locale.ENGLISH)) .with("target_url", targetUrl) .with("description", description) .to(String.format("/repos/%s/%s/statuses/%s",owner.login,this.name,sha1),GHCommitStatus.class).wrapUp(root); @@ -504,7 +508,7 @@ public GHHook createHook(String name, Map config, Collection Date: Thu, 13 Sep 2012 15:46:25 -0700 Subject: [PATCH 05/10] fixed issue #20 PagedIterator is updated to cope with the base iterator returning array of length 0 --- src/main/java/org/kohsuke/github/PagedIterator.java | 12 ++++++++++-- src/test/java/org/kohsuke/AppTest.java | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/PagedIterator.java b/src/main/java/org/kohsuke/github/PagedIterator.java index 74c197aa2e..9149290911 100644 --- a/src/main/java/org/kohsuke/github/PagedIterator.java +++ b/src/main/java/org/kohsuke/github/PagedIterator.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; /** * Iterator over a pagenated data source. @@ -28,17 +29,24 @@ public abstract class PagedIterator implements Iterator { protected abstract void wrapUp(T[] page); public boolean hasNext() { - return (current!=null && pos v = repository.getIssue(13).getComments(); + System.out.println(v); + assertTrue(v.isEmpty()); + + v = repository.getIssue(5).getComments(); + System.out.println(v); + assertTrue(v.size()==3); + } + public void testRateLimit() throws IOException { System.out.println(GitHub.connect().getRateLimit()); } From e53e62bfa0321de51f6176f73e5e6d232316819a Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 15:56:05 -0700 Subject: [PATCH 06/10] added code to create a new issue --- .../org/kohsuke/github/GHIssueBuilder.java | 29 +++++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 4 +++ src/test/java/org/kohsuke/AppTest.java | 8 +++-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHIssueBuilder.java diff --git a/src/main/java/org/kohsuke/github/GHIssueBuilder.java b/src/main/java/org/kohsuke/github/GHIssueBuilder.java new file mode 100644 index 0000000000..cd51752ecc --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHIssueBuilder.java @@ -0,0 +1,29 @@ +package org.kohsuke.github; + +import java.io.IOException; + +/** + * @author Kohsuke Kawaguchi + */ +public class GHIssueBuilder { + private final GHRepository repo; + private final Requester builder; + + GHIssueBuilder(GHRepository repo, String title) { + this.repo = repo; + this.builder = new Requester(repo.root); + builder.with("title",title); + } + + public GHIssueBuilder body(String str) { + builder.with("body",str); + return this; + } + + /** + * Creates a new issue. + */ + public GHIssue create() throws IOException { + return builder.to("/repos/"+repo.getOwnerName()+'/'+repo.getName()+"/issues",GHIssue.class).wrap(repo); + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 50a184fc48..c12a6c16c9 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -144,6 +144,10 @@ public GHIssue getIssue(int id) throws IOException { return root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues/" + id, GHIssue.class).wrap(this); } + public GHIssueBuilder createIssue(String title) { + return new GHIssueBuilder(this,title); + } + public List getIssues(GHIssueState state) throws IOException { return Arrays.asList(GHIssue.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this)); } diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index f8d9976d6a..79110bed1c 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -4,7 +4,6 @@ import org.kohsuke.github.GHCommit; import org.kohsuke.github.GHCommit.File; import org.kohsuke.github.GHCommitComment; -import org.kohsuke.github.GHCommitState; import org.kohsuke.github.GHCommitStatus; import org.kohsuke.github.GHEvent; import org.kohsuke.github.GHEventInfo; @@ -24,7 +23,6 @@ import org.kohsuke.github.GHUser; import org.kohsuke.github.GitHub; import org.kohsuke.github.PagedIterable; -import org.kohsuke.github.PagedIterator; import java.io.IOException; import java.net.URL; @@ -63,6 +61,12 @@ public void testIssueWithNoComment() throws IOException { assertTrue(v.size()==3); } + public void testCreateIssue() throws IOException { + GHIssue o = GitHub.connect().getRepository("kohsuke/test").createIssue("testing").body("this is body").create(); + System.out.println(o.getUrl()); + o.close(); + } + public void testRateLimit() throws IOException { System.out.println(GitHub.connect().getRateLimit()); } From 7f15f1266887627a17cec17bd3d685641fe511a4 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 16:23:06 -0700 Subject: [PATCH 07/10] completed code to create a new issue --- .../org/kohsuke/github/GHIssueBuilder.java | 32 ++++++++++++++++++- .../java/org/kohsuke/github/GHRepository.java | 32 ++++++++++++++++--- src/test/java/org/kohsuke/AppTest.java | 6 +++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHIssueBuilder.java b/src/main/java/org/kohsuke/github/GHIssueBuilder.java index cd51752ecc..385fcd04a0 100644 --- a/src/main/java/org/kohsuke/github/GHIssueBuilder.java +++ b/src/main/java/org/kohsuke/github/GHIssueBuilder.java @@ -1,6 +1,8 @@ package org.kohsuke.github; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * @author Kohsuke Kawaguchi @@ -8,6 +10,7 @@ public class GHIssueBuilder { private final GHRepository repo; private final Requester builder; + private List labels = new ArrayList(); GHIssueBuilder(GHRepository repo, String title) { this.repo = repo; @@ -15,15 +18,42 @@ public class GHIssueBuilder { builder.with("title",title); } + /** + * Sets the main text of an issue, which is arbitrary multi-line text. + */ public GHIssueBuilder body(String str) { builder.with("body",str); return this; } + public GHIssueBuilder assignee(GHUser user) { + if (user!=null) + builder.with("assignee",user.getLogin()); + return this; + } + + public GHIssueBuilder assignee(String user) { + if (user!=null) + builder.with("assignee",user); + return this; + } + + public GHIssueBuilder milestone(GHMilestone milestone) { + if (milestone!=null) + builder.with("milestone",milestone.getNumber()); + return this; + } + + public GHIssueBuilder label(String label) { + if (label!=null) + labels.add(label); + return this; + } + /** * Creates a new issue. */ public GHIssue create() throws IOException { - return builder.to("/repos/"+repo.getOwnerName()+'/'+repo.getName()+"/issues",GHIssue.class).wrap(repo); + return builder.with("labels",labels).to(repo.getApiTailUrl("issues"),GHIssue.class).wrap(repo); } } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index c12a6c16c9..e8e23ffa28 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -616,24 +616,42 @@ public boolean remove(Object url) { */ public Map getBranches() throws IOException { Map r = new TreeMap(); - for (GHBranch p : root.retrieve().to("/repos/" + owner.login + "/" + name + "/branches", GHBranch[].class)) { + for (GHBranch p : root.retrieve().to(getApiTailUrl("branches"), GHBranch[].class)) { p.wrap(this); r.put(p.getName(),p); } return r; } + /** + * @deprecated + * Use {@link #listMilestones(GHIssueState)} + */ public Map getMilestones() throws IOException { Map milestones = new TreeMap(); - GHMilestone[] ms = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone[].class); - for (GHMilestone m : ms) { - m.owner = this; - m.root = root; + for (GHMilestone m : listMilestones(GHIssueState.OPEN)) { milestones.put(m.getNumber(), m); } return milestones; } + /** + * Lists up all the milestones in this repository. + */ + public PagedIterable listMilestones(final GHIssueState state) { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("milestones?state="+state.toString().toLowerCase(Locale.ENGLISH)), GHMilestone[].class)) { + @Override + protected void wrapUp(GHMilestone[] page) { + for (GHMilestone c : page) + c.wrap(GHRepository.this); + } + }; + } + }; + } + public GHMilestone getMilestone(int number) throws IOException { GHMilestone m = milestones.get(number); if (m == null) { @@ -669,4 +687,8 @@ public boolean equals(Object obj) { } return false; } + + String getApiTailUrl(String tail) { + return "/repos/" + owner.login + "/" + name +'/'+tail; + } } diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index 79110bed1c..0475137c74 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -14,6 +14,7 @@ import org.kohsuke.github.GHIssueComment; import org.kohsuke.github.GHIssueState; import org.kohsuke.github.GHKey; +import org.kohsuke.github.GHMilestone; import org.kohsuke.github.GHMyself; import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHOrganization.Permission; @@ -62,7 +63,10 @@ public void testIssueWithNoComment() throws IOException { } public void testCreateIssue() throws IOException { - GHIssue o = GitHub.connect().getRepository("kohsuke/test").createIssue("testing").body("this is body").create(); + GHUser u = GitHub.connect().getUser("kohsuke"); + GHRepository r = u.getRepository("test"); + GHMilestone someMilestone = r.listMilestones(GHIssueState.CLOSED).iterator().next(); + GHIssue o = r.createIssue("testing").body("this is body").assignee(u).label("bug").label("question").milestone(someMilestone).create(); System.out.println(o.getUrl()); o.close(); } From b17f506c201f5f4cdeb155691bef0d9b41f3d54d Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 16:23:57 -0700 Subject: [PATCH 08/10] clean up --- src/main/java/org/kohsuke/github/GHRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index e8e23ffa28..0260bb888a 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -655,7 +655,7 @@ protected void wrapUp(GHMilestone[] page) { public GHMilestone getMilestone(int number) throws IOException { GHMilestone m = milestones.get(number); if (m == null) { - m = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones/" + number, GHMilestone.class); + m = root.retrieve().to(getApiTailUrl("milestones/" + number), GHMilestone.class); m.owner = this; m.root = root; milestones.put(m.getNumber(), m); @@ -665,7 +665,7 @@ public GHMilestone getMilestone(int number) throws IOException { public GHMilestone createMilestone(String title, String description) throws IOException { return new Requester(root) - .with("title", title).with("description", description).method("POST").to("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone.class).wrap(this); + .with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this); } @Override From 87f5231c9a1cb88cbf2316a13af47ca263ce5cbb Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 16:28:50 -0700 Subject: [PATCH 09/10] updated a test --- src/test/java/org/kohsuke/AppTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index 0475137c74..b272ee7a4d 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -52,12 +52,12 @@ public void testCredentialValid() throws IOException { } public void testIssueWithNoComment() throws IOException { - GHRepository repository = GitHub.connect().getRepository("kohsuke/github-api"); - List v = repository.getIssue(13).getComments(); + GHRepository repository = GitHub.connect().getRepository("kohsuke/test"); + List v = repository.getIssue(4).getComments(); System.out.println(v); assertTrue(v.isEmpty()); - v = repository.getIssue(5).getComments(); + v = repository.getIssue(3).getComments(); System.out.println(v); assertTrue(v.size()==3); } From f71afca828ef423de7198e805bc98d8e0d93e16a Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 13 Sep 2012 16:31:52 -0700 Subject: [PATCH 10/10] [maven-release-plugin] prepare release github-api-1.33 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 966a5854c9..3ca2bd00c0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.33-SNAPSHOT + 1.33 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java