Advertisement
Guest User

Redmine + Subversion PHP automation script

a guest
Jan 25th, 2011
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. <?php
  2. include("Mail.php");
  3.  
  4. /*
  5. * Author: Fernando Possebon
  6. * Date: 01/25/2010
  7. * Credits: This script was developed based on script posted by aadreja on his blog
  8. * http://aadreja.blogspot.com/2010/08/subversion-redmine-ldap-automatically.html
  9. */
  10.  
  11. #Setup configuration
  12. $redmineURL = "";
  13. $redmineDatabase = "redminedevelopment";
  14. $redmineDatabaseServer = "localhost";
  15. $redmineDatabaseUser = "redminedev";
  16. $redmineDatabasePassword = "dbPassword";
  17. $svnpath = "/opt/svndevelopment";
  18. $svnrepotemplate = "/opt/redmine-additionals/svntemplaterepodev.dump";
  19. $SCMType = "Subversion";
  20. $svnURLRoot = "http://svndev.com/svn/";
  21. $SVNReadOnlyUser = "redmine.svn";
  22. $SVNReadOnlyPassword = "svnPassword";
  23. $logMessage ="";
  24. $mailFrom = "redmineadmin@localhost";
  25. $mailHost = "192.168.1.25";
  26. $mailPort = "25";
  27. $mailAuth = false;
  28. $mailUsername = "smtpusername";
  29. $mailPassword = "smtppassword";
  30. $mailSubject = "[Redmine & Subversion] - Results from script";
  31. $apacheUser = "apache";
  32. $apacheGroup = "apache";
  33.  
  34. date_default_timezone_set('America/Sao_Paulo');
  35.  
  36. $authfile = "/etc/svndev-acl-conf"; //path of AuthzSVNAccessFile
  37.  
  38. /* Tables and columns used
  39. * repositories (id, project_id, url, login, password, root_url, type)
  40. * projects (id, name, identifier, status)
  41. * users (id, login, mail, status, admin)
  42. */
  43.  
  44. //Read projects database from Redmine
  45. //only projects that there is no repository configured on database
  46. //and for projects active i.e. status=1
  47. $redmine_link =mysql_connect($redmineDatabaseServer, $redmineDatabaseUser, $redmineDatabasePassword) or die("Couldn't connect to MySQL Server on $myServer");
  48.  
  49. if (!mysql_select_db($redmineDatabase, $redmine_link)) {
  50. echo 'Could not select database';
  51. exit;
  52. }
  53.  
  54. $query = "select id,name,identifier from projects where status=1 and id not in (select distinct project_id from repositories)";
  55. $result = mysql_query($query,$redmine_link);
  56.  
  57. while ($row = mysql_fetch_assoc($result)) {
  58.  
  59. $projectid = $row["id"];
  60. $projectident = $row["identifier"];
  61.  
  62. $svnprojectpath = $svnpath . "/" . $projectident;
  63.  
  64. //First create the repository
  65. system("/usr/bin/svnadmin create " . $svnprojectpath,$logMessage);
  66.  
  67. //Load the template (directory structures and SVN properties)
  68. system("/usr/bin/svnadmin load --ignore-uuid " . $svnprojectpath . " < " . $svnrepotemplate,$logMessage);
  69.  
  70. // Insert repositories information on Redmine database
  71. $queryinsert = "insert into repositories (project_id, url, login, password, root_url, type) values (" . $projectid . ",'" . $svnURLRoot . $projectident . "','" . $SVNReadOnlyUser . "','" . $SVNReadOnlyPassword . "','" . $svnURLRoot . $projectident . "','" . $SCMType . "')";
  72. $resultinsert = mysql_query($queryinsert,$redmine_link);
  73.  
  74. $logMessage = $logMessage . "\n" . "Repository for " . $projectident . " configured on Redmine.\n";
  75. }
  76.  
  77. // Change the ownership of Subversion folders to apache user
  78. system("chown " . $apacheUser . ":" . $apacheGroup . " " . $svnpath . " -R",$logMessage);
  79.  
  80. mysql_free_result($result);
  81.  
  82. //Set the permissions on ACL file (project members)
  83. //Every administrator user on Redmine will be granted on subversion
  84. //as administrator
  85.  
  86. $admin_users = NULL;
  87.  
  88. $fp = fopen($authfile, "w");
  89. fwrite($fp, "# Subversion authorization file created by script \n");
  90.  
  91. #section [groups]
  92. fwrite($fp, "[groups]\n");
  93. $queryadmin = "select login from users where admin=1 and status=1";
  94. $resultadmin = mysql_query($queryadmin,$redmine_link);
  95.  
  96. while ($adminrow = mysql_fetch_assoc($resultadmin)) {
  97.  
  98. $admin_users = $admin_users . ($admin_users != NULL ? ", " : "") . $adminrow["login"];
  99.  
  100. }
  101.  
  102. mysql_free_result($resultadmin);
  103.  
  104. $group_name = "svnadmin";
  105.  
  106. fwrite($fp, "$group_name = $admin_users \n");
  107.  
  108. #create groups from redmine projects
  109. $query = "select id,name,identifier from projects";
  110. $result = mysql_query($query,$redmine_link);
  111.  
  112. while ($row = mysql_fetch_assoc($result)) {
  113.  
  114. $group_name = $row["identifier"];
  115. $group_id = $row["id"];
  116. $group_users = NULL;
  117.  
  118. //get users for this group
  119. $query = "select b.id, b.login from members a, users b where a.user_id=b.id AND a.project_id=$group_id and b.status=1";
  120. $usvn_users_result = mysql_query($query,$redmine_link);
  121.  
  122. while ($userrow = mysql_fetch_assoc($usvn_users_result)) {
  123.  
  124. $group_users = $group_users . ($group_users != NULL ? ", " : "") . $userrow["login"];
  125.  
  126. }
  127.  
  128. mysql_free_result($usvn_users_result);
  129.  
  130. fwrite($fp, "$group_name = $group_users \n");
  131.  
  132. }
  133.  
  134. mysql_free_result($result);
  135.  
  136. #section [project]
  137. fwrite($fp, "\n\n#Projects from Redmine \n");
  138.  
  139. #First give permission to SVNAdmin group
  140. fwrite($fp, "#Project / \n");
  141. fwrite($fp, "[:/] \n");
  142. fwrite($fp, "@svnadmin = rw \n\n");
  143.  
  144. # Now add the rest
  145. $query = "select id,name,identifier from projects";
  146. $result = mysql_query($query,$redmine_link);
  147.  
  148. while ($row = mysql_fetch_assoc($result)) {
  149.  
  150. $project_name = $row["name"];
  151. $project_id = $row["id"];
  152. $project_identifier = $row["identifier"];
  153.  
  154. fwrite($fp, "#Project $project_name \n");
  155. fwrite($fp, "[$project_identifier:/] \n");
  156. fwrite($fp, "@$project_identifier = rw \n\n");
  157. }
  158.  
  159. mysql_free_result($result);
  160.  
  161. fclose($fp);
  162.  
  163. print("SVN Auth file $authfile generated sucessfully\n");
  164.  
  165. $logMessage = $logMessage . "\nSVN Auth file $authfile generated sucessfully\n";
  166.  
  167. //Send an email to Redmine administrators with the results
  168.  
  169. $queryadmin = "select mail from users where admin=1 and status=1";
  170. $resultadmin = mysql_query($queryadmin,$redmine_link);
  171.  
  172. while ($adminrow = mysql_fetch_assoc($resultadmin)) {
  173.  
  174. $admin_users = $admin_users . ($admin_users != NULL ? ", " : "") . $adminrow["mail"];
  175.  
  176. }
  177.  
  178. mysql_free_result($resultadmin);
  179.  
  180. /* mail setup recipients, subject etc */
  181. $recipients = $admin_users;
  182.  
  183. $from = $mailFrom;
  184. $to = $recipients;
  185. $subject = $mailSubject;
  186. $body = $logMessage;
  187.  
  188. $host = $mailHost;
  189. $username = $mailUsername;
  190. $password = $mailPassword;
  191.  
  192. $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => $mailAuth, 'username' => $username, 'password' => $password));
  193.  
  194. $mail = $smtp->send($to, $headers, $body);
  195.  
  196. if (PEAR::isError($mail)) {
  197. echo("" . $mail->getMessage() . "");
  198. }
  199. else {
  200. echo("Message successfully sent!");
  201. }
  202.  
  203. mysql_close($redmine_link);
  204.  
  205. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement