Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 JFrog Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.jfrog.hudson.release;
 
 import hudson.Util;
 
 import java.util.List;
 import java.util.Map;
This action leads to execution of the release wrapper. It will collect information from the user about the release and will trigger the release build. This action is not saved in the job xml file.

Author(s):
Yossi Shaul
 
 public abstract class ReleaseAction<P extends AbstractProject & BuildableItemWithBuildWrappers,
         W extends BuildWrapperimplements Action {
 
     private static final Logger log = Logger.getLogger(ReleaseAction.class.getName());
 
     protected final transient P project;
     private Class<W> wrapperClass;
 
     protected VERSIONING versioning;

    
The next release version to change the model to if using one global version.
 
     protected String releaseVersion;
    
Next (development) version to change the model to if using one global version.
 
     protected String nextVersion;
 
     Boolean pro;
 
     boolean createVcsTag;
     String tagUrl;
     String tagComment;
 
     boolean createReleaseBranch;
 
     protected transient boolean strategyRequestFailed = false;
     protected transient String strategyRequestErrorMessage = null;
     protected transient boolean strategyPluginExists;
     protected transient Map stagingStrategy;
     protected transient String defaultVersioning;
     protected transient VersionedModule defaultGlobalModule;
     protected transient Map<StringVersionedModuledefaultModules;
     protected transient VcsConfig defaultVcsConfig;
     protected transient PromotionConfig defaultPromotionConfig;
 
     public enum VERSIONING {
         GLOBAL("One version for all modules"),
         PER_MODULE("Version per module"),
         NONE("Use existing module versions");
         // The description to display in the UI
         private final String displayMessage;
 
        VERSIONING(String displayMessage) {
            this. = displayMessage;
        }
        public String getDisplayMessage() {
            return ;
        }
    }
    public ReleaseAction(P projectClass<W> wrapperClass) {
        this. = project;
        this. = wrapperClass;
    }

    
invoked from the jelly file

Throws:
java.lang.Exception Any exception
    @SuppressWarnings("UnusedDeclaration")
    public void init() throws Exception {
        initBuilderSpecific();
        resetFields();
        PluginSettings selectedStagingPluginSettings = getSelectedStagingPlugin();
        if ((selectedStagingPluginSettings != null) &&
                !..equals(selectedStagingPluginSettings.getPluginName())) {
            try {
                 = getArtifactoryServer().getStagingStrategy(selectedStagingPluginSettings,
                        Util.rawEncode(.getName()));
            } catch (Exception e) {
                .log(."Failed to obtain staging strategy: " + e.getMessage(), e);
                 = true;
                 = "Failed to obtain staging strategy '" +
                        selectedStagingPluginSettings.getPluginName() + "': " + e.getMessage() +
                        ".\nPlease review the log for further information.";
                 = null;
            }
             = ( != null) && !.isEmpty();
        }
        prepareDefaultVersioning();
        prepareDefaultModules();
        prepareDefaultVcsSettings();
    }
    private void resetFields() {
         = false;
         = null;
         = false;
         = null;
         = null;
         = null;
         = null;
         = null;
         = null;
    }
    public boolean isStrategyRequestFailed() {
        return ;
    }
        return ;
    }
    public String getTitle() {
        StringBuilder titleBuilder = new StringBuilder("Artifactory Pro Release Staging");
        if ( && StringUtils.isNotBlank(getSelectedStagingPlugin().getPluginName())) {
            titleBuilder.append(" - Using the '").append(getSelectedStagingPlugin().getPluginName())
                    .append("' staging plugin.");
        }
        return titleBuilder.toString();
    }
    public String getIconFileName() {
            return "/plugin/artifactory/images/artifactory-release.png";
        }
        // return null to hide the action (doSubmit will also perform permission check if someone tries direct link)
        return null;
    }

    

Returns:
The message to display on the left panel for the perform release action.
    public String getDisplayName() {
        return "Artifactory Release Staging";
    }
    public String getUrlName() {
        return "release";
    }
    public VERSIONING getVersioning() {
        return ;
    }
    public String getReleaseVersion() {
        return ;
    }
    public String getNextVersion() {
        return ;
    }
    public boolean isCreateVcsTag() {
        return ;
    }
    public String getTagUrl() {
        return ;
    }
    public String getTagComment() {
        return ;
    }
    public String getNextDevelCommitComment() {
        return ;
    }
    public boolean isCreateReleaseBranch() {
        return ;
    }
    public String getReleaseBranch() {
        return ;
    }
    public String getStagingComment() {
        return ;
    }
    public String getStagingRepositoryKey() {
        return ;
    }
    public String latestVersioningSelection() {
        return ..name();
    }
    public boolean isArtifactoryPro() {
        return getArtifactoryServer().isArtifactoryPro();
    }
        return ( != null) ? .getReleaseVersion() : null;
    }
        return ( != null) ? .getNextDevelopmentVersion() : null;
    }
        return .values();
    }
    public boolean isGit() {
        return AbstractScmCoordinator.isGitScm();
    }
    public VcsConfig getDefaultVcsConfig() {
        return ;
    }
        return ;
    }

    

Returns:
List of target repositories for deployment (release repositories first). Called from the UI.
    @SuppressWarnings({"UnusedDeclaration"})
    public abstract List<StringgetRepositoryKeys();
    public abstract ArtifactoryServer getArtifactoryServer();

    
Form submission is calling this method
    @SuppressWarnings({"UnusedDeclaration"})
    public void doSubmit(StaplerRequest reqStaplerResponse respthrows IOExceptionServletException {
        // enforce release permissions
        req.bindParameters(this);
        String versioningStr = req.getParameter("versioning");
         = VERSIONING.valueOf(versioningStr);
        switch () {
            case :
                doGlobalVersioning(req);
                break;
            case :
                doPerModuleVersioning(req);
        }
         = req.getParameter("createVcsTag") != null;
        if () {
             = req.getParameter("tagUrl");
             = req.getParameter("tagComment");
        }
         = req.getParameter("nextDevelCommitComment");
         = req.getParameter("createReleaseBranch") != null;
        if () {
             = req.getParameter("releaseBranch");
        }
         = req.getParameter("repositoryKey");
         = req.getParameter("stagingComment");
        // schedule release build
        if (.scheduleBuild(0, new UserCause(), this)) {
            // redirect to the project page
            resp.sendRedirect(.getAbsoluteUrl());
        }
    }
    public abstract String getReleaseVersionFor(Object moduleName);
    public abstract String getNextVersionFor(Object moduleName);
    protected void initBuilderSpecific() throws Exception {
    }
        return . + "Next development version";
    }
    protected String getBaseTagUrlAccordingToScm(String baseTagUrl) {
        if (AbstractScmCoordinator.isSvn() && !baseTagUrl.endsWith("/")) {
            return baseTagUrl + "/";
        }
        return baseTagUrl;
    }

    
Execute the ReleaseAction.VERSIONING.GLOBAL strategy of the versioning mechanism, which assigns all modules the same version for the release and for the next development version.

Parameters:
req The request that is coming from the form when staging.
    protected void doGlobalVersioning(StaplerRequest req) {
         = req.getParameter("releaseVersion");
         = req.getParameter("nextVersion");
    }
    protected W getWrapper() {
        return ActionableHelper.getBuildWrapper();
    }
    protected String calculateReleaseVersion(String fromVersion) {
        return fromVersion.replace("-SNAPSHOT""");
    }

    
Calculates the next snapshot version based on the current release version

Parameters:
fromVersion The version to bump to next development version
Returns:
The next calculated development (snapshot) version
    protected String calculateNextVersion(String fromVersion) {
        // first turn it to release version
        fromVersion = calculateReleaseVersion(fromVersion);
        String nextVersion;
        int lastDotIndex = fromVersion.lastIndexOf('.');
        try {
            if (lastDotIndex != -1) {
                // probably a major minor version e.g., 2.1.1
                String minorVersionToken = fromVersion.substring(lastDotIndex + 1);
                String nextMinorVersion;
                int lastDashIndex = minorVersionToken.lastIndexOf('-');
                if (lastDashIndex != -1) {
                    // probably a minor-buildNum e.g., 2.1.1-4 (should change to 2.1.1-5)
                    String buildNumber = minorVersionToken.substring(lastDashIndex + 1);
                    int nextBuildNumber = Integer.parseInt(buildNumber) + 1;
                    nextMinorVersion = minorVersionToken.substring(0, lastDashIndex + 1) + nextBuildNumber;
                } else {
                    nextMinorVersion = Integer.parseInt(minorVersionToken) + 1 + "";
                }
                nextVersion = fromVersion.substring(0, lastDotIndex + 1) + nextMinorVersion;
            } else {
                // maybe it's just a major version; try to parse as an int
                int nextMajorVersion = Integer.parseInt(fromVersion) + 1;
                nextVersion = nextMajorVersion + "";
            }
        } catch (NumberFormatException e) {
            return fromVersion;
        }
        return nextVersion + "-SNAPSHOT";
    }

    
Execute the ReleaseAction.VERSIONING.PER_MODULE strategy of the versioning mechanism, which assigns each module its own version for release and for the next development version

Parameters:
req The request that is coming from the form when staging.
    protected abstract void doPerModuleVersioning(StaplerRequest req);
    protected abstract PluginSettings getSelectedStagingPlugin();
    protected abstract void prepareBuilderSpecificDefaultGlobalModule();
    protected abstract void prepareBuilderSpecificDefaultModules();
    protected abstract void prepareBuilderSpecificDefaultVcsConfig();
    protected abstract void prepareBuilderSpecificDefaultPromotionConfig();
    protected void prepareBuilderSpecificDefaultVersioning() {
    }
    private void prepareDefaultVersioning() {
        if () {
            if (.containsKey("defaultModuleVersion")) {
                 = ..name();
            } else if (.containsKey("moduleVersionsMap")) {
                 = ..name();
            }
        }
        if (StringUtils.isBlank()) {
        }
    }
    private void prepareDefaultGlobalModule() {
        if () {
            if (.containsKey("defaultModuleVersion")) {
                Map<StringStringdefaultModuleVersion =
                        (Map<StringString>) .get("defaultModuleVersion");
                 = new VersionedModule(defaultModuleVersion.get("moduleId"),
                        defaultModuleVersion.get("nextRelease"), defaultModuleVersion.get("nextDevelopment"));
            }
        }
        if ( == null) {
        }
    }
    private void prepareDefaultModules() {
        if () {
            if (.containsKey("moduleVersionsMap")) {
                Map<String, ? extends Map<StringString>> moduleVersionsMap =
                        (Map<String, ? extends Map<StringString>>) .get("moduleVersionsMap");
                 = Maps.newHashMap();
                if (!moduleVersionsMap.isEmpty()) {
                    for (Map<StringStringmoduleVersion : moduleVersionsMap.values()) {
                        String moduleId = moduleVersion.get("moduleId");
                        .put(moduleIdnew VersionedModule(moduleIdmoduleVersion.get("nextRelease"),
                                moduleVersion.get("nextDevelopment")));
                    }
                }
            }
        }
        if ( == null) {
            prepareBuilderSpecificDefaultModules();
        }
    }
    private void prepareDefaultVcsSettings() {
        if () {
            if (.containsKey("vcsConfig")) {
                Map<StringObjectvcsConfig = (Map<StringObject>) .get("vcsConfig");
                 = new VcsConfig(((BooleanvcsConfig.get("useReleaseBranch")),
                        getStagingConfigAsString(vcsConfig"releaseBranchName"),
                        ((BooleanvcsConfig.get("createTag")),
                        getStagingConfigAsString(vcsConfig"tagUrlOrName"),
                        getStagingConfigAsString(vcsConfig"tagComment"),
                        getStagingConfigAsString(vcsConfig"nextDevelopmentVersionComment"));
            }
        }
        if ( == null) {
        }
    }
    private void prepareDefaultPromotionConfig() {
        if () {
            if (.containsKey("promotionConfig")) {
                Map<StringStringpromotionConfig = (Map<StringString>) .get("promotionConfig");
                 = new PromotionConfig(promotionConfig.get("targetRepository"),
                        promotionConfig.get("comment"));
            }
        }
        if ( == null) {
        }
    }
    private String getStagingConfigAsString(Map<StringObjectconfigMapString key) {
        if (configMap.containsKey(key)) {
            return configMap.get(key).toString();
        }
        return null;
    }
New to GrepCode? Check out our FAQ X