|
(→AHPSCRIPTS-96)
|
(→AHPSCRIPTS-98)
|
| Line 1606: | |||
| log.error("Exception:", e); | log.error("Exception:", e); | ||
| }</pre> | }</pre> | ||
| + | = Overwrite Dependency Pushed Properties = | ||
| + | * AnthillPro pushes properties to builds triggered by dependencies, this script overwrites the values for the properties that are defined on the dependent worfklow/project | ||
| + | ==== AHPSCRIPTS-101 ==== | ||
| + | <pre>import com.urbancode.anthill3.domain.buildrequest.BuildRequest; | ||
| + | import com.urbancode.anthill3.domain.buildrequest.RequestSourceEnum; | ||
| + | import com.urbancode.anthill3.domain.persistent.PersistenceException; | ||
| + | import com.urbancode.anthill3.domain.project.Project; | ||
| + | import com.urbancode.anthill3.domain.project.prop.ProjectProperty; | ||
| + | import com.urbancode.anthill3.domain.property.PropertyValue; | ||
| + | import com.urbancode.anthill3.domain.workflow.Workflow; | ||
| + | import com.urbancode.anthill3.persistence.UnitOfWork; | ||
| + | import com.urbancode.anthill3.runtime.scripting.helpers.BuildRequestLookup; | ||
| + | import com.urbancode.anthill3.runtime.scripting.helpers.ProjectLookup; | ||
| + | import com.urbancode.anthill3.runtime.scripting.helpers.WorkflowLookup; | ||
| + | import java.util.HashMap; | ||
| + | import java.util.Map; | ||
| + | try { | ||
| + | BuildRequest request = BuildRequestLookup.getCurrent(); | ||
| + | if (request != null && RequestSourceEnum.DEPENDENCY.equals(request.getRequestSource()) && | ||
| + | request.getPropertyNames().length > 0) { | ||
| + | Project project = ProjectLookup.getCurrent(); | ||
| + | Workflow workflow = WorkflowLookup.getCurrent(); | ||
| + | Map<String, PropertyValue> changedPropertyMap = new HashMap<String, PropertyValue>(); | ||
| + | for (String property : request.getPropertyNames()) { | ||
| + | PropertyValue initialValue = request.getPropertyValue(property); | ||
| + | // check the workflow first | ||
| + | PropertyValue originalValue = workflow.getPropertyValue(property); | ||
| + | // if not on the workflow check the project | ||
| + | if (originalValue == null) { | ||
| + | ProjectProperty projectProp = project.getProperty(property); | ||
| + | if (projectProp != null) { | ||
| + | originalValue = new PropertyValue(projectProp.getValue(), false); | ||
| + | } | ||
| + | } | ||
| + | // and now see if they match | ||
| + | if (originalValue != null && !originalValue.equals(initialValue)) { | ||
| + | changedPropertyMap.put(property, originalValue); | ||
| + | } | ||
| + | else if (originalValue == null) { | ||
| + | commandOutput.println("Could not find original value for " + property); | ||
| + | } | ||
| + | else { | ||
| + | commandOutput.println("Value did not change for " + property); | ||
| + | } | ||
| + | } | ||
| + | if (changedPropertyMap.size() > 0) { | ||
| + | request = (BuildRequest) UnitOfWork.getCurrent().lockAndReload(request); | ||
| + | for (String property : changedPropertyMap.keySet()) { | ||
| + | request.setPropertyValue(property, changedPropertyMap.get(property)); | ||
| + | commandOutput.println("Replaced value of " + property + " with " + | ||
| + | changedPropertyMap.get(property).getDisplayedValue()); | ||
| + | } | ||
| + | try { | ||
| + | UnitOfWork.getCurrent().commit(); | ||
| + | } | ||
| + | catch (PersistenceException pe) { | ||
| + | pe.printStackTrace(commandOutput); | ||
| + | try { | ||
| + | UnitOfWork.getCurrent().cancel(); | ||
| + | } | ||
| + | catch (Exception e) { | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | else { | ||
| + | commandOutput.println("Not a dependency request!"); | ||
| + | } | ||
| + | } | ||
| + | catch (Throwable t) { | ||
| + | t.printStackTrace(commandOutput); | ||
| + | }</pre> | ||