View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.changes.issues;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  
26  /**
27   * A utility class for working with issue objects.
28   *
29   * @author Dennis Lundberg
30   * @version $Id$
31   * @since 2.4
32   */
33  public class IssueUtils {
34      public static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
35  
36      /**
37       * Find the issues that has a Fix Version that matches the supplied prefix.
38       *
39       * @param issues A list of issues
40       * @param prefix The prefix of the "Fix Version" that should match
41       * @return A <code>List</code> of issues fixed in versions that match the supplied prefix
42       * @throws org.apache.maven.plugin.MojoExecutionException If no issues could be found for the supplied prefix
43       */
44      public static List<Issue> filterIssuesWithVersionPrefix(List<Issue> issues, String prefix)
45              throws MojoExecutionException {
46          List<Issue> filteredIssues = new ArrayList<>();
47          boolean isFound = false;
48          Issue issue;
49  
50          for (Issue issue1 : issues) {
51              issue = issue1;
52  
53              if (issue.getFixVersions() != null) {
54                  for (String fixVersion : issue.getFixVersions()) {
55                      if (prefix == null || fixVersion.startsWith(prefix)) {
56                          isFound = true;
57                          filteredIssues.add(issue);
58                          break;
59                      }
60                  }
61              }
62          }
63  
64          if (!isFound) {
65              throw new MojoExecutionException("Couldn't find any issues with a Fix Version prefix of '" + prefix
66                      + "' among the supplied issues: " + toString(issues));
67          }
68          return filteredIssues;
69      }
70  
71      /**
72       * Find the issues for the supplied version, by matching the "Fix for" version in the supplied list of issues
73       * with the supplied version. If the supplied version is a SNAPSHOT, then that part of the version will be removed
74       * prior to the matching.
75       *
76       * @param issues a list of issues
77       * @param version the version that issues should be returned for
78       * @return a <code>List</code> of issues for the supplied version, possibly empty if there are no issues
79       */
80      public static List<Issue> getIssuesForVersion(List<Issue> issues, String version) {
81          List<Issue> issuesForVersion = new ArrayList<>();
82          String releaseVersion = version;
83  
84          // Remove "-SNAPSHOT" from the end of the version, if it's there
85          if (version != null && version.endsWith(SNAPSHOT_SUFFIX)) {
86              releaseVersion = version.substring(0, version.length() - SNAPSHOT_SUFFIX.length());
87          }
88  
89          for (Issue issue : issues) {
90              if (issue.getFixVersions() != null && issue.getFixVersions().contains(releaseVersion)) {
91                  issuesForVersion.add(issue);
92              }
93          }
94  
95          return issuesForVersion;
96      }
97  
98      public static String toString(List<Issue> issues) {
99          List<String> issueStrings = new ArrayList<>(issues.size());
100         for (Issue issue : issues) {
101             issueStrings.add(issue.toString());
102         }
103         return issueStrings.toString();
104     }
105 }