001package org.apache.maven.scm.provider.integrity.command.tag; 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 com.mks.api.response.WorkItem; 025import groovy.lang.Binding; 026import groovy.lang.GroovyShell; 027import org.apache.maven.scm.ScmException; 028import org.apache.maven.scm.ScmFileSet; 029import org.apache.maven.scm.ScmTagParameters; 030import org.apache.maven.scm.command.tag.AbstractTagCommand; 031import org.apache.maven.scm.command.tag.TagScmResult; 032import org.apache.maven.scm.provider.ScmProviderRepository; 033import org.apache.maven.scm.provider.integrity.ExceptionHandler; 034import org.apache.maven.scm.provider.integrity.Project; 035import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository; 036import org.codehaus.groovy.control.CompilationFailedException; 037import org.codehaus.groovy.control.CompilerConfiguration; 038 039/** 040 * MKS Integrity implementation of Maven's AbstractTagCommand 041 * <br>This command will execute a 'si checkpoint' command using a groovy 042 * script for evaluating the tag (label) name 043 * 044 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a> 045 * @since 1.6 046 */ 047public class IntegrityTagCommand 048 extends AbstractTagCommand 049{ 050 /** 051 * {@inheritDoc} 052 */ 053 @Override 054 public TagScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName, 055 ScmTagParameters scmTagParameters ) 056 throws ScmException 057 { 058 getLogger().info( 059 "Attempting to checkpoint project associated with sandbox " + fileSet.getBasedir().getAbsolutePath() ); 060 TagScmResult result; 061 String message = scmTagParameters.getMessage(); 062 IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository; 063 064 try 065 { 066 // First validate the checkpoint label string by evaluating the groovy script 067 String chkptLabel = evalGroovyExpression( tagName ); 068 Project.validateTag( chkptLabel ); 069 String msg = ( ( null == message || message.length() == 0 ) ? System.getProperty( "message" ) : message ); 070 // Get information about the Project 071 Project siProject = iRepo.getProject(); 072 // Ensure this is not a build project configuration 073 if ( !siProject.isBuild() ) 074 { 075 Response res = siProject.checkpoint( msg, chkptLabel ); 076 int exitCode = res.getExitCode(); 077 boolean success = ( exitCode == 0 ? true : false ); 078 WorkItem wi = res.getWorkItem( siProject.getConfigurationPath() ); 079 String chkpt = wi.getResult().getField( "resultant" ).getItem().getId(); 080 getLogger().info( 081 "Successfully checkpointed project " + siProject.getConfigurationPath() + " with label '" 082 + chkptLabel + "', new revision is " + chkpt ); 083 result = 084 new TagScmResult( res.getCommandString(), wi.getResult().getMessage(), "Exit Code: " + exitCode, 085 success ); 086 } 087 else 088 { 089 getLogger().error( 090 "Cannot checkpoint a build project configuration: " + siProject.getConfigurationPath() + "!" ); 091 result = 092 new TagScmResult( "si checkpoint", "Cannot checkpoint a build project configuration!", "", false ); 093 } 094 } 095 catch ( CompilationFailedException cfe ) 096 { 097 getLogger().error( "Groovy Compilation Exception: " + cfe.getMessage() ); 098 result = new TagScmResult( "si checkpoint", cfe.getMessage(), "", false ); 099 } 100 catch ( APIException aex ) 101 { 102 ExceptionHandler eh = new ExceptionHandler( aex ); 103 getLogger().error( "MKS API Exception: " + eh.getMessage() ); 104 getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() ); 105 result = new TagScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false ); 106 } 107 catch ( Exception e ) 108 { 109 getLogger().error( "Failed to checkpoint project! " + e.getMessage() ); 110 result = new TagScmResult( "si checkpoint", e.getMessage(), "", false ); 111 } 112 return result; 113 } 114 115 public String evalGroovyExpression( String expression ) 116 { 117 Binding binding = new Binding(); 118 binding.setVariable( "env", System.getenv() ); 119 binding.setVariable( "sys", System.getProperties() ); 120 CompilerConfiguration config = new CompilerConfiguration(); 121 GroovyShell shell = new GroovyShell( binding, config ); 122 Object result = shell.evaluate( "return \"" + expression + "\"" ); 123 if ( result == null ) 124 { 125 return ""; 126 } 127 else 128 { 129 return result.toString().trim(); 130 } 131 } 132}