20070709

Getting your CruiseControl.Net/.rb 's Project Status in C#

Ever wondered how to get a CruiseControl.Net/.rb project status?

Below is a spike I did to see if I can do it.


using System;

using System.Runtime.Remoting;

using MbUnit.Framework;

using Rhino.Mocks;

using ThoughtWorks.CruiseControl.CCTrayLib.Configuration;

using ThoughtWorks.CruiseControl.CCTrayLib.Monitoring;

using ThoughtWorks.CruiseControl.Remote;

namespace i.ma.gine.spike

{

[TestFixture]

public class CruiseControlMonitorSpike

{

[Test]

public void Create_ShouldCallCreate()

{

MockRepository mockery = new MockRepository();

ICruiseProjectManager mockManager = mockery.CreateMock<ICruiseProjectManager>();

ProjectStatus status = new ProjectStatus();

Expect.Call(mockManager.ProjectStatus).Return(status);

mockery.ReplayAll();

ProjectMonitor monitor = new ProjectMonitor(mockManager);

monitor.Poll();

Assert.AreSame(status, monitor.ProjectStatus);

mockery.VerifyAll();

}

[Test, Ignore("Just wanted to verify if I can actually connect to my CruiseControl.rb server")]

public void Create_ShouldCallCreateRunningRealCruiseControlServer()

{

ICruiseProjectManagerFactory cruiseProjectManagerFactory =

new CruiseProjectManagerFactory(new FakeCruiseManagerFactory(), new WebRetriever());

ProjectMonitor monitor =

new ProjectMonitor(

cruiseProjectManagerFactory.Create(

new Project("http://localhost:3333/XmlStatusReport.aspx", "imagine")));

monitor.Poll();

Console.Out.WriteLine(monitor.ProjectState);

Console.Out.WriteLine(monitor.LastBuildTime);

}

}

public class FakeCruiseManagerFactory : ICruiseManagerFactory

{

public ICruiseManager GetCruiseManager(string url)

{

return (ICruiseManager) RemotingServices.Connect(typeof (ICruiseManager), url);

}

}

}



With the following spike above, I was able to do this in code:

public static string Format(ICruiseControlProject project)

{

try

{

if (project.ProjectMonitor.ProjectState.ToString() == "Success")

return GetHTMLString(project.ProjectMonitor, "green");

return GetHTMLString(project.ProjectMonitor, "red");

}

catch

{

return string.Format("<font color=\"red\">{0}</font>", project.ProjectMonitor.ProjectState);

}

}

private static string GetHTMLString(IProjectMonitor monitor, string fontColor)

{

return

string.Format("<font color=\"{2}\">{0} - {1}</font>", monitor.ProjectState,

((ProjectMonitor) monitor).LastBuildTime, fontColor);

}




Note:
I imported the following .dll's from CCTray (1)ThoughtWorks.CruiseControl.CCTrayLib.dll (2)ThoughtWorks.CruiseControl.Remote.dll

0 comments: