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