1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.changes.trac;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import org.apache.maven.plugins.changes.issues.Issue;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.xmlrpc.XmlRpcException;
35 import org.apache.xmlrpc.client.XmlRpcClient;
36 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
37 import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
38
39
40
41
42
43
44
45
46 public class TracDownloader {
47
48 private MavenProject project;
49
50
51 private String query;
52
53
54 private String tracPassword;
55
56
57 private String tracUser;
58
59 private Issue createIssue(Object[] ticketObj) {
60 Issue issue = new Issue();
61
62 issue.setId(String.valueOf(ticketObj[0]));
63
64 issue.setKey(String.valueOf(ticketObj[0]));
65
66 issue.setLink(getUrl() + "/ticket/" + ticketObj[0]);
67
68 issue.setCreated(parseDate(String.valueOf(ticketObj[1])));
69
70 issue.setUpdated(parseDate(String.valueOf(ticketObj[2])));
71
72 @SuppressWarnings("unchecked")
73 Map<String, String> attributes = (Map<String, String>) ticketObj[3];
74
75 issue.setType(attributes.get("type"));
76
77 issue.setSummary(attributes.get("summary"));
78
79 issue.setStatus(attributes.get("status"));
80
81 issue.setResolution(attributes.get("resolution"));
82
83 issue.setAssignee(attributes.get("owner"));
84
85 issue.addFixVersion(attributes.get("milestone"));
86
87 issue.setPriority(attributes.get("priority"));
88
89 issue.setReporter(attributes.get("reporter"));
90
91 issue.addComponent(attributes.get("component"));
92
93 return issue;
94 }
95
96 public List<Issue> getIssueList() throws MalformedURLException, XmlRpcException {
97
98 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
99
100 try {
101 config.setServerURL(new URL(getUrl() + "/login/xmlrpc"));
102 } catch (MalformedURLException e) {
103 throw new MalformedURLException("The Trac URL is incorrect.");
104 }
105 config.setBasicUserName(tracUser);
106 config.setBasicPassword(tracPassword);
107
108 XmlRpcClient client = new XmlRpcClient();
109
110 client.setConfig(config);
111
112 client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
113
114
115 String qstr = "";
116
117 if (!(query == null || query.isEmpty())) {
118 qstr = query;
119 }
120
121 Object[] params = new Object[] {qstr};
122 Object[] queryResult;
123 ArrayList<Issue> issueList = new ArrayList<>();
124 try {
125 queryResult = (Object[]) client.execute("ticket.query", params);
126
127 for (Object aQueryResult : queryResult) {
128 params = new Object[] {aQueryResult};
129 Object[] ticketGetResult;
130 ticketGetResult = (Object[]) client.execute("ticket.get", params);
131 issueList.add(createIssue(ticketGetResult));
132 }
133 } catch (XmlRpcException e) {
134 throw new XmlRpcException("XmlRpc Error.", e);
135 }
136 return issueList;
137 }
138
139 private String getUrl() {
140
141 String url = project.getIssueManagement().getUrl();
142
143 if (url.endsWith("/")) {
144 url = url.substring(0, url.length() - 1);
145 }
146
147 return url;
148 }
149
150 public void setProject(MavenProject project) {
151 this.project = project;
152 }
153
154 public void setQuery(String query) {
155 this.query = query;
156 }
157
158 public void setTracPassword(String tracPassword) {
159 this.tracPassword = tracPassword;
160 }
161
162 public void setTracUser(String tracUser) {
163 this.tracUser = tracUser;
164 }
165
166 private Date parseDate(String timeCreated) throws RuntimeException {
167 try {
168 long millis = Long.parseLong(timeCreated);
169 Calendar cld = Calendar.getInstance();
170 cld.setTimeInMillis(millis * 1000L);
171 return cld.getTime();
172 } catch (NumberFormatException e) {
173 SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
174 try {
175 return format.parse(timeCreated);
176 } catch (ParseException e1) {
177 throw new RuntimeException("Failed to parse date '" + timeCreated + "' as a date.", e1);
178 }
179 }
180 }
181 }