View Javadoc

1   package org.apache.continuum.builder.distributed.util;
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.continuum.builder.utils.ContinuumBuildConstant;
23  import org.apache.continuum.dao.BuildResultDao;
24  import org.apache.continuum.dao.ProjectDao;
25  import org.apache.maven.continuum.ContinuumException;
26  import org.apache.maven.continuum.model.project.BuildResult;
27  import org.apache.maven.continuum.model.project.Project;
28  import org.apache.maven.continuum.model.project.ProjectDependency;
29  import org.apache.maven.continuum.model.scm.ChangeFile;
30  import org.apache.maven.continuum.model.scm.ChangeSet;
31  import org.apache.maven.continuum.model.scm.ScmResult;
32  import org.apache.maven.continuum.store.ContinuumStoreException;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * @plexus.component role="org.apache.continuum.builder.distributed.util.DistributedBuildUtil"
42   */
43  public class DistributedBuildUtil
44  {
45      private Logger log = LoggerFactory.getLogger( DistributedBuildUtil.class );
46  
47      /**
48       * @plexus.requirement
49       */
50      private ProjectDao projectDao;
51  
52      /**
53       * @plexus.requirement
54       */
55      private BuildResultDao buildResultDao;
56  
57      public BuildResult convertMapToBuildResult( Map<String, Object> context )
58      {
59          BuildResult buildResult = new BuildResult();
60  
61          buildResult.setStartTime( ContinuumBuildConstant.getStartTime( context ) );
62          buildResult.setEndTime( ContinuumBuildConstant.getEndTime( context ) );
63          buildResult.setError( ContinuumBuildConstant.getBuildError( context ) );
64          buildResult.setExitCode( ContinuumBuildConstant.getBuildExitCode( context ) );
65          buildResult.setState( ContinuumBuildConstant.getBuildState( context ) );
66          buildResult.setTrigger( ContinuumBuildConstant.getTrigger( context ) );
67          buildResult.setUsername( ContinuumBuildConstant.getUsername( context ) );
68          buildResult.setBuildUrl( ContinuumBuildConstant.getBuildAgentUrl( context ) );
69  
70          return buildResult;
71      }
72  
73      public List<ProjectDependency> getModifiedDependencies( BuildResult oldBuildResult, Map<String, Object> context )
74          throws ContinuumException
75      {
76          if ( oldBuildResult == null )
77          {
78              return null;
79          }
80  
81          try
82          {
83              Project project = projectDao.getProjectWithAllDetails( ContinuumBuildConstant.getProjectId( context ) );
84              List<ProjectDependency> dependencies = project.getDependencies();
85  
86              if ( dependencies == null )
87              {
88                  dependencies = new ArrayList<ProjectDependency>();
89              }
90  
91              if ( project.getParent() != null )
92              {
93                  dependencies.add( project.getParent() );
94              }
95  
96              if ( dependencies.isEmpty() )
97              {
98                  return null;
99              }
100 
101             List<ProjectDependency> modifiedDependencies = new ArrayList<ProjectDependency>();
102 
103             for ( ProjectDependency dep : dependencies )
104             {
105                 Project dependencyProject = projectDao.getProject( dep.getGroupId(), dep.getArtifactId(),
106                                                                    dep.getVersion() );
107 
108                 if ( dependencyProject != null )
109                 {
110                     long nbBuild = buildResultDao.getNbBuildResultsInSuccessForProject( dependencyProject.getId(),
111                                                                                         oldBuildResult.getEndTime() );
112                     if ( nbBuild > 0 )
113                     {
114                         log.debug( "Dependency changed: " + dep.getGroupId() + ":" + dep.getArtifactId() + ":" +
115                                        dep.getVersion() );
116                         modifiedDependencies.add( dep );
117                     }
118                     else
119                     {
120                         log.debug( "Dependency not changed: " + dep.getGroupId() + ":" + dep.getArtifactId() + ":" +
121                                        dep.getVersion() );
122                     }
123                 }
124                 else
125                 {
126                     log.debug( "Skip non Continuum project: " + dep.getGroupId() + ":" + dep.getArtifactId() + ":" +
127                                    dep.getVersion() );
128                 }
129             }
130 
131             return modifiedDependencies;
132         }
133         catch ( ContinuumStoreException e )
134         {
135             log.warn( "Can't get the project dependencies", e );
136         }
137 
138         return null;
139     }
140 
141     public ScmResult getScmResult( Map<String, Object> context )
142     {
143         Map<String, Object> map = ContinuumBuildConstant.getScmResult( context );
144 
145         if ( !map.isEmpty() )
146         {
147             ScmResult scmResult = new ScmResult();
148             scmResult.setCommandLine( ContinuumBuildConstant.getScmCommandLine( map ) );
149             scmResult.setCommandOutput( ContinuumBuildConstant.getScmCommandOutput( map ) );
150             scmResult.setException( ContinuumBuildConstant.getScmException( map ) );
151             scmResult.setProviderMessage( ContinuumBuildConstant.getScmProviderMessage( map ) );
152             scmResult.setSuccess( ContinuumBuildConstant.isScmSuccess( map ) );
153             scmResult.setChanges( getScmChanges( map ) );
154 
155             return scmResult;
156         }
157 
158         return null;
159     }
160 
161     public List<ChangeSet> getScmChanges( Map<String, Object> context )
162     {
163         List<ChangeSet> changes = new ArrayList<ChangeSet>();
164         List<Map<String, Object>> scmChanges = ContinuumBuildConstant.getScmChanges( context );
165 
166         if ( scmChanges != null )
167         {
168             for ( Map<String, Object> map : scmChanges )
169             {
170                 ChangeSet changeSet = new ChangeSet();
171                 changeSet.setAuthor( ContinuumBuildConstant.getChangeSetAuthor( map ) );
172                 changeSet.setComment( ContinuumBuildConstant.getChangeSetComment( map ) );
173                 changeSet.setDate( ContinuumBuildConstant.getChangeSetDate( map ) );
174                 setChangeFiles( changeSet, map );
175                 changes.add( changeSet );
176             }
177         }
178 
179         return changes;
180     }
181 
182     private void setChangeFiles( ChangeSet changeSet, Map<String, Object> context )
183     {
184         List<Map<String, Object>> changeFiles = ContinuumBuildConstant.getChangeSetFiles( context );
185 
186         if ( changeFiles != null )
187         {
188             for ( Map<String, Object> map : changeFiles )
189             {
190                 ChangeFile changeFile = new ChangeFile();
191                 changeFile.setName( ContinuumBuildConstant.getChangeFileName( map ) );
192                 changeFile.setRevision( ContinuumBuildConstant.getChangeFileRevision( map ) );
193                 changeFile.setStatus( ContinuumBuildConstant.getChangeFileStatus( map ) );
194 
195                 changeSet.addFile( changeFile );
196             }
197         }
198     }
199 }