forked from jenkinsci/multiple-scms-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSCM.java
274 lines (236 loc) · 10.3 KB
/
MultiSCM.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
package org.jenkinsci.plugins.multiplescms;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Saveable;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Run;
import hudson.model.Hudson;
import hudson.scm.ChangeLogParser;
import hudson.scm.PollingResult;
import hudson.scm.PollingResult.Change;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import hudson.scm.NullSCM;
import hudson.scm.SCM;
import hudson.util.DescribableList;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
public class MultiSCM extends SCM implements Saveable {
private DescribableList<SCM,Descriptor<SCM>> scms =
new DescribableList<SCM,Descriptor<SCM>>(this);
@DataBoundConstructor
public MultiSCM(List<SCM> scmList) throws IOException {
scms.addAll(scmList);
}
@Exported
public List<SCM> getConfiguredSCMs() {
return scms.toList();
}
@Override
public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build,
Launcher launcher, TaskListener listener) throws IOException,
InterruptedException {
MultiSCMRevisionState revisionStates = new MultiSCMRevisionState();
for(SCM scm : scms) {
SCMRevisionState scmState = scm.calcRevisionsFromBuild(build, launcher, listener);
revisionStates.add(scm, build.getWorkspace(), build, scmState);
}
return revisionStates;
}
@Override
public void buildEnvVars(AbstractBuild<?,?> build, Map<String, String> env) {
// Add each SCM's env vars, appending indices where needed to avoid collisions
for (int i = 0; i < scms.size(); i++) {
try {
EnvVars currScmVars = new EnvVars();
scms.get(i).buildEnvVars(build, currScmVars);
for (Entry<String, String> entry : currScmVars.entrySet()) {
if (env.containsKey(entry.getKey())) {
// We have a collision; append the index of this SCM to the env var name
env.put(entry.getKey() + "_" + i, entry.getValue());
} else {
// No collision; just put the var as usual
env.put(entry.getKey(), entry.getValue());
}
}
}
catch(NullPointerException npe)
{}
}
}
@Override
protected PollingResult compareRemoteRevisionWith(
AbstractProject<?, ?> project, Launcher launcher,
FilePath workspace, TaskListener listener, SCMRevisionState baseline)
throws IOException, InterruptedException {
MultiSCMRevisionState baselineStates = baseline instanceof MultiSCMRevisionState ? (MultiSCMRevisionState) baseline : null;
MultiSCMRevisionState currentStates = new MultiSCMRevisionState();
Change overallChange = Change.NONE;
for(SCM scm : scms) {
SCMRevisionState scmBaseline = baselineStates != null ? baselineStates.get(scm, workspace, null) : null;
if (scmBaseline instanceof MultiSCMRevisionState
&& !(scm instanceof MultiSCM)) {
continue;
}
PollingResult scmResult = scm.poll(project, launcher, workspace, listener, scmBaseline != null ? scmBaseline : SCMRevisionState.NONE);
currentStates.add(scm, workspace, null, scmResult.remote);
if(scmResult.change.compareTo(overallChange) > 0)
overallChange = scmResult.change;
}
return new PollingResult(baselineStates, currentStates, overallChange);
}
@Override
public void checkout(Run<?, ?> build, Launcher launcher,
FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline)
throws IOException, InterruptedException {
MultiSCMRevisionState oldBaseline = baseline instanceof MultiSCMRevisionState ? (MultiSCMRevisionState) baseline : null;
MultiSCMRevisionState revisionState = new MultiSCMRevisionState();
build.addAction(revisionState);
HashSet<Object> scmActions = new HashSet<Object>();
FileOutputStream logStream = new FileOutputStream(changelogFile);
OutputStreamWriter logWriter = new OutputStreamWriter(logStream);
logWriter.write(String.format("<%s>\n", MultiSCMChangeLogParser.ROOT_XML_TAG));
for(SCM scm : scms) {
File subChangeLog = changelogFile != null ? new File(changelogFile.getPath() + ".temp") : null;
SCMRevisionState workspaceRevision = null;
if (oldBaseline != null) {
workspaceRevision = oldBaseline.get(scm, workspace, build instanceof AbstractBuild ? (AbstractBuild) build : null);
}
scm.checkout(build, launcher, workspace, listener, subChangeLog, workspaceRevision);
List<Action> actions = build.getActions();
for(Action a : actions) {
if(!scmActions.contains(a) && a instanceof SCMRevisionState && !(a instanceof MultiSCMRevisionState)) {
scmActions.add(a);
revisionState.add(scm, workspace, build, (SCMRevisionState) a);
}
}
if (subChangeLog != null && subChangeLog.exists()) {
String subLogText = FileUtils.readFileToString(subChangeLog);
//Dont forget to escape the XML in case there is any CDATA sections
logWriter.write(String.format("<%s scm=\"%s\">\n<![CDATA[%s]]>\n</%s>\n",
MultiSCMChangeLogParser.SUB_LOG_TAG,
scm.getKey(),
StringEscapeUtils.escapeXml(subLogText),
MultiSCMChangeLogParser.SUB_LOG_TAG));
subChangeLog.delete();
}
}
logWriter.write(String.format("</%s>\n", MultiSCMChangeLogParser.ROOT_XML_TAG));
logWriter.close();
}
@Override
public FilePath[] getModuleRoots(FilePath workspace, AbstractBuild build) {
ArrayList<FilePath> paths = new ArrayList<FilePath>();
for(SCM scm : scms) {
FilePath[] p = scm.getModuleRoots(workspace, build);
for(FilePath p2 : p)
paths.add(p2);
}
return paths.toArray(new FilePath[paths.size()]);
}
// Only return supportsPolling when all scms do report back that
// they supports polling
@Override
public boolean supportsPolling()
{
for(SCM scm : scms) {
if (!scm.supportsPolling()) return false;
}
return true;
}
// When one scm does require a workspace we return true, else
// we don't need a workspace for polling
@Override
public boolean requiresWorkspaceForPolling()
{
for(SCM scm : scms) {
if (scm.requiresWorkspaceForPolling()) return true;
}
return false;
}
@Override
public ChangeLogParser createChangeLogParser() {
return new MultiSCMChangeLogParser(scms.toList());
}
public void save() throws IOException {
// TODO Auto-generated method stub
}
@Extension // this marker indicates Hudson that this is an implementation of an extension point.
public static final class DescriptorImpl extends SCMDescriptor<MultiSCM> {
public DescriptorImpl() {
super(MultiSCMRepositoryBrowser.class);
// TODO Auto-generated constructor stub
}
public List<SCMDescriptor<?>> getApplicableSCMs(AbstractProject<?, ?> project) {
List<SCMDescriptor<?>> scms = new ArrayList<SCMDescriptor<?>>();
for(SCMDescriptor<?> scm : SCM._for(project)) {
// Filter MultiSCM itself from the list of choices.
// Theoretically it might work, but I see no practical reason to allow
// nested MultiSCM configurations.
if(!(scm instanceof DescriptorImpl) && !(scm instanceof NullSCM.DescriptorImpl))
scms.add(scm);
}
return scms;
}
/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "Multiple SCMs";
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
save();
return super.configure(req,formData);
}
@Override
public SCM newInstance(StaplerRequest req, JSONObject formData)
throws FormException {
// Read descriptions and invoke newInstance() for each of them
List<SCM> scmList = new LinkedList<SCM>();
if (formData.containsKey("scmList")) {
JSONObject scm = formData.optJSONObject("scmList");
if (scm == null) {
for (Object obj : formData.getJSONArray("scmList")) {
readItem(req, (JSONObject)obj, scmList);
}
} else {
readItem(req, scm, scmList);
}
}
// return list and wrap exception
try {
return new MultiSCM(scmList);
} catch (IOException ex) {
throw new FormException(ex, "scmList");
}
}
private static void readItem(StaplerRequest req, JSONObject obj, List<SCM> dest) throws FormException {
String staplerClass = obj.getString("stapler-class");
Descriptor<SCM> d = (Descriptor<SCM>) Hudson.getInstance().getDescriptor(staplerClass);
dest.add(d.newInstance(req, obj));
}
}
}