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

Commit 72683ce

Browse files
authored
Merge pull request #7 from osoykan/dev
dev to master
2 parents 1a4648b + b9fa514 commit 72683ce

File tree

6 files changed

+139
-74
lines changed

6 files changed

+139
-74
lines changed

Cake.OctoVariapus.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26430.14
4+
VisualStudioVersion = 15.0.27004.2010
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E922420A-F873-4290-B5AD-F37574F16A2D}"
77
EndProject
@@ -41,4 +41,7 @@ Global
4141
{00E978A7-5A42-454E-9612-377CD9C5E4D1} = {E922420A-F873-4290-B5AD-F37574F16A2D}
4242
{302592FA-79E6-4903-BE4F-A2C745068AEC} = {AFC42151-A521-4E67-836F-7368D6A741D8}
4343
EndGlobalSection
44+
GlobalSection(ExtensibilityGlobals) = postSolution
45+
SolutionGuid = {C65F7E3C-2059-4AB4-9F81-BE294C8BA736}
46+
EndGlobalSection
4447
EndGlobal

src/Cake.OctoVariapus/Cake.OctoVariapus.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

2525
<ItemGroup>
26-
<PackageReference Include="Cake.Core" Version="0.22.2" />
26+
<PackageReference Include="Cake.Core" Version="0.23.0" />
2727
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
2828
<PackageReference Include="Octopus.Client" Version="4.22.1" />
2929
</ItemGroup>

src/Cake.OctoVariapus/OctoVariableImportAlias.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public static class OctoVariableImportAlias
2929
/// <param name="octopusProjectName">Name of the octopus project.</param>
3030
/// <param name="octopusApiKey">The octopus API key.</param>
3131
/// <param name="variables">The variables.</param>
32+
/// <param name="clearAllNonSensitiveExistingVariables">Clear all nonsensitive variables before adding variables.</param>
3233
[CakeMethodAlias]
3334
public static void OctoImportVariables(this ICakeContext context,
3435
string octopusServerEndpoint,
3536
string octopusProjectName,
3637
string octopusApiKey,
37-
IEnumerable<OctoVariable> variables)
38+
IEnumerable<OctoVariable> variables,
39+
bool clearAllNonSensitiveExistingVariables = false)
3840
{
3941
try
4042
{
@@ -45,6 +47,19 @@ public static void OctoImportVariables(this ICakeContext context,
4547

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

50+
if (clearAllNonSensitiveExistingVariables)
51+
{
52+
context.Log.Information($"Deleting all nonsensitive variables...");
53+
54+
List<VariableResource> sensitiveVariables = variableSet.Variables.Where(variable => variable.IsSensitive).ToList();
55+
56+
variableSet.Variables.Clear();
57+
58+
sensitiveVariables.ForEach(sensitiveVariable => { variableSet.Variables.Add(sensitiveVariable); });
59+
60+
context.Log.Information($"Deleting operation finished.");
61+
}
62+
4863
foreach (OctoVariable variable in variables)
4964
{
5065
var newVariable = new VariableResource
@@ -53,11 +68,10 @@ public static void OctoImportVariables(this ICakeContext context,
5368
Value = variable.Value,
5469
IsSensitive = variable.IsSensitive,
5570
Type = variable.IsSensitive ? VariableType.Sensitive : VariableType.String,
56-
IsEditable = variable.IsEditable
71+
IsEditable = variable.IsEditable,
72+
Scope = CreateScopeSpesification(variable, variableSet)
5773
};
5874

59-
newVariable.Scope = CreateScopeSpesification(variable, variableSet);
60-
6175
string scopeNames = CreateScopeInformationsForLogging(variable);
6276

6377
VariableResource existingVariable = variableSet.Variables.FirstOrDefault(x => x.Name == variable.Name && x.Scope.Equals(newVariable.Scope));
@@ -108,12 +122,14 @@ private static string CreateScopeInformationsForLogging(OctoVariable variable)
108122
/// <param name="octopusProjectName">Name of the octopus project.</param>
109123
/// <param name="octopusApiKey">The octopus API key.</param>
110124
/// <param name="jsonVariableFilePath">The json variable file path.</param>
125+
/// <param name="clearAllNonSensitiveExistingVariables"></param>
111126
[CakeMethodAlias]
112127
public static void OctoImportVariables(this ICakeContext context,
113128
string octopusServerEndpoint,
114129
string octopusProjectName,
115130
string octopusApiKey,
116-
FilePath jsonVariableFilePath)
131+
FilePath jsonVariableFilePath,
132+
bool clearAllNonSensitiveExistingVariables = false)
117133
{
118134
string jsonString = File.ReadAllText(jsonVariableFilePath.FullPath);
119135
var variables = JsonConvert.DeserializeObject<List<OctoVariable>>(jsonString);
@@ -132,15 +148,12 @@ private static ScopeSpecification CreateScopeSpesification(OctoVariable variable
132148
List<ReferenceDataItem> referenceDataItems = FindScopeValue(scopeName, variableSet);
133149

134150
List<string> scopeValues = referenceDataItems.Join(scope.Values,
135-
refDataItem => refDataItem.Name,
136-
selectedScope => selectedScope,
137-
(item, s) => item.Id)
151+
refDataItem => refDataItem.Name,
152+
selectedScope => selectedScope,
153+
(item, s) => item.Id)
138154
.ToList();
139155

140-
if (!scopeValues.Any())
141-
{
142-
throw new CakeException($"({string.Join(",", scope.Values)}) value(s) can not be found on ({scope.Name}) scope.");
143-
}
156+
if (!scopeValues.Any()) throw new CakeException($"({string.Join(",", scope.Values)}) value(s) can not be found on ({scope.Name}) scope.");
144157

145158
var value = new ScopeValue(scopeValues.First(), scopeValues.Skip(1).ToArray());
146159

test/Cake.OctoVariapus.Tests/Cake.OctoVariapus.Tests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net46</TargetFramework>
4+
<TargetFramework>net461</TargetFramework>
55
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
66
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
77
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
@@ -22,11 +22,11 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Cake.Core" Version="0.22.2" />
26-
<PackageReference Include="FakeItEasy" Version="4.0.0" />
27-
<PackageReference Include="FluentAssertions" Version="4.19.4" />
28-
<PackageReference Include="HttpMock" Version="2.1.0" />
29-
<PackageReference Include="xunit" Version="2.2.0" />
25+
<PackageReference Include="Cake.Core" Version="0.23.0" />
26+
<PackageReference Include="FakeItEasy" Version="4.2.0" />
27+
<PackageReference Include="FluentAssertions" Version="5.0.0-beta0004" />
28+
<PackageReference Include="HttpMock" Version="2.2.0" />
29+
<PackageReference Include="xunit" Version="2.3.1" />
3030
</ItemGroup>
3131

3232
<ItemGroup>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
using Cake.Core;
4+
using Cake.Core.IO;
5+
6+
using FakeItEasy;
7+
8+
namespace Cake.OctoVariapus.Tests
9+
{
10+
public class CakeOctoVariableImportAliasFixture
11+
{
12+
public CakeOctoVariableImportAliasFixture(int exitCode)
13+
{
14+
GetDirectoryPath = Guid.NewGuid().ToString();
15+
16+
A.CallTo(() => CakeContext.ProcessRunner).Returns(ProcessRunner);
17+
A.CallTo(() => CakeContext.FileSystem).Returns(FileSystem);
18+
A.CallTo(() => CakeContext.Log).Returns(GetCakeLog);
19+
A.CallTo(() => ProcessRunner.Start(A<FilePath>._, A<ProcessSettings>._)).Returns(Process);
20+
A.CallTo(() => Process.GetExitCode()).Returns(exitCode);
21+
}
22+
23+
public CakeOctoVariableImportAliasFixture()
24+
: this(0)
25+
{
26+
}
27+
28+
public ICakeContext CakeContext { get; } = A.Fake<ICakeContext>();
29+
30+
public IFileSystem FileSystem { get; } = A.Fake<IFileSystem>();
31+
32+
public DirectoryPath GetDirectoryPath { get; }
33+
34+
public IProcess Process { get; } = A.Fake<IProcess>();
35+
36+
public IProcessRunner ProcessRunner { get; } = A.Fake<IProcessRunner>();
37+
38+
public CakeLogFixture GetCakeLog { get; } = new CakeLogFixture();
39+
}
40+
}
Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
4-
using Cake.Core;
5-
using Cake.Core.IO;
6-
7-
using FakeItEasy;
1+
using System.Collections.Generic;
82

93
using FluentAssertions;
104

@@ -16,24 +10,27 @@ namespace Cake.OctoVariapus.Tests
1610
{
1711
public class CakeOctoVariapus_Tests
1812
{
13+
private const string OctopusUrl = "http://local.octopus.com";
14+
15+
private const string OctoProjectName = "Cake.OctoVariapus";
16+
17+
private const string OctoApiKey = "API-OW0PLJT3JLSZXFCRBE0EKQ7KU9I";
18+
1919
[Fact]
2020
public void import_with_handwritten_list_should_work()
2121
{
2222
//-----------------------------------------------------------------------------------------------------------
2323
// Arrange
2424
//-----------------------------------------------------------------------------------------------------------
2525
var octoVaribleImportAlias = new CakeOctoVariableImportAliasFixture(0);
26-
const string octopusUrl = "http://local.octopus.com";
27-
const string octoProjectName = "Cake.OctoVariapus";
28-
const string octoApiKey = "API-FZNNNTXZK0NWFHLLMYJL4JGFIU";
2926

3027
//-----------------------------------------------------------------------------------------------------------
3128
// Act
3229
//-----------------------------------------------------------------------------------------------------------
3330
HttpMockRepository.At("http://localhost/api/variables/variableset-Projects-1");
34-
octoVaribleImportAlias.CakeContext.OctoImportVariables(octopusUrl,
35-
octoProjectName,
36-
octoApiKey,
31+
octoVaribleImportAlias.CakeContext.OctoImportVariables(OctopusUrl,
32+
OctoProjectName,
33+
OctoApiKey,
3734
new List<OctoVariable>
3835
{
3936
new OctoVariable
@@ -48,11 +45,6 @@ public void import_with_handwritten_list_should_work()
4845
{
4946
Name = "Environment",
5047
Values = new List<string> { "Development", "Stage" }
51-
},
52-
new OctoScope
53-
{
54-
Name = "Role",
55-
Values = new List<string> { "Development" }
5648
}
5749
}
5850
}
@@ -65,60 +57,77 @@ public void import_with_handwritten_list_should_work()
6557
}
6658

6759
[Fact]
68-
public void import_from_a_json_file_should_work()
60+
public void clear_nonsensitive_variables_should_work()
6961
{
7062
//-----------------------------------------------------------------------------------------------------------
7163
// Arrange
7264
//-----------------------------------------------------------------------------------------------------------
7365
var octoVaribleImportAlias = new CakeOctoVariableImportAliasFixture(0);
74-
const string octopusUrl = "http://local.octopus.com";
75-
const string octoProjectName = "Cake.OctoVariapus";
76-
const string octoApiKey = "API-FZNNNTXZK0NWFHLLMYJL4JGFIU";
7766

7867
//-----------------------------------------------------------------------------------------------------------
7968
// Act
8069
//-----------------------------------------------------------------------------------------------------------
81-
82-
octoVaribleImportAlias.CakeContext.OctoImportVariables(octopusUrl,
83-
octoProjectName,
84-
octoApiKey,
85-
"variables.json");
70+
HttpMockRepository.At("http://localhost/api/variables/variableset-Projects-1");
71+
octoVaribleImportAlias.CakeContext.OctoImportVariables(OctopusUrl,
72+
OctoProjectName,
73+
OctoApiKey,
74+
new List<OctoVariable>
75+
{
76+
new OctoVariable
77+
{
78+
Name = "Username",
79+
IsSensitive = false,
80+
IsEditable = true,
81+
Value = "user",
82+
Scopes = new List<OctoScope>
83+
{
84+
new OctoScope
85+
{
86+
Name = "Environment",
87+
Values = new List<string> { "Development", "Stage" }
88+
}
89+
}
90+
},
91+
new OctoVariable
92+
{
93+
Name = "Password",
94+
IsSensitive = true,
95+
IsEditable = true,
96+
Value = "123456",
97+
Scopes = new List<OctoScope>
98+
{
99+
new OctoScope
100+
{
101+
Name = "Environment",
102+
Values = new List<string> { "Development", "Stage" }
103+
}
104+
}
105+
}
106+
}, true);
86107

87108
//-----------------------------------------------------------------------------------------------------------
88109
// Assert
89110
//-----------------------------------------------------------------------------------------------------------
90111
octoVaribleImportAlias.GetCakeLog.Messages.Count.Should().BeGreaterThan(0);
91112
}
92113

93-
private class CakeOctoVariableImportAliasFixture
114+
[Fact]
115+
public void import_from_a_json_file_should_work()
94116
{
95-
public CakeOctoVariableImportAliasFixture(int exitCode)
96-
{
97-
GetDirectoryPath = Guid.NewGuid().ToString();
98-
99-
A.CallTo(() => CakeContext.ProcessRunner).Returns(ProcessRunner);
100-
A.CallTo(() => CakeContext.FileSystem).Returns(FileSystem);
101-
A.CallTo(() => CakeContext.Log).Returns(GetCakeLog);
102-
A.CallTo(() => ProcessRunner.Start(A<FilePath>._, A<ProcessSettings>._)).Returns(Process);
103-
A.CallTo(() => Process.GetExitCode()).Returns(exitCode);
104-
}
105-
106-
public CakeOctoVariableImportAliasFixture()
107-
: this(0)
108-
{
109-
}
110-
111-
public ICakeContext CakeContext { get; } = A.Fake<ICakeContext>();
112-
113-
public IFileSystem FileSystem { get; } = A.Fake<IFileSystem>();
114-
115-
public DirectoryPath GetDirectoryPath { get; }
116-
117-
public IProcess Process { get; } = A.Fake<IProcess>();
117+
//-----------------------------------------------------------------------------------------------------------
118+
// Arrange
119+
//-----------------------------------------------------------------------------------------------------------
120+
var octoVaribleImportAlias = new CakeOctoVariableImportAliasFixture(0);
118121

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

121-
public CakeLogFixture GetCakeLog { get; } = new CakeLogFixture();
127+
//-----------------------------------------------------------------------------------------------------------
128+
// Assert
129+
//-----------------------------------------------------------------------------------------------------------
130+
octoVaribleImportAlias.GetCakeLog.Messages.Count.Should().BeGreaterThan(0);
122131
}
123132
}
124133
}

0 commit comments

Comments
 (0)