Search in sources :

Example 1 with PromotionBadge

use of hudson.plugins.promoted_builds.PromotionBadge in project promoted-builds-plugin by jenkinsci.

the class GroovyCondition method isMet.

@Override
public PromotionBadge isMet(final PromotionProcess promotionProcess, final AbstractBuild<?, ?> build) {
    // TODO switch to Jenkins.getActiveInstance() once bumped to 1.590
    final Jenkins jenkins = Jenkins.getInstance();
    if (jenkins == null) {
        // Jenkins not started or shut down
        LOGGER.log(Level.WARNING, "Missing Jenkins instance");
        return null;
    }
    final PluginManager pluginManager = jenkins.getPluginManager();
    if (pluginManager == null) {
        LOGGER.log(Level.WARNING, "Unable to retrieve PluginManager");
        return null;
    }
    final ClassLoader classLoader = pluginManager.uberClassLoader;
    final Binding binding = new Binding();
    binding.setVariable("promotionProcess", promotionProcess);
    binding.setVariable("build", build);
    binding.setVariable("jenkins", jenkins);
    Object result = null;
    try {
        result = script.evaluate(classLoader, binding);
    } catch (final RejectedAccessException e) {
        LOGGER.log(Level.WARNING, "Sandbox exception", e);
        return null;
    } catch (final UnapprovedUsageException e) {
        LOGGER.log(Level.WARNING, "Unapproved script", e);
        return null;
    } catch (final UnapprovedClasspathException e) {
        LOGGER.log(Level.WARNING, "Unapproved classpath", e);
        return null;
    } catch (final Exception e) {
        LOGGER.log(Level.WARNING, "Evaluation error", e);
        return null;
    }
    final String displayLabel = metQualificationLabel == null ? Messages.GroovyCondition_MetQualificationLabel() : metQualificationLabel;
    if (Boolean.TRUE.equals(result)) {
        return new Badge(displayLabel, Collections.<String, String>emptyMap());
    } else if (result instanceof Map && !((Map) result).isEmpty()) {
        final Map<String, String> variables = new HashMap<String, String>(((Map) result).size());
        for (final Map.Entry entry : ((Map<Object, Object>) result).entrySet()) {
            final Object key = entry.getKey();
            final Object value = entry.getValue();
            if (key == null) {
                continue;
            }
            variables.put(key.toString(), value == null ? "" : value.toString());
        }
        return new Badge(displayLabel, variables);
    }
    return null;
}
Also used : Binding(groovy.lang.Binding) RejectedAccessException(org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException) UnapprovedUsageException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException) UnapprovedClasspathException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedClasspathException) PromotionBadge(hudson.plugins.promoted_builds.PromotionBadge) UnapprovedClasspathException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedClasspathException) UnapprovedUsageException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException) RejectedAccessException(org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException) Jenkins(jenkins.model.Jenkins) PluginManager(hudson.PluginManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PromotionBadge

use of hudson.plugins.promoted_builds.PromotionBadge in project promoted-builds-plugin by jenkinsci.

the class UpstreamPromotionCondition method isMet.

@Override
public PromotionBadge isMet(PromotionProcess promotionProcess, AbstractBuild<?, ?> build) {
    Badge badge = new Badge();
    Set<String> requiredPromotions = getRequiredPromotionNamesAsSet();
    if (requiredPromotions.isEmpty()) {
        return badge;
    }
    PromotedBuildAction pba = build.getAction(PromotedBuildAction.class);
    if (pba == null) {
        return null;
    }
    for (Status status : pba.getPromotions()) {
        if (status.isPromotionSuccessful()) {
            requiredPromotions.remove(status.getName());
            badge.add(status.getName());
            // short circuit for loop if 
            if (requiredPromotions.isEmpty())
                break;
        }
    }
    return requiredPromotions.isEmpty() ? badge : null;
}
Also used : Status(hudson.plugins.promoted_builds.Status) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) PromotionBadge(hudson.plugins.promoted_builds.PromotionBadge)

Example 3 with PromotionBadge

use of hudson.plugins.promoted_builds.PromotionBadge in project promoted-builds-plugin by jenkinsci.

the class DownstreamPassCondition method isMet.

@Override
public PromotionBadge isMet(PromotionProcess promotionProcess, AbstractBuild<?, ?> build) {
    Badge badge = new Badge();
    PseudoDownstreamBuilds pdb = build.getAction(PseudoDownstreamBuilds.class);
    EnvVars buildEnvironment = new EnvVars(build.getBuildVariables());
    OUTER: for (AbstractProject<?, ?> j : getJobList(build.getProject().getParent(), buildEnvironment)) {
        for (AbstractBuild<?, ?> b : build.getDownstreamBuilds(j)) {
            if (!b.isBuilding()) {
                Result r = b.getResult();
                if ((r == Result.SUCCESS) || (evenIfUnstable && r == Result.UNSTABLE)) {
                    badge.add(b);
                    continue OUTER;
                }
            }
        }
        if (pdb != null) {
            // if fingerprint doesn't have any, try the pseudo-downstream
            for (AbstractBuild<?, ?> b : pdb.listBuilds(j)) {
                if (!b.isBuilding()) {
                    Result r = b.getResult();
                    if ((r == Result.SUCCESS) || (evenIfUnstable && r == Result.UNSTABLE)) {
                        badge.add(b);
                        continue OUTER;
                    }
                }
            }
        }
        // none of the builds of this job passed.
        return null;
    }
    return badge;
}
Also used : EnvVars(hudson.EnvVars) AbstractBuild(hudson.model.AbstractBuild) PromotionBadge(hudson.plugins.promoted_builds.PromotionBadge) AbstractProject(hudson.model.AbstractProject) Result(hudson.model.Result)

Aggregations

PromotionBadge (hudson.plugins.promoted_builds.PromotionBadge)3 Binding (groovy.lang.Binding)1 EnvVars (hudson.EnvVars)1 PluginManager (hudson.PluginManager)1 AbstractBuild (hudson.model.AbstractBuild)1 AbstractProject (hudson.model.AbstractProject)1 Result (hudson.model.Result)1 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)1 Status (hudson.plugins.promoted_builds.Status)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Jenkins (jenkins.model.Jenkins)1 RejectedAccessException (org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException)1 UnapprovedClasspathException (org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedClasspathException)1 UnapprovedUsageException (org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException)1