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

Streamline GitHub root handling #1230

Merged
merged 5 commits into from
Sep 15, 2021
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
29 changes: 10 additions & 19 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ public void setPermissions(Map<String, String> permissions) {
throw new RuntimeException("Do not use this method.");
}

GHApp wrapUp(GitHub root) {
this.root = root;
return this;
}

/**
* Obtains all the installations associated with this app.
* <p>
Expand All @@ -198,10 +193,10 @@ GHApp wrapUp(GitHub root) {
*/
@Preview(MACHINE_MAN)
public PagedIterable<GHAppInstallation> listInstallations() {
return root.createRequest()
return root().createRequest()
.withPreview(MACHINE_MAN)
.withUrlPath("/app/installations")
.toIterable(GHAppInstallation[].class, item -> item.wrapUp(root));
.toIterable(GHAppInstallation[].class, null);
}

/**
Expand All @@ -218,11 +213,10 @@ public PagedIterable<GHAppInstallation> listInstallations() {
*/
@Preview(MACHINE_MAN)
public GHAppInstallation getInstallationById(long id) throws IOException {
return root.createRequest()
return root().createRequest()
.withPreview(MACHINE_MAN)
.withUrlPath(String.format("/app/installations/%d", id))
.fetch(GHAppInstallation.class)
.wrapUp(root);
.fetch(GHAppInstallation.class);
}

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

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

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

}
5 changes: 2 additions & 3 deletions src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GHAppCreateTokenBuilder extends GitHubInteractiveObject {

@BetaApi
GHAppCreateTokenBuilder(GitHub root, String apiUrlTail) {
this.root = root;
super(root);
this.apiUrlTail = apiUrlTail;
this.builder = root.createRequest();
}
Expand Down Expand Up @@ -78,8 +78,7 @@ public GHAppInstallationToken create() throws IOException {
return builder.method("POST")
.withPreview(MACHINE_MAN)
.withUrlPath(apiUrlTail)
.fetch(GHAppInstallationToken.class)
.wrapUp(root);
.fetch(GHAppInstallationToken.class);
}

}
29 changes: 5 additions & 24 deletions src/main/java/org/kohsuke/github/GHAppInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ public URL getHtmlUrl() {
return GitHubClient.parseURL(htmlUrl);
}

/**
* Gets root.
*
* @return the root
* @deprecated This method should be used internally only.
*/
@Deprecated
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GitHub getRoot() {
return root;
}

/**
* Sets root.
*
Expand Down Expand Up @@ -136,21 +124,19 @@ public PagedSearchIterable<GHRepository> listRepositories() {
GitHubRequest request;

try {
request = root.createRequest().withPreview(MACHINE_MAN).withUrlPath("/installation/repositories").build();
request = root().createRequest().withPreview(MACHINE_MAN).withUrlPath("/installation/repositories").build();
} catch (MalformedURLException e) {
throw new GHException("", e);
}

return new PagedSearchIterable<>(root, request, GHAppInstallationRepositoryResult.class);
return new PagedSearchIterable<>(root(), request, GHAppInstallationRepositoryResult.class);
}

private static class GHAppInstallationRepositoryResult extends SearchResult<GHRepository> {
private GHRepository[] repositories;

@Override
GHRepository[] getItems(GitHub root) {
for (GHRepository item : repositories)
item.wrap(root);
return repositories;
}
}
Expand Down Expand Up @@ -316,11 +302,6 @@ public void setRepositorySelection(GHRepositorySelection repositorySelection) {
throw new RuntimeException("Do not use this method.");
}

GHAppInstallation wrapUp(GitHub root) {
this.root = root;
return this;
}

/**
* Delete a Github App installation
* <p>
Expand All @@ -332,7 +313,7 @@ GHAppInstallation wrapUp(GitHub root) {
*/
@Preview(GAMBIT)
public void deleteInstallation() throws IOException {
root.createRequest()
root().createRequest()
.method("DELETE")
.withPreview(GAMBIT)
.withUrlPath(String.format("/app/installations/%d", getId()))
Expand All @@ -353,7 +334,7 @@ public void deleteInstallation() throws IOException {
*/
@BetaApi
public GHAppCreateTokenBuilder createToken(Map<String, GHPermissionType> permissions) {
return new GHAppCreateTokenBuilder(root,
return new GHAppCreateTokenBuilder(root(),
String.format("/app/installations/%d/access_tokens", getId()),
permissions);
}
Expand All @@ -369,6 +350,6 @@ public GHAppCreateTokenBuilder createToken(Map<String, GHPermissionType> permiss
*/
@BetaApi
public GHAppCreateTokenBuilder createToken() {
return new GHAppCreateTokenBuilder(root, String.format("/app/installations/%d/access_tokens", getId()));
return new GHAppCreateTokenBuilder(root(), String.format("/app/installations/%d/access_tokens", getId()));
}
}
17 changes: 0 additions & 17 deletions src/main/java/org/kohsuke/github/GHAppInstallationToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ public class GHAppInstallationToken extends GitHubInteractiveObject {
private List<GHRepository> repositories;
private GHRepositorySelection repositorySelection;

/**
* Gets root.
*
* @return the root
* @deprecated This method should be used internally only.
*/
@Deprecated
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GitHub getRoot() {
return root;
}

/**
* Sets root.
*
Expand Down Expand Up @@ -143,9 +131,4 @@ public Date getExpiresAt() throws IOException {
private Object expiresAtStr(Date id, Class type) {
return expires_at;
}

GHAppInstallationToken wrapUp(GitHub root) {
this.root = root;
return this;
}
}
14 changes: 4 additions & 10 deletions src/main/java/org/kohsuke/github/GHArtifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public URL getHtmlUrl() throws IOException {
* the io exception
*/
public void delete() throws IOException {
root.createRequest().method("DELETE").withUrlPath(getApiRoute()).fetchHttpStatusCode();
root().createRequest().method("DELETE").withUrlPath(getApiRoute()).fetchHttpStatusCode();
}

/**
Expand All @@ -116,27 +116,21 @@ public void delete() throws IOException {
public <T> T download(InputStreamFunction<T> streamFunction) throws IOException {
requireNonNull(streamFunction, "Stream function must not be null");

return root.createRequest().method("GET").withUrlPath(getApiRoute(), "zip").fetchStream(streamFunction);
return root().createRequest().method("GET").withUrlPath(getApiRoute(), "zip").fetchStream(streamFunction);
}

private String getApiRoute() {
if (owner == null) {
// Workflow runs returned from search to do not have an owner. Attempt to use url.
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
return StringUtils.prependIfMissing(url.toString().replace(root.getApiUrl(), ""), "/");
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/");
}
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/actions/artifacts/" + getId();
}

GHArtifact wrapUp(GHRepository owner) {
this.owner = owner;
return wrapUp(owner.root);
}

GHArtifact wrapUp(GitHub root) {
this.root = root;
if (owner != null)
owner.wrap(root);
return this;
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHArtifactsIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public GHArtifactsIterable(GHRepository owner, GitHubRequest.Builder<?> requestB
@Override
public PagedIterator<GHArtifact> _iterator(int pageSize) {
return new PagedIterator<>(
adapt(GitHubPageIterator.create(owner.getRoot().getClient(), GHArtifactsPage.class, request, pageSize)),
adapt(GitHubPageIterator.create(owner.root().getClient(), GHArtifactsPage.class, request, pageSize)),
null);
}

Expand Down
17 changes: 2 additions & 15 deletions src/main/java/org/kohsuke/github/GHAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ public GHRepository getOwner() {
return owner;
}

/**
* Gets root.
*
* @return the root
* @deprecated This method should be used internally only.
*/
@Deprecated
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GitHub getRoot() {
return root;
}

/**
* Gets size.
*
Expand Down Expand Up @@ -140,7 +128,7 @@ public String getBrowserDownloadUrl() {
}

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

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

private String getApiRoute() {
Expand All @@ -159,7 +147,6 @@ private String getApiRoute() {

GHAsset wrap(GHRelease release) {
this.owner = release.getOwner();
this.root = owner.root;
return this;
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/java/org/kohsuke/github/GHAuthorization.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ public class GHAuthorization extends GHObject {
// TODO add some user class for https://developer.github.com/v3/oauth_authorizations/#check-an-authorization ?
// private GHUser user;

/**
* Gets root.
*
* @return the root
* @deprecated This method should be used internally only.
*/
@Deprecated
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GitHub getRoot() {
return root;
}

/**
* Gets scopes.
*
Expand Down Expand Up @@ -158,11 +146,6 @@ public String getFingerprint() {
return fingerprint;
}

GHAuthorization wrap(GitHub root) {
this.root = root;
return this;
}

@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD" },
justification = "JSON API")
private static class App {
Expand Down
2 changes: 1 addition & 1 deletion 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 = repo.root.createRequest();
req = repo.root().createRequest();
}

/**
Expand Down
Loading