-
Notifications
You must be signed in to change notification settings - Fork 760
Convenience method to auth with app installation token && documentation examples #583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -14,16 +14,16 @@ | |
<description>GitHub API for Java</description> | ||
|
||
<scm> | ||
<connection>scm:git:[email protected]/kohsuke/${project.artifactId}.git</connection> | ||
<developerConnection>scm:git:ssh://[email protected]/kohsuke/${project.artifactId}.git</developerConnection> | ||
<connection>scm:git:[email protected]/github-api/${project.artifactId}.git</connection> | ||
<developerConnection>scm:git:ssh://[email protected]/github-api/${project.artifactId}.git</developerConnection> | ||
<url>https://${project.artifactId}.kohsuke.org/</url> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<distributionManagement> | ||
<site> | ||
<id>github-pages</id> | ||
<url>gitsite:[email protected]/kohsuke/${project.artifactId}.git</url> | ||
<url>gitsite:[email protected]/github-api/${project.artifactId}.git</url> | ||
</site> | ||
</distributionManagement> | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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,154 @@ | ||
What is this? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to talk through my thoughts here :
Meh, okay, let's try this. |
||
|
||
This library defines an object oriented representation of the GitHub API. By "object oriented" we mean | ||
there are classes that correspond to the domain model of GitHub (such as <<<GHUser>>> and <<<GHRepository>>>), | ||
operations that act on them as defined as methods (such as <<<GHUser.follow()>>>), and those object references | ||
are used in favor of using string handle (such as <<<GHUser.isMemberOf(GHOrganization)>>> instead of | ||
<<<GHUser.isMemberOf(String)>>>) | ||
|
||
The library supports both github.com and GitHub Enterprise. | ||
|
||
Most of the GitHub APIs are covered, although there are some corners that are still not yet implemented. | ||
|
||
Sample Usage | ||
|
||
+-----+ | ||
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(); | ||
+-----+ | ||
|
||
Authentication | ||
|
||
The library allows connecting to GitHub via several different authentication mechanisms. | ||
|
||
* Programmatically | ||
|
||
To connect via Username and Password (not recommended): | ||
|
||
+-----+ | ||
GitHub github = new GitHubBuilder().withPassword("my_user", "my_passwd").build(); | ||
+-----+ | ||
|
||
To connect via Personal access token: | ||
|
||
+-----+ | ||
// If you don't specify the GitHub user id then the sdk will retrieve it via /user endpoint | ||
GitHub github = new GitHubBuilder().withOAuthToken("my_personal_token").build(); | ||
|
||
// If the token has access to an organization, you can specify it here. | ||
GitHub github = new GitHubBuilder().withOAuthToken("my_personal_token","user_id_OR_org_name").build(); | ||
+-----+ | ||
|
||
To connect via JWT token as a GitHub App: | ||
|
||
+-----+ | ||
GitHub github = new GitHubBuilder().withJwtToken("my_jwt_token").build(); | ||
+-----+ | ||
|
||
To connect via GitHub App installation token on behalf of a user or organization: | ||
|
||
+-----+ | ||
GitHub github = new GitHubBuilder().withAppInstallationToken("my_installation_token").build(); | ||
+-----+ | ||
|
||
* Property file | ||
|
||
This library defines a common convention so that applications using this library will look at a consistent location. | ||
In this convention, the library looks at <<<~/.github>>> property file. The content of the files depends on the way | ||
you want this library to authenticate as shown below: | ||
|
||
|
||
To connect via Username and Password (not recommended): | ||
|
||
+-----+ | ||
login=kohsuke | ||
password=012345678 | ||
+-----+ | ||
|
||
To connect via Personal access token: | ||
|
||
+-----+ | ||
oauth=4d98173f7c075527cb64878561d1fe70 | ||
+-----+ | ||
|
||
To connect via Personal access token as a user or organization: | ||
|
||
+-----+ | ||
login=my_org | ||
oauth=4d98173f7c075527cb64878561d1fe70 | ||
+-----+ | ||
|
||
To connect via JWT token as a GitHub App: | ||
|
||
+-----+ | ||
jwt=my_jwt_token | ||
+-----+ | ||
|
||
Once your <<<~/.github>>> property file is properly configured, you can obtain a <<<GitHub>>> instance using: | ||
|
||
+-----+ | ||
// if you are using the default configuration file | ||
GitHub github = new GitHubBuilder().fromPropertyFile().build(); | ||
|
||
// if you need to use a separate configuration file | ||
GitHub github = new GitHubBuilder().fromPropertyFile("location/my_custom_github.properties").build(); | ||
+-----+ | ||
|
||
* Environmental variables | ||
|
||
This library also allows developers to authenticate GitHub with environmental variables. | ||
|
||
To connect via Username and Password (not recommended): | ||
|
||
+-----+ | ||
export GITHUB_LOGIN=kohsuke | ||
export GITHUB_PASSWORD=012345678 | ||
+-----+ | ||
|
||
To connect via Personal access token: | ||
|
||
+-----+ | ||
export GITHUB_OAUTH=4d98173f7c075527cb64878561d1fe70 | ||
+-----+ | ||
|
||
To connect via Personal access token as a user or organization: | ||
|
||
+-----+ | ||
export GITHUB_LOGIN=my_org | ||
export GITHUB_OAUTH=4d98173f7c075527cb64878561d1fe70 | ||
+-----+ | ||
|
||
To connect via JWT token as a GitHub App: | ||
|
||
+-----+ | ||
export GITHUB_JWT=my_jwt_token | ||
+-----+ | ||
|
||
Once exported, you can obtain a <<<GitHub>>> instance using: | ||
|
||
+-----+ | ||
GitHub github = new GitHubBuilder().fromEnvironment().build(); | ||
+-----+ | ||
|
||
|
||
Pluggable HTTP client | ||
|
||
This library comes with a pluggable connector to use different HTTP client implementations | ||
through <<<HttpConnector>>>. In particular, this means you can use {{{http://square.github.io/okhttp/}OkHttp}}, | ||
so we can make use of it's HTTP response cache. | ||
Making a conditional request against the GitHub API and receiving a 304 response | ||
{{{http://developer.github.com/v3/#conditional-requests}does not count against the rate limit}}. | ||
|
||
The following code shows an example of how to set up persistent cache on the disk: | ||
|
||
+-----+ | ||
Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024); // 10MB cache | ||
GitHub gitHub = GitHubBuilder.fromCredentials() | ||
.withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache)))) | ||
.build(); | ||
+-----+ |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.