1 package org.apache.maven.scm.provider.integrity.command.tag;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.mks.api.response.APIException;
23 import com.mks.api.response.Response;
24 import com.mks.api.response.WorkItem;
25 import groovy.lang.Binding;
26 import groovy.lang.GroovyShell;
27 import org.apache.maven.scm.ScmException;
28 import org.apache.maven.scm.ScmFileSet;
29 import org.apache.maven.scm.ScmTagParameters;
30 import org.apache.maven.scm.command.tag.AbstractTagCommand;
31 import org.apache.maven.scm.command.tag.TagScmResult;
32 import org.apache.maven.scm.provider.ScmProviderRepository;
33 import org.apache.maven.scm.provider.integrity.ExceptionHandler;
34 import org.apache.maven.scm.provider.integrity.Project;
35 import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
36 import org.codehaus.groovy.control.CompilationFailedException;
37 import org.codehaus.groovy.control.CompilerConfiguration;
38
39
40
41
42
43
44
45
46
47 public class IntegrityTagCommand
48 extends AbstractTagCommand
49 {
50
51
52
53 @Override
54 public TagScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
55 ScmTagParameters scmTagParameters )
56 throws ScmException
57 {
58 getLogger().info(
59 "Attempting to checkpoint project associated with sandbox " + fileSet.getBasedir().getAbsolutePath() );
60 TagScmResult result;
61 String message = scmTagParameters.getMessage();
62 IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
63
64 try
65 {
66
67 String chkptLabel = evalGroovyExpression( tagName );
68 Project.validateTag( chkptLabel );
69 String msg = ( ( null == message || message.length() == 0 ) ? System.getProperty( "message" ) : message );
70
71 Project siProject = iRepo.getProject();
72
73 if ( !siProject.isBuild() )
74 {
75 Response res = siProject.checkpoint( msg, chkptLabel );
76 int exitCode = res.getExitCode();
77 boolean success = ( exitCode == 0 ? true : false );
78 WorkItem wi = res.getWorkItem( siProject.getConfigurationPath() );
79 String chkpt = wi.getResult().getField( "resultant" ).getItem().getId();
80 getLogger().info(
81 "Successfully checkpointed project " + siProject.getConfigurationPath() + " with label '"
82 + chkptLabel + "', new revision is " + chkpt );
83 result =
84 new TagScmResult( res.getCommandString(), wi.getResult().getMessage(), "Exit Code: " + exitCode,
85 success );
86 }
87 else
88 {
89 getLogger().error(
90 "Cannot checkpoint a build project configuration: " + siProject.getConfigurationPath() + "!" );
91 result =
92 new TagScmResult( "si checkpoint", "Cannot checkpoint a build project configuration!", "", false );
93 }
94 }
95 catch ( CompilationFailedException cfe )
96 {
97 getLogger().error( "Groovy Compilation Exception: " + cfe.getMessage() );
98 result = new TagScmResult( "si checkpoint", cfe.getMessage(), "", false );
99 }
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 }