Search in sources :

Example 36 with BulkChange

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

the class OrganizationFolder method computeChildren.

/**
 * {@inheritDoc}
 */
@Override
protected void computeChildren(final ChildObserver<MultiBranchProject<?, ?>> observer, final TaskListener listener) throws IOException, InterruptedException {
    // capture the current digests to prevent unnecessary rescan if re-saving after scan
    try {
        navDigest = Util.getDigestOf(Items.XSTREAM2.toXML(navigators));
    } catch (XStreamException e) {
        navDigest = null;
    }
    try {
        facDigest = Util.getDigestOf(Items.XSTREAM2.toXML(projectFactories));
    } catch (XStreamException e) {
        facDigest = null;
    }
    try {
        bbsDigest = Util.getDigestOf(Items.XSTREAM2.toXML(buildStrategies));
    } catch (XStreamException e) {
        bbsDigest = null;
    }
    long start = System.currentTimeMillis();
    listener.getLogger().format("[%tc] Starting organization scan...%n", start);
    try {
        listener.getLogger().format("[%tc] Updating actions...%n", System.currentTimeMillis());
        Map<SCMNavigator, List<Action>> navigatorActions = new HashMap<>();
        for (SCMNavigator navigator : navigators) {
            List<Action> actions;
            try {
                actions = navigator.fetchActions(this, null, listener);
            } catch (IOException e) {
                printStackTrace(e, listener.error("[%tc] Could not refresh actions for navigator %s", System.currentTimeMillis(), navigator));
                // preserve previous actions if we have some transient error fetching now (e.g. API rate limit)
                actions = Util.fixNull(state.getActions().get(navigator));
            }
            navigatorActions.put(navigator, actions);
        }
        // update any persistent actions for the SCMNavigator
        if (!navigatorActions.equals(state.getActions())) {
            boolean saveProject = false;
            for (List<Action> actions : navigatorActions.values()) {
                for (Action a : actions) {
                    // undo any hacks that attached the contributed actions without attribution
                    saveProject = removeActions(a.getClass()) || saveProject;
                }
            }
            BulkChange bc = new BulkChange(state);
            try {
                state.setActions(navigatorActions);
                try {
                    bc.commit();
                } catch (IOException | RuntimeException e) {
                    listener.error("[%tc] Could not persist folder level actions", System.currentTimeMillis());
                    throw e;
                }
                if (saveProject) {
                    try {
                        save();
                    } catch (IOException | RuntimeException e) {
                        listener.error("[%tc] Could not persist folder level configuration changes", System.currentTimeMillis());
                        throw e;
                    }
                }
            } finally {
                bc.abort();
            }
        }
        for (SCMNavigator navigator : navigators) {
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
            listener.getLogger().format("[%tc] Consulting %s%n", System.currentTimeMillis(), navigator.getDescriptor().getDisplayName());
            try {
                navigator.visitSources(new SCMSourceObserverImpl(listener, observer, navigator, (SCMSourceEvent<?>) null));
            } catch (IOException | InterruptedException | RuntimeException e) {
                listener.error("[%tc] Could not fetch sources from navigator %s", System.currentTimeMillis(), navigator);
                throw e;
            }
        }
    } finally {
        long end = System.currentTimeMillis();
        listener.getLogger().format("[%tc] Finished organization scan. Scan took %s%n", end, Util.getTimeSpanString(end - start));
    }
}
Also used : SCMNavigator(jenkins.scm.api.SCMNavigator) ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) Action(hudson.model.Action) HashMap(java.util.HashMap) BulkChange(hudson.BulkChange) IOException(java.io.IOException) SCMSourceEvent(jenkins.scm.api.SCMSourceEvent) XStreamException(com.thoughtworks.xstream.XStreamException) DescribableList(hudson.util.DescribableList) List(java.util.List) ArrayList(java.util.ArrayList) ExtensionList(hudson.ExtensionList)

Example 37 with BulkChange

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

the class MultiBranchProject method computeChildren.

/**
 * {@inheritDoc}
 */
@Override
protected void computeChildren(final ChildObserver<P> observer, final TaskListener listener) throws IOException, InterruptedException {
    // capture the current digests to prevent unnecessary reindex if re-saving after index
    try {
        srcDigest = Util.getDigestOf(Items.XSTREAM2.toXML(sources));
    } catch (XStreamException e) {
        srcDigest = null;
    }
    try {
        facDigest = Util.getDigestOf(Items.XSTREAM2.toXML(getProjectFactory()));
    } catch (XStreamException e) {
        facDigest = null;
    }
    long start = System.currentTimeMillis();
    listener.getLogger().format("[%tc] Starting branch indexing...%n", start);
    try {
        final BranchProjectFactory<P, R> _factory = getProjectFactory();
        List<SCMSource> scmSources = getSCMSources();
        Map<String, List<Action>> sourceActions = new LinkedHashMap<>();
        for (SCMSource source : scmSources) {
            try {
                sourceActions.put(source.getId(), source.fetchActions(null, listener));
            } catch (IOException | InterruptedException | RuntimeException e) {
                listener.error("[%tc] Could not update folder level actions from source %s", System.currentTimeMillis(), source.getId());
                throw e;
            }
        }
        // update any persistent actions for the SCMSource
        if (!sourceActions.equals(state.sourceActions)) {
            boolean saveProject = false;
            for (List<Action> actions : sourceActions.values()) {
                for (Action a : actions) {
                    // undo any hacks that attached the contributed actions without attribution
                    saveProject = removeActions(a.getClass()) || saveProject;
                }
            }
            BulkChange bc = new BulkChange(state);
            try {
                state.sourceActions.keySet().retainAll(sourceActions.keySet());
                state.sourceActions.putAll(sourceActions);
                try {
                    bc.commit();
                } catch (IOException | RuntimeException e) {
                    listener.error("[%tc] Could not persist folder level actions", System.currentTimeMillis());
                    throw e;
                }
                if (saveProject) {
                    try {
                        save();
                    } catch (IOException | RuntimeException e) {
                        listener.error("[%tc] Could not persist folder level configuration changes", System.currentTimeMillis());
                        throw e;
                    }
                }
            } finally {
                bc.abort();
            }
        }
        for (final SCMSource source : scmSources) {
            try {
                source.fetch(new SCMHeadObserverImpl(source, observer, listener, _factory, new IndexingCauseFactory(), null), listener);
            } catch (IOException | InterruptedException | RuntimeException e) {
                listener.error("[%tc] Could not fetch branches from source %s", System.currentTimeMillis(), source.getId());
                throw e;
            }
        }
    } finally {
        long end = System.currentTimeMillis();
        listener.getLogger().format("[%tc] Finished branch indexing. Indexing took %s%n", end, Util.getTimeSpanString(end - start));
    }
}
Also used : ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) Action(hudson.model.Action) CauseAction(hudson.model.CauseAction) BulkChange(hudson.BulkChange) NullSCMSource(jenkins.scm.impl.NullSCMSource) SCMSource(jenkins.scm.api.SCMSource) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) XStreamException(com.thoughtworks.xstream.XStreamException) List(java.util.List) PersistedList(hudson.util.PersistedList) ArrayList(java.util.ArrayList)

Example 38 with BulkChange

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

the class NamespaceAction method pop.

@Deprecated
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
public String pop() throws IOException {
    if (run == null) {
        LOGGER.warning("run is null, cannot pop");
        return null;
    }
    synchronized (run) {
        BulkChange bc = new BulkChange(run);
        try {
            NamespaceAction action = run.getAction(NamespaceAction.class);
            if (action == null) {
                action = new NamespaceAction(run);
                run.addAction(action);
            }
            String namespace = action.stack.pop();
            bc.commit();
            return namespace;
        } finally {
            bc.abort();
            return null;
        }
    }
}
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