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