/
TeamFoundationServerScm.java
519 lines (453 loc) · 21.8 KB
/
TeamFoundationServerScm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package hudson.plugins.tfs;
import static hudson.Util.fixEmpty;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import hudson.util.Secret;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.tfs.actions.CheckoutAction;
import hudson.plugins.tfs.actions.RemoveWorkspaceAction;
import hudson.plugins.tfs.browsers.TeamFoundationServerRepositoryBrowser;
import hudson.plugins.tfs.model.Project;
import hudson.plugins.tfs.model.WorkspaceConfiguration;
import hudson.plugins.tfs.model.Server;
import hudson.plugins.tfs.model.ChangeSet;
import hudson.plugins.tfs.util.BuildVariableResolver;
import hudson.plugins.tfs.util.BuildWorkspaceConfigurationRetriever;
import hudson.plugins.tfs.util.BuildWorkspaceConfigurationRetriever.BuildWorkspaceConfiguration;
import hudson.scm.ChangeLogParser;
import hudson.scm.PollingResult;
import hudson.scm.PollingResult.Change;
import hudson.scm.RepositoryBrowsers;
import hudson.scm.SCMRevisionState;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.util.FormValidation;
import hudson.util.LogTaskListener;
import hudson.util.Scrambler;
import hudson.util.VariableResolver;
import org.kohsuke.stapler.QueryParameter;
/**
* SCM for Microsoft Team Foundation Server.
*
* @author Erik Ramfelt
*/
public class TeamFoundationServerScm extends SCM {
public static final String WORKSPACE_ENV_STR = "TFS_WORKSPACE";
public static final String WORKFOLDER_ENV_STR = "TFS_WORKFOLDER";
public static final String PROJECTPATH_ENV_STR = "TFS_PROJECTPATH";
public static final String SERVERURL_ENV_STR = "TFS_SERVERURL";
public static final String USERNAME_ENV_STR = "TFS_USERNAME";
public static final String WORKSPACE_CHANGESET_ENV_STR = "TFS_CHANGESET";
private static final String VERSION_SPEC = "VERSION_SPEC";
private final String serverUrl;
private final String projectPath;
private final String localPath;
private final String workspaceName;
private transient @Deprecated String userPassword;
private /* almost final */ Secret password;
private final String userName;
private final boolean useUpdate;
private TeamFoundationServerRepositoryBrowser repositoryBrowser;
private transient String normalizedWorkspaceName;
private transient String workspaceChangesetVersion;
private static final Logger logger = Logger.getLogger(TeamFoundationServerScm.class.getName());
@Deprecated
public TeamFoundationServerScm(String serverUrl, String projectPath, String localPath, boolean useUpdate, String workspaceName, String userName, String password) {
this(serverUrl, projectPath, localPath, useUpdate, workspaceName, userName, Secret.fromString(password));
}
@DataBoundConstructor
public TeamFoundationServerScm(String serverUrl, String projectPath, String localPath, boolean useUpdate, String workspaceName, String userName, Secret password) {
this.serverUrl = serverUrl;
this.projectPath = projectPath;
this.useUpdate = useUpdate;
this.localPath = (Util.fixEmptyAndTrim(localPath) == null ? "." : localPath);
this.workspaceName = (Util.fixEmptyAndTrim(workspaceName) == null ? "Hudson-${JOB_NAME}-${NODE_NAME}" : workspaceName);
this.userName = userName;
this.password = password;
}
/* Migrate legacy data */
private Object readResolve() {
if (password == null && userPassword != null)
password = Secret.fromString(Scrambler.scramble(userPassword));
return this;
}
// Bean properties need for job configuration
public String getServerUrl() {
return serverUrl;
}
public String getWorkspaceName() {
return workspaceName;
}
public String getProjectPath() {
return projectPath;
}
public String getLocalPath() {
return localPath;
}
public boolean isUseUpdate() {
return useUpdate;
}
public String getUserPassword() {
return Secret.toString(password);
}
public Secret getPassword() {
return password;
}
public String getUserName() {
return userName;
}
// Bean properties END
String getWorkspaceName(AbstractBuild<?,?> build, Computer computer) {
normalizedWorkspaceName = workspaceName;
if (build != null) {
normalizedWorkspaceName = substituteBuildParameter(build, normalizedWorkspaceName);
normalizedWorkspaceName = Util.replaceMacro(normalizedWorkspaceName, new BuildVariableResolver(build.getProject(), computer));
}
normalizedWorkspaceName = normalizedWorkspaceName.replaceAll("[\"/:<>\\|\\*\\?]+", "_");
normalizedWorkspaceName = normalizedWorkspaceName.replaceAll("[\\.\\s]+$", "_");
return normalizedWorkspaceName;
}
public String getServerUrl(Run<?,?> run) {
return substituteBuildParameter(run, serverUrl);
}
String getProjectPath(Run<?,?> run) {
return Util.replaceMacro(substituteBuildParameter(run, projectPath), new BuildVariableResolver(run.getParent()));
}
private String substituteBuildParameter(Run<?,?> run, String text) {
if (run instanceof AbstractBuild<?, ?>){
AbstractBuild<?,?> build = (AbstractBuild<?, ?>) run;
if (build.getAction(ParametersAction.class) != null) {
return build.getAction(ParametersAction.class).substitute(build, text);
}
}
return text;
}
@Override
public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath workspaceFilePath, BuildListener listener, File changelogFile) throws IOException, InterruptedException {
Server server = createServer(new TfTool(getDescriptor().getTfExecutable(), launcher, listener, workspaceFilePath), build);
try {
WorkspaceConfiguration workspaceConfiguration = new WorkspaceConfiguration(server.getUrl(), getWorkspaceName(build, Computer.currentComputer()), getProjectPath(build), getLocalPath());
// Check if the configuration has changed
if (build.getPreviousBuild() != null) {
BuildWorkspaceConfiguration nodeConfiguration = new BuildWorkspaceConfigurationRetriever().getLatestForNode(build.getBuiltOn(), build.getPreviousBuild());
if ((nodeConfiguration != null) &&
nodeConfiguration.workspaceExists()
&& (! workspaceConfiguration.equals(nodeConfiguration))) {
listener.getLogger().println("Deleting workspace as the configuration has changed since a build was performed on this computer.");
new RemoveWorkspaceAction(workspaceConfiguration.getWorkspaceName()).remove(server);
nodeConfiguration.setWorkspaceWasRemoved();
nodeConfiguration.save();
}
}
build.addAction(workspaceConfiguration);
CheckoutAction action = new CheckoutAction(workspaceConfiguration.getWorkspaceName(), workspaceConfiguration.getProjectPath(), workspaceConfiguration.getWorkfolder(), isUseUpdate());
try {
List<ChangeSet> list = checkout(build, workspaceFilePath, server, action);
ChangeSetWriter writer = new ChangeSetWriter();
writer.write(list, changelogFile);
} catch (ParseException pe) {
listener.fatalError(pe.getMessage());
throw new AbortException();
}
try {
setWorkspaceChangesetVersion(null);
String projectPath = workspaceConfiguration.getProjectPath();
Project project = server.getProject(projectPath);
// TODO: even better would be to call this first, then use the changeset when calling checkout
int buildChangeset = project.getRemoteChangesetVersion(build.getTimestamp());
setWorkspaceChangesetVersion(Integer.toString(buildChangeset, 10));
// by adding this action, we prevent calcRevisionsFromBuild() from being called
build.addAction(new TFSRevisionState(buildChangeset, projectPath));
} catch (ParseException pe) {
listener.fatalError(pe.getMessage());
throw new AbortException();
}
} finally {
server.close();
}
return true;
}
private List<ChangeSet> checkout(AbstractBuild<?, ?> build,
FilePath workspaceFilePath, Server server, CheckoutAction action)
throws IOException, InterruptedException, ParseException {
VariableResolver<String> buildVariableResolver = build.getBuildVariableResolver();
String label = buildVariableResolver.resolve(VERSION_SPEC);
if (StringUtils.isNotEmpty(label)) {
if (label.startsWith("L")) {
String preffixRemoved = label.substring(1);
return action.checkoutByLabel(server, workspaceFilePath, preffixRemoved);
}
//TODO to be implemented another checkout strategies...
}
return action.checkout(server, workspaceFilePath, (build.getPreviousBuild() != null ? build.getPreviousBuild().getTimestamp() : null), build.getTimestamp());
}
void setWorkspaceChangesetVersion(String workspaceChangesetVersion) {
this.workspaceChangesetVersion = workspaceChangesetVersion;
}
@Override
public boolean pollChanges(AbstractProject hudsonProject, Launcher launcher, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
Run<?,?> lastRun = hudsonProject.getLastBuild();
if (lastRun == null) {
return true;
} else {
Server server = createServer(new TfTool(getDescriptor().getTfExecutable(), launcher, listener, workspace), lastRun);
try {
return (server.getProject(getProjectPath(lastRun)).getDetailedHistory(
lastRun.getTimestamp(),
Calendar.getInstance()
).size() > 0);
} catch (ParseException pe) {
listener.fatalError(pe.getMessage());
throw new AbortException();
} finally {
server.close();
}
}
}
@Override
public boolean processWorkspaceBeforeDeletion(AbstractProject<?, ?> project, FilePath workspace, Node node) throws IOException, InterruptedException {
Run<?,?> lastRun = project.getLastBuild();
if ((lastRun == null) || !(lastRun instanceof AbstractBuild<?, ?>)) {
return true;
}
// Due to an error in Hudson core (pre 1.321), null was sent in for all invocations of this method
// Therefore we try to work around the problem, and see if its only built on one node or not.
if (node == null) {
while (lastRun != null) {
AbstractBuild<?,?> build = (AbstractBuild<?, ?>) lastRun;
Node buildNode = build.getBuiltOn();
if (node == null) {
node = buildNode;
} else {
if (!buildNode.getNodeName().equals(node.getNodeName())) {
logger.warning("Could not wipe out workspace as there is no way of telling what Node the request is for. Please upgrade Hudson to a newer version.");
return false;
}
}
lastRun = lastRun.getPreviousBuild();
}
if (node == null) {
return true;
}
lastRun = project.getLastBuild();
}
BuildWorkspaceConfiguration configuration = new BuildWorkspaceConfigurationRetriever().getLatestForNode(node, lastRun);
if ((configuration != null) && configuration.workspaceExists()) {
LogTaskListener listener = new LogTaskListener(logger, Level.INFO);
Launcher launcher = node.createLauncher(listener);
Server server = createServer(new TfTool(getDescriptor().getTfExecutable(), launcher, listener, workspace), lastRun);
try {
if (new RemoveWorkspaceAction(configuration.getWorkspaceName()).remove(server)) {
configuration.setWorkspaceWasRemoved();
configuration.save();
}
} finally {
server.close();
}
}
return true;
}
protected Server createServer(TfTool tool, Run<?,?> run) {
return new Server(tool, getServerUrl(run), getUserName(), getUserPassword());
}
@Override
public boolean requiresWorkspaceForPolling() {
return false;
}
@Override
public boolean supportsPolling() {
return true;
}
@Override
public ChangeLogParser createChangeLogParser() {
return new ChangeSetReader();
}
@Override
public FilePath getModuleRoot(FilePath workspace) {
return workspace.child(getLocalPath());
}
@Override
public TeamFoundationServerRepositoryBrowser getBrowser() {
return repositoryBrowser;
}
@Override
public void buildEnvVars(AbstractBuild<?,?> build, Map<String, String> env) {
super.buildEnvVars(build, env);
if (normalizedWorkspaceName != null) {
env.put(WORKSPACE_ENV_STR, normalizedWorkspaceName);
}
if (env.containsKey("WORKSPACE")) {
env.put(WORKFOLDER_ENV_STR, env.get("WORKSPACE") + File.separator + getLocalPath());
}
if (projectPath != null) {
env.put(PROJECTPATH_ENV_STR, projectPath);
}
if (serverUrl != null) {
env.put(SERVERURL_ENV_STR, serverUrl);
}
if (userName != null) {
env.put(USERNAME_ENV_STR, userName);
}
if (workspaceChangesetVersion != null && workspaceChangesetVersion.length() > 0) {
env.put(WORKSPACE_CHANGESET_ENV_STR, workspaceChangesetVersion);
}
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl)super.getDescriptor();
}
@Extension
public static class DescriptorImpl extends SCMDescriptor<TeamFoundationServerScm> {
public static final Pattern WORKSPACE_NAME_REGEX = Pattern.compile("[^\"/:<>\\|\\*\\?]+[^\\s\\.\"/:<>\\|\\*\\?]$", Pattern.CASE_INSENSITIVE);
public static final Pattern USER_AT_DOMAIN_REGEX = Pattern.compile("^([^\\/\\\\\"\\[\\]:|<>+=;,\\*@]+)@([a-z][a-z0-9.-]+)$", Pattern.CASE_INSENSITIVE);
public static final Pattern DOMAIN_SLASH_USER_REGEX = Pattern.compile("^([a-z][a-z0-9.-]+)\\\\([^\\/\\\\\"\\[\\]:|<>+=;,\\*@]+)$", Pattern.CASE_INSENSITIVE);
public static final Pattern PROJECT_PATH_REGEX = Pattern.compile("^\\$\\/.*", Pattern.CASE_INSENSITIVE);
private String tfExecutable;
public DescriptorImpl() {
super(TeamFoundationServerScm.class, TeamFoundationServerRepositoryBrowser.class);
load();
}
public String getTfExecutable() {
if (tfExecutable == null) {
return "tf";
} else {
return tfExecutable;
}
}
@Override
public SCM newInstance(StaplerRequest req, JSONObject formData) throws FormException {
TeamFoundationServerScm scm = (TeamFoundationServerScm) super.newInstance(req, formData);
scm.repositoryBrowser = RepositoryBrowsers.createInstance(TeamFoundationServerRepositoryBrowser.class,req,formData,"browser");
return scm;
}
public FormValidation doExecutableCheck(@QueryParameter final String value) {
return FormValidation.validateExecutable(value);
}
private FormValidation doRegexCheck(final Pattern[] regexArray,
final String noMatchText, final String nullText, String value) {
value = fixEmpty(value);
if (value == null) {
if (nullText == null) {
return FormValidation.ok();
} else {
return FormValidation.error(nullText);
}
}
for (Pattern regex : regexArray) {
if (regex.matcher(value).matches()) {
return FormValidation.ok();
}
}
return FormValidation.error(noMatchText);
}
public FormValidation doUsernameCheck(@QueryParameter final String value) {
return doRegexCheck(new Pattern[]{DOMAIN_SLASH_USER_REGEX, USER_AT_DOMAIN_REGEX},
"Login name must contain the name of the domain and user", null, value );
}
public FormValidation doProjectPathCheck(@QueryParameter final String value) {
return doRegexCheck(new Pattern[]{PROJECT_PATH_REGEX},
"Project path must begin with '$/'.",
"Project path is mandatory.", value );
}
public FormValidation doWorkspaceNameCheck(@QueryParameter final String value) {
return doRegexCheck(new Pattern[]{WORKSPACE_NAME_REGEX},
"Workspace name cannot end with a space or period, and cannot contain any of the following characters: \"/:<>|*?",
"Workspace name is mandatory", value);
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
tfExecutable = Util.fixEmpty(req.getParameter("tfs.tfExecutable").trim());
save();
return true;
}
@Override
public String getDisplayName() {
return "Team Foundation Server";
}
}
@Override
public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build,
Launcher launcher, TaskListener listener) throws IOException,
InterruptedException {
/*
* This method does nothing, since the work has already been done in
* the checkout() method, as per the documentation:
* """
* As an optimization, SCM implementation can choose to compute SCMRevisionState
* and add it as an action during check out, in which case this method will not called.
* """
*/
return null;
}
@Override
protected PollingResult compareRemoteRevisionWith(
AbstractProject<?, ?> project, Launcher launcher,
FilePath workspace, TaskListener listener, SCMRevisionState baseline)
throws IOException, InterruptedException {
final Launcher localLauncher = launcher != null ? launcher : new Launcher.LocalLauncher(listener);
if (!(baseline instanceof TFSRevisionState))
{
// This plugin was just upgraded, we don't yet have a new-style baseline,
// so we perform an old-school poll
boolean shouldBuild = pollChanges(project, localLauncher, workspace, listener);
return shouldBuild ? PollingResult.BUILD_NOW : PollingResult.NO_CHANGES;
}
final TFSRevisionState tfsBaseline = (TFSRevisionState) baseline;
if (!projectPath.equalsIgnoreCase(tfsBaseline.projectPath))
{
// There's no PollingResult.INCOMPARABLE, so we use the next closest thing
return PollingResult.BUILD_NOW;
}
Run<?, ?> build = project.getLastBuild();
final TfTool tool = new TfTool(getDescriptor().getTfExecutable(), localLauncher, listener, workspace);
final Server server = createServer(tool, build);
final Project tfsProject = server.getProject(projectPath);
try {
final List<ChangeSet> briefHistory = tfsProject.getBriefHistory(
tfsBaseline.changesetVersion,
Calendar.getInstance()
);
// TODO: Given we have a tfsBaseline with a changeset,
// briefHistory will probably always contain at least one entry
final TFSRevisionState tfsRemote =
(briefHistory.size() > 0)
? new TFSRevisionState(briefHistory.get(0).getVersion(), projectPath)
: tfsBaseline;
// TODO: we could return INSIGNIFICANT if all the changesets
// contain the string "***NO_CI***" at the end of their comment
final Change change =
tfsBaseline.changesetVersion == tfsRemote.changesetVersion
? Change.NONE
: Change.SIGNIFICANT;
return new PollingResult(tfsBaseline, tfsRemote, change);
} catch (ParseException pe) {
listener.fatalError(pe.getMessage());
throw new AbortException();
} finally {
server.close();
}
}
}