Shared Label Plugin for CCNET

Recently, i have to develop plugin to share the build label across CCNET projects depending upon previously success or failure build. So, I came up with below solution in C#-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Remote;
using System.IO;
using Exortech.NetReflector;

namespace ThoughtWorks.CruiseControl.Core.labeller
{

// this is the labeller name that will be used in  ccnet.config
[ReflectorType(“sharedLabeller”)]
public class SharedLabeller : ILabeller
{
[ReflectorProperty(“sharedLabelFilePath”, Required = true)]
public string sharedLabelFilePath { get; set; }

[ReflectorProperty(“buildSuccessFile”, Required = true)]
public string buildSuccessFile { get; set; }

[ReflectorProperty(“alwaysincrement”, Required = true)]
public string alwaysincrement { get; set; }

#region ILabeller Members

public string Generate(IIntegrationResult previousResult)
{
if (ShouldIncrementLabel(previousResult))
return IncrementLabel();

if (previousResult.Status == IntegrationStatus.Unknown)
return “0”;

return previousResult.Label;
}

public void Run(IIntegrationResult result)
{
result.Label = Generate(result);
}

#endregion

private string IncrementLabel()
{
if (!File.Exists(sharedLabelFilePath))
return “0”;

using (FileStream fileStream = File.Open(sharedLabelFilePath,
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None))
{
// read last build number from file
var bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);

string rawBuildNumber = Encoding.ASCII.GetString(bytes);

// parse last build number
int previousBuildNumber = int.Parse(rawBuildNumber);
int newBuildNumber=0;
if (File.Exists(buildSuccessFile) && alwaysincrement==”yes”)
newBuildNumber = previousBuildNumber + 1;
else
newBuildNumber=previousBuildNumber;

// increment build number and write back to file
bytes = Encoding.ASCII.GetBytes(newBuildNumber.ToString());

fileStream.Seek(0, SeekOrigin.Begin);
fileStream.Write(bytes, 0, bytes.Length);

return newBuildNumber.ToString();
}
}

private static bool ShouldIncrementLabel(IIntegrationResult previousResult)
{
return (previousResult.Status == IntegrationStatus.Success || previousResult.Status == IntegrationStatus.Unknown);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s