Version Differences for Miscellaneous Scripts

(Alphabetization Almost Complete)
(Alphabetization Complete!)
Line 2308:
  // set the property    // set the property 
  workflow.getRequest().setProperty("resolve.status", status); </pre>    workflow.getRequest().setProperty("resolve.status", status); </pre> 
       
    + = Show Test Changes =  
    + This is designed to be a "Evaluate a Script" step that creates a report that goes under the Build's report tab.  
    + ==== AHPSCRIPTS-62 ====  
    + <pre>import com.urbancode.anthill3.domain.buildlife.BuildLife;  
    + import com.urbancode.anthill3.domain.jobtrace.JobTrace;  
    + import com.urbancode.anthill3.domain.test.TestCase;  
    + import com.urbancode.anthill3.domain.test.TestReport;  
    + import com.urbancode.anthill3.domain.test.TestReportFactory;  
    + import com.urbancode.anthill3.domain.test.TestSuite;  
    + import com.urbancode.anthill3.runtime.paths.PublishPathHelper;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.BuildLifeLookup;  
    + import com.urbancode.anthill3.runtime.scripting.helpers.JobTraceLookup;  
    + import com.urbancode.anthill3.services.template.TemplateHelper;  
    + import com.urbancode.commons.fileutils.FileUtils;  
    + import com.urbancode.devilfish.services.var.VarService;  
       
    + import java.io.BufferedWriter;  
    + import java.io.File;  
    + import java.io.FileWriter;  
    + import java.util.HashMap;  
    + import java.util.Iterator;  
    + import java.util.Map;  
       
    + try {  
    + // helper class for the tests  
    + class TestInstance {  
    + String key = null;  
    + String suite = null;  
    + String test = null;  
    + boolean success = false;  
    + int duration = 0;  
    + }  
       
    + // two maps to hold test information about this and previous build lives  
    + Map oldTestMap = new HashMap();  
    + Map newTestMap = new HashMap();  
       
    + TemplateHelper templateHelper = TemplateHelper.getInstance(null);  
       
    + // populate the sets  
    + BuildLife currentBuildLife = BuildLifeLookup.getCurrent();  
    + TestReport[] testReportArray = TestReportFactory.getInstance()  
    + .restoreAllForBuildLife(currentBuildLife);  
    + for (int i = 0; i < testReportArray.length; i++) {  
    + TestSuite[] testSuiteArray = testReportArray[i].getTestSuites();  
    + for (int j = 0; j < testSuiteArray.length; j++) {  
    + TestCase[] testCaseArray = testSuiteArray[j].getTestCases();  
    + for (int k = 0; k < testCaseArray.length; k++) {  
    + TestInstance testInstance = new TestInstance();  
    + testInstance.suite = testSuiteArray[j].getName();  
    + testInstance.test = testCaseArray[k].getName();  
    + testInstance.key =  
    + testInstance.test + testInstance.suite;  
    + testInstance.success = "success"  
    + .equalsIgnoreCase(testCaseArray[k].getResult());  
    + testInstance.duration = testCaseArray[k].getTime();  
    + newTestMap.put(testInstance.key, testInstance);  
    + }  
    + }  
    + }  
       
    + BuildLife lastSuccessBuildLife = BuildLifeLookup.mostRecentSuccess();  
    + testReportArray = TestReportFactory.getInstance()  
    + .restoreAllForBuildLife(lastSuccessBuildLife);  
    + for (int i = 0; i < testReportArray.length; i++) {  
    + TestSuite[] testSuiteArray = testReportArray[i].getTestSuites();  
    + for (int j = 0; j < testSuiteArray.length; j++) {  
    + TestCase[] testCaseArray = testSuiteArray[j].getTestCases();  
    + for (int k = 0; k < testCaseArray.length; k++) {  
    + TestInstance testInstance = new TestInstance();  
    + testInstance.suite = testSuiteArray[j].getName();  
    + testInstance.test = testCaseArray[k].getName();  
    + testInstance.key =  
    + testInstance.test + testInstance.suite;  
    + testInstance.success = "success"  
    + .equalsIgnoreCase(testCaseArray[k].getResult());  
    + testInstance.duration = testCaseArray[k].getTime();  
    + oldTestMap.put(testInstance.key, testInstance);  
    + }  
    + }  
    + }  
       
    + // get the path to the reports directory  
    + JobTrace jobTrace = JobTraceLookup.getCurrent();  
    + if (jobTrace != null) {  
    + String publishPath = VarService.getInstance()  
    + .resolve(PublishPathHelper.getInstance()  
    + .getPublishPath(jobTrace, "Test Changes"));  
       
    + FileUtils.assertDirectory(publishPath);  
       
    + // create the report  
    + File reportFile =  
    + new File(publishPath, "Test Differences.html");  
    + BufferedWriter writer =  
    + new BufferedWriter(new FileWriter(reportFile));  
       
    + writer.write("<HTML>\n" + "<HEAD>\n" +  
    + "<TITLE>Anthill3 - Test Diff Report</TITLE>\n" +  
    + "<link rel=\"StyleSheet\" href=\"/css/anthill3.css\" type=\"text/css\">" +  
    + "</head>\n" + "<BODY>\n" + "<TABLE class=\"data-table\" WIDTH=\"100%\">\n" +  
    + "\n" + "<TR><TD>\n" + "<H1>Test Diff Report</H1>\n" +  
    + "</TD></TR>\n");  
       
    + // output all tests that are new  
    + writer.write("<TR><TD>\n" +  
    + "<H2>New Tests:</H2>\n" +  
    + "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" +  
    + "<TH>Suite</TH>\n" + "<TH>Test</TH>\n");  
    + itr = newTestMap.keySet().iterator();  
    + while (itr.hasNext()) {  
    + String key = (String) itr.next();  
    + TestInstance newTest = (TestInstance) newTestMap.get(key);  
    + TestInstance oldTest = (TestInstance) oldTestMap.get(key);  
    + if (newTest != null && oldTest == null) {  
    + writer.write("<TR>");  
    + writer.write("<TD VALIGN=\"top\">" + newTest.suite +  
    + "</TD>");  
    + writer.write(  
    + "<TD VALIGN=\"top\">" + newTest.test + "</TD>");  
    + writer.write("</TR>");  
    + }  
    + }  
    + writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>");  
       
    + // output all tests failed this time but succeeded before  
    + writer.write("<TR><TD>\n" +  
    + "<H2>New Failing Tests:</H2>\n" +  
    + "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" +  
    + "<TH>Suite</TH>\n" + "<TH>Test</TH>\n");  
    + itr = newTestMap.keySet().iterator();  
    + while (itr.hasNext()) {  
    + String key = (String) itr.next();  
    + TestInstance newTest = (TestInstance) newTestMap.get(key);  
    + TestInstance oldTest = (TestInstance) oldTestMap.get(key);  
    + if (newTest != null && !newTest.success && (oldTest == null || oldTest.success)) {  
    + writer.write("<TR>");  
    + writer.write("<TD VALIGN=\"top\">" + newTest.suite +  
    + "</TD>");  
    + writer.write(  
    + "<TD VALIGN=\"top\">" + newTest.test + "</TD>");  
    + writer.write("</TR>");  
    + }  
    + }  
    + writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>");  
       
    + // output all tests that took more time  
    + writer.write("<TR><TD>\n" +  
    + "<H2>Tests that ran slower:</H2>\n" +  
    + "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" +  
    + "<TH>Suite</TH>\n" + "<TH>Test</TH>\n" +  
    + "<TH>Last Run Duration</TH>\n" +  
    + "<TH>Current Run Duration</TH>\n");  
    + Iterator itr = newTestMap.keySet().iterator();  
    + while (itr.hasNext()) {  
    + String key = (String) itr.next();  
    + TestInstance newTest = (TestInstance) newTestMap.get(key);  
    + TestInstance oldTest = (TestInstance) oldTestMap.get(key);  
    + if (newTest != null && oldTest != null &&  
    + newTest.duration > oldTest.duration*1.1) {  
    + writer.write("<TR>");  
    + writer.write("<TD VALIGN=\"top\">" + newTest.suite +  
    + "</TD>");  
    + writer.write(  
    + "<TD VALIGN=\"top\">" + newTest.test + "</TD>");  
    + writer.write("<TD VALIGN=\"top\" ALIGN=\"center\" >" +  
    + templateHelper.formatNumber(  
    + templateHelper.duration(  
    + oldTest.duration), "##0.0000") +  
    + " secs</TD>");  
    + writer.write("<TD VALIGN=\"top\" ALIGN=\"center\" >" +  
    + templateHelper.formatNumber(  
    + templateHelper.duration(  
    + newTest.duration), "##0.0000") +  
    + " secs</TD>");  
    + writer.write("</TR>");  
    + }  
    + }  
    + writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>");  
       
    + // output all tests that were fixed or are new and succeede  
    + writer.write("<TR><TD>\n" +  
    + "<H2>New or Newly Fixed Tests:</H2>\n" +  
    + "<table cellpadding=\"4\" cellspacing=\"1\" width=\"100%\">\n" +  
    + "<TH>Suite</TH>\n" + "<TH>Test</TH>\n");  
    + itr = newTestMap.keySet().iterator();  
    + while (itr.hasNext()) {  
    + String key = (String) itr.next();  
    + TestInstance newTest = (TestInstance) newTestMap.get(key);  
    + TestInstance oldTest = (TestInstance) oldTestMap.get(key);  
    + if (newTest != null && newTest.success && (oldTest == null || !oldTest.success)) {  
    + writer.write("<TR>");  
    + writer.write("<TD VALIGN=\"top\">" + newTest.suite +  
    + "</TD>");  
    + writer.write(  
    + "<TD VALIGN=\"top\">" + newTest.test + "</TD>");  
    + writer.write("</TR>");  
    + }  
    + }  
    + writer.write("</TABLE>\n" + "<BR/>\n" + "<BR/>\n" + "</TD></TR>");  
       
    + writer.write("</TABLE>\n" + "</BODY>\n" + "</HTML>");  
       
    + writer.flush();  
    + writer.close();  
    + }  
    + }  
    + catch (Exception e) {  
    + e.printStackTrace(commandOutput);  
    + }  
    + </pre>  
       
    + = Template for adding users to realms =  
       
    + Script Notes:  
    + * Treat this mostly as a snippet for doing something more interesting with.  
       
    + ==== AHPSCRIPTS-1 ====  
       
    + <pre>import com.urbancode.anthill3.domain.project.*;  
    + import com.urbancode.anthill3.domain.authentication.*;  
    + import com.urbancode.anthill3.domain.security.*;  
    + import com.urbancode.anthill3.domain.singleton.security.*;  
       
    + // Lookup Your roles  
    + buildRole = RoleFactory.getInstance().restoreForName("Build Master");  
    + userRole = RoleFactory.getInstance().restoreForName("User");  
       
    + // Look-up the LDAP realm here  
    + // This is a lookup by name, so insert the name of your LDAP realm.  
    + realm = AuthenticationRealmFactory.getInstance().restore("LDAP");  
       
    + // Do this for each user:  
    + User user1 = new User(true, realm);  
       
    + user1.setName("JaneDoe");  
    + user1.setPassword("Bogus");  
    + user1.addRole(userRole);  
    + user1.addRole(buildRole);  
    + user1.store();</pre>  
       
    + = Update Environment Properties for All Projects =  
    + Here's a modification that runs form within AnthillPro. The environment is selected to be the current running environment and the other values are read out of properties (the environment could come from a property as well).  
    + ==== AHPSCRIPTS-14 ====  
    + <pre>import com.urbancode.anthill3.main.client.AnthillClient;  
    + import com.urbancode.anthill3.persistence.UnitOfWork;  
    + import com.urbancode.anthill3.domain.project.*;  
    + import com.urbancode.anthill3.domain.project.envprops.*;  
    + import com.urbancode.anthill3.domain.servergroup.*;  
    + import java.util.*;  
       
       
    + variableName = PropertyLookup.get("variableName");  
    + newVariableValue = PropertyLookup.get("variableValue");  
    + boolean isSecure = Boolean.parseBoolean(PropertyLookup.get("isSecure"));  
    + environment = EnvironmentLookup.getCurrent();  
       
       
    + // Project  
    + Project[] projects = ProjectFactory.getInstance().restoreAll();  
       
    + // Loop through projects. If it has environment props for this, find the  
    + // right one and change it as appropriate.  
    + for (int i = 0; i < projects.length; i++) {  
    +  
    + ProjectEnvironmentProperty[] envPropArray = environment.getEnvironmentPropertiesForProject(projects[i]);  
    + boolean foundVariable = false;  
    + for (int j = 0; j < envPropArray.length; j++) {  
    + if (envPropArray[j].getName().equals(variableName)) {  
    + foundVariable = true;  
    + envPropArray[j].setValue(newVariableValue);  
    + envPropArray[j].store();  
    + }  
    + }  
    +  
    + if (!foundVariable) {  
    + ProjectEnvironmentProperty newProp = new ProjectEnvironmentProperty(true);  
    +  
    + newProp.setProject(projects[i]);  
    + newProp.setServerGroup(environment);  
    + newProp.setName(variableName);  
    + newProp.setValue(newVariableValue);  
    + newProp.setSetAsEnvProp(false);  
    + newProp.setSecureValue(isSecure);  
    +  
    + newProp.store();  
    + }  
    +  
    + }  
    + </pre>