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

hudson.scm.RevisionParameterAction Maven / Gradle / Ivy

There is a newer version: 2.3.11
Show newest version
/*
 * The MIT License
 *
 * Copyright (c) 2009-2010, Tom Huybrechts, Yahoo! Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.scm;

import hudson.model.InvisibleAction;
import hudson.model.Action;
import hudson.model.Queue;
import hudson.model.Queue.Task;
import hudson.model.queue.FoldableAction;
import hudson.scm.SubversionSCM.SvnInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.tmatesoft.svn.core.wc.SVNRevision;

/**
 * Action containing a list of SVN revisions that should be checked out. Used for parameterized builds.
 * 
 * @author Tom Huybrechts
 */
public class RevisionParameterAction extends InvisibleAction implements Serializable, FoldableAction {
	
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(RevisionParameterAction.class.getName());
	private final List revisions;

	public RevisionParameterAction(List revisions) {
		super();
		this.revisions = revisions;
	}
	
	public RevisionParameterAction(RevisionParameterAction action) {
		super();
		this.revisions = new ArrayList(action.revisions);
	}
	
	public RevisionParameterAction(SvnInfo... revisions) {
		this.revisions = new ArrayList(Arrays.asList(revisions));
	}

	public List getRevisions() {
		return revisions;
	}
	
	public SVNRevision getRevision(String url) {
		for (SvnInfo revision: revisions) {
			if (revision.url.equals(url)) {
				return SVNRevision.create(revision.revision);
			}
		}
		return null;
	}
		
	public void foldIntoExisting(Queue.Item item, Task owner, List otherActions) {
		RevisionParameterAction existing = item.getAction(RevisionParameterAction.class);
		if (existing!=null) {
		    existing.mergeRevisions(this.revisions);
		    return;
		}
		// no RevisionParameterAction found, so add a copy of this one
		item.getActions().add(new RevisionParameterAction(this));
	}
	
	private void mergeRevisions(List newRevisions) {
		
		for (SvnInfo newRev : newRevisions) {
			boolean found = false;
			for (SvnInfo oldRev : this.revisions) {
				if (oldRev.url.equals(newRev.url)) {

					LOGGER.info("Updating revision parameter for " + oldRev.url + " from " + oldRev.revision + " to " + newRev.revision);

					this.revisions.add(new SvnInfo(oldRev.url, newRev.revision));
					this.revisions.remove(oldRev);
					found = true;
					break;
				}
			}
			if (!found) {
				this.revisions.add(newRev);
			}
		}
	}

	@Override
	public String toString() {
		StringBuffer result = new StringBuffer("[RevisionParameterAction ");
		for(SvnInfo i : revisions) {
			result.append(i.url + "(" + i.revision + ") ");
		}
		result.append("]");
		return result.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy