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
48 public class IntegrityTagCommand
49 extends AbstractTagCommand
50 {
51
52
53
54 @Override
55 public TagScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
56 ScmTagParameters scmTagParameters )
57 throws ScmException
58 {
59 getLogger().info(
60 "Attempting to checkpoint project associated with sandbox " + fileSet.getBasedir().getAbsolutePath() );
61 TagScmResult result;
62 String message = scmTagParameters.getMessage();
63 IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
64
65 try
66 {
67
68 String chkptLabel = evalGroovyExpression( tagName );
69 Project.validateTag( chkptLabel );
70 String msg = ( ( null == message || message.length() == 0 ) ? System.getProperty( "message" ) : message );
71
72 Project siProject = iRepo.getProject();
73
74 if ( !siProject.isBuild() )
75 {
76 Response res = siProject.checkpoint( msg, chkptLabel );
77 int exitCode = res.getExitCode();
78 boolean success = ( exitCode == 0 ? true : false );
79 WorkItem wi = res.getWorkItem( siProject.getConfigurationPath() );
80 String chkpt = wi.getResult().getField( "resultant" ).getItem().getId();
81 getLogger().info(
82 "Successfully checkpointed project " + siProject.getConfigurationPath() + " with label '"
83 + chkptLabel + "', new revision is " + chkpt );
84 result =
85 new TagScmResult( res.getCommandString(), wi.getResult().getMessage(), "Exit Code: " + exitCode,
86 success );
87 }
88 else
89 {
90 getLogger().error(
91 "Cannot checkpoint a build project configuration: " + siProject.getConfigurationPath() + "!" );
92 result =
93 new TagScmResult( "si checkpoint", "Cannot checkpoint a build project configuration!", "", false );
94 }
95 }
96 catch ( CompilationFailedException cfe )
97 {
98 getLogger().error( "Groovy Compilation Exception: " + cfe.getMessage() );
99 result = new TagScmResult( "si checkpoint", cfe.getMessage(), "", false );
100 }
101 catch ( APIException aex )
102 {
103 ExceptionHandler eh = new ExceptionHandler( aex );
104 getLogger().error( "MKS API Exception: " + eh.getMessage() );
105 getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
106 result = new TagScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
107 }
108 catch ( Exception e )
109 {
110 getLogger().error( "Failed to checkpoint project! " + e.getMessage() );
111 result = new TagScmResult( "si checkpoint", e.getMessage(), "", false );
112 }
113 return result;
114 }
115
116 public String evalGroovyExpression( String expression )
117 {
118 Binding binding = new Binding();
119 binding.setVariable( "env", System.getenv() );
120 binding.setVariable( "sys", System.getProperties() );
121 CompilerConfiguration config = new CompilerConfiguration();
122 GroovyShell shell = new GroovyShell( binding, config );
123 Object result = shell.evaluate( "return \"" + expression + "\"" );
124 if ( result == null )
125 {
126 return "";
127 }
128 else
129 {
130 return result.toString().trim();
131 }
132 }
133 }