-
Notifications
You must be signed in to change notification settings - Fork 43
[JENKINS-42849] add BranchProperty to assign ownership to branch jobs #63
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2017 Jordan Coll | ||
| * | ||
| * 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 org.jenkinsci.plugins.ownership.model.branches; | ||
|
|
||
| import com.synopsys.arc.jenkins.plugins.ownership.Messages; | ||
| import hudson.Extension; | ||
| import hudson.Util; | ||
| import hudson.model.User; | ||
| import hudson.util.FormValidation; | ||
| import jenkins.branch.Branch; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
| import org.kohsuke.stapler.QueryParameter; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import javax.annotation.RegEx; | ||
| import java.util.Collections; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.regex.PatternSyntaxException; | ||
|
|
||
| /** | ||
| * A {@link BranchOwnershipStrategy} that determines ownership by matching branch names against a {@link Pattern regular expression}. | ||
| */ | ||
| public class BranchNameOwnershipStrategy extends BranchOwnershipStrategy { | ||
|
|
||
| private Pattern pattern; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure it will be persisted correctly and then recovered? I have some doubts about it, config roundtrip tests would be useful |
||
| private String ownerExpression; | ||
|
|
||
| /** | ||
| * Constructs an instance. | ||
| * | ||
| * @param pattern A {@link Pattern regular expression} against which branch names should be matched | ||
| * @param ownerExpression A string representing the prospective owner. May contain references to capture groups in the pattern. | ||
| * See {@link Matcher#appendReplacement(StringBuffer, String)} for syntax. | ||
| * @throws PatternSyntaxException If the pattern's syntax is invalid | ||
| */ | ||
| @DataBoundConstructor | ||
| public BranchNameOwnershipStrategy(@RegEx String pattern, @Nonnull String ownerExpression) throws PatternSyntaxException { | ||
| this.pattern = Pattern.compile(pattern); | ||
| this.ownerExpression = ownerExpression; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix empty and trim here as well? Then you can make this field |
||
| } | ||
|
|
||
| public String getPattern() { | ||
| return pattern.pattern(); | ||
| } | ||
|
|
||
| public String getOwnerExpression() { | ||
| return ownerExpression; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public String determineOwner(Branch branch) { | ||
| Matcher matcher = pattern.matcher(branch.getName()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No PatternSyntaxException handling. It would be better to catch it here just in case (e.g. manual config edits on the disk) |
||
|
|
||
| if (matcher.matches()) { | ||
| String prospectiveOwner = matcher.replaceAll(ownerExpression); | ||
|
|
||
| if (User.get(prospectiveOwner, false, Collections.emptyMap()) != null) { | ||
| return prospectiveOwner; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Extension | ||
| public static class DescriptorImpl extends BranchOwnershipStrategy.BranchOwnershipStrategyDescriptor { | ||
|
|
||
| @Override | ||
| @Nonnull | ||
| public String getDisplayName() { | ||
| return Messages.BranchOwnership_Strategy_BranchNameOwnershipStrategy_DisplayName(); | ||
| } | ||
|
|
||
| @Restricted(NoExternalUse.class) | ||
| public FormValidation doCheckPattern(@QueryParameter String value) { | ||
| try { | ||
| Pattern.compile(value); | ||
| } catch (PatternSyntaxException ex) { | ||
| return FormValidation.error(Messages.BranchOwnership_Strategy_BranchNameOwnershipStrategy_InvalidRegex(ex.getMessage())); | ||
| } | ||
|
|
||
| return FormValidation.ok(); | ||
| } | ||
|
|
||
| @Restricted(NoExternalUse.class) | ||
| public FormValidation doCheckOwnerExpression(@QueryParameter String value) { | ||
| return Util.fixEmptyAndTrim(value) != null ? FormValidation.ok() : FormValidation.warning("Ownership will be disabled"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2017 Jordan Coll | ||
| * | ||
| * 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 org.jenkinsci.plugins.ownership.model.branches; | ||
|
|
||
| import hudson.ExtensionPoint; | ||
| import hudson.model.AbstractDescribableImpl; | ||
| import hudson.model.Descriptor; | ||
| import jenkins.branch.Branch; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| public abstract class BranchOwnershipStrategy extends AbstractDescribableImpl<BranchOwnershipStrategy> implements ExtensionPoint { | ||
|
|
||
| /** | ||
| * Determine the owner for the given branch using the implemented strategy. | ||
| * | ||
| * @param branch The branch | ||
| * @return The prospective owner's user ID or full name. {@code null} if the owner cannot be determined. | ||
| */ | ||
| @CheckForNull | ||
| public abstract String determineOwner(Branch branch); | ||
|
|
||
| @Nonnull | ||
| @SuppressWarnings("unchecked") | ||
| public BranchOwnershipStrategyDescriptor getDescriptor() { | ||
| return (BranchOwnershipStrategyDescriptor) super.getDescriptor(); | ||
| } | ||
|
|
||
| static abstract class BranchOwnershipStrategyDescriptor extends Descriptor<BranchOwnershipStrategy> { | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2017 Jordan Coll | ||
| * | ||
| * 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 org.jenkinsci.plugins.ownership.model.branches; | ||
|
|
||
|
|
||
| import com.synopsys.arc.jenkins.plugins.ownership.Messages; | ||
| import hudson.Extension; | ||
| import jenkins.branch.Branch; | ||
| import jenkins.scm.api.metadata.ContributorMetadataAction; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class FromScmBranchOwnershipStrategy extends BranchOwnershipStrategy { | ||
|
|
||
|
|
||
| @Nullable | ||
| @Override | ||
| public String determineOwner(Branch branch) { | ||
| ContributorMetadataAction contributorMetadataAction = branch.getAction(ContributorMetadataAction.class); | ||
| return contributorMetadataAction != null ? contributorMetadataAction.getContributor() : null; | ||
| } | ||
|
|
||
| @Extension | ||
| public static class DescriptorImpl extends BranchOwnershipStrategy.BranchOwnershipStrategyDescriptor { | ||
| @Override | ||
| @Nonnull | ||
| public String getDisplayName() { | ||
| return Messages.BranchOwnership_Strategy_FromScmOwnershipStrategy_DisplayName(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2017 Jordan Coll | ||
| * | ||
| * 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 org.jenkinsci.plugins.ownership.model.branches; | ||
|
|
||
| import com.synopsys.arc.jenkins.plugins.ownership.Messages; | ||
| import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription; | ||
| import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPlugin; | ||
| import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper; | ||
| import hudson.Extension; | ||
| import hudson.model.Job; | ||
| import hudson.model.Run; | ||
| import hudson.model.TopLevelItem; | ||
| import hudson.model.User; | ||
| import hudson.util.FormValidation; | ||
| import jenkins.branch.Branch; | ||
| import jenkins.branch.BranchProperty; | ||
| import jenkins.branch.BranchPropertyDescriptor; | ||
| import jenkins.branch.JobDecorator; | ||
| import jenkins.branch.MultiBranchProject; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
| import org.kohsuke.stapler.QueryParameter; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.logging.Level; | ||
|
|
||
| public class OwnershipBranchProperty extends BranchProperty { | ||
|
|
||
| private BranchOwnershipStrategy strategy; | ||
| private String fallbackOwner; | ||
|
|
||
| @DataBoundConstructor | ||
| public OwnershipBranchProperty(@Nonnull String fallbackOwner, @Nonnull BranchOwnershipStrategy strategy) { | ||
| this.strategy = strategy; | ||
| this.fallbackOwner = fallbackOwner; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixEmptyAndTrim()? Null would be better |
||
| } | ||
|
|
||
| public BranchOwnershipStrategy getStrategy() { | ||
| return strategy; | ||
| } | ||
|
|
||
| public String getFallbackOwner() { | ||
| return fallbackOwner; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public <P extends Job<P, B>, B extends Run<P, B>> JobDecorator<P, B> jobDecorator(final Class<P> clazz) { | ||
| return new JobDecorator<P, B>() { | ||
| @Nonnull | ||
| public P project(@Nonnull P project) { | ||
| if (project.getParent() instanceof MultiBranchProject && TopLevelItem.class.isAssignableFrom(clazz)) { | ||
| MultiBranchProject multiBranchProject = (MultiBranchProject) project.getParent(); | ||
| Branch branch = multiBranchProject.getProjectFactory().getBranch(project); | ||
|
|
||
| String prospectiveOwner = strategy.determineOwner(branch); | ||
| String owner = prospectiveOwner != null ? prospectiveOwner : getFallbackOwner(); | ||
|
|
||
| OwnershipDescription ownershipDescription = new OwnershipDescription(true, owner, null); | ||
| try { | ||
| JobOwnerHelper.setOwnership(project, ownershipDescription); | ||
| } catch (IOException ioe) { | ||
| // TODO: handle somehow | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle what? |
||
| String msg = String.format("Failed setting owner for branch %s in project %s", | ||
| branch.getName(), multiBranchProject.getFullName()); | ||
| OwnershipPlugin.getLogger().log(Level.SEVERE, msg, ioe); | ||
| } | ||
| } | ||
| return project; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Extension | ||
| public static class DescriptorImpl extends BranchPropertyDescriptor { | ||
| @Nonnull | ||
| @Override | ||
| public String getDisplayName() { | ||
| return Messages.BranchOwnership_BranchProperty_DisplayName(); | ||
| } | ||
|
|
||
| @Restricted(NoExternalUse.class) | ||
| public FormValidation doCheckFallbackOwner(@QueryParameter String value) { | ||
| User user = User.get(value, false, Collections.emptyMap()); | ||
| return user != null ? FormValidation.ok() : FormValidation.error(Messages.BranchOwnership_BranchProperty_UnknownUserError(), value); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to remove explicit
jarrequirement. It may becomehpiat some point