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