Search in sources :

Example 1 with CommandOutputHandler

use of com.atlassian.stash.scm.CommandOutputHandler in project stashbot by palantir.

the class MockGitCommandBuilderFactory method reset.

@SuppressWarnings("unchecked")
private void reset() {
    // list of changesets in order
    changesets = new ArrayList<String>();
    blacklistedChangesets = new HashSet<String>();
    // for each hash, list of branches that contain said hash
    branchMap = new HashMap<String, List<String>>();
    gcbf = Mockito.mock(GitCommandBuilderFactory.class);
    grlb = Mockito.mock(GitRevListBuilder.class);
    gscb = Mockito.mock(GitScmCommandBuilder.class);
    branchCommandBuilder = Mockito.mock(GitScmCommandBuilder.class);
    cmd = Mockito.mock(GitCommand.class);
    branchCommand = Mockito.mock(GitCommand.class);
    // RevList cmd
    Mockito.when(gcbf.builder()).thenReturn(gscb);
    Mockito.when(gcbf.builder(Mockito.any(Repository.class))).thenReturn(gscb);
    Mockito.when(gscb.revList()).thenReturn(grlb);
    final ArgumentCaptor<CommandOutputHandler<Object>> cohCaptor = (ArgumentCaptor<CommandOutputHandler<Object>>) (Object) ArgumentCaptor.forClass(CommandOutputHandler.class);
    Mockito.when(grlb.build(cohCaptor.capture())).thenReturn(cmd);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CommandOutputHandler<Object> coh = cohCaptor.getValue();
            List<String> finalCS = new ArrayList<String>();
            for (String cs : changesets) {
                if (!blacklistedChangesets.contains(cs)) {
                    finalCS.add(cs);
                }
            }
            String str = StringUtils.join(finalCS, "\n") + "\n";
            InputStream is = new ByteArrayInputStream(str.getBytes());
            coh.process(is);
            return null;
        }
    }).when(cmd).call();
    // Branch cmd - returns list of all branches
    final ArgumentCaptor<CommandOutputHandler<Object>> branchCOHCaptor = (ArgumentCaptor<CommandOutputHandler<Object>>) (Object) ArgumentCaptor.forClass(CommandOutputHandler.class);
    Mockito.when(gscb.command("branch")).thenReturn(branchCommandBuilder);
    Mockito.when(branchCommandBuilder.build(branchCOHCaptor.capture())).thenReturn(branchCommand);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CommandOutputHandler<Object> coh = branchCOHCaptor.getValue();
            String output = "";
            List<String> branches = new ArrayList<String>(branchMap.keySet());
            for (String branch : branches) {
                output = output + "  " + branch + "\n";
            }
            InputStream is = new ByteArrayInputStream(output.getBytes());
            coh.process(is);
            return null;
        }
    }).when(branchCommand).call();
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GitScmCommandBuilder(com.atlassian.stash.scm.git.GitScmCommandBuilder) GitRevListBuilder(com.atlassian.stash.scm.git.revlist.GitRevListBuilder) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler) GitCommand(com.atlassian.stash.scm.git.GitCommand) Repository(com.atlassian.stash.repository.Repository) ByteArrayInputStream(java.io.ByteArrayInputStream) GitCommandBuilderFactory(com.atlassian.stash.scm.git.GitCommandBuilderFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with CommandOutputHandler

use of com.atlassian.stash.scm.CommandOutputHandler in project stashbot by palantir.

the class CommandOutputHandlerFactory method getBranchContainsOutputHandler.

/**
 * Returns an output handler which provides an ImmutableList of branches in the form "refs/heads/foo" when the
 * command prints out a list of the form "XXfoo\n" where X doesn't matter.
 *
 * @return ImmutableList<String> branchNames
 */
public CommandOutputHandler<Object> getBranchContainsOutputHandler() {
    return new CommandOutputHandler<Object>() {

        private ArrayList<String> branches;

        private LineReader lr;

        private boolean processed = false;

        @Override
        public void complete() throws ProcessException {
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public Object getOutput() {
            if (processed == false) {
                throw new IllegalStateException("getOutput() called before process()");
            }
            return ImmutableList.copyOf(branches);
        }

        @Override
        public void process(InputStream output) throws ProcessException {
            processed = true;
            if (branches == null) {
                branches = new ArrayList<String>();
                lr = new LineReader(new InputStreamReader(output));
            }
            String branch = "";
            while (branch != null) {
                try {
                    branch = lr.readLine();
                } catch (IOException e) {
                    // dear god, why is this happening?
                    throw new RuntimeException(e);
                }
                // format is "  somebranch\n* branchIamOn\n  someOtherBranch"
                if (branch != null) {
                    branches.add("refs/heads/" + branch.substring(2));
                }
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Watchdog(com.atlassian.utils.process.Watchdog) InputStream(java.io.InputStream) LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler)

Example 3 with CommandOutputHandler

use of com.atlassian.stash.scm.CommandOutputHandler in project stash-codesearch-plugin by palantir.

the class GitVersionValidator method validateVersion.

private String validateVersion(GitScm gitScm) {
    GitCommandBuilderFactory gcbf = gitScm.getCommandBuilderFactory();
    CommandOutputHandler<String> coh = new CommandOutputHandler<String>() {

        private final StringBuilder sb = new StringBuilder();

        private boolean finished = false;

        @Override
        public void process(InputStream output) throws ProcessException {
            final LineReader lr = new LineReader(new InputStreamReader(output));
            String line = null;
            try {
                while ((line = lr.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error while determining git version:", e);
            }
        }

        @Override
        public void complete() throws ProcessException {
            finished = true;
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public String getOutput() {
            if (!finished) {
                throw new IllegalStateException("Called getOutput() before complete()");
            }
            return sb.toString();
        }
    };
    Command<String> versionCommand = gcbf.builder().version(coh);
    versionCommand.call();
    String versionString = coh.getOutput();
    // version string looks like this: git version 1.7.9.5
    String versionNumber = versionString.split(" ")[2];
    String[] versionParts = versionNumber.split("\\.");
    int min = (versionParts.length <= REQUIRED_GIT_VERSION.length) ? versionParts.length : REQUIRED_GIT_VERSION.length;
    for (int i = 0; i < min; ++i) {
        try {
            int test = Integer.parseInt(versionParts[i]);
            if (test > REQUIRED_GIT_VERSION[i]) {
                return versionNumber;
            } else if (test < REQUIRED_GIT_VERSION[i]) {
                throw new IllegalStateException("Invalid Git Version '" + versionNumber + "', must be equal to or greater than '" + REQUIRED_GIT_VERSION_STRING + "'");
            }
        // else: keep checking next number
        } catch (NumberFormatException e) {
            throw new RuntimeException("Error while determining git version:", e);
        }
    }
    // if we get here, we had a perfect match
    return versionNumber;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler) Watchdog(com.atlassian.utils.process.Watchdog) GitCommandBuilderFactory(com.atlassian.stash.scm.git.GitCommandBuilderFactory) LineReader(com.google.common.io.LineReader)

Example 4 with CommandOutputHandler

use of com.atlassian.stash.scm.CommandOutputHandler in project stashbot by palantir.

the class CommandOutputHandlerFactory method getRevlistOutputHandler.

public CommandOutputHandler<Object> getRevlistOutputHandler() {
    return new CommandOutputHandler<Object>() {

        private ArrayList<String> changesets;

        private LineReader lr;

        private boolean processed = false;

        @Override
        public void complete() throws ProcessException {
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public Object getOutput() {
            if (processed == false) {
                throw new IllegalStateException("getOutput() called before process()");
            }
            return ImmutableList.copyOf(changesets).reverse();
        }

        @Override
        public void process(InputStream output) throws ProcessException {
            processed = true;
            if (changesets == null) {
                changesets = new ArrayList<String>();
                lr = new LineReader(new InputStreamReader(output));
            }
            String sha1 = "";
            while (sha1 != null) {
                try {
                    sha1 = lr.readLine();
                } catch (IOException e) {
                    // dear god, why is this happening?
                    throw new RuntimeException(e);
                }
                if (sha1 != null && sha1.matches("[0-9a-fA-F]{40}")) {
                    changesets.add(sha1);
                }
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Watchdog(com.atlassian.utils.process.Watchdog) InputStream(java.io.InputStream) LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler)

Aggregations

CommandOutputHandler (com.atlassian.stash.scm.CommandOutputHandler)4 InputStream (java.io.InputStream)4 Watchdog (com.atlassian.utils.process.Watchdog)3 LineReader (com.google.common.io.LineReader)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 ArrayList (java.util.ArrayList)3 GitCommandBuilderFactory (com.atlassian.stash.scm.git.GitCommandBuilderFactory)2 Repository (com.atlassian.stash.repository.Repository)1 GitCommand (com.atlassian.stash.scm.git.GitCommand)1 GitScmCommandBuilder (com.atlassian.stash.scm.git.GitScmCommandBuilder)1 GitRevListBuilder (com.atlassian.stash.scm.git.revlist.GitRevListBuilder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 List (java.util.List)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1