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
12 changes: 8 additions & 4 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ public PagedIterable<GHAppInstallation> listInstallations() {
public GHAppInstallation getInstallationById(long id) throws IOException {
return root.retrieve()
.withPreview(MACHINE_MAN)
.to(String.format("/app/installations/%d", id), GHAppInstallation.class)
.withUrlPath(String.format("/app/installations/%d", id))
.to(GHAppInstallation.class)
.wrapUp(root);
}

Expand All @@ -222,7 +223,8 @@ public GHAppInstallation getInstallationById(long id) throws IOException {
public GHAppInstallation getInstallationByOrganization(String name) throws IOException {
return root.retrieve()
.withPreview(MACHINE_MAN)
.to(String.format("/orgs/%s/installation", name), GHAppInstallation.class)
.withUrlPath(String.format("/orgs/%s/installation", name))
.to(GHAppInstallation.class)
.wrapUp(root);
}

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

Expand All @@ -267,7 +270,8 @@ public GHAppInstallation getInstallationByRepository(String ownerName, String re
public GHAppInstallation getInstallationByUser(String name) throws IOException {
return root.retrieve()
.withPreview(MACHINE_MAN)
.to(String.format("/users/%s/installation", name), GHAppInstallation.class)
.withUrlPath(String.format("/users/%s/installation", name))
.to(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.retrieve().method("POST");
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't that be just root.retrieve() as we are setting the method on the create() method?

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)
.to(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.retrieve()
.method("DELETE")
.withPreview(GAMBIT)
.withUrlPath(String.format("/app/installations/%d", id))
.to();
}

/**
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.retrieve().method("POST").with(key, value).method("PATCH").withUrlPath(getApiRoute()).to();
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe remove the method("POST") as we are setting method("PATCH") almost right after it

}

/**
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.retrieve().method("DELETE").withUrlPath(getApiRoute()).to();
}

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 @@ -13,7 +13,7 @@ public class GHBlobBuilder {

GHBlobBuilder(GHRepository repo) {
this.repo = repo;
req = new Requester(repo.root);
req = repo.root.retrieve().method("POST");
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

}

/**
Expand Down Expand Up @@ -55,6 +55,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()).to(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.retrieve().withUrlPath(protection_url).to(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.retrieve().method("DELETE").withUrlPath(protection_url).to();
}

/**
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).to(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).to();
}

/**
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().method("GET").withUrlPath(url + REQUIRE_SIGNATURES_URI).to(RequiredSignatures.class).enabled;
Copy link
Contributor

Choose a reason for hiding this comment

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

If you implement the suggestion of the private Requester requester() then you can get rid of .method("GET") and take advantage of the default HTTP method

}

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

private Requester requester() {
return new Requester(root).withPreview(ZZZAX);
return root.retrieve().method("POST").withPreview(ZZZAX);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe replace this with return root.retrieve()..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())
.to(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().retrieve().method("POST").withPreview(LUKE_CAGE);
}

private static class Restrictions {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.retrieve()
.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))
.to(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.retrieve().withUrlPath(owner.getApiTailUrl("commits/" + sha)).to(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.retrieve().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()).to(GHCommit.class).wrapUp(repo);
}
}
16 changes: 12 additions & 4 deletions src/main/java/org/kohsuke/github/GHCommitComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,24 @@ 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.retrieve()
.method("POST")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe remove this line as you're overriding the method value a few lines below

.with("body", body)
.method("PATCH")
.withUrlPath(getApiTail())
.to(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.retrieve()
.method("POST")
.withPreview(SQUIRREL_GIRL)
.with("content", content.getContent())
.to(getApiTail() + "/reactions", GHReaction.class)
.withUrlPath(getApiTail() + "/reactions")
.to(GHReaction.class)
.wrap(owner.root);
}

Expand All @@ -141,7 +149,7 @@ public PagedIterable<GHReaction> listReactions() {
* the io exception
*/
public void delete() throws IOException {
new Requester(owner.root).method("DELETE").to(getApiTail());
owner.root.retrieve().method("DELETE").withUrlPath(getApiTail()).to();
}

private String getApiTail() {
Expand Down
20 changes: 13 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.retrieve().withUrlPath(url).to(this);
}

/**
Expand All @@ -237,7 +237,7 @@ 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.retrieve().setRawUrlPath(url).asPagedIterable(GHContent[].class, item -> item.wrap(repository));
}

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

Requester requester = new Requester(root).with("path", path)
Requester requester = root.retrieve()
.method("POST")
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
.with("content", encodedContent)
Expand All @@ -316,7 +318,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))
.to(GHContentUpdateResponse.class);

response.getContent().wrap(repository);
response.getCommit().wrapUp(repository);
Expand Down Expand Up @@ -350,7 +353,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.retrieve()
.method("POST")
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
.method("DELETE");
Expand All @@ -359,7 +364,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))
.to(GHContentUpdateResponse.class);

response.getCommit().wrapUp(repository);
return response;
Expand Down Expand Up @@ -405,6 +411,6 @@ public static GHContent[] wrap(GHContent[] contents, GHRepository repository) {
*/
@Override
public synchronized void refresh() throws IOException {
root.retrieve().to(url, this);
root.retrieve().setRawUrlPath(url).to(this);
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/kohsuke/github/GHContentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class GHContentBuilder {

GHContentBuilder(GHRepository repo) {
this.repo = repo;
this.req = new Requester(repo.root).method("PUT");
this.req = repo.root.retrieve().method("PUT");
}

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ public GHContentBuilder branch(String branch) {
}

/**
* Used when updating (but not creating a new content) to specify Thetblob SHA of the file being replaced.
* Used when updating (but not creating a new content) to specify the blob SHA of the file being replaced.
*
* @param sha
* the sha
Expand Down Expand Up @@ -108,7 +108,8 @@ public GHContentBuilder message(String commitMessage) {
* the io exception
*/
public GHContentUpdateResponse commit() throws IOException {
GHContentUpdateResponse response = req.to(GHContent.getApiRoute(repo, path), GHContentUpdateResponse.class);
GHContentUpdateResponse response = req.withUrlPath(GHContent.getApiRoute(repo, path))
.to(GHContentUpdateResponse.class);

response.getContent().wrap(repo);
response.getCommit().wrapUp(repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GHCreateRepositoryBuilder {
GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name) {
this.root = root;
this.apiUrlTail = apiUrlTail;
this.builder = new Requester(root);
this.builder = root.retrieve().method("POST");
this.builder.with("name", name);
}

Expand Down Expand Up @@ -196,7 +196,7 @@ public GHCreateRepositoryBuilder team(GHTeam team) {
* if repsitory cannot be created
*/
public GHRepository create() throws IOException {
return builder.method("POST").to(apiUrlTail, GHRepository.class).wrap(root);
return builder.method("POST").withUrlPath(apiUrlTail).to(GHRepository.class).wrap(root);
}

}
6 changes: 4 additions & 2 deletions src/main/java/org/kohsuke/github/GHDeployKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public String toString() {
* the io exception
*/
public void delete() throws IOException {
new Requester(owner.root).method("DELETE")
.to(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id));
owner.root.retrieve()
.method("DELETE")
.withUrlPath(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id))
.to();
}
}
Loading