From beb021472d034617e1ce216aee38d918ae7f1a67 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 31 Mar 2014 11:45:21 -0400
Subject: [PATCH] Configure Tickets close-on-push commit message regex (issue-404)

---
 src/main/java/com/gitblit/git/PatchsetReceivePack.java |   19 ++++++++++++++-----
 releases.moxie                                         |    2 ++
 src/main/distrib/data/gitblit.properties               |    7 +++++++
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/releases.moxie b/releases.moxie
index 11d2a41..fc679ef 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -15,6 +15,7 @@
     - Ensure the Lucene ticket index is updated on repository deletion.
     changes:
     - Specify the --dailyLogFile option for the Ubuntu and CentOS service scripts (issue-348)
+    - The ticket close-on-push commit message regular expression is now configurable by a setting (issue-404)
     - Option to allow LDAP users to directly authenticate without performing LDAP searches (pr-162)
     - Replace JCommander with args4j to be consistent with other tools (ticket-28)
     additions:
@@ -29,6 +30,7 @@
     - Jeremie Brebec
     settings:
     - { name: 'realm.ldap.bindpattern', defaultValue: ' ' }
+    - { name: 'tickets.closeOnPushCommitMessageRegex', defaultValue: '(?:fixes|closes)[\\s-]+#?(\\d+)' }
 }
 
 #
diff --git a/src/main/distrib/data/gitblit.properties b/src/main/distrib/data/gitblit.properties
index b819b38..3c60539 100644
--- a/src/main/distrib/data/gitblit.properties
+++ b/src/main/distrib/data/gitblit.properties
@@ -479,6 +479,13 @@
 # SINCE 1.4.0
 tickets.requireApproval = false
 
+# The case-insensitive regular expression used to identify and close tickets on
+# push to the integration branch for commits that are NOT already referenced as
+# a patchset tip.
+#
+# SINCE 1.5.0
+tickets.closeOnPushCommitMessageRegex = (?:fixes|closes)[\\s-]+#?(\\d+)
+
 # Specify the location of the Lucene Ticket index
 #
 # SINCE 1.4.0
diff --git a/src/main/java/com/gitblit/git/PatchsetReceivePack.java b/src/main/java/com/gitblit/git/PatchsetReceivePack.java
index 1d3312a..64a739e 100644
--- a/src/main/java/com/gitblit/git/PatchsetReceivePack.java
+++ b/src/main/java/com/gitblit/git/PatchsetReceivePack.java
@@ -897,11 +897,20 @@
 
 		if (parseMessage) {
 			// parse commit message looking for fixes/closes #n
-			Pattern p = Pattern.compile("(?:fixes|closes)[\\s-]+#?(\\d+)", Pattern.CASE_INSENSITIVE);
-			Matcher m = p.matcher(commit.getFullMessage());
-			while (m.find()) {
-				String val = m.group(1);
-				return Long.parseLong(val);
+			String dx = "(?:fixes|closes)[\\s-]+#?(\\d+)";
+			String x = settings.getString(Keys.tickets.closeOnPushCommitMessageRegex, dx);
+			if (StringUtils.isEmpty(x)) {
+				x = dx;
+			}
+			try {
+				Pattern p = Pattern.compile(x, Pattern.CASE_INSENSITIVE);
+				Matcher m = p.matcher(commit.getFullMessage());
+				while (m.find()) {
+					String val = m.group(1);
+					return Long.parseLong(val);
+				}
+			} catch (Exception e) {
+				LOGGER.error(String.format("Failed to parse \"%s\" in commit %s", x, commit.getName()), e);
 			}
 		}
 		return 0L;

--
Gitblit v1.9.1