-
Notifications
You must be signed in to change notification settings - Fork 358
Add a new trait to discard all tags with creation date older than the configured days #1017
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
@Override | ||
protected void decorateContext(SCMSourceContext<?, ?> context) { | ||
if (keepForDays > 0) { | ||
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(); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return Messages.DiscardOldTagTrait_displayName(); | ||
} | ||
|
||
@Override | ||
public boolean isApplicableToBuilder(@NonNull Class<? extends SCMBuilder> builderClass) { | ||
return BitbucketGitSCMBuilder.class.isAssignableFrom(builderClass); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...n/resources/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait/config.jelly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
26 changes: 26 additions & 0 deletions
26
...es/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait/help-keepForDays.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
26 changes: 26 additions & 0 deletions
26
...main/resources/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTrait/help.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
84 changes: 84 additions & 0 deletions
84
src/test/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldTagTraitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.