Skip to content

Commit

Permalink
Merge pull request #1298 from RehanSaeed/Go.CD-GetHistory
Browse files Browse the repository at this point in the history
GH1296: Add Go.CD build history API call
  • Loading branch information
gep13 authored Oct 25, 2016
2 parents a17d7b1 + 769fd9a commit 38fcfab
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Cake.Common.Tests/Fixtures/Build/GoCDFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Cake.Common.Build.GoCD;
using Cake.Core;
using Cake.Testing;
using NSubstitute;

namespace Cake.Common.Tests.Fixtures.Build
Expand All @@ -12,10 +13,13 @@ internal sealed class GoCDFixture
{
public ICakeEnvironment Environment { get; set; }

public FakeLog CakeLog { get; set; }

public GoCDFixture()
{
Environment = Substitute.For<ICakeEnvironment>();
Environment.GetEnvironmentVariable("https://127.0.0.1:8154/go").Returns((string)null);
CakeLog = new FakeLog();
}

public void IsRunningOnGoCD()
Expand All @@ -25,7 +29,7 @@ public void IsRunningOnGoCD()

public GoCDProvider CreateGoCDService()
{
return new GoCDProvider(Environment);
return new GoCDProvider(Environment, CakeLog);
}
}
}
63 changes: 62 additions & 1 deletion src/Cake.Common.Tests/Unit/Build/GoCD/GoCDProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using Cake.Common.Build.GoCD;
using Cake.Common.Tests.Fixtures.Build;
using Cake.Core;
using Cake.Core.Diagnostics;
using NSubstitute;
using Xunit;

namespace Cake.Common.Tests.Unit.Build.GoCD
Expand All @@ -16,11 +19,23 @@ public sealed class TheConstructor
public void Should_Throw_If_Environment_Is_Null()
{
// Given, When
var result = Record.Exception(() => new GoCDProvider(null));
var cakeLog = Substitute.For<ICakeLog>();
var result = Record.Exception(() => new GoCDProvider(null, cakeLog));

// Then
Assert.IsArgumentNullException(result, "environment");
}

[Fact]
public void Should_Throw_If_Log_Is_Null()
{
// Given, When
var environment = Substitute.For<ICakeEnvironment>();
var result = Record.Exception(() => new GoCDProvider(environment, null));

// Then
Assert.IsArgumentNullException(result, "cakeLog");
}
}

public sealed class TheIsRunningOnGoCDProperty
Expand Down Expand Up @@ -71,5 +86,51 @@ public void Should_Return_Non_Null_Reference()
Assert.NotNull(result);
}
}

public sealed class TheGetHistoryMethod
{
[Fact]
public void Should_Throw_If_Username_Is_Null()
{
// Given
var fixture = new GoCDFixture();
var appVeyor = fixture.CreateGoCDService();

// When
var result = Record.Exception(() => appVeyor.GetHistory(null, "password"));

// Then
Assert.IsArgumentNullException(result, "username");
}

[Fact]
public void Should_Throw_If_Password_Is_Null()
{
// Given
var fixture = new GoCDFixture();
var appVeyor = fixture.CreateGoCDService();

// When
var result = Record.Exception(() => appVeyor.GetHistory("username", null));

// Then
Assert.IsArgumentNullException(result, "password");
}

[Fact]
public void Should_Throw_If_Not_Running_On_GoCD()
{
// Given
var fixture = new GoCDFixture();
var appVeyor = fixture.CreateGoCDService();

// When
var result = Record.Exception(() => appVeyor.GetHistory("username", "password"));

// Then
Assert.IsExceptionWithMessage<CakeException>(result,
"The current build is not running on Go.CD.");
}
}
}
}
2 changes: 1 addition & 1 deletion src/Cake.Common/Build/BuildSystemAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static BuildSystem BuildSystem(this ICakeContext context)
var bitriseProvider = new BitriseProvider(context.Environment);
var travisCIProvider = new TravisCIProvider(context.Environment, context.Log);
var bitbucketPipelinesProvider = new BitbucketPipelinesProvider(context.Environment);
var goCDProvider = new GoCDProvider(context.Environment);
var goCDProvider = new GoCDProvider(context.Environment, context.Log);
var gitlabCIProvider = new GitLabCIProvider(context.Environment);
var tfBuildProvider = new TFBuildProvider(context.Environment);
return new BuildSystem(appVeyorProvider, teamCityProvider, myGetProvider, bambooProvider, continuaCIProvider, jenkinsProvider, bitriseProvider, travisCIProvider, bitbucketPipelinesProvider, goCDProvider, gitlabCIProvider, tfBuildProvider);
Expand Down
52 changes: 52 additions & 0 deletions src/Cake.Common/Build/GoCD/Data/GoCDBuildCauseInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Cake.Common.Build.GoCD.Data
{
/// <summary>
/// The Go.CD build cause.
/// </summary>
[DataContract]
public class GoCDBuildCauseInfo
{
/// <summary>
/// Gets or sets the approver.
/// </summary>
/// <value>
/// The approver.
/// </value>
[DataMember(Name = "approver")]
public string Approver { get; set; }

/// <summary>
/// Gets or sets the material revisions.
/// </summary>
/// <value>
/// The material revisions.
/// </value>
[DataMember(Name = "material_revisions")]
public IEnumerable<GoCDMaterialRevisionsInfo> MaterialRevisions { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the trigger was forced.
/// </summary>
/// <value>
/// <c>true</c> if the trigger was forced; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "trigger_forced")]
public bool TriggerForced { get; set; }

/// <summary>
/// Gets or sets the trigger message.
/// </summary>
/// <value>
/// The trigger message.
/// </value>
[DataMember(Name = "trigger_message")]
public string TriggerMessage { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/Cake.Common/Build/GoCD/Data/GoCDHistoryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Cake.Common.Build.GoCD.Data
{
/// <summary>
/// The Go.CD history.
/// </summary>
[DataContract]
public class GoCDHistoryInfo
{
/// <summary>
/// Gets or sets the pipelines.
/// </summary>
/// <value>
/// The pipelines.
/// </value>
[DataMember(Name = "pipelines")]
public IEnumerable<GoCDPipelineHistoryInfo> Pipelines { get; set; }
}
}
34 changes: 34 additions & 0 deletions src/Cake.Common/Build/GoCD/Data/GoCDMaterialRevisionsInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Cake.Common.Build.GoCD.Data
{
/// <summary>
/// The Go.CD material revision information.
/// </summary>
[DataContract]
public class GoCDMaterialRevisionsInfo
{
/// <summary>
/// Gets or sets a value indicating whether a change was made.
/// </summary>
/// <value>
/// <c>true</c> if changed; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "changed")]
public bool Changed { get; set; }

/// <summary>
/// Gets or sets the modifications.
/// </summary>
/// <value>
/// The modifications.
/// </value>
[DataMember(Name = "modifications")]
public IEnumerable<GoCDModificationInfo> Modifications { get; set; }
}
}
98 changes: 98 additions & 0 deletions src/Cake.Common/Build/GoCD/Data/GoCDModificationInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.Serialization;

namespace Cake.Common.Build.GoCD.Data
{
/// <summary>
/// A change made in the repository since the last time the Go.CD pipeline was run.
/// </summary>
[DataContract]
public class GoCDModificationInfo
{
/// <summary>
/// Gets or sets the email address.
/// </summary>
/// <value>
/// The email address.
/// </value>
[DataMember(Name = "email_address")]
public string EmailAddress { get; set; }

/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
[DataMember(Name = "id")]
public int Id { get; set; }

/// <summary>
/// Gets or sets the modified time in milliseconds from the Unix epoch.
/// </summary>
/// <value>
/// The modified time in milliseconds from the Unix epoch.
/// </value>
[DataMember(Name = "modified_time")]
public long ModifiedTimeUnixMilliseconds { get; set; }

/// <summary>
/// Gets or sets the modified time.
/// </summary>
/// <value>
/// The modified time.
/// </value>
public DateTime ModifiedTime
{
get { return FromUnixTimeMilliseconds(ModifiedTimeUnixMilliseconds); }
set { ModifiedTimeUnixMilliseconds = ToUnixTimeMilliseconds(value); }
}

/// <summary>
/// Gets or sets the username.
/// </summary>
/// <value>
/// The username.
/// </value>
[DataMember(Name = "user_name")]
public string Username { get; set; }

/// <summary>
/// Gets or sets the comment.
/// </summary>
/// <value>
/// The comment.
/// </value>
[DataMember(Name = "comment")]
public string Comment { get; set; }

/// <summary>
/// Gets or sets the revision.
/// </summary>
/// <value>
/// The revision.
/// </value>
[DataMember(Name = "revision")]
public string Revision { get; set; }

private static DateTime FromUnixTimeMilliseconds(long milliseconds)
{
if ((milliseconds < -62135596800000L) || (milliseconds > 0xe677d21fdbffL))
{
throw new ArgumentOutOfRangeException(nameof(milliseconds));
}

return new DateTime((milliseconds * 0x2710L) + 0x89f7ff5f7b58000L);
}

private static long ToUnixTimeMilliseconds(DateTime dateTime)
{
long num = dateTime.Ticks / 0x2710L;
return num - 0x3883122cd800L;
}
}
}
51 changes: 51 additions & 0 deletions src/Cake.Common/Build/GoCD/Data/GoCDPipelineHistoryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.Serialization;

namespace Cake.Common.Build.GoCD.Data
{
/// <summary>
/// The Go.CD pipeline history.
/// </summary>
[DataContract]
public class GoCDPipelineHistoryInfo
{
/// <summary>
/// Gets or sets the build cause.
/// </summary>
/// <value>
/// The build cause.
/// </value>
[DataMember(Name = "build_cause")]
public GoCDBuildCauseInfo BuildCause { get; set; }

/// <summary>
/// Gets or sets the comment.
/// </summary>
/// <value>
/// The comment.
/// </value>
[DataMember(Name = "comment")]
public string Comment { get; set; }

/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
[DataMember(Name = "name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the natural order.
/// </summary>
/// <value>
/// The natural order.
/// </value>
[DataMember(Name = "natural_order")]
public string NaturalOrder { get; set; }
}
}
Loading

0 comments on commit 38fcfab

Please sign in to comment.