Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  The MIT License
   *
   *  Copyright 2010 Sony Ericsson Mobile Communications. All rights reserved.
   *  Copyright 2012 Sony Mobile Communications AB. All rights reserved.
   *
   *  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 com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier;
 
 
 import java.util.Map;
Expands a parameterized string to its full potential.

Author(s):
Robert Sandell <robert.sandell@sonyericsson.com>
 
 public class ParameterExpander {

    
How many default parameters there are (plus one) to initialize the size of the parameters-map.
 
     public static final int DEFAULT_PARAMETERS_COUNT = 11;

    
The delimiter used to separate build URLs from their messages.
 
     public static final String MESSAGE_DELIMITER = " : ";
 
     private static final Logger logger = LoggerFactory.getLogger(ParameterExpander.class);
     private Hudson hudson;

    
Constructor.

Parameters:
config the global config.
hudson the Hudson instance.
 
     public ParameterExpander(IGerritHudsonTriggerConfig configHudson hudson) {
         this. = config;
         this. = hudson;
     }

    
Constructor.

Parameters:
config the global config.
 
     public ParameterExpander(IGerritHudsonTriggerConfig config) {
         this(config, Hudson.getInstance());
     }

    
Gets the expanded string to send to Gerrit for a build-started event.

Parameters:
r the build.
taskListener the taskListener.
event the event.
stats the statistics.
Returns:
the "expanded" command string.
 
     public String getBuildStartedCommand(AbstractBuild rTaskListener taskListener,
             ChangeBasedEvent eventBuildsStartedStats stats) {
 
         GerritTrigger trigger = GerritTrigger.getTrigger(r.getProject());
         String gerritCmd = .getGerritCmdBuildStarted();
         Map<StringStringparameters = createStandardParameters(revent,
                 getBuildStartedCodeReviewValue(r),
                getBuildStartedVerifiedValue(r));
        StringBuilder startedStats = new StringBuilder();
        if (stats.getTotalBuildsToStart() > 1) {
            startedStats.append(stats.toString());
        }
        String buildStartMessage = trigger.getBuildStartMessage();
        if (buildStartMessage != null && !buildStartMessage.equals("")) {
            startedStats.append(" \n ").append(buildStartMessage);
        }
        parameters.put("STARTED_STATS"startedStats.toString());
        StringBuilder customMessages = new StringBuilder();
        for (GerritMessageProvider messageProvider : emptyIfNull(GerritMessageProvider.all())) {
            String customMessage = messageProvider.getBuildStartedMessage(r);
            if (customMessage != null) {
                customMessages.append(" \n ").append(customMessage);
            }
        }
        parameters.put("CUSTOM_MESSAGES"customMessages.toString());
        return expandParameters(gerritCmdrtaskListenerparameters);
    }

    
Helper for ensuring no NPEs when iterating iterables.

Parameters:
<T> type
iterable the iterable
Returns:
empty if null or the iterable
    private static <T> Iterable<T> emptyIfNull(Iterable<T> iterable) {
        if (iterable == null) {
            return Collections.<T>emptyList();
        } else {
            return iterable;
        }
    }

    
Finds the verified vote for build started of the specified build. If there is a com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger and it has a com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger.getGerritBuildStartedVerifiedValue() specified, that value will be used, otherwise the global config value in com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig.getGerritBuildStartedVerifiedValue() will be used.

Parameters:
r the build.
Returns:
the value.
    private int getBuildStartedVerifiedValue(AbstractBuild r) {
        GerritTrigger trigger = GerritTrigger.getTrigger(r.getProject());
        if (trigger == null) {
            .warn("Unable to get trigger config for build {} will use global value.");
            return .getGerritBuildStartedVerifiedValue();
        } else if (trigger.getGerritBuildStartedVerifiedValue() != null) {
            final Integer value = trigger.getGerritBuildStartedVerifiedValue();
            .trace("BuildStartedVerified overridden in project config. returning {}"value);
            return value;
        } else {
            final int value = .getGerritBuildStartedVerifiedValue();
            .trace("BuildStartedVerified standard value used {}"value);
            return value;
        }
    }

    
Finds the code review vote for build started of the specified build. If there is a com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger and it has a com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger.getGerritBuildStartedCodeReviewValue() specified, that value will be used, otherwise the global config value in com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig.getGerritBuildStartedCodeReviewValue() will be used.

Parameters:
r the build.
Returns:
the value.
        GerritTrigger trigger = GerritTrigger.getTrigger(r.getProject());
        if (trigger == null) {
            .warn("Unable to get trigger config for build {} will use global value.");
            return .getGerritBuildStartedCodeReviewValue();
        } else if (trigger.getGerritBuildStartedCodeReviewValue() != null) {
            final Integer value = trigger.getGerritBuildStartedCodeReviewValue();
            .trace("BuildStartedCodeReview overridden in project config. returning {}"value);
            return value;
        } else {
            final int value = .getGerritBuildStartedCodeReviewValue();
            .trace("BuildStartedCodeReview standard value used {}"value);
            return value;
        }
    }

    
Creates a list of the "standard" trigger parameters. They are present both for build started and completed. The parameters are:
  • GERRIT_NAME: The Gerrit project name.
  • CHANGE_ID: The Gerrit change-id (SHA-1).
  • BRANCH: The branch of the project.
  • CHANGE: The change number.
  • PATCHSET: The patchset number.
  • PATCHSET_REVISION: The patchset revision.
  • REFSPEC: The ref-spec. (refs/changes/xx/xxxx/z).
  • BUILDURL: The URL to the build.
  • VERIFIED: The verified vote.
  • CODE_REVIEW: The code review vote.

Parameters:
r the build.
gerritEvent the event.
codeReview the code review vote.
verified the verified vote.
Returns:
the parameters and their values.
            int codeReviewint verified) {
        //<GERRIT_NAME> <BRANCH> <CHANGE> <PATCHSET> <PATCHSET_REVISION> <REFSPEC> <BUILDURL> VERIFIED CODE_REVIEW
        Map<StringStringmap = new HashMap<StringString>();
        if (gerritEvent instanceof ChangeBasedEvent) {
            ChangeBasedEvent event = (ChangeBasedEvent)gerritEvent;
            map.put("GERRIT_NAME"event.getChange().getProject());
            map.put("CHANGE_ID"event.getChange().getId());
            map.put("BRANCH"event.getChange().getProject());
            map.put("CHANGE"event.getChange().getNumber());
            map.put("PATCHSET"event.getPatchSet().getNumber());
            map.put("PATCHSET_REVISION"event.getPatchSet().getRevision());
            map.put("REFSPEC", StringUtil.makeRefSpec(event));
        }
        if (r != null) {
            map.put("BUILDURL".getRootUrl() + r.getUrl());
        }
        map.put("VERIFIED", String.valueOf(verified));
        map.put("CODE_REVIEW", String.valueOf(codeReview));
        return map;
    }

    
Expands all types of parameters in the string and returns the "replaced" string. Both types means both $ENV_VARS and <PLUGIN_VARS>

Parameters:
gerritCommand the command "template"
r the build containing the environment vars.
taskListener the taskListener
parameters the <parameters> from the trigger.
Returns:
the expanded string.
    private String expandParameters(String gerritCommandAbstractBuild rTaskListener taskListener,
            Map<StringStringparameters) {
        if (r != null && taskListener != null) {
            try {
                gerritCommand = r.getEnvironment(taskListener).expand(gerritCommand);
            } catch (Exception ex) {
                .error("Failed to expand env vars into gerrit cmd. Gerrit won't be notified!!"ex);
                return null;
            }
        }
        for (Map.Entry<StringStringparam : parameters.entrySet()) {
            gerritCommand = gerritCommand.replace("<" + param.getKey() + ">"param.getValue());
        }
        return gerritCommand;
    }

    
Finds the code review value for the specified build result on the configured trigger.

Parameters:
res the build result.
trigger the trigger that might have overridden values.
Returns:
the value.
    protected int getCodeReviewValue(Result resGerritTrigger trigger) {
        if (res == .) {
            if (trigger.getGerritBuildSuccessfulCodeReviewValue() != null) {
                return trigger.getGerritBuildSuccessfulCodeReviewValue();
            } else {
                return .getGerritBuildSuccessfulCodeReviewValue();
            }
        } else if (res == . || res == .) {
            if (trigger.getGerritBuildFailedCodeReviewValue() != null) {
                return trigger.getGerritBuildFailedCodeReviewValue();
            } else {
                return .getGerritBuildFailedCodeReviewValue();
            }
        } else if (res == .) {
            if (trigger.getGerritBuildUnstableCodeReviewValue() != null) {
                return trigger.getGerritBuildUnstableCodeReviewValue();
            } else {
                return .getGerritBuildUnstableCodeReviewValue();
            }
        } else if (res == .) {
            if (trigger.getGerritBuildNotBuiltCodeReviewValue() != null) {
                return trigger.getGerritBuildNotBuiltCodeReviewValue();
            } else {
                return .getGerritBuildNotBuiltCodeReviewValue();
            }
        } else {
            //As bad as failue, for now
            if (trigger.getGerritBuildFailedCodeReviewValue() != null) {
                return trigger.getGerritBuildFailedCodeReviewValue();
            } else {
                return .getGerritBuildFailedCodeReviewValue();
            }
        }
    }

    
Finds the verified value for the specified build result on the configured trigger.

Parameters:
res the build result.
trigger the trigger that might have overridden values.
Returns:
the value.
    protected int getVerifiedValue(Result resGerritTrigger trigger) {
        if (res == .) {
            if (trigger.getGerritBuildSuccessfulVerifiedValue() != null) {
                return trigger.getGerritBuildSuccessfulVerifiedValue();
            } else {
                return .getGerritBuildSuccessfulVerifiedValue();
            }
        } else if (res == . || res == .) {
            if (trigger.getGerritBuildFailedVerifiedValue() != null) {
                return trigger.getGerritBuildFailedVerifiedValue();
            } else {
                return .getGerritBuildFailedVerifiedValue();
            }
        } else if (res == .) {
            if (trigger.getGerritBuildUnstableVerifiedValue() != null) {
                return trigger.getGerritBuildUnstableVerifiedValue();
            } else {
                return .getGerritBuildUnstableVerifiedValue();
            }
        } else if (res == .) {
            if (trigger.getGerritBuildNotBuiltVerifiedValue() != null) {
                return trigger.getGerritBuildNotBuiltVerifiedValue();
            } else {
                return .getGerritBuildNotBuiltVerifiedValue();
            }
        } else {
            //As bad as failure, for now
            if (trigger.getGerritBuildFailedVerifiedValue() != null) {
                return trigger.getGerritBuildFailedVerifiedValue();
            } else {
                return .getGerritBuildFailedVerifiedValue();
            }
        }
    }

    
Returns the minimum verified value for the build results in the memory.

Parameters:
memoryImprint the memory.
onlyBuilt only count builds that completed (no NOT_BUILT builds)
Returns:
the lowest verified value.
    protected int getMinimumVerifiedValue(MemoryImprint memoryImprintboolean onlyBuilt) {
        int verified = .;
        for (Entry entry : memoryImprint.getEntries()) {
            Result result = entry.getBuild().getResult();
            if (onlyBuilt && result == .) {
                continue;
            }
            GerritTrigger trigger = GerritTrigger.getTrigger(entry.getProject());
            verified = Math.min(verifiedgetVerifiedValue(resulttrigger));
        }
        return verified;
    }

    
Returns the minimum code review value for the build results in the memory.

Parameters:
memoryImprint the memory
onlyBuilt only count builds that completed (no NOT_BUILT builds)
Returns:
the lowest code review value.
    protected int getMinimumCodeReviewValue(MemoryImprint memoryImprintboolean onlyBuilt) {
        int codeReview = .;
        for (Entry entry : memoryImprint.getEntries()) {
            Result result = entry.getBuild().getResult();
            if (onlyBuilt && result == .) {
                continue;
            }
            GerritTrigger trigger = GerritTrigger.getTrigger(entry.getProject());
            codeReview = Math.min(codeReviewgetCodeReviewValue(resulttrigger));
        }
        return codeReview;
    }

    
Gets the "expanded" build completed command to send to gerrit.

Parameters:
memoryImprint the memory with all the information
listener the taskListener
Returns:
the command.
    public String getBuildCompletedCommand(MemoryImprint memoryImprintTaskListener listener) {
        String command;
        // We only count builds without NOT_BUILT status normally. If *no*
        // builds were successful, unstable or failed, we find the minimum
        // verified/code review value for the NOT_BUILT ones too.
        boolean onlyCountBuilt = true;
        if (memoryImprint.whereAllBuildsSuccessful()) {
            command = .getGerritCmdBuildSuccessful();
        } else if (memoryImprint.whereAnyBuildsFailed()) {
            command = .getGerritCmdBuildFailed();
        } else if (memoryImprint.whereAnyBuildsUnstable()) {
            command = .getGerritCmdBuildUnstable();
        } else if (memoryImprint.wereAllBuildsNotBuilt()) {
            onlyCountBuilt = false;
            command = .getGerritCmdBuildNotBuilt();
        } else {
            //Just as bad as failed for now.
            command = .getGerritCmdBuildFailed();
        }
        int verified = 0;
        int codeReview = 0;
        if (memoryImprint.getEvent().isScorable()) {
            verified = getMinimumVerifiedValue(memoryImprintonlyCountBuilt);
            codeReview = getMinimumCodeReviewValue(memoryImprintonlyCountBuilt);
        }
        Map<StringStringparameters = createStandardParameters(nullmemoryImprint.getEvent(), codeReviewverified);
        parameters.put("BUILDS_STATS"createBuildsStats(memoryImprintlistenerparameters));
        AbstractBuild build = null;
        Entry[] entries = memoryImprint.getEntries();
        if (entries.length > 0 && entries[0].getBuild() != null) {
            build = entries[0].getBuild();
        }
        StringBuilder customMessages = new StringBuilder();
        for (GerritMessageProvider messageProvider : emptyIfNull(GerritMessageProvider.all())) {
            String customMessage = messageProvider.getBuildCompletedMessage(build);
            if (customMessage != null) {
                customMessages.append(" \n ").append(customMessage);
            }
        }
        parameters.put("CUSTOM_MESSAGES"customMessages.toString());
        return expandParameters(commandbuildlistenerparameters);
    }

    
Creates the BUILD_STATS string to send in a message, it contains the status of every build with its URL.

Parameters:
memoryImprint the memory of all the builds.
listener the taskListener
parameters the <parameters> from the trigger.
Returns:
the string.
    private String createBuildsStats(MemoryImprint memoryImprintTaskListener listener,
            Map<StringStringparameters) {
        StringBuilder str = new StringBuilder("");
        final String rootUrl = .getRootUrl();
        String unsuccessfulMessage = null;
        Entry[] entries = memoryImprint.getEntries();
        // In Gerrit, all lines before the first empty line are used as the summary.
        // For the summary all single linefeeds will be removed (only in Gerrit, not sent mails).
        // Hence, for the multi-builds, we will add a double linefeed before actually listing
        // the build results.
        if (entries.length > 0) {
            for (Entry entry : entries) {
                AbstractBuild build = entry.getBuild();
                if (build != null) {
                    GerritTrigger trigger = GerritTrigger.getTrigger(build.getProject());
                    Result res = build.getResult();
                    String customMessage = null;
                    /* Gerrit comments cannot contain single-newlines, as they will be joined
                     * together. Double newlines are interpreted as paragraph breaks. Lines that
                     * begin with a space (even if the space occurs somewhere in the middle of
                     * a multi-line paragraph) are interpreted as code blocks.
                     */
                    str.append("\n\n");
                    if (trigger.getCustomUrl() == null || trigger.getCustomUrl().isEmpty()) {
                        str.append(rootUrl).append(entry.getBuild().getUrl());
                    } else {
                        str.append(expandParameters(trigger.getCustomUrl(), buildlistenerparameters));
                    }
                    str.append();
                    if (res == .) {
                        customMessage = trigger.getBuildSuccessfulMessage();
                    } else if (res == . || res == .) {
                        customMessage = trigger.getBuildFailureMessage();
                    } else if (res == .) {
                        customMessage = trigger.getBuildUnstableMessage();
                    } else if (res == .) {
                        customMessage = trigger.getBuildNotBuiltMessage();
                    } else {
                        customMessage = trigger.getBuildFailureMessage();
                    }
                    // If the user has specified a message, use it
                    // otherwise use a generic indicator
                    if (customMessage == null || customMessage.equals("")) {
                        str.append(res.toString());
                    } else {
                        str.append(customMessage);
                    }
                    if (res.isWorseThan(.)) {
                        unsuccessfulMessage = entry.getUnsuccessfulMessage();
                        if (null != unsuccessfulMessage && !unsuccessfulMessage.isEmpty()) {
                            .trace("Using unsuccessful message from file.");
                            str.append(" <<<\n");
                            str.append(unsuccessfulMessage.trim());
                            str.append("\n>>>");
                        }
                    }
                }
            }
        } else {
            .error("I got a request to create build statistics, but no entries where found!");
        }
        return str.toString();
    }
New to GrepCode? Check out our FAQ X