-
Notifications
You must be signed in to change notification settings - Fork 110
/
BuildWrapperService.java
81 lines (69 loc) · 3.33 KB
/
BuildWrapperService.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
package org.jenkinsci.plugins.envinject.service;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.maven.MavenModuleSet;
import hudson.model.*;
import hudson.tasks.BuildWrapper;
import hudson.util.DescribableList;
import org.jenkinsci.lib.envinject.EnvInjectException;
import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
/**
* @author Gregory Boissinot
*/
public class BuildWrapperService implements Serializable {
@SuppressWarnings("unchecked")
public void addBuildWrapper(AbstractBuild build, BuildWrapper buildWrapper) throws EnvInjectException {
if (buildWrapper == null) {
throw new NullPointerException("A build wrapper object is required.");
}
try {
DescribableList<BuildWrapper, Descriptor<BuildWrapper>> wrappersProject = getBuildWrapperDescriptorDescribableList(build);
wrappersProject.add(buildWrapper);
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
@SuppressWarnings("unchecked")
public void removeBuildWrappers(AbstractBuild build, Class<? extends BuildWrapper>... wrappersClass) throws EnvInjectException {
if (wrappersClass == null) {
throw new NullPointerException("A class wrappers is required.");
}
DescribableList<BuildWrapper, Descriptor<BuildWrapper>> wrappersProject = getBuildWrapperDescriptorDescribableList(build);
Iterator<BuildWrapper> iterator = wrappersProject.iterator();
while (iterator.hasNext()) {
BuildWrapper buildWrapper = iterator.next();
for (Class<? extends BuildWrapper> wrapperClass : wrappersClass) {
if ((wrapperClass.getName()).equals(buildWrapper.getClass().getName())) {
try {
wrappersProject.remove(buildWrapper);
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
}
}
}
private DescribableList<BuildWrapper, Descriptor<BuildWrapper>> getBuildWrapperDescriptorDescribableList(AbstractBuild build) throws EnvInjectException {
DescribableList<BuildWrapper, Descriptor<BuildWrapper>> wrappersProject;
if (build instanceof MatrixRun) {
MatrixProject project = ((MatrixRun) build).getParentBuild().getProject();
return project.getBuildWrappersList();
} else {
AbstractProject abstractProject = build.getProject();
if (abstractProject instanceof FreeStyleProject) {
Project project = (Project) abstractProject;
return project.getBuildWrappersList();
} else if (abstractProject instanceof MavenModuleSet) {
MavenModuleSet moduleSet = (MavenModuleSet) abstractProject;
return moduleSet.getBuildWrappersList();
} else if (Hudson.getInstance().getPlugin("ivy") != null && abstractProject instanceof hudson.ivy.IvyModuleSet) {
hudson.ivy.IvyModuleSet ivyModuleSet = (hudson.ivy.IvyModuleSet) abstractProject;
return ivyModuleSet.getBuildWrappersList();
} else {
throw new EnvInjectException(String.format("Job type %s is not supported", abstractProject));
}
}
}
}