|
| 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 | +} |
0 commit comments