4444import com .cloudbees .plugins .credentials .common .StandardCredentials ;
4545import edu .umd .cs .findbugs .annotations .CheckForNull ;
4646import edu .umd .cs .findbugs .annotations .NonNull ;
47- import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
4847import hudson .Extension ;
49- import hudson .RestrictedSince ;
5048import hudson .Util ;
5149import hudson .console .HyperlinkNote ;
5250import hudson .model .Action ;
5856import hudson .util .FormValidation ;
5957import hudson .util .ListBoxModel ;
6058import java .io .IOException ;
61- import java .io .ObjectStreamException ;
6259import java .util .ArrayList ;
6360import java .util .Arrays ;
6461import java .util .Collections ;
6764import java .util .Iterator ;
6865import java .util .List ;
6966import java .util .Set ;
70- import java .util .logging .Level ;
71- import java .util .logging .Logger ;
7267import jenkins .authentication .tokens .api .AuthenticationTokens ;
7368import jenkins .model .Jenkins ;
7469import jenkins .plugins .git .traits .GitBrowserSCMSourceTrait ;
9186import jenkins .scm .impl .UncategorizedSCMSourceCategory ;
9287import jenkins .scm .impl .form .NamedArrayList ;
9388import jenkins .scm .impl .trait .Discovery ;
94- import jenkins .scm .impl .trait .RegexSCMSourceFilterTrait ;
9589import jenkins .scm .impl .trait .Selection ;
96- import jenkins .scm .impl .trait .WildcardSCMHeadFilterTrait ;
9790import org .apache .commons .lang .StringUtils ;
9891import org .jenkins .ui .icon .Icon ;
9992import org .jenkins .ui .icon .IconSet ;
10093import org .jenkinsci .Symbol ;
101- import org .kohsuke .accmod .Restricted ;
102- import org .kohsuke .accmod .restrictions .DoNotUse ;
103- import org .kohsuke .accmod .restrictions .NoExternalUse ;
10494import org .kohsuke .stapler .AncestorInPath ;
10595import org .kohsuke .stapler .DataBoundConstructor ;
10696import org .kohsuke .stapler .DataBoundSetter ;
111101
112102public class BitbucketSCMNavigator extends SCMNavigator {
113103
114- private static final Logger LOGGER = Logger .getLogger (BitbucketSCMSource .class .getName ());
115-
116104 @ NonNull
117105 private String serverUrl ;
118106 @ CheckForNull
@@ -125,31 +113,6 @@ public class BitbucketSCMNavigator extends SCMNavigator {
125113 private String projectKey ;
126114 @ NonNull
127115 private List <SCMTrait <? extends SCMTrait <?>>> traits ;
128- @ Deprecated
129- @ Restricted (NoExternalUse .class )
130- @ RestrictedSince ("2.2.0" )
131- private transient String checkoutCredentialsId ;
132- @ Deprecated
133- @ Restricted (NoExternalUse .class )
134- @ RestrictedSince ("2.2.0" )
135- private transient String pattern ;
136- @ Deprecated
137- @ Restricted (NoExternalUse .class )
138- @ RestrictedSince ("2.2.0" )
139- private transient boolean autoRegisterHooks ;
140- @ Deprecated
141- @ Restricted (NoExternalUse .class )
142- @ RestrictedSince ("2.2.0" )
143- private transient String includes ;
144- @ Deprecated
145- @ Restricted (NoExternalUse .class )
146- @ RestrictedSince ("2.2.0" )
147- private transient String excludes ;
148- @ Deprecated
149- @ Restricted (NoExternalUse .class )
150- @ RestrictedSince ("2.2.0" )
151- private transient String bitbucketServerUrl ;
152-
153116
154117 @ DataBoundConstructor
155118 public BitbucketSCMNavigator (String repoOwner ) {
@@ -177,46 +140,6 @@ public BitbucketSCMNavigator(String repoOwner, String credentialsId, String chec
177140 }
178141 }
179142
180- @ SuppressWarnings ({"ConstantConditions" , "deprecation" })
181- @ SuppressFBWarnings (value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" ,
182- justification = "Only non-null after we set them here!" )
183- private Object readResolve () throws ObjectStreamException {
184- if (serverUrl == null ) {
185- serverUrl = BitbucketEndpointConfiguration .get ().readResolveServerUrl (bitbucketServerUrl );
186- }
187- if (serverUrl == null ) {
188- LOGGER .log (Level .WARNING , "BitbucketSCMNavigator::readResolve : serverUrl is still empty" );
189- }
190- if (traits == null ) {
191- // legacy instance, reconstruct traits to reflect legacy behaviour
192- traits = new ArrayList <>();
193- this .traits .add (new BranchDiscoveryTrait (true , true ));
194- this .traits .add (new OriginPullRequestDiscoveryTrait (EnumSet .of (ChangeRequestCheckoutStrategy .HEAD )));
195- this .traits .add (new ForkPullRequestDiscoveryTrait (
196- EnumSet .of (ChangeRequestCheckoutStrategy .HEAD ),
197- new ForkPullRequestDiscoveryTrait .TrustEveryone ())
198- );
199- this .traits .add (new PublicRepoPullRequestFilterTrait ());
200- if ((includes != null && !"*" .equals (includes )) || (excludes != null && !"" .equals (excludes ))) {
201- traits .add (new WildcardSCMHeadFilterTrait (
202- StringUtils .defaultIfBlank (includes , "*" ),
203- StringUtils .defaultIfBlank (excludes , "" )));
204- }
205- if (checkoutCredentialsId != null
206- && !BitbucketSCMSource .DescriptorImpl .SAME .equals (checkoutCredentialsId )
207- && !checkoutCredentialsId .equals (credentialsId )) {
208- traits .add (new SSHCheckoutTrait (checkoutCredentialsId ));
209- }
210- traits .add (new WebhookRegistrationTrait (
211- autoRegisterHooks ? WebhookRegistration .ITEM : WebhookRegistration .DISABLE )
212- );
213- if (pattern != null && !".*" .equals (pattern )) {
214- traits .add (new RegexSCMSourceFilterTrait (pattern ));
215- }
216- }
217- return this ;
218- }
219-
220143 @ CheckForNull
221144 public String getCredentialsId () {
222145 return credentialsId ;
@@ -284,7 +207,7 @@ public void setTraits(@CheckForNull SCMTrait[] traits) {
284207 */
285208 @ Override
286209 public void setTraits (@ CheckForNull List <SCMTrait <? extends SCMTrait <?>>> traits ) {
287- this .traits = traits != null ? new ArrayList <>(traits ) : new ArrayList <SCMTrait <? extends SCMTrait <?>> >();
210+ this .traits = traits != null ? new ArrayList <>(traits ) : new ArrayList <>();
288211 }
289212
290213 public String getServerUrl () {
@@ -300,193 +223,11 @@ public void setServerUrl(@CheckForNull String serverUrl) {
300223 }
301224 }
302225
303- @ Deprecated
304- @ Restricted (DoNotUse .class )
305- @ RestrictedSince ("2.2.0" )
306- @ DataBoundSetter
307- public void setPattern (String pattern ) {
308- for (int i = 0 ; i < traits .size (); i ++) {
309- SCMTrait <?> trait = traits .get (i );
310- if (trait instanceof RegexSCMSourceFilterTrait ) {
311- if (".*" .equals (pattern )) {
312- traits .remove (i );
313- } else {
314- traits .set (i , new RegexSCMSourceFilterTrait (pattern ));
315- }
316- return ;
317- }
318- }
319- if (!".*" .equals (pattern )) {
320- traits .add (new RegexSCMSourceFilterTrait (pattern ));
321- }
322- }
323-
324- @ Deprecated
325- @ Restricted (NoExternalUse .class )
326- @ RestrictedSince ("2.2.0" )
327- @ DataBoundSetter
328- public void setAutoRegisterHooks (boolean autoRegisterHook ) {
329- traits .removeIf (WebhookRegistrationTrait .class ::isInstance );
330- traits .add (new WebhookRegistrationTrait (
331- autoRegisterHook ? WebhookRegistration .ITEM : WebhookRegistration .DISABLE
332- ));
333- }
334-
335- @ Deprecated
336- @ Restricted (NoExternalUse .class )
337- @ RestrictedSince ("2.2.0" )
338- public boolean isAutoRegisterHooks () {
339- for (SCMTrait <? extends SCMTrait <?>> t : traits ) {
340- if (t instanceof WebhookRegistrationTrait hookTrait ) {
341- return hookTrait .getMode () != WebhookRegistration .DISABLE ;
342- }
343- }
344- return true ;
345- }
346-
347-
348- @ Deprecated
349- @ Restricted (NoExternalUse .class )
350- @ RestrictedSince ("2.2.0" )
351- @ NonNull
352- public String getCheckoutCredentialsId () {
353- for (SCMTrait <?> t : traits ) {
354- if (t instanceof SSHCheckoutTrait sshTrait ) {
355- return StringUtils .defaultString (sshTrait .getCredentialsId (), BitbucketSCMSource
356- .DescriptorImpl .ANONYMOUS );
357- }
358- }
359- return BitbucketSCMSource .DescriptorImpl .SAME ;
360- }
361-
362- @ Deprecated
363- @ Restricted (NoExternalUse .class )
364- @ RestrictedSince ("2.2.0" )
365- @ DataBoundSetter
366- public void setCheckoutCredentialsId (String checkoutCredentialsId ) {
367- traits .removeIf (SSHCheckoutTrait .class ::isInstance );
368- if (checkoutCredentialsId != null && !BitbucketSCMSource .DescriptorImpl .SAME .equals (checkoutCredentialsId )) {
369- traits .add (new SSHCheckoutTrait (checkoutCredentialsId ));
370- }
371- }
372-
373- @ Deprecated
374- @ Restricted (DoNotUse .class )
375- @ RestrictedSince ("2.2.0" )
376- public String getPattern () {
377- for (SCMTrait <?> trait : traits ) {
378- if (trait instanceof RegexSCMSourceFilterTrait regexTrait ) {
379- return regexTrait .getRegex ();
380- }
381- }
382- return ".*" ;
383- }
384-
385- @ Deprecated
386- @ Restricted (DoNotUse .class )
387- @ RestrictedSince ("2.2.0" )
388- @ DataBoundSetter
389- public void setBitbucketServerUrl (String url ) {
390- url = BitbucketEndpointConfiguration .normalizeServerUrl (url );
391- url = StringUtils .defaultIfBlank (url , BitbucketCloudEndpoint .SERVER_URL ); // when BitbucketServerUrl was null means cloud was configured
392- AbstractBitbucketEndpoint endpoint = BitbucketEndpointConfiguration .get ()
393- .findEndpoint (url )
394- .orElse (null );
395- if (endpoint == null ) {
396- LOGGER .log (Level .WARNING , "Call to legacy setBitbucketServerUrl({0}) method is configuring a url missing "
397- + "from the global configuration." , url );
398- }
399- setServerUrl (url );
400- }
401-
402- @ Deprecated
403- @ Restricted (DoNotUse .class )
404- @ RestrictedSince ("2.2.0" )
405- @ CheckForNull
406- public String getBitbucketServerUrl () {
407- if (BitbucketEndpointConfiguration .get ()
408- .findEndpoint (serverUrl )
409- .filter (BitbucketCloudEndpoint .class ::isInstance )
410- .isPresent ()) {
411- return null ;
412- }
413- return serverUrl ;
414- }
415-
416226 @ NonNull
417227 public String getEndpointJenkinsRootUrl () {
418228 return AbstractBitbucketEndpoint .getEndpointJenkinsRootUrl (serverUrl );
419229 }
420230
421- @ Deprecated
422- @ Restricted (NoExternalUse .class )
423- @ RestrictedSince ("2.2.0" )
424- @ NonNull
425- public String getIncludes () {
426- for (SCMTrait <?> trait : traits ) {
427- if (trait instanceof WildcardSCMHeadFilterTrait wildcardTrait ) {
428- return wildcardTrait .getIncludes ();
429- }
430- }
431- return "*" ;
432- }
433-
434- @ Deprecated
435- @ Restricted (NoExternalUse .class )
436- @ RestrictedSince ("2.2.0" )
437- @ DataBoundSetter
438- public void setIncludes (@ NonNull String includes ) {
439- for (int i = 0 ; i < traits .size (); i ++) {
440- SCMTrait <?> trait = traits .get (i );
441- if (trait instanceof WildcardSCMHeadFilterTrait wildcardTrait ) {
442- if ("*" .equals (includes ) && "" .equals (wildcardTrait .getExcludes ())) {
443- traits .remove (i );
444- } else {
445- traits .set (i , new WildcardSCMHeadFilterTrait (includes , wildcardTrait .getExcludes ()));
446- }
447- return ;
448- }
449- }
450- if (!"*" .equals (includes )) {
451- traits .add (new WildcardSCMHeadFilterTrait (includes , "" ));
452- }
453- }
454-
455- @ Deprecated
456- @ Restricted (NoExternalUse .class )
457- @ RestrictedSince ("2.2.0" )
458- @ NonNull
459- public String getExcludes () {
460- for (SCMTrait <?> trait : traits ) {
461- if (trait instanceof WildcardSCMHeadFilterTrait wildcardTrait ) {
462- return wildcardTrait .getExcludes ();
463- }
464- }
465- return "" ;
466- }
467-
468- @ Deprecated
469- @ Restricted (NoExternalUse .class )
470- @ RestrictedSince ("2.2.0" )
471- @ DataBoundSetter
472- public void setExcludes (@ NonNull String excludes ) {
473- for (int i = 0 ; i < traits .size (); i ++) {
474- SCMTrait <?> trait = traits .get (i );
475- if (trait instanceof WildcardSCMHeadFilterTrait wildcardTrait ) {
476- if ("*" .equals (wildcardTrait .getIncludes ()) && "" .equals (excludes )) {
477- traits .remove (i );
478- } else {
479- traits .set (i , new WildcardSCMHeadFilterTrait (wildcardTrait .getIncludes (), excludes ));
480- }
481- return ;
482- }
483- }
484- if (!"" .equals (excludes )) {
485- traits .add (new WildcardSCMHeadFilterTrait ("*" , excludes ));
486- }
487- }
488-
489-
490231 @ NonNull
491232 @ Override
492233 protected String id () {
@@ -522,16 +263,17 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
522263
523264 BitbucketAuthenticator authenticator = AuthenticationTokens .convert (BitbucketAuthenticator .authenticationContext (serverUrl ), credentials );
524265
525- BitbucketApi bitbucket = BitbucketApiFactory .newInstance (serverUrl , authenticator , repoOwner , projectKey , null );
526- BitbucketTeam team = bitbucket .getTeam ();
527- if (team != null ) {
528- // Navigate repositories of the team
529- listener .getLogger ().format ("Looking up repositories of team %s%n" , repoOwner );
530- request .withRepositories (bitbucket .getRepositories ());
531- } else {
532- // Navigate the repositories of the repoOwner as a user
533- listener .getLogger ().format ("Looking up repositories of user %s%n" , repoOwner );
534- request .withRepositories (bitbucket .getRepositories (UserRoleInRepository .ADMIN ));
266+ try (BitbucketApi bitbucket = BitbucketApiFactory .newInstance (serverUrl , authenticator , repoOwner , projectKey , null )) {
267+ BitbucketTeam team = bitbucket .getTeam ();
268+ if (team != null ) {
269+ // Navigate repositories of the team
270+ listener .getLogger ().format ("Looking up repositories of team %s%n" , repoOwner );
271+ request .withRepositories (bitbucket .getRepositories ());
272+ } else {
273+ // Navigate the repositories of the repoOwner as a user
274+ listener .getLogger ().format ("Looking up repositories of user %s%n" , repoOwner );
275+ request .withRepositories (bitbucket .getRepositories (UserRoleInRepository .ADMIN ));
276+ }
535277 }
536278 for (BitbucketRepository repo : request .repositories ()) {
537279 if (request .process (repo .getRepositoryName (), sourceFactory , null , witness )) {
0 commit comments