Commits

Sebastian Sdorra  committed 444b995

remove issue self reference link, in newer scm-manager versions

  • Participants
  • Parent commits 6e12e0e

Comments (0)

Files changed (2)

File src/main/java/sonia/scm/jira/SoapJiraHandler.java

 import sonia.scm.jira.soap.RemoteComment;
 import sonia.scm.jira.soap.RemoteFieldValue;
 import sonia.scm.jira.soap.RemoteNamedObject;
+import sonia.scm.repository.EscapeUtil;
 
 //~--- JDK imports ------------------------------------------------------------
 
 import java.rmi.RemoteException;
 
+import java.text.MessageFormat;
+
 import java.util.GregorianCalendar;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  *
    *
    *
    * @param service
+   * @param jiraUrl
    * @param token
    * @param username
    */
-  public SoapJiraHandler(JiraSoapService service, String token, String username)
+  public SoapJiraHandler(JiraSoapService service, String jiraUrl, String token,
+    String username)
   {
     this.service = service;
+    this.jiraUrl = jiraUrl;
     this.token = token;
     this.username = username;
   }
 
     remoteComment.setAuthor(username);
     remoteComment.setCreated(new GregorianCalendar());
-    remoteComment.setBody(comment.getBody());
+    remoteComment.setBody(prepareComment(issueId, comment));
 
     if (!Strings.isNullOrEmpty(comment.getBody()))
     {
     }
   }
 
+  /**
+   * Method description
+   *
+   *
+   * @param issueId
+   * @param comment
+   *
+   * @return
+   */
+  public String prepareComment(String issueId, Comment comment)
+  {
+    String body = Strings.nullToEmpty(comment.getBody());
+
+    return removeIssueLink(issueId, body);
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**
     return result;
   }
 
+  /**
+   * Remove issue self reference link.
+   * {@see https://bitbucket.org/sdorra/scm-manager/issue/337/jira-comment-contains-unneccessary-link}.
+   *
+   * TODO: The preprocessor order on hooks should be fixed in the core.
+   *
+   *
+   * @param issueId
+   * @param body
+   *
+   * @return
+   */
+  private String removeIssueLink(String issueId, String body)
+  {
+    //J-
+    String link = MessageFormat.format(
+      JiraChangesetPreProcessorFactory.REPLACEMENT_LINK,
+      jiraUrl
+    ).replaceAll(Matcher.quoteReplacement("$0"), issueId);
+    //J+
+
+    body = body.replaceAll(link, issueId);
+    body = body.replaceAll(EscapeUtil.escape(link), issueId);
+
+    return body;
+  }
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
+  private String jiraUrl;
+
+  /** Field description */
   private JiraSoapService service;
 
   /** Field description */

File src/main/java/sonia/scm/jira/SoapJiraHandlerFactory.java

         new JiraSoapServiceServiceLocator().getJirasoapserviceV2(url);
       String token = service.login(username, password);
 
-      handler = new SoapJiraHandler(service, token, username);
+      handler = new SoapJiraHandler(service, urlString, token, username);
     }
     catch (Exception ex)
     {