39
39
import hudson .util .FormValidation ;
40
40
import jenkins .branch .Branch ;
41
41
import jenkins .model .Jenkins ;
42
+ import jenkins .scm .api .SCMFileSystem ;
43
+ import jenkins .scm .api .SCMHead ;
44
+ import jenkins .scm .api .SCMRevision ;
42
45
import jenkins .scm .api .SCMSourceOwner ;
43
46
import org .jenkinsci .plugins .workflow .cps .CpsScmFlowDefinition ;
44
47
import org .jenkinsci .plugins .workflow .flow .FlowDefinition ;
@@ -335,6 +338,59 @@ private String extractDefaultedVersionGitSCM(@NonNull SCM scm, @NonNull Run<?, ?
335
338
return runVersion ;
336
339
}
337
340
341
+ private String extractDefaultedVersionSCMFS (@ NonNull SCM scm , @ NonNull Run <?, ?> run , @ NonNull TaskListener listener , PrintStream logger ) {
342
+ String runVersion = null ;
343
+ Item runParent = run .getParent ();
344
+ if (runParent == null )
345
+ return null ;
346
+
347
+ SCMFileSystem fs ;
348
+ try {
349
+ fs = SCMFileSystem .of (runParent , scm );
350
+ if (fs == null && logger != null ) {
351
+ logger .println ("defaultedVersion(): " +
352
+ "got no SCMFileSystem: " +
353
+ "method of() returned null" );
354
+ }
355
+ } catch (Exception x ) {
356
+ fs = null ;
357
+ if (logger != null ) {
358
+ logger .println ("defaultedVersion(): " +
359
+ "failed to get SCMFileSystem: " +
360
+ x .getMessage ());
361
+ }
362
+ }
363
+ if (fs == null )
364
+ return null ;
365
+
366
+ SCMRevision rev = fs .getRevision ();
367
+ if (rev == null ) {
368
+ if (logger != null ) {
369
+ logger .println ("defaultedVersion(): " +
370
+ "got no SCMRevision from SCMFileSystem" );
371
+ }
372
+ return null ;
373
+ }
374
+
375
+ SCMHead head = rev .getHead ();
376
+ if (head == null ) {
377
+ if (logger != null ) {
378
+ logger .println ("defaultedVersion(): " +
379
+ "got no SCMHead of SCMRevision from SCMFileSystem" );
380
+ }
381
+ return null ;
382
+ }
383
+
384
+ if (logger != null ) {
385
+ logger .println ("defaultedVersion(): " +
386
+ "got SCMHead of SCMRevision from SCMFileSystem: " +
387
+ "name='" + head .getName () + "' " +
388
+ "toString='" + head .toString () + "'" );
389
+ }
390
+
391
+ return runVersion ;
392
+ }
393
+
338
394
private String extractDefaultedVersionSCM (@ NonNull SCM scm , @ NonNull Run <?, ?> run , @ NonNull TaskListener listener , PrintStream logger ) {
339
395
String runVersion = null ;
340
396
@@ -348,7 +404,10 @@ private String extractDefaultedVersionSCM(@NonNull SCM scm, @NonNull Run<?, ?> r
348
404
// until a non-null result.
349
405
// Ideally SCM API itself would have all classes return this
350
406
// value (or null if branch concept is not supported there):
351
- runVersion = extractDefaultedVersionGitSCM (scm , run , listener , logger );
407
+ runVersion = extractDefaultedVersionSCMFS (scm , run , listener , logger );
408
+ if (runVersion == null ) {
409
+ runVersion = extractDefaultedVersionGitSCM (scm , run , listener , logger );
410
+ }
352
411
353
412
if (runVersion == null ) {
354
413
// got SVN, Gerritt or some other SCM -
0 commit comments