1 package org.apache.maven.plugin.changes;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.logging.Log;
28 import org.apache.maven.plugins.changes.model.Action;
29 import org.apache.maven.plugins.changes.model.Release;
30
31
32
33
34
35
36
37
38 public class ReleaseUtils
39 {
40 private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
41
42 private Log log;
43
44 public ReleaseUtils( Log log )
45 {
46 this.log = log;
47 }
48
49
50
51
52
53
54
55
56
57
58 public Release getLatestRelease( List<Release> releases, String pomVersion )
59 throws MojoExecutionException
60 {
61
62 if ( pomVersion != null && pomVersion.endsWith( SNAPSHOT_SUFFIX ) )
63 {
64 pomVersion = pomVersion.substring( 0, pomVersion.length() - SNAPSHOT_SUFFIX.length() );
65 }
66 getLog().debug( "Found " + releases.size() + " releases." );
67
68 Release release = getRelease( releases, pomVersion );
69
70 if ( release == null )
71 {
72 throw new MojoExecutionException( "Couldn't find the release '" + pomVersion
73 + "' among the supplied releases: " + toString( releases ) );
74 }
75
76 return release;
77 }
78
79 private Log getLog()
80 {
81 return log;
82 }
83
84
85
86
87
88
89
90
91 protected Release getRelease( List<Release> releases, String version )
92 {
93 for ( Release release : releases )
94 {
95 if ( getLog().isDebugEnabled() )
96 {
97 getLog().debug( "The release: " + release.getVersion()
98 + " has " + release.getActions().size() + " actions." );
99 }
100
101 if ( release.getVersion() != null && release.getVersion().equals( version ) )
102 {
103 if ( getLog().isDebugEnabled() )
104 {
105 getLog().debug( "Found the correct release: " + release.getVersion() );
106 logRelease( release );
107 }
108 return release;
109 }
110 }
111 return null;
112 }
113
114 protected void logRelease( Release release )
115 {
116 Action action;
117 for ( Action action1 : release.getActions() )
118 {
119 action = action1;
120 getLog().debug( "o " + action.getType() );
121 getLog().debug( "issue : " + action.getIssue() );
122 getLog().debug( "action : " + action.getAction() );
123 getLog().debug( "dueTo : " + action.getDueTo() );
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136 public List<Release> mergeReleases( final List<Release> firstReleases, final List<Release> secondReleases )
137 {
138 if ( firstReleases == null && secondReleases == null )
139 {
140 return Collections.emptyList();
141 }
142 if ( firstReleases == null )
143 {
144 return secondReleases;
145 }
146 if ( secondReleases == null )
147 {
148 return firstReleases;
149 }
150
151 List<Release> mergedReleases = new ArrayList<Release>();
152
153
154
155
156 for ( Release firstRelease : firstReleases )
157 {
158 Release secondRelease = getRelease( secondReleases, firstRelease.getVersion() );
159 if ( secondRelease != null )
160 {
161 if ( secondRelease.getActions() != null )
162 {
163 firstRelease.getActions().addAll( secondRelease.getActions() );
164 }
165 }
166 mergedReleases.add( firstRelease );
167 }
168
169
170 for ( Release secondRelease : secondReleases )
171 {
172 Release mergedRelease = getRelease( mergedReleases, secondRelease.getVersion() );
173 if ( mergedRelease == null )
174 {
175 mergedReleases.add( secondRelease );
176 }
177 }
178 return mergedReleases;
179 }
180
181
182
183
184
185
186
187
188
189 public List<Release> convertReleaseList( List changesReleases )
190 {
191 List<Release> releases = new ArrayList<Release>();
192
193
194
195 for ( Object changesRelease : changesReleases )
196 {
197 Release release = (Release) changesRelease;
198 releases.add( release );
199 }
200 return releases;
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214 public List mergeReleases( final List releases, final String componentName, final List componentReleases )
215 {
216 if ( releases == null && componentReleases == null )
217 {
218 return Collections.EMPTY_LIST;
219 }
220 if ( componentReleases == null )
221 {
222 return releases;
223 }
224
225 final List mergedReleases = new ArrayList();
226
227 if ( releases != null )
228 {
229 for ( Object release1 : releases )
230 {
231 final Release release = (Release) release1;
232 final Release componentRelease = getRelease( componentReleases, release.getVersion() );
233 if ( componentRelease != null )
234 {
235 release.addComponent( componentName, componentRelease );
236 }
237 mergedReleases.add( release );
238 }
239 }
240
241 for ( Object componentRelease1 : componentReleases )
242 {
243 final Release release = (Release) componentRelease1;
244 final Release mergedRelease = getRelease( mergedReleases, release.getVersion() );
245 if ( mergedRelease == null )
246 {
247 final Release componentRelease = new Release();
248 componentRelease.setVersion( release.getVersion() );
249 componentRelease.setDateRelease( release.getDateRelease() );
250 componentRelease.addComponent( componentName, release );
251 mergedReleases.add( componentRelease );
252 }
253 }
254
255 return mergedReleases;
256 }
257
258 private static String toString( Release release )
259 {
260 return release.getClass().getSimpleName()
261 + "[version='" + release.getVersion() + "'"
262 + ", date='" + release.getDateRelease() + "'"
263 + ", description='" + release.getDescription() + "'"
264 + ", actionsSize=" + release.getActions().size()
265 + "]";
266 }
267
268 public static String toString( List<Release> releases )
269 {
270 List<String> releaseStrings = new ArrayList<String>( releases.size() );
271 for ( Release release : releases )
272 {
273 releaseStrings.add( toString( release ) );
274 }
275 return releaseStrings.toString();
276 }
277 }