Skip to content
Permalink
Browse files
[JENKINS-28881] - Support AssignCreatorPolicy for Folders
  • Loading branch information
oleg-nenashev committed Jan 3, 2016
1 parent 0be1333 commit 206a36c085f35c2a06966af6aca6ea8f80772bc3
Showing 4 changed files with 131 additions and 4 deletions.
@@ -48,7 +48,7 @@ public AssignCreatorPolicy() {
@Override
public OwnershipDescription onCreated(Item item) {
User creator = User.current();
if (creator != null && creator != User.getUnknown() && item instanceof Job) {
if (creator != null && creator != User.getUnknown()) {
return new OwnershipDescription(true, creator.getId(), null);
}

@@ -0,0 +1,74 @@
/*
* 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.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.extensions.ItemOwnershipPolicy;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.listeners.ItemListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Locates changes in {@link Folder}s and assigns ownership accordingly.
* @author Oleg Nenashev
*/
@Extension(optional = true)
public class FolderItemListener extends ItemListener {

private static final Logger LOGGER = Logger.getLogger(FolderItemListener.class.getName());

@Override
public void onCopied(Item src, Item item) {
OwnershipDescription d = getPolicy().onCopied(src, item);
modifyOwnership(item, d);
}

@Override
public void onCreated(Item item) {
OwnershipDescription d = getPolicy().onCreated(item);
modifyOwnership(item, d);
}

private ItemOwnershipPolicy getPolicy() {
return OwnershipPlugin.getInstance().getConfiguration().getItemOwnershipPolicy();
}

private void modifyOwnership(Item item, OwnershipDescription ownership) {
if (item instanceof AbstractFolder) {
AbstractFolder<?> folder = (AbstractFolder) item;
try {
FolderOwnershipHelper.setOwnership(folder, ownership);
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Cannot change ownership of {0} to [{1}]. {2}",
new Object[] {item, ownership, ex});
}
}
}
}
@@ -66,7 +66,7 @@ public AbstractFolder<?> getDescribedItem() {

@Override
public OwnershipDescription getOwnership() {
return ownership;
return ownership != null ? ownership : OwnershipDescription.DISABLED_DESCR;
}

/**
@@ -26,8 +26,15 @@
import com.cloudbees.hudson.plugins.folder.AbstractFolder;
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 hudson.model.User;
import hudson.remoting.Callable;
import hudson.security.ACL;
import hudson.security.SecurityRealm;
import java.util.Arrays;
import static org.hamcrest.Matchers.*;
import org.jenkinsci.plugins.ownership.test.util.OwnershipPluginConfigurer;
import org.jenkinsci.remoting.RoleChecker;
import static org.junit.Assert.assertThat;

import org.junit.Rule;
@@ -48,8 +55,10 @@ public class FolderOwnershipTest {
@Test
public void ownershipInfoShouldBeEmptyByDefault() throws Exception {
Folder folder = j.jenkins.createProject(Folder.class, "myFolder");
assertThat("Property should not be injected by default",
FolderOwnershipHelper.getOwnerProperty(folder), nullValue());
assertThat("Property should be injected by default",
FolderOwnershipHelper.getOwnerProperty(folder), notNullValue());
assertThat("Property should be disabled by default",
FolderOwnershipHelper.getOwnerProperty(folder).getOwnership().isOwnershipEnabled(), equalTo(false));

assertThat("Folder ownership helper should return the \"disabled\" description",
ownershipHelper.getOwnershipDescription(folder),
@@ -75,4 +84,48 @@ public void ownershipInfoShouldSurviveRoundtrip() throws Exception {
ownershipHelper.getOwnershipDescription(folder),
equalTo(original));
}

@Test
public void shouldSupportAssignCreatorPolicy() throws Exception {

// Init security
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
User myUser = User.get("testUser");

// Configure the policy
OwnershipPluginConfigurer.forJenkinsRule(j)
.withItemOwnershipPolicy(new AssignCreatorPolicy())
.configure();

// Create Item from the user account
ACL.impersonate(myUser.impersonate(), new Callable<Void, Exception>() {
@Override
public Void call() throws Exception {
j.jenkins.createProject(Folder.class, "myFolder");
return null;
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
// do nothing
}
});

// Retrieve item and verify it's status
Folder folder = j.jenkins.getItemByFullName("myFolder", Folder.class);
assertThat("Cannot locate folder 'myFolder'", folder, notNullValue());
FolderOwnershipProperty ownerProperty = FolderOwnershipHelper.getOwnerProperty(folder);
assertThat("Property should be injected by AssignCreatorPolicy", ownerProperty, notNullValue());
assertThat("Ownership should be enabled according to AssignCreatorPolicy",
ownerProperty.getOwnership().isOwnershipEnabled(), equalTo(true));
assertThat("testUser should be automatically assigned as a Folder owner",
ownerProperty.getOwnership().getPrimaryOwnerId(), equalTo("testUser"));

// Reload configs in order to verify the persistence
j.jenkins.reload();
Folder folderReloaded = j.jenkins.getItemByFullName("myFolder", Folder.class);
FolderOwnershipProperty ownerPropertyReloaded = FolderOwnershipHelper.getOwnerProperty(folderReloaded);
assertThat("testUser should be retained after the restart",
ownerPropertyReloaded.getOwnership().getPrimaryOwnerId(), equalTo("testUser"));
}
}

0 comments on commit 206a36c

Please sign in to comment.