Coverage Report - org.apache.maven.plugins.patchtracker.tracking.jira.JiraPatchTracker
 
Classes in this File Line Coverage Branch Coverage Complexity
JiraPatchTracker
11 %
7/62
27 %
5/18
6,286
 
 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  7
 public class JiraPatchTracker
 45  
     implements PatchTracker
 46  
 {
 47  
     public PatchTrackerResult createPatch( PatchTrackerRequest patchTrackerRequest, Log log )
 48  
         throws PatchTrackerException
 49  
     {
 50  
 
 51  0
         JiraSession jiraSession = createSession( patchTrackerRequest, log );
 52  
         try
 53  
         {
 54  0
             RemoteIssue remoteIssue = new RemoteIssue();
 55  0
             remoteIssue.setProject( extractProjectKey( patchTrackerRequest.getUrl() ) );
 56  0
             remoteIssue.setSummary( patchTrackerRequest.getSummary() );
 57  0
             remoteIssue.setDescription( patchTrackerRequest.getDescription() );
 58  0
             remoteIssue.setType( patchTrackerRequest.getPatchType() );
 59  0
             remoteIssue.setPriority( patchTrackerRequest.getPatchPriority() );
 60  
 
 61  
             // do we have a component id ??
 62  0
             String componentId =
 63  
                 getComponentId( patchTrackerRequest.getUrl(), extractProjectKey( patchTrackerRequest.getUrl() ) );
 64  
 
 65  0
             if ( StringUtils.isNotEmpty( componentId ) )
 66  
             {
 67  0
                 List<RemoteComponent> remoteComponents = jiraSession.getRemoteComponents();
 68  0
                 for ( RemoteComponent remoteComponent : remoteComponents )
 69  
                 {
 70  0
                     if ( StringUtils.equalsIgnoreCase( componentId, remoteComponent.getId() ) )
 71  
                     {
 72  0
                         remoteIssue.setComponents( new RemoteComponent[]{ remoteComponent } );
 73  0
                         break;
 74  
                     }
 75  
                 }
 76  
             }
 77  
 
 78  0
             remoteIssue = jiraSession.createIssue( remoteIssue );
 79  
 
 80  
             // TODO handle of boolean result
 81  0
             jiraSession.addBase64EncodedAttachmentsToIssue( remoteIssue.getKey(), remoteIssue.getKey(),
 82  
                                                             patchTrackerRequest.getPatchContent() );
 83  
 
 84  
             // add a comment
 85  
 
 86  0
             return new PatchTrackerResult().setPatchId( remoteIssue.getKey() ).setPatchUrl(
 87  
                 extractBaseUrl( patchTrackerRequest.getUrl() ) + "/browse/" + remoteIssue.getKey() );
 88  
         }
 89  0
         catch ( RemoteAuthenticationException e )
 90  
         {
 91  0
             throw new PatchTrackerException( e.getMessage(), e );
 92  
         }
 93  0
         catch ( RemoteException e )
 94  
         {
 95  0
             throw new PatchTrackerException( e.getMessage(), e );
 96  
         }
 97  0
         catch ( java.rmi.RemoteException e )
 98  
         {
 99  0
             throw new PatchTrackerException( e.getMessage(), e );
 100  
         }
 101  
     }
 102  
 
 103  
     public PatchTrackerResult updatePatch( PatchTrackerRequest patchTrackerRequest, Log log )
 104  
         throws PatchTrackerException
 105  
     {
 106  
 
 107  0
         JiraSession jiraSession = createSession( patchTrackerRequest, log );
 108  
         try
 109  
         {
 110  0
             RemoteIssue remoteIssue = jiraSession.findRemoteIssue( patchTrackerRequest.getPatchId() );
 111  
 
 112  0
             if ( patchTrackerRequest.getPatchId() == null )
 113  
             {
 114  0
                 throw new PatchTrackerException( "patch id is mandatory when updating the patch tracker" );
 115  
             }
 116  
 
 117  
             // TODO handle of boolean result
 118  0
             jiraSession.addBase64EncodedAttachmentsToIssue( remoteIssue.getKey(), remoteIssue.getKey(),
 119  
                                                             patchTrackerRequest.getPatchContent() );
 120  
 
 121  0
             jiraSession.addCommentToIssue( remoteIssue.getKey(), patchTrackerRequest.getDescription() );
 122  
 
 123  0
             return new PatchTrackerResult().setPatchId( remoteIssue.getKey() ).setPatchUrl(
 124  
                 extractBaseUrl( patchTrackerRequest.getUrl() ) + "/browse/" + remoteIssue.getKey() );
 125  
 
 126  
         }
 127  0
         catch ( RemoteAuthenticationException e )
 128  
         {
 129  0
             throw new PatchTrackerException( e.getMessage(), e );
 130  
         }
 131  0
         catch ( RemoteException e )
 132  
         {
 133  0
             throw new PatchTrackerException( e.getMessage(), e );
 134  
         }
 135  0
         catch ( java.rmi.RemoteException e )
 136  
         {
 137  0
             throw new PatchTrackerException( e.getMessage(), e );
 138  
         }
 139  
     }
 140  
 
 141  
     public JiraSession createSession( PatchTrackerRequest patchTrackerRequest, Log log )
 142  
         throws PatchTrackerException
 143  
     {
 144  0
         if ( StringUtils.isEmpty( patchTrackerRequest.getUserName() ) || StringUtils.isEmpty(
 145  
             patchTrackerRequest.getPassword() ) )
 146  
         {
 147  
             // remote access not supported
 148  0
             throw new PatchTrackerException( "username or password for jira access are null or empty" );
 149  
         }
 150  0
         JiraSoapServiceService jiraSoapServiceGetter = new JiraSoapServiceServiceLocator();
 151  
         try
 152  
         {
 153  0
             URL baseUrl = extractBaseUrlAsUrl( patchTrackerRequest.getUrl() );
 154  0
             log.debug( "baseUrl:" + baseUrl.toExternalForm() );
 155  0
             org.apache.maven.plugins.patchtracker.tracking.jira.soap.JiraSoapService service =
 156  
                 jiraSoapServiceGetter.getJirasoapserviceV2( new URL( baseUrl, "/rpc/soap/jirasoapservice-v2" ) );
 157  0
             return new JiraSession( service, service.login( patchTrackerRequest.getUserName(),
 158  
                                                             patchTrackerRequest.getPassword() ),
 159  
                                     extractProjectKey( patchTrackerRequest.getUrl() ) );
 160  
         }
 161  0
         catch ( MalformedURLException e )
 162  
         {
 163  0
             throw new PatchTrackerException( e.getMessage(), e );
 164  
         }
 165  0
         catch ( ServiceException e )
 166  
         {
 167  0
             throw new PatchTrackerException( e.getMessage(), e );
 168  
         }
 169  0
         catch ( RemoteAuthenticationException e )
 170  
         {
 171  0
             throw new PatchTrackerException( e.getMessage(), e );
 172  
         }
 173  0
         catch ( RemoteException e )
 174  
         {
 175  0
             throw new PatchTrackerException( e.getMessage(), e );
 176  
         }
 177  0
         catch ( java.rmi.RemoteException e )
 178  
         {
 179  0
             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  3
         if ( StringUtils.contains( url, "/component/" ) )
 191  
         {
 192  1
             url = StringUtils.substringBeforeLast( url, "/component" );
 193  
         }
 194  
 
 195  3
         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  3
         return StringUtils.substringBefore( url, "/browse" );
 207  
 
 208  
     }
 209  
 
 210  
     protected URL extractBaseUrlAsUrl( String url )
 211  
         throws MalformedURLException
 212  
     {
 213  0
         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  1
         if ( StringUtils.contains( url, "/" + projectKey + "/component/" ) )
 222  
         {
 223  1
             return StringUtils.substringAfterLast( url, "/" + projectKey + "/component/" );
 224  
         }
 225  0
         return null;
 226  
     }
 227  
 }