Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f64994

Browse files
committed
Make Github.connect() fail if no credentials are setup
With the previous change if no credentials were defined Github.connect() would fall back on an anonymous connection. This commit changes the behaviour back to what it was before so that if there are no credentials defined in the ~/.github file and no credentials defined in the environment an IOException is thrown to alert the method caller. The caller can call Github.connectAnonymously() if that scenario is allowed. This should handle most cases unless callers are depending on the FileNotFoundException being specifically thrown instead of an IOException.
1 parent 4d6c5c1 commit 0f64994

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public GitHubBuilder() {
3030
*
3131
* If no user is specified it means there is no configuration present so check the environment instead.
3232
*
33-
* the system properties for the login, password or oauth environment variables.
33+
* If there is still no user it means there are no credentials defined and throw an IOException.
3434
*
3535
* @return the configured Builder from credentials defined on the system or in the environment.
3636
*
37-
* @throws IOException
37+
* @throws IOException If there are no credentials defined in the ~/.github properties file or the process environment.
3838
*/
3939
public static GitHubBuilder fromCredentials() throws IOException {
4040

@@ -44,11 +44,25 @@ public static GitHubBuilder fromCredentials() throws IOException {
4444

4545
if (builder.user != null)
4646
return builder;
47-
else
48-
return fromEnvironment();
47+
else {
48+
49+
// this is the case where the ~/.github file exists but has no content.
50+
51+
builder = fromEnvironment();
52+
53+
if (builder.user != null)
54+
return builder;
55+
else
56+
throw new IOException("Failed to resolve credentials from ~/.github or the environment.");
57+
}
4958

5059
} catch (FileNotFoundException e) {
51-
return fromEnvironment();
60+
builder = fromEnvironment();
61+
62+
if (builder.user != null)
63+
return builder;
64+
else
65+
throw new IOException("Failed to resolve credentials from ~/.github or the environment.", e);
5266
}
5367

5468
}

0 commit comments

Comments
 (0)