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

com.marvelution.hudson.plugins.jirareporter.JIRASite Maven / Gradle / Ivy

/*
 * 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.hudson.plugins.jirareporter;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.xml.rpc.ServiceException;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import com.atlassian.jira.rpc.soap.client.JiraSoapService;
import com.atlassian.jira.rpc.soap.client.JiraSoapServiceService;
import com.atlassian.jira.rpc.soap.client.JiraSoapServiceServiceLocator;
import com.atlassian.jira.rpc.soap.client.RemoteAuthenticationException;
import com.atlassian.jira.rpc.soap.client.RemoteException;
import com.marvelution.hudson.plugins.apiv2.APIv2Plugin;

/**
 * JIRA Site object used by the Notifier
 * 
 * @author Mark Rekveld
 */
public class JIRASite extends AbstractDescribableImpl {

	/**
	 * The Endpoint where the JIRA SOAP service can be found
	 */
	public static final String SERVICE_ENDPOINT = "rpc/soap/jirasoapservice-v2";

	/**
	 * The Endpoint where the JIRA SOAP service WSDL can be found
	 */
	public static final String SERVICE_ENDPOINT_WSDL = SERVICE_ENDPOINT + "?wsdl";

	public final String name;
	public final URL url;
	public final String username;
	public final String password;
	public final String closeAction;
	public final boolean supportsWikiStyle;
	public final boolean checkIssueExistence;
	public final List userMappings;
	public final String issueKeyPattern;

	transient volatile Map priorities;

	/**
	 * Constructor
	 * 
	 * @param name the JIRA Site name
	 * @param url the JIRA Site {@link URL}
	 * @param username the username to use for Authentication with the JIRA Site
	 * @param password the password for the given username
	 * @param supportsWikiStyle flag is Wiki style texts are supported
	 * @param closeAction the name of the Close action, defaults to Close
	 * @param checkIssueExistence flag to check if an Issue exists before annotating it in the Changelog
	 * @param userMappings the Array of Hudson to JIRA {@link UserMapping}s
	 * @param issueKeyPattern the custom issue key pattern
	 */
	@DataBoundConstructor
	public JIRASite(String name, URL url, String username, String password, boolean supportsWikiStyle,
			String closeAction, boolean checkIssueExistence, List userMappings, String issueKeyPattern) {
		this.name = name;
		if (!url.toExternalForm().endsWith("/"))
		try {
			url = new URL(url.toExternalForm() + "/");
		} catch (MalformedURLException e) {
			// Ignore this, cannot happen any way
		}
		this.url = url;
		this.username = username;
		this.password = password;
		this.supportsWikiStyle = supportsWikiStyle;
		this.closeAction = closeAction;
		this.checkIssueExistence = checkIssueExistence;
		this.userMappings = userMappings;
		this.issueKeyPattern = issueKeyPattern;
	}

	/**
	 * Get the {@link JIRAClient} for this {@link JIRASite}
	 * 
	 * @return the {@link JIRAClient}
	 * @throws RemoteAuthenticationException
	 * @throws RemoteException
	 * @throws java.rmi.RemoteException
	 * @throws MalformedURLException
	 * @throws ServiceException
	 */
	@Deprecated
	public JIRAClient createClient() throws RemoteAuthenticationException, RemoteException, java.rmi.RemoteException,
			MalformedURLException, ServiceException {
		if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			return null;
		}
		JiraSoapServiceService jiraSoapServiceLocator = new JiraSoapServiceServiceLocator();
		JiraSoapService service = jiraSoapServiceLocator.getJirasoapserviceV2(new URL(url, SERVICE_ENDPOINT));
		return new JIRAClient(service, service.login(username, password), this);
	}

	/**
	 * Getter for the {@link #closeAction}
	 * 
	 * @return the {@link #closeAction}
	 */
	public String getCloseActionName() {
		if (StringUtils.isBlank(closeAction)) {
			return "Close Issue";
		} else {
			return closeAction;
		}
	}

	/**
	 * The {@link Pattern} used to find JIRA Issue keys
	 * 
	 * @return the Issue key {@link Pattern}
	 */
	public Pattern getIssuePattern() {
		if (StringUtils.isNotBlank(issueKeyPattern)) {
			return Pattern.compile(issueKeyPattern);
		} else {
			return Pattern.compile(APIv2Plugin.getPlugin().getIssueKeyPattern());
		}
	}

	/**
	 * GEtter for the JIRA username of the given Hudson username
	 * 
	 * @param hudsonUser the Hudson username to get the JIRA username for
	 * @return the JIRA username, or the Hudson username if there is no explicit mapping for the given username
	 * @since 5.0.0
	 */
	public String getJIRAUsername(String hudsonUser) {
		if (userMappings != null) {
			for (UserMapping userMapping : userMappings) {
				if (userMapping.hudsonId != null && userMapping.hudsonId.equals(hudsonUser)) {
					return userMapping.jiraId;
				}
			}
		}
		return hudsonUser;
	}

	/**
	 * Static helper method to get a {@link JIRASite} by its name
	 * 
	 * @param name the name of the {@link JIRASite} to get
	 * @return the {@link JIRASite}, may be null if no {@link JIRASite} with the given name can be found
	 */
	public static JIRASite getSite(String name) {
		for (JIRASite site : JIRABuildResultReportNotifier.DESCRIPTOR.getSites()) {
			if (site.name.equals(name)) {
				return site;
			}
		}
		return null;
	}

	/**
	 * Static helper method to get a {@link JIRASite} by the configuration of a Build and this Project
	 * 
	 * @param build the {@link AbstractBuild} to get the {@link JIRASite} for
	 * @return the {@link JIRASite}, may be null if the project is not configured with
	 * 			{@link JIRABuildResultReportNotifier}
	 */
	public static JIRASite getSite(AbstractBuild build) {
		JIRABuildResultReportNotifier notifier = build.getProject().getPublishersList()
			.get(JIRABuildResultReportNotifier.class);
		if (notifier == null) {
			return null;
		} else {
			return notifier.getSite();
		}
	}

	/**
	 * Check if the given Build has a {@link JIRASite} configured
	 * 
	 * @param build the {@link AbstractBuild} to check
	 * @return true is a {@link JIRASite} is configured, false otherwise
	 */
	public static boolean hasSite(AbstractBuild build) {
		return getSite(build) != null;
	}

	/**
	 * @author Mark Rekveld
	 *
	 * @since 5.0.0
	 */
	@Extension
	public static class DescriptorImpl extends Descriptor {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getDisplayName() {
			return "";
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy