Advertisement
Guest User

Redmine + Subversion PHP automation script

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