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