Skip to content

Commit

Permalink
[JENKINS-28881] - Add OwnershipHelperLocator extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Jan 4, 2016
1 parent 206a36c commit 4afd8a3
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 2 deletions.
Expand Up @@ -23,6 +23,7 @@
*/
package com.synopsys.arc.jenkins.plugins.ownership.jobs;

import com.synopsys.arc.jenkins.plugins.ownership.IOwnershipHelper;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPlugin;
import com.synopsys.arc.jenkins.plugins.ownership.security.itemspecific.ItemSpecificSecurity;
Expand All @@ -31,8 +32,11 @@
import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.AccessRightsFilter;
import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.IUserFilter;
import com.synopsys.arc.jenkins.plugins.ownership.wrappers.OwnershipBuildWrapper;
import hudson.Extension;
import hudson.matrix.MatrixConfiguration;
import hudson.model.AbstractBuild;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.Project;
Expand All @@ -42,6 +46,7 @@
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;

/**
* Helper for Jobs Ownership.
Expand All @@ -53,9 +58,12 @@ public class JobOwnerHelper extends AbstractOwnershipHelper<Job<?,?>> {
public final static JobOwnerHelper Instance = new JobOwnerHelper();

/**
* Gets JobOwnerProperty from job if possible.
* Gets {@link JobOwnerJobProperty} from the job if possible.
* The function also handles multi-configuration jobs, so it should be used
* wherever it is possible.
* <p/>
* This method should not be used to retrieve Ownership descriptions,
* because it does not take inheritance into account.
* @param job Job
* @return JobOwnerJobProperty or null if it is not configured
*/
Expand Down Expand Up @@ -87,7 +95,33 @@ public static boolean isUserExists(@Nonnull String userIdOrFullName) {
@Override
public @Nonnull OwnershipDescription getOwnershipDescription(@Nonnull Job<?, ?> job) {
JobOwnerJobProperty prop = getOwnerProperty(job);
return (prop != null) ? prop.getOwnership() : OwnershipDescription.DISABLED_DESCR;
if (prop != null) {
OwnershipDescription d = prop.getOwnership();
if (d.isOwnershipEnabled()) {
// If Ownership on this level is enabled, we return it
return d;
}
}

// We go to upper items in order to get the ownership description
ItemGroup parent = job.getParent();
IOwnershipHelper<ItemGroup> located = OwnershipHelperLocator.locate(parent);
while (located != null) {
OwnershipDescription fromParent = located.getOwnershipDescription(parent);
if (fromParent.isOwnershipEnabled()) {
return fromParent;
}
if (parent instanceof Item) {
Item parentItem = (Item)parent;
parent = parentItem.getParent();
located = OwnershipHelperLocator.locate(parent);
} else {
located = null;
}
}

// Fallback: we have not found the Ownership using known approaches
return OwnershipDescription.DISABLED_DESCR;
}

/**
Expand Down Expand Up @@ -146,4 +180,16 @@ public String getItemDisplayName(Job<?, ?> item) {
public String getItemURL(Job<?, ?> item) {
return item.getUrl();
}

@Extension
public static class LocatorImpl extends OwnershipHelperLocator<Job<?,?>> {

@Override
public IOwnershipHelper<Job<?,?>> findHelper(Object item) {
if (item instanceof Job<?,?>) {
return Instance;
}
return null;
}
}
}
@@ -0,0 +1,89 @@
/*
* The MIT License
*
* Copyright (c) 2016 Oleg Nenashev.
*
* 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;

import com.synopsys.arc.jenkins.plugins.ownership.IOwnershipHelper;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;

/**
* Extension point, which allows to identify {@link IOwnershipHelper} for particular classes.
* @param <T> Type of the item
* @author Oleg Nenashev
* @since TODO
*/
public abstract class OwnershipHelperLocator <T extends Object> implements ExtensionPoint {

/**
* Looks up ownership helpers for a class.
* @param item Item, for which the ownership should be retrieved
* @return Helper. Returns null if there is no applicable helper provided by this extension.
*/
@CheckForNull
public abstract IOwnershipHelper<T> findHelper(Object item);

/**
* Returns all the registered {@link OwnershipHelperLocator}s.
* @return All registered {@link OwnershipHelperLocator}s
*/
@Nonnull
public static ExtensionList<OwnershipHelperLocator> all() {
return Jenkins.getActiveInstance().getExtensionList(OwnershipHelperLocator.class);
}

/**
* Locates {@link IOwnershipHelper} for the specified item.
* @param <T> Class of the required helper
* @param item Item, for which we need a helper
* @return Located helper. May be null if there is no relevant helper
*/
@CheckForNull
@SuppressWarnings("unchecked")
public static <T> IOwnershipHelper<T> locate(T item) {
return (IOwnershipHelper<T>)locate(item, item.getClass());
}

/**
* Locates {@link IOwnershipHelper} for the specified item.
* @param <T> Class of the required helper
* @param item Item, for which we need a helper
* @param requiredClass Required class
* @return Located helper. May be null if there is no relevant helper
*/
@CheckForNull
@SuppressWarnings("unchecked")
public static <T> IOwnershipHelper<T> locate(Object item, Class<T> requiredClass) {
for (OwnershipHelperLocator<?> helper : all()) {
IOwnershipHelper<?> located = helper.findHelper(item);
//TODO: Helper verification would be useful
if (located != null) {
return (IOwnershipHelper<T>) located;
}
}
return null;
}
}
@@ -0,0 +1,49 @@
/*
* The MIT License
*
* Copyright (c) 2016 Oleg Nenashev.
*
* 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 com.synopsys.arc.jenkins.plugins.ownership.jobs;

import hudson.model.FreeStyleProject;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;
import static org.junit.Assert.assertEquals;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

/**
* Tests for {@link JobOwnerHelper}.
* @author Oleg Nenashev
*/
public class JobOwnerHelperTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void locatorShouldReturnRightHelperForFolder() throws Exception {
FreeStyleProject folder = j.jenkins.createProject(FreeStyleProject.class, "myFolder");

assertEquals("OwnershipHelperLocator should return the FolderOwnershipHelper instance",
OwnershipHelperLocator.locate(folder), JobOwnerHelper.Instance);
}
}

0 comments on commit 4afd8a3

Please sign in to comment.