Search in sources :

Example 21 with BulkChange

use of hudson.BulkChange in project branch-api-plugin by jenkinsci.

the class BasicBranchProjectFactory method setBranch.

@Override
public FreeStyleProject setBranch(FreeStyleProject project, Branch branch) {
    BulkChange bc = new BulkChange(project);
    try {
        BasicBranchProperty prop = project.getProperty(BasicBranchProperty.class);
        if (prop == null) {
            project.addProperty(new BasicBranchProperty(branch));
        } else {
            prop.setBranch(branch);
        }
        assert project.getProperty(BasicBranchProperty.class).getBranch().equals(branch);
        if (branch instanceof Branch.Dead) {
            if (!project.isDisabled()) {
                project.disable();
            }
        } else {
            if (project.isDisabled()) {
                project.enable();
            }
        }
        bc.commit();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        bc.abort();
    }
    return project;
}
Also used : BulkChange(hudson.BulkChange) IOException(java.io.IOException)

Example 22 with BulkChange

use of hudson.BulkChange in project branch-api-plugin by jenkinsci.

the class BranchProjectFactory method decorate.

/**
 * Decorates the project in with all the {@link JobDecorator} instances.
 * NOTE: This method should suppress saving the project and only affect the in-memory state.
 * NOTE: Override if the default strategy is not appropriate for the specific project type.
 *
 * @param project the project.
 * @return the project for nicer method chaining
 */
@SuppressWarnings({ "ConstantConditions", "unchecked" })
public P decorate(P project) {
    if (!isProject(project)) {
        return project;
    }
    Branch branch = getBranch(project);
    // HACK ALERT
    // ==========
    // We don't want to trigger a save, so we will do some trickery to inject the new values
    // it would be better if Core gave us some hooks to do this
    BulkChange bc = new BulkChange(project);
    try {
        List<BranchProperty> properties = new ArrayList<>(branch.getProperties());
        Collections.sort(properties, DescriptorOrder.reverse(BranchProperty.class));
        for (BranchProperty property : properties) {
            JobDecorator<P, R> decorator = property.jobDecorator((Class) project.getClass());
            if (decorator != null) {
                // if Project then we can feed the publishers and build wrappers
                if (project instanceof Project && decorator instanceof ProjectDecorator) {
                    DescribableList<Publisher, Descriptor<Publisher>> publishersList = ((Project) project).getPublishersList();
                    DescribableList<BuildWrapper, Descriptor<BuildWrapper>> buildWrappersList = ((Project) project).getBuildWrappersList();
                    List<Publisher> publishers = ((ProjectDecorator) decorator).publishers(publishersList.toList());
                    List<BuildWrapper> buildWrappers = ((ProjectDecorator) decorator).buildWrappers(buildWrappersList.toList());
                    publishersList.replaceBy(publishers);
                    buildWrappersList.replaceBy(buildWrappers);
                }
                // we can always feed the job properties... but just not as easily as we'd like
                List<JobProperty<? super P>> jobProperties = decorator.jobProperties(project.getAllProperties());
                // both removal and addition
                for (JobProperty<? super P> p : project.getAllProperties()) {
                    project.removeProperty(p);
                }
                for (JobProperty<? super P> p : jobProperties) {
                    project.addProperty(p);
                }
                // now apply the final layer
                decorator.project(project);
            }
        }
    } catch (IOException e) {
    // should be safe to ignore as the BulkChange suppresses the save operation.
    } finally {
        bc.abort();
    }
    return project;
}
Also used : ArrayList(java.util.ArrayList) BulkChange(hudson.BulkChange) Publisher(hudson.tasks.Publisher) IOException(java.io.IOException) Project(hudson.model.Project) Descriptor(hudson.model.Descriptor) JobProperty(hudson.model.JobProperty) BuildWrapper(hudson.tasks.BuildWrapper)

Example 23 with BulkChange

use of hudson.BulkChange in project kubernetes-plugin by jenkinsci.

the class AbstractInvisibleRunAction2 method push.

protected static void push(@NonNull Run<?, ?> run, @NonNull Class<? extends AbstractInvisibleRunAction2> clazz, @NonNull String item) throws IOException {
    synchronized (run) {
        BulkChange bc = new BulkChange(run);
        try {
            AbstractInvisibleRunAction2 action = run.getAction(clazz);
            if (action == null) {
                action = clazz.newInstance();
                run.addAction(action);
            }
            LOGGER.log(Level.FINEST, "Pushing item {0} to action {1} in run {2}", new Object[] { item, action, run });
            action.stack.push(item);
            bc.commit();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Can not instantiate class " + clazz, e);
        } finally {
            bc.abort();
        }
    }
}
Also used : BulkChange(hudson.BulkChange)

Example 24 with BulkChange

use of hudson.BulkChange in project kubernetes-plugin by jenkinsci.

the class NamespaceAction method push.

@Deprecated
public void push(String namespace) throws IOException {
    if (run == null) {
        LOGGER.warning("run is null, cannot push");
        return;
    }
    synchronized (run) {
        BulkChange bc = new BulkChange(run);
        try {
            NamespaceAction action = run.getAction(NamespaceAction.class);
            if (action == null) {
                action = new NamespaceAction(run);
                run.addAction(action);
            }
            action.stack.push(namespace);
            bc.commit();
        } finally {
            bc.abort();
        }
    }
}
Also used : BulkChange(hudson.BulkChange)

Example 25 with BulkChange

use of hudson.BulkChange in project kubernetes-plugin by jenkinsci.

the class PodTemplateAction method push.

@Deprecated
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
public void push(String template) throws IOException {
    if (run == null) {
        LOGGER.warning("run is null, cannot push");
        return;
    }
    synchronized (run) {
        BulkChange bc = new BulkChange(run);
        try {
            PodTemplateAction action = run.getAction(PodTemplateAction.class);
            if (action == null) {
                action = new PodTemplateAction(run);
                run.addAction(action);
            }
            action.stack.push(template);
            bc.commit();
        } finally {
            bc.abort();
        }
    }
}
Also used : BulkChange(hudson.BulkChange) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

BulkChange (hudson.BulkChange)38 IOException (java.io.IOException)11 JSONObject (net.sf.json.JSONObject)7 POST (org.kohsuke.stapler.verb.POST)7 RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 XStreamException (com.thoughtworks.xstream.XStreamException)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Action (hudson.model.Action)2 Descriptor (hudson.model.Descriptor)2 FormException (hudson.model.Descriptor.FormException)2 FreeStyleBuild (hudson.model.FreeStyleBuild)2 FreeStyleProject (hudson.model.FreeStyleProject)2 Saveable (hudson.model.Saveable)2 NodeMonitor (hudson.node_monitors.NodeMonitor)2 List (java.util.List)2 ObjectMetadataAction (jenkins.scm.api.metadata.ObjectMetadataAction)2