View Javadoc

1   package org.apache.maven.continuum.web.action;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.continuum.builder.distributed.manager.DistributedBuildManager;
24  import org.apache.continuum.builder.utils.ContinuumBuildConstant;
25  import org.apache.continuum.buildmanager.BuildManagerException;
26  import org.apache.continuum.web.util.AuditLog;
27  import org.apache.continuum.web.util.AuditLogConstants;
28  import org.apache.maven.continuum.ContinuumException;
29  import org.apache.maven.continuum.configuration.ConfigurationException;
30  import org.apache.maven.continuum.configuration.ConfigurationService;
31  import org.apache.maven.continuum.model.project.BuildResult;
32  import org.apache.maven.continuum.model.project.Project;
33  import org.apache.maven.continuum.model.scm.ChangeSet;
34  import org.apache.maven.continuum.project.ContinuumProjectState;
35  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
36  import org.apache.maven.continuum.web.util.StateGenerator;
37  import org.apache.struts2.ServletActionContext;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.io.InputStream;
44  import java.util.List;
45  import java.util.Map;
46  
47  
48  /**
49   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
50   * @version $Id: BuildResultAction.java 1372260 2012-08-13 04:29:09Z brett $
51   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="buildResult"
52   */
53  public class BuildResultAction
54      extends AbstractBuildAction
55  {
56      /**
57       * @plexus.requirement
58       */
59      private DistributedBuildManager distributedBuildManager;
60  
61      private Project project;
62  
63      private BuildResult buildResult;
64  
65      private int buildId;
66  
67      private List<ChangeSet> changeSet;
68  
69      private boolean hasSurefireResults;
70  
71      private String buildOutput;
72  
73      private String state;
74  
75      private String projectGroupName = "";
76  
77      private int projectGroupId;
78  
79      public String execute()
80          throws ContinuumException, ConfigurationException, IOException, BuildManagerException
81      {
82          try
83          {
84              checkViewProjectGroupAuthorization( getProjectGroupName() );
85          }
86          catch ( AuthorizationRequiredException e )
87          {
88              return REQUIRES_AUTHORIZATION;
89          }
90  
91          //todo get this working for other types of test case rendering other then just surefire
92          // check if there are surefire results to display
93          project = getContinuum().getProject( getProjectId() );
94  
95          ConfigurationService configuration = getContinuum().getConfiguration();
96  
97          // view build result of the current build from the distributed build agent
98          if ( configuration.isDistributedBuildEnabled() &&
99              project.getState() == ContinuumProjectState.BUILDING && getBuildId() == 0 )
100         {
101             // if the project is currently building in distributed build agent, the build result will be stored in the database after the build is finished. 
102             // it's safe to assume that the build result will be null at this point
103             Map<String, Object> map = distributedBuildManager.getBuildResult( project.getId() );
104 
105             if ( map == null )
106             {
107                 projectGroupId = project.getProjectGroup().getId();
108 
109                 return ERROR;
110             }
111 
112             if ( map.size() > 0 )
113             {
114                 buildResult = ContinuumBuildConstant.getBuildResult( map, null );
115 
116                 buildOutput = ContinuumBuildConstant.getBuildOutput( map );
117 
118                 if ( ServletActionContext.getRequest() != null )
119                 {
120                     state = StateGenerator.generate( buildResult.getState(),
121                                                      ServletActionContext.getRequest().getContextPath() );
122                 }
123             }
124             changeSet = null;
125 
126             hasSurefireResults = false;
127 
128             this.setCanDelete( false );
129         }
130         else
131         {
132             buildResult = getContinuum().getBuildResult( getBuildId() );
133 
134             // directory contains files ?
135             File surefireReportsDirectory = configuration.getTestReportsDirectory( buildId, getProjectId() );
136             File[] files = surefireReportsDirectory.listFiles();
137             hasSurefireResults = files != null && files.length > 0;
138             changeSet = getContinuum().getChangesSinceLastSuccess( getProjectId(), getBuildId() );
139 
140             buildOutput = getBuildOutputText();
141 
142             if ( ServletActionContext.getRequest() != null )
143             {
144                 state = StateGenerator.generate( buildResult.getState(),
145                                                  ServletActionContext.getRequest().getContextPath() );
146             }
147 
148             this.setCanDelete( this.canRemoveBuildResult( buildResult ) );
149         }
150 
151         return SUCCESS;
152     }
153 
154     public String remove()
155         throws ContinuumException
156     {
157         try
158         {
159             checkModifyProjectGroupAuthorization( getProjectGroupName() );
160         }
161         catch ( AuthorizationRequiredException e )
162         {
163             return REQUIRES_AUTHORIZATION;
164         }
165         if ( this.isConfirmed() )
166         {
167             try
168             {
169                 if ( canRemoveBuildResult( getContinuum().getBuildResult( buildId ) ) )
170                 {
171                     getContinuum().removeBuildResult( buildId );
172                 }
173                 else
174                 {
175                     addActionError( getText( "buildResult.cannot.delete" ) );
176                 }
177             }
178             catch ( ContinuumException e )
179             {
180                 addActionError( getText( "buildResult.delete.error", "Unable to delete build result", new Integer(
181                     buildId ).toString() ) );
182             }
183             catch ( BuildManagerException e )
184             {
185                 throw new ContinuumException( e.getMessage(), e );
186             }
187 
188             AuditLog event = new AuditLog( "Build Result id=" + buildId, AuditLogConstants.REMOVE_BUILD_RESULT );
189             event.setCategory( AuditLogConstants.BUILD_RESULT );
190             event.setCurrentUser( getPrincipal() );
191             event.log();
192 
193             return SUCCESS;
194         }
195 
196         return CONFIRM;
197     }
198 
199 
200     public String buildLogAsText()
201         throws ConfigurationException, IOException
202     {
203         buildOutput = getBuildOutputText();
204         return SUCCESS;
205     }
206 
207     public InputStream getBuildOutputInputStream()
208         throws ConfigurationException, IOException
209     {
210         String outputText = getBuildOutputText();
211         return outputText == null ? null : IOUtils.toInputStream( outputText );
212     }
213 
214     private String getBuildOutputText()
215         throws ConfigurationException, IOException
216     {
217         ConfigurationService configuration = getContinuum().getConfiguration();
218         File buildOutputFile = configuration.getBuildOutputFile( getBuildId(), getProjectId() );
219 
220         if ( buildOutputFile.exists() )
221         {
222             return FileUtils.fileRead( buildOutputFile );
223         }
224         return null;
225     }
226 
227 
228     public int getBuildId()
229     {
230         return buildId;
231     }
232 
233     public void setBuildId( int buildId )
234     {
235         this.buildId = buildId;
236     }
237 
238     public Project getProject()
239     {
240         return project;
241     }
242 
243     public BuildResult getBuildResult()
244     {
245         return buildResult;
246     }
247 
248     public List<ChangeSet> getChangesSinceLastSuccess()
249     {
250         return changeSet;
251     }
252 
253     public boolean isHasSurefireResults()
254     {
255         return hasSurefireResults;
256     }
257 
258     public void setHasSurefireResults( boolean hasSurefireResults )
259     {
260         this.hasSurefireResults = hasSurefireResults;
261     }
262 
263     public String getBuildOutput()
264     {
265         return buildOutput;
266     }
267 
268     public String getState()
269     {
270         return state;
271     }
272 
273     public String getProjectGroupName()
274         throws ContinuumException
275     {
276         if ( StringUtils.isEmpty( projectGroupName ) )
277         {
278             projectGroupName = getContinuum().getProjectGroupByProjectId( getProjectId() ).getName();
279         }
280 
281         return projectGroupName;
282     }
283 
284     public int getProjectGroupId()
285     {
286         return projectGroupId;
287     }
288 
289     // for testing
290     public void setDistributedBuildManager( DistributedBuildManager distributedBuildManager )
291     {
292         this.distributedBuildManager = distributedBuildManager;
293     }
294 }