Skip to content
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

Move array population to single iterator code path #623

Merged
merged 8 commits into from
Dec 17, 2019
22 changes: 13 additions & 9 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ GHApp wrapUp(GitHub root) {
@Preview
@Deprecated
public PagedIterable<GHAppInstallation> listInstallations() {
return root.retrieve()
return root.createRequest()
.withPreview(MACHINE_MAN)
.asPagedIterable("/app/installations", GHAppInstallation[].class, item -> item.wrapUp(root));
}
Expand All @@ -198,9 +198,10 @@ public PagedIterable<GHAppInstallation> listInstallations() {
@Preview
@Deprecated
public GHAppInstallation getInstallationById(long id) throws IOException {
return root.retrieve()
return root.createRequest()
.withPreview(MACHINE_MAN)
.to(String.format("/app/installations/%d", id), GHAppInstallation.class)
.withUrlPath(String.format("/app/installations/%d", id))
.fetch(GHAppInstallation.class)
.wrapUp(root);
}

Expand All @@ -220,9 +221,10 @@ public GHAppInstallation getInstallationById(long id) throws IOException {
@Preview
@Deprecated
public GHAppInstallation getInstallationByOrganization(String name) throws IOException {
return root.retrieve()
return root.createRequest()
.withPreview(MACHINE_MAN)
.to(String.format("/orgs/%s/installation", name), GHAppInstallation.class)
.withUrlPath(String.format("/orgs/%s/installation", name))
.fetch(GHAppInstallation.class)
.wrapUp(root);
}

Expand All @@ -244,9 +246,10 @@ public GHAppInstallation getInstallationByOrganization(String name) throws IOExc
@Preview
@Deprecated
public GHAppInstallation getInstallationByRepository(String ownerName, String repositoryName) throws IOException {
return root.retrieve()
return root.createRequest()
.withPreview(MACHINE_MAN)
.to(String.format("/repos/%s/%s/installation", ownerName, repositoryName), GHAppInstallation.class)
.withUrlPath(String.format("/repos/%s/%s/installation", ownerName, repositoryName))
.fetch(GHAppInstallation.class)
.wrapUp(root);
}

Expand All @@ -265,9 +268,10 @@ public GHAppInstallation getInstallationByRepository(String ownerName, String re
@Preview
@Deprecated
public GHAppInstallation getInstallationByUser(String name) throws IOException {
return root.retrieve()
return root.createRequest()
.withPreview(MACHINE_MAN)
.to(String.format("/users/%s/installation", name), GHAppInstallation.class)
.withUrlPath(String.format("/users/%s/installation", name))
.fetch(GHAppInstallation.class)
.wrapUp(root);
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class GHAppCreateTokenBuilder {
GHAppCreateTokenBuilder(GitHub root, String apiUrlTail, Map<String, GHPermissionType> permissions) {
this.root = root;
this.apiUrlTail = apiUrlTail;
this.builder = new Requester(root);
this.builder = root.createRequest();
withPermissions(builder, permissions);
}

Expand Down Expand Up @@ -57,7 +57,8 @@ public GHAppCreateTokenBuilder repositoryIds(List<Long> repositoryIds) {
public GHAppInstallationToken create() throws IOException {
return builder.method("POST")
.withPreview(MACHINE_MAN)
.to(apiUrlTail, GHAppInstallationToken.class)
.withUrlPath(apiUrlTail)
.fetch(GHAppInstallationToken.class)
.wrapUp(root);
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/kohsuke/github/GHAppInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ GHAppInstallation wrapUp(GitHub root) {
@Preview
@Deprecated
public void deleteInstallation() throws IOException {
root.retrieve().method("DELETE").withPreview(GAMBIT).to(String.format("/app/installations/%d", id));
root.createRequest()
.method("DELETE")
.withPreview(GAMBIT)
.withUrlPath(String.format("/app/installations/%d", id))
.send();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/kohsuke/github/GHAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public String getBrowserDownloadUrl() {
}

private void edit(String key, Object value) throws IOException {
new Requester(root).with(key, value).method("PATCH").to(getApiRoute());
root.createRequest().with(key, value).method("PATCH").withUrlPath(getApiRoute()).send();
}

/**
Expand All @@ -145,7 +145,7 @@ private void edit(String key, Object value) throws IOException {
* the io exception
*/
public void delete() throws IOException {
new Requester(root).method("DELETE").to(getApiRoute());
root.createRequest().method("DELETE").withUrlPath(getApiRoute()).send();
}

private String getApiRoute() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/kohsuke/github/GHBlobBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GHBlobBuilder {

GHBlobBuilder(GHRepository repo) {
this.repo = repo;
req = new Requester(repo.root);
req = repo.root.createRequest();
}

/**
Expand Down Expand Up @@ -54,6 +54,6 @@ private String getApiTail() {
* if the blob cannot be created.
*/
public GHBlob create() throws IOException {
return req.method("POST").to(getApiTail(), GHBlob.class);
return req.method("POST").withUrlPath(getApiTail()).fetch(GHBlob.class);
}
}
6 changes: 2 additions & 4 deletions src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.util.Collection;
import java.util.Objects;

import static org.kohsuke.github.Previews.*;

/**
* A branch in a repository.
*
Expand Down Expand Up @@ -103,7 +101,7 @@ public URL getProtectionUrl() {
* the io exception
*/
public GHBranchProtection getProtection() throws IOException {
return root.retrieve().to(protection_url, GHBranchProtection.class).wrap(this);
return root.createRequest().withUrlPath(protection_url).fetch(GHBranchProtection.class).wrap(this);
}

/**
Expand All @@ -122,7 +120,7 @@ public String getSHA1() {
* if disabling protection fails
*/
public void disableProtection() throws IOException {
new Requester(root).method("DELETE").to(protection_url);
root.createRequest().method("DELETE").withUrlPath(protection_url).send();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/kohsuke/github/GHBranchProtection.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GHBranchProtection {
@Preview
@Deprecated
public void enabledSignedCommits() throws IOException {
requester().method("POST").to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class);
requester().method("POST").withUrlPath(url + REQUIRE_SIGNATURES_URI).fetch(RequiredSignatures.class);
}

/**
Expand All @@ -56,7 +56,7 @@ public void enabledSignedCommits() throws IOException {
@Preview
@Deprecated
public void disableSignedCommits() throws IOException {
requester().method("DELETE").to(url + REQUIRE_SIGNATURES_URI);
requester().method("DELETE").withUrlPath(url + REQUIRE_SIGNATURES_URI).send();
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public RequiredReviews getRequiredReviews() {
@Preview
@Deprecated
public boolean getRequiredSignatures() throws IOException {
return requester().method("GET").to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class).enabled;
return requester().withUrlPath(url + REQUIRE_SIGNATURES_URI).fetch(RequiredSignatures.class).enabled;
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ GHBranchProtection wrap(GHBranch branch) {
}

private Requester requester() {
return new Requester(root).withPreview(ZZZAX);
return root.createRequest().withPreview(ZZZAX);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public GHBranchProtection enable() throws IOException {
.withNullable("required_pull_request_reviews", prReviews)
.withNullable("restrictions", restrictions)
.withNullable("enforce_admins", enforceAdmins)
.to(branch.getProtectionUrl().toString(), GHBranchProtection.class)
.withUrlPath(branch.getProtectionUrl().toString())
.fetch(GHBranchProtection.class)
.wrap(branch);
}

Expand Down Expand Up @@ -352,7 +353,7 @@ private StatusChecks getStatusChecks() {
}

private Requester requester() {
return new Requester(branch.getRoot()).withPreview(LUKE_CAGE);
return branch.getRoot().createRequest().withPreview(LUKE_CAGE);
}

private static class Restrictions {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private GHUser resolveUser(User author) throws IOException {
* @return {@link PagedIterable} with all the commit comments in this repository.
*/
public PagedIterable<GHCommitComment> listComments() {
return owner.root.retrieve()
return owner.root.createRequest()
.asPagedIterable(
String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha),
GHCommitComment[].class,
Expand All @@ -466,12 +466,15 @@ public PagedIterable<GHCommitComment> listComments() {
* if comment is not created
*/
public GHCommitComment createComment(String body, String path, Integer line, Integer position) throws IOException {
GHCommitComment r = new Requester(owner.root).with("body", body)
GHCommitComment r = owner.root.createRequest()
.method("POST")
.with("body", body)
.with("path", path)
.with("line", line)
.with("position", position)
.to(String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha),
GHCommitComment.class);
.withUrlPath(
String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha))
.fetch(GHCommitComment.class);
return r.wrap(owner);
}

Expand Down Expand Up @@ -518,7 +521,7 @@ public GHCommitStatus getLastStatus() throws IOException {
*/
void populate() throws IOException {
if (files == null && stats == null)
owner.root.retrieve().to(owner.getApiTailUrl("commits/" + sha), this);
owner.root.createRequest().withUrlPath(owner.getApiTailUrl("commits/" + sha)).fetchInto(this);
}

GHCommit wrapUp(GHRepository owner) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/kohsuke/github/GHCommitBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private UserInfo(String name, String email, Date date) {

GHCommitBuilder(GHRepository repo) {
this.repo = repo;
req = new Requester(repo.root);
req = repo.root.createRequest().method("POST");
}

/**
Expand Down Expand Up @@ -118,6 +118,6 @@ private String getApiTail() {
*/
public GHCommit create() throws IOException {
req.with("parents", parents);
return req.method("POST").to(getApiTail(), GHCommit.class).wrapUp(repo);
return req.method("POST").withUrlPath(getApiTail()).fetch(GHCommit.class).wrapUp(repo);
}
}
17 changes: 12 additions & 5 deletions src/main/java/org/kohsuke/github/GHCommitComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,30 @@ public GHCommit getCommit() throws IOException {
* the io exception
*/
public void update(String body) throws IOException {
new Requester(owner.root).with("body", body).method("PATCH").to(getApiTail(), GHCommitComment.class);
owner.root.createRequest()
.method("PATCH")
.with("body", body)
.withUrlPath(getApiTail())
.fetch(GHCommitComment.class);
this.body = body;
}

@Preview
@Deprecated
public GHReaction createReaction(ReactionContent content) throws IOException {
return new Requester(owner.root).withPreview(SQUIRREL_GIRL)
return owner.root.createRequest()
.method("POST")
.withPreview(SQUIRREL_GIRL)
.with("content", content.getContent())
.to(getApiTail() + "/reactions", GHReaction.class)
.withUrlPath(getApiTail() + "/reactions")
.fetch(GHReaction.class)
.wrap(owner.root);
}

@Preview
@Deprecated
public PagedIterable<GHReaction> listReactions() {
return owner.root.retrieve()
return owner.root.createRequest()
.withPreview(SQUIRREL_GIRL)
.asPagedIterable(getApiTail() + "/reactions", GHReaction[].class, item -> item.wrap(owner.root));
}
Expand All @@ -141,7 +148,7 @@ public PagedIterable<GHReaction> listReactions() {
* the io exception
*/
public void delete() throws IOException {
new Requester(owner.root).method("DELETE").to(getApiTail());
owner.root.createRequest().method("DELETE").withUrlPath(getApiTail()).send();
}

private String getApiTail() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class GHCommitQueryBuilder {

GHCommitQueryBuilder(GHRepository repo) {
this.repo = repo;
this.req = repo.root.retrieve(); // requester to build up
this.req = repo.root.createRequest(); // requester to build up
}

/**
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/org/kohsuke/github/GHContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public boolean isDirectory() {
* the io exception
*/
protected synchronized void populate() throws IOException {
root.retrieve().to(url, this);
root.createRequest().withUrlPath(url).fetchInto(this);
}

/**
Expand All @@ -237,7 +237,9 @@ public PagedIterable<GHContent> listDirectoryContent() throws IOException {
if (!isDirectory())
throw new IllegalStateException(path + " is not a directory");

return root.retrieve().asPagedIterable(url, GHContent[].class, item -> item.wrap(repository));
return root.createRequest()
.setRawUrlPath(url)
.asPagedIterable(GHContent[].class, item -> item.wrap(repository));
}

/**
Expand Down Expand Up @@ -306,7 +308,9 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa
throws IOException {
String encodedContent = Base64.getMimeEncoder().encodeToString(newContentBytes);

Requester requester = new Requester(root).with("path", path)
Requester requester = root.createRequest()
.method("POST")
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
.with("content", encodedContent)
Expand All @@ -316,7 +320,8 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa
requester.with("branch", branch);
}

GHContentUpdateResponse response = requester.to(getApiRoute(repository, path), GHContentUpdateResponse.class);
GHContentUpdateResponse response = requester.withUrlPath(getApiRoute(repository, path))
.fetch(GHContentUpdateResponse.class);

response.getContent().wrap(repository);
response.getCommit().wrapUp(repository);
Expand Down Expand Up @@ -350,7 +355,9 @@ public GHContentUpdateResponse delete(String message) throws IOException {
* the io exception
*/
public GHContentUpdateResponse delete(String commitMessage, String branch) throws IOException {
Requester requester = new Requester(root).with("path", path)
Requester requester = root.createRequest()
.method("POST")
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
.method("DELETE");
Expand All @@ -359,7 +366,8 @@ public GHContentUpdateResponse delete(String commitMessage, String branch) throw
requester.with("branch", branch);
}

GHContentUpdateResponse response = requester.to(getApiRoute(repository, path), GHContentUpdateResponse.class);
GHContentUpdateResponse response = requester.withUrlPath(getApiRoute(repository, path))
.fetch(GHContentUpdateResponse.class);

response.getCommit().wrapUp(repository);
return response;
Expand Down Expand Up @@ -405,6 +413,6 @@ public static GHContent[] wrap(GHContent[] contents, GHRepository repository) {
*/
@Override
public synchronized void refresh() throws IOException {
root.retrieve().to(url, this);
root.createRequest().setRawUrlPath(url).fetchInto(this);
}
}
Loading