From d40527857a27eddaf1d3215112e8d0d72dbaccc8 Mon Sep 17 00:00:00 2001 From: lucamilanesio Date: Mon, 1 Sep 2008 20:07:35 +0000 Subject: [PATCH] [Issue 2298] First implementation of SubversionSCM parameters expansion based on StringParameterValue parameters values git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11912 71c3de6d-444a-0410-be80-ed276b4c234a --- .../hudson/model/StringParameterValue.java | 5 + .../scm/SubversionChangeLogBuilder.java | 2 +- .../main/java/hudson/scm/SubversionSCM.java | 109 +++++++++++++++--- .../hudson/tasks/MailAddressResolver.java | 25 +++- core/src/main/java/hudson/tasks/Mailer.java | 12 +- 5 files changed, 131 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/hudson/model/StringParameterValue.java b/core/src/main/java/hudson/model/StringParameterValue.java index 730374abb9..6e09bbed41 100644 --- a/core/src/main/java/hudson/model/StringParameterValue.java +++ b/core/src/main/java/hudson/model/StringParameterValue.java @@ -34,4 +34,9 @@ public class StringParameterValue extends ParameterValue { } }; } + + @Override + public String toString() { + return "(StringParameterValue) " + getName() + "='" + value + "'"; + } } diff --git a/core/src/main/java/hudson/scm/SubversionChangeLogBuilder.java b/core/src/main/java/hudson/scm/SubversionChangeLogBuilder.java index 1d9f2e4fe8..9963401f15 100644 --- a/core/src/main/java/hudson/scm/SubversionChangeLogBuilder.java +++ b/core/src/main/java/hudson/scm/SubversionChangeLogBuilder.java @@ -70,7 +70,7 @@ public final class SubversionChangeLogBuilder { th.setDocumentLocator(DUMMY_LOCATOR); logHandler.startDocument(); - for (ModuleLocation l : scm.getLocations()) { + for (ModuleLocation l : scm.getLocations(build)) { changelogFileCreated |= buildModule(l.remote, svnlc, logHandler); } for(SubversionSCM.External ext : externals) { diff --git a/core/src/main/java/hudson/scm/SubversionSCM.java b/core/src/main/java/hudson/scm/SubversionSCM.java index 528914921f..f4842c5156 100644 --- a/core/src/main/java/hudson/scm/SubversionSCM.java +++ b/core/src/main/java/hudson/scm/SubversionSCM.java @@ -13,6 +13,8 @@ import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; import hudson.model.Hudson; +import hudson.model.ParameterValue; +import hudson.model.ParametersAction; import hudson.model.TaskListener; import hudson.remoting.Callable; import hudson.remoting.Channel; @@ -85,6 +87,7 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -171,6 +174,16 @@ public class SubversionSCM extends SCM implements Serializable { * @since 1.91 */ public ModuleLocation[] getLocations() { + return getLocations(null); + } + + /** + * list of all configured svn locations, expanded according to + * build parameters values; + * + * @since 1.91 + */ + public ModuleLocation[] getLocations(AbstractBuild build) { // check if we've got a old location if (modules != null) { // import the old configuration @@ -187,7 +200,16 @@ public class SubversionSCM extends SCM implements Serializable { locations = oldLocations.toArray(new ModuleLocation[oldLocations.size()]); modules = null; } - return locations; + + if(build == null) + return locations; + + ModuleLocation[] outLocations = new ModuleLocation[locations.length]; + for (int i = 0; i < outLocations.length; i++) { + outLocations[i] = locations[i].getExpandedLocation(build); + } + + return outLocations; } public boolean isUseUpdate() { @@ -205,6 +227,8 @@ public class SubversionSCM extends SCM implements Serializable { @Override public void buildEnvVars(AbstractBuild build, Map env) { super.buildEnvVars(build, env); + + ModuleLocation[] locations = getLocations(build); try { Map revisions = parseRevisionFile(build); @@ -326,7 +350,7 @@ public class SubversionSCM extends SCM implements Serializable { // write out the revision file PrintWriter w = new PrintWriter(new FileOutputStream(getRevisionFile(build))); try { - Map revMap = workspace.act(new BuildRevisionMapTask(this, listener, externals)); + Map revMap = workspace.act(new BuildRevisionMapTask(build, this, listener, externals)); for (Entry e : revMap.entrySet()) { w.println( e.getKey() +'/'+ e.getValue().revision ); } @@ -354,10 +378,14 @@ public class SubversionSCM extends SCM implements Serializable { */ private List checkout(AbstractBuild build, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { try { - if (!repositoryLocationsExist() && build.getProject().getLastSuccessfulBuild()!=null) { + ArrayList warnings = new ArrayList(); + + if (!repositoryLocationsExist(build, warnings) && build.getProject().getLastSuccessfulBuild()!=null) { // Disable this project, see issue #763 // but only do so if there was at least some successful build, // to make sure that initial configuration error won't disable the build. see issue #1567 + listenerPrintln(listener, warnings); + listener.getLogger().println("One or more repository locations do not exist anymore for " + build.getProject().getName() + ", project will be disabled."); build.getProject().makeDisabled(true); return null; @@ -366,11 +394,20 @@ public class SubversionSCM extends SCM implements Serializable { e.printStackTrace(listener.error(e.getMessage())); return null; } - Boolean isUpdatable = useUpdate && workspace.act(new IsUpdatableTask(this, listener)); - return workspace.act(new CheckOutTask(this, build.getTimestamp().getTime(), isUpdatable, listener)); + Boolean isUpdatable = useUpdate && workspace.act(new IsUpdatableTask(build, this, listener)); + return workspace.act(new CheckOutTask(build, this, build.getTimestamp().getTime(), isUpdatable, listener)); } - /** + private void listenerPrintln(TaskListener listener, + ArrayList messages) { + PrintStream out = listener.getLogger(); + for (String string : messages) { + out.println(string); + } + + } + + /** * Either run "svn co" or "svn up" equivalent. */ private static class CheckOutTask implements FileCallable> { @@ -381,12 +418,12 @@ public class SubversionSCM extends SCM implements Serializable { private final TaskListener listener; private final ModuleLocation[] locations; - public CheckOutTask(SubversionSCM parent, Date timestamp, boolean update, TaskListener listener) { + public CheckOutTask(AbstractBuild build, SubversionSCM parent, Date timestamp, boolean update, TaskListener listener) { this.authProvider = parent.getDescriptor().createAuthenticationProvider(); this.timestamp = timestamp; this.update = update; this.listener = listener; - this.locations = parent.getLocations(); + this.locations = parent.getLocations(build); } public List invoke(File ws, VirtualChannel channel) throws IOException { @@ -643,11 +680,11 @@ public class SubversionSCM extends SCM implements Serializable { private final List externals; private final ModuleLocation[] locations; - public BuildRevisionMapTask(SubversionSCM parent, TaskListener listener, List externals) { + public BuildRevisionMapTask(AbstractBuild build, SubversionSCM parent, TaskListener listener, List externals) { this.authProvider = parent.getDescriptor().createAuthenticationProvider(); this.listener = listener; this.externals = externals; - this.locations = parent.getLocations(); + this.locations = parent.getLocations(build); } public Map invoke(File ws, VirtualChannel channel) throws IOException { @@ -705,10 +742,10 @@ public class SubversionSCM extends SCM implements Serializable { private final ISVNAuthenticationProvider authProvider; private final ModuleLocation[] locations; - IsUpdatableTask(SubversionSCM parent,TaskListener listener) { + IsUpdatableTask(AbstractBuild build, SubversionSCM parent,TaskListener listener) { this.authProvider = parent.getDescriptor().createAuthenticationProvider(); this.listener = listener; - this.locations = parent.getLocations(); + this.locations = parent.getLocations(build); } public Boolean invoke(File ws, VirtualChannel channel) throws IOException { @@ -749,8 +786,11 @@ public class SubversionSCM extends SCM implements Serializable { } try { - if (!repositoryLocationsExist()) { + ArrayList warnings = new ArrayList(); + + if (!repositoryLocationsExist(lastBuild, warnings)) { // Disable this project, see issue #763 + listenerPrintln(listener, warnings); listener.getLogger().println("One or more repository locations do not exist anymore for " + project + ", project will be disabled."); project.makeDisabled(true); return false; @@ -1364,10 +1404,21 @@ public class SubversionSCM extends SCM implements Serializable { } } - public boolean repositoryLocationsExist() throws SVNException { - for (ModuleLocation l : getLocations()) - if (getDescriptor().checkRepositoryPath(SVNURL.parseURIDecoded(l.remote)) == SVNNodeKind.NONE) + public boolean repositoryLocationsExist(AbstractBuild build, ArrayList warningList) throws SVNException { + for (ModuleLocation l : getLocations(build)) + if (getDescriptor().checkRepositoryPath(SVNURL.parseURIDecoded(l.remote)) == SVNNodeKind.NONE) { + warningList.add("Location '" + l.remote + "' does not exist"); + + ParametersAction params = build.getAction(ParametersAction.class); + if(params != null) { + warningList.add("[expanded on build '" + build + "' parameters values]"); + + for (ParameterValue paramValue : params) { + warningList.add("[" + paramValue + "]"); + } + } return false; + } return true; } @@ -1426,6 +1477,32 @@ public class SubversionSCM extends SCM implements Serializable { } private static final long serialVersionUID = 1L; + + /* + * (2008/08/10,Luca Milanesio) + * I need to access 'remote' field with a getter to allow Project-based + * parameter substitution. + */ + private String getExpandedRemote(AbstractBuild build) { + String outRemote = remote; + + ParametersAction parameters = build.getAction(ParametersAction.class); + if (parameters != null) + outRemote = parameters.substitute(build,remote); + + return outRemote; + } + + /** + * Expand location value based on Build parametric execution. + * + * @param build Build instance for expanding parameters into their values + * + * @return Output ModuleLocation expanded according to Build parameters values. + */ + public ModuleLocation getExpandedLocation(AbstractBuild build) { + return new ModuleLocation(getExpandedRemote(build), local); + } } private static final Logger LOGGER = Logger.getLogger(SubversionSCM.class.getName()); diff --git a/core/src/main/java/hudson/tasks/MailAddressResolver.java b/core/src/main/java/hudson/tasks/MailAddressResolver.java index 3279f3b72a..2e41d213e9 100644 --- a/core/src/main/java/hudson/tasks/MailAddressResolver.java +++ b/core/src/main/java/hudson/tasks/MailAddressResolver.java @@ -5,6 +5,7 @@ import hudson.Plugin; import hudson.scm.SCM; import hudson.scm.CVSSCM; import hudson.scm.SubversionSCM; +import hudson.model.AbstractBuild; import hudson.model.User; import hudson.model.AbstractProject; @@ -59,9 +60,15 @@ public abstract class MailAddressResolver implements ExtensionPoint { */ public abstract String findMailAddressFor(User u); - public static String resolve(User u) { + public static String resolve(AbstractBuild build, User u) { for (MailAddressResolver r : LIST) { - String email = r.findMailAddressFor(u); + String email = null; + + if(build != null) + email = r.findMailAddressFor(build, u); + else + email = r.findMailAddressFor(u); + if(email!=null) return email; } @@ -79,6 +86,14 @@ public abstract class MailAddressResolver implements ExtensionPoint { return u.getId()+ds; else return null; + } + + protected String findMailAddressFor(AbstractBuild build, User u) { + return findMailAddressFor(u); + } + + public static String resolve(User u) { + return resolve(null, u); } /** @@ -117,6 +132,10 @@ public abstract class MailAddressResolver implements ExtensionPoint { */ public static class DefaultAddressResolver extends MailAddressResolver { public String findMailAddressFor(User u) { + return findMailAddressFor(null, u); + } + + public String findMailAddressFor(AbstractBuildbuild, User u) { for (AbstractProject p : u.getProjects()) { SCM scm = p.getScm(); if (scm instanceof CVSSCM) { @@ -127,7 +146,7 @@ public abstract class MailAddressResolver implements ExtensionPoint { } if (scm instanceof SubversionSCM) { SubversionSCM svn = (SubversionSCM) scm; - for (SubversionSCM.ModuleLocation loc : svn.getLocations()) { + for (SubversionSCM.ModuleLocation loc : svn.getLocations(build)) { String s = findMailAddressFor(u,loc.remote); if(s!=null) return s; } diff --git a/core/src/main/java/hudson/tasks/Mailer.java b/core/src/main/java/hudson/tasks/Mailer.java index 010b5ec398..37620ad5b1 100644 --- a/core/src/main/java/hudson/tasks/Mailer.java +++ b/core/src/main/java/hudson/tasks/Mailer.java @@ -3,6 +3,7 @@ package hudson.tasks; import hudson.Launcher; import hudson.Functions; import hudson.maven.AbstractMavenProject; +import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.Build; import hudson.model.BuildListener; @@ -414,12 +415,19 @@ public class Mailer extends Publisher { @Exported public String getAddress() { + return getAddress(null); + } + + public String getAddress(AbstractBuild build) { if(emailAddress!=null) return emailAddress; // try the inference logic - return MailAddressResolver.resolve(user); - } + if(build != null) + return MailAddressResolver.resolve(build, user); + else + return MailAddressResolver.resolve(user); + } public DescriptorImpl getDescriptor() { return DESCRIPTOR;