Skip to content
Permalink
Browse files
[JENKINS-28881] - Extend the internal implementation to allow display…
…ing the ownership info source
  • Loading branch information
oleg-nenashev committed Jan 5, 2016
1 parent e31043d commit cd1568f63f2c1480a0b57c69768ae19eb824c521
Showing 24 changed files with 530 additions and 26 deletions.
@@ -47,6 +47,8 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;
import org.jenkinsci.plugins.ownership.model.OwnershipInfo;
import org.jenkinsci.plugins.ownership.model.jobs.JobOwnershipDescriptionSource;

/**
* Helper for Jobs Ownership.
@@ -94,21 +96,27 @@ public static boolean isUserExists(@Nonnull String userIdOrFullName) {

@Override
public @Nonnull OwnershipDescription getOwnershipDescription(@Nonnull Job<?, ?> job) {
// TODO: Maybe makes sense to unwrap the method to get a better performance (esp. for Security)
return getOwnershipInfo(job).getDescription();
}

@Override
public OwnershipInfo getOwnershipInfo(Job<?, ?> job) {
JobOwnerJobProperty prop = getOwnerProperty(job);
if (prop != null) {
OwnershipDescription d = prop.getOwnership();
if (d.isOwnershipEnabled()) {
// If Ownership on this level is enabled, we return it
return d;
return new OwnershipInfo(d, new JobOwnershipDescriptionSource(job));
}
}

// We go to upper items in order to get the ownership description
ItemGroup parent = job.getParent();
IOwnershipHelper<ItemGroup> located = OwnershipHelperLocator.locate(parent);
AbstractOwnershipHelper<ItemGroup> located = OwnershipHelperLocator.locate(parent);
while (located != null) {
OwnershipDescription fromParent = located.getOwnershipDescription(parent);
if (fromParent.isOwnershipEnabled()) {
OwnershipInfo fromParent = located.getOwnershipInfo(parent);
if (fromParent.getDescription().isOwnershipEnabled()) {
return fromParent;
}
if (parent instanceof Item) {
@@ -121,9 +129,9 @@ public static boolean isUserExists(@Nonnull String userIdOrFullName) {
}

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

/**
* Sets the ownership information.
* @param job A job to be modified
@@ -185,7 +193,7 @@ public String getItemURL(Job<?, ?> item) {
public static class LocatorImpl extends OwnershipHelperLocator<Job<?,?>> {

@Override
public IOwnershipHelper<Job<?,?>> findHelper(Object item) {
public AbstractOwnershipHelper<Job<?,?>> findHelper(Object item) {
if (item instanceof Job<?,?>) {
return Instance;
}
@@ -32,6 +32,7 @@
import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipInfo;

/**
* Provides ownership helper for {@link Computer}.
@@ -48,12 +49,21 @@ public static ComputerOwnerHelper getInstance() {

@Override
public OwnershipDescription getOwnershipDescription(@Nonnull Computer item) {
// TODO: This method impl is a performance hack. May be replaced by getOwnershipInfo() in 1.0
Node node = item.getNode();
return node != null
? NodeOwnerHelper.Instance.getOwnershipDescription(node)
: OwnershipDescription.DISABLED_DESCR; // No node - no ownership
}


@Override
public OwnershipInfo getOwnershipInfo(Computer item) {
Node node = item.getNode();
return node != null
? NodeOwnerHelper.Instance.getOwnershipInfo(node)
: OwnershipInfo.DISABLED_INFO;
}

@Override
public Collection<User> getPossibleOwners(@Nonnull Computer computer) {
Node node = computer.getNode();
@@ -37,6 +37,9 @@
import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipDescriptionSource;
import org.jenkinsci.plugins.ownership.model.OwnershipInfo;
import org.jenkinsci.plugins.ownership.model.nodes.NodeOwnershipDescriptionSource;

/**
* Provides helper for Node owner.
@@ -63,13 +66,25 @@ public static OwnerNodeProperty getOwnerProperty(@Nonnull Node node) {

@Override
public OwnershipDescription getOwnershipDescription(Node item) {
// TODO: This method impl is a performance hack. May be replaced by getOwnershipInfo() in 1.0
if (item == null) { // Handle renames, etc.
return OwnershipDescription.DISABLED_DESCR;
}

OwnerNodeProperty prop = getOwnerProperty(item);
return prop != null ? prop.getOwnership() : OwnershipDescription.DISABLED_DESCR;
}

@Override
public OwnershipInfo getOwnershipInfo(Node item) {
if (item == null) { // Handle renames, etc.
return OwnershipInfo.DISABLED_INFO;
}

OwnerNodeProperty prop = getOwnerProperty(item);
return prop != null ? new OwnershipInfo(OwnershipDescription.DISABLED_DESCR,
new NodeOwnershipDescriptionSource(item)) : OwnershipInfo.DISABLED_INFO;
}

@Override
public Collection<User> getPossibleOwners(Node item) {
@@ -33,6 +33,8 @@
import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipInfo;
import org.jenkinsci.plugins.ownership.model.nodes.NodeOwnershipDescriptionSource;

/**
* Provides helper for Node owner
@@ -57,10 +59,20 @@ private static OwnerNodeProperty getOwnerProperty(@CheckForNull NodeProperty nod
@Nonnull
@Override
public OwnershipDescription getOwnershipDescription(@CheckForNull NodeProperty item) {
// TODO: This method impl is a performance hack. May be replaced by getOwnershipInfo() in 1.0
OwnerNodeProperty prop = getOwnerProperty(item);
OwnershipDescription descr = (prop != null) ? prop.getOwnership() : null;
return descr != null ? descr : OwnershipDescription.DISABLED_DESCR;
}

@Override
public OwnershipInfo getOwnershipInfo(NodeProperty item) {
OwnerNodeProperty prop = getOwnerProperty(item);
OwnershipDescription descr = (prop != null) ? prop.getOwnership() : null;
return descr != null
? new OwnershipInfo(descr, new NodeOwnershipDescriptionSource(getNode(item)))
: OwnershipInfo.DISABLED_INFO;
}

@Nonnull
@Override
@@ -38,6 +38,8 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.ownership.model.OwnershipDescriptionSource;
import org.jenkinsci.plugins.ownership.model.OwnershipInfo;

/**
* Provides basic operations for ownership helpers.
@@ -105,4 +107,14 @@ public boolean isDisplayOwnershipSummaryBox(@Nonnull TObjectType item) {

return true;
}

/**
* Gets ownership info of the requested item.
* @param item Item to be described
* @return Ownership description. The method returns a
* {@link OwnershipDescription.DISABLED}
* @since TODO
*/
@Nonnull
public abstract OwnershipInfo getOwnershipInfo(@Nonnull TObjectType item);
}
@@ -0,0 +1,42 @@
/*
* 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.folders;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import javax.annotation.CheckForNull;
import org.jenkinsci.plugins.ownership.model.OwnershipDescriptionSource;

/**
* References {@link OwnershipDescription}s provided by various {@link AbstractFolder}s.
* @author Oleg Nenashev
* @since TODO
*/
public class FolderOwnershipDescriptionSource extends OwnershipDescriptionSource<AbstractFolder<?>> {

public FolderOwnershipDescriptionSource(@CheckForNull AbstractFolder<?> item) {
super(item);
}

}
@@ -46,6 +46,7 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;
import org.jenkinsci.plugins.ownership.model.OwnershipInfo;

/**
* Integration with Folders plugin.
@@ -89,25 +90,31 @@ public String getItemURL(AbstractFolder<?> item) {

@Override
public OwnershipDescription getOwnershipDescription(AbstractFolder<?> item) {
// TODO: Maybe makes sense to unwrap the method to get a better performance (esp. for Security)
return getOwnershipInfo(item).getDescription();
}

@Override
public OwnershipInfo getOwnershipInfo(AbstractFolder<?> item) {
if (item == null) { // Handle renames, etc.
return OwnershipDescription.DISABLED_DESCR;
return OwnershipInfo.DISABLED_INFO;
}

// Retrieve Ownership from the Folder property
FolderOwnershipProperty prop = getOwnerProperty(item);
if (prop != null) {
OwnershipDescription d = prop.getOwnership();
if (d.isOwnershipEnabled()) {
return prop.getOwnership();
return new OwnershipInfo(prop.getOwnership(), new FolderOwnershipDescriptionSource(item));
}
}

// We go to upper items in order to get the ownership description
ItemGroup parent = item.getParent();
IOwnershipHelper<ItemGroup> located = OwnershipHelperLocator.locate(parent);
AbstractOwnershipHelper<ItemGroup> located = OwnershipHelperLocator.locate(parent);
while (located != null) {
OwnershipDescription fromParent = located.getOwnershipDescription(parent);
if (fromParent.isOwnershipEnabled()) {
OwnershipInfo fromParent = located.getOwnershipInfo(parent);
if (fromParent.getDescription().isOwnershipEnabled()) {
return fromParent;
}
if (parent instanceof Item) {
@@ -119,9 +126,9 @@ public OwnershipDescription getOwnershipDescription(AbstractFolder<?> item) {
}
}

return OwnershipDescription.DISABLED_DESCR;
return OwnershipInfo.DISABLED_INFO;
}

@Override
public Collection<User> getPossibleOwners(AbstractFolder<?> item) {
if (OwnershipPlugin.getInstance().isRequiresConfigureRights()) {
@@ -153,7 +160,7 @@ public static void setOwnership(@Nonnull AbstractFolder<?> folder,
public static class LocatorImpl extends OwnershipHelperLocator<AbstractFolder<?>> {

@Override
public IOwnershipHelper<AbstractFolder<?>> findHelper(Object item) {
public AbstractOwnershipHelper<AbstractFolder<?>> findHelper(Object item) {
if (item instanceof AbstractFolder<?>) {
return INSTANCE;
}
@@ -0,0 +1,64 @@
/*
* 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.OwnershipDescription;
import hudson.model.Job;
import hudson.model.Node;
import javax.annotation.CheckForNull;

/**
* References the source of {@link OwnershipDescription}.
* This describable class is being used in UI (e.g. {@code manage-owners} page).
* The summary info can be retrieved using {@code summary.jelly}.
* @param <TSourceType> Type of the source items.
* @author Oleg Nenashev
* @since TODO
*/
public abstract class OwnershipDescriptionSource<TSourceType extends Object> {

@CheckForNull
private final TSourceType item;

public OwnershipDescriptionSource(@CheckForNull TSourceType item) {
this.item = item;
}

/**
* Provides a reference to the item, which acts as a source.
* It may be {@link Job}, {@link Node} or whatever other class.
* @return Instance, which is referenced as an Item source.
* {@code null} means that there is no item, which could describe the {@link OwnershipDescription}.
*/
@CheckForNull
public TSourceType getItem() {
return item;
}

public static class DisabledSource<TSourceType extends Object> extends OwnershipDescriptionSource<TSourceType>{
public DisabledSource() {
super(null);
}
}
}

0 comments on commit cd1568f

Please sign in to comment.