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

com.marvelution.bamboo.plugins.sonar.build.actions.SonarCompletedAction 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 java.util.Map;

import com.atlassian.bamboo.build.Build;
import com.atlassian.bamboo.build.CustomBuildCompleteAction;
import com.atlassian.bamboo.labels.LabelManager;
import com.atlassian.bamboo.results.BuildResults;
import com.atlassian.bamboo.resultsummary.ExtendedBuildResultsSummary;
import com.atlassian.bamboo.v2.build.BaseConfigurablePlugin;
import com.atlassian.bandana.BandanaManager;
import com.marvelution.bamboo.plugins.sonar.BambooSonarServer;
import com.marvelution.bamboo.plugins.sonar.BandanaUtils;
import com.marvelution.bamboo.plugins.sonar.SonarPluginHelper;

/**
 * {@link CustomBuildCompleteAction} for teh Sonar build
 * 
 * @author Mark Rekveld
 */
public class SonarCompletedAction extends BaseConfigurablePlugin implements CustomBuildCompleteAction {

	private LabelManager labelManager;

	private BandanaManager bandanaManager;

	private BandanaUtils bandanaUtils = new BandanaUtils();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void run(Build build, BuildResults buildResults) {
		final ExtendedBuildResultsSummary summary = buildResults.getBuildResultsSummary();
		final Map customConfiguration = build.getBuildDefinition().getCustomConfiguration();
		if (Boolean.parseBoolean(customConfiguration.get(SonarPluginHelper.SONAR_RUN))) {
			// Only label the build if Sonar was executed and if the Sonar execution failed and the LABEL failure
			// behavior is configured
			if (summary.getCustomBuildData().containsKey(SonarPluginHelper.SONAR_BUILD_STATE_KEY)
				&& Integer.parseInt(summary.getCustomBuildData().get(SonarPluginHelper.SONAR_BUILD_STATE_KEY)) != 0
				&& customConfiguration.containsKey(SonarPluginHelper.SONAR_FAILURE_BEHAVIOR)
				&& SonarPluginHelper.SONAR_LABEL_BEHAVIOR.equals(customConfiguration
					.get(SonarPluginHelper.SONAR_FAILURE_BEHAVIOR))) {
				labelManager.addLabel("Sonar-Analysis-Failed", buildResults, null);
			}
			if (!SonarPluginHelper.BUILD_PLAN_SPECIFIC
				.equals(customConfiguration.get(SonarPluginHelper.SONAR_SERVER))) {
				try {
					final int serverId = Integer.parseInt(customConfiguration.get(SonarPluginHelper.SONAR_SERVER));
					bandanaUtils.loadSonarServers();
					final BambooSonarServer server = bandanaUtils.getSonarServer(serverId);
					if (server != null) {
						customConfiguration.remove(SonarPluginHelper.SONAR_HOST_URL);
						customConfiguration.remove(SonarPluginHelper.SONAR_JDBC_URL);
						customConfiguration.remove(SonarPluginHelper.SONAR_JDBC_DRIVER);
						customConfiguration.remove(SonarPluginHelper.SONAR_JDBC_USERNAME);
						customConfiguration.remove(SonarPluginHelper.SONAR_JDBC_PASSWORD);
						String additionalArguments = customConfiguration.get(SonarPluginHelper.SONAR_ADD_ARGS);
						customConfiguration.remove(SonarPluginHelper.SONAR_ADD_ARGS);
						if (additionalArguments != null) {
							additionalArguments =
								additionalArguments.replaceAll(server.getAdditionalArguments(), "").trim();
						}
						customConfiguration.put(SonarPluginHelper.SONAR_ADD_ARGS, additionalArguments);
					}
				} catch (NumberFormatException e) {
					// Ignore this exception, this probably means that the Sonar was skipped since invalid
					// configuration is handled by the pre build queued action
				}
			}
		}
	}

	/**
	 * Set the {@link LabelManager}
	 * 
	 * @param labelManager the {@link LabelManager} implementation
	 */
	public void setLabelManager(LabelManager labelManager) {
		this.labelManager = labelManager;
	}

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy