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

com.marvelution.bamboo.plugins.sonar.upgrade.SonarCleanupUpgradeTask 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.upgrade;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

import com.atlassian.bamboo.build.Build;
import com.atlassian.bamboo.build.BuildManager;
import com.atlassian.bandana.BandanaManager;
import com.atlassian.sal.api.message.Message;
import com.atlassian.sal.api.upgrade.PluginUpgradeTask;
import com.marvelution.bamboo.plugins.sonar.BambooSonarServer;
import com.marvelution.bamboo.plugins.sonar.BandanaUtils;
import com.marvelution.bamboo.plugins.sonar.SonarPluginHelper;

/**
 * Upgrade task for the Sonar plugin to cleanup some errors of the previous version
 * 
 * @author Mark Rekveld
 */
public class SonarCleanupUpgradeTask implements PluginUpgradeTask {

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

	private BuildManager buildManager;

	private BandanaUtils bandanaUtils = new BandanaUtils();

	/**
	 * Constructor
	 * 
	 * @param buildManager the {@link BuildManager} implementation
	 * @param bandanaManager the {@link BandanaManager} implementation
	 */
	public SonarCleanupUpgradeTask(BuildManager buildManager, BandanaManager bandanaManager) {
		this.buildManager = buildManager;
		bandanaUtils.setBandanaManager(bandanaManager);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Collection doUpgrade() throws Exception {
		bandanaUtils.loadSonarServers();
		final Collection builds = buildManager.getAllBuildsForEdit();
		for (final Iterator iter = builds.iterator(); iter.hasNext();) {
			final Build build = iter.next();
			final Map customConfig = build.getBuildDefinition().getCustomConfiguration();
			if (Boolean.parseBoolean(customConfig.get(SonarPluginHelper.SONAR_RUN))
				&& !SonarPluginHelper.BUILD_PLAN_SPECIFIC.equals(customConfig.get(SonarPluginHelper.SONAR_SERVER))) {
				logger.info("Checking if Build '" + build.getName() + "' requires a cleanup action");
				try {
					final int serverId = Integer.parseInt(customConfig.get(SonarPluginHelper.SONAR_SERVER));
					final BambooSonarServer server = bandanaUtils.getSonarServer(serverId);
					if (server != null) {
						String additionalArguments = customConfig.get(SonarPluginHelper.SONAR_ADD_ARGS);
						additionalArguments =
							additionalArguments.replaceAll(server.getAdditionalArguments(), "").trim();
						build.getBuildDefinition().getCustomConfiguration().put(SonarPluginHelper.SONAR_ADD_ARGS,
							additionalArguments);
						build.getBuildDefinition().getCustomConfiguration().put(SonarPluginHelper.SONAR_HOST_URL, "");
						build.getBuildDefinition().getCustomConfiguration().put(SonarPluginHelper.SONAR_JDBC_URL, "");
						build.getBuildDefinition().getCustomConfiguration().put(SonarPluginHelper.SONAR_JDBC_DRIVER,
							"");
						build.getBuildDefinition().getCustomConfiguration().put(
							SonarPluginHelper.SONAR_JDBC_USERNAME, "");
						build.getBuildDefinition().getCustomConfiguration().put(
							SonarPluginHelper.SONAR_JDBC_PASSWORD, "");
					} else {
						logger.warn("Disabling Sonar for Build '" + build.getName()
							+ "'. Reason: No Server available under Id: " + serverId);
						build.getBuildDefinition().getCustomConfiguration().put(SonarPluginHelper.SONAR_RUN, "false");
					}
				} catch (NumberFormatException e) {
					logger.warn("Disabling Sonar for Build '" + build.getName()
						+ "'. Reason: Invalid Sonar Server configuration");
					build.getBuildDefinition().getCustomConfiguration().put(SonarPluginHelper.SONAR_RUN, "false");
				} finally {
					buildManager.saveBuildAndDefinition(build);
				}
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getBuildNumber() {
		return 1;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getPluginKey() {
		return "com.marvelution.bamboo.plugins.sonar.gadgets";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getShortDescription() {
		return "Upgrade task to cleanup the build plan configurations";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy