(→Migrate All Workflows Using One Branch to Use Different Branch (CVS))
|
(→AHPSCRIPTS-74)
|
Line 753: | |||
////////////////////////////////////////</pre> | ////////////////////////////////////////</pre> | ||
+ | = Script to Add Dependencies to Projects Based on XML Input File = | ||
+ | *This script is pointed to an XML configuration file and it will take the file and configure the respective depencies in Anthill3. | ||
+ | ==== AHPSCRIPTS-71 ==== | ||
+ | <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.profile.*; | ||
+ | import com.urbancode.anthill3.domain.workflow.*; | ||
+ | import com.urbancode.anthill3.domain.status.*; | ||
+ | import com.urbancode.anthill3.runtime.scripting.helpers.*; | ||
+ | import com.urbancode.codestation2.domain.artifacts.*; | ||
+ | import com.urbancode.codestation2.domain.project.*; | ||
+ | import org.xml.sax.*; | ||
+ | import org.w3c.dom.*; | ||
+ | import java.io.*; | ||
+ | import java.util.*; | ||
+ | import javax.xml.parsers.*; | ||
+ | class ParsedProject { | ||
+ | String name = null; | ||
+ | String workflow = null; | ||
+ | List dependencies = new ArrayList(); | ||
+ | void addDependency(ParsedDependency dep) { | ||
+ | dependencies.add(dep); | ||
+ | } | ||
+ | } | ||
+ | class ParsedDependency { | ||
+ | String name = null; | ||
+ | String type = null; | ||
+ | String workflow = null; | ||
+ | String status = null; | ||
+ | String stamp = null; | ||
+ | List artifactSets = new ArrayList(); | ||
+ | void addArtifactSet(ParsedArtifactSet artifactSet) { | ||
+ | artifactSets.add(artifactSet); | ||
+ | } | ||
+ | boolean isAnthill() { | ||
+ | boolean result = false; | ||
+ | if ("Anthill".equals(type)) { | ||
+ | result = true; | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | boolean isCodestation() { | ||
+ | boolean result = false; | ||
+ | if ("Codestation".equals(type)) { | ||
+ | result = true; | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | } | ||
+ | class ParsedArtifactSet { | ||
+ | String name = null; | ||
+ | String destination = null; | ||
+ | } | ||
+ | Document loadDocument(InputStream in) | ||
+ | throws ParserConfigurationException, SAXException, IOException { | ||
+ | Document result = null; | ||
+ | try { | ||
+ | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
+ | DocumentBuilder builder = factory.newDocumentBuilder(); | ||
+ | result = builder.parse(new InputSource(in)); | ||
+ | } | ||
+ | finally { | ||
+ | if (in != null) { | ||
+ | try { | ||
+ | in.close(); | ||
+ | } catch (IOException e) {e.printStackTrace();} | ||
+ | } | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | List getChildElementList(Element element, String name) { | ||
+ | List result = new ArrayList(); | ||
+ | NodeList childList = element.getChildNodes(); | ||
+ | for (int i =0; i < childList.getLength(); i++) { | ||
+ | if (childList.item(i).getNodeName().equals(name)) { | ||
+ | result.add(childList.item(i)); | ||
+ | } | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | Element getFirstChild(Element element, String name) { | ||
+ | Element result = null; | ||
+ | List childList = getChildElementList(element, name); | ||
+ | if (childList.size() >= 1) { | ||
+ | result = (Element) childList.get(0); | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | void parseConnectionInfo(Element element) { | ||
+ | if (element != null) { | ||
+ | serverHost = element.getAttribute("hostname"); | ||
+ | String serverPortStr = element.getAttribute("port"); | ||
+ | serverPort = Integer.parseInt(serverPortStr); | ||
+ | userName = element.getAttribute("username"); | ||
+ | password = element.getAttribute("password"); | ||
+ | } | ||
+ | } | ||
+ | ParsedArtifactSet parseArtifactSet(Element element) { | ||
+ | ParsedArtifactSet result = null; | ||
+ | if (element != null) { | ||
+ | result = new ParsedArtifactSet(); | ||
+ | result.name = element.getAttribute("name"); | ||
+ | result.destination = element.getAttribute("destination"); | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | ParsedDependency parseDependency(Element element) { | ||
+ | ParsedDependency result = null; | ||
+ | if (element != null) { | ||
+ | result = new ParsedDependency(); | ||
+ | result.name = element.getAttribute("name"); | ||
+ | result.type = element.getAttribute("type"); | ||
+ | result.workflow = element.getAttribute("workflow"); | ||
+ | Element criteria = getFirstChild(element, "criteria"); | ||
+ | if (criteria != null) { | ||
+ | result.status = criteria.getAttribute("status"); | ||
+ | result.stamp = criteria.getAttribute("stamp"); | ||
+ | } | ||
+ | List artifactSetElements = getChildElementList(element, "artifact"); | ||
+ | for (int i =0; i < artifactSetElements.size(); i++) { | ||
+ | Element artifactSetElement = (Element) artifactSetElements.get(i); | ||
+ | result.addArtifactSet(parseArtifactSet(artifactSetElement)); | ||
+ | } | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | ParsedProject parseProject(Element element) { | ||
+ | ParsedProject result = null; | ||
+ | if (element != null) { | ||
+ | result = new ParsedProject(); | ||
+ | result.name = element.getAttribute("name"); | ||
+ | result.workflow = element.getAttribute("workflow"); | ||
+ | List dependencyElements = getChildElementList(element, "dependency"); | ||
+ | for (int i =0; i < dependencyElements.size(); i++) { | ||
+ | Element dependencyElement = (Element) dependencyElements.get(i); | ||
+ | result.addDependency(parseDependency(dependencyElement)); | ||
+ | } | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | ParsedProject[] parseProjects(List elements) { | ||
+ | ParsedProject[] result = new ParsedProject[elements.size()]; | ||
+ | for (int i =0; i < elements.size(); i++) { | ||
+ | Element element = (Element) elements.get(i); | ||
+ | result[i] = parseProject(element); | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | String fileName = bsh.args[0]; | ||
+ | String serverHost = null; | ||
+ | int serverPort = 0; | ||
+ | String userName = null; | ||
+ | String password = null; | ||
+ | Element rootElement = loadDocument(new FileInputStream(fileName)).getDocumentElement(); | ||
+ | parseConnectionInfo(rootElement); | ||
+ | ParsedProject[] parsedProjects = parseProjects(getChildElementList(rootElement, "project")); | ||
+ | print("Found " + parsedProjects.length + " Projects to add dependencies to."); | ||
+ | // obtain connection to the Anthill server | ||
+ | AnthillClient anthill = AnthillClient.connect(serverHost, serverPort, | ||
+ | userName, password); | ||
+ | // create a Unit of Work | ||
+ | UnitOfWork uow = anthill.createUnitOfWork(); | ||
+ | for (int i =0; i < parsedProjects.length; i++) { | ||
+ | ParsedProject parsedProject = parsedProjects[i]; | ||
+ | print("DependentProject:" + parsedProject.name + " DependentWorkflow:" + parsedProject.workflow); | ||
+ | Project dependentProject = ProjectFactory.getInstance().restoreForName(parsedProject.name); | ||
+ | if (dependentProject == null) { | ||
+ | throw new IllegalStateException("Dependent Project " + parsedProject.name + " was not found"); | ||
+ | } | ||
+ | Workflow dependentWorkflow = dependentProject.getWorkflow(parsedProject.workflow); | ||
+ | if (dependentWorkflow == null) { | ||
+ | throw new IllegalStateException("Dependent Wofklow " + parsedProject.workflow + " was not found"); | ||
+ | } | ||
+ | BuildProfile dependentBuildProfile = dependentWorkflow.getBuildProfile(); | ||
+ | AnthillProject dependentAnthillProject = new AnthillProject(dependentBuildProfile); | ||
+ | for (int j =0; j < parsedProject.dependencies.size(); j++) { | ||
+ | ParsedDependency parsedDependency = (ParsedDependency) parsedProject.dependencies.get(j); | ||
+ | if (parsedDependency.isCodestation()) { | ||
+ | print("\tCodestation DependencyName:" + parsedDependency.name); | ||
+ | } | ||
+ | else { | ||
+ | print("\tAnthill DependencyName:" + parsedDependency.name + " DependencyWorkflow:" + parsedDependency.workflow); | ||
+ | } | ||
+ | if (parsedDependency.stamp != null && | ||
+ | parsedDependency.stamp.length() > 0) { | ||
+ | print("\tStamp:" + parsedDependency.stamp); | ||
+ | } | ||
+ | if (parsedDependency.status != null && | ||
+ | parsedDependency.status.length() > 0) { | ||
+ | print("\tStatus:" + parsedDependency.status); | ||
+ | } | ||
+ | Dependency dep = new Dependency(); | ||
+ | CodestationCompatableProject csProject = null; | ||
+ | if (parsedDependency.isAnthill()) { | ||
+ | Project dependencyProject = ProjectFactory.getInstance().restoreForName(parsedDependency.name); | ||
+ | if (dependencyProject == null) { | ||
+ | throw new IllegalStateException("Anthill Dependency Project " + parsedDependency.name + " was not found"); | ||
+ | } | ||
+ | Workflow dependencyWorkflow = dependencyProject.getWorkflow(parsedDependency.workflow); | ||
+ | if (dependencyWorkflow == null) { | ||
+ | throw new IllegalStateException("Anthill Dependency Workflow " + parsedDependency.workflow + " was not found"); | ||
+ | } | ||
+ | BuildProfile dependencyBuildProfile = dependencyProject.getWorkflow(parsedDependency.workflow).getBuildProfile(); | ||
+ | csProject = new AnthillProject(dependencyBuildProfile); | ||
+ | dep.setBuildCondition(Dependency.PUSH_BUILD); | ||
+ | Status status = csProject.getStatusGroup().getStatus("success"); | ||
+ | if (status == null) { | ||
+ | throw new IllegalStateException("Anthill Dependency Project " + parsedDependency.name + " has no status success"); | ||
+ | } | ||
+ | dep.setStatus(status); | ||
+ | dep.setUsingExistingOnFail(false); | ||
+ | dep.setAlwaysForce(false); | ||
+ | dep.setCascadeForce(false); | ||
+ | } | ||
+ | else if (parsedDependency.isCodestation()) { | ||
+ | csProject = CodestationProjectFactory.getInstance().restoreForName(parsedDependency.name); | ||
+ | if (csProject == null) { | ||
+ | throw new IllegalStateException("Codestaion Dependency Project " + parsedDependency.name + " was not found"); | ||
+ | } | ||
+ | dep.setBuildCondition(Dependency.USE_EXISTING); | ||
+ | if (parsedDependency.status != null && | ||
+ | parsedDependency.status.length() > 0) { | ||
+ | Status status = csProject.getStatusGroup().getStatus(parsedDependency.status); | ||
+ | if (status == null) { | ||
+ | throw new IllegalStateException("Codestation Dependency Project " + parsedDependency.name + " has no status " + parsedDependency.status); | ||
+ | } | ||
+ | dep.setStatus(status); | ||
+ | } | ||
+ | else { | ||
+ | dep.setStatus(null); | ||
+ | } | ||
+ | if (parsedDependency.stamp != null && | ||
+ | parsedDependency.stamp.length() > 0) { | ||
+ | dep.setStampValue(parsedDependency.stamp); | ||
+ | } | ||
+ | else { | ||
+ | dep.setStampValue(null); | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | throw new IllegalArgumentException("No or invalid type specified. Anthill | Codestation"); | ||
+ | } | ||
+ | if (csProject != null) { | ||
+ | dep.setDependency(csProject); | ||
+ | for (int k =0; k < parsedDependency.artifactSets.size(); k++) { | ||
+ | ParsedArtifactSet parsedArtifactSet = (ParsedArtifactSet) parsedDependency.artifactSets.get(k); | ||
+ | print("\t\t ArtifactSet:" + parsedArtifactSet.name + " Destinaion Dir:" + parsedArtifactSet.destination); | ||
+ | String[] dirs = {parsedArtifactSet.destination}; | ||
+ | CodestationCompatableArtifactSet artifactSet = csProject.getArtifactSet(parsedArtifactSet.name); | ||
+ | if (artifactSet == null) { | ||
+ | throw new IllegalStateException("Dependency Project " + parsedDependency.name + " has no artifact set " + parsedArtifactSet.name); | ||
+ | } | ||
+ | dep.addSet2Dirs(artifactSet, dirs, false); | ||
+ | } | ||
+ | dependentBuildProfile.addDependency(dep); | ||
+ | dep.setDependent(dependentAnthillProject); | ||
+ | dep.store(); | ||
+ | } | ||
+ | else { | ||
+ | if (parsedDependency.isCodestation()) { | ||
+ | print("\tCould Not Find Codestation DependencyName:" + parsedDependency.name); | ||
+ | } | ||
+ | else { | ||
+ | print("\tCould Not Find Anthill DependencyName:" + parsedDependency.name + " DependencyWorkflow:" + parsedDependency.workflow); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | uow.commitAndClose();</pre> | ||
= Migrate All Workflows Using One Branch to Use Different Branch (CVS) = | = Migrate All Workflows Using One Branch to Use Different Branch (CVS) = | ||
This script changes the branch that is being checked out from cvs to be a different branch. This helps teams that are using a release branching strategy where the branch their workflows are building changes with each release and this change effects every workflow in the stack. | This script changes the branch that is being checked out from cvs to be a different branch. This helps teams that are using a release branching strategy where the branch their workflows are building changes with each release and this change effects every workflow in the stack. |