Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from osoykan/dev
Browse files Browse the repository at this point in the history
dev to master
  • Loading branch information
osoykan authored Nov 29, 2017
2 parents 1a4648b + b9fa514 commit 72683ce
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 74 deletions.
5 changes: 4 additions & 1 deletion Cake.OctoVariapus.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.14
VisualStudioVersion = 15.0.27004.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E922420A-F873-4290-B5AD-F37574F16A2D}"
EndProject
Expand Down Expand Up @@ -41,4 +41,7 @@ Global
{00E978A7-5A42-454E-9612-377CD9C5E4D1} = {E922420A-F873-4290-B5AD-F37574F16A2D}
{302592FA-79E6-4903-BE4F-A2C745068AEC} = {AFC42151-A521-4E67-836F-7368D6A741D8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C65F7E3C-2059-4AB4-9F81-BE294C8BA736}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Cake.OctoVariapus/Cake.OctoVariapus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net46;netstandard1.6</TargetFrameworks>
<VersionPrefix>1.1.4</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<PackageIconUrl>https://raw.githubusercontent.com/cake-contrib/graphics/a5cf0f881c390650144b2243ae551d5b9f836196/png/cake-contrib-medium.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/osoykan/Cake.OctoVariapus</PackageProjectUrl>
Expand All @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cake.Core" Version="0.22.2" />
<PackageReference Include="Cake.Core" Version="0.23.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Octopus.Client" Version="4.22.1" />
</ItemGroup>
Expand Down
37 changes: 25 additions & 12 deletions src/Cake.OctoVariapus/OctoVariableImportAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public static class OctoVariableImportAlias
/// <param name="octopusProjectName">Name of the octopus project.</param>
/// <param name="octopusApiKey">The octopus API key.</param>
/// <param name="variables">The variables.</param>
/// <param name="clearAllNonSensitiveExistingVariables">Clear all nonsensitive variables before adding variables.</param>
[CakeMethodAlias]
public static void OctoImportVariables(this ICakeContext context,
string octopusServerEndpoint,
string octopusProjectName,
string octopusApiKey,
IEnumerable<OctoVariable> variables)
IEnumerable<OctoVariable> variables,
bool clearAllNonSensitiveExistingVariables = false)
{
try
{
Expand All @@ -45,6 +47,19 @@ public static void OctoImportVariables(this ICakeContext context,

VariableSetResource variableSet = octopus.VariableSets.Get(project.Link("Variables")).Result;

if (clearAllNonSensitiveExistingVariables)
{
context.Log.Information($"Deleting all nonsensitive variables...");

List<VariableResource> sensitiveVariables = variableSet.Variables.Where(variable => variable.IsSensitive).ToList();

variableSet.Variables.Clear();

sensitiveVariables.ForEach(sensitiveVariable => { variableSet.Variables.Add(sensitiveVariable); });

context.Log.Information($"Deleting operation finished.");
}

foreach (OctoVariable variable in variables)
{
var newVariable = new VariableResource
Expand All @@ -53,11 +68,10 @@ public static void OctoImportVariables(this ICakeContext context,
Value = variable.Value,
IsSensitive = variable.IsSensitive,
Type = variable.IsSensitive ? VariableType.Sensitive : VariableType.String,
IsEditable = variable.IsEditable
IsEditable = variable.IsEditable,
Scope = CreateScopeSpesification(variable, variableSet)
};

newVariable.Scope = CreateScopeSpesification(variable, variableSet);

string scopeNames = CreateScopeInformationsForLogging(variable);

VariableResource existingVariable = variableSet.Variables.FirstOrDefault(x => x.Name == variable.Name && x.Scope.Equals(newVariable.Scope));
Expand Down Expand Up @@ -108,12 +122,14 @@ private static string CreateScopeInformationsForLogging(OctoVariable variable)
/// <param name="octopusProjectName">Name of the octopus project.</param>
/// <param name="octopusApiKey">The octopus API key.</param>
/// <param name="jsonVariableFilePath">The json variable file path.</param>
/// <param name="clearAllNonSensitiveExistingVariables"></param>
[CakeMethodAlias]
public static void OctoImportVariables(this ICakeContext context,
string octopusServerEndpoint,
string octopusProjectName,
string octopusApiKey,
FilePath jsonVariableFilePath)
FilePath jsonVariableFilePath,
bool clearAllNonSensitiveExistingVariables = false)
{
string jsonString = File.ReadAllText(jsonVariableFilePath.FullPath);
var variables = JsonConvert.DeserializeObject<List<OctoVariable>>(jsonString);
Expand All @@ -132,15 +148,12 @@ private static ScopeSpecification CreateScopeSpesification(OctoVariable variable
List<ReferenceDataItem> referenceDataItems = FindScopeValue(scopeName, variableSet);
List<string> scopeValues = referenceDataItems.Join(scope.Values,
refDataItem => refDataItem.Name,
selectedScope => selectedScope,
(item, s) => item.Id)
refDataItem => refDataItem.Name,
selectedScope => selectedScope,
(item, s) => item.Id)
.ToList();
if (!scopeValues.Any())
{
throw new CakeException($"({string.Join(",", scope.Values)}) value(s) can not be found on ({scope.Name}) scope.");
}
if (!scopeValues.Any()) throw new CakeException($"({string.Join(",", scope.Values)}) value(s) can not be found on ({scope.Name}) scope.");
var value = new ScopeValue(scopeValues.First(), scopeValues.Skip(1).ToArray());
Expand Down
12 changes: 6 additions & 6 deletions test/Cake.OctoVariapus.Tests/Cake.OctoVariapus.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFramework>net461</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
Expand All @@ -22,11 +22,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cake.Core" Version="0.22.2" />
<PackageReference Include="FakeItEasy" Version="4.0.0" />
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="HttpMock" Version="2.1.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="Cake.Core" Version="0.23.0" />
<PackageReference Include="FakeItEasy" Version="4.2.0" />
<PackageReference Include="FluentAssertions" Version="5.0.0-beta0004" />
<PackageReference Include="HttpMock" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 40 additions & 0 deletions test/Cake.OctoVariapus.Tests/CakeOctoVariableImportAliasFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

using Cake.Core;
using Cake.Core.IO;

using FakeItEasy;

namespace Cake.OctoVariapus.Tests
{
public class CakeOctoVariableImportAliasFixture
{
public CakeOctoVariableImportAliasFixture(int exitCode)
{
GetDirectoryPath = Guid.NewGuid().ToString();

A.CallTo(() => CakeContext.ProcessRunner).Returns(ProcessRunner);
A.CallTo(() => CakeContext.FileSystem).Returns(FileSystem);
A.CallTo(() => CakeContext.Log).Returns(GetCakeLog);
A.CallTo(() => ProcessRunner.Start(A<FilePath>._, A<ProcessSettings>._)).Returns(Process);
A.CallTo(() => Process.GetExitCode()).Returns(exitCode);
}

public CakeOctoVariableImportAliasFixture()
: this(0)
{
}

public ICakeContext CakeContext { get; } = A.Fake<ICakeContext>();

public IFileSystem FileSystem { get; } = A.Fake<IFileSystem>();

public DirectoryPath GetDirectoryPath { get; }

public IProcess Process { get; } = A.Fake<IProcess>();

public IProcessRunner ProcessRunner { get; } = A.Fake<IProcessRunner>();

public CakeLogFixture GetCakeLog { get; } = new CakeLogFixture();
}
}
115 changes: 62 additions & 53 deletions test/Cake.OctoVariapus.Tests/CakeOctoVariapus_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;

using Cake.Core;
using Cake.Core.IO;

using FakeItEasy;
using System.Collections.Generic;

using FluentAssertions;

Expand All @@ -16,24 +10,27 @@ namespace Cake.OctoVariapus.Tests
{
public class CakeOctoVariapus_Tests
{
private const string OctopusUrl = "http://local.octopus.com";

private const string OctoProjectName = "Cake.OctoVariapus";

private const string OctoApiKey = "API-OW0PLJT3JLSZXFCRBE0EKQ7KU9I";

[Fact]
public void import_with_handwritten_list_should_work()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var octoVaribleImportAlias = new CakeOctoVariableImportAliasFixture(0);
const string octopusUrl = "http://local.octopus.com";
const string octoProjectName = "Cake.OctoVariapus";
const string octoApiKey = "API-FZNNNTXZK0NWFHLLMYJL4JGFIU";

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
HttpMockRepository.At("http://localhost/api/variables/variableset-Projects-1");
octoVaribleImportAlias.CakeContext.OctoImportVariables(octopusUrl,
octoProjectName,
octoApiKey,
octoVaribleImportAlias.CakeContext.OctoImportVariables(OctopusUrl,
OctoProjectName,
OctoApiKey,
new List<OctoVariable>
{
new OctoVariable
Expand All @@ -48,11 +45,6 @@ public void import_with_handwritten_list_should_work()
{
Name = "Environment",
Values = new List<string> { "Development", "Stage" }
},
new OctoScope
{
Name = "Role",
Values = new List<string> { "Development" }
}
}
}
Expand All @@ -65,60 +57,77 @@ public void import_with_handwritten_list_should_work()
}

[Fact]
public void import_from_a_json_file_should_work()
public void clear_nonsensitive_variables_should_work()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var octoVaribleImportAlias = new CakeOctoVariableImportAliasFixture(0);
const string octopusUrl = "http://local.octopus.com";
const string octoProjectName = "Cake.OctoVariapus";
const string octoApiKey = "API-FZNNNTXZK0NWFHLLMYJL4JGFIU";

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------

octoVaribleImportAlias.CakeContext.OctoImportVariables(octopusUrl,
octoProjectName,
octoApiKey,
"variables.json");
HttpMockRepository.At("http://localhost/api/variables/variableset-Projects-1");
octoVaribleImportAlias.CakeContext.OctoImportVariables(OctopusUrl,
OctoProjectName,
OctoApiKey,
new List<OctoVariable>
{
new OctoVariable
{
Name = "Username",
IsSensitive = false,
IsEditable = true,
Value = "user",
Scopes = new List<OctoScope>
{
new OctoScope
{
Name = "Environment",
Values = new List<string> { "Development", "Stage" }
}
}
},
new OctoVariable
{
Name = "Password",
IsSensitive = true,
IsEditable = true,
Value = "123456",
Scopes = new List<OctoScope>
{
new OctoScope
{
Name = "Environment",
Values = new List<string> { "Development", "Stage" }
}
}
}
}, true);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
octoVaribleImportAlias.GetCakeLog.Messages.Count.Should().BeGreaterThan(0);
}

private class CakeOctoVariableImportAliasFixture
[Fact]
public void import_from_a_json_file_should_work()
{
public CakeOctoVariableImportAliasFixture(int exitCode)
{
GetDirectoryPath = Guid.NewGuid().ToString();

A.CallTo(() => CakeContext.ProcessRunner).Returns(ProcessRunner);
A.CallTo(() => CakeContext.FileSystem).Returns(FileSystem);
A.CallTo(() => CakeContext.Log).Returns(GetCakeLog);
A.CallTo(() => ProcessRunner.Start(A<FilePath>._, A<ProcessSettings>._)).Returns(Process);
A.CallTo(() => Process.GetExitCode()).Returns(exitCode);
}

public CakeOctoVariableImportAliasFixture()
: this(0)
{
}

public ICakeContext CakeContext { get; } = A.Fake<ICakeContext>();

public IFileSystem FileSystem { get; } = A.Fake<IFileSystem>();

public DirectoryPath GetDirectoryPath { get; }

public IProcess Process { get; } = A.Fake<IProcess>();
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var octoVaribleImportAlias = new CakeOctoVariableImportAliasFixture(0);

public IProcessRunner ProcessRunner { get; } = A.Fake<IProcessRunner>();
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
octoVaribleImportAlias.CakeContext.OctoImportVariables(OctopusUrl, OctoProjectName, OctoApiKey, "variables.json");

public CakeLogFixture GetCakeLog { get; } = new CakeLogFixture();
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
octoVaribleImportAlias.GetCakeLog.Messages.Count.Should().BeGreaterThan(0);
}
}
}

0 comments on commit 72683ce

Please sign in to comment.