001package org.apache.maven.scm.provider.integrity.command.branch;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import com.mks.api.response.APIException;
023import com.mks.api.response.Response;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFile;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.branch.AbstractBranchCommand;
029import org.apache.maven.scm.command.branch.BranchScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.integrity.ExceptionHandler;
032import org.apache.maven.scm.provider.integrity.Project;
033import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
034
035import java.util.ArrayList;
036
037/**
038 * MKS Integrity implementation for Maven's AbstractBranchCommand
039 * <br>For a normal and variant configuration, a fresh checkpoint is executed
040 * prior to creating a Development Path (branch).  In the case of a build
041 * configuration, the specified checkpoint revision is used to create
042 * the Development Path
043 *
044 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
045 * @since 1.6
046 */
047public class IntegrityBranchCommand
048    extends AbstractBranchCommand
049{
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public BranchScmResult executeBranchCommand( ScmProviderRepository repository, ScmFileSet fileSet,
055                                                 String branchName, String message )
056        throws ScmException
057    {
058        BranchScmResult result;
059        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
060        Project siProject = iRepo.getProject();
061        getLogger().info(
062            "Attempting to branch project " + siProject.getProjectName() + " using branch name '" + branchName + "'" );
063        try
064        {
065            Project.validateTag( branchName );
066            Response res = siProject.createDevPath( branchName );
067            int exitCode = res.getExitCode();
068            boolean success = ( exitCode == 0 ? true : false );
069            ScmResult scmResult = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
070            result = new BranchScmResult( new ArrayList<ScmFile>(), scmResult );
071        }
072        catch ( APIException aex )
073        {
074            ExceptionHandler eh = new ExceptionHandler( aex );
075            getLogger().error( "MKS API Exception: " + eh.getMessage() );
076            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
077            result = new BranchScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
078        }
079        catch ( Exception e )
080        {
081            getLogger().error( "Failed to checkpoint project! " + e.getMessage() );
082            result = new BranchScmResult( "si createdevpath", e.getMessage(), "", false );
083        }
084
085        return result;
086    }
087
088}