Search in sources :

Example 16 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project stdlib by petergeneric.

the class ConfigGuiceModule method getRepository.

@Provides
@Singleton
@Named("config")
public ConfigRepository getRepository(@Named("repo.config.path") final File workingDirectory, @Named("repo.config.force-reclone-on-startup") final boolean reclone, @Named("repo.config.remote") final String remote) throws IOException, URISyntaxException, GitAPIException {
    log.info("Repo path " + workingDirectory + ", reclone: " + reclone);
    if (reclone && workingDirectory.exists()) {
        log.info("Recloning " + workingDirectory);
        File[] files = workingDirectory.listFiles();
        if (files != null)
            for (File file : files) {
                FileUtils.deleteQuietly(file);
                if (!file.exists()) {
                    throw new RuntimeException("Tried to delete local checkout contents but did not succeed. File was: " + file);
                }
            }
    }
    final File gitDir = new File(workingDirectory, ".git");
    final boolean newlyCreated;
    // Create the git repository if it doesn't already exist
    if (!gitDir.exists()) {
        log.info("Initialising new git dir at: " + workingDirectory);
        FileUtils.forceMkdir(workingDirectory);
        InitCommand init = new InitCommand();
        init.setBare(false).setDirectory(workingDirectory).setGitDir(gitDir).call();
        newlyCreated = true;
    } else {
        newlyCreated = false;
    }
    FileRepositoryBuilder frb = new FileRepositoryBuilder();
    Repository repo = frb.setGitDir(gitDir).readEnvironment().findGitDir().build();
    final boolean hasRemote = !remote.equalsIgnoreCase("none");
    final CredentialsProvider credentials;
    if (hasRemote) {
        // Try to extract username/password from the remote URL
        final URIish uri = new URIish(remote);
        if (uri.getUser() != null && uri.getPass() != null)
            credentials = new UsernamePasswordCredentialsProvider(uri.getUser(), uri.getPass());
        else
            credentials = null;
    } else {
        credentials = null;
    }
    if (newlyCreated) {
        // Add the remote and pull from it
        if (hasRemote) {
            RepoHelper.addRemote(repo, "origin", remote);
            RepoHelper.pull(repo, "origin", credentials);
        }
        Git git = new Git(repo);
        // If there are no commits in this repository, create one
        if (!git.log().setMaxCount(1).call().iterator().hasNext()) {
            git.commit().setAll(true).setAuthor("system", "system@localhost").setMessage("initial commit").call();
            if (hasRemote)
                RepoHelper.push(repo, "origin", credentials);
        }
    }
    return new ConfigRepository(repo, hasRemote, credentials);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) ConfigRepository(com.peterphi.configuration.service.git.ConfigRepository) Repository(org.eclipse.jgit.lib.Repository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) InitCommand(org.eclipse.jgit.api.InitCommand) ConfigRepository(com.peterphi.configuration.service.git.ConfigRepository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) Named(com.google.inject.name.Named) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 17 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHook_IT method smokeTest_build_masterBranch.

@Test
public void smokeTest_build_masterBranch() throws Exception {
    // Instantiate the Gogs Handler object and wait for the server to be available
    GogsConfigHandler gogsServer = new GogsConfigHandler(GOGS_URL, GOGS_USER, GOGS_PASSWORD);
    gogsServer.waitForServer(12, 5);
    // Create the test repository on the server
    try {
        gogsServer.createEmptyRepo("testRep1");
    } catch (IOException e) {
        // check for the exist message;
        if (e.getMessage().contains("422")) {
            log.warn("GOGS Repo already exists. Trying to continue.");
        } else {
            fail("Unexpected error creating GOGS repo: " + e.getMessage());
        }
    }
    // initialize local Git repository used for tests
    File testRepoDir = new File("target/test-repos/demo-app");
    Git git = Git.init().setDirectory(testRepoDir).call();
    // Configure user, email, remote and tracking branch
    StoredConfig config = git.getRepository().getConfig();
    config.setString(CONFIG_USER_SECTION, null, "name", "Automated Test");
    config.setString(CONFIG_USER_SECTION, null, "email", "test@test.org");
    config.setString(CONFIG_REMOTE_SECTION, "origin", "url", "http://localhost:3000/butler/testRep1.git");
    config.setString(CONFIG_REMOTE_SECTION, "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
    config.setString(CONFIG_BRANCH_SECTION, "master", "remote", "origin");
    config.setString(CONFIG_BRANCH_SECTION, "master", "merge", "refs/heads/master");
    config.save();
    // add the files located there and commit them
    Status status = git.status().call();
    DirCache index = git.add().addFilepattern(".").call();
    RevCommit commit = git.commit().setMessage("Repos initialization").call();
    log.info("Commit" + commit.getName());
    // push
    UsernamePasswordCredentialsProvider user2 = new UsernamePasswordCredentialsProvider("butler", "butler");
    RefSpec spec = new RefSpec("refs/heads/master:refs/heads/master");
    Iterable<PushResult> resultIterable = git.push().setRemote("origin").setCredentialsProvider(user2).setRefSpecs(spec).call();
    // Setup the Jenkins job
    JenkinsServer jenkins = new JenkinsServer(new URI(JENKINS_URL), JENKINS_USER, JENKINS_PASSWORD);
    waitUntilJenkinsHasBeenStartedUp(jenkins);
    // Check if the job exist. If not create it.
    Job job = jenkins.getJob(JENKINS_JOB_NAME);
    if (job == null) {
        // Read the job configuration into a string
        File jenkinsConfigFile = new File(JENKINS_CONFIGS_PATH + "test-project.xml");
        byte[] encoded = Files.readAllBytes(jenkinsConfigFile.toPath());
        String configXml = new String(encoded, Charset.defaultCharset());
        jenkins.createJob(JENKINS_JOB_NAME, configXml);
    }
    // Get the expected build number
    JobWithDetails jobAtIntitalState = jenkins.getJob(JENKINS_JOB_NAME);
    int expectedBuildNbr = jobAtIntitalState.getNextBuildNumber();
    log.info("Next build number: " + expectedBuildNbr);
    // Build the job
    jobAtIntitalState.build();
    // Wait for the job to complete
    long timeOut = 60000L;
    waitForBuildToComplete(jenkins, expectedBuildNbr, timeOut);
    // Get the data we stored in the marker file and check it
    Properties markerAsProperty = loadMarkerArtifactAsProperty(jenkins);
    String buildedCommit = markerAsProperty.getProperty("GIT_COMMIT");
    assertEquals("Not the expected GIT commit", commit.getName(), buildedCommit);
    // add the trigger to Gogs
    File jsonCommandFile = new File(JSON_COMMANDFILE_PATH + "webHookDefinition.json");
    int hookId = gogsServer.createWebHook(jsonCommandFile, "testRep1");
    log.info("Created hook with ID " + hookId);
    // Get what is the next build number of the test jenkins job
    jobAtIntitalState = jenkins.getJob(JENKINS_JOB_NAME);
    expectedBuildNbr = jobAtIntitalState.getNextBuildNumber();
    // change the source file
    changeTheSourceFile("target/test-repos/demo-app/README.md");
    // commit and push the changed file
    git.add().addFilepattern(".").call();
    RevCommit commitForHook = git.commit().setMessage("Small test modification").call();
    log.info("Commit" + commitForHook.getName());
    git.push().setRemote("origin").setCredentialsProvider(user2).setRefSpecs(spec).call();
    // wait for the build
    waitForBuildToComplete(jenkins, expectedBuildNbr, timeOut);
    // Get the data we stored in the marker file and check it
    Properties hookMarkerAsProperty = loadMarkerArtifactAsProperty(jenkins);
    String hookBuildedCommit = hookMarkerAsProperty.getProperty("GIT_COMMIT");
    assertEquals("Not the expected GIT commit", commitForHook.getName(), hookBuildedCommit);
    // Cleanup - remove the hook we created
    gogsServer.removeHook("demoApp", hookId);
}
Also used : Status(org.eclipse.jgit.api.Status) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) PushResult(org.eclipse.jgit.transport.PushResult) JobWithDetails(com.offbytwo.jenkins.model.JobWithDetails) Properties(java.util.Properties) URI(java.net.URI) StoredConfig(org.eclipse.jgit.lib.StoredConfig) DirCache(org.eclipse.jgit.dircache.DirCache) Git(org.eclipse.jgit.api.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) Job(com.offbytwo.jenkins.model.Job) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 18 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project repairnator by Spirals-Team.

the class AbstractStep method lastPush.

private void lastPush() {
    if (RepairnatorConfig.getInstance().isPush() && this.getInspector().getJobStatus().getPushState() != PushState.NONE) {
        File sourceDir = new File(this.getInspector().getRepoLocalPath());
        File targetDir = new File(this.getInspector().getRepoToPushLocalPath());
        try {
            Git git = Git.open(targetDir);
            org.apache.commons.io.FileUtils.copyDirectory(sourceDir, targetDir, new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    return !pathname.toString().contains(".git") && !pathname.toString().contains(".m2") && !pathname.toString().contains(".travis.yml");
                }
            });
            git.add().addFilepattern(".").call();
            for (String fileToPush : this.getInspector().getJobStatus().getCreatedFilesToPush()) {
                // add force is not supported by JGit...
                ProcessBuilder processBuilder = new ProcessBuilder("git", "add", "-f", fileToPush).directory(git.getRepository().getDirectory().getParentFile()).inheritIO();
                try {
                    Process p = processBuilder.start();
                    p.waitFor();
                } catch (InterruptedException | IOException e) {
                    this.getLogger().error("Error while executing git command to add files: " + e);
                }
            }
            PersonIdent personIdent = new PersonIdent("Luc Esape", "luc.esape@gmail.com");
            git.commit().setMessage("End of the repairnator process").setAuthor(personIdent).setCommitter(personIdent).call();
            if (this.getInspector().getJobStatus().isHasBeenPushed()) {
                CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(this.config.getGithubToken(), "");
                git.push().setRemote(PushIncriminatedBuild.REMOTE_NAME).setCredentialsProvider(credentialsProvider).call();
            }
        } catch (GitAPIException | IOException e) {
            this.getLogger().error("Error while trying to commit last information for repairnator", e);
        }
    }
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) FileFilter(java.io.FileFilter) File(java.io.File)

Example 19 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider 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)

Example 20 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gitblit by gitblit.

the class GitServletTest method testPushToNonBareRepository.

@Test
public void testPushToNonBareRepository() throws Exception {
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/working/jgit", url));
    clone.setDirectory(jgit2Folder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    Git git = Git.open(jgit2Folder);
    File file = new File(jgit2Folder, "NONBARE");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit followed by push to non-bare repository").call();
    Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.REJECTED_OTHER_REASON, update.getStatus());
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Aggregations

UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)76 Git (org.eclipse.jgit.api.Git)47 File (java.io.File)34 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)23 IOException (java.io.IOException)19 Test (org.junit.Test)18 CloneCommand (org.eclipse.jgit.api.CloneCommand)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)15 RepositoryModel (com.gitblit.models.RepositoryModel)12 PushResult (org.eclipse.jgit.transport.PushResult)12 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)9 FileOutputStream (java.io.FileOutputStream)8 PushCommand (org.eclipse.jgit.api.PushCommand)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 BufferedWriter (java.io.BufferedWriter)7 OutputStreamWriter (java.io.OutputStreamWriter)7 Date (java.util.Date)7 Ref (org.eclipse.jgit.lib.Ref)7 Repository (org.eclipse.jgit.lib.Repository)7 URIish (org.eclipse.jgit.transport.URIish)7