Skip to content

Commit 5d2b4bd

Browse files
committed
reworked the connect method
1 parent bd75890 commit 5d2b4bd

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/main/java/org/kohsuke/github/GitHub.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
*/
2424
package org.kohsuke.github;
2525

26+
import org.apache.commons.io.IOUtils;
2627
import org.codehaus.jackson.map.DeserializationConfig.Feature;
2728
import org.codehaus.jackson.map.ObjectMapper;
2829
import org.codehaus.jackson.map.introspect.VisibilityChecker.Std;
2930

31+
import java.io.File;
32+
import java.io.FileInputStream;
33+
import java.io.FileNotFoundException;
3034
import java.io.IOException;
3135
import java.net.URL;
3236
import java.util.HashMap;
3337
import java.util.Map;
38+
import java.util.Properties;
3439

3540
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.ANY;
3641
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
@@ -46,18 +51,37 @@ public class GitHub {
4651

4752
private final Map<String,GHUser> users = new HashMap<String, GHUser>();
4853

54+
private GitHub(String login, String apiToken) {
55+
this.login = login;
56+
this.token = apiToken;
57+
}
58+
59+
/**
60+
* Obtains the credential from "~/.github"
61+
*/
62+
public static GitHub connect() throws IOException {
63+
Properties props = new Properties();
64+
File homeDir = new File(System.getProperty("user.home"));
65+
FileInputStream in = new FileInputStream(new File(homeDir, ".github"));
66+
try {
67+
props.load(in);
68+
} finally {
69+
IOUtils.closeQuietly(in);
70+
}
71+
return new GitHub(props.getProperty("login"),props.getProperty("token"));
72+
}
73+
74+
public static GitHub connect(String login, String apiToken) throws IOException {
75+
return new GitHub(login,apiToken);
76+
}
77+
4978
/**
5079
* Connects to GitHub anonymously.
5180
*
5281
* All operations that requires authentication will fail.
5382
*/
54-
public GitHub() {
55-
this(null,null);
56-
}
57-
58-
public GitHub(String login, String apiToken) {
59-
this.login = login;
60-
this.token = apiToken;
83+
public static GitHub connectAnonymously() {
84+
return new GitHub(null,null);
6185
}
6286

6387
/*package*/ void requireCredential() {

src/site/apt/index.apt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ What is this?
55
Sample Usage
66

77
------------------
8-
GitHub github = new GitHub("kohsuke","myApiToken");
8+
GitHub github = GitHub.connect();
99
GHRepository repo = github.createRepository(
1010
"new-repository","this is my new repository",
1111
"http://www.kohsuke.org/",true/*public*/);
1212
repo.addCollaborators(github.getUser("abayer"),github.getUser("rtyler"));
1313
repo.delete();
1414
------------------
15+
16+
Credential
17+
18+
This library allows the caller to supply the credential as parameters, but it also defines a common convention
19+
so that applications using this library will look at the consistent location. In this convention, the library
20+
looks at "~/.github" property file, which should have the following two values:
21+
22+
------------------
23+
login=kohsuke
24+
token=012345678
25+
------------------

src/test/java/org/kohsuke/AppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class AppTest extends TestCase {
1414
public void testApp() throws IOException {
15-
GitHub hub = new GitHub("kohsuke","9138245daf860415dfc66419177d95da");
15+
GitHub hub = GitHub.connectAnonymously();
1616
// hub.createRepository("test","test repository",null,true);
1717
// hub.getUser("kohsuke").getRepository("test").delete();
1818

0 commit comments

Comments
 (0)