View Javadoc

1   package org.apache.maven.plugins.patchtracker.tracking.jira;
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.commons.lang.StringUtils;
23  import org.apache.maven.plugin.logging.Log;
24  import org.apache.maven.plugins.patchtracker.tracking.PatchTracker;
25  import org.apache.maven.plugins.patchtracker.tracking.PatchTrackerException;
26  import org.apache.maven.plugins.patchtracker.tracking.PatchTrackerRequest;
27  import org.apache.maven.plugins.patchtracker.tracking.PatchTrackerResult;
28  import org.apache.maven.plugins.patchtracker.tracking.jira.soap.JiraSoapServiceService;
29  import org.apache.maven.plugins.patchtracker.tracking.jira.soap.JiraSoapServiceServiceLocator;
30  import org.apache.maven.plugins.patchtracker.tracking.jira.soap.RemoteAuthenticationException;
31  import org.apache.maven.plugins.patchtracker.tracking.jira.soap.RemoteComponent;
32  import org.apache.maven.plugins.patchtracker.tracking.jira.soap.RemoteException;
33  import org.apache.maven.plugins.patchtracker.tracking.jira.soap.RemoteIssue;
34  
35  import javax.xml.rpc.ServiceException;
36  import java.net.MalformedURLException;
37  import java.net.URL;
38  import java.util.List;
39  
40  /**
41   * @author Olivier Lamy
42   * @plexus.component role="org.apache.maven.plugins.patchtracker.tracking.PatchTracker" role-hint="jira"
43   */
44  public class JiraPatchTracker
45      implements PatchTracker
46  {
47      public PatchTrackerResult createPatch( PatchTrackerRequest patchTrackerRequest, Log log )
48          throws PatchTrackerException
49      {
50  
51          JiraSession jiraSession = createSession( patchTrackerRequest, log );
52          try
53          {
54              RemoteIssue remoteIssue = new RemoteIssue();
55              remoteIssue.setProject( extractProjectKey( patchTrackerRequest.getUrl() ) );
56              remoteIssue.setSummary( patchTrackerRequest.getSummary() );
57              remoteIssue.setDescription( patchTrackerRequest.getDescription() );
58              remoteIssue.setType( patchTrackerRequest.getPatchType() );
59              remoteIssue.setPriority( patchTrackerRequest.getPatchPriority() );
60  
61              // do we have a component id ??
62              String componentId =
63                  getComponentId( patchTrackerRequest.getUrl(), extractProjectKey( patchTrackerRequest.getUrl() ) );
64  
65              if ( StringUtils.isNotEmpty( componentId ) )
66              {
67                  List<RemoteComponent> remoteComponents = jiraSession.getRemoteComponents();
68                  for ( RemoteComponent remoteComponent : remoteComponents )
69                  {
70                      if ( StringUtils.equalsIgnoreCase( componentId, remoteComponent.getId() ) )
71                      {
72                          remoteIssue.setComponents( new RemoteComponent[]{ remoteComponent } );
73                          break;
74                      }
75                  }
76              }
77  
78              remoteIssue = jiraSession.createIssue( remoteIssue );
79  
80              // TODO handle of boolean result
81              jiraSession.addBase64EncodedAttachmentsToIssue( remoteIssue.getKey(), remoteIssue.getKey(),
82                                                              patchTrackerRequest.getPatchContent() );
83  
84              // add a comment
85  
86              return new PatchTrackerResult().setPatchId( remoteIssue.getKey() ).setPatchUrl(
87                  extractBaseUrl( patchTrackerRequest.getUrl() ) + "/browse/" + remoteIssue.getKey() );
88          }
89          catch ( RemoteAuthenticationException e )
90          {
91              throw new PatchTrackerException( e.getMessage(), e );
92          }
93          catch ( RemoteException e )
94          {
95              throw new PatchTrackerException( e.getMessage(), e );
96          }
97          catch ( java.rmi.RemoteException e )
98          {
99              throw new PatchTrackerException( e.getMessage(), e );
100         }
101     }
102 
103     public PatchTrackerResult updatePatch( PatchTrackerRequest patchTrackerRequest, Log log )
104         throws PatchTrackerException
105     {
106 
107         JiraSession jiraSession = createSession( patchTrackerRequest, log );
108         try
109         {
110             RemoteIssue remoteIssue = jiraSession.findRemoteIssue( patchTrackerRequest.getPatchId() );
111 
112             if ( patchTrackerRequest.getPatchId() == null )
113             {
114                 throw new PatchTrackerException( "patch id is mandatory when updating the patch tracker" );
115             }
116 
117             // TODO handle of boolean result
118             jiraSession.addBase64EncodedAttachmentsToIssue( remoteIssue.getKey(), remoteIssue.getKey(),
119                                                             patchTrackerRequest.getPatchContent() );
120 
121             jiraSession.addCommentToIssue( remoteIssue.getKey(), patchTrackerRequest.getDescription() );
122 
123             return new PatchTrackerResult().setPatchId( remoteIssue.getKey() ).setPatchUrl(
124                 extractBaseUrl( patchTrackerRequest.getUrl() ) + "/browse/" + remoteIssue.getKey() );
125 
126         }
127         catch ( RemoteAuthenticationException e )
128         {
129             throw new PatchTrackerException( e.getMessage(), e );
130         }
131         catch ( RemoteException e )
132         {
133             throw new PatchTrackerException( e.getMessage(), e );
134         }
135         catch ( java.rmi.RemoteException e )
136         {
137             throw new PatchTrackerException( e.getMessage(), e );
138         }
139     }
140 
141     public JiraSession createSession( PatchTrackerRequest patchTrackerRequest, Log log )
142         throws PatchTrackerException
143     {
144         if ( StringUtils.isEmpty( patchTrackerRequest.getUserName() ) || StringUtils.isEmpty(
145             patchTrackerRequest.getPassword() ) )
146         {
147             // remote access not supported
148             throw new PatchTrackerException( "username or password for jira access are null or empty" );
149         }
150         JiraSoapServiceService jiraSoapServiceGetter = new JiraSoapServiceServiceLocator();
151         try
152         {
153             URL baseUrl = extractBaseUrlAsUrl( patchTrackerRequest.getUrl() );
154             log.debug( "baseUrl:" + baseUrl.toExternalForm() );
155             org.apache.maven.plugins.patchtracker.tracking.jira.soap.JiraSoapService service =
156                 jiraSoapServiceGetter.getJirasoapserviceV2( new URL( baseUrl, "/rpc/soap/jirasoapservice-v2" ) );
157             return new JiraSession( service, service.login( patchTrackerRequest.getUserName(),
158                                                             patchTrackerRequest.getPassword() ),
159                                     extractProjectKey( patchTrackerRequest.getUrl() ) );
160         }
161         catch ( MalformedURLException e )
162         {
163             throw new PatchTrackerException( e.getMessage(), e );
164         }
165         catch ( ServiceException e )
166         {
167             throw new PatchTrackerException( e.getMessage(), e );
168         }
169         catch ( RemoteAuthenticationException e )
170         {
171             throw new PatchTrackerException( e.getMessage(), e );
172         }
173         catch ( RemoteException e )
174         {
175             throw new PatchTrackerException( e.getMessage(), e );
176         }
177         catch ( java.rmi.RemoteException e )
178         {
179             throw new PatchTrackerException( e.getMessage(), e );
180         }
181     }
182 
183     /**
184      * @param url https://jira.codehaus.org/browse/MNG
185      * @return the project key MNG
186      */
187     protected String extractProjectKey( String url )
188     {
189         // case component id in url: https://jira.codehaus.org/browse/MSHARED/component/15255
190         if ( StringUtils.contains( url, "/component/" ) )
191         {
192             url = StringUtils.substringBeforeLast( url, "/component" );
193         }
194 
195         return ( StringUtils.endsWith( url, "/" ) )
196             ? StringUtils.substringAfterLast( StringUtils.removeEnd( url, "/" ), "/" )
197             : StringUtils.substringAfterLast( url, "/" );
198     }
199 
200     /**
201      * @param url https://jira.codehaus.org/browse/MNG
202      * @return https://jira.codehaus.org
203      */
204     protected String extractBaseUrl( String url )
205     {
206         return StringUtils.substringBefore( url, "/browse" );
207 
208     }
209 
210     protected URL extractBaseUrlAsUrl( String url )
211         throws MalformedURLException
212     {
213         return new URL( extractBaseUrl( url ) );
214 
215     }
216 
217     protected String getComponentId( String url, String projectKey )
218     {
219         //https://jira.codehaus.org/browse/MSHARED/component/15255
220         // return 15255
221         if ( StringUtils.contains( url, "/" + projectKey + "/component/" ) )
222         {
223             return StringUtils.substringAfterLast( url, "/" + projectKey + "/component/" );
224         }
225         return null;
226     }
227 }