You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/03/07 14:43:11 UTC

[maven-changes-plugin] 01/01: invesigate JQL query URLs

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch i344
in repository https://gitbox.apache.org/repos/asf/maven-changes-plugin.git

commit 730434a42754de691dea72874399ae066ce2626a
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Sat Mar 7 09:42:48 2020 -0500

    invesigate JQL query URLs
---
 .../apache/maven/plugins/changes/ReleaseUtils.java | 12 ++--
 .../maven/plugins/jira/ClassicJiraDownloader.java  | 51 ++++++++--------
 .../org/apache/maven/plugins/jira/JiraHelper.java  | 68 +++++++++++-----------
 .../apache/maven/plugins/jira/PomException.java    | 36 ++++++++++++
 .../maven/plugins/jira/JiraHelperTestCase.java     | 41 ++++++++++---
 5 files changed, 137 insertions(+), 71 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/changes/ReleaseUtils.java b/src/main/java/org/apache/maven/plugins/changes/ReleaseUtils.java
index fbf29f2..99b829a 100644
--- a/src/main/java/org/apache/maven/plugins/changes/ReleaseUtils.java
+++ b/src/main/java/org/apache/maven/plugins/changes/ReleaseUtils.java
@@ -83,9 +83,9 @@ public class ReleaseUtils
     /**
      * Get a release with the specified version from the list of releases.
      *
-     * @param releases A list of releases
-     * @param version The version we want
-     * @return A Release, or null if no release with the specified version can be found
+     * @param releases a list of releases
+     * @param version the version we want
+     * @return a Release, or null if no release with the specified version can be found
      */
     protected Release getRelease( List<Release> releases, String version )
     {
@@ -181,10 +181,10 @@ public class ReleaseUtils
      * components, i.e. they have the same version, their issues are merged into one (parent) release with component
      * marker for component issues.
      *
-     * @param releases Releases from the parent component
+     * @param releases releases from the parent component
      * @param componentName child component name (retrieved from project name)
-     * @param componentReleases Releases from the child component
-     * @return A list containing the merged releases
+     * @param componentReleases releases from the child component
+     * @return a list containing the merged releases
      */
     public List<Release> mergeReleases( final List<Release> releases, final String componentName,
                                         final List<Release> componentReleases )
diff --git a/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java b/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java
index ee0e33c..fbb66f7 100644
--- a/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java
+++ b/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java
@@ -120,7 +120,11 @@ public final class ClassicJiraDownloader
                 download( client, fullUrl );
             }
         }
-        catch ( Exception e )
+        catch ( PomException e )
+        {
+            getLog().error( "Invalid issue management URL " + project.getIssueManagement().getUrl(), e );
+        }
+        catch ( RuntimeException e )
         {
             if ( project.getIssueManagement() != null )
             {
@@ -133,49 +137,48 @@ public final class ClassicJiraDownloader
         }
     }
 
-    private String getJqlQueryURL()
+    private String getJqlQueryURL() throws PomException
     {
         // JQL is based on project names instead of project ID's
-        Map<String, String> urlMap = JiraHelper.getJiraUrlAndProjectName( project.getIssueManagement().getUrl() );
-        String jiraUrl = urlMap.get( "url" );
-        String jiraProject = urlMap.get( "project" );
-
-        if ( jiraProject == null )
-        {
-            throw new RuntimeException( "The issue management URL in the POM does not include a JIRA project name" );
-        }
-        else
+        try
         {
+            Map<String, String> urlMap = JiraHelper.getJiraUrlAndProjectName( project.getIssueManagement().getUrl() );
+            String jiraUrl = urlMap.get( "url" );
+            String jiraProject = urlMap.get( "project" );
+
             // create the URL for getting the proper issues from JIRA
-            String jqlQuery = new JqlQueryBuilder( log ).project( jiraProject ).fixVersion( getFixFor() )
-                .fixVersionIds( fixVersionIds ).statusIds( statusIds ).priorityIds( priorityIds )
-                .resolutionIds( resolutionIds ).components( component ).typeIds( typeIds )
-                .sortColumnNames( sortColumnNames )
-                .build();
+            String jqlQuery = new JqlQueryBuilder( log ).project( jiraProject ).fixVersion( getFixFor() ).fixVersionIds(
+                    fixVersionIds ).statusIds( statusIds ).priorityIds( priorityIds ).resolutionIds(
+                    resolutionIds ).components( component ).typeIds( typeIds ).sortColumnNames(
+                    sortColumnNames ).build();
 
             return new UrlBuilder( jiraUrl, "sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml" )
-                        .addParameter( "tempMax", nbEntriesMax ).addParameter( "reset", "true" )
-                        .addParameter( "jqlQuery", jqlQuery )
-                        .build();
+                    .addParameter( "tempMax", nbEntriesMax ).addParameter( "reset", "true" )
+                    .addParameter( "jqlQuery", jqlQuery ).build();
+        }
+        catch ( IllegalArgumentException ex )
+        {
+            throw new PomException( ex );
         }
     }
 
-    private String getParameterBasedQueryURL( HttpClient client )
+    private String getParameterBasedQueryURL( HttpClient client ) throws PomException
     {
-        Map<String, String> urlMap = JiraHelper.getJiraUrlAndProjectId( project.getIssueManagement().getUrl() );
+        String url = project.getIssueManagement().getUrl();
+        Map<String, String> urlMap = JiraHelper.getJiraUrlAndProjectId( url );
         String jiraUrl = urlMap.get( "url" );
         String jiraId = urlMap.get( "id" );
 
         if ( jiraId == null || jiraId.length() == 0 )
         {
-            log.debug( "The JIRA URL " + project.getIssueManagement().getUrl()
+            log.debug( "The JIRA URL " + url
                 + " doesn't include a pid, trying to extract it from JIRA." );
-            jiraId = JiraHelper.getPidFromJira( log, project.getIssueManagement().getUrl(), client );
+            jiraId = JiraHelper.getPidFromJira( log, url, client );
         }
 
         if ( jiraId == null )
         {
-            throw new RuntimeException( "The issue management URL in the POM does not include a pid,"
+            throw new PomException( "The issue management URL in the POM does not include a pid,"
                 + " and it was not possible to extract it from the page at that URL." );
         }
         else
diff --git a/src/main/java/org/apache/maven/plugins/jira/JiraHelper.java b/src/main/java/org/apache/maven/plugins/jira/JiraHelper.java
index d50bc85..01ddf60 100644
--- a/src/main/java/org/apache/maven/plugins/jira/JiraHelper.java
+++ b/src/main/java/org/apache/maven/plugins/jira/JiraHelper.java
@@ -19,6 +19,9 @@ package org.apache.maven.plugins.jira;
  * under the License.
  */
 
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.text.NumberFormat;
 import java.text.ParsePosition;
 import java.util.HashMap;
@@ -96,10 +99,10 @@ public class JiraHelper
     /**
      * Try to get a JIRA pid from the issue management URL.
      *
-     * @param log Used to tell the user what happened
-     * @param issueManagementUrl The URL to the issue management system
-     * @param client The client used to connect to JIRA
-     * @return The JIRA id for the project, or null if it can't be found
+     * @param log used to tell the user what happened
+     * @param issueManagementUrl the URL to the issue management system
+     * @param client the client used to connect to JIRA
+     * @return the JIRA id for the project, or null if it can't be found
      */
     public static String getPidFromJira( Log log, String issueManagementUrl, HttpClient client )
     {
@@ -113,7 +116,7 @@ public class JiraHelper
             log.debug( "Successfully reached JIRA." );
             projectPage = gm.getResponseBodyAsString();
         }
-        catch ( Exception e )
+        catch ( IOException e )
         {
             if ( log.isDebugEnabled() )
             {
@@ -148,49 +151,48 @@ public class JiraHelper
     }
 
     /**
-     * Parse out the base URL for JIRA and the JIRA project name from the issue management URL. The issue management URL
-     * is assumed to be of the format http(s)://host:port/browse/{projectname}
+     * Parse out the base URL for JIRA and the JIRA project name from the issue management URL.
+     * The issue management UURL must be in the format http(s)://host:port/browse/{projectname}.
+     * The URL is mapped to the key {@code "url"}.
+     * The project is mapped to the key {@code "project"}.
      *
-     * @param issueManagementUrl The URL to the issue management system
-     * @return A <code>Map</code> containing the URL and project name
+     * @param issueManagementUrl the URL of the issue management system
+     * @return a <code>Map</code> containing the URL and project name
+     * @throws IllegalArgumentException if the URL does not contain /browse
      * @since 2.8
      */
     public static Map<String, String> getJiraUrlAndProjectName( String issueManagementUrl )
     {
-        final int indexBrowse = issueManagementUrl.indexOf( "/browse/" );
-
-        String jiraUrl;
-        String project;
 
-        if ( indexBrowse != -1 )
+        try
         {
-            jiraUrl = issueManagementUrl.substring( 0, indexBrowse );
+            URI uri = new URI( issueManagementUrl );
+            String path = uri.getPath();
+            if ( !path.contains( "/browse/" ) )
+            {
+                throw new IllegalArgumentException( "Invalid browse URL " + issueManagementUrl  );
+            }
 
-            final int indexBrowseEnd = indexBrowse + "/browse/".length();
+            path = path.substring( path.indexOf( "/browse/" ) );
 
-            final int indexProject = issueManagementUrl.indexOf( "/", indexBrowseEnd );
+            String jiraUrl = issueManagementUrl.substring( 0,  issueManagementUrl.indexOf( "/browse/" ) );
 
-            if ( indexProject != -1 )
-            {
-                // Project name has trailing '/'
-                project = issueManagementUrl.substring( indexBrowseEnd, indexProject );
-            }
-            else
+            String project = path.substring( "/browse/".length() );
+            if ( project.endsWith( "/" ) )
             {
-                // Project name without trailing '/'
-                project = issueManagementUrl.substring( indexBrowseEnd );
+                project = project.substring( 0, project.length() - 1 );
             }
+
+            HashMap<String, String> urlMap = new HashMap<>( 4 );
+            urlMap.put( "url", jiraUrl );
+            urlMap.put( "project", project );
+
+            return urlMap;
         }
-        else
+        catch ( URISyntaxException | IndexOutOfBoundsException ex )
         {
-            throw new IllegalArgumentException( "Invalid browse URL" );
+            throw new IllegalArgumentException( ex );
         }
-
-        HashMap<String, String> urlMap = new HashMap<>( 4 );
-        urlMap.put( "url", jiraUrl );
-        urlMap.put( "project", project );
-
-        return urlMap;
     }
 
     /**
diff --git a/src/main/java/org/apache/maven/plugins/jira/PomException.java b/src/main/java/org/apache/maven/plugins/jira/PomException.java
new file mode 100644
index 0000000..4eaa557
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/jira/PomException.java
@@ -0,0 +1,36 @@
+package org.apache.maven.plugins.jira;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Some data in the pom.xml was not as it should be.
+ */
+class PomException extends Exception
+{
+    PomException( Exception ex )
+    {
+        super( ex );
+    }
+
+    PomException( String message )
+    {
+        super( message );
+    }
+}
diff --git a/src/test/java/org/apache/maven/plugins/jira/JiraHelperTestCase.java b/src/test/java/org/apache/maven/plugins/jira/JiraHelperTestCase.java
index bb580ed..459c12e 100644
--- a/src/test/java/org/apache/maven/plugins/jira/JiraHelperTestCase.java
+++ b/src/test/java/org/apache/maven/plugins/jira/JiraHelperTestCase.java
@@ -23,7 +23,6 @@ import junit.framework.TestCase;
 
 import java.util.Map;
 
-import org.apache.maven.plugins.jira.JiraHelper;
 
 /**
  * Tests for the JiraHelper class.
@@ -35,32 +34,55 @@ import org.apache.maven.plugins.jira.JiraHelper;
 public class JiraHelperTestCase
     extends TestCase
 {
+
+    private Map<String, String> map;
+
     public void testGetJiraUrlAndProjectId()
     {
-        Map<String, String> map;
-
         map = JiraHelper.getJiraUrlAndProjectId( "https://issues.apache.org/jira/browse/DOXIA" );
         assertEquals( "https://issues.apache.org/jira", map.get( "url" ) );
+    }
 
+    public void testGetJiraUrlAndProjectId_MCHANGES_218()
+    {
         // MCHANGES-218
         map = JiraHelper.getJiraUrlAndProjectId( "https://issues.apache.org/jira/browse/DOXIA/" );
         assertEquals( "https://issues.apache.org/jira", map.get( "url" ) );
+    }
 
+    public void testGetJiraUrlAndProjectId_MCHANGES_222()
+    {
         // MCHANGES-222
-        map =
-            JiraHelper.getJiraUrlAndProjectId( "https://issues.apache.org/jira/secure/IssueNavigator.jspa?pid=11761&reset=true" );
+        map = JiraHelper.getJiraUrlAndProjectId(
+                "https://issues.apache.org/jira/secure/IssueNavigator.jspa?pid=11761&reset=true" );
         assertEquals( "https://issues.apache.org/jira", map.get( "url" ) );
         map = JiraHelper.getJiraUrlAndProjectId( "https://issues.apache.org/jira/browse/MSHARED/component/13380" );
         assertEquals( "https://issues.apache.org/jira", map.get( "url" ) );
     }
 
+    public void testGetJiraUrlAndProjectName_JQL()
+    {
+        // MCHANGES-385
+        String url = "https://issues.apache.org/jira/issues/?jql=component%20%3D%20changes.xml";
+        try
+        {
+            JiraHelper.getJiraUrlAndProjectName( url );
+            fail( "Expected IllegalArgumentException" );
+        }
+        catch ( IllegalArgumentException expected ) {
+            assertTrue( expected.getMessage().contains( url ) );
+        }
+    }
+
     public void testGetJiraUrlAndProjectName()
     {
-        Map<String, String> map;
         map = JiraHelper.getJiraUrlAndProjectName( "https://issues.apache.org/jira/browse/DOXIA/" );
         assertEquals( "https://issues.apache.org/jira", map.get( "url" ) );
         assertEquals( "DOXIA", map.get( "project" ) );
+    }
 
+    public void testGetJiraUrlAndProjectName_https()
+    {
         map = JiraHelper.getJiraUrlAndProjectName( "https://issues.apache.org/jira/browse/DOXIA" );
         assertEquals( "https://issues.apache.org/jira", map.get( "url" ) );
         assertEquals( "DOXIA", map.get( "project" ) );
@@ -71,9 +93,12 @@ public class JiraHelperTestCase
         String expected = "http://www.jira.com";
         String actual = JiraHelper.getBaseUrl( "http://www.jira.com/context/test?werewrew" );
         assertEquals( expected, actual );
+    }
 
-        expected = "https://www.jira.com";
-        actual = JiraHelper.getBaseUrl( "https://www.jira.com/context/test?werewrew" );
+    public void testGetBaseUrl_https()
+    {
+        String expected = "https://www.jira.com";
+        String actual = JiraHelper.getBaseUrl( "https://www.jira.com/context/test?werewrew" );
         assertEquals( expected, actual );
     }
 }