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