@Override public boolean execute(BuildCommand command, BuildSession buildSession) { throw new RuntimeException(command.getStringArg("message")); } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { String value = command.getStringArg("value"); String substitution = command.getStringArg("substitution"); if (value != null) { buildSession.addSecret(value, substitution); } return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { buildSession.reportBuildStatus(command.getStringArg("status")); return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { String type = command.getStringArg("type"); if ("tfs".equals(type)) { return tfsExecutor.execute(command, buildSession); } throw bomb(format("Don't know how to handle plugin of type: %s", type)); } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { File dir = buildSession.resolveRelativeDir(command.getWorkingDirectory(), command.getStringArg("path")); return dir.mkdirs(); } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { String line = command.getStringArg("line"); buildSession.println(buildSession.buildVariableSubstitute(line)); return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { if (command.hasArg("message")) { buildSession.println(command.getStringArg("message")); } return false; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { String url = command.getStringArg("url"); String username = command.getStringArg("username"); String password = command.getStringArg("password"); String domain = command.getStringArg("domain"); String projectPath = command.getStringArg("projectPath"); String revision = command.getStringArg("revision"); File workingDir = buildSession.resolveRelativeDir(command.getWorkingDirectory()); ConsoleOutputStreamConsumer consoleOutputStreamConsumer = new LabeledOutputStreamConsumer(TaggedStreamConsumer.PREP, TaggedStreamConsumer.PREP_ERR, buildSession.processOutputStreamConsumer()); RevisionContext revisionContext = new RevisionContext(new StringRevision(revision)); AgentSubprocessExecutionContext execCtx = new AgentSubprocessExecutionContext(buildSession.getAgentIdentifier(), workingDir.getAbsolutePath()); createMaterial(url, username, password, domain, projectPath).updateTo(consoleOutputStreamConsumer, workingDir, revisionContext, execCtx); return true; }
private void createBuildCommand() { buildCommand = mock(BuildCommand.class); when(buildCommand.getStringArg("url")).thenReturn("some url"); when(buildCommand.getStringArg("username")).thenReturn("username"); when(buildCommand.getStringArg("password")).thenReturn("password"); when(buildCommand.getStringArg("domain")).thenReturn("domain"); when(buildCommand.getStringArg("projectPath")).thenReturn("project path"); when(buildCommand.getStringArg("revision")).thenReturn("revision1"); when(buildCommand.getWorkingDirectory()).thenReturn("working dir"); } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { String propertyName = command.getStringArg("name"); File file = buildSession.resolveRelativeDir(command.getWorkingDirectory(), command.getStringArg("src")); String xpath = command.getStringArg("xpath"); String indent = " "; if (!file.exists()) { buildSession.println(format("%sFailed to create property %s. File %s does not exist.", indent, propertyName, file.getAbsolutePath())); return true; } try { if (!XpathUtils.nodeExists(file, xpath)) { buildSession.println(format("%sFailed to create property %s. Nothing matched xpath \"%s\" in the file: %s.", indent, propertyName, xpath, file.getAbsolutePath())); } else { String value = XpathUtils.evaluate(file, xpath); buildSession.getPublisher().setProperty(new Property(propertyName, value)); buildSession.println(format("%sProperty %s = %s created." + "\n", indent, propertyName, value)); } } catch (Exception e) { String error = (e instanceof XPathExpressionException) ? (format("Illegal xpath: \"%s\"", xpath)) : ExceptionUtils.messageOf(e); String message = format("%sFailed to create property %s. %s", indent, propertyName, error); buildSession.getPublisher().reportErrorMessage(message, e); } return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { String name = command.getStringArg("name"); if (!command.hasArg("value")) { String displayValue = buildSession.getEnvs().get(name); buildSession.printlnSafely(format("[%s] setting environment variable '%s' to value '%s'", GoConstants.PRODUCT_NAME, name, displayValue)); return true; } String value = command.getStringArg("value"); boolean secure = command.getBooleanArg("secure"); String displayValue = secure ? EnvironmentVariableContext.EnvironmentVariable.MASK_VALUE : value; Set<String> processLevelEnvs = ProcessManager.getInstance().environmentVariableNames(); if (buildSession.getEnvs().containsKey(name) || processLevelEnvs.contains(name)) { buildSession.printlnSafely(format("[%s] overriding environment variable '%s' with value '%s'", GoConstants.PRODUCT_NAME, name, displayValue)); } else { buildSession.printlnSafely(format("[%s] setting environment variable '%s' to value '%s'", GoConstants.PRODUCT_NAME, name, displayValue)); } buildSession.setEnv(name, value); return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { URLService urlService = new URLService(); String url = urlService.prefixPartialUrl(command.getStringArg("url")); String dest = command.getStringArg("dest"); String src = command.getStringArg("src"); String checksumUrl = null; ChecksumFileHandler checksumFileHandler = null; if (command.hasArg("checksumUrl")) { checksumUrl = new URLService().prefixPartialUrl(command.getStringArg("checksumUrl")); File checksumFile; if (command.hasArg("checksumFile")) { checksumFile = buildSession.resolveRelativeDir(command.getWorkingDirectory(), command.getStringArg("checksumFile")); } else { checksumFile = TempFiles.createUniqueFile("checksum"); } checksumFileHandler = new ChecksumFileHandler(checksumFile); } DirHandler handler = new DirHandler(src, buildSession.resolveRelativeDir(command.getWorkingDirectory(), dest)); buildSession.download(handler, url, checksumFileHandler, checksumUrl); return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { File workingDirectory = buildSession.resolveRelativeDir(command.getWorkingDirectory()); String uploadPath = command.getStringArg("uploadPath"); String[] sources = command.getArrayArg("srcs"); ArrayList<File> allFiles = findMatchedSourceFiles(buildSession, workingDirectory, sources); if (allFiles.size() > 0) { File tempFolder = null; try { tempFolder = FileUtil.createTempFolder(); File testResultSource = new File(tempFolder, "result"); testResultSource.mkdirs(); UnitTestReportGenerator generator = new UnitTestReportGenerator(buildSession.getPublisher(), testResultSource); generator.generate(allFiles.toArray(new File[allFiles.size()]), uploadPath); } finally { if (tempFolder != null) { FileUtils.deleteQuietly(tempFolder); } } } else { String message = "No files were found in the Test Results folders"; buildSession.printlnWithPrefix(message); LOG.warn(message); } return true; }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { final String src = command.getStringArg("src"); final String dest = command.getStringArg("dest"); final Boolean ignoreUnmatchError = command.getBooleanArg("ignoreUnmatchError"); final File rootPath = buildSession.resolveRelativeDir(command.getWorkingDirectory()); WildcardScanner scanner = new WildcardScanner(rootPath, src); File[] files = scanner.getFiles(); if (files.length == 0) { String message = "The rule [" + src + "] cannot match any resource under [" + rootPath + "]"; buildSession.printlnWithPrefix(message); return ignoreUnmatchError; } for (File file : files) { buildSession.upload(file, destURL(rootPath, file, src, dest)); } return true; }
@Test public void shouldNotExecuteATfsExecutorIfPluginTypeIsNotTfs() throws Exception { when(buildCommand.getStringArg("type")).thenReturn("non-tfs type"); try { pluginCommandExecutor.execute(buildCommand, buildSession); fail("Should have failed since this type in not understood"); } catch (Exception e) { assertThat(e.getMessage(), containsString("Don't know how to handle plugin of type: non-tfs type")); } }
@Test public void shouldExecuteATfsExecutorIfPluginTypeIsTfs() throws Exception { when(buildCommand.getStringArg("type")).thenReturn("tfs"); when(tfsExecutor.execute(buildCommand, buildSession)).thenReturn(true); boolean result = pluginCommandExecutor.execute(buildCommand, buildSession); verify(tfsExecutor).execute(buildCommand, buildSession); assertThat(result, is(true)); } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { File dir = buildSession.resolveRelativeDir(command.getWorkingDirectory(), command.getStringArg("path")); String[] allowed = command.getArrayArg("allowed"); if (allowed.length == 0) { try { FileUtils.cleanDirectory(dir); } catch (IOException e) { return false; } } else { DirectoryCleaner cleaner = new DirectoryCleaner(dir, buildSession.processOutputStreamConsumer()); cleaner.allowed(allowed); cleaner.clean(); } return true; } }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { File workingDir = buildSession.resolveRelativeDir(command.getWorkingDirectory()); if (!workingDir.isDirectory()) { String message = "Working directory \"" + workingDir.getAbsolutePath() + "\" is not a directory!"; LOG.error(message); buildSession.println(message); return false; } String cmd = command.getStringArg("command"); String[] args = command.getArrayArg("args"); Map<String, String> secrets = buildSession.getSecretSubstitutions(); Set<String> leftSecrets = new HashSet<>(secrets.keySet()); CommandLine commandLine = createCommandLine(cmd, buildSession.getConsoleLogCharset()); for (String arg : args) { if(secrets.containsKey(arg)) { leftSecrets.remove(arg); commandLine.withArg(new SubstitutableCommandArgument(arg, secrets.get(arg))); } else { commandLine.withArg(arg); } } for (String secret: leftSecrets) { commandLine.withNonArgSecret(new SecretSubstitution(secret, secrets.get(secret))); } commandLine.withWorkingDir(workingDir); commandLine.withEnv(buildSession.getEnvs()); commandLine.withEnv(command.getCommandEnvVars()); return executeCommandLine(buildSession, commandLine, command.getExecInput()) == 0; }
@Test public void shouldGenerateCleanupCommandForRemovingJunkFoldersWhenCleanUpIsCalled_hasOneMaterialUseBaseFolderReturnsFalse() throws Exception { Materials materials = new Materials(); GitMaterial gitMaterial = new GitMaterial("http://some-url.com", "some-branch", "some-folder"); materials.add(gitMaterial); BuildCommand command = materials.cleanUpCommand("basedir"); assertThat(command.getName(), is("cleandir")); assertThat(command.getStringArg("path"), is("basedir")); assertThat(command.getArrayArg("allowed"), is(new String[]{"some-folder", "cruise-output"})); }
@Test public void testGetArgs() { assertThat(new BuildCommand("foo", map("foo", new Gson().toJson(new String[]{"arg1", "arg2"}))).getArrayArg("foo"), is(new String[]{"arg1","arg2"})); assertThat(new BuildCommand("foo", map("foo", "true")).getBooleanArg("foo"), is(true)); assertThat(new BuildCommand("foo", map("foo", "true")).getBooleanArg("bar"), is(false)); assertThat(new BuildCommand("foo", map("foo", "bar")).getStringArg("foo"), is("bar")); }