Skip to content

Commit

Permalink
Merge pull request #522 from PauloMigAlmeida/master
Browse files Browse the repository at this point in the history
Implement GitHub App API methods
  • Loading branch information
bitwiseman committed Oct 4, 2019
2 parents f622114 + 2fe7342 commit 8da6db9
Show file tree
Hide file tree
Showing 46 changed files with 2,065 additions and 19 deletions.
177 changes: 177 additions & 0 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

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

/**
* A Github App.
*
* @author Paulo Miguel Almeida
*
* @see GitHub#getApp()
*/

public class GHApp extends GHObject {

private GitHub root;
private GHUser owner;
private String name;
private String description;
@JsonProperty("external_url")
private String externalUrl;
private Map<String,String> permissions;
private List<GHEvent> events;
@JsonProperty("installations_count")
private long installationsCount;
@JsonProperty("html_url")
private String htmlUrl;


public GHUser getOwner() {
return owner;
}

public void setOwner(GHUser owner) {
this.owner = owner;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getExternalUrl() {
return externalUrl;
}

public void setExternalUrl(String externalUrl) {
this.externalUrl = externalUrl;
}

public List<GHEvent> getEvents() {
return events;
}

public void setEvents(List<GHEvent> events) {
this.events = events;
}

public long getInstallationsCount() {
return installationsCount;
}

public void setInstallationsCount(long installationsCount) {
this.installationsCount = installationsCount;
}

public URL getHtmlUrl() {
return GitHub.parseURL(htmlUrl);
}

public Map<String, String> getPermissions() {
return permissions;
}

public void setPermissions(Map<String, String> permissions) {
this.permissions = permissions;
}

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

/**
* Obtains all the installations associated with this app.
*
* You must use a JWT to access this endpoint.
*
* @see <a href="https://developer.github.com/v3/apps/#list-installations">List installations</a>
*/
@Preview @Deprecated
public PagedIterable<GHAppInstallation> listInstallations() {
return new PagedIterable<GHAppInstallation>() {
public PagedIterator<GHAppInstallation> _iterator(int pageSize) {
return new PagedIterator<GHAppInstallation>(root.retrieve().withPreview(MACHINE_MAN).asIterator("/app/installations", GHAppInstallation[].class, pageSize)) {
protected void wrapUp(GHAppInstallation[] page) {
for (GHAppInstallation appInstallation : page) {
appInstallation.wrapUp(root);
}
}
};
}
};
}

/**
* Obtain an installation associated with this app
* @param id - Installation Id
*
* You must use a JWT to access this endpoint.
*
* @see <a href="https://developer.github.com/v3/apps/#get-an-installation">Get an installation</a>
*/
@Preview @Deprecated
public GHAppInstallation getInstallationById(long id) throws IOException {
return root.retrieve().withPreview(MACHINE_MAN).to(String.format("/app/installations/%d", id), GHAppInstallation.class).wrapUp(root);
}

/**
* Obtain an organization installation associated with this app
* @param name - Organization name
*
* You must use a JWT to access this endpoint.
*
* @see <a href="https://developer.github.com/v3/apps/#get-an-organization-installation">Get an organization installation</a>
*/
@Preview @Deprecated
public GHAppInstallation getInstallationByOrganization(String name) throws IOException {
return root.retrieve().withPreview(MACHINE_MAN).to(String.format("/orgs/%s/installation", name), GHAppInstallation.class).wrapUp(root);
}

/**
* Obtain an repository installation associated with this app
* @param ownerName - Organization or user name
* @param repositoryName - Repository name
*
* You must use a JWT to access this endpoint.
*
* @see <a href="https://developer.github.com/v3/apps/#get-a-repository-installation">Get a repository installation</a>
*/
@Preview @Deprecated
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).wrapUp(root);
}

/**
* Obtain a user installation associated with this app
* @param name - user name
*
* You must use a JWT to access this endpoint.
*
* @see <a href="https://developer.github.com/v3/apps/#get-a-user-installation">Get a user installation</a>
*/
@Preview @Deprecated
public GHAppInstallation getInstallationByUser(String name) throws IOException {
return root.retrieve().withPreview(MACHINE_MAN).to(String.format("/users/%s/installation", name), GHAppInstallation.class).wrapUp(root);
}

}

53 changes: 53 additions & 0 deletions src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.List;
import java.util.Map;

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

/**
* Creates a access token for a GitHub App Installation
*
* @author Paulo Miguel Almeida
*
* @see GHAppInstallation#createToken(Map)
*/
public class GHAppCreateTokenBuilder {
private final GitHub root;
protected final Requester builder;
private final String apiUrlTail;

@Preview @Deprecated
/*package*/ GHAppCreateTokenBuilder(GitHub root, String apiUrlTail, Map<String, GHPermissionType> permissions) {
this.root = root;
this.apiUrlTail = apiUrlTail;
this.builder = new Requester(root);
this.builder.withPermissions("permissions",permissions);
}

/**
* By default the installation token has access to all repositories that the installation can access. To restrict
* the access to specific repositories, you can provide the repository_ids when creating the token. When you omit
* repository_ids, the response does not contain neither the repositories nor the permissions key.
*
* @param repositoryIds - Array containing the repositories Ids
*
*/
@Preview @Deprecated
public GHAppCreateTokenBuilder repositoryIds(List<Integer> repositoryIds) {
this.builder.with("repository_ids",repositoryIds);
return this;
}

/**
* Creates an app token with all the parameters.
*
* You must use a JWT to access this endpoint.
*/
@Preview @Deprecated
public GHAppInstallationToken create() throws IOException {
return builder.method("POST").withPreview(MACHINE_MAN).to(apiUrlTail, GHAppInstallationToken.class).wrapUp(root);
}

}
167 changes: 167 additions & 0 deletions src/main/java/org/kohsuke/github/GHAppInstallation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

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

/**
* A Github App Installation.
*
* @author Paulo Miguel Almeida
*
* @see GHApp#listInstallations()
* @see GHApp#getInstallationById(long)
* @see GHApp#getInstallationByOrganization(String)
* @see GHApp#getInstallationByRepository(String, String)
* @see GHApp#getInstallationByUser(String)
*/

public class GHAppInstallation extends GHObject {
private GitHub root;
private GHUser account;

@JsonProperty("access_tokens_url")
private String accessTokenUrl;
@JsonProperty("repositories_url")
private String repositoriesUrl;
@JsonProperty("app_id")
private long appId;
@JsonProperty("target_id")
private long targetId;
@JsonProperty("target_type")
private GHTargetType targetType;
private Map<String, GHPermissionType> permissions;
private List<GHEvent> events;
@JsonProperty("single_file_name")
private String singleFileName;
@JsonProperty("repository_selection")
private GHRepositorySelection repositorySelection;
private String htmlUrl;

public URL getHtmlUrl() {
return GitHub.parseURL(htmlUrl);
}

public GitHub getRoot() {
return root;
}

public void setRoot(GitHub root) {
this.root = root;
}

public GHUser getAccount() {
return account;
}

public void setAccount(GHUser account) {
this.account = account;
}

public String getAccessTokenUrl() {
return accessTokenUrl;
}

public void setAccessTokenUrl(String accessTokenUrl) {
this.accessTokenUrl = accessTokenUrl;
}

public String getRepositoriesUrl() {
return repositoriesUrl;
}

public void setRepositoriesUrl(String repositoriesUrl) {
this.repositoriesUrl = repositoriesUrl;
}

public long getAppId() {
return appId;
}

public void setAppId(long appId) {
this.appId = appId;
}

public long getTargetId() {
return targetId;
}

public void setTargetId(long targetId) {
this.targetId = targetId;
}

public GHTargetType getTargetType() {
return targetType;
}

public void setTargetType(GHTargetType targetType) {
this.targetType = targetType;
}

public Map<String, GHPermissionType> getPermissions() {
return permissions;
}

public void setPermissions(Map<String, GHPermissionType> permissions) {
this.permissions = permissions;
}

public List<GHEvent> getEvents() {
return events;
}

public void setEvents(List<GHEvent> events) {
this.events = events;
}

public String getSingleFileName() {
return singleFileName;
}

public void setSingleFileName(String singleFileName) {
this.singleFileName = singleFileName;
}

public GHRepositorySelection getRepositorySelection() {
return repositorySelection;
}

public void setRepositorySelection(GHRepositorySelection repositorySelection) {
this.repositorySelection = repositorySelection;
}

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

/**
* Delete a Github App installation
*
* You must use a JWT to access this endpoint.
*
* @see <a href="https://developer.github.com/v3/apps/#delete-an-installation">Delete an installation</a>
*/
@Preview @Deprecated
public void deleteInstallation() throws IOException {
root.retrieve().method("DELETE").withPreview(GAMBIT).to(String.format("/app/installations/%d", id));
}


/**
* Starts a builder that creates a new App Installation Token.
*
* <p>
* You use the returned builder to set various properties, then call {@link GHAppCreateTokenBuilder#create()}
* to finally create an access token.
*/
@Preview @Deprecated
public GHAppCreateTokenBuilder createToken(Map<String,GHPermissionType> permissions){
return new GHAppCreateTokenBuilder(root,String.format("/app/installations/%d/access_tokens", id), permissions);
}
}
Loading

0 comments on commit 8da6db9

Please sign in to comment.