rename.2.tfsbot
[tfs.git] / tools / tfsbot / tfsbot.cs
blob7dceaa5521219693548ce2a050c8163800246fff
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.Specialized;
5 using System.Configuration;
6 using System.IO;
7 using System.Net;
8 using System.Text;
9 using System.Threading;
11 using Meebey.SmartIrc4net;
12 using Microsoft.TeamFoundation.Client;
13 using Microsoft.TeamFoundation.VersionControl.Client;
14 using Microsoft.TeamFoundation.VersionControl.Common;
15 using OpenTF.Common;
17 class NotifierDaemon
19 private IrcClient irc = new IrcClient();
20 private int port = 6667;
22 private static void Main(string[] arguments)
24 NotifierDaemon nd = new NotifierDaemon();
25 nd.Run();
28 private void UpdateLatestChangset(int cid)
30 // Open App.Config of executable
31 System.Configuration.Configuration config =
32 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
34 KeyValueConfigurationCollection settings = config.AppSettings.Settings;
35 settings.Remove("tfs.changeset");
36 settings.Add("tfs.changeset", cid.ToString());
38 // Save the configuration file.
39 config.Save(ConfigurationSaveMode.Modified);
42 public void Run()
44 if (AppSettings.LatestChangesetId == 0)
45 UpdateLatestChangset(VersionControlServer.GetLatestChangesetId() - 1);
47 // Force a reload of a changed section.
48 ConfigurationManager.RefreshSection("appSettings");
49 irc.OnConnected += new EventHandler(OnConnected);
50 irc.OnChannelMessage += new IrcEventHandler(OnChannelMessage);
52 try
54 irc.Connect(AppSettings.IrcServer, port);
55 new Thread(new ThreadStart(MonitorCheckins)).Start();
56 irc.Listen();
58 catch (Exception e)
60 Console.Write("Failed to connect: "+ e.Message);
64 private VersionControlServer VersionControlServer
66 get
68 NetworkCredential creds = new NetworkCredential(AppSettings.Username, AppSettings.Password, AppSettings.Domain);
69 TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(AppSettings.TfsUrl, creds);
70 return tfs.GetService(typeof(VersionControlServer)) as VersionControlServer;
74 void OnConnected(object sender, EventArgs e)
76 irc.Login(AppSettings.Nick, "Team Foundation Notification Daemon");
77 irc.RfcJoin(AppSettings.Channel);
80 void OnChannelMessage(object sender, IrcEventArgs e)
82 if ((e.Data.MessageArray.Length == 0) || (e.Data.MessageArray[0] != "tfs")) return;
84 if (e.Data.MessageArray.Length < 2)
86 irc.SendMessage(SendType.Message, e.Data.Nick, "No commands implemented yet!");
90 void MonitorCheckins()
92 int lastestChangesetId = AppSettings.LatestChangesetId;
94 while (true)
96 ChangesetVersionSpec versionFrom = new ChangesetVersionSpec(lastestChangesetId.ToString());
97 IEnumerable changeSets = VersionControlServer.QueryHistory(VersionControlPath.RootFolder,
98 VersionSpec.Latest, 0, RecursionType.Full,
99 null, versionFrom, null, 100, true, false, false);
101 Stack<string> msgStack = new Stack<string>();
103 foreach (Changeset changeSet in changeSets)
105 if (changeSet.ChangesetId > lastestChangesetId)
107 lastestChangesetId = changeSet.ChangesetId;
109 StringBuilder sb = new StringBuilder();
110 foreach (Change change in changeSet.Changes)
112 if (sb.Length != 0) sb.Append(", ");
113 sb.Append(change.Item.ServerItem);
116 msgStack.Push("Files: " + sb.ToString());
118 //string msg0 = String.Format("Url: {0}/VersionControl/Changeset.aspx?artifactMoniker={1}&webView=true",
119 //AppSettings.TfsUrl, changeSet.ChangesetId);
120 //msgStack.Push(msg0);
122 if (!String.IsNullOrEmpty(changeSet.Comment))
124 string msg1 = String.Format("Comment: {0}", changeSet.Comment);
125 msgStack.Push(msg1);
128 string msg2 = String.Format("Changset: {0} by {1} on {2}",
129 changeSet.ChangesetId,
130 changeSet.Committer,
131 changeSet.CreationDate.ToString());
132 msgStack.Push(msg2);
136 bool fnd = msgStack.Count > 0;
137 while (msgStack.Count > 0)
139 string msg = msgStack.Pop();
140 irc.SendMessage(SendType.Message, AppSettings.Channel, msg);
143 if (fnd) UpdateLatestChangset(lastestChangesetId);
144 Thread.Sleep(1000*300);