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