Skip to content

Commit 336397b

Browse files
committed
pollChanges needs different methods to get the correct workspaceName
1 parent c94b291 commit 336397b

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

src/main/java/com/codicesoftware/plugins/hudson/PlasticSCM.java

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public PlasticSCM(String workspaceName, String selector, String workfolder, bool
5555
this.useUpdate = useUpdate;
5656
}
5757

58+
/**
59+
* This is a getter for Hudson only! Don't use it
60+
* See getWorkspace() instead or better use the WorkSpaceConfig from getWorkspaceConfigurationForBuild()!
61+
* @return the raw string that is set in build config
62+
*/
5863
public String getWorkspaceName() {
5964
return workspaceName;
6065
}
@@ -75,20 +80,57 @@ public boolean isUseUpdate() {
7580
public ChangeLogParser createChangeLogParser() {
7681
return new ChangeSetReader();
7782
}
78-
79-
String getWorkspace(AbstractBuild<?,?> build, Computer computer) {
83+
84+
protected WorkspaceConfiguration getWorkspaceConfigurationForBuild(AbstractBuild build)
85+
{
86+
return new WorkspaceConfiguration(
87+
normalizeAndEvaluateWorkspaceNameStringForBuild(build),
88+
selector,
89+
workfolder
90+
);
91+
}
92+
93+
protected WorkspaceConfiguration getWorkspaceConfigurationForJob(Job job)
94+
{
95+
return new WorkspaceConfiguration(
96+
normalizeAndEvaluateWorkspaceNameStringForJob(job),
97+
selector,
98+
workfolder
99+
);
100+
}
101+
102+
private String normalizeAndEvaluateWorkspaceNameStringForBuild(AbstractBuild<?,?> build) {
80103
normalizedWorkspace = workspaceName;
81-
104+
82105
if (build != null) {
83106
normalizedWorkspace = substituteBuildParameter(build, normalizedWorkspace);
84-
normalizedWorkspace = Util.replaceMacro(normalizedWorkspace, new BuildVariableResolver(build.getProject(), computer));
107+
normalizedWorkspace = substituteJobParameter(build.getProject(), normalizedWorkspace);
85108
}
86-
normalizedWorkspace = normalizedWorkspace.replaceAll("[\"/:<>\\|\\*\\?]+", "_");
87-
normalizedWorkspace = normalizedWorkspace.replaceAll("[\\.\\s]+$", "_");
109+
normalizedWorkspace = substituteInvalidCharacters(normalizedWorkspace);
110+
return normalizedWorkspace;
111+
}
112+
113+
private String normalizeAndEvaluateWorkspaceNameStringForJob(Job<?,?> job) {
114+
normalizedWorkspace = workspaceName;
115+
116+
if (job != null) {
117+
normalizedWorkspace = substituteJobParameter(job, normalizedWorkspace);
118+
}
119+
normalizedWorkspace = substituteInvalidCharacters(normalizedWorkspace);
88120

89121
return normalizedWorkspace;
90122
}
91123

124+
private String substituteInvalidCharacters(String text) {
125+
text = text.replaceAll("[\"/:<>\\|\\*\\?]+", "_");
126+
text = text.replaceAll("[\\.\\s]+$", "_");
127+
return text;
128+
}
129+
130+
private String substituteJobParameter(Job<?,?> prj, String text) {
131+
return Util.replaceMacro(text, new BuildVariableResolver(prj, Computer.currentComputer()));
132+
}
133+
92134
private String substituteBuildParameter(Run<?,?> run, String text) {
93135
if (run instanceof AbstractBuild<?,?>) {
94136
AbstractBuild<?,?> build = (AbstractBuild<?,?>)run;
@@ -104,7 +146,7 @@ private String substituteBuildParameter(Run<?,?> run, String text) {
104146
public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspaceFilePath,
105147
BuildListener listener, File changelogFile) throws IOException, InterruptedException {
106148
Server server = new Server(new PlasticTool(getDescriptor().getCmExecutable(), launcher, listener, workspaceFilePath));
107-
WorkspaceConfiguration workspaceConfiguration = new WorkspaceConfiguration(getWorkspace(build, Computer.currentComputer()), selector, workfolder);
149+
WorkspaceConfiguration workspaceConfiguration = getWorkspaceConfigurationForBuild(build);
108150

109151
if (build.getPreviousBuild() != null) {
110152
BuildWorkspaceConfiguration nodeConfiguration = new BuildWorkspaceConfigurationRetriever().getLatestForNode(build.getBuiltOn(), build.getPreviousBuild());
@@ -135,11 +177,13 @@ public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspa
135177
@Override
136178
public boolean pollChanges(AbstractProject hudsonProject, Launcher launcher, FilePath workspaceFilePath, TaskListener listener) throws IOException, InterruptedException {
137179
Run<?,?> lastRun = hudsonProject.getLastBuild();
180+
138181
if (lastRun == null) {
139182
return true;
140183
} else {
184+
WorkspaceConfiguration workspaceConfiguration = getWorkspaceConfigurationForJob(lastRun.getParent());
141185
Server server = new Server(new PlasticTool(getDescriptor().getCmExecutable(), launcher, listener, workspaceFilePath));
142-
Workspace workspace = server.getWorkspaces().getWorkspace(workspaceName);
186+
Workspace workspace = server.getWorkspaces().getWorkspace(workspaceConfiguration.getWorkspaceName());
143187
try {
144188
return (workspace.getBriefHistory(lastRun.getTimestamp(), Calendar.getInstance()).size() > 0);
145189
} catch (ParseException e) {

src/main/java/com/codicesoftware/plugins/hudson/util/BuildVariableResolver.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package com.codicesoftware.plugins.hudson.util;
22

3+
import hudson.Launcher;
4+
import hudson.Util;
5+
import hudson.model.*;
6+
import hudson.util.VariableResolver;
7+
38
import java.io.IOException;
49
import java.util.ArrayList;
510
import java.util.HashMap;
611
import java.util.List;
712
import java.util.Map;
813
import java.util.logging.Logger;
914

10-
import hudson.Launcher;
11-
import hudson.Util;
12-
import hudson.model.AbstractBuild;
13-
import hudson.model.AbstractProject;
14-
import hudson.model.Computer;
15-
import hudson.model.Job;
16-
import hudson.model.TaskListener;
17-
import hudson.util.VariableResolver;
18-
1915
/**
2016
* A {@link VariableResolver} that resolves certain Build variables.
2117
* <p>
@@ -41,16 +37,7 @@ public class BuildVariableResolver implements VariableResolver<String> {
4137

4238
private static final Logger LOGGER = Logger.getLogger(BuildVariableResolver.class.getName());
4339

44-
public BuildVariableResolver(final Job<?, ?> job) {
45-
computer = null;
46-
lazyResolvers.put("JOB_NAME", new LazyResolver() {
47-
public String getValue() {
48-
return job.getName();
49-
}
50-
});
51-
}
52-
53-
public BuildVariableResolver(final AbstractProject<?, ?> project, final Computer computer) {
40+
public BuildVariableResolver(final Job<?, ?> project, final Computer computer) {
5441
this.computer = computer;
5542
lazyResolvers.put("JOB_NAME", new LazyResolver() {
5643
public String getValue() {

0 commit comments

Comments
 (0)