1 | |
package org.apache.maven.plugins.patchtracker.tracking.jira; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
42 | |
|
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 | |
|
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 | |
|
81 | 0 | jiraSession.addBase64EncodedAttachmentsToIssue( remoteIssue.getKey(), remoteIssue.getKey(), |
82 | |
patchTrackerRequest.getPatchContent() ); |
83 | |
|
84 | |
|
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 | |
|
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 | |
|
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 | |
|
185 | |
|
186 | |
|
187 | |
protected String extractProjectKey( String url ) |
188 | |
{ |
189 | |
|
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 | |
|
202 | |
|
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 | |
|
220 | |
|
221 | 1 | if ( StringUtils.contains( url, "/" + projectKey + "/component/" ) ) |
222 | |
{ |
223 | 1 | return StringUtils.substringAfterLast( url, "/" + projectKey + "/component/" ); |
224 | |
} |
225 | 0 | return null; |
226 | |
} |
227 | |
} |