Skip to content

Commit 843d62a

Browse files
committed
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.
1 parent b39735a commit 843d62a

File tree

8 files changed

+312
-3
lines changed

8 files changed

+312
-3
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketBranch.java

-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public interface BitbucketBranch {
4949
* Returns the head commit message for this branch.
5050
*
5151
* @return the head commit message of this branch
52-
* @author Nikolas Falco
5352
* @since 2.2.14
5453
*/
5554
String getMessage();
@@ -58,7 +57,6 @@ public interface BitbucketBranch {
5857
* Returns the head commit author for this branch.
5958
*
6059
* @return the head commit author of this branch
61-
* @author Nikolas Falco
6260
* @since 2.2.14
6361
*/
6462
String getAuthor();

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketCommit.java

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public interface BitbucketCommit {
3232
* Returns the head commit author for this branch.
3333
*
3434
* @return the head commit author of this branch
35-
* @author Nikolas Falco
3635
* @since 2.2.14
3736
*/
3837
String getAuthor();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2025, Nikolas Falco
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.cloudbees.jenkins.plugins.bitbucket.trait;
25+
26+
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketGitSCMBuilder;
27+
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSourceRequest;
28+
import com.cloudbees.jenkins.plugins.bitbucket.Messages;
29+
import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMHead;
30+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBranch;
31+
import edu.umd.cs.findbugs.annotations.CheckForNull;
32+
import edu.umd.cs.findbugs.annotations.NonNull;
33+
import hudson.Extension;
34+
import hudson.util.FormValidation;
35+
import java.io.IOException;
36+
import java.time.LocalDate;
37+
import jenkins.scm.api.SCMHead;
38+
import jenkins.scm.api.trait.SCMBuilder;
39+
import jenkins.scm.api.trait.SCMHeadFilter;
40+
import jenkins.scm.api.trait.SCMSourceContext;
41+
import jenkins.scm.api.trait.SCMSourceRequest;
42+
import jenkins.scm.api.trait.SCMSourceTrait;
43+
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
44+
import org.jenkinsci.Symbol;
45+
import org.kohsuke.stapler.DataBoundConstructor;
46+
import org.kohsuke.stapler.QueryParameter;
47+
48+
/**
49+
* Discard all branches with head commit older than the configured days.
50+
*
51+
* @author Nikolas Falco
52+
* @since 933.3.0
53+
* @see <a href="https://github.com/nfalco79/bitbucket-trait-plugin/blob/master/src/main/java/com/github/nfalco79/jenkins/plugins/bitbucket/trait/DiscardOldBranchTrait.java">Original source</a>
54+
*/
55+
public class DiscardOldBranchTrait extends SCMSourceTrait {
56+
57+
private int keepForDays = 1;
58+
59+
@DataBoundConstructor
60+
public DiscardOldBranchTrait(@CheckForNull int keepForDays) {
61+
this.keepForDays = keepForDays;
62+
}
63+
64+
public int getKeepForDays() {
65+
return keepForDays;
66+
}
67+
68+
@Override
69+
protected void decorateContext(SCMSourceContext<?, ?> context) {
70+
context.withFilter(new ExcludeOldSCMHeadBranch());
71+
}
72+
73+
public final class ExcludeOldSCMHeadBranch extends SCMHeadFilter {
74+
@Override
75+
public boolean isExcluded(SCMSourceRequest request, SCMHead head) throws IOException, InterruptedException {
76+
if (keepForDays > 0) {
77+
BitbucketSCMSourceRequest bbRequest = (BitbucketSCMSourceRequest) request;
78+
String branchName = head.getName();
79+
if (head instanceof PullRequestSCMHead prHead) {
80+
// getName return the PR-<id>, not the branch name
81+
branchName = prHead.getBranchName();
82+
}
83+
84+
for (BitbucketBranch branch : bbRequest.getBranches()) {
85+
if (branchName.equals(branch.getName())) {
86+
LocalDate expiryDate = asLocalDate(branch.getDateMillis());
87+
return LocalDate.now().isAfter(expiryDate);
88+
}
89+
}
90+
}
91+
return false;
92+
}
93+
94+
@NonNull
95+
private LocalDate asLocalDate(@NonNull long milliseconds) {
96+
return new java.sql.Date(milliseconds).toLocalDate();
97+
}
98+
}
99+
100+
/**
101+
* Our descriptor.
102+
*/
103+
@Symbol("bitbucketDiscardOldBranch")
104+
@Extension
105+
public static class DescriptorImpl extends SCMSourceTraitDescriptor {
106+
107+
public FormValidation doCheckKeepForDays(@QueryParameter final int keepForDays) {
108+
if (keepForDays <= 0) {
109+
return FormValidation.error("Invalid value. Days must be greater than 0");
110+
}
111+
return FormValidation.ok();
112+
}
113+
114+
@Override
115+
public String getDisplayName() {
116+
return Messages.DiscardOldBranchTrait_displayName();
117+
}
118+
119+
/**
120+
* {@inheritDoc}
121+
*/
122+
@Override
123+
public boolean isApplicableToBuilder(@SuppressWarnings("rawtypes") @NonNull Class<? extends SCMBuilder> builderClass) {
124+
return BitbucketGitSCMBuilder.class.isAssignableFrom(builderClass);
125+
}
126+
}
127+
128+
}

src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/Messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ BranchSCMHead.Pronoun=Branch
6464
BitbucketTagSCMHead.Pronoun=Tag
6565
TagDiscoveryTrait.authorityDisplayName=Trust origin tags
6666
BitbucketBuildStatusNotificationsTrait.displayName=Bitbucket build status notifications
67+
DiscardOldBranchTrait.displayName=Discard branch older than given days
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
- The MIT License
3+
-
4+
- Copyright (c) 2025, Nikolas Falco
5+
-
6+
- Permission is hereby granted, free of charge, to any person obtaining a copy
7+
- of this software and associated documentation files (the "Software"), to deal
8+
- in the Software without restriction, including without limitation the rights
9+
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
- copies of the Software, and to permit persons to whom the Software is
11+
- furnished to do so, subject to the following conditions:
12+
-
13+
- The above copyright notice and this permission notice shall be included in
14+
- all copies or substantial portions of the Software.
15+
-
16+
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
- THE SOFTWARE.
23+
-->
24+
<?jelly escape-by-default='true'?>
25+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
26+
<f:entry title="${%Days to keep}" field="keepForDays">
27+
<f:number default="1" />
28+
</f:entry>
29+
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--
2+
- The MIT License
3+
-
4+
- Copyright (c) 2025, Nikolas Falco
5+
-
6+
- Permission is hereby granted, free of charge, to any person obtaining a copy
7+
- of this software and associated documentation files (the "Software"), to deal
8+
- in the Software without restriction, including without limitation the rights
9+
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
- copies of the Software, and to permit persons to whom the Software is
11+
- furnished to do so, subject to the following conditions:
12+
-
13+
- The above copyright notice and this permission notice shall be included in
14+
- all copies or substantial portions of the Software.
15+
-
16+
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
- THE SOFTWARE.
23+
-->
24+
<div>
25+
Number of days since the last commit in the branch before it is discarded.
26+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--
2+
- The MIT License
3+
-
4+
- Copyright (c) 2025, Nikolas Falco
5+
-
6+
- Permission is hereby granted, free of charge, to any person obtaining a copy
7+
- of this software and associated documentation files (the "Software"), to deal
8+
- in the Software without restriction, including without limitation the rights
9+
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
- copies of the Software, and to permit persons to whom the Software is
11+
- furnished to do so, subject to the following conditions:
12+
-
13+
- The above copyright notice and this permission notice shall be included in
14+
- all copies or substantial portions of the Software.
15+
-
16+
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
- THE SOFTWARE.
23+
-->
24+
<div>
25+
Discard all branches with head commit older than the configured days.
26+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2025, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.cloudbees.jenkins.plugins.bitbucket.trait;
25+
26+
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSourceContext;
27+
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSourceRequest;
28+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBranch;
29+
import com.cloudbees.jenkins.plugins.bitbucket.trait.DiscardOldBranchTrait.ExcludeOldSCMHeadBranch;
30+
import java.util.Arrays;
31+
import java.util.Calendar;
32+
import java.util.Date;
33+
import jenkins.scm.api.SCMHead;
34+
import jenkins.scm.api.SCMHeadObserver;
35+
import jenkins.scm.api.trait.SCMHeadFilter;
36+
import org.junit.jupiter.api.Test;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.Mockito.mock;
40+
import static org.mockito.Mockito.when;
41+
42+
class DiscardOldBranchTraitTest {
43+
44+
@Test
45+
void verify_that_branch_is_not_excluded_if_has_recent_commits() throws Exception {
46+
DiscardOldBranchTrait trait = new DiscardOldBranchTrait(10);
47+
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
48+
trait.decorateContext(ctx);
49+
assertThat(ctx.filters()).hasAtLeastOneElementOfType(ExcludeOldSCMHeadBranch.class);
50+
51+
long lastCommitDate = new Date().getTime();
52+
53+
SCMHead head = mock(SCMHead.class);
54+
when(head.getName()).thenReturn("feature/release");
55+
56+
BitbucketBranch branch1 = mock(BitbucketBranch.class);
57+
when(branch1.getName()).thenReturn("feature/xyz");
58+
when(branch1.getDateMillis()).thenReturn(lastCommitDate);
59+
60+
BitbucketBranch branch2 = mock(BitbucketBranch.class);
61+
when(branch2.getName()).thenReturn("feature/release");
62+
when(branch2.getDateMillis()).thenReturn(lastCommitDate);
63+
64+
BitbucketSCMSourceRequest request = mock(BitbucketSCMSourceRequest.class);
65+
when(request.getBranches()).thenReturn(Arrays.asList(branch1, branch2));
66+
67+
for (SCMHeadFilter filter : ctx.filters()) {
68+
assertThat(filter.isExcluded(request, head)).isFalse();
69+
}
70+
}
71+
72+
@Test
73+
void verify_that_branch_is_excluded_if_has_head_commit_older_than_specified() throws Exception {
74+
DiscardOldBranchTrait trait = new DiscardOldBranchTrait(5);
75+
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
76+
trait.decorateContext(ctx);
77+
assertThat(ctx.filters()).hasAtLeastOneElementOfType(ExcludeOldSCMHeadBranch.class);
78+
79+
Calendar c = Calendar.getInstance();
80+
c.add(Calendar.DAY_OF_MONTH, -100);
81+
long lastCommitDate = c.getTimeInMillis();
82+
83+
SCMHead head = mock(SCMHead.class);
84+
when(head.getName()).thenReturn("feature/release");
85+
86+
BitbucketBranch branch1 = mock(BitbucketBranch.class);
87+
when(branch1.getName()).thenReturn("feature/xyz");
88+
when(branch1.getDateMillis()).thenReturn(lastCommitDate);
89+
90+
BitbucketBranch branch2 = mock(BitbucketBranch.class);
91+
when(branch2.getName()).thenReturn("feature/release");
92+
when(branch2.getDateMillis()).thenReturn(lastCommitDate);
93+
94+
BitbucketSCMSourceRequest request = mock(BitbucketSCMSourceRequest.class);
95+
when(request.getBranches()).thenReturn(Arrays.asList(branch1, branch2));
96+
97+
for (SCMHeadFilter filter : ctx.filters()) {
98+
assertThat(filter.isExcluded(request, head)).isTrue();
99+
}
100+
}
101+
102+
}

0 commit comments

Comments
 (0)