Skip to content

Commit 8a88c14

Browse files
Add convenience method to authenticate with app installation tokens;
Convert existing markdown doc to APT due to formatting features; Replace occurrences of kohsuke user to github-api org where applicable; Fix repositoryIds method on GHAppCreateTokenBuilder; Signed-off-by: PauloMigAlmeida <[email protected]>
1 parent efb87c5 commit 8a88c14

7 files changed

Lines changed: 184 additions & 7 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
<description>GitHub API for Java</description>
1515

1616
<scm>
17-
<connection>scm:git:[email protected]/kohsuke/${project.artifactId}.git</connection>
18-
<developerConnection>scm:git:ssh://[email protected]/kohsuke/${project.artifactId}.git</developerConnection>
17+
<connection>scm:git:[email protected]/github-api/${project.artifactId}.git</connection>
18+
<developerConnection>scm:git:ssh://[email protected]/github-api/${project.artifactId}.git</developerConnection>
1919
<url>https://${project.artifactId}.kohsuke.org/</url>
2020
<tag>HEAD</tag>
2121
</scm>
2222

2323
<distributionManagement>
2424
<site>
2525
<id>github-pages</id>
26-
<url>gitsite:[email protected]/kohsuke/${project.artifactId}.git</url>
26+
<url>gitsite:[email protected]/github-api/${project.artifactId}.git</url>
2727
</site>
2828
</distributionManagement>
2929

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GHAppCreateTokenBuilder {
3535
*
3636
*/
3737
@Preview @Deprecated
38-
public GHAppCreateTokenBuilder repositoryIds(List<Integer> repositoryIds) {
38+
public GHAppCreateTokenBuilder repositoryIds(List<Long> repositoryIds) {
3939
this.builder.with("repository_ids",repositoryIds);
4040
return this;
4141
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ public GitHubBuilder withOAuthToken(String oauthToken, String user) {
180180
this.user = user;
181181
return this;
182182
}
183+
184+
/**
185+
* Configures {@link GitHubBuilder} with Installation Token generated by the GitHub Application
186+
*
187+
* @param appInstallationToken A string containing the GitHub App installation token
188+
* @return the configured Builder from given GitHub App installation token.
189+
* @see GHAppInstallation#createToken(java.util.Map)
190+
*/
191+
public GitHubBuilder withAppInstallationToken(String appInstallationToken){
192+
return withOAuthToken(appInstallationToken, "");
193+
}
194+
183195
public GitHubBuilder withJwtToken(String jwtToken){
184196
this.jwtToken = jwtToken;
185197
return this;

src/site/apt/index.apt

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
+-----+

src/site/site.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
<artifactId>maven-skin</artifactId>
1010
<version>1.2</version>
1111
</skin>
12+
1213

1314
<body>
1415
<menu name="Git Hub API for Java">
1516
<item name="Introduction" href="/index.html"/>
1617
<item name="Download" href="http://mvnrepository.com/artifact/${project.groupId}/${project.artifactId}"/>
17-
<item name="Source code" href="https://github.com/kohsuke/${project.artifactId}"/>
18+
<item name="Source code" href="https://github.com/github-api/${project.artifactId}"/>
1819
<item name="Mailing List" href="https://groups.google.com/forum/#!forum/github-api"/>
1920
</menu>
2021

src/test/java/org/kohsuke/github/GHAppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void createToken() throws IOException {
103103
permissions.put("metadata", GHPermissionType.READ);
104104

105105
GHAppInstallationToken installationToken = installation.createToken(permissions)
106-
.repositoryIds(Arrays.asList(111111111))
106+
.repositoryIds(Arrays.asList((long)111111111))
107107
.create();
108108

109109
assertThat(installationToken.getToken(), is("bogus"));

src/test/java/org/kohsuke/github/GitHubConnectionTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public void testGitHubServerWithoutServer() throws Exception {
4646
GitHub hub = GitHub.connectUsingPassword("kohsuke", "bogus");
4747
assertEquals("https://api.github.com/test", hub.getApiURL("/test").toString());
4848
}
49-
5049
@Test
5150
public void testGitHubBuilderFromEnvironment() throws IOException {
5251

@@ -86,6 +85,17 @@ public void testGitHubBuilderFromCustomEnvironment() throws IOException {
8685
assertEquals("bogusPassword", builder.password);
8786
assertEquals("bogusEndpoint", builder.endpoint);
8887
}
88+
@Test
89+
public void testGithubBuilderWithAppInstallationToken() throws Exception{
90+
GitHubBuilder builder = new GitHubBuilder().withAppInstallationToken("bogus");
91+
assertEquals("bogus", builder.oauthToken);
92+
assertEquals("", builder.user);
93+
94+
// test authorization header is set as in the RFC6749
95+
GitHub github = builder.build();
96+
assertEquals("token bogus",github.encodedAuthorization);
97+
assertEquals("",github.login);
98+
}
8999

90100
@Test
91101
public void testGitHubRateLimit() throws Exception {

0 commit comments

Comments
 (0)