View Javadoc
1   package org.apache.maven.plugin.issues;
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.maven.plugin.MojoExecutionException;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * A utility class for working with issue objects.
29   *
30   * @author Dennis Lundberg
31   * @version $Id: IssueUtils.java 1685894 2015-06-16 19:29:09Z khmarbaise $
32   * @since 2.4
33   */
34  public class IssueUtils
35  {
36      public static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
37  
38      /**
39       * Find the issues that has a Fix Version that matches the supplied prefix.
40       *
41       * @param issues A list of issues
42       * @param prefix The prefix of the "Fix Version" that should match
43       * @return A <code>List</code> of issues fixed in versions that match the supplied prefix
44       * @throws org.apache.maven.plugin.MojoExecutionException If no issues could be found for the supplied prefix
45       */
46      public static List<Issue> filterIssuesWithVersionPrefix( List<Issue> issues, String prefix )
47          throws MojoExecutionException
48      {
49          List<Issue> filteredIssues = new ArrayList<Issue>();
50          boolean isFound = false;
51          Issue issue;
52  
53          for ( Issue issue1 : issues )
54          {
55              issue = issue1;
56  
57              if ( issue.getFixVersions() != null )
58              {
59                  for ( String fixVersion : issue.getFixVersions() )
60                  {
61                      if ( prefix == null || fixVersion.startsWith( prefix ) )
62                      {
63                          isFound = true;
64                          filteredIssues.add( issue );
65                          break;
66                      }
67                  }
68              }
69          }
70  
71          if ( !isFound )
72          {
73              throw new MojoExecutionException( "Couldn't find any issues with a Fix Version prefix of '" + prefix
74                  + "' among the supplied issues: " + toString( issues ) );
75          }
76          return filteredIssues;
77      }
78  
79      /**
80       * Find the issues for only the supplied version, by matching the "Fix for" version in the supplied list of issues
81       * with the supplied version. If the supplied version is a SNAPSHOT, then that part of the version will be removed
82       * prior to the matching.
83       *
84       * @param issues A list of issues
85       * @param version The version that issues should be returned for
86       * @return A <code>List</code> of issues for the supplied version
87       * @throws org.apache.maven.plugin.MojoExecutionException If no issues could be found for the supplied version
88       */
89      public static List<Issue> getIssuesForVersion( List<Issue> issues, String version )
90          throws MojoExecutionException
91      {
92          List<Issue> issuesForVersion = new ArrayList<Issue>();
93          boolean isFound = false;
94          Issue issue;
95          String releaseVersion = version;
96  
97          // Remove "-SNAPSHOT" from the end of the version, if it's there
98          if ( version != null && version.endsWith( SNAPSHOT_SUFFIX ) )
99          {
100             releaseVersion = version.substring( 0, version.length() - SNAPSHOT_SUFFIX.length() );
101         }
102 
103         for ( Issue issue1 : issues )
104         {
105             issue = issue1;
106 
107             if ( issue.getFixVersions() != null && issue.getFixVersions().contains( releaseVersion ) )
108             {
109                 isFound = true;
110                 issuesForVersion.add( issue );
111             }
112         }
113 
114         if ( !isFound )
115         {
116             throw new MojoExecutionException( "Couldn't find any issues for the version '" + releaseVersion
117                 + "' among the supplied issues: " + toString( issues ) );
118         }
119         return issuesForVersion;
120     }
121 
122     public static String toString( List<Issue> issues )
123     {
124         List<String> issueStrings = new ArrayList<String>( issues.size() );
125         for ( Issue issue : issues )
126         {
127             issueStrings.add( issue.toString() );
128         }
129         return issueStrings.toString();
130     }
131 }