io.dangernoodle.grt.GithubClient Maven / Gradle / Ivy
package io.dangernoodle.grt;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import org.kohsuke.github.GHCreateRepositoryBuilder;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import io.dangernoodle.grt.Repository.Settings;
public class GithubClient
{
public final GitHub github;
private final Map collaborators;
private final Map organizations;
private final Map> orgTeams;
private final Map repositories;
public GithubClient(GitHub github)
{
this.github = github;
this.collaborators = createMap();
this.repositories = createMap();
this.organizations = createMap();
this.orgTeams = createMap();
}
public GHRepository createOrgRepository(Repository repository) throws IOException
{
GHOrganization ghOrg = getOrganization(repository.getOrganization());
return createRepository(repository, name -> {
return ghOrg.createRepository(name);
});
}
public GHRepository createUserRepository(Repository repository) throws IOException
{
return createRepository(repository, name -> github.createRepository(name));
}
public String getCurrentLogin() throws IOException
{
return github.getMyself().getLogin();
}
public GHMyself getMyself() throws IOException
{
return (GHMyself) computeIfAbsent(collaborators, GHMyself.class.getName(), n -> {
return github.getMyself();
});
}
public GHOrganization getOrganization(String name) throws IOException
{
return computeIfAbsent(organizations, name, n -> github.getOrganization(name));
}
public GHRepository getRepository(String repository) throws IOException
{
return computeIfAbsent(repositories, repository, name -> getMyself().getRepository(name));
}
public GHRepository getRepository(String organization, String repository) throws IOException
{
return computeIfAbsent(repositories, repository, name -> getOrganization(organization).getRepository(name));
}
public GHTeam getTeam(String organization, String slug) throws IOException
{
GHOrganization ghOrganization = getOrganization(organization);
Map teams = computeIfAbsent(orgTeams, ghOrganization, o -> createMap());
return computeIfAbsent(teams, slug, s -> {
return ghOrganization.getTeamBySlug(s);
});
}
public GHUser getUser(String login) throws IOException
{
return computeIfAbsent(collaborators, login, l -> {
return github.getUser(l);
});
}
public GHEventPayload.Push parsePushEvent(Reader reader) throws IOException
{
return github.parseEventPayload(reader, GHEventPayload.Push.class);
}
private V computeIfAbsent(Map map, K name, IOExceptionFunction function) throws IOException
{
try
{
return map.computeIfAbsent(name, n -> {
try
{
return function.apply(name);
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
});
}
catch (UncheckedIOException e)
{
throw e.getCause();
}
}
private Map createMap()
{
return new ConcurrentHashMap<>(8, 0.9f);
}
private GHRepository createRepository(Repository repository, Function function)
throws IOException
{
return computeIfAbsent(repositories, repository.getName(), name -> {
Settings settings = repository.getSettings();
GHCreateRepositoryBuilder builder = function.apply(name);
// would be nice to chain, but then the all these would have to be mocked to return the builder
builder.allowMergeCommit(settings.enableMergeCommits());
builder.allowRebaseMerge(settings.enableRebaseMerge());
builder.allowSquashMerge(settings.enableSquashMerge());
builder.autoInit(settings.autoInitialize());
builder.description(repository.getDescription());
builder.gitignoreTemplate(repository.getIgnoreTemplate());
builder.homepage(repository.getHomepage());
builder.issues(settings.enableIssues());
builder.licenseTemplate(repository.getLicenseTemplate());
builder.private_(settings.isPrivate());
builder.wiki(settings.enableWiki());
return builder.create();
});
}
public static GithubClient createClient(String token) throws IOException
{
GithubClient client = new GithubClient(GitHub.connectUsingOAuth(token));
client.github.checkApiUrlValidity();
return client;
}
@FunctionalInterface
private interface IOExceptionFunction
{
R apply(T t) throws IOException;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy