forked from eficode/ClearCaseUCMPlugin
/
CCUCMScm.java
1128 lines (935 loc) · 43.5 KB
/
CCUCMScm.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.praqma.hudson.scm;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.scm.ChangeLogParser;
import hudson.scm.PollingResult;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import hudson.scm.SCM;
import hudson.util.FormValidation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.praqma.clearcase.exceptions.DeliverException;
import net.praqma.clearcase.exceptions.DeliverException.Type;
import net.praqma.clearcase.exceptions.UnableToInitializeEntityException;
import net.praqma.clearcase.ucm.entities.*;
import net.praqma.clearcase.ucm.entities.Project;
import net.praqma.clearcase.ucm.view.SnapshotView;
import net.praqma.hudson.CCUCMBuildAction;
import net.praqma.hudson.Config;
import net.praqma.hudson.Util;
import net.praqma.hudson.exception.CCUCMException;
import net.praqma.hudson.exception.DeliverNotCancelledException;
import net.praqma.hudson.exception.TemplateException;
import net.praqma.hudson.nametemplates.NameTemplate;
import net.praqma.hudson.notifier.CCUCMNotifier;
import net.praqma.hudson.remoting.*;
import net.praqma.hudson.remoting.deliver.GetChanges;
import net.praqma.hudson.remoting.deliver.MakeDeliverView;
import net.praqma.hudson.remoting.deliver.StartDeliver;
import static net.praqma.hudson.scm.CCUCMScm.getLastAction;
import net.praqma.hudson.scm.Polling.PollingType;
import net.praqma.util.execute.AbnormalProcessTerminationException;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
/**
* is responsible for everything regarding Hudsons connection to ClearCase
* pre-build. This class defines all the files required by the user. The
* information can be entered on the config page.
*/
public class CCUCMScm extends SCM {
private static final Logger logger = Logger.getLogger(CCUCMScm.class.getName());
private Boolean multisitePolling;
private Project.PromotionLevel plevel;
private String loadModule;
private String component;
private String stream;
private String bl;
private StringBuffer pollMsgs = new StringBuffer();
private Stream integrationstream;
private String buildProject;
private String jobName = "";
private Integer jobNumber;
private boolean forceDeliver;
/**
* Determines whether to remove the view private files or not
*/
private boolean removeViewPrivateFiles;
private boolean trimmedChangeSet;
private boolean discard;
/* Old notifier fields */
private boolean recommend;
private boolean makeTag;
private boolean setDescription;
private Unstable treatUnstable;
private boolean createBaseline;
private String nameTemplate;
private Polling polling;
private String viewtag = "";
private Baseline lastBaseline;
private String levelToPoll;
private boolean addPostBuild = true;
/**
* Default constructor, mainly used for unit tests.
*/
public CCUCMScm() {
discard = false;
}
/**
* To support backwards compatibility.
*
* @since 1.4.0
*/
public CCUCMScm(String component, String levelToPoll, String loadModule, boolean newest, String polling, String stream, String treatUnstable,
boolean createBaseline, String nameTemplate, boolean forceDeliver, boolean recommend, boolean makeTag, boolean setDescription, String buildProject) {
this(component, levelToPoll, loadModule, newest, polling, stream, treatUnstable, createBaseline, nameTemplate, forceDeliver, recommend, makeTag, setDescription, buildProject, true, false, false);
}
@DataBoundConstructor
public CCUCMScm(String component, String levelToPoll, String loadModule, boolean newest, String polling, String stream, String treatUnstable,
boolean createBaseline, String nameTemplate, boolean forceDeliver, boolean recommend, boolean makeTag, boolean setDescription, String buildProject, boolean removeViewPrivateFiles, boolean trimmedChangeSet, boolean discard) {
this.component = component;
this.loadModule = loadModule;
this.stream = stream;
this.buildProject = buildProject;
this.polling = new Polling(polling);
this.treatUnstable = new Unstable(treatUnstable);
this.createBaseline = createBaseline;
this.nameTemplate = nameTemplate;
this.forceDeliver = forceDeliver;
this.removeViewPrivateFiles = removeViewPrivateFiles;
this.trimmedChangeSet = trimmedChangeSet;
this.recommend = recommend;
this.makeTag = makeTag;
this.setDescription = setDescription;
this.plevel = Util.getLevel(levelToPoll);
this.levelToPoll = levelToPoll;
this.discard = discard;
}
@Override
public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath workspace, BuildListener listener, File changelogFile) throws IOException, InterruptedException {
/* Prepare job variables */
jobName = build.getParent().getDisplayName().replace(' ', '_');
jobNumber = build.getNumber();
PrintStream out = listener.getLogger();
/* Printing short description of build */
String version = Hudson.getInstance().getPlugin("clearcase-ucm-plugin").getWrapper().getVersion();
out.println("[" + Config.nameShort + "] ClearCase UCM Plugin version " + version);
out.println("[" + Config.nameShort + "] Allow for slave polling: " + this.getSlavePolling());
out.println("[" + Config.nameShort + "] Poll for posted deliveries: " + this.getMultisitePolling());
logger.info("ClearCase UCM plugin v. " + version);
/* Check for ClearCase on remote */
try {
workspace.act(new RemoteClearCaseCheck());
} catch (AbnormalProcessTerminationException e) {
build.setDescription(e.getMessage());
throw e;
}
/* Make build action */
/* Store the configuration */
CCUCMBuildAction action = null;
try {
action = getBuildAction();
} catch (UnableToInitializeEntityException e) {
Util.println(out, e);
throw new AbortException(e.getMessage());
}
action.setBuild(build);
build.addAction(action);
action.setListener(listener);
/* Determining the user has parameterized a Baseline */
String baselineInput = getBaselineValue(build);
logger.fine("PUBLISHERS BEFORE: " + build.getProject().getPublishersList());
if (addPostBuild) {
ensurePublisher(build);
}
out.println("PUBLISHERS AFTER: " + build.getProject().getPublishersList());
/* The special Baseline parameter case */
if (build.getBuildVariables().get(baselineInput) != null) {
logger.fine("Baseline parameter: " + baselineInput);
action.setPolling(new Polling(PollingType.none));
polling = action.getPolling();
try {
resolveBaselineInput(build, baselineInput, action, listener);
} catch (Exception e) {
logger.log(Level.WARNING, "Resolving baseline input failed", e);
Util.println(out, "No Baselines found");
}
} else {
out.println("[" + Config.nameShort + "] Polling streams: " + polling.toString());
try {
resolveBaseline(workspace, build.getProject(), action, listener);
} catch (CCUCMException e) {
logger.warning(e.getMessage());
/* If the promotion level is not set, ANY, use the last found Baseline */
if (plevel == null) {
logger.fine("Promotion level was null [=ANY], finding the last built baseline");
CCUCMBuildAction last = getLastAction(build.getProject());
if (action != null) {
action.setBaseline(last.getBaseline());
} else {
build.setDescription("No valid baselines found");
throw new AbortException("No valid baselines found");
}
} else {
build.setDescription("No valid baselines found");
throw new AbortException("No valid baselines found");
}
} catch (IOException e) {
Exception cause = (Exception) e.getCause();
if (cause != null) {
action.setResolveBaselineException(cause);
build.setDescription(cause.getMessage());
throw new AbortException(cause.getMessage());
} else {
throw new AbortException("Unable to list baselines");
}
}
}
/* Try to save */
build.save();
boolean result = true;
/* If a baseline is found */
if (action.getBaseline() != null) {
out.println("[" + Config.nameShort + "] Using " + action.getBaseline().getNormalizedName());
if (polling.isPollingSelf() || !polling.isPolling()) {
logger.fine("Initializing workspace");
result = initializeWorkspace(build, workspace, changelogFile, listener, action);
} else {
/* Only start deliver when NOT polling self */
logger.fine("Deliver");
SnapshotView snapshotView = initializeDeliverView(build, action, listener);
generateChangeLog(build, action, listener, changelogFile, snapshotView);
}
action.setViewTag(viewtag);
}
out.println("[" + Config.nameShort + "] Pre build steps done");
/* We'll switch it back after using it, because it is only for testing purposes */
addPostBuild = true;
/* If there's a result let's find out whether a baseline is found or not */
if (action.getBaseline() == null) {
out.println("[" + Config.nameShort + "] Finished processing, no baseline found");
} else {
out.println("[" + Config.nameShort + "] Finished processing " + action.getBaseline());
}
logger.fine("ENDING CHECKOUT");
return result;
}
@Override
public void postCheckout(AbstractBuild<?, ?> build, Launcher launcher, FilePath workspace, BuildListener listener) throws IOException, InterruptedException {
logger.fine("BEGINNING POSTCHECKOUT");
/* This is really only interesting if child or sibling polling */
if (polling.isPollingOther()) {
CCUCMBuildAction state = build.getAction(CCUCMBuildAction.class);
PrintStream consoleOutput = listener.getLogger();
try {
logger.fine("Starting deliver");
StartDeliver sd = new StartDeliver(listener, state.getStream(), state.getBaseline(), state.getSnapshotView(), loadModule, state.doForceDeliver(), state.doRemoveViewPrivateFiles());
workspace.act(sd);
consoleOutput.println("[" + Config.nameShort + "] Deliver successful");
/* Deliver failed */
} catch (Exception e) {
consoleOutput.println("[" + Config.nameShort + "] Deliver failed");
/* Check for exception types */
Exception cause = (Exception) net.praqma.util.ExceptionUtils.unpackFrom(IOException.class, e);
consoleOutput.println("[" + Config.nameShort + "] Cause: " + cause.getClass());
/* Re-throw */
try {
logger.log(Level.WARNING, "", cause);
throw cause;
} catch (DeliverException de) {
consoleOutput.println("[" + Config.nameShort + "] " + de.getType());
/* We need to store this information anyway */
state.setViewPath(de.getDeliver().getViewContext());
state.setViewTag(de.getDeliver().getViewtag());
/* The deliver is started, cancel it */
if (de.isStarted()) {
try {
consoleOutput.print("[" + Config.nameShort + "] Cancelling deliver. ");
RemoteUtil.completeRemoteDeliver(workspace, listener, state.getBaseline(), state.getStream(), de.getDeliver().getViewtag(), de.getDeliver().getViewContext(), false);
consoleOutput.println("Success");
/* Make sure, that the post step is not run */
state.setNeedsToBeCompleted(false);
} catch (Exception ex) {
consoleOutput.println("[" + Config.nameShort + "] Failed to cancel deliver");
consoleOutput.println(ExceptionUtils.getFullStackTrace(e));
logger.warning(ExceptionUtils.getFullStackTrace(e));
}
} else {
logger.fine("No need for completing deliver");
state.setNeedsToBeCompleted(false);
}
/* Write something useful to the output */
if (de.getType().equals(Type.MERGE_ERROR)) {
consoleOutput.println("[" + Config.nameShort + "] Changes need to be manually merged, The stream " + state.getBaseline().getStream().getShortname() + " must be rebased to the most recent baseline on " + state.getStream().getShortname() + " - During the rebase the merge conflict should be solved manually. Hereafter create a new baseline on " + state.getBaseline().getStream().getShortname() + ".");
state.setError("merge error");
}
/* Force deliver not cancelled */
} catch (DeliverNotCancelledException e1) {
consoleOutput.println("[" + Config.nameShort + "] Failed to force cancel existing deliver");
state.setNeedsToBeCompleted(false);
} catch (Exception e1) {
logger.log(Level.WARNING, "", e);
e.printStackTrace(consoleOutput);
}
throw new AbortException("Unable to start deliver");
}
}
logger.fine("ENDING POSTCHECKOUT");
}
public void ensurePublisher(AbstractBuild build) throws IOException {
Describable describable = build.getProject().getPublishersList().get(CCUCMNotifier.class);
if (describable == null) {
logger.info("Adding notifier to project");
build.getProject().getPublishersList().add(new CCUCMNotifier());
}
}
public void setAddPostBuild(boolean addPostBuild) {
this.addPostBuild = addPostBuild;
}
private boolean checkInput(TaskListener listener) {
PrintStream out = listener.getLogger();
out.println("[" + Config.nameShort + "] Verifying input");
/* Check baseline template */
if (createBaseline) {
/* Sanity check */
if (polling.isPollingOther()) {
if (nameTemplate != null && nameTemplate.length() > 0) {
if (nameTemplate.matches("^\".+\"$")) {
nameTemplate = nameTemplate.substring(1, nameTemplate.length() - 1);
}
try {
NameTemplate.testTemplate(nameTemplate);
} catch (TemplateException e) {
out.println("[" + Config.nameShort + "] The template could not be parsed correctly: " + e.getMessage());
return false;
}
} else {
out.println("[" + Config.nameShort + "] A valid template must be provided to create a Baseline");
return false;
}
} else {
out.println("[" + Config.nameShort + "] You cannot create a baseline in this mode");
return false;
}
}
/* Check polling vs plevel */
if (plevel == null) {
if (polling.isPollingSelf()) {
return true;
} else {
out.println("[" + Config.nameShort + "] You cannot poll any on other than self");
return false;
}
}
return true;
}
private boolean initializeWorkspace(AbstractBuild<?, ?> build, FilePath workspace, File changelogFile, BuildListener listener, CCUCMBuildAction action) throws IOException, InterruptedException {
PrintStream consoleOutput = listener.getLogger();
EstablishResult er = null;
CheckoutTask ct = new CheckoutTask(listener, jobName, build.getNumber(), action.getStream(), loadModule, action.getBaseline(), buildProject, (plevel == null), action.doRemoveViewPrivateFiles());
er = workspace.act(ct);
//String changelog = er.getMessage();
String changelog = "";
changelog = Util.createChangelog(er.getActivities(), action.getBaseline(), trimmedChangeSet, discard, er.getView().getViewRoot(), er.getView().getReadOnlyLoadLines());
action.setActivities(er.getActivities());
this.viewtag = er.getViewtag();
/* Write change log */
try {
FileOutputStream fos = new FileOutputStream(changelogFile);
fos.write(changelog.getBytes());
fos.close();
} catch (IOException e) {
logger.fine("Could not write change log file");
consoleOutput.println("[" + Config.nameShort + "] Could not write change log file");
}
return true;
}
/**
* Resolve the {@link Baseline} parameter and store the information in the
* Action
*
* @param build
* @param baselineInput
* @param action
* @param listener
* @throws UnableToInitializeEntityException
* @throws CCUCMException
*/
public void resolveBaselineInput(AbstractBuild<?, ?> build, String baselineInput, CCUCMBuildAction action, BuildListener listener) throws UnableToInitializeEntityException, IOException, InterruptedException {
PrintStream consoleOutput = listener.getLogger();
String baselinename = build.getBuildVariables().get(baselineInput);
action.setBaseline(Baseline.get(baselinename));
/* Load the baseline */
action.setBaseline((Baseline) RemoteUtil.loadEntity(build.getWorkspace(), action.getBaseline(), true));
action.setStream(action.getBaseline().getStream());
consoleOutput.println("[" + Config.nameShort + "] Starting parameterized build with a Baseline.");
action.setComponent(action.getBaseline().getComponent());
action.setStream(action.getBaseline().getStream());
}
public String getBaselineValue(AbstractBuild<?, ?> build) {
Collection<?> c = build.getBuildVariables().keySet();
Iterator<?> i = c.iterator();
while (i.hasNext()) {
String next = i.next().toString();
if (next.equalsIgnoreCase("baseline")) {
return next;
}
}
return null;
}
/**
* Resolve the {@link Baseline} to be build
*
* @param workspace
* @param project
* @param action
* @param listener
* @throws CCUCMException is thrown if no valid baselines are found
*/
private void resolveBaseline(FilePath workspace, AbstractProject<?, ?> project, CCUCMBuildAction action, BuildListener listener) throws IOException, InterruptedException, CCUCMException {
logger.fine("Resolving Baseline from the Stream " + action.getStream().getNormalizedName());
PrintStream out = listener.getLogger();
printParameters(out);
/* The Stream must be loaded */
action.setStream((Stream) RemoteUtil.loadEntity(workspace, action.getStream(), getSlavePolling()));
List<Baseline> baselines = null;
/* We need to discriminate on promotion level, JENKINS-16620 */
Date date = null;
if (plevel == null) {
CCUCMBuildAction lastAction = getLastAction(project);
if (lastAction != null) {
date = lastAction.getBaseline().getDate();
}
}
/* Find the Baselines and store them, none of the methods returns null! At least an empty list */
/* Old skool self polling */
if (polling.isPollingSelf()) {
baselines = getValidBaselinesFromStream(workspace, plevel, action.getStream(), action.getComponent(), date);
} else {
baselines = getBaselinesFromStreams(workspace, listener, out, action.getStream(), action.getComponent(), polling.isPollingChilds(), date);
}
/* if we did not find any baselines we should return false */
if (baselines.size() < 1) {
throw new CCUCMException("No valid Baselines found");
}
/* Select and load baseline */
action.setBaseline(selectBaseline(baselines, plevel, workspace));
/* Print the baselines to jenkins out */
printBaselines(baselines, out);
out.println("");
}
/**
* Initialize the deliver view
*/
public SnapshotView initializeDeliverView(AbstractBuild<?, ?> build, CCUCMBuildAction state, BuildListener listener) throws IOException, InterruptedException {
FilePath workspace = build.getWorkspace();
PrintStream consoleOutput = listener.getLogger();
logger.fine("Initializing deliver view");
MakeDeliverView mdv = new MakeDeliverView(listener, build.getParent().getDisplayName(), loadModule, state.getStream());
SnapshotView view = workspace.act(mdv);
state.setViewPath(view.getViewRoot());
state.setViewTag(view.getViewtag());
state.setSnapshotView(view);
this.viewtag = view.getViewtag();
return view;
}
/**
* Generate the change log for poll/sibling mode
* @param build
* @throws java.lang.InterruptedException
*/
public void generateChangeLog(AbstractBuild<?, ?> build, CCUCMBuildAction state, BuildListener listener, File changelogFile, SnapshotView snapshotView) throws IOException, InterruptedException {
FilePath workspace = build.getWorkspace();
PrintStream consoleOutput = listener.getLogger();
logger.fine("Generating change log");
logger.fine(String.format( "Trim changeset = %s, Discard changes under read-only = %s", trimmedChangeSet, discard ) );
GetChanges gc = new GetChanges(listener, state.getStream(), state.getBaseline(), snapshotView.getPath());
List<Activity> activities = workspace.act(gc);
String changelog = "";
changelog = Util.createChangelog(activities, state.getBaseline(), trimmedChangeSet, discard, new File(snapshotView.getPath()), snapshotView.getReadOnlyLoadLines());
state.setActivities(activities);
/* Write change log */
try {
FileOutputStream fos = new FileOutputStream(changelogFile);
fos.write(changelog.getBytes());
fos.close();
} catch (IOException e) {
logger.fine("Could not write change log file");
consoleOutput.println("[" + Config.nameShort + "] Could not write change log file");
}
}
@Override
public ChangeLogParser createChangeLogParser() {
return new ChangeLogParserImpl();
}
@Override
public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
super.buildEnvVars(build, env);
String CC_BASELINE = "";
String CC_VIEWPATH = "";
String CC_VIEWTAG = "";
try {
CCUCMBuildAction action = build.getAction(CCUCMBuildAction.class);
CC_BASELINE = action.getBaseline().getFullyQualifiedName();
} catch (Exception e1) {
if (build != null) {
System.out.println( String.format ( "Failure in build environment variables (buildEnvVars) for job %s %nFull trace written to log", build.getProject().getName()) );
} else {
System.out.println( String.format ( "Build is null in buildEnvVars.%nTrace written to log as no other information can be gathered" ) );
}
logger.log(Level.WARNING, "Exception caught in buildEnvVars method", e1);
}
/* View tag */
CC_VIEWTAG = viewtag;
/* View path */
String workspace = env.get("WORKSPACE");
if (workspace != null) {
CC_VIEWPATH = workspace + File.separator + "view";
} else {
CC_VIEWPATH = "";
}
env.put("CC_BASELINE", CC_BASELINE);
env.put("CC_VIEWTAG", CC_VIEWTAG);
env.put("CC_VIEWPATH", CC_VIEWPATH);
}
/**
* This method polls the version control system to see if there are any
* changes in the source code.
*
* @param project
* @param launcher
* @param workspace
* @param listener
* @param rstate
* @return
* @throws java.io.IOException
* @throws java.lang.InterruptedException
*/
@Override
public PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project, Launcher launcher, FilePath workspace, TaskListener listener, SCMRevisionState rstate) throws IOException, InterruptedException {
workspace.act(new RemoteClearCaseCheck());
jobName = project.getDisplayName().replace(' ', '_');
jobNumber = project.getNextBuildNumber();
PollingResult p = PollingResult.NO_CHANGES;
/*
See [FB11107]. This is also related to JENKINS-14806. Discovered during investigation of performance issues. We need to talk
to Leif and possibly Lars to figure out why we distinguish between multisite and non multisite polling
with this method.
*/
if (this.getMultisitePolling()) {
/* multisite polling and a build is in progress */
if (project.isBuilding()) {
logger.info("A build already building - cancelling poll");
return PollingResult.NO_CHANGES;
}
} else {
/* not multisite polling and a the project is already in queue */
if (project.isInQueue()) {
logger.fine("A build already in queue - cancelling poll");
return PollingResult.NO_CHANGES;
}
}
logger.fine("Need for polling");
PrintStream out = listener.getLogger();
Stream stream = null;
Component component = null;
try {
stream = Stream.get(this.stream);
component = Component.get(this.component);
} catch (UnableToInitializeEntityException e) {
Util.println(out, e);
throw new AbortException("Unable initialize ClearCase entities");
}
logger.fine("Let's go!");
/* Check input */
if (checkInput(listener)) {
printParameters(out);
List<Baseline> baselines = null;
/* We need to discriminate on promotion level, JENKINS-16620.
*
* This is ONLY for ANY!
* */
Date date = null;
if (plevel == null) {
CCUCMBuildAction lastAction = getLastAction(project);
if (lastAction != null) {
date = lastAction.getBaseline().getDate();
}
}
/* Old skool self polling */
if (polling.isPollingSelf()) {
baselines = getValidBaselinesFromStream(workspace, plevel, stream, component, date);
} else {
/* Find the Baselines and store them */
baselines = getBaselinesFromStreams(workspace, listener, out, stream, component, polling.isPollingChilds(), date);
}
if (baselines.size() > 0) {
p = PollingResult.BUILD_NOW;
}
}
return p;
}
/**
* Get the {@link Baseline}s from a {@link Stream}s related Streams.
*
* @return A list of {@link Baseline}'s
*/
private List<Baseline> getBaselinesFromStreams(FilePath workspace, TaskListener listener, PrintStream consoleOutput, Stream stream, Component component, boolean pollingChildStreams, Date date) {
List<Stream> streams = null;
List<Baseline> baselines = new ArrayList<Baseline>();
try {
streams = RemoteUtil.getRelatedStreams(workspace, listener, stream, pollingChildStreams, this.getSlavePolling(), this.getMultisitePolling());
} catch (Exception e1) {
Throwable root = ExceptionUtils.getRootCause(e1);
logger.log(Level.WARNING, "Could not get related streams from " + stream, root);
consoleOutput.println("[" + Config.nameShort + "] No streams found");
return baselines;
}
consoleOutput.println("[" + Config.nameShort + "] Scanning " + streams.size() + " stream" + (streams.size() == 1 ? "" : "s") + " for baselines.");
int c = 1;
for (Stream s : streams) {
try {
consoleOutput.printf("[" + Config.nameShort + "] [%02d] %s ", c, s.getShortname());
c++;
List<Baseline> found = RemoteUtil.getRemoteBaselinesFromStream(workspace, component, s, plevel, this.getSlavePolling(), this.getMultisitePolling(), date);
for (Baseline b : found) {
baselines.add(b);
}
consoleOutput.println(found.size() + " baseline" + (found.size() == 1 ? "" : "s") + " found");
} catch (Exception e) {
Throwable root = ExceptionUtils.getRootCause(e);
logger.log(Level.WARNING, "Could not get baselines from " + s, root);
consoleOutput.println("No baselines: " + root.getMessage());
}
}
consoleOutput.println("");
return baselines;
}
/**
* Given the {@link Stream}, {@link Component} and
* {@link net.praqma.clearcase.ucm.entities.Project.PromotionLevel} a list
* of valid {@link Baseline}s is returned.
*
* @param workspace
* @param plevel
* @param stream
* @param component
* @return A list of {@link Baseline}'s
* @throws CCUCMException
*/
private List<Baseline> getValidBaselinesFromStream(FilePath workspace, Project.PromotionLevel plevel, Stream stream, Component component, Date date) throws IOException, InterruptedException {
logger.fine("Retrieving valid baselines.");
return RemoteUtil.getRemoteBaselinesFromStream(workspace, component, stream, plevel, this.getSlavePolling(), this.getMultisitePolling(), date);
}
/**
* Returns the last {@link CCUCMBuildAction}, that has a valid
* {@link Baseline}
*
* @param project
* @return An Action
*/
public static CCUCMBuildAction getLastAction(AbstractProject<?, ?> project) {
for (AbstractBuild<?, ?> b = project.getLastBuild(); b != null; b = b.getPreviousBuild()) {
CCUCMBuildAction action = b.getAction(CCUCMBuildAction.class);
if (action != null && action.getBaseline() != null) {
return action;
}
}
return null;
}
private CCUCMBuildAction getBuildAction() throws UnableToInitializeEntityException {
CCUCMBuildAction action = new CCUCMBuildAction(Stream.get(stream), Component.get(component));
action.setDescription(setDescription);
action.setMakeTag(makeTag);
action.setRecommend(recommend);
action.setForceDeliver(forceDeliver);
action.setPromotionLevel(plevel);
action.setUnstable(treatUnstable);
action.setLoadModule(loadModule);
action.setRemoveViewPrivateFiles(removeViewPrivateFiles);
action.setTrimmedChangeSet(trimmedChangeSet);
/* Deliver and template */
action.setCreateBaseline(createBaseline);
/* Trim template, strip out quotes */
if (nameTemplate.matches("^\".+\"$")) {
nameTemplate = nameTemplate.substring(1, nameTemplate.length() - 1);
}
action.setNameTemplate(nameTemplate);
action.setPolling(polling);
return action;
}
@Override
public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
SCMRevisionState scmRS = null;
if (bl != null) {
scmRS = new SCMRevisionStateImpl();
}
return scmRS;
}
private Baseline selectBaseline(List<Baseline> baselines, Project.PromotionLevel plevel, FilePath workspace) throws IOException, InterruptedException {
Baseline selected = null;
if (baselines.size() > 0) {
if (plevel != null) {
selected = baselines.get(0);
} else {
selected = baselines.get(baselines.size() - 1);
}
return (Baseline) RemoteUtil.loadEntity(workspace, selected, true);
} else {
return null;
}
}
private void printParameters(PrintStream ps) {
ps.println("[" + Config.nameShort + "] Getting baselines for :");
ps.println("[" + Config.nameShort + "] * Stream: " + stream);
ps.println("[" + Config.nameShort + "] * Component: " + component);
if (plevel == null) {
ps.println("[" + Config.nameShort + "] * Promotion level: " + "ANY");
} else {
ps.println("[" + Config.nameShort + "] * Promotion level: " + plevel);
}
ps.println("");
}
public void printBaselines(List<Baseline> baselines, PrintStream ps) {
if (baselines != null) {
ps.println("[" + Config.nameShort + "] Retrieved " + baselines.size() + " baseline" + (baselines.size() == 1 ? "" : "s") + ":");
if (!(baselines.size() > 8)) {
for (Baseline b : baselines) {
ps.println("[" + Config.nameShort + "] + " + b.getShortname() + "(" + b.getDate() + ")");
}
} else {
int i = baselines.size();
ps.println("[" + Config.nameShort + "] + " + baselines.get(0).getShortname() + "(" + baselines.get(0).getDate() + ")");
ps.println("[" + Config.nameShort + "] + " + baselines.get(1).getShortname() + "(" + baselines.get(1).getDate() + ")");
ps.println("[" + Config.nameShort + "] + " + baselines.get(2).getShortname() + "(" + baselines.get(2).getDate() + ")");
ps.println("[" + Config.nameShort + "] ...");
ps.println("[" + Config.nameShort + "] + " + baselines.get(i - 3).getShortname() + "(" + baselines.get(i - 3).getDate() + ")");
ps.println("[" + Config.nameShort + "] + " + baselines.get(i - 2).getShortname() + "(" + baselines.get(i - 2).getDate() + ")");
ps.println("[" + Config.nameShort + "] + " + baselines.get(i - 1).getShortname() + "(" + baselines.get(i - 1).getDate() + ")");
}
}
}
/*
* The following getters and booleans (six in all) are used to display saved
* userdata in Hudsons gui
*/
public String getLevelToPoll() {
return levelToPoll;
}
public String getComponent() {
return component;
}
public String getStream() {
return stream;
}
public String getLoadModule() {
return loadModule;
}
/*
* getStreamObject() and getBaseline() are used by CCUCMNotifier to get the
* Baseline and Stream in use, but does not work with concurrent builds!!!
*/
public Stream getStreamObject() {
return integrationstream;
}
@Exported
public String getBaseline() {
return bl;
}
public boolean getSlavePolling() {
CCUCMScm.CCUCMScmDescriptor desc = (CCUCMScm.CCUCMScmDescriptor) this.getDescriptor();
return desc.getSlavePolling();
}
public boolean getMultisitePolling() {
if (this.multisitePolling != null) {
return this.multisitePolling;
} else {
CCUCMScm.CCUCMScmDescriptor desc = (CCUCMScm.CCUCMScmDescriptor) this.getDescriptor();
return desc.getMultisitePolling();
}
}
@Exported
public String getPolling() {
return polling.toString();
}
@Exported
public String getTreatUnstable() {
return treatUnstable.toString();
}
public String getBuildProject() {
return buildProject;
}
public boolean getForceDeliver() {
return forceDeliver;
}
public boolean isTrimmedChangeSet() {
return trimmedChangeSet;
}
public boolean isRemoveViewPrivateFiles() {
return removeViewPrivateFiles;
}
public boolean isCreateBaseline() {
return this.createBaseline;
}
public String getNameTemplate() {
return this.nameTemplate;
}
public boolean isMakeTag() {
return this.makeTag;
}
public boolean isSetDescription() {
return this.setDescription;
}
public boolean isRecommend() {
return this.recommend;
}
public void setMultisitePolling(boolean mp) {
this.multisitePolling = mp;
}
public boolean isDiscard() {
return discard;
}
public void setDiscard(boolean discard) {
this.discard = discard;
}
/**
* This class is used to describe the plugin to Hudson
*
* @author Troels Selch
* @author Margit Bennetzen
*
*/
@Extension
public static class CCUCMScmDescriptor extends SCMDescriptor<CCUCMScm> implements hudson.model.ModelObject {
private boolean slavePolling;
private boolean multisitePolling;
private List<String> loadModules;
public CCUCMScmDescriptor() {
super(CCUCMScm.class, null);