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 1579078 2014-03-18 22:44:41Z dennisl $
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
45 * If no issues could be found for the supplied prefix
46 */
47 public static List<Issue> filterIssuesWithVersionPrefix( List<Issue> issues, String prefix )
48 throws MojoExecutionException
49 {
50 List<Issue> filteredIssues = new ArrayList<Issue>();
51 boolean isFound = false;
52 Issue issue;
53
54 for ( Issue issue1 : issues )
55 {
56 issue = issue1;
57
58 if ( issue.getFixVersions() != null )
59 {
60 for ( String fixVersion : issue.getFixVersions() )
61 {
62 if ( prefix == null || fixVersion.startsWith( prefix ) )
63 {
64 isFound = true;
65 filteredIssues.add( issue );
66 break;
67 }
68 }
69 }
70 }
71
72 if ( !isFound )
73 {
74 throw new MojoExecutionException(
75 "Couldn't find any issues with a Fix Version prefix of '" + prefix + "' among the supplied issues: " + toString( issues ) );
76 }
77 return filteredIssues;
78 }
79
80 /**
81 * Find the issues for only the supplied version, by matching the "Fix for"
82 * version in the supplied list of issues with the supplied version.
83 * If the supplied version is a SNAPSHOT, then that part of the version
84 * will be removed prior to the matching.
85 *
86 * @param issues A list of issues
87 * @param version The version that issues should be returned for
88 * @return A <code>List</code> of issues for the supplied version
89 * @throws org.apache.maven.plugin.MojoExecutionException
90 * If no issues could be found for the supplied version
91 */
92 public static List<Issue> getIssuesForVersion( List<Issue> issues, String version )
93 throws MojoExecutionException
94 {
95 List<Issue> issuesForVersion = new ArrayList<Issue>();
96 boolean isFound = false;
97 Issue issue;
98 String releaseVersion = version;
99
100 // Remove "-SNAPSHOT" from the end of the version, if it's there
101 if ( version != null && version.endsWith( SNAPSHOT_SUFFIX ) )
102 {
103 releaseVersion = version.substring( 0, version.length() - SNAPSHOT_SUFFIX.length() );
104 }
105
106 for ( Issue issue1 : issues )
107 {
108 issue = issue1;
109
110 if ( issue.getFixVersions() != null && issue.getFixVersions().contains( releaseVersion ) )
111 {
112 isFound = true;
113 issuesForVersion.add( issue );
114 }
115 }
116
117 if ( !isFound )
118 {
119 throw new MojoExecutionException(
120 "Couldn't find any issues for the version '" + releaseVersion + "' among the supplied issues: " + toString( issues ) );
121 }
122 return issuesForVersion;
123 }
124
125 public static String toString( List<Issue> issues )
126 {
127 List<String> issueStrings = new ArrayList<String>( issues.size() );
128 for ( Issue issue : issues )
129 {
130 issueStrings.add( issue.toString() );
131 }
132 return issueStrings.toString();
133 }
134 }