From df162cdbdfeb5fbf5500546c9783e1685be6980f Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 04 Jan 2012 08:42:54 -0500
Subject: [PATCH] Federation pull_scripts request. Documentation.

---
 src/com/gitblit/FederationPullExecutor.java |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 46 insertions(+), 1 deletions(-)

diff --git a/src/com/gitblit/FederationPullExecutor.java b/src/com/gitblit/FederationPullExecutor.java
index 2976c40..d54395e 100644
--- a/src/com/gitblit/FederationPullExecutor.java
+++ b/src/com/gitblit/FederationPullExecutor.java
@@ -50,6 +50,7 @@
 import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.FederationUtils;
+import com.gitblit.utils.FileUtils;
 import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.JGitUtils.CloneResult;
 import com.gitblit.utils.StringUtils;
@@ -281,6 +282,8 @@
 			r.close();
 		}
 
+		IUserService userService = null;
+
 		try {
 			// Pull USERS
 			// TeamModels are automatically pulled because they are contained
@@ -290,7 +293,7 @@
 			if (users != null && users.size() > 0) {
 				File realmFile = new File(registrationFolderFile, registration.name + "_users.conf");
 				realmFile.delete();
-				ConfigUserService userService = new ConfigUserService(realmFile);
+				userService = new ConfigUserService(realmFile);
 				for (UserModel user : users) {
 					userService.updateUserModel(user.username, user);
 
@@ -358,6 +361,27 @@
 		}
 
 		try {
+			// Pull TEAMS
+			// We explicitly pull these even though they are embedded in
+			// UserModels because it is possible to use teams to specify
+			// mailing lists or push scripts without specifying users.
+			if (userService != null) {
+				Collection<TeamModel> teams = FederationUtils.getTeams(registration);
+				if (teams != null && teams.size() > 0) {
+					for (TeamModel team : teams) {
+						userService.updateTeamModel(team);
+					}
+				}
+			}
+		} catch (ForbiddenException e) {
+			// ignore forbidden exceptions
+		} catch (IOException e) {
+			logger.warn(MessageFormat.format(
+					"Failed to retrieve TEAMS from federated gitblit ({0} @ {1})",
+					registration.name, registration.url), e);
+		}
+
+		try {
 			// Pull SETTINGS
 			Map<String, String> settings = FederationUtils.getSettings(registration);
 			if (settings != null && settings.size() > 0) {
@@ -375,6 +399,27 @@
 					"Failed to retrieve SETTINGS from federated gitblit ({0} @ {1})",
 					registration.name, registration.url), e);
 		}
+		
+		try {
+			// Pull SCRIPTS
+			Map<String, String> scripts = FederationUtils.getScripts(registration);
+			if (scripts != null && scripts.size() > 0) {
+				for (Map.Entry<String, String> script : scripts.entrySet()) {
+					String scriptName = script.getKey();
+					if (scriptName.endsWith(".groovy")) {
+						scriptName = scriptName.substring(0,  scriptName.indexOf(".groovy"));
+					}
+					File file = new File(registrationFolderFile, registration.name + "_" + scriptName + ".groovy");
+					FileUtils.writeContent(file, script.getValue());
+				}
+			}
+		} catch (ForbiddenException e) {
+			// ignore forbidden exceptions
+		} catch (IOException e) {
+			logger.warn(MessageFormat.format(
+					"Failed to retrieve SCRIPTS from federated gitblit ({0} @ {1})",
+					registration.name, registration.url), e);
+		}
 	}
 
 	/**

--
Gitblit v1.9.1