From 5d2b4bdb16ea145b9dd7974dc338ebfb8a0e37f7 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sun, 18 Apr 2010 21:47:03 -0700 Subject: [PATCH] reworked the connect method --- src/main/java/org/kohsuke/github/GitHub.java | 38 ++++++++++++++++---- src/site/apt/index.apt | 13 ++++++- src/test/java/org/kohsuke/AppTest.java | 2 +- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 4b2ca258c..84bd1cb93 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -23,14 +23,19 @@ */ package org.kohsuke.github; +import org.apache.commons.io.IOUtils; import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.introspect.VisibilityChecker.Std; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.ANY; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE; @@ -46,18 +51,37 @@ public class GitHub { private final Map users = new HashMap(); + private GitHub(String login, String apiToken) { + this.login = login; + this.token = apiToken; + } + + /** + * Obtains the credential from "~/.github" + */ + public static GitHub connect() throws IOException { + Properties props = new Properties(); + File homeDir = new File(System.getProperty("user.home")); + FileInputStream in = new FileInputStream(new File(homeDir, ".github")); + try { + props.load(in); + } finally { + IOUtils.closeQuietly(in); + } + return new GitHub(props.getProperty("login"),props.getProperty("token")); + } + + public static GitHub connect(String login, String apiToken) throws IOException { + return new GitHub(login,apiToken); + } + /** * Connects to GitHub anonymously. * * All operations that requires authentication will fail. */ - public GitHub() { - this(null,null); - } - - public GitHub(String login, String apiToken) { - this.login = login; - this.token = apiToken; + public static GitHub connectAnonymously() { + return new GitHub(null,null); } /*package*/ void requireCredential() { diff --git a/src/site/apt/index.apt b/src/site/apt/index.apt index 90a6a6fd3..28254de23 100644 --- a/src/site/apt/index.apt +++ b/src/site/apt/index.apt @@ -5,10 +5,21 @@ What is this? Sample Usage ------------------ -GitHub github = new GitHub("kohsuke","myApiToken"); +GitHub github = GitHub.connect(); GHRepository repo = github.createRepository( "new-repository","this is my new repository", "http://www.kohsuke.org/",true/*public*/); repo.addCollaborators(github.getUser("abayer"),github.getUser("rtyler")); repo.delete(); ------------------ + +Credential + + This library allows the caller to supply the credential as parameters, but it also defines a common convention + so that applications using this library will look at the consistent location. In this convention, the library + looks at "~/.github" property file, which should have the following two values: + +------------------ +login=kohsuke +token=012345678 +------------------ diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index c6e4c9bab..ebbce47e3 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -12,7 +12,7 @@ import java.io.IOException; */ public class AppTest extends TestCase { public void testApp() throws IOException { - GitHub hub = new GitHub("kohsuke","9138245daf860415dfc66419177d95da"); + GitHub hub = GitHub.connectAnonymously(); // hub.createRepository("test","test repository",null,true); // hub.getUser("kohsuke").getRepository("test").delete();