-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new trait to discard all branches with head commits older than …
…the configured days. This trait is useful when you do not want to pollute the Jenkins instance with older work such as support, release or feature branches for which there will be no further work, for which without this trait the work will be kept there for weeks or years taking up resources. When a new commit is pushed to the discarded branch, a new Jenkins work will be recreated.
- Loading branch information
Showing
6 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/main/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait.java
This file contains 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,127 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2025, Nikolas Falco | ||
* | ||
* 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.BitbucketSCMSourceRequest; | ||
import com.cloudbees.jenkins.plugins.bitbucket.Messages; | ||
import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMHead; | ||
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBranch; | ||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.util.FormValidation; | ||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
import jenkins.scm.api.SCMHead; | ||
import jenkins.scm.api.trait.SCMBuilder; | ||
import jenkins.scm.api.trait.SCMHeadFilter; | ||
import jenkins.scm.api.trait.SCMSourceContext; | ||
import jenkins.scm.api.trait.SCMSourceRequest; | ||
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 branches with head commit older than the configured days. | ||
* | ||
* @author Nikolas Falco | ||
* @see https://github.com/nfalco79/bitbucket-trait-plugin/blob/master/src/main/java/com/github/nfalco79/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait.java | ||
*/ | ||
public class DiscardOldBranchTrait extends SCMSourceTrait { | ||
|
||
private int keepForDays = 1; | ||
|
||
@DataBoundConstructor | ||
public DiscardOldBranchTrait(@CheckForNull int keepForDays) { | ||
this.keepForDays = keepForDays; | ||
} | ||
|
||
public int getKeepForDays() { | ||
return keepForDays; | ||
} | ||
|
||
@Override | ||
protected void decorateContext(SCMSourceContext<?, ?> context) { | ||
context.withFilter(new ExcludeOldSCMHeadBranch()); | ||
} | ||
|
||
public final class ExcludeOldSCMHeadBranch extends SCMHeadFilter { | ||
@Override | ||
public boolean isExcluded(SCMSourceRequest request, SCMHead head) throws IOException, InterruptedException { | ||
if (keepForDays > 0) { | ||
BitbucketSCMSourceRequest bbRequest = (BitbucketSCMSourceRequest) request; | ||
String branchName = head.getName(); | ||
if (head instanceof PullRequestSCMHead prHead) { | ||
// getName return the PR-<id>, not the branch name | ||
branchName = prHead.getBranchName(); | ||
} | ||
|
||
for (BitbucketBranch branch : bbRequest.getBranches()) { | ||
if (branchName.equals(branch.getName())) { | ||
LocalDate expiryDate = asLocalDate(branch.getDateMillis()); | ||
return LocalDate.now().isAfter(expiryDate); | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@NonNull | ||
private LocalDate asLocalDate(@NonNull long milliseconds) { | ||
return new java.sql.Date(milliseconds).toLocalDate(); | ||
} | ||
} | ||
|
||
/** | ||
* Our descriptor. | ||
*/ | ||
@Symbol("bitbucketDiscardOldBranch") | ||
@Extension | ||
public static class DescriptorImpl extends SCMSourceTraitDescriptor { | ||
|
||
public FormValidation doCheckKeepForDays(@QueryParameter final 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.DiscardOldBranchTrait_displayName(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean isApplicableToBuilder(@SuppressWarnings("rawtypes") @NonNull Class<? extends SCMBuilder> builderClass) { | ||
return BitbucketGitSCMBuilder.class.isAssignableFrom(builderClass); | ||
} | ||
} | ||
|
||
} |
This file contains 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
...esources/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait/config.jelly
This file contains 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, Nikolas Falco | ||
- | ||
- 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
...com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait/help-keepForDays.html
This file contains 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, Nikolas Falco | ||
- | ||
- 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 since the last commit in the branch before it is discarded. | ||
</div> |
26 changes: 26 additions & 0 deletions
26
...n/resources/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait/help.html
This file contains 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, Nikolas Falco | ||
- | ||
- 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 branches with head commit older than the configured days. | ||
</div> |
102 changes: 102 additions & 0 deletions
102
src/test/java/com/cloudbees/jenkins/plugins/bitbucket/trait/DiscardOldBranchTraitTest.java
This file contains 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,102 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2025, CloudBees, Inc. | ||
* | ||
* 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.BitbucketSCMSourceRequest; | ||
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBranch; | ||
import com.cloudbees.jenkins.plugins.bitbucket.trait.DiscardOldBranchTrait.ExcludeOldSCMHeadBranch; | ||
import java.util.Arrays; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import jenkins.scm.api.SCMHead; | ||
import jenkins.scm.api.SCMHeadObserver; | ||
import jenkins.scm.api.trait.SCMHeadFilter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class DiscardOldBranchTraitTest { | ||
|
||
@Test | ||
void verify_that_branch_is_not_excluded_if_has_recent_commits() throws Exception { | ||
DiscardOldBranchTrait trait = new DiscardOldBranchTrait(10); | ||
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()); | ||
trait.decorateContext(ctx); | ||
assertThat(ctx.filters()).hasAtLeastOneElementOfType(ExcludeOldSCMHeadBranch.class); | ||
|
||
long lastCommitDate = new Date().getTime(); | ||
|
||
SCMHead head = mock(SCMHead.class); | ||
when(head.getName()).thenReturn("feature/release"); | ||
|
||
BitbucketBranch branch1 = mock(BitbucketBranch.class); | ||
when(branch1.getName()).thenReturn("feature/xyz"); | ||
when(branch1.getDateMillis()).thenReturn(lastCommitDate); | ||
|
||
BitbucketBranch branch2 = mock(BitbucketBranch.class); | ||
when(branch2.getName()).thenReturn("feature/release"); | ||
when(branch2.getDateMillis()).thenReturn(lastCommitDate); | ||
|
||
BitbucketSCMSourceRequest request = mock(BitbucketSCMSourceRequest.class); | ||
when(request.getBranches()).thenReturn(Arrays.asList(branch1, branch2)); | ||
|
||
for (SCMHeadFilter filter : ctx.filters()) { | ||
assertThat(filter.isExcluded(request, head)).isFalse(); | ||
} | ||
} | ||
|
||
@Test | ||
void verify_that_branch_is_excluded_if_has_head_commit_older_than_specified() throws Exception { | ||
DiscardOldBranchTrait trait = new DiscardOldBranchTrait(5); | ||
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()); | ||
trait.decorateContext(ctx); | ||
assertThat(ctx.filters()).hasAtLeastOneElementOfType(ExcludeOldSCMHeadBranch.class); | ||
|
||
Calendar c = Calendar.getInstance(); | ||
c.add(Calendar.DAY_OF_MONTH, -100); | ||
long lastCommitDate = c.getTimeInMillis(); | ||
|
||
SCMHead head = mock(SCMHead.class); | ||
when(head.getName()).thenReturn("feature/release"); | ||
|
||
BitbucketBranch branch1 = mock(BitbucketBranch.class); | ||
when(branch1.getName()).thenReturn("feature/xyz"); | ||
when(branch1.getDateMillis()).thenReturn(lastCommitDate); | ||
|
||
BitbucketBranch branch2 = mock(BitbucketBranch.class); | ||
when(branch2.getName()).thenReturn("feature/release"); | ||
when(branch2.getDateMillis()).thenReturn(lastCommitDate); | ||
|
||
BitbucketSCMSourceRequest request = mock(BitbucketSCMSourceRequest.class); | ||
when(request.getBranches()).thenReturn(Arrays.asList(branch1, branch2)); | ||
|
||
for (SCMHeadFilter filter : ctx.filters()) { | ||
assertThat(filter.isExcluded(request, head)).isTrue(); | ||
} | ||
} | ||
|
||
} |