86
86
import java .util .logging .Level ;
87
87
import java .util .logging .Logger ;
88
88
import jenkins .plugins .git .AbstractGitSCMSource .SCMRevisionImpl ;
89
+ import jenkins .plugins .git .GitTagSCMRevision ;
89
90
import jenkins .plugins .git .traits .GitBrowserSCMSourceTrait ;
90
91
import jenkins .scm .api .SCMHead ;
91
92
import jenkins .scm .api .SCMHeadCategory ;
106
107
import jenkins .scm .api .trait .SCMSourceTrait ;
107
108
import jenkins .scm .api .trait .SCMSourceTraitDescriptor ;
108
109
import jenkins .scm .impl .ChangeRequestSCMHeadCategory ;
110
+ import jenkins .scm .impl .TagSCMHeadCategory ;
109
111
import jenkins .scm .impl .UncategorizedSCMHeadCategory ;
110
112
import jenkins .scm .impl .form .NamedArrayList ;
111
113
import jenkins .scm .impl .trait .Discovery ;
@@ -506,11 +508,15 @@ public BitbucketRepositoryType getRepositoryType() throws IOException, Interrupt
506
508
}
507
509
508
510
public BitbucketApi buildBitbucketClient () {
509
- return BitbucketApiFactory . newInstance ( getServerUrl (), credentials (), repoOwner , repository );
511
+ return buildBitbucketClient ( repoOwner , repository );
510
512
}
511
513
512
514
public BitbucketApi buildBitbucketClient (PullRequestSCMHead head ) {
513
- return BitbucketApiFactory .newInstance (getServerUrl (), credentials (), head .getRepoOwner (), head .getRepository ());
515
+ return buildBitbucketClient (head .getRepoOwner (), head .getRepository ());
516
+ }
517
+
518
+ public BitbucketApi buildBitbucketClient (String repoOwner , String repository ) {
519
+ return BitbucketApiFactory .newInstance (getServerUrl (), credentials (), repoOwner , repository );
514
520
}
515
521
516
522
@ Override
@@ -566,7 +572,16 @@ protected Iterable<BitbucketBranch> create() {
566
572
});
567
573
}
568
574
if (request .isFetchTags ()) {
569
- // TODO request.setTags(...);
575
+ request .setTags (new LazyIterable <BitbucketBranch >() {
576
+ @ Override
577
+ protected Iterable <BitbucketBranch > create () {
578
+ try {
579
+ return (Iterable <BitbucketBranch >) buildBitbucketClient ().getTags ();
580
+ } catch (IOException | InterruptedException e ) {
581
+ throw new BitbucketSCMSource .WrappedException (e );
582
+ }
583
+ }
584
+ });
570
585
}
571
586
572
587
// now server the request
@@ -579,7 +594,8 @@ protected Iterable<BitbucketBranch> create() {
579
594
retrievePullRequests (request );
580
595
}
581
596
if (request .isFetchTags () && !request .isComplete ()) {
582
- // TODO
597
+ //Search tags
598
+ retrieveTags (request );
583
599
}
584
600
} catch (WrappedException e ) {
585
601
e .unwrap ();
@@ -778,6 +794,38 @@ public String create() {
778
794
request .listener ().getLogger ().format ("%n %d branches were processed%n" , count );
779
795
}
780
796
797
+
798
+ private void retrieveTags (final BitbucketSCMSourceRequest request )
799
+ throws IOException , InterruptedException {
800
+ String fullName = repoOwner + "/" + repository ;
801
+ request .listener ().getLogger ().println ("Looking up " + fullName + " for tags" );
802
+
803
+ final BitbucketApi bitbucket = buildBitbucketClient ();
804
+ Map <String , List <BitbucketHref >> links = bitbucket .getRepository ().getLinks ();
805
+ if (links != null && links .containsKey ("clone" )) {
806
+ cloneLinks = links .get ("clone" );
807
+ }
808
+ int count = 0 ;
809
+ for (final BitbucketBranch tag : request .getTags ()) {
810
+ request .listener ().getLogger ().println ("Checking tag " + tag .getName () + " from " + fullName );
811
+ count ++;
812
+ if (request .process (new BitbucketTagSCMHead (tag .getName (), tag .getDateMillis (), repositoryType ),
813
+ new SCMSourceRequest .IntermediateLambda <String >() {
814
+ @ Nullable
815
+ @ Override
816
+ public String create () {
817
+ return tag .getRawNode ();
818
+ }
819
+ }, new BitbucketProbeFactory (bitbucket , request ), new BitbucketRevisionFactory (),
820
+ new CriteriaWitness (request )
821
+ )) {
822
+ request .listener ().getLogger ().format ("%n %d tags were processed (query completed)%n" , count );
823
+ return ;
824
+ }
825
+ }
826
+ request .listener ().getLogger ().format ("%n %d tags were processed%n" , count );
827
+ }
828
+
781
829
@ Override
782
830
protected SCMRevision retrieve (SCMHead head , TaskListener listener ) throws IOException , InterruptedException {
783
831
final BitbucketApi bitbucket = buildBitbucketClient ();
@@ -823,6 +871,19 @@ protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOExc
823
871
new SCMRevisionImpl (h , sourceRevision )
824
872
);
825
873
}
874
+ } else if (head instanceof BitbucketTagSCMHead ) {
875
+ BitbucketTagSCMHead tagHead = (BitbucketTagSCMHead ) head ;
876
+ List <? extends BitbucketBranch > tags = bitbucket .getTags ();
877
+ String revision = findRawNode (head .getName (), tags , listener );
878
+ if (revision == null ) {
879
+ LOGGER .log (Level .WARNING , "No tag found in {0}/{1} with name [{2}]" , new Object [] { repoOwner , repository , head .getName () });
880
+ return null ;
881
+ }
882
+ if (getRepositoryType () == BitbucketRepositoryType .MERCURIAL ) {
883
+ return new MercurialRevision (head , revision );
884
+ } else {
885
+ return new GitTagSCMRevision (tagHead , revision );
886
+ }
826
887
} else {
827
888
String revision = findRawNode (head .getName (), branches , listener );
828
889
if (revision == null ) {
@@ -889,8 +950,10 @@ public SCM build(SCMHead head, SCMRevision revision) {
889
950
type = ((PullRequestSCMHead ) head ).getRepositoryType ();
890
951
} else if (head instanceof BranchSCMHead ) {
891
952
type = ((BranchSCMHead ) head ).getRepositoryType ();
953
+ } else if (head instanceof BitbucketTagSCMHead ) {
954
+ type = ((BitbucketTagSCMHead ) head ).getRepositoryType ();
892
955
} else {
893
- throw new IllegalArgumentException ("Either PullRequestSCMHead or BranchSCMHead required as parameter" );
956
+ throw new IllegalArgumentException ("Either PullRequestSCMHead, BitbucketTagSCMHead or BranchSCMHead required as parameter" );
894
957
}
895
958
if (type == null ) {
896
959
if (revision instanceof MercurialRevision ) {
@@ -1260,8 +1323,9 @@ public ListBoxModel doFillCheckoutCredentialsIdItems(@AncestorInPath SCMSourceOw
1260
1323
protected SCMHeadCategory [] createCategories () {
1261
1324
return new SCMHeadCategory []{
1262
1325
new UncategorizedSCMHeadCategory (Messages ._BitbucketSCMSource_UncategorizedSCMHeadCategory_DisplayName ()),
1263
- new ChangeRequestSCMHeadCategory (Messages ._BitbucketSCMSource_ChangeRequestSCMHeadCategory_DisplayName ())
1264
- // TODO add support for tags and maybe feature branch identification
1326
+ new ChangeRequestSCMHeadCategory (Messages ._BitbucketSCMSource_ChangeRequestSCMHeadCategory_DisplayName ()),
1327
+ new TagSCMHeadCategory (Messages ._BitbucketSCMSource_TagSCMHeadCategory_DisplayName ())
1328
+ // TODO add support for feature branch identification
1265
1329
};
1266
1330
}
1267
1331
0 commit comments