-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from ohtake/connector
Allow to use custom HttpConnector when only OAuth token is given
- Loading branch information
Showing
2 changed files
with
82 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.kohsuke.github; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
/** | ||
* @since 1.59 | ||
*/ | ||
public class GitHubBuilder { | ||
private String endpoint = GitHub.GITHUB_URL; | ||
private String user; | ||
private String password; | ||
private String oauthToken; | ||
private HttpConnector connector; | ||
|
||
public GitHubBuilder() { | ||
} | ||
|
||
public static GitHubBuilder fromPropertyFile() throws IOException { | ||
File homeDir = new File(System.getProperty("user.home")); | ||
File propertyFile = new File(homeDir, ".github"); | ||
return fromPropertyFile(propertyFile.getPath()); | ||
} | ||
public static GitHubBuilder fromPropertyFile(String propertyFileName) throws IOException { | ||
Properties props = new Properties(); | ||
FileInputStream in = null; | ||
try { | ||
in = new FileInputStream(propertyFileName); | ||
props.load(in); | ||
} finally { | ||
IOUtils.closeQuietly(in); | ||
} | ||
GitHubBuilder self = new GitHubBuilder(); | ||
self.withOAuthToken(props.getProperty("oauth"), props.getProperty("login")); | ||
self.withPassword(props.getProperty("login"), props.getProperty("password")); | ||
return self; | ||
} | ||
|
||
public GitHubBuilder withEndpoint(String endpoint) { | ||
this.endpoint = endpoint; | ||
return this; | ||
} | ||
public GitHubBuilder withPassword(String user, String password) { | ||
this.user = user; | ||
this.password = password; | ||
return this; | ||
} | ||
public GitHubBuilder withOAuthToken(String oauthToken) { | ||
return withOAuthToken(oauthToken, null); | ||
} | ||
public GitHubBuilder withOAuthToken(String oauthToken, String user) { | ||
this.oauthToken = oauthToken; | ||
this.user = user; | ||
return this; | ||
} | ||
public GitHubBuilder withConnector(HttpConnector connector) { | ||
this.connector = connector; | ||
return this; | ||
} | ||
|
||
public GitHub build() throws IOException { | ||
return new GitHub(endpoint, user, oauthToken, password, connector); | ||
} | ||
} |