SVN post-commit in C#–Send mail on commit
Basically, any executable file you place in \path\to\repository\hooks will trigger when ever you commit. It can be a post-commit.bat or post-commit.exe.
Below script will send you a mail notification in this format:
Subject: [MyProject SVN] rev:14 user:praveen Revision: 14 Repo: D:\Repositories\testrepo Server Time:18-Apr-2012 01:06:08 A trunk/menu-sharing.txt A trunk/menu-shar.ini
This is a console applicaiton. Also sorry for the poor exception handling. This is just for demonstration purpose
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; using System.Net; using System.Diagnostics; using System.IO; using System.Reflection; namespace post_commit { class Program { static void Main(string[] args) { try { string str = string.Empty; string user = string.Empty; StringBuilder sb = new StringBuilder(); foreach (string s in args) { str += s + " | "; } user = ExecuteProcess("author -r " + args[1] + " " + args[0]); sb.Append("Revision: " + args[1] + "\r\nRepo: " + args[0] + "\r\n"); sb.Append("Server Time:" + DateTime.Now.ToString ("dd-MMM-yyyy hh:mm:ss")); str = ExecuteProcess("changed -r " + args[1] + " " + args[0]); sb.Append("\r\n\r\n" + str); SendMail(user.Replace("\r\n", string.Empty), args[1], sb.ToString()); } catch { } } private static string ExecuteProcess(string arg) { ProcessStartInfo psinfo = new ProcessStartInfo(); // make sure you have svnlook.exe available in your machine. // Take care of your path. psinfo.FileName = Path.GetDirectoryName(Assembly .GetExecutingAssembly().Location) + "\\bin\\svnlook.exe"; psinfo.Arguments = arg; psinfo.UseShellExecute = false; psinfo.RedirectStandardOutput = true; string str = string.Empty; using (Process exeProcess = Process.Start(psinfo)) { exeProcess.WaitForExit(); str = exeProcess.StandardOutput.ReadToEnd(); } return str; } private static void SendMail(string user, string rev, string content) { try { MailMessage mail = new MailMessage(); mail.From = new MailAddress("noreply@mymailpraveen.com"); mail.Subject = "[MyProject SVN] rev:" + rev + " user:" + user; mail.Priority = MailPriority.Low; // I made a generic emails.txt for you to add any number of // emails without touching the project IEnumerable<string> emails; string emailsfile = Path.GetDirectoryName(Assembly .GetExecutingAssembly().Location) + "\\emails.txt"; emails = File.ReadLines(emailsfile); foreach (var email in emails) { mail.To.Add(email); } mail.IsBodyHtml = false; mail.Body = content; SmtpClient smtp = new SmtpClient(); smtp.Host = "mail.gmi-projects.com"; smtp.Credentials = new NetworkCredential( "noreply@mysmtphostpraveen","mypassword"); smtp.Send(mail); } catch { } } } }
No related posts.
Categories: C#
Recent Comments