Skip to content

Commit fb6f83e

Browse files
authored
[JENKINS-75534] DiscardOldBranch trait uses AuthorDate of HEAD commit to determine branch age rather than CommitDate (#1021)
Change authorTimestamp to committerTimestamp for Bitbucket DataCenter commit details to reflect the same date that Bitbucket Cloud send in the commit details. Improve help of DiscardOldTagTrait
1 parent 4eace31 commit fb6f83e

File tree

6 files changed

+34
-37
lines changed

6 files changed

+34
-37
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/branch/BitbucketServerCommit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class BitbucketServerCommit implements BitbucketCommit {
4848
@JsonCreator
4949
public BitbucketServerCommit(@NonNull @JsonProperty("message") String message, //
5050
@NonNull @JsonProperty("id") String hash, //
51-
@NonNull @JsonProperty("authorTimestamp") long dateMillis, //
51+
@NonNull @JsonProperty("committerTimestamp") long dateMillis, //
5252
@Nullable @JsonProperty("author") BitbucketServerAuthor author) {
5353
// date it is not in the payload
5454
this(message, hash, dateMillis, author != null ? MessageFormat.format(GIT_COMMIT_AUTHOR, author.getName(), author.getEmail()) : null);

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ public int getKeepForDays() {
6767

6868
@Override
6969
protected void decorateContext(SCMSourceContext<?, ?> context) {
70-
context.withFilter(new ExcludeOldSCMHeadBranch());
70+
context.withFilter(new ExcludeOldSCMHeadBranch(keepForDays));
7171
}
7272

73-
public final class ExcludeOldSCMHeadBranch extends SCMHeadFilter {
73+
public static final class ExcludeOldSCMHeadBranch extends SCMHeadFilter {
74+
private int keepForDays;
75+
76+
public ExcludeOldSCMHeadBranch(int keepForDays) {
77+
this.keepForDays = keepForDays;
78+
}
79+
7480
@Override
7581
public boolean isExcluded(SCMSourceRequest request, SCMHead head) throws IOException, InterruptedException {
7682
if (keepForDays > 0) {

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import edu.umd.cs.findbugs.annotations.NonNull;
3030
import hudson.Extension;
3131
import hudson.util.FormValidation;
32-
import java.util.concurrent.TimeUnit;
32+
import java.time.LocalDate;
3333
import jenkins.scm.api.SCMHead;
3434
import jenkins.scm.api.SCMSource;
3535
import jenkins.scm.api.mixin.TagSCMHead;
@@ -70,21 +70,28 @@ protected void decorateContext(SCMSourceContext<?, ?> context) {
7070

7171
public static final class ExcludeOldSCMTag extends SCMHeadPrefilter {
7272

73-
private final long expiryMs;
73+
private final int keepForDays;
7474

7575
public ExcludeOldSCMTag(int keepForDays) {
76-
this.expiryMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(keepForDays);
76+
this.keepForDays = keepForDays;
7777
}
7878

7979
@Override
8080
public boolean isExcluded(@NonNull SCMSource source, @NonNull SCMHead head) {
81-
if (!(head instanceof TagSCMHead tagHead)) {
81+
if (!(head instanceof TagSCMHead tagHead) || tagHead.getTimestamp() == 0) {
82+
// it's not a tag or can not resolve tag commit timestamp
8283
return false;
8384
}
84-
long tagTimestamp = tagHead.getTimestamp();
85-
return tagTimestamp > 0 && tagTimestamp < expiryMs;
85+
86+
LocalDate commitDate = asLocalDate(tagHead.getTimestamp());
87+
LocalDate expiryDate = LocalDate.now().minusDays(keepForDays);
88+
return commitDate.isBefore(expiryDate);
8689
}
8790

91+
@NonNull
92+
private LocalDate asLocalDate(@NonNull long milliseconds) {
93+
return new java.sql.Date(milliseconds).toLocalDate();
94+
}
8895
}
8996

9097
@Symbol("bitbucketDiscardOldTag")

src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait/help-keepForDays.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
- THE SOFTWARE.
2323
-->
2424
<div>
25-
Number of days after the tag creation before it is discarded.
25+
Number of days since the commit date referred by the tag before it is discarded.<br/>
26+
(Note: Since Bitbucket does not provide API to gather annotated tag details there are not differences between tag and annotated one)
2627
</div>

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMRevisionTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void init(JenkinsRule rule) {
5353
j = rule;
5454
}
5555

56-
private static Stream<Arguments> revisionData() {
56+
private static Stream<Arguments> revisionDataProvider() {
5757
return Stream.of(Arguments.of("branch on cloud", new BranchDiscoveryTrait(true, true), BitbucketCloudEndpoint.SERVER_URL), //
5858
Arguments.of("branch on server", new BranchDiscoveryTrait(true, true), "localhost"), //
5959
Arguments.of("PR on cloud", new OriginPullRequestDiscoveryTrait(2), BitbucketCloudEndpoint.SERVER_URL), //
@@ -66,7 +66,7 @@ private static Stream<Arguments> revisionData() {
6666
}
6767

6868
@ParameterizedTest(name = "verify revision informations from {0}")
69-
@MethodSource("revisionData")
69+
@MethodSource("revisionDataProvider")
7070
void verify_revision_informations_are_valued(String testName, SCMSourceTrait trait, String serverURL) throws Exception {
7171
BitbucketMockApiFactory.add(serverURL, getApiMockClient(serverURL));
7272
BitbucketSCMSource source = new BitbucketSCMSource("amuniz", "test-repos");

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTraitTest.java

+8-25
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import jenkins.scm.api.trait.SCMHeadPrefilter;
3535
import jenkins.scm.impl.NullSCMSource;
3636
import org.apache.commons.lang3.time.DateUtils;
37-
import org.junit.jupiter.api.Test;
3837
import org.junit.jupiter.params.ParameterizedTest;
3938
import org.junit.jupiter.params.provider.Arguments;
4039
import org.junit.jupiter.params.provider.MethodSource;
@@ -43,41 +42,25 @@
4342

4443
class DiscardOldTagTraitTest {
4544

46-
static Stream<Arguments> verify_that_tag_is_not_excluded_args() {
45+
static Stream<Arguments> tagSCMHeadProvider() {
4746
return Stream.of(
48-
Arguments.argumentSet("too_recent",
49-
new BitbucketTagSCMHead("tag/1234", DateUtils.addDays(new Date(), -4).getTime())),
50-
Arguments.argumentSet("no_timestamp", new BitbucketTagSCMHead("tag/zer0", 0L)),
51-
Arguments.argumentSet("not_a_tag", new BranchSCMHead("someBranch"))
47+
Arguments.argumentSet("expired", new BitbucketTagSCMHead("tag/1234", DateUtils.addDays(new Date(), -6).getTime()), true),
48+
Arguments.argumentSet("too_recent", new BitbucketTagSCMHead("tag/1234", DateUtils.addDays(new Date(), -4).getTime()), false),
49+
Arguments.argumentSet("no_timestamp", new BitbucketTagSCMHead("tag/zer0", 0L), false),
50+
Arguments.argumentSet("not_a_tag", new BranchSCMHead("someBranch"), false)
5251
);
5352
}
5453

5554
@ParameterizedTest
56-
@MethodSource("verify_that_tag_is_not_excluded_args")
57-
void verify_that_tag_is_not_excluded(SCMHead head) {
55+
@MethodSource("tagSCMHeadProvider")
56+
void verify_that_tag_is_not_excluded(SCMHead head, boolean expectedResult) {
5857
DiscardOldTagTrait trait = new DiscardOldTagTrait(5);
5958
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
6059
trait.decorateContext(ctx);
6160
assertThat(ctx.prefilters()).hasAtLeastOneElementOfType(ExcludeOldSCMTag.class);
6261

6362
for (SCMHeadPrefilter filter : ctx.prefilters()) {
64-
assertThat(filter.isExcluded(new NullSCMSource(), head)).isFalse();
65-
}
66-
}
67-
68-
@Test
69-
void verify_that_tag_is_excluded_if_expired() {
70-
DiscardOldTagTrait trait = new DiscardOldTagTrait(5);
71-
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
72-
trait.decorateContext(ctx);
73-
assertThat(ctx.prefilters()).hasAtLeastOneElementOfType(ExcludeOldSCMTag.class);
74-
75-
Date now = new Date();
76-
77-
BitbucketTagSCMHead head = new BitbucketTagSCMHead("tag/1234", DateUtils.addDays(now, -6).getTime());
78-
79-
for (SCMHeadPrefilter filter : ctx.prefilters()) {
80-
assertThat(filter.isExcluded(new NullSCMSource(), head)).isTrue();
63+
assertThat(filter.isExcluded(new NullSCMSource(), head)).isEqualTo(expectedResult);
8164
}
8265
}
8366

0 commit comments

Comments
 (0)