-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
201 additions
and
8 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
34 changes: 34 additions & 0 deletions
34
src/main/java/org/kohsuke/github/GHFileNotFoundException.java
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,34 @@ | ||
package org.kohsuke.github; | ||
|
||
import javax.annotation.CheckForNull; | ||
import java.io.FileNotFoundException; | ||
import java.net.HttpURLConnection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Request/responce contains useful metadata. | ||
* Custom exception allows store info for next diagnostics. | ||
* | ||
* @author Kanstantsin Shautsou | ||
*/ | ||
public class GHFileNotFoundException extends FileNotFoundException { | ||
protected Map<String, List<String>> responseHeaderFields; | ||
|
||
public GHFileNotFoundException() { | ||
} | ||
|
||
public GHFileNotFoundException(String s) { | ||
super(s); | ||
} | ||
|
||
@CheckForNull | ||
public Map<String, List<String>> getResponseHeaderFields() { | ||
return responseHeaderFields; | ||
} | ||
|
||
GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) { | ||
this.responseHeaderFields = urlConnection.getHeaderFields(); | ||
return this; | ||
} | ||
} |
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,34 @@ | ||
package org.kohsuke.github; | ||
|
||
import javax.annotation.CheckForNull; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Request/responce contains useful metadata. | ||
* Custom exception allows store info for next diagnostics. | ||
* | ||
* @author Kanstantsin Shautsou | ||
*/ | ||
public class GHIOException extends IOException { | ||
protected Map<String, List<String>> responseHeaderFields; | ||
|
||
public GHIOException() { | ||
} | ||
|
||
public GHIOException(String message) { | ||
super(message); | ||
} | ||
|
||
@CheckForNull | ||
public Map<String, List<String>> getResponseHeaderFields() { | ||
return responseHeaderFields; | ||
} | ||
|
||
GHIOException withResponseHeaderFields(HttpURLConnection urlConnection) { | ||
this.responseHeaderFields = urlConnection.getHeaderFields(); | ||
return this; | ||
} | ||
} |
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
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,78 @@ | ||
package org.kohsuke.github; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static java.util.Collections.singletonMap; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.hasKey; | ||
import static org.hamcrest.Matchers.hasValue; | ||
import static org.hamcrest.core.IsInstanceOf.instanceOf; | ||
import static org.junit.Assert.assertThat; | ||
|
||
|
||
/** | ||
* @author Kanstantsin Shautsou | ||
*/ | ||
public class GHHookTest { | ||
|
||
@Ignore | ||
@Test | ||
public void exposeResponceHeaders() throws Exception { | ||
String user1Login = "KostyaSha-auto"; | ||
String user1Pass = "secret"; | ||
|
||
String clientId = "90140219451"; | ||
String clientSecret = "1451245425"; | ||
|
||
String orgRepo = "KostyaSha-org/test"; | ||
|
||
// some login based user that has access to application | ||
final GitHub gitHub = GitHub.connectUsingPassword(user1Login, user1Pass); | ||
gitHub.getMyself(); | ||
|
||
// we request read | ||
final List<String> scopes = Arrays.asList("repo", "read:org", "user:email", "read:repo_hook"); | ||
|
||
// application creates token with scopes | ||
final GHAuthorization auth = gitHub.createOrGetAuth(clientId, clientSecret, scopes, "", ""); | ||
String token = auth.getToken(); | ||
if (StringUtils.isEmpty(token)) { | ||
gitHub.deleteAuth(auth.getId()); | ||
token = gitHub.createOrGetAuth(clientId, clientSecret, scopes, "", "").getToken(); | ||
} | ||
|
||
/// now create connection using token | ||
final GitHub gitHub2 = GitHub.connectUsingOAuth(token); | ||
// some repo in organisation | ||
final GHRepository repository = gitHub2.getRepository(orgRepo); | ||
|
||
// doesn't fail because we have read access | ||
final List<GHHook> hooks = repository.getHooks(); | ||
|
||
try { | ||
// fails because application isn't approved in organisation and you can find it only after doing real call | ||
final GHHook hook = repository.createHook( | ||
"my-hook", | ||
singletonMap("url", "http://localhost"), | ||
singletonList(GHEvent.PUSH), | ||
true | ||
); | ||
} catch (IOException ex) { | ||
assertThat(ex, instanceOf(GHFileNotFoundException.class)); | ||
final GHFileNotFoundException ghFileNotFoundException = (GHFileNotFoundException) ex; | ||
final Map<String, List<String>> responseHeaderFields = ghFileNotFoundException.getResponseHeaderFields(); | ||
assertThat(responseHeaderFields, hasKey("X-Accepted-OAuth-Scopes")); | ||
assertThat(responseHeaderFields.get("X-Accepted-OAuth-Scopes"), | ||
hasItem("admin:repo_hook, public_repo, repo, write:repo_hook") | ||
); | ||
} | ||
} | ||
} |