Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
JiraHelper |
|
| 4.0;4 |
1 | package org.apache.maven.jira; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2006 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import org.apache.commons.httpclient.HttpClient; |
|
20 | import org.apache.commons.httpclient.methods.GetMethod; |
|
21 | import org.apache.maven.plugin.logging.Log; |
|
22 | ||
23 | import java.text.NumberFormat; |
|
24 | import java.text.ParsePosition; |
|
25 | ||
26 | /** |
|
27 | * A helper class with common JIRA related functionality. |
|
28 | * |
|
29 | * @author Dennis Lundberg |
|
30 | * @version $Id: org.apache.maven.jira.JiraHelper.html 816584 2012-05-08 12:33:35Z hboutemy $ |
|
31 | */ |
|
32 | 0 | public class JiraHelper |
33 | 0 | { |
34 | private static final String PID = "pid="; |
|
35 | ||
36 | /** |
|
37 | * Try to get a JIRA pid from the issue management URL. |
|
38 | * |
|
39 | * @param log Used to tell the user what happened |
|
40 | * @param issueManagementUrl The URL to the issue management system |
|
41 | * @param client The client used to connect to JIRA |
|
42 | * @return The JIRA id for the project, or null if it can't be found |
|
43 | */ |
|
44 | 0 | public static String getPidFromJira( Log log, String issueManagementUrl, HttpClient client ) |
45 | 0 | { |
46 | 0 | String jiraId = null; |
47 | 0 | GetMethod gm = new GetMethod( issueManagementUrl ); |
48 | 0 | log.info( "JIRA URL " + issueManagementUrl + " doesn't include a pid, trying to get it" ); |
49 | 0 | try |
50 | 0 | { |
51 | 0 | client.executeMethod( gm ); |
52 | 0 | log.info( "Successfully reached JIRA." ); |
53 | 0 | } |
54 | 0 | catch ( Exception e ) |
55 | 0 | { |
56 | 0 | if ( log.isDebugEnabled() ) |
57 | 0 | { |
58 | 0 | log.error( "Unable to reach JIRA project page:", e ); |
59 | } |
|
60 | 0 | else |
61 | 0 | { |
62 | 0 | log.error( "Unable to reach JIRA project page. Cause is: " + e.getLocalizedMessage() ); |
63 | 0 | } |
64 | 0 | } |
65 | 0 | String projectPage = gm.getResponseBodyAsString(); |
66 | 0 | int pidIndex = projectPage.indexOf( PID ); |
67 | 0 | |
68 | 0 | if ( pidIndex == -1 ) |
69 | 0 | { |
70 | 0 | log.error( "Unable to get JIRA pid using the url " + issueManagementUrl ); |
71 | } |
|
72 | 0 | else |
73 | 0 | { |
74 | 0 | NumberFormat nf = NumberFormat.getInstance(); |
75 | 0 | Number pidNumber = nf.parse( projectPage, new ParsePosition( pidIndex + PID.length() ) ); |
76 | 0 | jiraId = Integer.toString( pidNumber.intValue() ); |
77 | 0 | log.debug( "Found the pid " + jiraId + " at " + issueManagementUrl ); |
78 | 0 | } |
79 | 0 | return jiraId; |
80 | } |
|
81 | } |