|
| 1 | +What is this? |
| 2 | + |
| 3 | + This library defines an object oriented representation of the GitHub API. By "object oriented" we mean |
| 4 | + there are classes that correspond to the domain model of GitHub (such as <<<GHUser>>> and <<<GHRepository>>>), |
| 5 | + operations that act on them as defined as methods (such as <<<GHUser.follow()>>>), and those object references |
| 6 | + are used in favor of using string handle (such as <<<GHUser.isMemberOf(GHOrganization)>>> instead of |
| 7 | + <<<GHUser.isMemberOf(String)>>>) |
| 8 | + |
| 9 | + The library supports both github.com and GitHub Enterprise. |
| 10 | + |
| 11 | + Most of the GitHub APIs are covered, although there are some corners that are still not yet implemented. |
| 12 | + |
| 13 | +Sample Usage |
| 14 | + |
| 15 | ++-----+ |
| 16 | +GitHub github = GitHub.connect(); |
| 17 | + |
| 18 | +GHRepository repo = github.createRepository( |
| 19 | + "new-repository","this is my new repository", |
| 20 | + "http://www.kohsuke.org/",true/*public*/); |
| 21 | +repo.addCollaborators(github.getUser("abayer"),github.getUser("rtyler")); |
| 22 | +repo.delete(); |
| 23 | ++-----+ |
| 24 | + |
| 25 | +Authentication |
| 26 | + |
| 27 | + The library allows connecting to GitHub via several different authentication mechanisms. |
| 28 | + |
| 29 | +* Programmatically |
| 30 | + |
| 31 | + To connect via Username and Password (not recommended): |
| 32 | + |
| 33 | ++-----+ |
| 34 | +GitHub github = new GitHubBuilder().withPassword("my_user", "my_passwd").build(); |
| 35 | ++-----+ |
| 36 | + |
| 37 | + To connect via Personal access token: |
| 38 | + |
| 39 | ++-----+ |
| 40 | +// If you don't specify the GitHub user id then the sdk will retrieve it via /user endpoint |
| 41 | +GitHub github = new GitHubBuilder().withOAuthToken("my_personal_token").build(); |
| 42 | + |
| 43 | +// If the token has access to an organization, you can specify it here. |
| 44 | +GitHub github = new GitHubBuilder().withOAuthToken("my_personal_token","user_id_OR_org_name").build(); |
| 45 | ++-----+ |
| 46 | + |
| 47 | + To connect via JWT token as a GitHub App: |
| 48 | + |
| 49 | ++-----+ |
| 50 | +GitHub github = new GitHubBuilder().withJwtToken("my_jwt_token").build(); |
| 51 | ++-----+ |
| 52 | + |
| 53 | + To connect via GitHub App installation token on behalf of a user or organization: |
| 54 | + |
| 55 | ++-----+ |
| 56 | +GitHub github = new GitHubBuilder().withAppInstallationToken("my_installation_token").build(); |
| 57 | ++-----+ |
| 58 | + |
| 59 | +* Property file |
| 60 | + |
| 61 | + This library defines a common convention so that applications using this library will look at a consistent location. |
| 62 | + In this convention, the library looks at <<<~/.github>>> property file. The content of the files depends on the way |
| 63 | + you want this library to authenticate as shown below: |
| 64 | + |
| 65 | + |
| 66 | + To connect via Username and Password (not recommended): |
| 67 | + |
| 68 | ++-----+ |
| 69 | +login=kohsuke |
| 70 | +password=012345678 |
| 71 | ++-----+ |
| 72 | + |
| 73 | + To connect via Personal access token: |
| 74 | + |
| 75 | ++-----+ |
| 76 | +oauth=4d98173f7c075527cb64878561d1fe70 |
| 77 | ++-----+ |
| 78 | + |
| 79 | + To connect via Personal access token as a user or organization: |
| 80 | + |
| 81 | ++-----+ |
| 82 | +login=my_org |
| 83 | +oauth=4d98173f7c075527cb64878561d1fe70 |
| 84 | ++-----+ |
| 85 | + |
| 86 | + To connect via JWT token as a GitHub App: |
| 87 | + |
| 88 | ++-----+ |
| 89 | +jwt=my_jwt_token |
| 90 | ++-----+ |
| 91 | + |
| 92 | + Once your <<<~/.github>>> property file is properly configured, you can obtain a <<<GitHub>>> instance using: |
| 93 | + |
| 94 | ++-----+ |
| 95 | +// if you are using the default configuration file |
| 96 | +GitHub github = new GitHubBuilder().fromPropertyFile().build(); |
| 97 | + |
| 98 | +// if you need to use a separate configuration file |
| 99 | +GitHub github = new GitHubBuilder().fromPropertyFile("location/my_custom_github.properties").build(); |
| 100 | ++-----+ |
| 101 | + |
| 102 | +* Environmental variables |
| 103 | + |
| 104 | + This library also allows developers to authenticate GitHub with environmental variables. |
| 105 | + |
| 106 | + To connect via Username and Password (not recommended): |
| 107 | + |
| 108 | ++-----+ |
| 109 | +export GITHUB_LOGIN=kohsuke |
| 110 | +export GITHUB_PASSWORD=012345678 |
| 111 | ++-----+ |
| 112 | + |
| 113 | + To connect via Personal access token: |
| 114 | + |
| 115 | ++-----+ |
| 116 | +export GITHUB_OAUTH=4d98173f7c075527cb64878561d1fe70 |
| 117 | ++-----+ |
| 118 | + |
| 119 | + To connect via Personal access token as a user or organization: |
| 120 | + |
| 121 | ++-----+ |
| 122 | +export GITHUB_LOGIN=my_org |
| 123 | +export GITHUB_OAUTH=4d98173f7c075527cb64878561d1fe70 |
| 124 | ++-----+ |
| 125 | + |
| 126 | + To connect via JWT token as a GitHub App: |
| 127 | + |
| 128 | ++-----+ |
| 129 | +export GITHUB_JWT=my_jwt_token |
| 130 | ++-----+ |
| 131 | + |
| 132 | + Once exported, you can obtain a <<<GitHub>>> instance using: |
| 133 | + |
| 134 | ++-----+ |
| 135 | +GitHub github = new GitHubBuilder().fromEnvironment().build(); |
| 136 | ++-----+ |
| 137 | + |
| 138 | + |
| 139 | +Pluggable HTTP client |
| 140 | + |
| 141 | + This library comes with a pluggable connector to use different HTTP client implementations |
| 142 | + through <<<HttpConnector>>>. In particular, this means you can use {{{http://square.github.io/okhttp/}OkHttp}}, |
| 143 | + so we can make use of it's HTTP response cache. |
| 144 | + Making a conditional request against the GitHub API and receiving a 304 response |
| 145 | + {{{http://developer.github.com/v3/#conditional-requests}does not count against the rate limit}}. |
| 146 | + |
| 147 | + The following code shows an example of how to set up persistent cache on the disk: |
| 148 | + |
| 149 | ++-----+ |
| 150 | +Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024); // 10MB cache |
| 151 | +GitHub gitHub = GitHubBuilder.fromCredentials() |
| 152 | + .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache)))) |
| 153 | + .build(); |
| 154 | ++-----+ |
0 commit comments