Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHEventInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public GHOrganization getOrganization() throws IOException {
* if payload cannot be parsed
*/
public <T extends GHEventPayload> T getPayload(Class<T> type) throws IOException {
T v = GitHubClient.getMappingObjectReader().readValue(payload.traverse(), type);
T v = GitHubClient.getMappingObjectReader(root).readValue(payload.traverse(), type);
v.wrapUp(root);
return v;
}
Expand Down
62 changes: 22 additions & 40 deletions src/main/java/org/kohsuke/github/GHGist.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

Expand All @@ -19,8 +20,9 @@
* @see <a href="https://developer.github.com/v3/gists/">documentation</a>
*/
public class GHGist extends GHObject {
/* package almost final */ GHUser owner;
/* package almost final */ GitHub root;

final GHUser owner;
final GitHub root;

private String forks_url, commits_url, id, git_pull_url, git_push_url, html_url;

Expand All @@ -33,7 +35,19 @@ public class GHGist extends GHObject {

private String comments_url;

private Map<String, GHGistFile> files = new HashMap<String, GHGistFile>();
private final Map<String, GHGistFile> files;

@JsonCreator
private GHGist(@JacksonInject GitHub root,
@JsonProperty("owner") GHUser owner,
@JsonProperty("files") Map<String, GHGistFile> files) {
this.root = root;
for (Entry<String, GHGistFile> e : files.entrySet()) {
e.getValue().fileName = e.getKey();
}
this.files = Collections.unmodifiableMap(files);
this.owner = root.getUser(owner);
}

/**
* Gets owner.
Expand All @@ -43,7 +57,7 @@ public class GHGist extends GHObject {
* the io exception
*/
public GHUser getOwner() throws IOException {
return root.intern(owner);
return owner;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner is alway interned during construction now.

}

/**
Expand Down Expand Up @@ -139,31 +153,7 @@ public GHGistFile getFile(String name) {
* @return the files
*/
public Map<String, GHGistFile> getFiles() {
return Collections.unmodifiableMap(files);
}

GHGist wrapUp(GHUser owner) {
this.owner = owner;
this.root = owner.root;
wrapUp();
return this;
}

/**
* Used when caller obtains {@link GHGist} without knowing its owner. A partially constructed owner object is
* interned.
*/
GHGist wrapUp(GitHub root) {
this.owner = root.getUser(owner);
this.root = root;
wrapUp();
return this;
}

private void wrapUp() {
for (Entry<String, GHGistFile> e : files.entrySet()) {
e.getValue().fileName = e.getKey();
}
Comment on lines -145 to -166
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actions that were being done after instantiation are now done in the constructor, allowing for those values to be final.

return files;
}

String getApiTailUrl(String tail) {
Expand Down Expand Up @@ -213,7 +203,7 @@ public boolean isStarred() throws IOException {
* the io exception
*/
public GHGist fork() throws IOException {
return root.createRequest().method("POST").withUrlPath(getApiTailUrl("forks")).fetch(GHGist.class).wrapUp(root);
return root.createRequest().method("POST").withUrlPath(getApiTailUrl("forks")).fetch(GHGist.class);
}

/**
Expand All @@ -222,9 +212,7 @@ public GHGist fork() throws IOException {
* @return the paged iterable
*/
public PagedIterable<GHGist> listForks() {
return root.createRequest()
.withUrlPath(getApiTailUrl("forks"))
.toIterable(GHGist[].class, item -> item.wrapUp(root));
return root.createRequest().withUrlPath(getApiTailUrl("forks")).toIterable(GHGist[].class, null);
}

/**
Expand Down Expand Up @@ -263,10 +251,4 @@ public boolean equals(Object o) {
public int hashCode() {
return id.hashCode();
}

GHGist wrap(GHUser owner) {
this.owner = owner;
this.root = owner.root;
return this;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHGistBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public GHGistBuilder file(String fileName, String content) {
*/
public GHGist create() throws IOException {
req.with("files", files);
return req.withUrlPath("/gists").fetch(GHGist.class).wrapUp(root);
return req.withUrlPath("/gists").fetch(GHGist.class);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHGistUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ public GHGistUpdater description(String desc) {
*/
public GHGist update() throws IOException {
builder.with("files", files);
return builder.method("PATCH").withUrlPath(base.getApiTailUrl("")).fetch(GHGist.class).wrap(base.owner);
return builder.method("PATCH").withUrlPath(base.getApiTailUrl("")).fetch(GHGist.class);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public PagedIterable<GHEventInfo> listEvents() throws IOException {
public PagedIterable<GHGist> listGists() throws IOException {
return root.createRequest()
.withUrlPath(String.format("/users/%s/gists", login))
.toIterable(GHGist[].class, item -> item.wrapUp(this));
.toIterable(GHGist[].class, null);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ public List<GHEventInfo> getEvents() throws IOException {
* the io exception
*/
public GHGist getGist(String id) throws IOException {
return createRequest().withUrlPath("/gists/" + id).fetch(GHGist.class).wrapUp(this);
return createRequest().withUrlPath("/gists/" + id).fetch(GHGist.class);
}

/**
Expand Down Expand Up @@ -772,7 +772,7 @@ public GHGistBuilder createGist() {
* the io exception
*/
public <T extends GHEventPayload> T parseEventPayload(Reader r, Class<T> type) throws IOException {
T t = GitHubClient.getMappingObjectReader().forType(type).readValue(r);
T t = GitHubClient.getMappingObjectReader(this).forType(type).readValue(r);
t.wrapUp(this);
return t;
}
Expand Down Expand Up @@ -1184,7 +1184,7 @@ GitHubClient getClient() {

@Nonnull
Requester createRequest() {
return new Requester(client);
return new Requester(client).injectMappingValue(this);
}

GHUser intern(GHUser user) throws IOException {
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/org/kohsuke/github/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,11 +706,16 @@ static ObjectWriter getMappingObjectWriter() {
/**
* Helper for {@link #getMappingObjectReader(GitHubResponse.ResponseInfo)}
*
* @param root
* the root GitHub object for this reader
*
* @return an {@link ObjectReader} instance that can be further configured.
*/
@Nonnull
static ObjectReader getMappingObjectReader() {
return getMappingObjectReader(null);
static ObjectReader getMappingObjectReader(@Nonnull GitHub root) {
ObjectReader reader = getMappingObjectReader((GitHubResponse.ResponseInfo) null);
((InjectableValues.Std) reader.getInjectableValues()).addValue(GitHub.class, root);
return reader;
}

/**
Expand All @@ -724,13 +729,21 @@ static ObjectReader getMappingObjectReader() {
*
* @param responseInfo
* the {@link GitHubResponse.ResponseInfo} to inject for this reader.
*
* @return an {@link ObjectReader} instance that can be further configured.
*/
@Nonnull
static ObjectReader getMappingObjectReader(@CheckForNull GitHubResponse.ResponseInfo responseInfo) {
InjectableValues.Std inject = new InjectableValues.Std();
inject.addValue(GitHubResponse.ResponseInfo.class, responseInfo);
Map<String, Object> injected = new HashMap<>();

// Required or many things break
injected.put(GitHubResponse.ResponseInfo.class.getName(), null);
injected.put(GitHub.class.getName(), null);

return MAPPER.reader(inject);
if (responseInfo != null) {
injected.put(GitHubResponse.ResponseInfo.class.getName(), responseInfo);
injected.putAll(responseInfo.request().injectedMappingValues());
}
return MAPPER.reader(new InjectableValues.Std(injected));
}
}
60 changes: 57 additions & 3 deletions src/main/java/org/kohsuke/github/GitHubRequest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
Expand Down Expand Up @@ -40,6 +41,7 @@ class GitHubRequest {
private static final List<String> METHODS_WITHOUT_BODY = asList("GET", "DELETE");
private final List<Entry> args;
private final Map<String, String> headers;
private final Map<String, Object> injectedMappingValues;
private final String apiUrl;
private final String urlPath;
private final String method;
Expand All @@ -50,13 +52,15 @@ class GitHubRequest {

private GitHubRequest(@Nonnull List<Entry> args,
@Nonnull Map<String, String> headers,
@Nonnull Map<String, Object> injectedMappingValues,
@Nonnull String apiUrl,
@Nonnull String urlPath,
@Nonnull String method,
@CheckForNull InputStream body,
boolean forceBody) throws MalformedURLException {
this.args = Collections.unmodifiableList(new ArrayList<>(args));
this.headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers));
this.injectedMappingValues = Collections.unmodifiableMap(new LinkedHashMap<>(injectedMappingValues));
this.apiUrl = apiUrl;
this.urlPath = urlPath;
this.method = method;
Expand Down Expand Up @@ -136,6 +140,16 @@ public Map<String, String> headers() {
return headers;
}

/**
* The headers for this request.
*
* @return the {@link Map} of headers
*/
@Nonnull
public Map<String, Object> injectedMappingValues() {
return injectedMappingValues;
}

/**
* The base GitHub API URL for this request represented as a {@link String}
*
Expand Down Expand Up @@ -203,7 +217,7 @@ public boolean inBody() {
* @return a {@link Builder} based on this request.
*/
public Builder<?> toBuilder() {
return new Builder<>(args, headers, apiUrl, urlPath, method, body, forceBody);
return new Builder<>(args, headers, injectedMappingValues, apiUrl, urlPath, method, body, forceBody);
}

private String buildTailApiUrl() {
Expand Down Expand Up @@ -248,6 +262,12 @@ static class Builder<B extends Builder<B>> {
@Nonnull
private final Map<String, String> headers;

/**
* Injected local data map
*/
@Nonnull
private final Map<String, Object> injectedMappingValues;

/**
* The base GitHub API for this request.
*/
Expand All @@ -268,18 +288,27 @@ static class Builder<B extends Builder<B>> {
* Create a new {@link GitHubRequest.Builder}
*/
protected Builder() {
this(new ArrayList<>(), new LinkedHashMap<>(), GitHubClient.GITHUB_URL, "/", "GET", null, false);
this(new ArrayList<>(),
new LinkedHashMap<>(),
new LinkedHashMap<>(),
GitHubClient.GITHUB_URL,
"/",
"GET",
null,
false);
}

private Builder(@Nonnull List<Entry> args,
@Nonnull Map<String, String> headers,
@Nonnull Map<String, Object> injectedMappingValues,
@Nonnull String apiUrl,
@Nonnull String urlPath,
@Nonnull String method,
@CheckForNull @WillClose InputStream body,
boolean forceBody) {
this.args = new ArrayList<>(args);
this.headers = new LinkedHashMap<>(headers);
this.injectedMappingValues = new LinkedHashMap<>(injectedMappingValues);
this.apiUrl = apiUrl;
this.urlPath = urlPath;
this.method = method;
Expand All @@ -295,7 +324,7 @@ private Builder(@Nonnull List<Entry> args,
* if the GitHub API URL cannot be constructed
*/
public GitHubRequest build() throws MalformedURLException {
return new GitHubRequest(args, headers, apiUrl, urlPath, method, body, forceBody);
return new GitHubRequest(args, headers, injectedMappingValues, apiUrl, urlPath, method, body, forceBody);
}

/**
Expand Down Expand Up @@ -338,6 +367,31 @@ public B withHeader(String name, String value) {
return (B) this;
}

/**
* Object to inject into binding.
*
* @param value
* the value
* @return the request builder
*/
public B injectMappingValue(@NonNull Object value) {
return injectMappingValue(value.getClass().getName(), value);
}

/**
* Object to inject into binding.
*
* @param name
* the name
* @param value
* the value
* @return the request builder
*/
public B injectMappingValue(@NonNull String name, Object value) {
this.injectedMappingValues.put(name, value);
return (B) this;
}

public B withPreview(String name) {
return withHeader("Accept", name);
}
Expand Down