Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.1</version>
<version>6.0.1</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down Expand Up @@ -117,6 +117,12 @@
<version>4.3.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>branch-api</artifactId>
<version>2.0.8</version>
<type>jar</type>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to remove explicit jar requirement. It may become hpi at some point

</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
Expand Down
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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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 @CheckForNull

}

public String getPattern() {
return pattern.pattern();
}

public String getOwnerExpression() {
return ownerExpression;
}

@Override
@Nullable
public String determineOwner(Branch branch) {
Matcher matcher = pattern.matcher(branch.getName());
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ OwnershipAction.ConfigureSpecificAccess.DisplayName=Configure specific access ri

Utils.UI.UserSelector=User ID

BranchOwnership.BranchProperty.DisplayName=Set branch job ownership
BranchOwnership.BranchProperty.UnknownUserError=User is not registered in Jenkins
BranchOwnership.Strategy.BranchNameOwnershipStrategy.DisplayName=By branch name
BranchOwnership.Strategy.BranchNameOwnershipStrategy.InvalidRegex=Invalid regex: {0}
BranchOwnership.Strategy.FromScmOwnershipStrategy.DisplayName=From SCM

# Extensions
ItemOwnershipPolicy.AssignCreatorPolicy.dipslayName=Assign job creators as owners
ItemOwnershipPolicy.DropOwnershipPolicy.dipslayName=Do not assign ownership
Expand Down
Loading