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 org.apache.maven.plugin.issues.Issue;
23  import org.apache.maven.plugin.issues.IssueManagementSystem;
24  import org.apache.maven.plugins.changes.model.Action;
25  import org.apache.maven.plugins.changes.model.Release;
26  
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * An adapter that can adapt data models from other issue management system to the data models used in the changes.xml
34   * file.
35   *
36   * @author Dennis Lundberg
37   * @version $Id: IssueAdapter.java 1737123 2016-03-30 15:19:18Z michaelo $
38   * @since 2.4
39   */
40  public class IssueAdapter
41  {
42      private static final String UNKNOWN_ISSUE_TYPE = "";
43  
44      private IssueManagementSystem ims;
45  
46      /**
47       * Create a new adapter.
48       *
49       * @param ims The issue management system that has the data that should be adapted
50       */
51      public IssueAdapter( IssueManagementSystem ims )
52      {
53          this.ims = ims;
54      }
55  
56      private Map<String, IssueType> getIssueTypeMap()
57      {
58          return ims.getIssueTypeMap();
59      }
60  
61      /**
62       * Adapt a <code>List</code> of <code>Issue</code>s to a <code>List</code> of <code>Release</code>s.
63       *
64       * @param issues The issues
65       * @return A list of releases
66       */
67      public List<Release> getReleases( List<Issue> issues )
68      {
69          // A Map of releases keyed by fixVersion
70          Map<String, Release> releasesMap = new HashMap<String, Release>();
71  
72          // Loop through all issues looking for fixVersions
73          for ( Issue issue : issues )
74          {
75              // Do NOT create a release for issues that lack a fixVersion
76              if ( issue.getFixVersions() != null )
77              {
78                  for ( String fixVersion : issue.getFixVersions() )
79                  {
80                      // Try to get a matching Release from the map
81                      Release release = releasesMap.get( fixVersion );
82                      if ( release == null )
83                      {
84                          // Add a new Release to the Map if it wasn't there
85                          release = new Release();
86                          release.setVersion( fixVersion );
87                          releasesMap.put( fixVersion, release );
88                      }
89  
90                      // Add this issue as an Action to this release
91                      Action action = createAction( issue );
92                      release.addAction( action );
93                  }
94              }
95          }
96  
97          // Extract the releases from the Map to a List
98          List<Release> releasesList = new ArrayList<Release>();
99          for ( Release release : releasesMap.values() )
100         {
101             releasesList.add( release );
102         }
103         return releasesList;
104     }
105 
106     /**
107      * Create an <code>Action</code> from an issue.
108      *
109      * @param issue The issue to extract the information from
110      * @return An <code>Action</code>
111      */
112     public Action createAction( Issue issue )
113     {
114         Action action = new Action();
115 
116         // @todo We need to add something like issue.getPresentationIdentifier() to be able to support other IMSes
117         // beside JIRA
118         action.setIssue( issue.getKey() );
119 
120         // Try to map the IMS-specific issue type to one that is used in a changes.xml file
121         IssueType type;
122         if ( getIssueTypeMap().containsKey( issue.getType() ) )
123         {
124             type = getIssueTypeMap().get( issue.getType() );
125             action.setType( type.modelRepresentation() );
126         }
127         else
128         {
129             action.setType( UNKNOWN_ISSUE_TYPE );
130         }
131 
132         action.setDev( issue.getAssignee() );
133 
134         action.setDueTo( issue.getReporter() == null ? "" : issue.getReporter() );
135 
136         action.setAction( issue.getSummary() );
137         return action;
138     }
139 }