getTree() {
+ return Collections.unmodifiableList(Arrays.asList(tree));
+ }
+
+ /**
+ * Returns true if the number of items in the tree array exceeded the GitHub maximum limit.
+ * @return true true if the number of items in the tree array exceeded the GitHub maximum limit otherwise false.
+ */
+ public boolean isTruncated() {
+ return truncated;
+ }
+
+ /**
+ * The API URL of this tag, such as
+ * "url": "https://api.github.com/repos/octocat/Hello-World/trees/fc6274d15fa3ae2ab983129fb037999f264ba9a7",
+ */
+ public URL getUrl() {
+ return GitHub.parseURL(url);
+ }
+
+ /* package */GHTree wrap(GitHub root) {
+ this.root = root;
+ return this;
+ }
}
diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java
index 33601668ff..744a6d36e9 100644
--- a/src/main/java/org/kohsuke/github/GitHub.java
+++ b/src/main/java/org/kohsuke/github/GitHub.java
@@ -60,7 +60,7 @@
*
* This library aims to be safe for use by multiple threads concurrently, although
* the library itself makes no attempt to control/serialize potentially conflicting
- * operations to GitHub, such as updating & deleting a repository at the same time.
+ * operations to GitHub, such as updating & deleting a repository at the same time.
*
* @author Kohsuke Kawaguchi
*/
@@ -252,11 +252,11 @@ public GHRateLimit getRateLimit() throws IOException {
}
/**
- * Gets the {@link GHUser} that represents yourself.
- */
+ * Gets the {@link GHUser} that represents yourself.
+ */
@WithBridgeMethods(GHUser.class)
- public GHMyself getMyself() throws IOException {
- requireCredential();
+ public GHMyself getMyself() throws IOException {
+ requireCredential();
GHMyself u = retrieve().to("/user", GHMyself.class);
@@ -264,20 +264,20 @@ public GHMyself getMyself() throws IOException {
users.put(u.getLogin(), u);
return u;
- }
-
- /**
- * Obtains the object that represents the named user.
- */
- public GHUser getUser(String login) throws IOException {
- GHUser u = users.get(login);
- if (u == null) {
+ }
+
+ /**
+ * Obtains the object that represents the named user.
+ */
+ public GHUser getUser(String login) throws IOException {
+ GHUser u = users.get(login);
+ if (u == null) {
u = retrieve().to("/users/" + login, GHUser.class);
u.root = this;
users.put(u.getLogin(), u);
- }
- return u;
- }
+ }
+ return u;
+ }
/**
@@ -415,14 +415,14 @@ public GHRepository createRepository(String name, String description, String hom
*
* @see Documentation
*/
- public GHAuthorization createToken(Collection scope, String note, String noteUrl) throws IOException{
- Requester requester = new Requester(this)
- .with("scopes", scope)
- .with("note", note)
- .with("note_url", noteUrl);
-
- return requester.method("POST").to("/authorizations", GHAuthorization.class).wrap(this);
- }
+ public GHAuthorization createToken(Collection scope, String note, String noteUrl) throws IOException{
+ Requester requester = new Requester(this)
+ .with("scopes", scope)
+ .with("note", note)
+ .with("note_url", noteUrl);
+
+ return requester.method("POST").to("/authorizations", GHAuthorization.class).wrap(this);
+ }
/**
* Ensures that the credential is valid.
diff --git a/src/main/java/org/kohsuke/github/GitHubBuilder.java b/src/main/java/org/kohsuke/github/GitHubBuilder.java
index 98248b94b0..c0ecaffda5 100644
--- a/src/main/java/org/kohsuke/github/GitHubBuilder.java
+++ b/src/main/java/org/kohsuke/github/GitHubBuilder.java
@@ -45,18 +45,18 @@ public GitHubBuilder() {
* @throws IOException If there are no credentials defined in the ~/.github properties file or the process environment.
*/
public static GitHubBuilder fromCredentials() throws IOException {
- Exception cause = null;
- GitHubBuilder builder;
+ Exception cause = null;
+ GitHubBuilder builder;
- try {
- builder = fromPropertyFile();
+ try {
+ builder = fromPropertyFile();
- if (builder.user != null)
- return builder;
- } catch (FileNotFoundException e) {
+ if (builder.user != null)
+ return builder;
+ } catch (FileNotFoundException e) {
// fall through
cause = e;
- }
+ }
builder = fromEnvironment();
@@ -77,8 +77,8 @@ public static GitHubBuilder fromEnvironment(String loginVariableName, String pas
private static void loadIfSet(String envName, Properties p, String propName) {
String v = System.getenv(envName);
- if (v != null)
- p.put(propName, v);
+ if (v != null)
+ p.put(propName, v);
}
/**
@@ -87,12 +87,12 @@ private static void loadIfSet(String envName, Properties p, String propName) {
* different clients of this library will all recognize one consistent set of coordinates.
*/
public static GitHubBuilder fromEnvironment(String loginVariableName, String passwordVariableName, String oauthVariableName, String endpointVariableName) throws IOException {
- Properties env = new Properties();
- loadIfSet(loginVariableName,env,"login");
+ Properties env = new Properties();
+ loadIfSet(loginVariableName,env,"login");
loadIfSet(passwordVariableName,env,"password");
loadIfSet(oauthVariableName,env,"oauth");
loadIfSet(endpointVariableName,env,"endpoint");
- return fromProperties(env);
+ return fromProperties(env);
}
/**
@@ -116,7 +116,7 @@ public static GitHubBuilder fromEnvironment(String loginVariableName, String pas
* login, password, oauth
*/
public static GitHubBuilder fromEnvironment() throws IOException {
- Properties props = new Properties();
+ Properties props = new Properties();
for (Entry e : System.getenv().entrySet()) {
String name = e.getKey().toLowerCase(Locale.ENGLISH);
if (name.startsWith("github_")) name=name.substring(7);
diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java
index cbab7b82fb..960940e7cf 100644
--- a/src/main/java/org/kohsuke/github/Requester.java
+++ b/src/main/java/org/kohsuke/github/Requester.java
@@ -479,6 +479,9 @@ private InputStream wrapStream(InputStream in) throws IOException {
* Handle API error by either throwing it or by returning normally to retry.
*/
/*package*/ void handleApiError(IOException e) throws IOException {
+ if (uc.getResponseCode() == 401) // Unauthorized == bad creds
+ throw e;
+
if ("0".equals(uc.getHeaderField("X-RateLimit-Remaining"))) {
root.rateLimitHandler.onError(e,uc);
}
diff --git a/src/test/java/org/kohsuke/github/AbstractGitHubApiTestBase.java b/src/test/java/org/kohsuke/github/AbstractGitHubApiTestBase.java
index a4311429d7..f1753481e7 100644
--- a/src/test/java/org/kohsuke/github/AbstractGitHubApiTestBase.java
+++ b/src/test/java/org/kohsuke/github/AbstractGitHubApiTestBase.java
@@ -19,9 +19,9 @@ public void setUp() throws Exception {
if (f.exists()) {
// use the non-standard credential preferentially, so that developers of this library do not have
// to clutter their event stream.
- gitHub = GitHubBuilder.fromPropertyFile(f.getPath()).build();
+ gitHub = GitHubBuilder.fromPropertyFile(f.getPath()).withRateLimitHandler(RateLimitHandler.FAIL).build();
} else {
- gitHub = GitHub.connect();
+ gitHub = GitHubBuilder.fromCredentials().withRateLimitHandler(RateLimitHandler.FAIL).build();
}
}
diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java
index b5d7c517a7..5cdb17e987 100755
--- a/src/test/java/org/kohsuke/github/AppTest.java
+++ b/src/test/java/org/kohsuke/github/AppTest.java
@@ -598,10 +598,10 @@ public void testCreateRelease() throws Exception {
}
@Test
- public void testRef() throws IOException {
- GHRef masterRef = gitHub.getRepository("jenkinsci/jenkins").getRef("heads/master");
- assertEquals("https://api.github.com/repos/jenkinsci/jenkins/git/refs/heads/master", masterRef.getUrl().toString());
- }
+ public void testRef() throws IOException {
+ GHRef masterRef = gitHub.getRepository("jenkinsci/jenkins").getRef("heads/master");
+ assertEquals("https://api.github.com/repos/jenkinsci/jenkins/git/refs/heads/master", masterRef.getUrl().toString());
+ }
@Test
public void directoryListing() throws IOException {
@@ -618,8 +618,8 @@ public void directoryListing() throws IOException {
@Test
public void testAddDeployKey() throws IOException {
- GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(),0);
- final GHDeployKey newDeployKey = myRepository.addDeployKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUt0RAycC5cS42JKh6SecfFZBR1RrF+2hYMctz4mk74/arBE+wFb7fnSHGzdGKX2h5CFOWODifRCJVhB7hlVxodxe+QkQQYAEL/x1WVCJnGgTGQGOrhOMj95V3UE5pQKhsKD608C+u5tSofcWXLToP1/wZ7U4/AHjqYi08OLsWToHCax55TZkvdt2jo0hbIoYU+XI9Q8Uv4ONDN1oabiOdgeKi8+crvHAuvNleiBhWVBzFh8KdfzaH5uNdw7ihhFjEd1vzqACsjCINCjdMfzl6jD9ExuWuE92nZJnucls2cEoNC6k2aPmrZDg9hA32FXVpyseY+bDUWFU6LO2LG6PB kohsuke@atlas");
+ GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(),0);
+ final GHDeployKey newDeployKey = myRepository.addDeployKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUt0RAycC5cS42JKh6SecfFZBR1RrF+2hYMctz4mk74/arBE+wFb7fnSHGzdGKX2h5CFOWODifRCJVhB7hlVxodxe+QkQQYAEL/x1WVCJnGgTGQGOrhOMj95V3UE5pQKhsKD608C+u5tSofcWXLToP1/wZ7U4/AHjqYi08OLsWToHCax55TZkvdt2jo0hbIoYU+XI9Q8Uv4ONDN1oabiOdgeKi8+crvHAuvNleiBhWVBzFh8KdfzaH5uNdw7ihhFjEd1vzqACsjCINCjdMfzl6jD9ExuWuE92nZJnucls2cEoNC6k2aPmrZDg9hA32FXVpyseY+bDUWFU6LO2LG6PB kohsuke@atlas");
try {
assertNotNull(newDeployKey.getId());
@@ -636,11 +636,11 @@ public boolean apply(GHDeployKey deployKey) {
@Test
public void testCommitStatusContext() throws IOException {
- GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(), 0);
- GHRef masterRef = myRepository.getRef("heads/master");
- GHCommitStatus commitStatus = myRepository.createCommitStatus(masterRef.getObject().getSha(), GHCommitState.SUCCESS, "http://www.example.com", "test", "test/context");
- assertEquals("test/context", commitStatus.getContext());
-
+ GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(), 0);
+ GHRef masterRef = myRepository.getRef("heads/master");
+ GHCommitStatus commitStatus = myRepository.createCommitStatus(masterRef.getObject().getSha(), GHCommitState.SUCCESS, "http://www.example.com", "test", "test/context");
+ assertEquals("test/context", commitStatus.getContext());
+
}
@Test
@@ -671,28 +671,28 @@ public void testReadme() throws IOException {
@Test
public void testTrees() throws IOException {
- GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTree("master");
- boolean foundReadme = false;
- for(GHTreeEntry e : masterTree.getTree()){
- if("readme".equalsIgnoreCase(e.getPath().replaceAll(".md", ""))){
- foundReadme = true;
- break;
- }
- }
- assertTrue(foundReadme);
+ GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTree("master");
+ boolean foundReadme = false;
+ for(GHTreeEntry e : masterTree.getTree()){
+ if("readme".equalsIgnoreCase(e.getPath().replaceAll("\\.md", ""))){
+ foundReadme = true;
+ break;
+ }
+ }
+ assertTrue(foundReadme);
}
@Test
public void testTreesRecursive() throws IOException {
- GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTreeRecursive("master", 1);
- boolean foundThisFile = false;
- for(GHTreeEntry e : masterTree.getTree()){
- if(e.getPath().endsWith(AppTest.class.getSimpleName() + ".java")){
- foundThisFile = true;
- break;
- }
- }
- assertTrue(foundThisFile);
+ GHTree masterTree = gitHub.getRepository("kohsuke/github-api").getTreeRecursive("master", 1);
+ boolean foundThisFile = false;
+ for(GHTreeEntry e : masterTree.getTree()){
+ if(e.getPath().endsWith(AppTest.class.getSimpleName() + ".java")){
+ foundThisFile = true;
+ break;
+ }
+ }
+ assertTrue(foundThisFile);
}
@Test
diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java
index 4ac4ecfea2..578bbc13f3 100644
--- a/src/test/java/org/kohsuke/github/GitHubTest.java
+++ b/src/test/java/org/kohsuke/github/GitHubTest.java
@@ -30,21 +30,21 @@ public void testGitHubServerWithoutServer() throws Exception {
}
public void testGitHubBuilderFromEnvironment() throws IOException {
-
- Mapprops = new HashMap();
-
- props.put("login", "bogus");
- props.put("oauth", "bogus");
- props.put("password", "bogus");
-
- setupEnvironment(props);
-
- GitHubBuilder builder = GitHubBuilder.fromEnvironment();
-
- assertEquals("bogus", builder.user);
- assertEquals("bogus", builder.oauthToken);
- assertEquals("bogus", builder.password);
-
+
+ Mapprops = new HashMap();
+
+ props.put("login", "bogus");
+ props.put("oauth", "bogus");
+ props.put("password", "bogus");
+
+ setupEnvironment(props);
+
+ GitHubBuilder builder = GitHubBuilder.fromEnvironment();
+
+ assertEquals("bogus", builder.user);
+ assertEquals("bogus", builder.oauthToken);
+ assertEquals("bogus", builder.password);
+
}
/*
@@ -55,59 +55,54 @@ public void testGitHubBuilderFromEnvironment() throws IOException {
* Its used to wire in values for the github credentials to test that the GitHubBuilder works properly to resolve them.
*/
private void setupEnvironment(Map newenv) {
- try
- {
- Class> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
- Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
- theEnvironmentField.setAccessible(true);
- Map env = (Map) theEnvironmentField.get(null);
- env.putAll(newenv);
- Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
- theCaseInsensitiveEnvironmentField.setAccessible(true);
- Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null);
- cienv.putAll(newenv);
- }
- catch (NoSuchFieldException e)
- {
- try {
- Class[] classes = Collections.class.getDeclaredClasses();
- Map env = System.getenv();
- for(Class cl : classes) {
- if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
- Field field = cl.getDeclaredField("m");
- field.setAccessible(true);
- Object obj = field.get(env);
- Map map = (Map) obj;
- map.clear();
- map.putAll(newenv);
- }
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- } catch (Exception e1) {
- e1.printStackTrace();
- }
-
- }
+ try {
+ Class> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
+ Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
+ theEnvironmentField.setAccessible(true);
+ Map env = (Map) theEnvironmentField.get(null);
+ env.putAll(newenv);
+ Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
+ theCaseInsensitiveEnvironmentField.setAccessible(true);
+ Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null);
+ cienv.putAll(newenv);
+ } catch (NoSuchFieldException e) {
+ try {
+ Class[] classes = Collections.class.getDeclaredClasses();
+ Map env = System.getenv();
+ for (Class cl : classes) {
+ if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
+ Field field = cl.getDeclaredField("m");
+ field.setAccessible(true);
+ Object obj = field.get(env);
+ Map map = (Map) obj;
+ map.clear();
+ map.putAll(newenv);
+ }
+ }
+ } catch (Exception e2) {
+ e2.printStackTrace();
+ }
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ }
+
+ public void testGitHubBuilderFromCustomEnvironment() throws IOException {
+ Map props = new HashMap();
+
+ props.put("customLogin", "bogusLogin");
+ props.put("customOauth", "bogusOauth");
+ props.put("customPassword", "bogusPassword");
+ props.put("customEndpoint", "bogusEndpoint");
- public void testGitHubBuilderFromCustomEnvironment() throws IOException {
-
- Mapprops = new HashMap();
-
- props.put("customLogin", "bogusLogin");
- props.put("customOauth", "bogusOauth");
- props.put("customPassword", "bogusPassword");
- props.put("customEndpoint", "bogusEndpoint");
+ setupEnvironment(props);
- setupEnvironment(props);
-
- GitHubBuilder builder = GitHubBuilder.fromEnvironment("customLogin", "customPassword", "customOauth", "customEndpoint");
-
- assertEquals("bogusLogin", builder.user);
- assertEquals("bogusOauth", builder.oauthToken);
- assertEquals("bogusPassword", builder.password);
- assertEquals("bogusEndpoint", builder.endpoint);
+ GitHubBuilder builder = GitHubBuilder.fromEnvironment("customLogin", "customPassword", "customOauth", "customEndpoint");
+
+ assertEquals("bogusLogin", builder.user);
+ assertEquals("bogusOauth", builder.oauthToken);
+ assertEquals("bogusPassword", builder.password);
+ assertEquals("bogusEndpoint", builder.endpoint);
}
-
+
}