From 0dfb9845d7b5877bd0273a931121209b7cea4c86 Mon Sep 17 00:00:00 2001
From: Berke Viktor <sundayfunday1234@outlook.com>
Date: Tue, 28 Jan 2014 12:09:28 -0500
Subject: [PATCH] Add Redmine Fetch groovy hook (issue-359)

---
 releases.moxie                                    |    2 +
 build.xml                                         |    1 
 src/main/distrib/data/groovy/redmine-fetch.groovy |   68 ++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/build.xml b/build.xml
index 03bbd2c..ac4e541 100644
--- a/build.xml
+++ b/build.xml
@@ -958,6 +958,7 @@
 					<include name="fogbugz.groovy" />
 					<include name="thebuggenie.groovy" />
 					<include name="fisheye.groovy" />
+					<include name="redmine-fetch.groovy" />
 				</fileset>
 			</copy>
       </sequential>
diff --git a/releases.moxie b/releases.moxie
index e315508..1dac38e 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -57,6 +57,7 @@
 	- Added GO setting to automatically redirect all http requests to the secure https connector
 	- Automatically display common repository root documents as tabs on the docs page
 	- Support bugtraq configuration in collaboration with syntevo
+	- Added Redmine Fetch hook script (issue-359)
     dependencyChanges:
 	- updated to Jetty 8.1.13
 	- updated to JGit 3.2.0
@@ -96,6 +97,7 @@
 	- Alfred Schmid
 	- Gareth Collins
 	- Martijn van der Kleijn
+	- Berke Viktor
 }
 
 #
diff --git a/src/main/distrib/data/groovy/redmine-fetch.groovy b/src/main/distrib/data/groovy/redmine-fetch.groovy
new file mode 100644
index 0000000..6073835
--- /dev/null
+++ b/src/main/distrib/data/groovy/redmine-fetch.groovy
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2014 Berke Viktor.
+ *
+ * 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.
+ */
+import com.gitblit.GitBlit
+import com.gitblit.Keys
+import com.gitblit.models.RepositoryModel
+import com.gitblit.models.UserModel
+import com.gitblit.utils.JGitUtils
+import org.eclipse.jgit.lib.Repository
+import org.slf4j.Logger
+
+/*
+ * This script triggers automatic repo fetches in Redmine upon pushes.
+ * It won't work out-of-box, you need to configure a few things.
+ *
+ * Redmine
+ *  - Go to Administration / Settings / Repositories, and make sure that the
+ *    "Enable WS for repository management" option is checked. Also generate an
+ *    API key and take note of it.
+ *  - Open a project page, go to Settings / Repositories and add a repo. Take
+ *    note of the Identifier.
+ *
+ * Gitblit
+ *  - Set the redmineProject custom field in gitblit.properties, e.g.
+ *    groovy.customFields = "redmineProject=Redmine Project Identifier"
+ *  - Edit the repo you added to Redmine, go to hook scripts and add this script
+ *    to the post-receive hooks. Also specify the Redmine project's identifier
+ *    under custom fields.
+ *
+ * Troubleshooting
+ *  - run Gitblit interactively and check its console output
+ *  - on the Redmine server, tail -f log/access.log
+ *
+ * If you want your repos to work with multiple Redmine projects, you don't need
+ * to add the repos to all of them. Instead, add the repo to a single project,
+ * then go to Administration / Settings / Repositories and enable the "Allow
+ * issues of all the other projects to be referenced and fixed" option.
+ */
+
+/* specify the URL of your Redmine instance here */
+def redmineURL = "http://redmine.foo.bar/"
+
+/* specify the API key you generated in Redmine Administration here */
+def apiKey = "FIXME"
+
+/*
+ * construct the URL from global and repo properties, for more info refer to
+ * http://www.redmine.org/projects/redmine/wiki/RedmineSettings#Fetch-commits-automatically
+ */
+def triggerURL = redmineURL + "sys/fetch_changesets?id=" + repository.customFields.redmineProject + "&key=" + apiKey
+
+/* log the action */
+logger.info("Redmine Fetch hook triggered by ${user.username} for ${repository.name}: GET ${triggerURL}")
+
+/* send the HTTP GET query */
+new URL(triggerURL).getContent()

--
Gitblit v1.9.1