using System; using System.Net; using System.Text; using Exortech.NetReflector; using ThoughtWorks.CruiseControl.Core; using ThoughtWorks.CruiseControl.Core.Util; using ThoughtWorks.CruiseControl.Remote; namespace NabaztagCruiseControlPlugin { [ReflectorType("nabaztag")] public class NabaztagPublisher: ITask { private const int SuccessEarPosition = 0; private const int FailureEarPosition = 9; private const string NabaztagUrlFormat = @"http://api.nabaztag.com/vl/FR/api.jsp?key={0}&sn={1}&token={2}&posleft={3}&posright={3}&ears=ok&voice=graham22s&tts={4}"; private readonly WebClient _client; private string _key; private string _token; private string _serialNumber; private string _pronounceableName; public NabaztagPublisher() { _client = new WebClient(); _client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(WebClientDownloadStringCompleted); } [ReflectorProperty("key")] public string Key { get { return _key; } set { _key = value; } } [ReflectorProperty("token")] public string Token { get { return _token; } set { _token = value; } } [ReflectorProperty("serialNumber")] public string SerialNumber { get { return _serialNumber; } set { _serialNumber = value; } } [ReflectorProperty("pronounceableName", Required=false)] public string PronounceableName { get { return _pronounceableName; } set { _pronounceableName = value; } } public void Run(IIntegrationResult result) { string name = PronounceableName ?? result.ProjectName; string message; int earPosition; switch (result.Status) { case IntegrationStatus.Success: earPosition = SuccessEarPosition; if (result.LastIntegrationStatus == IntegrationStatus.Success) message = string.Format( "Yet another successful build for {0}.", name); else message = string.Format( "Recent check ins have fixed the build for {0}.", name); break; case IntegrationStatus.Failure: earPosition = FailureEarPosition; if (result.LastIntegrationStatus == IntegrationStatus.Success) message = string.Format( "The build has failed for {0}. Oh dear.", name); else message = string.Format( "The build is still broken for {0} people! Get your act together. Deary me.", name); break; case IntegrationStatus.Exception: earPosition = FailureEarPosition; message = string.Format( "Oh dear. The build has failed due to an exception for project {0}. This sucks.", name); break; case IntegrationStatus.Unknown: default: earPosition = FailureEarPosition; message = string.Format( "Oh dear. The build has failed due to an unknown reason for project {0}. This sucks.", name); break; } Uri uri = new Uri( string.Format(NabaztagUrlFormat, Key, SerialNumber, Token, earPosition, Uri.EscapeDataString(message))); _client.DownloadStringAsync(uri, uri); } void WebClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { Log.Debug(string.Format("Nabaztag API: {0}", e.UserState)); if (e.Error != null) Log.Error(e.Error); else Log.Debug(e.Result); } } }