diff --git a/pom.xml b/pom.xml index ac452a8..d0b710b 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,12 @@ org.jenkins-ci.plugins plugin - 3.5 + 3.49 com.synopsys.jenkinsci ownership - 0.12.2-SNAPSHOT + 0.13.0-SNAPSHOT Job and Node ownership plugin hpi Provides explicit ownership of jobs and nodes @@ -23,11 +23,11 @@ - 1.651.3 - 7 - UTF-8 - 1.14 + 2.138.4 + 8 Max + + true @@ -47,7 +47,19 @@ https://github.com/jenkinsci/${project.artifactId}-plugin HEAD - + + + + + io.jenkins.tools.bom + bom + 2.138.2 + import + pom + + + + org.jenkins-ci.plugins @@ -58,25 +70,22 @@ org.jenkins-ci.plugins token-macro - 1.6 true com.synopsys.arc.jenkinsci.plugins job-restrictions - 0.1 + 0.8 true org.jenkins-ci.plugins script-security - 1.20 true org.jenkins-ci.plugins cloudbees-folder - 6.1.0 true @@ -88,60 +97,52 @@ org.jenkins-ci.plugins security-inspector - 0.4 + 0.5 true - + org.jenkins-ci.plugins matrix-project - 1.6 org.jenkins-ci.plugins matrix-auth - 1.7 + 2.2 org.jenkins-ci.plugins mailer - 1.13 org.jenkins-ci.plugins apache-httpcomponents-client-4-api - 4.5.3-2.0 org.jenkins-ci.plugins.workflow workflow-cps - ${workflow.version} org.jenkins-ci.plugins.workflow workflow-step-api - ${workflow.version} tests test org.jenkins-ci.plugins.workflow workflow-job - ${workflow.version} test org.jenkins-ci.plugins.workflow workflow-basic-steps - ${workflow.version} test org.jenkins-ci.plugins credentials - 2.1.13 test diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin.java index 03aa55f..42ba26c 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin.java @@ -49,6 +49,7 @@ import javax.servlet.ServletException; import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; @@ -56,7 +57,9 @@ * Contains global actions and configurations. * @since 0.0.1 * @author Oleg Nenashev + * @deprecated Code has been moved to {@link OwnershipGlobalConfiguration} */ +@Deprecated public class OwnershipPlugin extends Plugin { public static final String LOG_PREFIX="[OwnershipPlugin] - "; @@ -68,18 +71,7 @@ public class OwnershipPlugin extends Plugin { public static final Permission MANAGE_ITEMS_OWNERSHIP = new Permission(PERMISSIONS, "Jobs", Messages._OwnershipPlugin_ManagePermissions_JobDescription(), Permission.CONFIGURE, PermissionScope.ITEM); public static final Permission MANAGE_SLAVES_OWNERSHIP = new Permission(PERMISSIONS, "Nodes", Messages._OwnershipPlugin_ManagePermissions_SlaveDescription(), Permission.CONFIGURE, PermissionScope.COMPUTER); - private boolean requiresConfigureRights; - - /** - * @deprecated Replaced by {@link ItemOwnershipPolicy} - */ - @Deprecated - private transient boolean assignOnCreate; - private final List pluginActions = new ArrayList<>(); - public String mailResolverClassName; - private ItemSpecificSecurity defaultJobsSecurity; - private OwnershipPluginConfiguration configuration; - + /** * @deprecated Use {@link #getInstance()} instead */ @@ -88,41 +80,25 @@ public class OwnershipPlugin extends Plugin { public static OwnershipPlugin Instance() { return getInstance(); } - + + /** + * Get the plugin instance. + * @return Plugin instance + * @throws IllegalStateException Jenkins instance or the plugin have not been initialized yet + */ + @Nonnull public static OwnershipPlugin getInstance() { - Jenkins j = Jenkins.getInstance(); - OwnershipPlugin plugin = j != null ? j.getPlugin(OwnershipPlugin.class) : null; + Jenkins j = Jenkins.get(); + OwnershipPlugin plugin = j.getPlugin(OwnershipPlugin.class); if (plugin == null) { // Fail horribly // TODO: throw a graceful error - throw new IllegalStateException("Cannot get the plugin's instance. Jenkins or the plugin have not been initialized yet"); + throw new IllegalStateException("Cannot get the plugin's instance. The plugin have not been initialized yet"); } return plugin; } - - @Override - public void start() throws Exception { - load(); - reinitActionsList(); - Jenkins.getActiveInstance().getActions().addAll(pluginActions); - } - @Override - protected void load() throws IOException { - super.load(); - - // Migration to 1.5.0: Check ItemOwnershipPolicy - if (configuration == null) { - - ItemOwnershipPolicy itemOwnershipPolicy = (assignOnCreate) - ? new AssignCreatorPolicy() : new DropOwnershipPolicy(); - configuration = new OwnershipPluginConfiguration(itemOwnershipPolicy); - - save(); - } - } - public boolean isRequiresConfigureRights() { - return requiresConfigureRights; + return OwnershipGlobalConfiguration.get().isRequiresConfigureRights(); } /** @@ -136,12 +112,14 @@ public boolean isAssignOnCreate() { } @CheckForNull + @Deprecated public ItemSpecificSecurity getDefaultJobsSecurity() { - return defaultJobsSecurity; + return OwnershipGlobalConfiguration.get().getDefaultJobsSecurity(); } - + + @Deprecated public OwnershipPluginConfiguration getConfiguration() { - return configuration; + return OwnershipGlobalConfiguration.get().getConfiguration(); } /** @@ -154,15 +132,8 @@ public ItemSpecificSecurity.ItemSpecificDescriptor getItemSpecificDescriptor() { return ItemSpecificSecurity.DESCRIPTOR; } - /** - * {@link OwnershipPlugin} initialization for test suites. - * @param requiresConfigureRights - * @param mailResolverClassName - * @param defaultJobsSecurity - * @param configuration - * @throws IOException - */ - public void configure(boolean requiresConfigureRights, String mailResolverClassName, +/* + public void configure(boolean requiresConfigureRights, String mailResolverClassName, ItemSpecificSecurity defaultJobsSecurity, OwnershipPluginConfiguration configuration) throws IOException { this.requiresConfigureRights = requiresConfigureRights; @@ -174,7 +145,7 @@ public void configure(boolean requiresConfigureRights, String mailResolverClassN save(); Jenkins.getActiveInstance().getActions().addAll(pluginActions); } - +/* @Override public void configure(StaplerRequest req, JSONObject formData) throws IOException, ServletException, Descriptor.FormException { @@ -194,54 +165,30 @@ public void configure(StaplerRequest req, JSONObject formData) if (formData.containsKey("defaultJobsSecurity")) { this.defaultJobsSecurity = getItemSpecificDescriptor().newInstance(req, formData.getJSONObject("defaultJobsSecurity")); } - - reinitActionsList(); - save(); - Jenkins.getActiveInstance().getActions().addAll(pluginActions); - } - - private void reinitActionsList() { - pluginActions.clear(); } - + */ + //TODO: clarify the default value @Nonnull public static String getDefaultOwner() { User current = User.current(); return current != null ? current.getId() : ""; } - + + @Deprecated public boolean hasMailResolverRestriction() { - return mailResolverClassName != null; + return getMailResolverClassName() != null; } @CheckForNull + @Deprecated public String getMailResolverClassName() { - return mailResolverClassName; + return OwnershipGlobalConfiguration.get().getMailResolverClassName(); } - /** - * Gets the configured {@link OwnershipLayoutFormatterProvider}. - * @since 0.5 - * @return Ownership Layout Formatter to be used - */ + @Deprecated public @Nonnull OwnershipLayoutFormatterProvider getOwnershipLayoutFormatterProvider() { - //TODO: replace by the extension point - return OwnershipLayoutFormatterProvider.DEFAULT_PROVIDER; - } - - public FormValidation doCheckUser(@QueryParameter String userId) { - userId = Util.fixEmptyAndTrim(userId); - if (userId == null) { - return FormValidation.error("Field is empty. Field will be ignored"); - } - - User usr = User.getById(userId, false); - if (usr == null) { - return FormValidation.warning("User " + userId + " is not registered in Jenkins"); - } - - return FormValidation.ok(); + return OwnershipGlobalConfiguration.get().getOwnershipLayoutFormatterProvider(); } /** @@ -252,7 +199,8 @@ public FormValidation doCheckUser(@QueryParameter String userId) { @CheckForNull public String resolveEmail(User user) { try { - if (hasMailResolverRestriction()) { + String mailResolverClassName = getMailResolverClassName(); + if (mailResolverClassName != null) { if (mailResolverClassName.equals(FAST_RESOLVER_ID)) { return MailAddressResolver.resolveFast(user); } else { diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerHelper.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerHelper.java index 2ee65a4..2b3f30e 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerHelper.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerHelper.java @@ -44,6 +44,7 @@ import javax.annotation.Nonnull; import hudson.security.Permission; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator; import org.jenkinsci.plugins.ownership.model.OwnershipInfo; import org.jenkinsci.plugins.ownership.model.jobs.JobOwnershipDescriptionSource; @@ -177,7 +178,7 @@ public static void setProjectSpecificSecurity(@Nonnull Job job, @Override public @Nonnull Collection getPossibleOwners(@Nonnull Job item) { - if (OwnershipPlugin.getInstance().isRequiresConfigureRights()) { + if (OwnershipGlobalConfiguration.get().isRequiresConfigureRights()) { IUserFilter filter = new AccessRightsFilter(item, Job.CONFIGURE); return UserCollectionFilter.filterUsers(User.getAll(), true, filter); } else { diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobAction.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobAction.java index 81c3c33..3f1cd14 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobAction.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobAction.java @@ -41,6 +41,7 @@ import net.sf.json.JSONObject; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.HttpResponses; import org.kohsuke.stapler.StaplerRequest; @@ -99,7 +100,7 @@ public ItemSpecificSecurity getItemSpecificSecurity() { @CheckForNull private static ItemSpecificSecurity getGlobalItemSpecificSecurity() { - return OwnershipPlugin.getInstance().getDefaultJobsSecurity(); + return OwnershipGlobalConfiguration.get().getDefaultJobsSecurity(); } /** @@ -154,7 +155,7 @@ public HttpResponse doProjectSpecificSecuritySubmit(StaplerRequest req, StaplerR public HttpResponse doRestoreDefaultSpecificSecuritySubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { getDescribedItem().checkPermission(OwnershipPlugin.MANAGE_ITEMS_OWNERSHIP); // Get default security - ItemSpecificSecurity defaultJobsSecurity = OwnershipPlugin.getInstance().getDefaultJobsSecurity(); + ItemSpecificSecurity defaultJobsSecurity = OwnershipGlobalConfiguration.get().getDefaultJobsSecurity(); ItemSpecificSecurity val = defaultJobsSecurity != null ? defaultJobsSecurity.clone() : null; JobOwnerHelper.setProjectSpecificSecurity(getDescribedItem(), val); diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobProperty.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobProperty.java index 0693758..37d78ec 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobProperty.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/JobOwnerJobProperty.java @@ -35,6 +35,7 @@ import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.IUserFilter; import net.sf.json.JSONObject; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; @@ -96,7 +97,7 @@ public OwnershipDescription getOwnership() { public ItemSpecificSecurity getItemSpecificSecurity() { return itemSpecificSecurity != null ? itemSpecificSecurity - : OwnershipPlugin.getInstance().getDefaultJobsSecurity(); + : OwnershipGlobalConfiguration.get().getDefaultJobsSecurity(); } /** @@ -136,7 +137,7 @@ public JobProperty reconfigure(StaplerRequest req, JSONObject form) throws De } public OwnershipLayoutFormatter> getLayoutFormatter() { - return OwnershipPlugin.getInstance().getOwnershipLayoutFormatterProvider().getLayoutFormatter(getDescribedItem()); + return OwnershipGlobalConfiguration.get().getOwnershipLayoutFormatterProvider().getLayoutFormatter(getDescribedItem()); } @Extension diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/OwnershipItemListener.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/OwnershipItemListener.java index 6f5a00a..566b3a7 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/OwnershipItemListener.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/jobs/OwnershipItemListener.java @@ -30,6 +30,8 @@ import hudson.model.Item; import hudson.model.Job; import hudson.model.listeners.ItemListener; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; + import java.io.IOException; /** @@ -55,7 +57,7 @@ public void onCreated(Item item) { } private ItemOwnershipPolicy getPolicy() { - return OwnershipPlugin.getInstance().getConfiguration().getItemOwnershipPolicy(); + return OwnershipGlobalConfiguration.get().getConfiguration().getItemOwnershipPolicy(); } private void modifyOwnership(Item item, OwnershipDescription ownership) { diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerHelper.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerHelper.java index dc59c2a..b9d83ef 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerHelper.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerHelper.java @@ -41,6 +41,7 @@ import javax.annotation.Nonnull; import hudson.security.Permission; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator; import org.jenkinsci.plugins.ownership.model.OwnershipInfo; import org.jenkinsci.plugins.ownership.model.nodes.NodeOwnershipDescriptionSource; @@ -103,7 +104,7 @@ public boolean hasLocallyDefinedOwnership(@Nonnull Node node) { @Override public Collection getPossibleOwners(Node item) { - if (OwnershipPlugin.getInstance().isRequiresConfigureRights()) { + if (OwnershipGlobalConfiguration.get().isRequiresConfigureRights()) { IUserFilter filter = new AccessRightsFilter(item, Computer.CONFIGURE); return UserCollectionFilter.filterUsers(User.getAll(), true, filter); } else { diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerPropertyHelper.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerPropertyHelper.java index b8b3c9a..bc73a35 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerPropertyHelper.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/NodeOwnerPropertyHelper.java @@ -37,6 +37,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator; import org.jenkinsci.plugins.ownership.model.OwnershipInfo; import org.jenkinsci.plugins.ownership.model.nodes.NodeOwnershipDescriptionSource; @@ -85,7 +86,7 @@ public OwnershipInfo getOwnershipInfo(NodeProperty item) { @Nonnull @Override public Collection getPossibleOwners(NodeProperty item) { - if (OwnershipPlugin.getInstance().isRequiresConfigureRights()) { + if (OwnershipGlobalConfiguration.get().isRequiresConfigureRights()) { //FIXME: Fix after fix of bug at Jenkins return UserCollectionFilter.filterUsers(User.getAll(), true); } else { diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/OwnerNodeProperty.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/OwnerNodeProperty.java index 53457e1..69ee79d 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/OwnerNodeProperty.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/nodes/OwnerNodeProperty.java @@ -42,6 +42,7 @@ import javax.annotation.CheckForNull; import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.kohsuke.stapler.Ancestor; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; @@ -108,7 +109,7 @@ public NodeProperty getDescribedItem() { } public OwnershipLayoutFormatter getLayoutFormatter() { - return OwnershipPlugin.getInstance().getOwnershipLayoutFormatterProvider().getLayoutFormatter(getNode()); + return OwnershipGlobalConfiguration.get().getOwnershipLayoutFormatterProvider().getLayoutFormatter(getNode()); } @Extension diff --git a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/util/AbstractOwnershipHelper.java b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/util/AbstractOwnershipHelper.java index 6fc9903..0d9e257 100644 --- a/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/util/AbstractOwnershipHelper.java +++ b/src/main/java/com/synopsys/arc/jenkins/plugins/ownership/util/AbstractOwnershipHelper.java @@ -34,6 +34,7 @@ import hudson.security.Permission; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.OwnershipInfo; /** @@ -93,7 +94,7 @@ public Collection getPossibleOwners(TObjectType item) { public boolean isDisplayOwnershipSummaryBox(@Nonnull TObjectType item) { // If there is no data, check global options if (!getOwnershipDescription(item).isOwnershipEnabled()) { - return !OwnershipPlugin.getInstance().getConfiguration().getDisplayOptions().isHideOwnershipIfNoData(); + return !OwnershipGlobalConfiguration.get().getConfiguration().getDisplayOptions().isHideOwnershipIfNoData(); } return true; diff --git a/src/main/java/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration.java b/src/main/java/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration.java new file mode 100644 index 0000000..be2b5f3 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration.java @@ -0,0 +1,138 @@ +package org.jenkinsci.plugins.ownership.config; + +import com.synopsys.arc.jenkins.plugins.ownership.OwnershipAction; +import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPluginConfiguration; +import com.synopsys.arc.jenkins.plugins.ownership.extensions.ItemOwnershipPolicy; +import com.synopsys.arc.jenkins.plugins.ownership.extensions.OwnershipLayoutFormatterProvider; +import com.synopsys.arc.jenkins.plugins.ownership.extensions.item_ownership_policy.AssignCreatorPolicy; +import com.synopsys.arc.jenkins.plugins.ownership.extensions.item_ownership_policy.DropOwnershipPolicy; +import com.synopsys.arc.jenkins.plugins.ownership.security.itemspecific.ItemSpecificSecurity; +import hudson.Extension; +import hudson.Util; +import hudson.XmlFile; +import hudson.model.User; +import hudson.util.FormValidation; +import jenkins.model.GlobalConfiguration; +import jenkins.model.Jenkins; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.QueryParameter; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +@Extension +public class OwnershipGlobalConfiguration extends GlobalConfiguration { + + /** + * @deprecated Replaced by {@link ItemOwnershipPolicy} + */ + @Deprecated + private transient boolean assignOnCreate; + @Deprecated + private transient List pluginActions = new ArrayList<>(); + + private String mailResolverClassName; + private ItemSpecificSecurity defaultJobsSecurity; + private OwnershipPluginConfiguration configuration; + private boolean requiresConfigureRights; + + @Nonnull + public static OwnershipGlobalConfiguration get() { + return (OwnershipGlobalConfiguration) Jenkins.get().getDescriptorOrDie(OwnershipGlobalConfiguration.class); + } + + public OwnershipGlobalConfiguration() { + load(); + } + + @Override + protected XmlFile getConfigFile() { + // Compatibility with the old plugin implementation + return new XmlFile(new File(Jenkins.get().getRootDir(),"ownership.xml")); + } + + private void readResolve() { + + // Migration to 1.5.0: Check ItemOwnershipPolicy + if (configuration == null) { + ItemOwnershipPolicy itemOwnershipPolicy = (assignOnCreate) + ? new AssignCreatorPolicy() : new DropOwnershipPolicy(); + configuration = new OwnershipPluginConfiguration(itemOwnershipPolicy); + } + } + + @DataBoundSetter + public void setConfiguration(OwnershipPluginConfiguration configuration) { + this.configuration = configuration; + } + + @DataBoundSetter + public void setDefaultJobsSecurity(ItemSpecificSecurity defaultJobsSecurity) { + this.defaultJobsSecurity = defaultJobsSecurity; + } + + @DataBoundSetter + public void setRequiresConfigureRights(boolean requiresConfigureRights) { + this.requiresConfigureRights = requiresConfigureRights; + } + + @DataBoundSetter + public void setMailResolverClassName(String mailResolverClassName) { + this.mailResolverClassName = mailResolverClassName; + } + + @CheckForNull + public String getMailResolverClassName() { + return mailResolverClassName; + } + + public boolean isRequiresConfigureRights() { + return requiresConfigureRights; + } + + + /** + * @deprecated This method is deprecated since 0.5 + * @return {@code true} if the Item ownership policy is an instance of + * {@link AssignCreatorPolicy}. + */ + @Deprecated + public boolean isAssignOnCreate() { + return (getConfiguration().getItemOwnershipPolicy() instanceof AssignCreatorPolicy); + } + + @CheckForNull + public ItemSpecificSecurity getDefaultJobsSecurity() { + return defaultJobsSecurity; + } + + public OwnershipPluginConfiguration getConfiguration() { + return configuration; + } + + public FormValidation doCheckUser(@QueryParameter String userId) { + userId = Util.fixEmptyAndTrim(userId); + if (userId == null) { + return FormValidation.error("Field is empty. Field will be ignored"); + } + + User usr = User.getById(userId, false); + if (usr == null) { + return FormValidation.warning("User " + userId + " is not registered in Jenkins"); + } + + return FormValidation.ok(); + } + + /** + * Gets the configured {@link OwnershipLayoutFormatterProvider}. + * @return Ownership Layout Formatter to be used + */ + public @Nonnull OwnershipLayoutFormatterProvider getOwnershipLayoutFormatterProvider() { + //TODO: replace by the extension point + return OwnershipLayoutFormatterProvider.DEFAULT_PROVIDER; + } +} diff --git a/src/main/java/org/jenkinsci/plugins/ownership/integrations/securityinspector/PermissionsForOwnerReportBuilder.java b/src/main/java/org/jenkinsci/plugins/ownership/integrations/securityinspector/PermissionsForOwnerReportBuilder.java index 4f4f1b2..d5dd400 100644 --- a/src/main/java/org/jenkinsci/plugins/ownership/integrations/securityinspector/PermissionsForOwnerReportBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/ownership/integrations/securityinspector/PermissionsForOwnerReportBuilder.java @@ -44,7 +44,6 @@ import org.acegisecurity.context.SecurityContext; import org.acegisecurity.context.SecurityContextHolder; import org.acegisecurity.userdetails.UsernameNotFoundException; -import org.jenkinsci.plugins.securityinspector.Messages; import static org.jenkinsci.plugins.securityinspector.SecurityInspectorAction.getSessionId; import org.jenkinsci.plugins.securityinspector.UserContext; import org.jenkinsci.plugins.securityinspector.UserContextCache; diff --git a/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderItemListener.java b/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderItemListener.java index 2ba1877..854c631 100644 --- a/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderItemListener.java +++ b/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderItemListener.java @@ -35,6 +35,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; /** * Locates changes in {@link AbstractFolder}s and assigns ownership accordingly. @@ -64,7 +65,7 @@ public void onCreated(Item item) { } private ItemOwnershipPolicy getPolicy() { - return OwnershipPlugin.getInstance().getConfiguration().getItemOwnershipPolicy(); + return OwnershipGlobalConfiguration.get().getConfiguration().getItemOwnershipPolicy(); } private boolean isFoldersPluginEnabled() { diff --git a/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderOwnershipHelper.java b/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderOwnershipHelper.java index 8908894..5b1a81d 100644 --- a/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderOwnershipHelper.java +++ b/src/main/java/org/jenkinsci/plugins/ownership/model/folders/FolderOwnershipHelper.java @@ -41,6 +41,7 @@ import javax.annotation.Nonnull; import hudson.security.Permission; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator; import org.jenkinsci.plugins.ownership.model.OwnershipInfo; import org.kohsuke.accmod.Restricted; @@ -142,7 +143,7 @@ public OwnershipInfo getOwnershipInfo(AbstractFolder item) { @Override public Collection getPossibleOwners(AbstractFolder item) { - if (OwnershipPlugin.getInstance().isRequiresConfigureRights()) { + if (OwnershipGlobalConfiguration.get().isRequiresConfigureRights()) { IUserFilter filter = new AccessRightsFilter(item, AbstractFolder.CONFIGURE); return UserCollectionFilter.filterUsers(User.getAll(), true, filter); } else { diff --git a/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipAction.java b/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipAction.java index 409a93f..61ae130 100644 --- a/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipAction.java +++ b/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipAction.java @@ -37,6 +37,8 @@ import hudson.model.Run; import hudson.security.Permission; import javax.annotation.Nonnull; + +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.util.environment.EnvSetupOptions; /** @@ -74,7 +76,7 @@ public IOwnershipHelper helper() { } public OwnershipLayoutFormatter getLayoutFormatter() { - return OwnershipPlugin.getInstance().getOwnershipLayoutFormatterProvider().getLayoutFormatter(getDescribedItem()); + return OwnershipGlobalConfiguration.get().getOwnershipLayoutFormatterProvider().getLayoutFormatter(getDescribedItem()); } @Override @@ -83,7 +85,7 @@ public void buildEnvVars(AbstractBuild build, EnvVars env) { boolean injectJobOwnership = false; // Handle global options - final EnvSetupOptions globalEnvSetupOptions = OwnershipPlugin.getInstance(). + final EnvSetupOptions globalEnvSetupOptions = OwnershipGlobalConfiguration.get(). getConfiguration().getGlobalEnvSetupOptions(); if (globalEnvSetupOptions != null) { injectNodeOwnership |= globalEnvSetupOptions.isInjectNodeOwnership(); diff --git a/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipHelper.java b/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipHelper.java index 3b390fb..082a80f 100644 --- a/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipHelper.java +++ b/src/main/java/org/jenkinsci/plugins/ownership/model/runs/RunOwnershipHelper.java @@ -40,6 +40,7 @@ import javax.annotation.Nonnull; import hudson.security.Permission; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator; import org.jenkinsci.plugins.ownership.model.OwnershipInfo; import org.kohsuke.accmod.Restricted; @@ -153,7 +154,7 @@ private static void getVariables(OwnershipDescription descr, Map @Override public boolean isDisplayOwnershipSummaryBox(Run item) { return super.isDisplayOwnershipSummaryBox(item) && - !OwnershipPlugin.getInstance().getConfiguration().getDisplayOptions().isHideRunOwnership(); + !OwnershipGlobalConfiguration.get().getConfiguration().getDisplayOptions().isHideRunOwnership(); } @Extension diff --git a/src/main/resources/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin/config.jelly b/src/main/resources/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration/config.jelly similarity index 100% rename from src/main/resources/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin/config.jelly rename to src/main/resources/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration/config.jelly diff --git a/src/main/resources/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin/config.properties b/src/main/resources/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration/config.properties similarity index 100% rename from src/main/resources/com/synopsys/arc/jenkins/plugins/ownership/OwnershipPlugin/config.properties rename to src/main/resources/org/jenkinsci/plugins/ownership/config/OwnershipGlobalConfiguration/config.properties diff --git a/src/main/resources/org/jenkinsci/plugins/ownership/integrations/securityinspector/Messages.properties b/src/main/resources/org/jenkinsci/plugins/ownership/integrations/securityinspector/Messages.properties new file mode 100644 index 0000000..3359e01 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ownership/integrations/securityinspector/Messages.properties @@ -0,0 +1 @@ +JobReport.RowColumnHeader=Items diff --git a/src/test/java/com/synopsys/arc/jenkins/plugins/ownership/wrappers/OwnershipBuildWrapperTest.java b/src/test/java/com/synopsys/arc/jenkins/plugins/ownership/wrappers/OwnershipBuildWrapperTest.java index d39b850..0bd4fe4 100644 --- a/src/test/java/com/synopsys/arc/jenkins/plugins/ownership/wrappers/OwnershipBuildWrapperTest.java +++ b/src/test/java/com/synopsys/arc/jenkins/plugins/ownership/wrappers/OwnershipBuildWrapperTest.java @@ -26,7 +26,6 @@ import com.cloudbees.hudson.plugins.folder.Folder; import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription; -import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPlugin; import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPluginConfiguration; import com.synopsys.arc.jenkins.plugins.ownership.extensions.item_ownership_policy.AssignCreatorPolicy; import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper; @@ -47,6 +46,8 @@ import java.io.IOException; import java.util.Collections; import java.util.concurrent.Future; + +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.model.folders.FolderOwnershipHelper; import org.jenkinsci.plugins.ownership.test.util.OwnershipPluginConfigurer; import org.jenkinsci.plugins.ownership.util.environment.EnvSetupOptions; @@ -83,8 +84,8 @@ public void initJenkinsInstance() throws Exception { projectOwner.setFullName("Test Project Owner"); // Configure ownership plugin - r.jenkins.getPlugin(OwnershipPlugin.class).configure( - true, null, null, new OwnershipPluginConfiguration(new AssignCreatorPolicy())); + OwnershipGlobalConfiguration.get(). + setConfiguration(new OwnershipPluginConfiguration(new AssignCreatorPolicy())); // Create node with ownership node = r.createOnlineSlave(); @@ -118,7 +119,7 @@ public void initJenkinsInstance() throws Exception { final OwnershipPluginConfiguration pluginConf = new OwnershipPluginConfiguration( new AssignCreatorPolicy(),MailOptions.DEFAULT, new EnvSetupOptions(true, true)); - r.jenkins.getPlugin(OwnershipPlugin.class).configure(true, null, null, pluginConf); + OwnershipGlobalConfiguration.get().setConfiguration(pluginConf); testVarsPresense(true); } @@ -128,7 +129,7 @@ public void initJenkinsInstance() throws Exception { final OwnershipPluginConfiguration pluginConf = new OwnershipPluginConfiguration( new AssignCreatorPolicy(),MailOptions.DEFAULT, new EnvSetupOptions(true, true)); - r.jenkins.getPlugin(OwnershipPlugin.class).configure(true, null, null, pluginConf); + OwnershipGlobalConfiguration.get().setConfiguration(pluginConf); FreeStyleBuild build = testVarsPresense(false); r.assertLogContains("NODE_COOWNERS="+NODE_OWNER_ID, build); diff --git a/src/test/java/org/jenkinsci/plugins/ownership/config/ConfigMigrationTest.java b/src/test/java/org/jenkinsci/plugins/ownership/config/ConfigMigrationTest.java new file mode 100644 index 0000000..dbd6b26 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/ownership/config/ConfigMigrationTest.java @@ -0,0 +1,18 @@ +package org.jenkinsci.plugins.ownership.config; + +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.recipes.LocalData; + +public class ConfigMigrationTest { + + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + @LocalData + public void shouldMigrateDataFromPluginImpl() { + OwnershipGlobalConfiguration.get(); + } +} diff --git a/src/test/java/org/jenkinsci/plugins/ownership/test/util/OwnershipPluginConfigurer.java b/src/test/java/org/jenkinsci/plugins/ownership/test/util/OwnershipPluginConfigurer.java index edafc49..8a971b4 100644 --- a/src/test/java/org/jenkinsci/plugins/ownership/test/util/OwnershipPluginConfigurer.java +++ b/src/test/java/org/jenkinsci/plugins/ownership/test/util/OwnershipPluginConfigurer.java @@ -33,9 +33,11 @@ import jenkins.model.Jenkins; import org.jenkinsci.plugins.ownership.config.DisplayOptions; import org.jenkinsci.plugins.ownership.config.InheritanceOptions; +import org.jenkinsci.plugins.ownership.config.OwnershipGlobalConfiguration; import org.jenkinsci.plugins.ownership.util.environment.EnvSetupOptions; import org.jenkinsci.plugins.ownership.util.mail.MailOptions; import org.jvnet.hudson.test.JenkinsRule; + /** * Manages configuration of {@link OwnershipPlugin}. * @author Oleg Nenashev @@ -111,7 +113,11 @@ public OwnershipPluginConfigurer withInheritanceOptions(InheritanceOptions inher public void configure() throws IOException { OwnershipPluginConfiguration conf = new OwnershipPluginConfiguration (itemOwnershipPolicy, mailOptions, globalEnvSetupOptions, displayOptions, inheritanceOptions); - jenkins.getPlugin(OwnershipPlugin.class).configure - (requiresConfigurePermissions, mailResolverClassName, defaultJobsSecurity, conf); + + OwnershipGlobalConfiguration cfg = OwnershipGlobalConfiguration.get(); + cfg.setConfiguration(conf); + cfg.setDefaultJobsSecurity(defaultJobsSecurity); + cfg.setMailResolverClassName(mailResolverClassName); + cfg.setRequiresConfigureRights(requiresConfigurePermissions); } } diff --git a/src/test/resources/org/jenkinsci/plugins/ownership/config/ConfigMigrationTest/shouldMigrateDataFromPluginImpl/ownership.xml b/src/test/resources/org/jenkinsci/plugins/ownership/config/ConfigMigrationTest/shouldMigrateDataFromPluginImpl/ownership.xml new file mode 100644 index 0000000..b560e53 --- /dev/null +++ b/src/test/resources/org/jenkinsci/plugins/ownership/config/ConfigMigrationTest/shouldMigrateDataFromPluginImpl/ownership.xml @@ -0,0 +1,52 @@ + + + true + + Fast resolver for UI (recommended) + + + + com.cloudbees.plugins.credentials.CredentialsProvider.Create:anonymous + com.cloudbees.plugins.credentials.CredentialsProvider.Delete:anonymous + com.cloudbees.plugins.credentials.CredentialsProvider.Delete:authenticated + hudson.model.Item.Build:anonymous + + + + + + [Jenkins] - TODO: Describe the issue with ${ITEM_TYPE_NAME} ${ITEM_DISPLAY_NAME} + Dear owners of ${ITEM_TYPE_NAME} ${ITEM_DISPLAY_NAME}, + + TODO: Add text + URL: ${ITEM_URL} + + Best regards, + ${USER_FULL_NAME} + false + [Jenkins] - TODO: Describe the issue with ${ITEM_TYPE_NAME} ${ITEM_DISPLAY_NAME} + Dear Jenkins admins, + + TODO: Describe the issue + URL: ${ITEM_URL} + + Best regards, + ${USER_FULL_NAME} + false + ; + myemail@example.com + false + + + false + false + + + false + + + true + false + + +