48
48
import com .cloudbees .jenkins .plugins .bitbucket .server .client .repository .BitbucketServerRepository ;
49
49
import com .cloudbees .plugins .credentials .CredentialsNameProvider ;
50
50
import com .cloudbees .plugins .credentials .common .StandardCredentials ;
51
+ import com .cloudbees .plugins .credentials .common .StandardUsernameCredentials ;
51
52
import com .damnhandy .uri .template .UriTemplate ;
52
53
import com .fasterxml .jackson .databind .util .StdDateFormat ;
53
54
import edu .umd .cs .findbugs .annotations .CheckForNull ;
66
67
import hudson .model .Items ;
67
68
import hudson .model .TaskListener ;
68
69
import hudson .plugins .git .GitSCM ;
70
+ import hudson .plugins .git .extensions .GitSCMExtension ;
69
71
import hudson .scm .SCM ;
70
72
import hudson .security .AccessControlled ;
71
73
import hudson .util .FormFillFailure ;
@@ -377,6 +379,7 @@ public String getEndpointJenkinsRootUrl() {
377
379
return AbstractBitbucketEndpoint .getEndpointJenkinsRootUrl (serverUrl );
378
380
}
379
381
382
+ @ Override
380
383
@ NonNull
381
384
public List <SCMSourceTrait > getTraits () {
382
385
return Collections .unmodifiableList (traits );
@@ -397,11 +400,11 @@ public void setBitbucketServerUrl(String url) {
397
400
if (endpoint != null ) {
398
401
// we have a match
399
402
setServerUrl (endpoint .getServerUrl ());
400
- return ;
403
+ } else {
404
+ LOGGER .log (Level .WARNING , "Call to legacy setBitbucketServerUrl({0}) method is configuring a url missing "
405
+ + "from the global configuration." , url );
406
+ setServerUrl (url );
401
407
}
402
- LOGGER .log (Level .WARNING , "Call to legacy setBitbucketServerUrl({0}) method is configuring a url missing "
403
- + "from the global configuration." , url );
404
- setServerUrl (url );
405
408
}
406
409
407
410
@ Deprecated
@@ -422,8 +425,8 @@ public String getBitbucketServerUrl() {
422
425
@ CheckForNull
423
426
public String getCheckoutCredentialsId () {
424
427
for (SCMSourceTrait t : traits ) {
425
- if (t instanceof SSHCheckoutTrait ) {
426
- return StringUtils .defaultString ((( SSHCheckoutTrait ) t ) .getCredentialsId (), DescriptorImpl .ANONYMOUS );
428
+ if (t instanceof SSHCheckoutTrait sshTrait ) {
429
+ return StringUtils .defaultString (sshTrait .getCredentialsId (), DescriptorImpl .ANONYMOUS );
427
430
}
428
431
}
429
432
return DescriptorImpl .SAME ;
@@ -446,8 +449,8 @@ public void setCheckoutCredentialsId(String checkoutCredentialsId) {
446
449
@ NonNull
447
450
public String getIncludes () {
448
451
for (SCMSourceTrait trait : traits ) {
449
- if (trait instanceof WildcardSCMHeadFilterTrait ) {
450
- return (( WildcardSCMHeadFilterTrait ) trait ) .getIncludes ();
452
+ if (trait instanceof WildcardSCMHeadFilterTrait wildcardTrait ) {
453
+ return wildcardTrait .getIncludes ();
451
454
}
452
455
}
453
456
return "*" ;
@@ -588,8 +591,7 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
588
591
@ Override
589
592
protected Iterable <BitbucketPullRequest > create () {
590
593
try {
591
- if (event instanceof HasPullRequests ) {
592
- HasPullRequests hasPrEvent = (HasPullRequests ) event ;
594
+ if (event instanceof HasPullRequests hasPrEvent ) {
593
595
return getBitbucketPullRequestsFromEvent (hasPrEvent , listener );
594
596
}
595
597
@@ -1000,12 +1002,34 @@ private BitbucketCommit findPRDestinationCommit(BitbucketPullRequest pr, TaskLis
1000
1002
public SCM build (SCMHead head , SCMRevision revision ) {
1001
1003
initCloneLinks ();
1002
1004
1003
- boolean sshAuth = traits .stream ()
1004
- .anyMatch (SSHCheckoutTrait .class ::isInstance );
1005
+ String scmCredentialsId = credentialsId ;
1005
1006
1006
1007
BitbucketAuthenticator authenticator = authenticator ();
1007
- return new BitbucketGitSCMBuilder (this , head , revision , null )
1008
- .withExtension (new GitClientAuthenticatorExtension (authenticator == null || sshAuth ? null : authenticator .getCredentialsForSCM ()))
1008
+ GitSCMExtension scmExtension ;
1009
+ if (authenticator != null ) {
1010
+ // workaround to force git-plugin to use the configured username/password credentialsId as is
1011
+ // remove this workaround as https://github.com/jenkinsci/bitbucket-branch-source-plugin/pull/867 will be merged
1012
+ boolean sshAuth = traits .stream ()
1013
+ .anyMatch (SSHCheckoutTrait .class ::isInstance );
1014
+ if (sshAuth ) {
1015
+ // trait will do the magic
1016
+ scmCredentialsId = null ;
1017
+ scmExtension = new GitClientAuthenticatorExtension (null );
1018
+ } else {
1019
+ StandardUsernameCredentials scmCredentials = authenticator .getCredentialsForSCM ();
1020
+ // extension overrides the configured credentialsId with a custom StandardUsernameCredentials provided by the Authenticator
1021
+ scmExtension = new GitClientAuthenticatorExtension (scmCredentials );
1022
+ if (scmCredentials != null ) {
1023
+ // will be overridden by git extension
1024
+ scmCredentialsId = null ;
1025
+ }
1026
+ }
1027
+ } else {
1028
+ scmExtension = new GitClientAuthenticatorExtension (null );
1029
+ }
1030
+
1031
+ return new BitbucketGitSCMBuilder (this , head , revision , scmCredentialsId )
1032
+ .withExtension (scmExtension )
1009
1033
.withCloneLinks (primaryCloneLinks , mirrorCloneLinks )
1010
1034
.withTraits (traits )
1011
1035
.build ();
@@ -1115,20 +1139,18 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
1115
1139
template = UriTemplate .fromTemplate (getServerUrl () + CLOUD_REPO_TEMPLATE + "/{branchOrPR}/{prIdOrHead}" )
1116
1140
.set ("owner" , repoOwner )
1117
1141
.set ("repo" , repository );
1118
- if (head instanceof PullRequestSCMHead ) {
1119
- PullRequestSCMHead pr = (PullRequestSCMHead ) head ;
1120
- template .set ("branchOrPR" , "pull-requests" ).set ("prIdOrHead" , pr .getId ());
1142
+ if (head instanceof PullRequestSCMHead prHead ) {
1143
+ template .set ("branchOrPR" , "pull-requests" ).set ("prIdOrHead" , prHead .getId ());
1121
1144
} else {
1122
1145
template .set ("branchOrPR" , "branch" ).set ("prIdOrHead" , head .getName ());
1123
1146
}
1124
1147
} else {
1125
- if (head instanceof PullRequestSCMHead ) {
1126
- PullRequestSCMHead pr = (PullRequestSCMHead ) head ;
1148
+ if (head instanceof PullRequestSCMHead prHead ) {
1127
1149
template = UriTemplate
1128
1150
.fromTemplate (getServerUrl () + SERVER_REPO_TEMPLATE + "/pull-requests/{id}/overview" )
1129
1151
.set ("owner" , repoOwner )
1130
1152
.set ("repo" , repository )
1131
- .set ("id" , pr .getId ());
1153
+ .set ("id" , prHead .getId ());
1132
1154
} else {
1133
1155
template = UriTemplate
1134
1156
.fromTemplate (getServerUrl () + SERVER_REPO_TEMPLATE + "/compare/commits{?sourceBranch}" )
@@ -1137,10 +1159,9 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
1137
1159
.set ("sourceBranch" , Constants .R_HEADS + head .getName ());
1138
1160
}
1139
1161
}
1140
- if (head instanceof PullRequestSCMHead ) {
1141
- PullRequestSCMHead pr = (PullRequestSCMHead ) head ;
1142
- title = getPullRequestTitleCache ().get (pr .getId ());
1143
- ContributorMetadataAction contributor = getPullRequestContributorCache ().get (pr .getId ());
1162
+ if (head instanceof PullRequestSCMHead prHead ) {
1163
+ title = getPullRequestTitleCache ().get (prHead .getId ());
1164
+ ContributorMetadataAction contributor = getPullRequestContributorCache ().get (prHead .getId ());
1144
1165
if (contributor != null ) {
1145
1166
result .add (contributor );
1146
1167
}
@@ -1149,8 +1170,8 @@ protected List<Action> retrieveActions(@NonNull SCMHead head,
1149
1170
result .add (new BitbucketLink ("icon-bitbucket-branch" , url ));
1150
1171
result .add (new ObjectMetadataAction (title , null , url ));
1151
1172
SCMSourceOwner owner = getOwner ();
1152
- if (owner instanceof Actionable ) {
1153
- for (BitbucketDefaultBranch p : (( Actionable ) owner ) .getActions (BitbucketDefaultBranch .class )) {
1173
+ if (owner instanceof Actionable actionable ) {
1174
+ for (BitbucketDefaultBranch p : actionable .getActions (BitbucketDefaultBranch .class )) {
1154
1175
if (StringUtils .equals (getRepoOwner (), p .getRepoOwner ())
1155
1176
&& StringUtils .equals (repository , p .getRepository ())
1156
1177
&& StringUtils .equals (p .getDefaultBranch (), head .getName ())) {
@@ -1371,8 +1392,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner cont
1371
1392
public ListBoxModel doFillRepositoryItems (@ AncestorInPath SCMSourceOwner context ,
1372
1393
@ QueryParameter String serverUrl ,
1373
1394
@ QueryParameter String credentialsId ,
1374
- @ QueryParameter String repoOwner )
1375
- throws IOException , InterruptedException {
1395
+ @ QueryParameter String repoOwner ) throws IOException {
1376
1396
BitbucketSupplier <ListBoxModel > listBoxModelSupplier = bitbucket -> {
1377
1397
ListBoxModel result = new ListBoxModel ();
1378
1398
BitbucketTeam team = bitbucket .getTeam ();
@@ -1439,6 +1459,7 @@ public List<NamedArrayList<? extends SCMSourceTraitDescriptor>> getTraitsDescrip
1439
1459
return result ;
1440
1460
}
1441
1461
1462
+ @ Override
1442
1463
public List <SCMSourceTrait > getTraitsDefaults () {
1443
1464
return Arrays .asList (
1444
1465
new BranchDiscoveryTrait (true , false ),
@@ -1465,7 +1486,6 @@ public void record(@NonNull SCMHead scmHead, SCMRevision revision, boolean isMat
1465
1486
request .listener ().getLogger ().println (" Met criteria" );
1466
1487
} else {
1467
1488
request .listener ().getLogger ().println (" Does not meet criteria" );
1468
- return ;
1469
1489
}
1470
1490
1471
1491
}
@@ -1484,8 +1504,8 @@ public BitbucketProbeFactory(BitbucketApi bitbucket, BitbucketSCMSourceRequest r
1484
1504
@ NonNull
1485
1505
@ Override
1486
1506
public Probe create (@ NonNull final SCMHead head , @ CheckForNull final I revisionInfo ) throws IOException , InterruptedException {
1487
- final String hash = (revisionInfo instanceof BitbucketCommit ) //
1488
- ? (( BitbucketCommit ) revisionInfo ) .getHash () //
1507
+ final String hash = (revisionInfo instanceof BitbucketCommit bbRevision ) //
1508
+ ? bbRevision .getHash () //
1489
1509
: (String ) revisionInfo ;
1490
1510
1491
1511
return new SCMSourceCriteria .Probe () {
@@ -1501,8 +1521,8 @@ public long lastModified() {
1501
1521
try {
1502
1522
BitbucketCommit commit = null ;
1503
1523
if (hash != null ) {
1504
- commit = (revisionInfo instanceof BitbucketCommit ) //
1505
- ? ( BitbucketCommit ) revisionInfo //
1524
+ commit = (revisionInfo instanceof BitbucketCommit bbRevision ) //
1525
+ ? bbRevision //
1506
1526
: bitbucket .resolveCommit (hash );
1507
1527
}
1508
1528
@@ -1559,8 +1579,7 @@ public SCMRevision create(@NonNull SCMHead head,
1559
1579
BitbucketCommit targetCommit = asCommit (targetInput );
1560
1580
1561
1581
SCMRevision revision ;
1562
- if (head instanceof PullRequestSCMHead ) {
1563
- PullRequestSCMHead prHead = (PullRequestSCMHead ) head ;
1582
+ if (head instanceof PullRequestSCMHead prHead ) {
1564
1583
SCMHead targetHead = prHead .getTarget ();
1565
1584
1566
1585
return new PullRequestSCMRevision ( //
@@ -1574,10 +1593,10 @@ public SCMRevision create(@NonNull SCMHead head,
1574
1593
}
1575
1594
1576
1595
private BitbucketCommit asCommit (I input ) throws IOException , InterruptedException {
1577
- if (input instanceof String ) {
1578
- return client .resolveCommit (( String ) input );
1579
- } else if (input instanceof BitbucketCommit ) {
1580
- return ( BitbucketCommit ) input ;
1596
+ if (input instanceof String value ) {
1597
+ return client .resolveCommit (value );
1598
+ } else if (input instanceof BitbucketCommit commit ) {
1599
+ return commit ;
1581
1600
}
1582
1601
return null ;
1583
1602
}
@@ -1626,14 +1645,14 @@ public WrappedException(Throwable cause) {
1626
1645
1627
1646
public void unwrap () throws IOException , InterruptedException {
1628
1647
Throwable cause = getCause ();
1629
- if (cause instanceof IOException ) {
1630
- throw ( IOException ) cause ;
1648
+ if (cause instanceof IOException ioEx ) {
1649
+ throw ioEx ;
1631
1650
}
1632
- if (cause instanceof InterruptedException ) {
1633
- throw ( InterruptedException ) cause ;
1651
+ if (cause instanceof InterruptedException interruptedEx ) {
1652
+ throw interruptedEx ;
1634
1653
}
1635
- if (cause instanceof RuntimeException ) {
1636
- throw ( RuntimeException ) cause ;
1654
+ if (cause instanceof RuntimeException rtEx ) {
1655
+ throw rtEx ;
1637
1656
}
1638
1657
throw this ;
1639
1658
}
0 commit comments