-
Notifications
You must be signed in to change notification settings - Fork 736
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
Expose Headers #339
Expose Headers #339
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.kohsuke.github.exception; | ||
|
||
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; | ||
} | ||
|
||
public GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) { | ||
this.responseHeaderFields = urlConnection.getHeaderFields(); | ||
return this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.kohsuke.github.exception; | ||
|
||
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>> responceHeaderFields; | ||
|
||
public GHIOException() { | ||
} | ||
|
||
public GHIOException(String message) { | ||
super(message); | ||
} | ||
|
||
public GHIOException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public GHIOException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
@CheckForNull | ||
public Map<String, List<String>> getResponceHeaderFields() { | ||
return responceHeaderFields; | ||
} | ||
|
||
public GHIOException withResponceHeaderFields(HttpURLConnection urlConnection) { | ||
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. response 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. @KostyaSha this typo was not addressed. 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 fixed it before the merge 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 fixed it before the merge |
||
this.responceHeaderFields = urlConnection.getHeaderFields(); | ||
return this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.kohsuke.github; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.kohsuke.github.exception.GHFileNotFoundException; | ||
|
||
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"); | ||
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.
|
||
|
||
// 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)); | ||
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. there is ExpectedExceptionRule for that purposes :) 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. But that requires it to be rethrown. |
||
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") | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or maybe even
GHObject
shouldextend GHResponse