adding lazy population to GHUser and got rid of GHSmallUser

This commit is contained in:
Kohsuke Kawaguchi
2012-09-05 19:25:56 -07:00
parent 2341f789ab
commit 1dd875adac
6 changed files with 80 additions and 124 deletions

View File

@@ -43,20 +43,20 @@ public class GHIssue {
GHRepository owner;
// API v3
private GHSmallUser assignee;
private String state;
private int number;
private String closed_at;
private int comments;
private String body;
private List<String> labels;
private GHSmallUser user;
private String title, created_at, html_url;
private GHIssue.PullRequest pull_request;
private GHMilestone milestone;
private String url, updated_at;
private int id;
private GHSmallUser closed_by;
protected GHUser assignee;
protected String state;
protected int number;
protected String closed_at;
protected int comments;
protected String body;
protected List<String> labels;
protected GHUser user;
protected String title, created_at, html_url;
protected GHIssue.PullRequest pull_request;
protected GHMilestone milestone;
protected String url, updated_at;
protected int id;
protected GHUser closed_by;
/*package*/ GHIssue wrap(GHRepository owner) {
this.owner = owner;
@@ -204,33 +204,19 @@ public class GHIssue {
return "/repos/"+owner.getOwnerName()+"/"+owner.getName()+"/issues/"+number;
}
public GHSmallUser getAssignee() {
public GHUser getAssignee() {
return assignee;
}
/**
* User who submitted the issue.
*
* @return May return null when IOException occures. Prefered way is getSmallUser().getUser().
* @see #getSmallUser()
*/
@Deprecated
public GHUser getUser() {
try {
return user.getUser();
} catch (IOException ex) {
return null;
}
return user;
}
/**
* Shallow user who submitted the issue.
*/
public GHSmallUser getSmallUser(){
return user;
}
public GHSmallUser getClosedBy() {
public GHUser getClosedBy() {
if(!"closed".equals(state)) return null;
if(closed_by != null) return closed_by;

View File

@@ -17,13 +17,13 @@ import java.util.TreeMap;
public abstract class GHPerson {
/*package almost final*/ GitHub root;
// common
protected String login,location,blog,email,name,created_at,company;
// core data fields that exist even for "small" user data (such as the user info in pull request)
protected String login, avatar_url, url, gravatar_id;
protected int id;
protected String gravatar_id; // appears in V3 as well but presumably subsumed by avatar_url?
// V3
protected String avatar_url,html_url;
// other fields (that only show up in full data)
protected String location,blog,email,name,created_at,company;
protected String html_url;
protected int followers,following,public_repos,public_gists;
/*package*/ GHPerson wrapUp(GitHub root) {
@@ -31,6 +31,17 @@ public abstract class GHPerson {
return this;
}
/**
* Fully populate the data by retrieving missing data.
*
* Depending on the original API call where this object is created, it may not contain everything.
*/
protected void populate() throws IOException {
if (created_at!=null) return; // already populated
root.retrieve().to(url, this);
}
/**
* Gets the repositories this user owns.
*/
@@ -123,51 +134,60 @@ public abstract class GHPerson {
/**
* Gets the human-readable name of the user, like "Kohsuke Kawaguchi"
*/
public String getName() {
public String getName() throws IOException {
populate();
return name;
}
/**
* Gets the company name of this user, like "Sun Microsystems, Inc."
*/
public String getCompany() {
public String getCompany() throws IOException {
populate();
return company;
}
/**
* Gets the location of this user, like "Santa Clara, California"
*/
public String getLocation() {
public String getLocation() throws IOException {
populate();
return location;
}
public String getCreatedAt() {
public String getCreatedAt() throws IOException {
populate();
return created_at;
}
/**
* Gets the blog URL of this user.
*/
public String getBlog() {
public String getBlog() throws IOException {
populate();
return blog;
}
/**
* Gets the e-mail address of the user.
*/
public String getEmail() {
public String getEmail() throws IOException {
populate();
return email;
}
public int getPublicGistCount() {
public int getPublicGistCount() throws IOException {
populate();
return public_gists;
}
public int getPublicRepoCount() {
public int getPublicRepoCount() throws IOException {
populate();
return public_repos;
}
public int getFollowingCount() {
public int getFollowingCount() throws IOException {
populate();
return following;
}
@@ -178,7 +198,8 @@ public abstract class GHPerson {
return id;
}
public int getFollowersCount() {
public int getFollowersCount() throws IOException {
populate();
return followers;
}

View File

@@ -23,6 +23,7 @@
*/
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Date;
@@ -41,7 +42,7 @@ public class GHPullRequest extends GHIssue {
private GHCommitPointer head;
// details that are only available when obtained from ID
private GHSmallUser merged_by;
private GHUser merged_by;
private int review_comments, additions;
private boolean merged;
private Boolean mergeable;
@@ -117,7 +118,7 @@ public class GHPullRequest extends GHIssue {
}
@Override
public GHSmallUser getClosedBy() {
public GHUser getClosedBy() {
return null;
}
@@ -130,49 +131,54 @@ public class GHPullRequest extends GHIssue {
// details that are only available via get with ID
//
//
public GHSmallUser getMergedBy() {
public GHUser getMergedBy() throws IOException {
populate();
return merged_by;
}
public int getReviewComments() {
public int getReviewComments() throws IOException {
populate();
return review_comments;
}
public int getAdditions() {
public int getAdditions() throws IOException {
populate();
return additions;
}
public boolean isMerged() {
public boolean isMerged() throws IOException {
populate();
return merged;
}
public Boolean getMergeable() {
public Boolean getMergeable() throws IOException {
populate();
return mergeable;
}
public int getDeletions() {
public int getDeletions() throws IOException {
populate();
return deletions;
}
public String getMergeableState() {
public String getMergeableState() throws IOException {
populate();
return mergeable_state;
}
public int getChangedFiles() {
public int getChangedFiles() throws IOException {
populate();
return changed_files;
}
private void populate() {
/**
* Fully populate the data by retrieving missing data.
*
* Depending on the original API call where this object is created, it may not contain everything.
*/
private void populate() throws IOException {
if (merged_by!=null) return; // already populated
root.retrieveWithAuth(getUrl(), this);
root.retrieve().to(url, this);
}
}

View File

@@ -1,65 +0,0 @@
/*
* The MIT License
*
* Copyright 2012 Honza Brázdil.
*
* 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.io.IOException;
import java.net.URL;
/**
* This represents a subset of user information that often appear as a part of a bigger data graph in the GitHub API.
*
* @author Honza Brázdil
* @see GHUser
*/
public class GHSmallUser {
private GitHub root;
private String avatar_url, login, url, gravatar_id;
private int id;
/*package*/ GHSmallUser wrapUp(GitHub root) {
this.root = root;
return this;
}
public URL getAvatarUrl() {
return GitHub.parseURL(avatar_url);
}
public String getLogin() {
return login;
}
public URL getApiUrl() {
return GitHub.parseURL(url);
}
public String getGravatarId() {
return gravatar_id;
}
public GHUser getUser() throws IOException{
return root.getUser(login);
}
}

View File

@@ -34,7 +34,6 @@ import java.util.Set;
* Represents an user of GitHub.
*
* @author Kohsuke Kawaguchi
* @see GHSmallUser
*/
public class GHUser extends GHPerson {

View File

@@ -11,6 +11,7 @@ import org.kohsuke.github.GHEventInfo;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHKey;
import org.kohsuke.github.GHMyself;
@@ -323,4 +324,12 @@ public class AppTest extends TestCase {
assertEquals("oops!",state.getDescription());
assertEquals("http://jenkins-ci.org/",state.getTargetUrl());
}
public void testPullRequestPopulate() throws Exception {
GitHub gitHub = GitHub.connect();
GHRepository r = gitHub.getUser("kohsuke").getRepository("github-api");
GHPullRequest p = r.getPullRequest(17);
GHUser u = p.getUser();
System.out.println(u.getName());
}
}