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;
}
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);
}
}
Aggregations