]> source.dussan.org Git - gitblit.git/commitdiff
Process bugtraq configuration in markup document rendering
authorJames Moger <james.moger@gitblit.com>
Tue, 4 Mar 2014 18:37:12 +0000 (13:37 -0500)
committerJames Moger <james.moger@gitblit.com>
Tue, 4 Mar 2014 18:37:12 +0000 (13:37 -0500)
.gitbugtraq
src/main/java/com/gitblit/servlet/SyndicationServlet.java
src/main/java/com/gitblit/utils/BugtraqProcessor.java [new file with mode: 0644]
src/main/java/com/gitblit/utils/MessageProcessor.java [deleted file]
src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
src/main/java/com/gitblit/wicket/pages/CommitPage.java
src/main/java/com/gitblit/wicket/pages/DocPage.java
src/main/java/com/gitblit/wicket/pages/RepositoryPage.java
src/main/java/com/gitblit/wicket/pages/TicketPage.java
src/main/java/com/gitblit/wicket/pages/TicketsPage.java

index cd5b228dc8085f8ba47c40777ac7e1a149b2b81e..21f08589bf67e82260ee5ba96a2f7abc2c830064 100644 (file)
@@ -8,3 +8,9 @@
   loglinkregex = "(?:pull request|pull|pr)\\s*[-#]?[0-9]+"\r
   logregex = "\\d+"\r
   loglinktext = "pull request #%BUGID%"\r
+[bugtraq "tickets"]\r
+  url = "https://dev.gitblit.com/tickets/gitblit.git/%BUGID%"\r
+  loglinkregex = "(?:ticket)\\s*[-#]?[0-9]+"\r
+  logregex = "\\d+"\r
+  loglinktext = "ticket #%BUGID%"\r
+  
\ No newline at end of file
index 66ca4a1981f00612bd34417366eab07185f0c559..24def99525db1e1716b3735ddd0c84a1eb8f59b1 100644 (file)
@@ -40,9 +40,9 @@ import com.gitblit.models.RefModel;
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.servlet.AuthenticationFilter.AuthenticatedRequest;\r
+import com.gitblit.utils.BugtraqProcessor;\r
 import com.gitblit.utils.HttpUtils;\r
 import com.gitblit.utils.JGitUtils;\r
-import com.gitblit.utils.MessageProcessor;\r
 import com.gitblit.utils.StringUtils;\r
 import com.gitblit.utils.SyndicationUtils;\r
 \r
@@ -257,7 +257,7 @@ public class SyndicationServlet extends DaggerServlet {
                                                offset, length);\r
                        }\r
                        Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository, model.showRemoteBranches);\r
-                       MessageProcessor processor = new MessageProcessor(settings);\r
+                       BugtraqProcessor processor = new BugtraqProcessor(settings);\r
 \r
                        // convert RevCommit to SyndicatedEntryModel\r
                        for (RevCommit commit : commits) {\r
diff --git a/src/main/java/com/gitblit/utils/BugtraqProcessor.java b/src/main/java/com/gitblit/utils/BugtraqProcessor.java
new file mode 100644 (file)
index 0000000..67f220f
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2013 gitblit.com.
+ *
+ * Licensed 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.
+ */
+package com.gitblit.utils;
+
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.Repository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.IStoredSettings;
+import com.gitblit.Keys;
+import com.gitblit.models.RepositoryModel;
+import com.syntevo.bugtraq.BugtraqConfig;
+import com.syntevo.bugtraq.BugtraqFormatter;
+import com.syntevo.bugtraq.BugtraqFormatter.OutputHandler;
+
+public class BugtraqProcessor {
+
+       private final Logger logger = LoggerFactory.getLogger(getClass());
+
+       private final IStoredSettings settings;
+
+       public BugtraqProcessor(IStoredSettings settings) {
+               this.settings = settings;
+       }
+
+       /**
+        * Returns an html version of the commit message with any global or
+        * repository-specific regular expression substitution applied.
+        *
+        * This method uses the preferred renderer to transform the commit message.
+        *
+        * @param repository
+        * @param model
+        * @param text
+        * @return html version of the commit message
+        */
+       public String processCommitMessage(Repository repository, RepositoryModel model, String text) {
+               switch (model.commitMessageRenderer) {
+               case MARKDOWN:
+                       try {
+                               String prepared = processTextRegex(repository, model.name, text);
+                               return MarkdownUtils.transformMarkdown(prepared);
+                       } catch (Exception e) {
+                               logger.error("Failed to render commit message as markdown", e);
+                       }
+                       break;
+               default:
+                       // noop
+                       break;
+               }
+
+               return processPlainCommitMessage(repository, model.name, text);
+       }
+
+       /**
+        * Returns an html version of the commit message with any global or
+        * repository-specific regular expression substitution applied.
+        *
+        * This method assumes the commit message is plain text.
+        *
+        * @param repository
+        * @param repositoryName
+        * @param text
+        * @return html version of the commit message
+        */
+       public String processPlainCommitMessage(Repository repository, String repositoryName, String text) {
+               String html = StringUtils.escapeForHtml(text, false);
+               html = processTextRegex(repository, repositoryName, html);
+               return StringUtils.breakLinesForHtml(html);
+
+       }
+
+       /**
+        * Returns an processed version of the text with any global or
+        * repository-specific regular expression substitution applied.
+        *
+        * @param repository
+        * @param repositoryName
+        * @param text
+        * @return processed version of the text
+        */
+       public String processText(Repository repository, String repositoryName, String text) {
+               String html = processTextRegex(repository, repositoryName, text);
+               return html;
+       }
+
+       /**
+        * Apply globally or per-repository specified regex substitutions to the
+        * text.
+        *
+        * @param repository
+        * @param repositoryName
+        * @param text
+        * @return the processed text
+        */
+       protected String processTextRegex(Repository repository, String repositoryName, String text) {
+               Map<String, String> map = new HashMap<String, String>();
+               // global regex keys
+               if (settings.getBoolean(Keys.regex.global, false)) {
+                       for (String key : settings.getAllKeys(Keys.regex.global)) {
+                               if (!key.equals(Keys.regex.global)) {
+                                       String subKey = key.substring(key.lastIndexOf('.') + 1);
+                                       map.put(subKey, settings.getString(key, ""));
+                               }
+                       }
+               }
+
+               // repository-specific regex keys
+               List<String> keys = settings.getAllKeys(Keys.regex._ROOT + "."
+                               + repositoryName.toLowerCase());
+               for (String key : keys) {
+                       String subKey = key.substring(key.lastIndexOf('.') + 1);
+                       map.put(subKey, settings.getString(key, ""));
+               }
+
+               for (Entry<String, String> entry : map.entrySet()) {
+                       String definition = entry.getValue().trim();
+                       String[] chunks = definition.split("!!!");
+                       if (chunks.length == 2) {
+                               text = text.replaceAll(chunks[0], chunks[1]);
+                       } else {
+                               logger.warn(entry.getKey()
+                                               + " improperly formatted.  Use !!! to separate match from replacement: "
+                                               + definition);
+                       }
+               }
+
+               try {
+                       // parse bugtraq repo config
+                       BugtraqConfig config = BugtraqConfig.read(repository);
+                       if (config != null) {
+                               BugtraqFormatter formatter = new BugtraqFormatter(config);
+                               StringBuilder sb = new StringBuilder();
+                               formatter.formatLogMessage(text, new BugtraqOutputHandler(sb));
+                               text = sb.toString();
+                       }
+               } catch (IOException e) {
+                       logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e);
+               } catch (ConfigInvalidException e) {
+                       logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e);
+               }
+
+               return text;
+       }
+
+       private class BugtraqOutputHandler implements OutputHandler {
+
+               final StringBuilder sb;
+
+               BugtraqOutputHandler(StringBuilder sb) {
+                       this.sb = sb;
+               }
+
+               @Override
+               public void appendText(String text) {
+                       sb.append(text);
+               }
+
+               @Override
+               public void appendLink(String name, String target) {
+                       sb.append(MessageFormat.format("<a class=\"bugtraq\" href=\"{1}\" target=\"_blank\">{0}</a>", name, target));
+               }
+       }
+}
diff --git a/src/main/java/com/gitblit/utils/MessageProcessor.java b/src/main/java/com/gitblit/utils/MessageProcessor.java
deleted file mode 100644 (file)
index c38e0e8..0000000
+++ /dev/null
@@ -1,171 +0,0 @@
-/*
- * Copyright 2013 gitblit.com.
- *
- * Licensed 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.
- */
-package com.gitblit.utils;
-
-import java.io.IOException;
-import java.text.MessageFormat;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.eclipse.jgit.errors.ConfigInvalidException;
-import org.eclipse.jgit.lib.Repository;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.gitblit.IStoredSettings;
-import com.gitblit.Keys;
-import com.gitblit.models.RepositoryModel;
-import com.syntevo.bugtraq.BugtraqConfig;
-import com.syntevo.bugtraq.BugtraqFormatter;
-import com.syntevo.bugtraq.BugtraqFormatter.OutputHandler;
-
-public class MessageProcessor {
-
-       private final Logger logger = LoggerFactory.getLogger(getClass());
-
-       private final IStoredSettings settings;
-
-       public MessageProcessor(IStoredSettings settings) {
-               this.settings = settings;
-       }
-
-       /**
-        * Returns an html version of the commit message with any global or
-        * repository-specific regular expression substitution applied.
-        *
-        * This method uses the preferred renderer to transform the commit message.
-        *
-        * @param repository
-        * @param model
-        * @param text
-        * @return html version of the commit message
-        */
-       public String processCommitMessage(Repository repository, RepositoryModel model, String text) {
-               switch (model.commitMessageRenderer) {
-               case MARKDOWN:
-                       try {
-                               String prepared = processCommitMessageRegex(repository, model.name, text);
-                               return MarkdownUtils.transformMarkdown(prepared);
-                       } catch (Exception e) {
-                               logger.error("Failed to render commit message as markdown", e);
-                       }
-                       break;
-               default:
-                       // noop
-                       break;
-               }
-
-               return processPlainCommitMessage(repository, model.name, text);
-       }
-
-       /**
-        * Returns an html version of the commit message with any global or
-        * repository-specific regular expression substitution applied.
-        *
-        * This method assumes the commit message is plain text.
-        *
-        * @param repository
-        * @param repositoryName
-        * @param text
-        * @return html version of the commit message
-        */
-       public String processPlainCommitMessage(Repository repository, String repositoryName, String text) {
-               String html = StringUtils.escapeForHtml(text, false);
-               html = processCommitMessageRegex(repository, repositoryName, html);
-               return StringUtils.breakLinesForHtml(html);
-
-       }
-
-       /**
-        * Apply globally or per-repository specified regex substitutions to the
-        * commit message.
-        *
-        * @param repository
-        * @param repositoryName
-        * @param text
-        * @return the processed commit message
-        */
-       protected String processCommitMessageRegex(Repository repository, String repositoryName, String text) {
-               Map<String, String> map = new HashMap<String, String>();
-               // global regex keys
-               if (settings.getBoolean(Keys.regex.global, false)) {
-                       for (String key : settings.getAllKeys(Keys.regex.global)) {
-                               if (!key.equals(Keys.regex.global)) {
-                                       String subKey = key.substring(key.lastIndexOf('.') + 1);
-                                       map.put(subKey, settings.getString(key, ""));
-                               }
-                       }
-               }
-
-               // repository-specific regex keys
-               List<String> keys = settings.getAllKeys(Keys.regex._ROOT + "."
-                               + repositoryName.toLowerCase());
-               for (String key : keys) {
-                       String subKey = key.substring(key.lastIndexOf('.') + 1);
-                       map.put(subKey, settings.getString(key, ""));
-               }
-
-               for (Entry<String, String> entry : map.entrySet()) {
-                       String definition = entry.getValue().trim();
-                       String[] chunks = definition.split("!!!");
-                       if (chunks.length == 2) {
-                               text = text.replaceAll(chunks[0], chunks[1]);
-                       } else {
-                               logger.warn(entry.getKey()
-                                               + " improperly formatted.  Use !!! to separate match from replacement: "
-                                               + definition);
-                       }
-               }
-
-               try {
-                       // parse bugtraq repo config
-                       BugtraqConfig config = BugtraqConfig.read(repository);
-                       if (config != null) {
-                               BugtraqFormatter formatter = new BugtraqFormatter(config);
-                               StringBuilder sb = new StringBuilder();
-                               formatter.formatLogMessage(text, new BugtraqOutputHandler(sb));
-                               text = sb.toString();
-                       }
-               } catch (IOException e) {
-                       logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e);
-               } catch (ConfigInvalidException e) {
-                       logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e);
-               }
-
-               return text;
-       }
-
-       private class BugtraqOutputHandler implements OutputHandler {
-
-               final StringBuilder sb;
-
-               BugtraqOutputHandler(StringBuilder sb) {
-                       this.sb = sb;
-               }
-
-               @Override
-               public void appendText(String text) {
-                       sb.append(text);
-               }
-
-               @Override
-               public void appendLink(String name, String target) {
-                       sb.append(MessageFormat.format("<a class=\"bugtraq\" href=\"{1}\" target=\"_blank\">{0}</a>", name, target));
-               }
-       }
-}
index f3f6175357272ad007ee690da61684017d84ee87..7f2a8a618ca99db95f2cd7e476d5e3b46557a4f9 100644 (file)
@@ -107,7 +107,7 @@ public class CommitDiffPage extends RepositoryPage {
                                item.add(new GravatarImage("noteAuthorAvatar", entry.notesRef.getAuthorIdent()));
                                item.add(WicketUtils.createTimestampLabel("authorDate", entry.notesRef
                                                .getAuthorIdent().getWhen(), getTimeZone(), getTimeUtils()));
-                               item.add(new Label("noteContent", messageProcessor().processPlainCommitMessage(getRepository(), repositoryName,
+                               item.add(new Label("noteContent", bugtraqProcessor().processPlainCommitMessage(getRepository(), repositoryName,
                                                entry.content)).setEscapeModelStrings(false));
                        }
                };
index 0998c714d6830e1bd8089940903deabb2c7d00b9..8bc984893d6880a5d37a8e220009dba5347ca760 100644 (file)
@@ -135,7 +135,7 @@ public class CommitPage extends RepositoryPage {
                                item.add(new GravatarImage("noteAuthorAvatar", entry.notesRef.getAuthorIdent()));\r
                                item.add(WicketUtils.createTimestampLabel("authorDate", entry.notesRef\r
                                                .getAuthorIdent().getWhen(), getTimeZone(), getTimeUtils()));\r
-                               item.add(new Label("noteContent", messageProcessor().processPlainCommitMessage(getRepository(), repositoryName,\r
+                               item.add(new Label("noteContent", bugtraqProcessor().processPlainCommitMessage(getRepository(), repositoryName,\r
                                                entry.content)).setEscapeModelStrings(false));\r
                        }\r
                };\r
index 03d233728bae5aed0911999bea535a91e3b9b9d9..875765ffdb6df6b5f4b512c39b46e542c2f960f3 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.wicket.markup.html.panel.Fragment;
 import org.eclipse.jgit.lib.Repository;\r
 import org.eclipse.jgit.revwalk.RevCommit;\r
 \r
+import com.gitblit.utils.BugtraqProcessor;\r
 import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.StringUtils;\r
 import com.gitblit.wicket.CacheControl;\r
@@ -66,6 +67,9 @@ public class DocPage extends RepositoryPage {
                        }\r
                }\r
 \r
+               BugtraqProcessor bugtraq = new BugtraqProcessor(app().settings());\r
+               markupText = bugtraq.processText(getRepository(), repositoryName, markupText);\r
+\r
                Fragment fragment;\r
                MarkupDocument markupDoc = processor.parse(repositoryName, getBestCommitId(commit), documentPath, markupText);\r
                if (MarkupSyntax.PLAIN.equals(markupDoc.syntax)) {\r
index 86df4565220937184af0e5ed9e8de9ac10099558..59cbdc893ada43e40e76822fadd3fc18bd656bad 100644 (file)
@@ -58,9 +58,9 @@ import com.gitblit.servlet.PagesServlet;
 import com.gitblit.servlet.SyndicationServlet;\r
 import com.gitblit.tickets.TicketIndexer.Lucene;\r
 import com.gitblit.utils.ArrayUtils;\r
+import com.gitblit.utils.BugtraqProcessor;\r
 import com.gitblit.utils.DeepCopier;\r
 import com.gitblit.utils.JGitUtils;\r
-import com.gitblit.utils.MessageProcessor;\r
 import com.gitblit.utils.RefLogUtils;\r
 import com.gitblit.utils.StringUtils;\r
 import com.gitblit.wicket.CacheControl;\r
@@ -177,8 +177,8 @@ public abstract class RepositoryPage extends RootPage {
                return getClass();\r
        }\r
 \r
-       protected MessageProcessor messageProcessor() {\r
-               return new MessageProcessor(app().settings());\r
+       protected BugtraqProcessor bugtraqProcessor() {\r
+               return new BugtraqProcessor(app().settings());\r
        }\r
 \r
        private Map<String, PageRegistration> registerPages() {\r
@@ -531,7 +531,7 @@ public abstract class RepositoryPage extends RootPage {
 \r
        protected void addFullText(String wicketId, String text) {\r
                RepositoryModel model = getRepositoryModel();\r
-               String content = messageProcessor().processCommitMessage(r, model, text);\r
+               String content = bugtraqProcessor().processCommitMessage(r, model, text);\r
                String html;\r
                switch (model.commitMessageRenderer) {\r
                case MARKDOWN:\r
index 0d60ec202bdb3d40163727388f2cc7a79954617b..3eb88dd5ab1dfd44812c62601e962b50f2776f13 100644 (file)
@@ -491,7 +491,7 @@ public class TicketPage extends TicketBasePage {
                        add(new Label("ticketTopic").setVisible(false));\r
                } else {\r
                        // process the topic using the bugtraq config to link things\r
-                       String topic = messageProcessor().processPlainCommitMessage(getRepository(), repositoryName, ticket.topic);\r
+                       String topic = bugtraqProcessor().processPlainCommitMessage(getRepository(), repositoryName, ticket.topic);\r
                        add(new Label("ticketTopic", topic).setEscapeModelStrings(false));\r
                }\r
 \r
index 525658c524b0aae45a4dc91b578b2fd11e3a6fee..2d60481f4cc0771904f956b531afb0e43e81694e 100644 (file)
@@ -529,7 +529,7 @@ public class TicketsPage extends TicketBasePage {
 \r
                                        @Override\r
                                        public void populateItem(final Item<String> labelItem) {\r
-                                               String content = messageProcessor().processPlainCommitMessage(getRepository(), repositoryName, labelItem.getModelObject());\r
+                                               String content = bugtraqProcessor().processPlainCommitMessage(getRepository(), repositoryName, labelItem.getModelObject());\r
                                                Label label = new Label("label", content);\r
                                                label.setEscapeModelStrings(false);\r
                                                TicketLabel tLabel = app().tickets().getLabel(getRepositoryModel(), labelItem.getModelObject());\r