Skip to content

Commit 9a51d2b

Browse files
committed
Merge branch 'fixMentionsInTickets-985'
2 parents 6b0ac41 + 7985115 commit 9a51d2b

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

src/main/java/com/gitblit/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public class Constants {
6262
public static final String GIT_PATH = "/git/";
6363

6464
public static final String REGEX_SHA256 = "[a-fA-F0-9]{64}";
65+
66+
/**
67+
* This regular expression is used when searching for "mentions" in tickets
68+
* (when someone writes @thisOtherUser)
69+
*/
70+
public static final String REGEX_TICKET_MENTION = "\\B@(?<user>[^\\s]+)\\b";
6571

6672
public static final String ZIP_PATH = "/zip/";
6773

src/main/java/com/gitblit/models/TicketModel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
import org.eclipse.jgit.util.RelativeDateFormatter;
4545

46+
import com.gitblit.Constants;
47+
4648
/**
4749
* The Gitblit Ticket model, its component classes, and enums.
4850
*
@@ -773,10 +775,10 @@ public Comment comment(String text) {
773775
}
774776

775777
try {
776-
Pattern mentions = Pattern.compile("\\s@([A-Za-z0-9-_]+)");
778+
Pattern mentions = Pattern.compile(Constants.REGEX_TICKET_MENTION);
777779
Matcher m = mentions.matcher(text);
778780
while (m.find()) {
779-
String username = m.group(1);
781+
String username = m.group("user");
780782
plusList(Field.mentions, username);
781783
}
782784
} catch (Exception e) {

src/main/java/com/gitblit/tickets/TicketNotifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,10 @@ protected void setRecipients(TicketModel ticket, Mailing mailing) {
573573
// cc users mentioned in last comment
574574
Change lastChange = ticket.changes.get(ticket.changes.size() - 1);
575575
if (lastChange.hasComment()) {
576-
Pattern p = Pattern.compile("\\s@([A-Za-z0-9-_]+)");
576+
Pattern p = Pattern.compile(Constants.REGEX_TICKET_MENTION);
577577
Matcher m = p.matcher(lastChange.comment.text);
578578
while (m.find()) {
579-
String username = m.group();
579+
String username = m.group("user");
580580
ccs.add(username);
581581
}
582582
}

src/main/java/com/gitblit/utils/MarkdownUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.pegdown.PegDownProcessor;
3131
import org.pegdown.ast.RootNode;
3232

33+
import com.gitblit.Constants;
3334
import com.gitblit.IStoredSettings;
3435
import com.gitblit.Keys;
3536
import com.gitblit.wicket.MarkupProcessor.WorkaroundHtmlSerializer;
@@ -137,8 +138,8 @@ public static String transformGFM(IStoredSettings settings, String input, String
137138
String canonicalUrl = settings.getString(Keys.web.canonicalUrl, "https://localhost:8443");
138139

139140
// emphasize and link mentions
140-
String mentionReplacement = String.format(" **[@$1](%1s/user/$1)**", canonicalUrl);
141-
text = text.replaceAll("\\s@([A-Za-z0-9-_]+)", mentionReplacement);
141+
String mentionReplacement = String.format("**[@${user}](%1s/user/${user})**", canonicalUrl);
142+
text = text.replaceAll(Constants.REGEX_TICKET_MENTION, mentionReplacement);
142143

143144
// link ticket refs
144145
String ticketReplacement = MessageFormat.format("$1[#$2]({0}/tickets?r={1}&h=$2)$3", canonicalUrl, repositoryName);

src/test/java/com/gitblit/tests/MarkdownUtilsTest.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package com.gitblit.tests;
1717

18+
import java.util.HashMap;
19+
import java.util.Map;
20+
1821
import org.junit.Test;
1922

23+
import com.gitblit.IStoredSettings;
24+
import com.gitblit.Keys;
25+
import com.gitblit.tests.mock.MemorySettings;
2026
import com.gitblit.utils.MarkdownUtils;
2127

2228
public class MarkdownUtilsTest extends GitblitUnitTest {
@@ -39,4 +45,70 @@ public void testMarkdown() throws Exception {
3945
assertEquals("<table><tr><td>&lt;test&gt;</td></tr></table>",
4046
MarkdownUtils.transformMarkdown("<table><tr><td>&lt;test&gt;</td></tr></table>"));
4147
}
42-
}
48+
49+
50+
@Test
51+
public void testUserMentions() {
52+
IStoredSettings settings = getSettings();
53+
String repositoryName = "test3";
54+
String mentionHtml = "<strong><a href=\"http://localhost/user/%1$s\">@%1$s</a></strong>";
55+
56+
String input = "@j.doe";
57+
String output = "<p>" + String.format(mentionHtml, "j.doe") + "</p>";
58+
assertEquals(output, MarkdownUtils.transformGFM(settings, input, repositoryName));
59+
60+
input = " @j.doe";
61+
output = "<p>" + String.format(mentionHtml, "j.doe") + "</p>";
62+
assertEquals(output, MarkdownUtils.transformGFM(settings, input, repositoryName));
63+
64+
input = "@j.doe.";
65+
output = "<p>" + String.format(mentionHtml, "j.doe") + ".</p>";
66+
assertEquals(output, MarkdownUtils.transformGFM(settings, input, repositoryName));
67+
68+
input = "To @j.doe: ask @jim.beam!";
69+
output = "<p>To " + String.format(mentionHtml, "j.doe")
70+
+ ": ask " + String.format(mentionHtml, "jim.beam") + "!</p>";
71+
assertEquals(output, MarkdownUtils.transformGFM(settings, input, repositoryName));
72+
73+
input = "@sta.rt\n"
74+
+ "\n"
75+
+ "User mentions in tickets are broken.\n"
76+
+ "So:\n"
77+
+ "@mc_guyver can fix this.\n"
78+
+ "@j.doe, can you test after the fix by @m+guyver?\n"
79+
+ "Please review this, @jim.beam!\n"
80+
+ "Was reported by @jill and @j!doe from jane@doe yesterday.\n"
81+
+ "\n"
82+
+ "@jack.daniels can vote for [email protected] hopefully.\n"
83+
+ "@en.de";
84+
output = "<p>" + String.format(mentionHtml, "sta.rt") + "</p>"
85+
+ "<p>" + "User mentions in tickets are broken.<br/>"
86+
+ "So:<br/>"
87+
+ String.format(mentionHtml, "mc_guyver") + " can fix this.<br/>"
88+
+ String.format(mentionHtml, "j.doe") + ", can you test after the fix by " + String.format(mentionHtml, "m+guyver") + "?<br/>"
89+
+ "Please review this, " + String.format(mentionHtml, "jim.beam") + "!<br/>"
90+
+ "Was reported by " + String.format(mentionHtml, "jill")
91+
+ " and " + String.format(mentionHtml, "j!doe")
92+
+ " from <a href=\"mailto:&#106;a&#110;&#x65;&#x40;&#x64;&#x6f;&#101;\">&#106;a&#110;&#x65;&#x40;&#x64;&#x6f;&#101;</a> yesterday."
93+
+ "</p>"
94+
+ "<p>" + String.format(mentionHtml, "jack.daniels") + " can vote for "
95+
+ "<a href=\"mailto:&#x6a;&#x6f;h&#110;&#x40;&#119;a&#121;&#110;&#101;.&#110;a&#x6d;&#101;\">&#x6a;&#x6f;h&#110;&#x40;&#119;a&#121;&#110;&#101;.&#110;a&#x6d;&#101;</a> hopefully.<br/>"
96+
+ String.format(mentionHtml, "en.de")
97+
+ "</p>";
98+
assertEquals(output, MarkdownUtils.transformGFM(settings, input, repositoryName));
99+
100+
}
101+
102+
103+
104+
105+
private MemorySettings getSettings() {
106+
Map<String, Object> backingMap = new HashMap<String, Object>();
107+
108+
backingMap.put(Keys.web.canonicalUrl, "http://localhost");
109+
backingMap.put(Keys.web.shortCommitIdLength, "7");
110+
111+
MemorySettings ms = new MemorySettings(backingMap);
112+
return ms;
113+
}
114+
}

0 commit comments

Comments
 (0)