Search in sources :

Example 46 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project MGit by maks.

the class RepoOpTask method setCredentials.

protected void setCredentials(TransportCommand command) {
    String username = mRepo.getUsername();
    String password = mRepo.getPassword();
    if (username != null && password != null && !username.trim().isEmpty() && !password.trim().isEmpty()) {
        UsernamePasswordCredentialsProvider auth = new UsernamePasswordCredentialsProvider(username, password);
        command.setCredentialsProvider(auth);
    } else {
        Timber.d("no CredentialsProvider when no username/password provided");
    }
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)

Example 47 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryTests method usernamePasswordShouldSetCredentials.

@Test
public void usernamePasswordShouldSetCredentials() throws Exception {
    Git mockGit = mock(Git.class);
    MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
    JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment, new JGitEnvironmentProperties());
    envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
    envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
    envRepository.setBasedir(new File("./mybasedir"));
    final String username = "someuser";
    final String password = "mypassword";
    envRepository.setUsername(username);
    envRepository.setPassword(password);
    envRepository.setCloneOnStart(true);
    envRepository.afterPropertiesSet();
    assertTrue(mockCloneCommand.getCredentialsProvider() instanceof UsernamePasswordCredentialsProvider);
    CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
    CredentialItem.Username usernameCredential = new CredentialItem.Username();
    CredentialItem.Password passwordCredential = new CredentialItem.Password();
    assertTrue(provider.supports(usernameCredential));
    assertTrue(provider.supports(passwordCredential));
    provider.get(new URIish(), usernameCredential);
    assertEquals(usernameCredential.getValue(), username);
    provider.get(new URIish(), passwordCredential);
    assertEquals(String.valueOf(passwordCredential.getValue()), password);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialItem(org.eclipse.jgit.transport.CredentialItem) Matchers.anyString(org.mockito.Matchers.anyString) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) PassphraseCredentialsProvider(org.springframework.cloud.config.server.support.PassphraseCredentialsProvider) Git(org.eclipse.jgit.api.Git) File(java.io.File) Test(org.junit.Test)

Example 48 with UsernamePasswordCredentialsProvider

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

the class CommitPatch method businessExecute.

@Override
protected void businessExecute() {
    if (RepairnatorConfig.getInstance().isPush()) {
        this.getLogger().info("Start the step push patch");
        File sourceDir = new File(this.getInspector().getRepoLocalPath());
        File targetDir = new File(this.getInspector().getRepoToPushLocalPath());
        try {
            Git git = Git.open(targetDir);
            Ref oldHeadRef = git.getRepository().exactRef("HEAD");
            RevWalk revWalk = new RevWalk(git.getRepository());
            RevCommit headRev = revWalk.parseCommit(oldHeadRef.getObjectId());
            revWalk.dispose();
            FileUtils.copyDirectory(sourceDir, targetDir, new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    return !pathname.toString().contains(".git") && !pathname.toString().contains(".m2");
                }
            });
            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);
                }
            }
            String commitMsg;
            if (pushHumanPatch) {
                commitMsg = "Human patch from the following repository " + this.getInspector().getRepoSlug() + "\n";
                Metrics metrics = this.getInspector().getJobStatus().getMetrics();
                commitMsg += "This commit is a reflect of the following : " + metrics.getPatchCommitUrl() + ".";
            } else {
                commitMsg = "Automatic repair information (optionally automatic patches).";
            }
            PersonIdent personIdent = new PersonIdent("Luc Esape", "luc.esape@gmail.com");
            RevCommit commit = git.commit().setMessage(commitMsg).setAuthor(personIdent).setCommitter(personIdent).call();
            this.computePatchStats(git, headRev, commit);
            if (this.getInspector().getJobStatus().isHasBeenPushed()) {
                CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(this.getConfig().getGithubToken(), "");
                git.push().setRemote(PushIncriminatedBuild.REMOTE_NAME).setCredentialsProvider(credentialsProvider).call();
            }
            if (pushHumanPatch) {
                this.setPushState(PushState.PATCH_COMMITTED);
            } else {
                this.setPushState(PushState.REPAIR_INFO_COMMITTED);
            }
            return;
        } catch (IOException e) {
            this.addStepError("Error while copying the folder to prepare the git repository.", e);
        } catch (GitAPIException e) {
            this.addStepError("Error while opening the git repository, maybe it has not been initialized yet.", e);
        }
        if (pushHumanPatch) {
            this.setPushState(PushState.PATCH_COMMITTED);
        } else {
            this.setPushState(PushState.REPAIR_INFO_COMMITTED);
        }
    } else {
        this.getLogger().info("Repairnator is configured to not push anything. This step will be bypassed.");
    }
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) RevWalk(org.eclipse.jgit.revwalk.RevWalk) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) Metrics(fr.inria.spirals.repairnator.process.inspectors.Metrics) Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) FileFilter(java.io.FileFilter) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 49 with UsernamePasswordCredentialsProvider

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

the class PushIncriminatedBuild method businessExecute.

@Override
protected void businessExecute() {
    if (this.getConfig().isPush()) {
        if (this.remoteRepoUrl == null || this.remoteRepoUrl.equals("")) {
            this.getLogger().error("Remote repo should be set !");
            this.setPushState(PushState.REPO_NOT_PUSHED);
            return;
        }
        String remoteRepo = this.remoteRepoUrl + REMOTE_REPO_EXT;
        this.getLogger().debug("Start to push failing pipelineState in the remote repository: " + remoteRepo + " branch: " + branchName);
        if (this.getConfig().getGithubToken() == null || this.getConfig().getGithubToken().equals("")) {
            this.getLogger().warn("You must the GITHUB_OAUTH env property to push incriminated build.");
            this.setPushState(PushState.REPO_NOT_PUSHED);
            return;
        }
        try {
            Git git = Git.open(new File(inspector.getRepoToPushLocalPath()));
            this.getLogger().debug("Add the remote repository to push the current pipelineState");
            RemoteAddCommand remoteAdd = git.remoteAdd();
            remoteAdd.setName(REMOTE_NAME);
            remoteAdd.setUri(new URIish(remoteRepo));
            remoteAdd.call();
            CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(this.getConfig().getGithubToken(), "");
            this.getLogger().debug("Check if a branch already exists in the remote repository");
            ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "git remote show " + REMOTE_NAME + " | grep " + branchName).directory(new File(this.inspector.getRepoLocalPath()));
            Process p = processBuilder.start();
            BufferedReader stdin = new BufferedReader(new InputStreamReader(p.getInputStream()));
            p.waitFor();
            this.getLogger().debug("Get result from grep process...");
            String processReturn = "";
            String line;
            while (stdin.ready() && (line = stdin.readLine()) != null) {
                processReturn += line;
            }
            if (!processReturn.equals("")) {
                this.getLogger().warn("A branch already exist in the remote repo with the following name: " + branchName);
                this.getLogger().debug("Here the grep return: " + processReturn);
                return;
            }
            this.getLogger().debug("Prepare the branch and push");
            Ref branch = git.checkout().setCreateBranch(true).setName(branchName).call();
            git.push().setRemote(REMOTE_NAME).add(branch).setCredentialsProvider(credentialsProvider).call();
            this.getInspector().getJobStatus().setHasBeenPushed(true);
            this.getInspector().getJobStatus().setGitBranchUrl(this.remoteRepoUrl + "/tree/" + branchName);
            this.setPushState(PushState.REPO_PUSHED);
            return;
        } catch (IOException e) {
            this.getLogger().error("Error while reading git directory at the following location: " + inspector.getRepoLocalPath() + " : " + e);
            this.addStepError(e.getMessage());
        } catch (URISyntaxException e) {
            this.getLogger().error("Error while setting remote repository with following URL: " + remoteRepo + " : " + e);
            this.addStepError(e.getMessage());
        } catch (GitAPIException e) {
            this.getLogger().error("Error while executing a JGit operation: " + e);
            this.addStepError(e.getMessage());
        } catch (InterruptedException e) {
            this.addStepError("Error while executing git command to check branch presence" + e.getMessage());
        }
        this.setPushState(PushState.REPO_NOT_PUSHED);
    } else {
        this.getLogger().info("The push argument is set to false. Nothing will be pushed.");
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) InputStreamReader(java.io.InputStreamReader) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 50 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project fabric8 by jboss-fuse.

the class DummyBatchingProgressMonitor method getCredentialsProvider.

private CredentialsProvider getCredentialsProvider() {
    Map<String, String> properties = getDataStoreProperties();
    String username;
    String password;
    RuntimeProperties sysprops = runtimeProperties.get();
    username = ZooKeeperUtils.getContainerLogin(sysprops);
    password = ZooKeeperUtils.generateContainerToken(sysprops, curator.get());
    if (isExternalGitConfigured(properties)) {
        username = getExternalUser(properties);
        password = getExternalCredential(properties);
    }
    return new UsernamePasswordCredentialsProvider(username, password);
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) RuntimeProperties(io.fabric8.api.RuntimeProperties)

Aggregations

UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)75 Git (org.eclipse.jgit.api.Git)47 File (java.io.File)33 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)23 IOException (java.io.IOException)18 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 RefSpec (org.eclipse.jgit.transport.RefSpec)7