/* * Copyright 2000-2009 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildserver.groovyPlug import com.intellij.openapi.diagnostic.Logger import java.text.SimpleDateFormat import java.util.Map.Entry import jetbrains.buildServer.serverSide.ParametersPreprocessor import jetbrains.buildServer.serverSide.SBuild import jetbrains.buildServer.serverSide.SRunningBuild import jetbrains.buildServer.serverSide.ServerExtensionHolder import jetbrains.buildServer.vcs.SVcsModification import jetbrains.buildServer.vcs.VcsModificationHistory import org.jetbrains.annotations.Nullable /** * @author Yegor.Yarko * Date: 15.01.2009 */ public class GroovyPropertyProvider implements ParametersPreprocessor { private static final Logger LOG = Logger.getInstance(GroovyPropertyProvider.class.getName()); VcsModificationHistory vcsModificationHistory; ServerExtensionHolder extensionHolder; GroovyPropertyProvider() { LOG.info("GroovyPropertyProvider initialized."); } public void fixRunBuildParameters(SRunningBuild build, Map runParameters, Map buildParams) { LOG.debug("GroovyPropertyProvider asked for properties for build (buildId=" + build.buildId + ")"); Map buildParamsToAdd = new HashMap(); addLastModification(buildParamsToAdd, build); addBuildStartTime(buildParamsToAdd, build); addBuildParameters(buildParamsToAdd, buildParams) } private def addBuildParameters(HashMap buildParamsToAdd, Map resultingBuildParams) { if (!buildParamsToAdd.empty) { for (Entry parameter: buildParamsToAdd.entrySet()) { LOG.debug("Adding build parameter '" + parameter.getKey() + "' with value =" + parameter.getValue()); resultingBuildParams.put(parameter.getKey(), parameter.getValue()); } } LOG.debug(buildParamsToAdd.size() + " parameters added."); } @Nullable SVcsModification getLastModification(SBuild build) { Long modificationId = build.getBuildPromotion().getLastModificationId(); if (modificationId == null || modificationId == -1) { return null; } return vcsModificationHistory.findChangeById(modificationId); } void addLastModification(Map buildParametersToAdd, SRunningBuild build) { SVcsModification lastModification = getLastModification(build); if (lastModification != null) { Date lastChangeDate = lastModification.getVcsDate(); addDateTimeProperty(buildParametersToAdd, lastChangeDate, "yyyyMMdd'T'HHmmssZ", "build.lastChange.time", "BUILD_VCS_LASTCHANGE_TIMESTAMP"); } } void addBuildStartTime(Map buildParametersToAdd, SRunningBuild build) { Date buildStartTime = build.getStartDate(); addDateTimeProperty(buildParametersToAdd, buildStartTime, "yyyyMMdd", "build.start.date", "BUILD_START_DATE"); addDateTimeProperty(buildParametersToAdd, buildStartTime, "HHmmss", "build.start.time", "BUILD_START_TIME"); } private def addDateTimeProperty(Map buildParametersToAdd, Date date, String dateFormat, String systemParamName, String envParamName) { String formattedDate = (new SimpleDateFormat(dateFormat)).format(date); if (systemParamName != null) buildParametersToAdd.put("system." + systemParamName, formattedDate); if (envParamName != null) buildParametersToAdd.put("env." + envParamName, formattedDate) } }