(→AHPSCRIPTS-77)
|
(→AHPSCRIPTS-76)
|
Line 557: | |||
//Close unit of work | //Close unit of work | ||
uow.close(); </pre> | uow.close(); </pre> | ||
+ | = Create a New Originating Workflow For Mass Branching or Projects (CVS) = | ||
+ | This script will go through all of the Projects and search for Projects Using an existing Branch and Create a new Originating Workflow which points to the new Branch. This script takes a listing of Projects and as well as a Branch Name and optionally a Workflow Name if you are not naming yours based on the BranchNameBuild convention. | ||
+ | ==== AHPSCRIPTS-75 ==== | ||
+ | <pre>import com.urbancode.anthill3.main.client.AnthillClient; | ||
+ | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
+ | import com.urbancode.anthill3.domain.profile.*; | ||
+ | import com.urbancode.anthill3.domain.project.*; | ||
+ | import com.urbancode.anthill3.domain.workflow.*; | ||
+ | import com.urbancode.anthill3.domain.source.cvs.CvsSourceConfig; | ||
+ | import com.urbancode.codestation2.domain.project.*; | ||
+ | import java.util.*; | ||
+ | List getProjectsForNameList(String projectListStr) { | ||
+ | List result = new ArrayList(); | ||
+ | String[] projectNames = projectListStr.split(","); | ||
+ | for (int i = 0; i < projectNames.length; i++) { | ||
+ | Project project = ProjectFactory.getInstance().restoreForName(projectNames[i].trim()); | ||
+ | //Verify that the project exists | ||
+ | if (project == null) { | ||
+ | throw new IllegalStateException("No Project Found with name " + projectNames[i]); | ||
+ | } | ||
+ | //Verify project contains a proper workflow to copy | ||
+ | if (project.getWorkflow(workflowName) == null) { | ||
+ | throw new IllegalStateException("Project " + projectNames[i] + " had no workflow named " + workflowName); | ||
+ | } | ||
+ | //Verify the workflow is originating | ||
+ | if (!project.getWorkflow(workflowName).isOriginating()) { | ||
+ | throw new IllegalStateException("Project " + projectNames[i] + " workflow named " + workflowName + " is not originating"); | ||
+ | } | ||
+ | result.add(project); | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | void modifyBranchOnSourceConfig(Workflow workflow) { | ||
+ | if (!(workflow.getBuildProfile().getSourceConfig() instanceof CvsSourceConfig)) { | ||
+ | throw new IllegalStateException("Workflow " + workflow.getName() + " for Project " + workflow.getProject().getName() + " does not contain a CvsSourceConfig"); | ||
+ | } | ||
+ | CvsSourceConfig sourceConfig = workflow.getBuildProfile().getSourceConfig(); | ||
+ | for (int i =0; i < sourceConfig.getModuleArray().length; i++) { | ||
+ | sourceConfig.getModuleArray()[i].setBranch(branchName); | ||
+ | } | ||
+ | } | ||
+ | Workflow getOrCreateBranchWorkflow(Project project, Workflow workflow) { | ||
+ | Workflow result = null; | ||
+ | String branchWorkflowName = strategicProjectName + "Build"; | ||
+ | if (project.getWorkflow(branchWorkflowName) == null) { | ||
+ | print("Creating Branch Workflow for Project " + project.getName()); | ||
+ | result = workflow.duplicate(); | ||
+ | result.setName(branchWorkflowName); | ||
+ | if (result.getBuildProfile() == null) { | ||
+ | throw new IllegalStateException("Workflow " + result.getName() + " for Project " + workflow.getProject().getName() + " has no Build Profile"); | ||
+ | } | ||
+ | result.getBuildProfile().setName(branchWorkflowName); | ||
+ | modifyBranchOnSourceConfig(result); | ||
+ | } | ||
+ | else { | ||
+ | print("Retrieving Existing Branch Workflow for Project " + project.getName()); | ||
+ | result = project.getWorkflow(branchWorkflowName); | ||
+ | } | ||
+ | return result; | ||
+ | } | ||
+ | void migrateDependenciesToBranchedProfiles(BuildProfile buildProfile, Map tacticalProfileToBranchProfile) { | ||
+ | for (int i =0; i < buildProfile.getDependencyArray().length; i++) { | ||
+ | Dependency dep = buildProfile.getDependencyArray()[i]; | ||
+ | if (dep.getDependency() instanceof AnthillProject) { | ||
+ | AnthillProject anthillProject = (AnthillProject) dep.getDependency(); | ||
+ | if (tacticalProfileToBranchProfile.containsKey(anthillProject.getBuildProfile())) { | ||
+ | BuildProfile branchProfile = (BuildProfile) tacticalProfileToBranchProfile.get(anthillProject.getBuildProfile()); | ||
+ | AnthillProject newDependency = new AnthillProject(branchProfile); | ||
+ | print("Changing Project " + buildProfile.getProject().getName() + " to depend on " + newDependency.getName()); | ||
+ | dep.setDependency(newDependency); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | String serverHost = "localhost"; | ||
+ | int serverPort = 4567; | ||
+ | String userName = "admin"; | ||
+ | String password = "admin"; | ||
+ | String strategicProjectName = null; | ||
+ | String projectListStr = null; | ||
+ | String branchName = null; | ||
+ | String workflowName = "TacticalBuild"; | ||
+ | Map tacticalProfileToBranchProfile = new HashMap(); | ||
+ | List branchProfiles = new ArrayList(); | ||
+ | if (bsh.args.length <= 6) { | ||
+ | throw new IllegalStateException("createBranchwWorkflows.bsh <server> <port> <username> <password> <strat-proj-name> <proj-list> <branch-name> ?<workflow-name> "); | ||
+ | } | ||
+ | serverHost = bsh.args[0]; | ||
+ | serverPort = Integer.parseInt(bsh.args[1]); | ||
+ | userName = bsh.args[2]; | ||
+ | password = bsh.args[3]; | ||
+ | strategicProjectName = bsh.args[4]; | ||
+ | projectListStr = bsh.args[5]; | ||
+ | branchName = bsh.args[6]; | ||
+ | if (bsh.args.length == 8) { | ||
+ | workflowName = bsh.args[7]; | ||
+ | } | ||
+ | // obtain connection to the Anthill server | ||
+ | AnthillClient anthill = AnthillClient.connect(serverHost, serverPort, | ||
+ | userName, password); | ||
+ | // create a Unit of Work | ||
+ | UnitOfWork uow = anthill.createUnitOfWork(); | ||
+ | // Query up all the projects | ||
+ | List projects = getProjectsForNameList(projectListStr); | ||
+ | print("Found " + projects.size() + " projects to modify"); | ||
+ | // Run through all of the projects creating any branch workflows that dont already exist | ||
+ | // This also builds up a list of the old workflows to the new workflows so we can change any dependencies | ||
+ | for (int i = 0; i < projects.size(); i++) { | ||
+ | Project project = (Project) projects.get(i); | ||
+ | Workflow workflow = project.getWorkflow(workflowName); | ||
+ | Workflow branchWorkflow = getOrCreateBranchWorkflow(project, workflow); | ||
+ | tacticalProfileToBranchProfile.put(workflow.getBuildProfile(), branchWorkflow.getBuildProfile()); | ||
+ | branchProfiles.add(branchWorkflow.getBuildProfile()); | ||
+ | } | ||
+ | for (int i =0; i < branchProfiles.size(); i++) { | ||
+ | BuildProfile buildProfile = (BuildProfile) branchProfiles.get(i); | ||
+ | migrateDependenciesToBranchedProfiles(buildProfile, tacticalProfileToBranchProfile); | ||
+ | } | ||
+ | uow.commitAndClose();</pre> |