Skip to content

Commit

Permalink
Merge pull request #1230 from bitwiseman/task/cleanup-root
Browse files Browse the repository at this point in the history
Streamline GitHub root handling
  • Loading branch information
bitwiseman committed Sep 15, 2021
2 parents bb72b8d + 0467f0a commit bcb71a3
Show file tree
Hide file tree
Showing 257 changed files with 5,076 additions and 2,558 deletions.
29 changes: 10 additions & 19 deletions src/main/java/org/kohsuke/github/GHApp.java
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
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
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
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
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
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
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
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
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

0 comments on commit bcb71a3

Please sign in to comment.