Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new trait to discard all tags with creation date older than the configured days #1017

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* The MIT License
*
* Copyright (c) 2025, Madis Parn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket.trait;

import com.cloudbees.jenkins.plugins.bitbucket.BitbucketGitSCMBuilder;
import com.cloudbees.jenkins.plugins.bitbucket.Messages;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.util.FormValidation;
import java.util.concurrent.TimeUnit;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.mixin.TagSCMHead;
import jenkins.scm.api.trait.SCMBuilder;
import jenkins.scm.api.trait.SCMHeadPrefilter;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

/**
* Discard all tags with creation date older than the configured days.
*
* @author Madis Parn
* @since 936.0.0
*/
public class DiscardOldTagTrait extends SCMSourceTrait {

private int keepForDays;

@DataBoundConstructor
public DiscardOldTagTrait(@CheckForNull int keepForDays) {
this.keepForDays = keepForDays;
}

public int getKeepForDays() {
return keepForDays;

Check warning on line 61 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 61 is not covered by tests
}

@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
if (keepForDays > 0) {

Check warning on line 66 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 66 is only partially covered, one branch is missing
context.withPrefilter(new ExcludeOldSCMTag(keepForDays));
}
}

public static final class ExcludeOldSCMTag extends SCMHeadPrefilter {

private final long expiryMs;

public ExcludeOldSCMTag(int keepForDays) {
this.expiryMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(keepForDays);
}

@Override
public boolean isExcluded(@NonNull SCMSource source, @NonNull SCMHead head) {
if (!(head instanceof TagSCMHead tagHead)) {
return false;
}
long tagTimestamp = tagHead.getTimestamp();
return tagTimestamp > 0 && tagTimestamp < expiryMs;
}

}

@Symbol("bitbucketDiscardOldTag")
@Extension
public static class DescriptorImpl extends SCMSourceTraitDescriptor {

public FormValidation doCheckKeepForDays(@QueryParameter int keepForDays) {
if (keepForDays <= 0) {
return FormValidation.error("Invalid value. Days must be greater than 0");
}
return FormValidation.ok();

Check warning on line 98 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 95-98 are not covered by tests
}

@Override
public String getDisplayName() {
return Messages.DiscardOldTagTrait_displayName();
}

@Override
public boolean isApplicableToBuilder(@NonNull Class<? extends SCMBuilder> builderClass) {
return BitbucketGitSCMBuilder.class.isAssignableFrom(builderClass);

Check warning on line 108 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 108 is not covered by tests
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ BitbucketTagSCMHead.Pronoun=Tag
TagDiscoveryTrait.authorityDisplayName=Trust origin tags
BitbucketBuildStatusNotificationsTrait.displayName=Bitbucket build status notifications
DiscardOldBranchTrait.displayName=Discard branch older than given days
DiscardOldTagTrait.displayName=Discard tag older than given days
ShowBitbucketAvatarTrait.displayName=Show Bitbucket avatar images
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
- The MIT License
-
- Copyright (c) 2025, Madis Parn
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="${%Days to keep}" field="keepForDays">
<f:number default="1" />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
- The MIT License
-
- Copyright (c) 2025, Madis Parn
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-->
<div>
Number of days after the tag creation before it is discarded.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
- The MIT License
-
- Copyright (c) 2025, Madis Parn
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-->
<div>
Discard all tags created before the configured days.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* The MIT License
*
* Copyright (c) 2025, Madis Parn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket.trait;

import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSourceContext;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketTagSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.BranchSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.trait.DiscardOldTagTrait.ExcludeOldSCMTag;
import java.util.Date;
import java.util.stream.Stream;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.trait.SCMHeadPrefilter;
import jenkins.scm.impl.NullSCMSource;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.assertj.core.api.Assertions.assertThat;

class DiscardOldTagTraitTest {

static Stream<Arguments> verify_that_tag_is_not_excluded_args() {
return Stream.of(
Arguments.argumentSet("too_recent",
new BitbucketTagSCMHead("tag/1234", DateUtils.addDays(new Date(), -4).getTime())),
Arguments.argumentSet("no_timestamp", new BitbucketTagSCMHead("tag/zer0", 0L)),
Arguments.argumentSet("not_a_tag", new BranchSCMHead("someBranch"))
);
}

@ParameterizedTest
@MethodSource("verify_that_tag_is_not_excluded_args")
void verify_that_tag_is_not_excluded(SCMHead head) {
DiscardOldTagTrait trait = new DiscardOldTagTrait(5);
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
trait.decorateContext(ctx);
assertThat(ctx.prefilters()).hasAtLeastOneElementOfType(ExcludeOldSCMTag.class);

for (SCMHeadPrefilter filter : ctx.prefilters()) {
assertThat(filter.isExcluded(new NullSCMSource(), head)).isFalse();
}
}

@Test
void verify_that_tag_is_excluded_if_expired() {
DiscardOldTagTrait trait = new DiscardOldTagTrait(5);
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
trait.decorateContext(ctx);
assertThat(ctx.prefilters()).hasAtLeastOneElementOfType(ExcludeOldSCMTag.class);

Date now = new Date();

BitbucketTagSCMHead head = new BitbucketTagSCMHead("tag/1234", DateUtils.addDays(now, -6).getTime());

for (SCMHeadPrefilter filter : ctx.prefilters()) {
assertThat(filter.isExcluded(new NullSCMSource(), head)).isTrue();
}
}

}