use of org.eclipse.jgit.api.RemoteAddCommand 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.api.RemoteAddCommand in project archi-modelrepository-plugin by archi-contribs.
the class ArchiRepository method createNewLocalGitRepository.
@Override
public Git createNewLocalGitRepository(String URL) throws GitAPIException, IOException, URISyntaxException {
if (getLocalRepositoryFolder().exists() && getLocalRepositoryFolder().list().length > 0) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("Directory: " + getLocalRepositoryFolder().getAbsolutePath() + " is not empty.");
}
InitCommand initCommand = Git.init();
initCommand.setDirectory(getLocalRepositoryFolder());
Git git = initCommand.call();
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName(IGraficoConstants.ORIGIN);
remoteAddCommand.setUri(new URIish(URL));
remoteAddCommand.call();
setDefaultConfigSettings(git.getRepository());
// Set tracked master branch
setTrackedBranch(git.getRepository(), IGraficoConstants.MASTER);
return git;
}
Aggregations