Search in sources :

Example 1 with GithubProject

use of org.talend.sdk.component.starter.server.model.GithubProject 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);
}
Also used : CreateProjectRequest(org.talend.sdk.component.starter.server.model.github.CreateProjectRequest) WebApplicationException(javax.ws.rs.WebApplicationException) ZipEntry(java.util.zip.ZipEntry) FileWriter(java.io.FileWriter) Result(org.talend.sdk.component.starter.server.model.Result) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BufferedInputStream(java.io.BufferedInputStream) Client(javax.ws.rs.client.Client) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) Git(org.eclipse.jgit.api.Git) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) GithubProject(org.talend.sdk.component.starter.server.model.GithubProject) ErrorMessage(org.talend.sdk.component.starter.server.model.ErrorMessage) File(java.io.File) Writer(java.io.Writer) FileWriter(java.io.FileWriter) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Writer (java.io.Writer)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Client (javax.ws.rs.client.Client)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1