Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 66fce79

Browse filesBrowse files
committed
Add ability to inject other local bindings
1 parent 46a141d commit 66fce79
Copy full SHA for 66fce79

File tree

Expand file treeCollapse file tree

4 files changed

+64
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+64
-11
lines changed

‎src/main/java/org/kohsuke/github/GHEventInfo.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHEventInfo.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public GHOrganization getOrganization() throws IOException {
142142
* if payload cannot be parsed
143143
*/
144144
public <T extends GHEventPayload> T getPayload(Class<T> type) throws IOException {
145-
T v = GitHubClient.getMappingObjectReader().readValue(payload.traverse(), type);
145+
T v = GitHubClient.getMappingObjectReader(root).readValue(payload.traverse(), type);
146146
v.wrapUp(root);
147147
return v;
148148
}

‎src/main/java/org/kohsuke/github/GitHub.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHub.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ public GHGistBuilder createGist() {
772772
* the io exception
773773
*/
774774
public <T extends GHEventPayload> T parseEventPayload(Reader r, Class<T> type) throws IOException {
775-
T t = GitHubClient.getMappingObjectReader().forType(type).readValue(r);
775+
T t = GitHubClient.getMappingObjectReader(this).forType(type).readValue(r);
776776
t.wrapUp(this);
777777
return t;
778778
}
@@ -1184,7 +1184,7 @@ GitHubClient getClient() {
11841184

11851185
@Nonnull
11861186
Requester createRequest() {
1187-
return new Requester(client);
1187+
return new Requester(client).inject(this);
11881188
}
11891189

11901190
GHUser intern(GHUser user) throws IOException {

‎src/main/java/org/kohsuke/github/GitHubClient.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHubClient.java
+18-5Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,16 @@ static ObjectWriter getMappingObjectWriter() {
706706
/**
707707
* Helper for {@link #getMappingObjectReader(GitHubResponse.ResponseInfo)}
708708
*
709+
* @param root
710+
* the root GitHub object for this reader
711+
*
709712
* @return an {@link ObjectReader} instance that can be further configured.
710713
*/
711714
@Nonnull
712-
static ObjectReader getMappingObjectReader() {
713-
return getMappingObjectReader(null);
715+
static ObjectReader getMappingObjectReader(@Nonnull GitHub root) {
716+
ObjectReader reader = getMappingObjectReader((GitHubResponse.ResponseInfo) null);
717+
((InjectableValues.Std) reader.getInjectableValues()).addValue(GitHub.class, root);
718+
return reader;
714719
}
715720

716721
/**
@@ -724,13 +729,21 @@ static ObjectReader getMappingObjectReader() {
724729
*
725730
* @param responseInfo
726731
* the {@link GitHubResponse.ResponseInfo} to inject for this reader.
732+
*
727733
* @return an {@link ObjectReader} instance that can be further configured.
728734
*/
729735
@Nonnull
730736
static ObjectReader getMappingObjectReader(@CheckForNull GitHubResponse.ResponseInfo responseInfo) {
731-
InjectableValues.Std inject = new InjectableValues.Std();
732-
inject.addValue(GitHubResponse.ResponseInfo.class, responseInfo);
737+
Map<String, Object> injected = new HashMap<>();
738+
739+
// Required or many things break
740+
injected.put(GitHubResponse.ResponseInfo.class.getName(), null);
741+
injected.put(GitHub.class.getName(), null);
733742

734-
return MAPPER.reader(inject);
743+
if (responseInfo != null) {
744+
injected.put(GitHubResponse.ResponseInfo.class.getName(), responseInfo);
745+
injected.putAll(responseInfo.request().injected());
746+
}
747+
return MAPPER.reader(new InjectableValues.Std(injected));
735748
}
736749
}

‎src/main/java/org/kohsuke/github/GitHubRequest.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHubRequest.java
+43-3Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class GitHubRequest {
4040
private static final List<String> METHODS_WITHOUT_BODY = asList("GET", "DELETE");
4141
private final List<Entry> args;
4242
private final Map<String, String> headers;
43+
private final Map<String, Object> injected;
4344
private final String apiUrl;
4445
private final String urlPath;
4546
private final String method;
@@ -50,13 +51,15 @@ class GitHubRequest {
5051

5152
private GitHubRequest(@Nonnull List<Entry> args,
5253
@Nonnull Map<String, String> headers,
54+
@Nonnull Map<String, Object> injected,
5355
@Nonnull String apiUrl,
5456
@Nonnull String urlPath,
5557
@Nonnull String method,
5658
@CheckForNull InputStream body,
5759
boolean forceBody) throws MalformedURLException {
5860
this.args = Collections.unmodifiableList(new ArrayList<>(args));
5961
this.headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers));
62+
this.injected = Collections.unmodifiableMap(new LinkedHashMap<>(injected));
6063
this.apiUrl = apiUrl;
6164
this.urlPath = urlPath;
6265
this.method = method;
@@ -136,6 +139,16 @@ public Map<String, String> headers() {
136139
return headers;
137140
}
138141

142+
/**
143+
* The headers for this request.
144+
*
145+
* @return the {@link Map} of headers
146+
*/
147+
@Nonnull
148+
public Map<String, Object> injected() {
149+
return injected;
150+
}
151+
139152
/**
140153
* The base GitHub API URL for this request represented as a {@link String}
141154
*
@@ -203,7 +216,7 @@ public boolean inBody() {
203216
* @return a {@link Builder} based on this request.
204217
*/
205218
public Builder<?> toBuilder() {
206-
return new Builder<>(args, headers, apiUrl, urlPath, method, body, forceBody);
219+
return new Builder<>(args, headers, injected, apiUrl, urlPath, method, body, forceBody);
207220
}
208221

209222
private String buildTailApiUrl() {
@@ -248,6 +261,12 @@ static class Builder<B extends Builder<B>> {
248261
@Nonnull
249262
private final Map<String, String> headers;
250263

264+
/**
265+
* Injected local data map
266+
*/
267+
@Nonnull
268+
private final Map<String, Object> injected;
269+
251270
/**
252271
* The base GitHub API for this request.
253272
*/
@@ -268,18 +287,27 @@ static class Builder<B extends Builder<B>> {
268287
* Create a new {@link GitHubRequest.Builder}
269288
*/
270289
protected Builder() {
271-
this(new ArrayList<>(), new LinkedHashMap<>(), GitHubClient.GITHUB_URL, "/", "GET", null, false);
290+
this(new ArrayList<>(),
291+
new LinkedHashMap<>(),
292+
new LinkedHashMap<>(),
293+
GitHubClient.GITHUB_URL,
294+
"/",
295+
"GET",
296+
null,
297+
false);
272298
}
273299

274300
private Builder(@Nonnull List<Entry> args,
275301
@Nonnull Map<String, String> headers,
302+
@Nonnull Map<String, Object> injected,
276303
@Nonnull String apiUrl,
277304
@Nonnull String urlPath,
278305
@Nonnull String method,
279306
@CheckForNull @WillClose InputStream body,
280307
boolean forceBody) {
281308
this.args = new ArrayList<>(args);
282309
this.headers = new LinkedHashMap<>(headers);
310+
this.injected = new LinkedHashMap<>(injected);
283311
this.apiUrl = apiUrl;
284312
this.urlPath = urlPath;
285313
this.method = method;
@@ -295,7 +323,7 @@ private Builder(@Nonnull List<Entry> args,
295323
* if the GitHub API URL cannot be constructed
296324
*/
297325
public GitHubRequest build() throws MalformedURLException {
298-
return new GitHubRequest(args, headers, apiUrl, urlPath, method, body, forceBody);
326+
return new GitHubRequest(args, headers, injected, apiUrl, urlPath, method, body, forceBody);
299327
}
300328

301329
/**
@@ -338,6 +366,18 @@ public B withHeader(String name, String value) {
338366
return (B) this;
339367
}
340368

369+
/**
370+
* Object to inject into binding.
371+
*
372+
* @param value
373+
* the value
374+
* @return the request builder
375+
*/
376+
public B inject(Object value) {
377+
this.injected.put(value.getClass().getName(), value);
378+
return (B) this;
379+
}
380+
341381
public B withPreview(String name) {
342382
return withHeader("Accept", name);
343383
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.