use of org.talend.sdk.component.starter.server.model.ErrorMessage in project component-runtime by Talend.
the class ProjectResource method createOnGithub.
@POST
@Path("github")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Result createOnGithub(final GithubProject project) {
// create an in-memory zip of the project
final ByteArrayOutputStream zip = new ByteArrayOutputStream();
generator.generate(toRequest(project.getModel()), zip);
final GithubProject.Repository githubConfig = project.getRepository();
final boolean useOrganization = githubConfig.isUseOrganization();
final String organization = useOrganization ? githubConfig.getOrganization() : githubConfig.getUsername();
// create the github project
final Client client = ClientBuilder.newClient();
try {
client.target(starterConfiguration.githubBaseApi()).path(useOrganization ? starterConfiguration.githubOrgCreateProjectPath() : starterConfiguration.githubCreateProjectPath()).resolveTemplate("name", organization).request(APPLICATION_JSON_TYPE).header("Accept", "application/vnd.github.v3+json").header("Authorization", "Basic " + Base64.getEncoder().encodeToString((githubConfig.getUsername() + ':' + githubConfig.getPassword()).getBytes(StandardCharsets.UTF_8))).method(starterConfiguration.githubCreateProjectMethod(), entity(new CreateProjectRequest(project.getModel().getArtifact(), project.getModel().getDescription(), false), APPLICATION_JSON_TYPE), CreateProjectResponse.class);
} finally {
client.close();
}
// clone the project in a temp repo
final File workDir = new File(starterConfiguration.workDir().replace("${java.io.tmpdir}", System.getProperty("java.io.tmpdir")), githubConfig.getRepository() + "_" + System.nanoTime());
if (!workDir.mkdirs()) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorMessage("can't create a temporary folder")).type(APPLICATION_JSON_TYPE).build());
}
final UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(githubConfig.getUsername(), githubConfig.getPassword());
try (final Git git = Git.cloneRepository().setBranch("master").setURI(String.format(starterConfiguration.githubRepository(), organization, githubConfig.getRepository())).setDirectory(workDir).setProgressMonitor(NullProgressMonitor.INSTANCE).setCredentialsProvider(credentialsProvider).call()) {
{
// copy the zip files into the project temporary folder
try (final ZipInputStream file = new ZipInputStream(new ByteArrayInputStream(zip.toByteArray()))) {
ZipEntry entry;
while ((entry = file.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
final InputStream in = new BufferedInputStream(file) {
@Override
public void close() throws IOException {
file.closeEntry();
}
};
// drop the root folder to import it directly into the repo
final String path = entry.getName().substring(ofNullable(project.getModel().getArtifact()).orElse("application").length() + 1);
final File out = new File(workDir, path);
out.getParentFile().mkdirs();
// see codenvy facet for more details
if (path.equals("README.adoc") || path.endsWith(".json")) {
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
final String content = reader.lines().collect(joining("\n"));
Files.write(out.toPath(), content.replace("@organization@", organization).replace("@repository@", githubConfig.getRepository()).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW);
}
} else {
Files.copy(in, out.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
// add a gitignore, we could use the template but the default doesn't match what we want
// see https://github.com/github/gitignore/
final File gitIgnore = new File(workDir, ".gitignore");
if (!gitIgnore.exists()) {
try (final Writer writer = new FileWriter(gitIgnore)) {
switch(ofNullable(project.getModel().getBuildType()).orElse("Maven").toLowerCase(ENGLISH)) {
case "gradle":
writer.write(".gradle\n" + "/build/\n" + "\n" + "# Ignore Gradle GUI config\n" + "gradle-app.setting\n" + "\n" + "# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)\n" + "!gradle-wrapper.jar\n" + "\n" + "# Cache of project\n" + ".gradletasknamecache\n" + "\n" + "# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898\n" + "# gradle/wrapper/gradle-wrapper.properties\n");
break;
default:
// maven
writer.write("target/\n" + "pom.xml.tag\n" + "pom.xml.releaseBackup\n" + "pom.xml.versionsBackup\n" + "pom.xml.next\n" + "release.properties\n" + "dependency-reduced-pom.xml\n" + "buildNumber.properties\n" + ".mvn/timing.properties\n");
}
}
}
// commit them all and push
git.add().addFilepattern(".").call();
git.commit().setMessage("Importing Talend Component Project from the Starter application").setAuthor("Talend Component Kit Starter WebApp", "tacokit@talend.com").call();
git.push().setProgressMonitor(NullProgressMonitor.INSTANCE).setCredentialsProvider(credentialsProvider).setTimeout((int) TimeUnit.MINUTES.toSeconds(5)).call();
} catch (final GitAPIException | IOException e) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorMessage(e.getMessage())).type(APPLICATION_JSON_TYPE).build());
} finally {
try {
FileUtils.delete(workDir, FileUtils.RECURSIVE);
} catch (final IOException e) {
log.warn(e.getMessage(), e);
}
}
return new Result(true);
}
use of org.talend.sdk.component.starter.server.model.ErrorMessage in project component-runtime by Talend.
the class ProjectResource method createZip.
@POST
@Path("zip/form")
@Produces("application/zip")
public Response createZip(@FormParam("project") final String compressedModel, @Context final Providers providers) {
final MessageBodyReader<ProjectModel> jsonReader = providers.getMessageBodyReader(ProjectModel.class, ProjectModel.class, NO_ANNOTATION, APPLICATION_JSON_TYPE);
final ProjectModel model;
try (final InputStream gzipInputStream = new ByteArrayInputStream(Base64.getUrlDecoder().decode(compressedModel))) {
model = jsonReader.readFrom(ProjectModel.class, ProjectModel.class, NO_ANNOTATION, APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), gzipInputStream);
} catch (final IOException e) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorMessage(e.getMessage())).type(APPLICATION_JSON_TYPE).build());
}
final String filename = ofNullable(model.getArtifact()).orElse("zip") + ".zip";
return Response.ok().entity((StreamingOutput) out -> {
generator.generate(toRequest(model), out);
out.flush();
}).header("Content-Disposition", "inline; filename=" + filename).build();
}
Aggregations