All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.bamboo.plugins.sonar.build.actions.SonarPreBuildQueuedAction Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you 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 com.marvelution.bamboo.plugins.sonar.build.actions;

import org.apache.log4j.Logger;

import com.atlassian.bamboo.build.BuildLoggerManager;
import com.atlassian.bamboo.build.logger.BuildLogger;
import com.atlassian.bamboo.buildqueue.manager.CustomPreBuildQueuedAction;
import com.atlassian.bamboo.configuration.AdministrationConfiguration;
import com.atlassian.bamboo.v2.build.BaseConfigurableBuildPlugin;
import com.atlassian.bamboo.v2.build.BuildContext;
import com.atlassian.bamboo.v2.build.BuildPlanDefinition;
import com.atlassian.bandana.BandanaManager;
import com.marvelution.bamboo.plugins.sonar.BandanaUtils;
import com.marvelution.bamboo.plugins.sonar.SonarPluginHelper;
import com.marvelution.bamboo.plugins.sonar.BambooSonarServer;

/**
 * Custom Pre Build Queued Action to populate any inherited global Sonar configuration
 * 
 * @author Mark Rekveld
 */
public class SonarPreBuildQueuedAction extends BaseConfigurableBuildPlugin implements CustomPreBuildQueuedAction {

	private final Logger logger = Logger.getLogger(SonarPreBuildQueuedAction.class);

	private AdministrationConfiguration administrationConfiguration;

	private BuildLoggerManager buildLoggerManager;

	private BandanaManager bandanaManager;

	private BandanaUtils bandanaUtils = new BandanaUtils();

	private SonarPluginHelper sonarPluginHelper = new SonarPluginHelper();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public BuildContext call() {
		final BuildPlanDefinition buildDefinition = buildContext.getBuildPlanDefinition();
		if (Boolean.parseBoolean(buildDefinition.getCustomConfiguration().get(SonarPluginHelper.SONAR_RUN))) {
			logger.debug("Sonar is configured to run... Checking if any global configuration needs to be applied");
			buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_CI_URL,
				administrationConfiguration.getBaseUrl() + "/browse/" + buildContext.getPlanKey());
			final BuildLogger sonarLogger = buildLoggerManager.getBuildLogger(buildContext.getPlanKey());
			if (!SonarPluginHelper.BUILD_PLAN_SPECIFIC.equals(buildDefinition.getCustomConfiguration().get(
				SonarPluginHelper.SONAR_SERVER))) {
				try {
					final int serverId =
						Integer.parseInt(buildDefinition.getCustomConfiguration().get(SonarPluginHelper.SONAR_SERVER));
					bandanaUtils.loadSonarServers();
					final BambooSonarServer server = bandanaUtils.getSonarServer(serverId);
					if (server != null) {
						if (server.isDisabled()) {
							buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_RUN, "false");
							logger.debug(sonarLogger.addBuildLogEntry("Skipping Sonar update... Sonar Server "
								+ server.getName() + " is disabled"));
						} else {
							sonarPluginHelper.copySonarServerToBuildPlanDefinition(server, buildDefinition);
							logger.debug(sonarLogger
								.addBuildLogEntry("Updated Sonar Server configuration to use server: "
									+ server.getName()));
						}
					} else {
						logger.error(sonarLogger
							.addErrorLogEntry("Sonar configuration is invalid. Skipping Sonar update"));
						buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_RUN, "false");
					}
				} catch (NumberFormatException e) {
					logger.error(sonarLogger
						.addErrorLogEntry("Sonar configuration is invalid. Skipping Sonar update"));
					buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_RUN, "false");
				}
			}
			if (!Boolean.parseBoolean(buildDefinition.getCustomConfiguration().get(
				SonarPluginHelper.SONAR_OVERRIDE_GLOBALS))) {
				buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_SKIP_ON_BUILD_FAILURE,
					administrationConfiguration.getSystemProperty(SonarPluginHelper.SONAR_SKIP_ON_BUILD_FAILURE));
				buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_SKIP_ON_MANUAL_BUILD,
					administrationConfiguration.getSystemProperty(SonarPluginHelper.SONAR_SKIP_ON_MANUAL_BUILD));
				buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_SKIP_ON_NO_CODE_CHANGES,
					administrationConfiguration.getSystemProperty(SonarPluginHelper.SONAR_SKIP_ON_NO_CODE_CHANGES));
				buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_FAILURE_BEHAVIOR,
					administrationConfiguration.getSystemProperty(SonarPluginHelper.SONAR_FAILURE_BEHAVIOR));
				buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_SKIP_BYTECODE_ANALYSIS,
					administrationConfiguration.getSystemProperty(SonarPluginHelper.SONAR_SKIP_BYTECODE_ANALYSIS));
				buildDefinition.getCustomConfiguration().put(SonarPluginHelper.SONAR_PROFILE,
					administrationConfiguration.getSystemProperty(SonarPluginHelper.SONAR_PROFILE));
				logger.debug(sonarLogger.addBuildLogEntry("Applied global Sonar configuration to Build Plan"));
			}
		}
		return buildContext;
	}

	/**
	 * Set the {@link AdministrationConfiguration}
	 * 
	 * @param administrationConfiguration the {@link AdministrationConfiguration}
	 */
	public void setAdministrationConfiguration(AdministrationConfiguration administrationConfiguration) {
		this.administrationConfiguration = administrationConfiguration;
	}

	/**
	 * Set the {@link BuildLoggerManager}
	 * 
	 * @param buildLoggerManager the {@link BuildLoggerManager}
	 */
	public void setBuildLoggerManager(BuildLoggerManager buildLoggerManager) {
		this.buildLoggerManager = buildLoggerManager;
	}

	/**
	 * Set the {@link BandanaManager}
	 * 
	 * @param bandanaManager the {@link BandanaManager}
	 */
	public void setBandanaManager(BandanaManager bandanaManager) {
		this.bandanaManager = bandanaManager;
		bandanaUtils.setBandanaManager(this.bandanaManager);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy