-
Notifications
You must be signed in to change notification settings - Fork 119
/
CopyArtifact.java
452 lines (407 loc) · 19.6 KB
/
CopyArtifact.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
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Alan Harder
*
* 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.plugins.copyartifact;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import hudson.DescriptorExtensionList;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.console.HyperlinkNote;
import hudson.diagnosis.OldDataMonitor;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.AbstractBuild;
import hudson.model.AbstractItem;
import hudson.model.AbstractProject;
import hudson.model.Build;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.EnvironmentContributingAction;
import hudson.model.Hudson;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.Item;
import hudson.model.Project;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.RunListener;
import hudson.security.SecurityRealm;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.DescribableList;
import hudson.util.FormValidation;
import hudson.util.XStream2;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
/**
* Build step to copy artifacts from another project.
* @author Alan Harder
*/
public class CopyArtifact extends Builder {
private String projectName;
private final String filter, target;
private /*almost final*/ BuildSelector selector;
@Deprecated private transient Boolean stable;
private final Boolean flatten, optional;
private final Boolean fingerprintArtifacts;
@DataBoundConstructor
public CopyArtifact(String projectName, BuildSelector selector, String filter, String target,
boolean flatten, boolean optional, boolean fingerprintArtifacts) {
// check the permissions only if we can
StaplerRequest req = Stapler.getCurrentRequest();
if (req!=null) {
ItemGroup context = req.findAncestorObject(ItemGroup.class);
if (context == null) context = Jenkins.getInstance();
// Prevents both invalid values and access to artifacts of projects which this user cannot see.
// If value is parameterized, it will be checked when build runs.
if (projectName.indexOf('$') < 0 && new JobResolver(context, projectName).job == null)
projectName = ""; // Ignore/clear bad value to avoid ugly 500 page
}
this.projectName = projectName;
this.selector = selector;
this.filter = Util.fixNull(filter).trim();
this.target = Util.fixNull(target).trim();
this.flatten = flatten ? Boolean.TRUE : null;
this.optional = optional ? Boolean.TRUE : null;
this.fingerprintArtifacts = fingerprintArtifacts ? Boolean.TRUE : null;
}
// Upgrade data from old format
public static class ConverterImpl extends XStream2.PassthruConverter<CopyArtifact> {
public ConverterImpl(XStream2 xstream) { super(xstream); }
@Override protected void callback(CopyArtifact obj, UnmarshallingContext context) {
if (obj.selector == null) {
obj.selector = new StatusBuildSelector(obj.stable != null && obj.stable);
OldDataMonitor.report(context, "1.355"); // Core version# when CopyArtifact 1.2 released
}
}
}
public String getProjectName() {
return projectName;
}
public BuildSelector getBuildSelector() {
return selector;
}
public String getFilter() {
return filter;
}
public String getTarget() {
return target;
}
public boolean isFlatten() {
return flatten != null && flatten;
}
public boolean isOptional() {
return optional != null && optional;
}
public boolean isFingerprintArtifacts() {
return fingerprintArtifacts != null && fingerprintArtifacts;
}
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws InterruptedException {
PrintStream console = listener.getLogger();
String expandedProject = projectName, expandedFilter = filter;
try {
EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables()); // Add in matrix axes..
expandedProject = env.expand(projectName);
JobResolver job = new JobResolver(build.getProject().getParent(),expandedProject);
if (job.job != null && !expandedProject.equals(projectName)
// If projectName is parameterized, need to do permission check on source project.
// Would like to check if user who started build has permission, but unable to get
// Authentication object for arbitrary user.. instead, only allow use of parameters
// to select jobs which are accessible to all authenticated users.
&& !job.job.getACL().hasPermission(
new UsernamePasswordAuthenticationToken("authenticated", "",
new GrantedAuthority[]{ SecurityRealm.AUTHENTICATED_AUTHORITY }),
Item.READ)) {
job.job = null; // Disallow access
}
if (job.job == null) {
console.println(Messages.CopyArtifact_MissingProject(expandedProject));
return false;
}
Run src = selector.getBuild(job.job, env, job.filter, build);
if (src == null) {
console.println(Messages.CopyArtifact_MissingBuild(expandedProject));
return isOptional(); // Fail build unless copy is optional
}
FilePath targetDir = build.getWorkspace(), baseTargetDir = targetDir;
if (targetDir == null || !targetDir.exists()) {
console.println(Messages.CopyArtifact_MissingWorkspace()); // (see JENKINS-3330)
return isOptional(); // Fail build unless copy is optional
}
// Add info about the selected build into the environment
EnvAction envData = build.getAction(EnvAction.class);
if (envData != null) {
envData.add(expandedProject, src.getNumber());
}
if (target.length() > 0) targetDir = new FilePath(targetDir, env.expand(target));
expandedFilter = env.expand(filter);
if (expandedFilter.trim().length() == 0) expandedFilter = "**";
// for backward compatibility, look up the copier as CopyMethod
Copier copier = Copier.from(Jenkins.getInstance().getExtensionList(CopyMethod.class).get(0)).clone();
if (src instanceof MavenModuleSetBuild) {
// Copy artifacts from the build (ArchiveArtifacts build step)
boolean ok = perform(src, build, expandedFilter, targetDir, baseTargetDir, copier, console);
// Copy artifacts from all modules of this Maven build (automatic archiving)
for (Run r : ((MavenModuleSetBuild)src).getModuleLastBuilds().values())
ok |= perform(r, build, expandedFilter, targetDir, baseTargetDir, copier, console);
return ok;
} else if (src instanceof MatrixBuild) {
boolean ok = false;
// Copy artifacts from all configurations of this matrix build
// Use MatrixBuild.getExactRuns if available
for (Run r : ((MatrixBuild) src).getExactRuns())
// Use subdir of targetDir with configuration name (like "jdk=java6u20")
ok |= perform(r, build, expandedFilter, targetDir.child(r.getParent().getName()),
baseTargetDir, copier, console);
return ok;
} else {
return perform(src, build, expandedFilter, targetDir, baseTargetDir, copier, console);
}
}
catch (IOException ex) {
Util.displayIOException(ex, listener);
ex.printStackTrace(listener.error(
Messages.CopyArtifact_FailedToCopy(expandedProject, expandedFilter)));
return false;
}
}
private boolean perform(Run src, AbstractBuild<?,?> dst, String expandedFilter, FilePath targetDir,
FilePath baseTargetDir, Copier copier, PrintStream console)
throws IOException, InterruptedException {
// Check special case for copying from workspace instead of artifacts:
boolean useWs = (selector instanceof WorkspaceSelector && src instanceof AbstractBuild);
FilePath srcDir = useWs ? ((AbstractBuild)src).getWorkspace()
: new FilePath(src.getArtifactsDir());
if (srcDir == null || !srcDir.exists()) {
console.println(useWs ? Messages.CopyArtifact_MissingSrcWorkspace() // (see JENKINS-3330)
: Messages.CopyArtifact_MissingSrcArtifacts());
return isOptional(); // Fail build unless copy is optional
}
copier.init(src,dst,srcDir,baseTargetDir);
try {
int cnt;
if (!isFlatten())
cnt = copier.copyAll(srcDir, expandedFilter, targetDir, isFingerprintArtifacts());
else {
targetDir.mkdirs(); // Create target if needed
FilePath[] list = srcDir.list(expandedFilter);
for (FilePath file : list)
copier.copyOne(file, new FilePath(targetDir, file.getName()), isFingerprintArtifacts());
cnt = list.length;
}
console.println(Messages.CopyArtifact_Copied(cnt, HyperlinkNote.encodeTo('/'+ src.getParent().getUrl(), src.getParent().getFullDisplayName()),
HyperlinkNote.encodeTo('/'+src.getUrl(), Integer.toString(src.getNumber()))));
// Fail build if 0 files copied unless copy is optional
return cnt > 0 || isOptional();
} finally {
copier.end();
}
}
// Find the job from the given name; usually just a Hudson.getItemByFullName lookup,
// but this class encapsulates additional logic like filtering on parameters.
private static class JobResolver {
Job<?,?> job;
BuildFilter filter = new BuildFilter();
JobResolver(ItemGroup context, String projectName) {
job = getItem(context, projectName);
if (job == null) {
// Check for parameterized job with filter (see help file)
int i = projectName.lastIndexOf('/');
if (i > 0) {
Job<?,?> candidate = getItem(context, projectName.substring(0, i));
if (candidate != null) {
ParametersBuildFilter pFilter = new ParametersBuildFilter(projectName.substring(i + 1));
if (pFilter.isValid(candidate)) {
job = candidate;
filter = pFilter;
}
}
}
}
}
// working around a bug in Jenkins < 1.461 that accepts arbitrary bogus tokens as suffix.
private Job getItem(ItemGroup context, String pathName) {
Jenkins jenkins = Jenkins.getInstance();
if (context==null) context = jenkins;
if (pathName==null) return null;
if (pathName.startsWith("/")) // absolute
return jenkins.getItemByFullName(pathName,Job.class);
Object/*Item|ItemGroup*/ ctx = context;
StringTokenizer tokens = new StringTokenizer(pathName,"/");
while (tokens.hasMoreTokens()) {
String s = tokens.nextToken();
if (s.equals("..")) {
if (ctx instanceof Item) {
ctx = ((Item)ctx).getParent();
continue;
}
ctx=null; // can't go up further
break;
}
if (s.equals(".")) {
continue;
}
if (ctx instanceof ItemGroup) {
ItemGroup g = (ItemGroup) ctx;
Item i;
try {
i = g.getItem(s);
} catch (Exception e) {
// working around a bug in MatrixProject that reports IAE.
// With Jenkins > 1.461, this is not necessary
i=null;
}
if (i==null || !i.hasPermission(Item.READ)) {
ctx=null; // can't go up further
break;
}
ctx=i;
} else {
return null;
}
}
if (ctx instanceof Job)
return (Job)ctx;
// fall back to the classic interpretation
return jenkins.getItemByFullName(pathName,Job.class);
}
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
public FormValidation doCheckProjectName(
@AncestorInPath AbstractItem anc, @QueryParameter String value) {
// Require CONFIGURE permission on this project
if (!anc.hasPermission(Item.CONFIGURE)) return FormValidation.ok();
FormValidation result;
Item item = new JobResolver(anc.getParent(),value).job;
if (item != null)
result = item instanceof MavenModuleSet
? FormValidation.warning(Messages.CopyArtifact_MavenProject())
: (item instanceof MatrixProject
? FormValidation.warning(Messages.CopyArtifact_MatrixProject())
: FormValidation.ok());
else if (value.indexOf('$') >= 0)
result = FormValidation.warning(Messages.CopyArtifact_ParameterizedName());
else
result = FormValidation.error(
hudson.tasks.Messages.BuildTrigger_NoSuchProject(
value, AbstractProject.findNearest(value).getName()));
return result;
}
public boolean isApplicable(Class<? extends AbstractProject> clazz) {
return true;
}
public String getDisplayName() {
return Messages.CopyArtifact_DisplayName();
}
public DescriptorExtensionList<BuildSelector,Descriptor<BuildSelector>> getBuildSelectors() {
return Hudson.getInstance().<BuildSelector,Descriptor<BuildSelector>>getDescriptorList(BuildSelector.class);
}
}
// Listen for project renames and update property here if needed.
@Extension
public static final class ListenerImpl extends ItemListener {
@Override
public void onRenamed(Item item, String oldName, String newName) {
for (AbstractProject<?,?> project
: Hudson.getInstance().getAllItems(AbstractProject.class)) {
for (CopyArtifact ca : getCopiers(project)) try {
if (ca.getProjectName().equals(oldName))
ca.projectName = newName;
else if (ca.getProjectName().startsWith(oldName + '/'))
// Support rename for "MatrixJobName/AxisName=value" type of name
ca.projectName = newName + ca.projectName.substring(oldName.length());
else continue;
project.save();
} catch (IOException ex) {
Logger.getLogger(ListenerImpl.class.getName()).log(Level.WARNING,
"Failed to resave project " + project.getName()
+ " for project rename in CopyArtifact build step ("
+ oldName + " =>" + newName + ")", ex);
}
}
}
private static List<CopyArtifact> getCopiers(AbstractProject project) {
DescribableList<Builder,Descriptor<Builder>> list =
project instanceof Project ? ((Project<?,?>)project).getBuildersList()
: (project instanceof MatrixProject ?
((MatrixProject)project).getBuildersList() : null);
if (list == null) return Collections.emptyList();
return (List<CopyArtifact>)list.getAll(CopyArtifact.class);
}
}
// Listen for new builds and add EnvAction in any that use CopyArtifact build step
@Extension
public static final class CopyArtifactRunListener extends RunListener<Build> {
public CopyArtifactRunListener() {
super(Build.class);
}
@Override
public void onStarted(Build r, TaskListener listener) {
if (((Build<?,?>)r).getProject().getBuildersList().get(CopyArtifact.class) != null)
r.addAction(new EnvAction());
}
}
private static class EnvAction implements EnvironmentContributingAction {
// Decided not to record this data in build.xml, so marked transient:
private transient Map<String,String> data = new HashMap<String,String>();
private void add(String projectName, int buildNumber) {
if (data==null) return;
int i = projectName.indexOf('/'); // Omit any detail after a /
if (i > 0) projectName = projectName.substring(0, i);
data.put("COPYARTIFACT_BUILD_NUMBER_"
+ projectName.toUpperCase().replaceAll("[^A-Z]+", "_"), // Only use letters and _
Integer.toString(buildNumber));
}
public void buildEnvVars(AbstractBuild<?,?> build, EnvVars env) {
if (data!=null) env.putAll(data);
}
public String getIconFileName() { return null; }
public String getDisplayName() { return null; }
public String getUrlName() { return null; }
}
}