Search in sources :

Example 1 with GitHubUser

use of io.micronaut.starter.api.create.github.client.v3.GitHubUser in project micronaut-starter by micronaut-projects.

the class GitHubCreateService method creatApp.

protected GitHubRepository creatApp(@NonNull ApplicationType type, @NonNull String name, @Nullable List<String> features, @Nullable BuildTool buildTool, @Nullable TestFramework testFramework, @Nullable Language lang, @Nullable JdkVersion javaVersion, @NonNull String code, @NonNull String state, @Nullable String userAgent) {
    AccessToken accessToken = getGitHubAccessToken(code, state);
    String authToken = TOKEN_PREFIX + accessToken.getAccessToken();
    GitHubUser gitHubUser = getGitHubUser(authToken);
    GeneratorContext generatorContext = createProjectGeneratorContext(type, name, features, buildTool, testFramework, lang, javaVersion, userAgent);
    String repoName = generatorContext.getProject().getName();
    String repoDescription = String.format("Micronaut %s Application", generatorContext.getProject().getNaturalName());
    GitHubRepository githubRepository = createGitHubRepository(authToken, repoName, repoDescription, gitHubUser);
    pushToGithubRepository(generatorContext, gitHubUser, githubRepository, accessToken);
    return githubRepository;
}
Also used : GitHubUser(io.micronaut.starter.api.create.github.client.v3.GitHubUser) AccessToken(io.micronaut.starter.api.create.github.client.oauth.AccessToken) GeneratorContext(io.micronaut.starter.application.generator.GeneratorContext) GitHubRepository(io.micronaut.starter.api.create.github.client.v3.GitHubRepository)

Example 2 with GitHubUser

use of io.micronaut.starter.api.create.github.client.v3.GitHubUser in project micronaut-starter by micronaut-projects.

the class GitHubCreateService method pushToRepository.

/**
 * Push files in {@code localPath} to the GitHub repository {@code repository}.
 *
 * @param repository github repository
 * @param user       github user
 * @param localPath  local directry with generated content
 * @param token      github user access token
 * @throws IOException
 */
protected void pushToRepository(@NotNull GitHubRepository repository, @NotNull GitHubUser user, @NotNull Path localPath, @NotNull String token) throws IOException {
    try {
        final String name = StringUtils.isEmpty(user.getName()) ? user.getLogin() : user.getName();
        final String email = StringUtils.isEmpty(user.getEmail()) ? user.getLogin() : user.getEmail();
        Git gitRepo = Git.init().setDirectory(localPath.toFile()).call();
        gitRepo.add().addFilepattern(".").call();
        gitRepo.commit().setMessage(COMMIT_MESSAGE).setAuthor(name, email).setCommitter(name, email).setSign(false).call();
        Iterable<PushResult> pushResults = gitRepo.push().setRemote(repository.getCloneUrl()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.getLogin(), token)).call();
        List<RemoteRefUpdate> failedRefUpdates = StreamSupport.stream(pushResults.spliterator(), false).flatMap(pushResult -> pushResult.getRemoteUpdates().stream()).filter(remoteRefUpdate -> remoteRefUpdate.getStatus() != RemoteRefUpdate.Status.OK).collect(Collectors.toList());
        if (!failedRefUpdates.isEmpty()) {
            String msg = String.format("Failed to push to %s repository.", repository.getName());
            LOG.error(msg);
            throw new IOException(msg);
        }
    } catch (GitAPIException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ApplicationType(io.micronaut.starter.application.ApplicationType) LoggerFactory(org.slf4j.LoggerFactory) TestFramework(io.micronaut.starter.api.TestFramework) Singleton(javax.inject.Singleton) AccessToken(io.micronaut.starter.api.create.github.client.oauth.AccessToken) AbstractCreateController(io.micronaut.starter.api.create.AbstractCreateController) ConsoleOutput(io.micronaut.starter.io.ConsoleOutput) NonNull(edu.umd.cs.findbugs.annotations.NonNull) StarterConfiguration(io.micronaut.starter.api.StarterConfiguration) Requires(io.micronaut.context.annotation.Requires) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) GeneratorContext(io.micronaut.starter.application.generator.GeneratorContext) StreamSupport(java.util.stream.StreamSupport) GitHubApiClient(io.micronaut.starter.api.create.github.client.v3.GitHubApiClient) ProjectGenerator(io.micronaut.starter.application.generator.ProjectGenerator) Path(java.nio.file.Path) BuildTool(io.micronaut.starter.options.BuildTool) Logger(org.slf4j.Logger) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Files(java.nio.file.Files) GitHubOAuthClient(io.micronaut.starter.api.create.github.client.oauth.GitHubOAuthClient) OutputHandler(io.micronaut.starter.io.OutputHandler) FileSystemOutputHandler(io.micronaut.starter.io.FileSystemOutputHandler) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) IOException(java.io.IOException) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) File(java.io.File) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Paths(java.nio.file.Paths) GitHubRepository(io.micronaut.starter.api.create.github.client.v3.GitHubRepository) GitHubUser(io.micronaut.starter.api.create.github.client.v3.GitHubUser) PushResult(org.eclipse.jgit.transport.PushResult) Nullable(edu.umd.cs.findbugs.annotations.Nullable) JdkVersion(io.micronaut.starter.options.JdkVersion) Language(io.micronaut.starter.options.Language) Git(org.eclipse.jgit.api.Git) Comparator(java.util.Comparator) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) PushResult(org.eclipse.jgit.transport.PushResult) IOException(java.io.IOException)

Aggregations

AccessToken (io.micronaut.starter.api.create.github.client.oauth.AccessToken)2 GitHubRepository (io.micronaut.starter.api.create.github.client.v3.GitHubRepository)2 GitHubUser (io.micronaut.starter.api.create.github.client.v3.GitHubUser)2 GeneratorContext (io.micronaut.starter.application.generator.GeneratorContext)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1 Requires (io.micronaut.context.annotation.Requires)1 ApplicationEventPublisher (io.micronaut.context.event.ApplicationEventPublisher)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)1 StarterConfiguration (io.micronaut.starter.api.StarterConfiguration)1 TestFramework (io.micronaut.starter.api.TestFramework)1 AbstractCreateController (io.micronaut.starter.api.create.AbstractCreateController)1 GitHubOAuthClient (io.micronaut.starter.api.create.github.client.oauth.GitHubOAuthClient)1 GitHubApiClient (io.micronaut.starter.api.create.github.client.v3.GitHubApiClient)1 ApplicationType (io.micronaut.starter.application.ApplicationType)1 ProjectGenerator (io.micronaut.starter.application.generator.ProjectGenerator)1 ConsoleOutput (io.micronaut.starter.io.ConsoleOutput)1 FileSystemOutputHandler (io.micronaut.starter.io.FileSystemOutputHandler)1 OutputHandler (io.micronaut.starter.io.OutputHandler)1