Search in sources :

Example 6 with RemoteAddCommand

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

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;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Git(org.eclipse.jgit.api.Git) InitCommand(org.eclipse.jgit.api.InitCommand) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) IOException(java.io.IOException)

Aggregations

RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)7 URIish (org.eclipse.jgit.transport.URIish)7 IOException (java.io.IOException)4 Git (org.eclipse.jgit.api.Git)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)2 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)1 User (hudson.model.User)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)1 FetchCommand (org.eclipse.jgit.api.FetchCommand)1 InitCommand (org.eclipse.jgit.api.InitCommand)1 MergeResult (org.eclipse.jgit.api.MergeResult)1 RemoteRemoveCommand (org.eclipse.jgit.api.RemoteRemoveCommand)1