View Javadoc

1   package org.apache.maven.plugin.changes;
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.logging.Log;
29  import org.apache.maven.plugins.changes.model.Action;
30  import org.apache.maven.plugins.changes.model.Release;
31  
32  /**
33   * A utility class for working with Release objects.
34   *
35   * @author Dennis Lundberg
36   * @version $Id: ReleaseUtils.html 816598 2012-05-08 12:46:49Z hboutemy $
37   * @since 2.4
38   */
39  public class ReleaseUtils
40  {
41      private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
42  
43      private Log log;
44  
45      public ReleaseUtils( Log log )
46      {
47          this.log = log;
48      }
49  
50      /**
51       * Get the latest release by matching the supplied releases
52       * with the version from the pom.
53       *
54       * @param releases list of releases
55       * @param pomVersion Version of the artifact
56       * @return A <code>Release</code> that matches the next release of the current project
57       * @throws org.apache.maven.plugin.MojoExecutionException If a release can't be found
58       */
59      public Release getLatestRelease( List releases, String pomVersion )
60          throws MojoExecutionException
61      {
62          // Remove "-SNAPSHOT" from the end, if it's there
63          if ( pomVersion != null && pomVersion.endsWith( SNAPSHOT_SUFFIX ) )
64          {
65              pomVersion = pomVersion.substring( 0, pomVersion.length() - SNAPSHOT_SUFFIX.length() );
66          }
67          getLog().debug( "Found " + releases.size() + " releases." );
68  
69          Release release = getRelease( releases, pomVersion );
70  
71          if ( release == null )
72          {
73              throw new MojoExecutionException( "Couldn't find the release '" + pomVersion
74                  + "' among the supplied releases." );
75          }
76  
77          return release;
78      }
79  
80      private Log getLog()
81      {
82          return log;
83      }
84  
85      /**
86       * Get a release with the specified version from the list of releases.
87       *
88       * @param releases A list of releases
89       * @param version The version we want
90       * @return A Release, or null if no release with the specified version can be found
91       */
92      protected Release getRelease( List releases, String version )
93      {
94          Release release = null;
95          for ( int i = 0; i < releases.size(); i++ )
96          {
97              release = (Release) releases.get( i );
98              if ( getLog().isDebugEnabled() )
99              {
100                 getLog().debug( "The release: " + release.getVersion()
101                     + " has " + release.getActions().size() + " actions." );
102             }
103 
104             if ( release.getVersion() != null && release.getVersion().equals( version ) )
105             {
106                 if ( getLog().isDebugEnabled() )
107                 {
108                     getLog().debug( "Found the correct release: " + release.getVersion() );
109                     logRelease( release );
110                 }
111                 return release;
112             }
113         }
114         return null;
115     }
116 
117     protected void logRelease( Release release )
118     {
119         Action action;
120         for ( Iterator iterator = release.getActions().iterator(); iterator.hasNext(); )
121         {
122             action = (Action) iterator.next();
123             getLog().debug( "o " + action.getType() );
124             getLog().debug( "issue : " + action.getIssue() );
125             getLog().debug( "action : " + action.getAction() );
126             getLog().debug( "dueTo : " + action.getDueTo() );
127         }
128     }
129 
130     /**
131      * Merge releases from one issue tracker with releases from another issue
132      * tracker. If a release is found in both issue trackers, i.e. they have
133      * the same version, their issues are merged into one release.
134      *
135      * @param firstReleases Releases from the first issue tracker
136      * @param secondReleases Releases from the second issue tracker
137      * @return A list containing the merged releases
138      */
139     public List mergeReleases( final List firstReleases, final List secondReleases )
140     {
141         if ( firstReleases == null && secondReleases == null )
142         {
143             return Collections.EMPTY_LIST;
144         }
145         if ( firstReleases == null )
146         {
147             return secondReleases;
148         }
149         if ( secondReleases == null )
150         {
151             return firstReleases;
152         }
153 
154         List mergedReleases = new ArrayList();
155 
156         // Loop through the releases from the first issue tracker, merging in
157         // actions from releases with the same version from the second issue
158         // tracker
159         for ( Iterator iterator = firstReleases.iterator(); iterator.hasNext(); )
160         {
161             Release firstRelease = (Release) iterator.next();
162             Release secondRelease = getRelease( secondReleases, firstRelease.getVersion() );
163             if ( secondRelease != null )
164             {
165                 if ( secondRelease.getActions() != null )
166                 {
167                     firstRelease.getActions().addAll( secondRelease.getActions() );
168                 }
169             }
170             mergedReleases.add( firstRelease );
171         }
172 
173         // Handle releases that are only in the second issue tracker
174         for ( Iterator iterator = secondReleases.iterator(); iterator.hasNext(); )
175         {
176             Release secondRelease = (Release) iterator.next();
177             Release mergedRelease = getRelease( mergedReleases, secondRelease.getVersion() );
178             if ( mergedRelease == null )
179             {
180                 mergedReleases.add( secondRelease );
181             }
182         }
183         return mergedReleases;
184     }
185 }