4141import com .cloudbees .jenkins .plugins .bitbucket .endpoints .BitbucketCloudEndpoint ;
4242import com .cloudbees .jenkins .plugins .bitbucket .endpoints .BitbucketEndpointConfiguration ;
4343import com .cloudbees .jenkins .plugins .bitbucket .hooks .HasPullRequests ;
44+ import com .cloudbees .jenkins .plugins .bitbucket .hooks .HasRefsChangedRequest ;
4445import com .cloudbees .plugins .credentials .CredentialsNameProvider ;
4546import com .cloudbees .plugins .credentials .CredentialsProvider ;
4647import com .cloudbees .plugins .credentials .common .StandardCredentials ;
7677import java .util .Locale ;
7778import java .util .Map ;
7879import java .util .Set ;
80+ import java .util .TreeSet ;
7981import java .util .concurrent .ConcurrentHashMap ;
8082import java .util .logging .Level ;
8183import java .util .logging .Logger ;
@@ -237,6 +239,12 @@ public class BitbucketSCMSource extends SCMSource {
237239 @ CheckForNull
238240 private transient List <BitbucketHref > cloneLinks = null ;
239241
242+ /**
243+ * The cache of the bitbucket repository.
244+ */
245+ @ CheckForNull
246+ private transient BitbucketRepository bitbucketRepository = null ;
247+
240248 /**
241249 * Constructor.
242250 *
@@ -502,7 +510,7 @@ public boolean isAutoRegisterHook() {
502510
503511 public BitbucketRepositoryType getRepositoryType () throws IOException , InterruptedException {
504512 if (repositoryType == null ) {
505- BitbucketRepository r = buildBitbucketClient (). getRepository ();
513+ BitbucketRepository r = getBitbucketRepository ();
506514 repositoryType = BitbucketRepositoryType .fromString (r .getScm ());
507515 Map <String , List <BitbucketHref >> links = r .getLinks ();
508516 if (links != null && links .containsKey ("clone" )) {
@@ -524,6 +532,13 @@ public BitbucketApi buildBitbucketClient(String repoOwner, String repository) {
524532 return BitbucketApiFactory .newInstance (getServerUrl (), authenticator (), repoOwner , repository );
525533 }
526534
535+ private BitbucketRepository getBitbucketRepository () throws IOException , InterruptedException {
536+ if (bitbucketRepository ==null ) {
537+ bitbucketRepository = buildBitbucketClient ().getRepository ();
538+ }
539+ return bitbucketRepository ;
540+ }
541+
527542 @ Override
528543 public void afterSave () {
529544 try {
@@ -575,6 +590,24 @@ protected Iterable<BitbucketPullRequest> create() {
575590 @ Override
576591 protected Iterable <BitbucketBranch > create () {
577592 try {
593+ if (event != null ) {
594+ Set <BitbucketBranch > branches = new TreeSet <>((a , b ) -> a .getName ().compareTo (b .getName ()));
595+ if (event instanceof HasRefsChangedRequest ) {
596+ HasRefsChangedRequest hasRefsChangedRequest = (HasRefsChangedRequest ) event ;
597+ hasRefsChangedRequest .getBranches (BitbucketSCMSource .this ).forEach ((b ) -> branches .add (b ));
598+ }
599+
600+ if (event instanceof HasPullRequests ) {
601+ HasPullRequests hasPrEvent = (HasPullRequests ) event ;
602+ hasPrEvent .getPullRequests (BitbucketSCMSource .this ).forEach ((pr ) -> {
603+ branches .add (pr .getSource ().getBranch ());
604+ branches .add (pr .getDestination ().getBranch ());
605+ });
606+ }
607+
608+ return branches ;
609+ }
610+
578611 return (Iterable <BitbucketBranch >) buildBitbucketClient ().getBranches ();
579612 } catch (IOException | InterruptedException e ) {
580613 throw new BitbucketSCMSource .WrappedException (e );
@@ -587,6 +620,11 @@ protected Iterable<BitbucketBranch> create() {
587620 @ Override
588621 protected Iterable <BitbucketBranch > create () {
589622 try {
623+ if (event instanceof HasRefsChangedRequest ) {
624+ HasRefsChangedRequest hasRefsChangedRequest = (HasRefsChangedRequest ) event ;
625+ return hasRefsChangedRequest .getTags (BitbucketSCMSource .this );
626+ }
627+
590628 return (Iterable <BitbucketBranch >) buildBitbucketClient ().getTags ();
591629 } catch (IOException | InterruptedException e ) {
592630 throw new BitbucketSCMSource .WrappedException (e );
@@ -746,7 +784,7 @@ private void retrieveBranches(final BitbucketSCMSourceRequest request)
746784 request .listener ().getLogger ().println ("Looking up " + fullName + " for branches" );
747785
748786 final BitbucketApi bitbucket = buildBitbucketClient ();
749- Map <String , List <BitbucketHref >> links = bitbucket . getRepository ().getLinks ();
787+ Map <String , List <BitbucketHref >> links = getBitbucketRepository ().getLinks ();
750788 if (links != null && links .containsKey ("clone" )) {
751789 cloneLinks = links .get ("clone" );
752790 }
@@ -772,7 +810,7 @@ private void retrieveTags(final BitbucketSCMSourceRequest request) throws IOExce
772810 request .listener ().getLogger ().println ("Looking up " + fullName + " for tags" );
773811
774812 final BitbucketApi bitbucket = buildBitbucketClient ();
775- Map <String , List <BitbucketHref >> links = bitbucket . getRepository ().getLinks ();
813+ Map <String , List <BitbucketHref >> links = getBitbucketRepository ().getLinks ();
776814 if (links != null && links .containsKey ("clone" )) {
777815 cloneLinks = links .get ("clone" );
778816 }
@@ -795,9 +833,11 @@ private void retrieveTags(final BitbucketSCMSourceRequest request) throws IOExce
795833 @ Override
796834 protected SCMRevision retrieve (SCMHead head , TaskListener listener ) throws IOException , InterruptedException {
797835 final BitbucketApi bitbucket = buildBitbucketClient ();
798- List <? extends BitbucketBranch > branches = bitbucket .getBranches ();
799836 if (head instanceof PullRequestSCMHead ) {
800837 PullRequestSCMHead h = (PullRequestSCMHead ) head ;
838+ List <BitbucketBranch > branches = new ArrayList <>();
839+ branches .addAll (bitbucket .getBranchesByFilterText (h .getBranchName ()));
840+ branches .addAll (bitbucket .getBranchesByFilterText (h .getTarget ().getName ()));
801841 BitbucketCommit targetRevision = findCommit (h .getTarget ().getName (), branches , listener );
802842 if (targetRevision == null ) {
803843 LOGGER .log (Level .WARNING , "No branch found in {0}/{1} with name [{2}]" ,
@@ -806,9 +846,10 @@ protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOExc
806846 }
807847 BitbucketCommit sourceRevision ;
808848 if (bitbucket instanceof BitbucketCloudApiClient ) {
809- branches = head .getOrigin () == SCMHeadOrigin .DEFAULT
810- ? branches
811- : buildBitbucketClient (h ).getBranches ();
849+ if (head .getOrigin () != SCMHeadOrigin .DEFAULT ) {
850+ branches .clear ();
851+ branches .addAll (buildBitbucketClient (h ).getBranchesByFilterText (h .getBranchName ()));
852+ }
812853 sourceRevision = findCommit (h .getBranchName (), branches , listener );
813854 } else {
814855 try {
@@ -836,14 +877,15 @@ protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOExc
836877 );
837878 } else if (head instanceof BitbucketTagSCMHead ) {
838879 BitbucketTagSCMHead tagHead = (BitbucketTagSCMHead ) head ;
839- List <? extends BitbucketBranch > tags = bitbucket .getTags ( );
880+ List <? extends BitbucketBranch > tags = bitbucket .getTagsByFilterText ( head . getName () );
840881 BitbucketCommit revision = findCommit (head .getName (), tags , listener );
841882 if (revision == null ) {
842883 LOGGER .log (Level .WARNING , "No tag found in {0}/{1} with name [{2}]" , new Object [] { repoOwner , repository , head .getName () });
843884 return null ;
844885 }
845886 return new BitbucketTagSCMRevision (tagHead , revision );
846887 } else {
888+ List <? extends BitbucketBranch > branches = bitbucket .getBranchesByFilterText (head .getName ());
847889 BitbucketCommit revision = findCommit (head .getName (), branches , listener );
848890 if (revision == null ) {
849891 LOGGER .log (Level .WARNING , "No branch found in {0}/{1} with name [{2}]" ,
@@ -925,7 +967,7 @@ public SCM build(SCMHead head, SCMRevision revision) {
925967 if (cloneLinks == null ) {
926968 BitbucketApi bitbucket = buildBitbucketClient ();
927969 try {
928- BitbucketRepository r = bitbucket . getRepository ();
970+ BitbucketRepository r = getBitbucketRepository ();
929971 Map <String , List <BitbucketHref >> links = r .getLinks ();
930972 if (links != null && links .containsKey ("clone" )) {
931973 cloneLinks = links .get ("clone" );
@@ -1009,7 +1051,7 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
10091051 // TODO when we have support for trusted events, use the details from event if event was from trusted source
10101052 List <Action > result = new ArrayList <>();
10111053 final BitbucketApi bitbucket = buildBitbucketClient ();
1012- BitbucketRepository r = bitbucket . getRepository ();
1054+ BitbucketRepository r = getBitbucketRepository ();
10131055 Map <String , List <BitbucketHref >> links = r .getLinks ();
10141056 if (links != null && links .containsKey ("clone" )) {
10151057 cloneLinks = links .get ("clone" );
0 commit comments