Skip to content

Commit

Permalink
[JENKINS-28881] - Support inheritance of Ownership info from folders
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Jan 4, 2016
1 parent 4afd8a3 commit 8207da5
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
Expand Up @@ -219,6 +219,43 @@ public boolean hasPrimaryOwner() {
public boolean isPrimaryOwner(User user) {
return user != null && user == getPrimaryOwner();
}

@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + (this.ownershipEnabled ? 1 : 0);
hash = 41 * hash + (this.primaryOwnerId != null ? this.primaryOwnerId.hashCode() : 0);
hash = 41 * hash + (this.coownersIds != null ? this.coownersIds.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final OwnershipDescription other = (OwnershipDescription) obj;
if (this.ownershipEnabled != other.ownershipEnabled) {
return false;
} else if (!this.ownershipEnabled) {
// Treat disabled ownership configurations as equal ones
return true;
}

if ((this.primaryOwnerId == null) ? (other.primaryOwnerId != null) : !this.primaryOwnerId.equals(other.primaryOwnerId)) {
return false;
}
if (this.coownersIds != other.coownersIds && (this.coownersIds == null || !this.coownersIds.equals(other.coownersIds))) {
return false;
}
return true;
}

/**
* Check if ownership is enabled.
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.AbstractFolderProperty;
import com.cloudbees.hudson.plugins.folder.Folder;
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.jobs.JobOwnerHelper;
Expand All @@ -34,13 +35,17 @@
import com.synopsys.arc.jenkins.plugins.ownership.util.UserCollectionFilter;
import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.AccessRightsFilter;
import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.IUserFilter;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.User;
import java.io.IOException;
import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;

/**
* Integration with Folders plugin.
Expand Down Expand Up @@ -88,8 +93,33 @@ public OwnershipDescription getOwnershipDescription(AbstractFolder<?> item) {
return OwnershipDescription.DISABLED_DESCR;
}

// Retrieve Ownership from the Folder property
FolderOwnershipProperty prop = getOwnerProperty(item);
return prop != null ? prop.getOwnership() : OwnershipDescription.DISABLED_DESCR;
if (prop != null) {
OwnershipDescription d = prop.getOwnership();
if (d.isOwnershipEnabled()) {
return prop.getOwnership();
}
}

// We go to upper items in order to get the ownership description
ItemGroup parent = item.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;
}
}

return OwnershipDescription.DISABLED_DESCR;
}

@Override
Expand Down Expand Up @@ -118,4 +148,16 @@ public static void setOwnership(@Nonnull AbstractFolder<?> folder,
prop.setOwnershipDescription(descr);
}
}

@Extension(optional = true)
public static class LocatorImpl extends OwnershipHelperLocator<AbstractFolder<?>> {

@Override
public IOwnershipHelper<AbstractFolder<?>> findHelper(Object item) {
if (item instanceof AbstractFolder<?>) {
return INSTANCE;
}
return null;
}
}
}
@@ -0,0 +1,52 @@
/*
* 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.Folder;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;

/**
* Tests of {@link FolderOwnershipHelper}.
* @author Oleg Nenashev
*/
public class FolderOwnershipHelperTest {

@Rule
public JenkinsRule j = new JenkinsRule();

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

assertEquals("OwnershipHelperLocator should return the FolderOwnershipHelper instance",
OwnershipHelperLocator.locate(folder), FolderOwnershipHelper.getInstance());
}
}
Expand Up @@ -27,6 +27,8 @@
import com.cloudbees.hudson.plugins.folder.Folder;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import com.synopsys.arc.jenkins.plugins.ownership.extensions.item_ownership_policy.AssignCreatorPolicy;
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper;
import hudson.model.FreeStyleProject;
import hudson.model.User;
import hudson.remoting.Callable;
import hudson.security.ACL;
Expand Down Expand Up @@ -128,4 +130,51 @@ public void checkRoles(RoleChecker checker) throws SecurityException {
assertThat("testUser should be retained after the restart",
ownerPropertyReloaded.getOwnership().getPrimaryOwnerId(), equalTo("testUser"));
}

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

// Set ownership via API
OwnershipDescription original = new OwnershipDescription(true, "ownerId",
Arrays.asList("coowner1, coowner2"));
FolderOwnershipHelper.setOwnership(folder, original);

assertThat("Folder ownership helper should return the inherited value",
JobOwnerHelper.Instance.getOwnershipDescription(project),
equalTo(original));


// Reload folder from disk and check the state
j.jenkins.reload();
assertThat("Folder ownership helper should return the inherited value after the reload",
JobOwnerHelper.Instance.getOwnershipDescription(
j.jenkins.getItemByFullName("myFolder/projectInFolder", FreeStyleProject.class)),
equalTo(original));
}

@Test
public void ownershipShouldBeInheritedFromTopLevelFolderByDefault() throws Exception {
Folder folder1 = j.jenkins.createProject(Folder.class, "folder1");
Folder folder2 = folder1.createProject(Folder.class, "folder2");
FreeStyleProject project = folder2.createProject(FreeStyleProject.class, "projectInFolder");

// Set ownership via API
OwnershipDescription original = new OwnershipDescription(true, "ownerId",
Arrays.asList("coowner1, coowner2"));
FolderOwnershipHelper.setOwnership(folder1, original);

assertThat("Folder ownership helper should return the inherited value",
JobOwnerHelper.Instance.getOwnershipDescription(project),
equalTo(original));


// Reload folder from disk and check the state
j.jenkins.reload();
assertThat("Folder ownership helper should return the inherited value after the reload",
JobOwnerHelper.Instance.getOwnershipDescription(
j.jenkins.getItemByFullName("folder1/folder2/projectInFolder", FreeStyleProject.class)),
equalTo(original));
}
}

0 comments on commit 8207da5

Please sign in to comment.