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