Skip to content

Instantly share code, notes, and snippets.

@nblumhardt
Created June 26, 2014 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nblumhardt/d6e388728750d0bc18b4 to your computer and use it in GitHub Desktop.
Save nblumhardt/d6e388728750d0bc18b4 to your computer and use it in GitHub Desktop.
Patch corrupted duplicate release numbers
// First:
// PM> Install-Package Octopus.Client
using System;
using System.Text.RegularExpressions;
using System.Threading;
using Octopus.Client;
namespace Octopus.Patch.ReleaseNumberCorruption
{
class Program
{
const RegexOptions _flags = RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;
static readonly Regex _semanticVersionRegex = new Regex(@"^(?<Version>\d+(\s*\.\s*\d+){0,3})(?<Release>-[a-z][0-9a-z-]*)?$", _flags);
static void Main()
{
const string server = "http://localhost:8065/";
const string apiKey = "API-IHH51S2D6BOTRKRNMVAOJZ0PU";
var endpoint = new OctopusServerEndpoint(server, apiKey);
var repository = new OctopusRepository(endpoint);
Console.WriteLine("Retrieving releases to check version numbers...");
var seen = 0;
var count = 0;
foreach (var release in repository.Releases.FindAll())
{
seen++;
if (string.IsNullOrWhiteSpace(release.Version) || !_semanticVersionRegex.IsMatch(release.Version))
{
// Generate a new version that should stay out of the way (low ordering) and
// use the current time as a tag.
var newVer = "0.0.1-" + DateTime.UtcNow.Ticks.ToString("X16");
Console.WriteLine("Fixing release {0} - version was: {1}, now: {2}",
release.Id, release.Version, newVer);
release.ReleaseNotes = "Version fixed automatically, was: " + release.Version + Environment.NewLine + (release.ReleaseNotes ?? "");
release.Version = newVer;
repository.Client.Put(release.Links["Self"], release);
++count;
Thread.Sleep(1); // millisecond, so that ticks are always unique IDs :)
}
}
Console.WriteLine("{0} releases seen, {1} fixed, done.", seen, count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment