Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variables with scope #697

Merged
merged 88 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
936a5b7
Adapted test cases to GitLab 16.11.1-ee.0 ...
fhebel May 8, 2024
285a6aa
Adapted test cases to GitLab 16.11.1-ee.0 ...
fhebel May 8, 2024
2cfc72e
Merge branch 'ubisoft:main' into main
fhebel May 8, 2024
e60f6ac
First incomplete version of scoped variables ...
fhebel May 8, 2024
075abea
Merge branch 'main' into feature/variables-with-scope
fhebel May 8, 2024
bea3178
Merge branch 'ubisoft:main' into main
fhebel May 13, 2024
c0babe4
Updated Environment PUT api:
fhebel May 13, 2024
f824d8d
Removed obsolete handling for outdated GitLab-version (15.6) ...
fhebel May 13, 2024
675f5c1
Updated test user latest activities to current functionality ...
fhebel May 13, 2024
dca5ce8
Fixed null comparison ...
fhebel May 13, 2024
0c1a8b6
Removed release link external property, deprecated in GitLab 15.9, re…
fhebel May 13, 2024
0295de8
Merge branch 'main' into feature/gitlab-16.11.1-ee.0
fhebel May 14, 2024
7105395
Fixed api definitions ...
fhebel May 14, 2024
db14ff4
Fixed another occurence of Edit with old signature ...
fhebel May 14, 2024
129077d
Merge branch 'ubisoft:main' into main
fhebel May 14, 2024
8820815
Merge branch 'main' into feature/variables-with-scope
fhebel May 14, 2024
79a5ec0
Added scoped variable functionality ...
fhebel May 15, 2024
00ea729
Set GitLab-versions to test against:
fhebel May 15, 2024
29db70b
Removed complicated compiler directives ...
fhebel May 15, 2024
23bebb8
Fixed missing update method signature ...
fhebel May 15, 2024
63d8672
Test against 'gitlab/gitlab-ee:16.6.2-ee.0' & 'gitlab/gitlab-ee:16.11…
fhebel May 17, 2024
56c6c60
Merge branch 'ubisoft:main' into feature/gitlab-16.11.1-ee.0
fhebel May 17, 2024
222dc3e
Version bump to gitlab/gitlab-ee:16.11.2-ee.0 ...
fhebel May 17, 2024
c38891f
Merge remote-tracking branch 'origin/feature/gitlab-16.11.1-ee.0' int…
fhebel May 17, 2024
0cf0975
Revert "Fixed null comparison ..."
fhebel May 27, 2024
8b125f3
Revert "Removed obsolete handling for outdated GitLab-version (15.6) …
fhebel May 27, 2024
87bbdf7
Fixed Test_merge_request_api by adding undocumented enum member to De…
fhebel May 27, 2024
fcdaa4f
Used louis-z's proposed solution for continued support for version 15…
fhebel May 27, 2024
efe72e1
Merge branch 'ubisoft:main' into feature/gitlab-16.11.1-ee.0
fhebel May 27, 2024
70b831d
Still test against version 15.11.x and against latest version 16.11.3…
fhebel May 29, 2024
8e0c37f
Version bump from 16.11.2-ee.0 to 16.11.3-ee.0 ...
fhebel May 29, 2024
52d9055
Merge branch 'ubisoft:main' into feature/gitlab-16.11.1-ee.0
fhebel May 29, 2024
673cf0a
Merge remote-tracking branch 'origin/feature/gitlab-16.11.1-ee.0' int…
fhebel May 29, 2024
0c0053f
Update supported 16.x versions
fhebel May 29, 2024
641574c
Added missing handling for root token ...
fhebel May 29, 2024
c34bada
Revert "Removed release link external property, deprecated in GitLab …
fhebel May 30, 2024
e4693d0
Fixed typo ...
fhebel May 30, 2024
4b218f2
Backward compatibility for version 15
fhebel May 30, 2024
bcb02b9
Revert "Updated Environment PUT api:"
fhebel May 30, 2024
7fdf1fd
Revert "Fixed another occurence of Edit with old signature ..."
fhebel May 30, 2024
47c75eb
Implemented backwards compatible (15.x) version of EnvironmentClient.…
fhebel May 30, 2024
337d70f
Fixed test case for both 15.x and 16.x ...
fhebel May 30, 2024
c379a97
Replaced one line conditions by indented ones ...
fhebel May 31, 2024
e3446ef
Removed test code ...
fhebel May 31, 2024
ff7b257
Added IsGitLabMajorVersion(int major) to GitLabTestContext as suggest…
fhebel May 31, 2024
f24ade5
Added missing annotations for old environment edit signatures ...
fhebel May 31, 2024
b93a572
Moved supported ranges to local variables as suggested by @Toa741 ...
fhebel May 31, 2024
1e67377
Moved gitLabVersion to correct place ...
fhebel May 31, 2024
1c1164d
Corrected LF ...
fhebel Jun 1, 2024
1d3a7f3
Merge branch 'ubisoft:main' into feature/gitlab-16.11.1-ee.0
fhebel Jun 5, 2024
e79bc9a
Resolve GitLab version in GitLabDockerContainer (#695)
Toa741 Jun 7, 2024
828a5ce
Merge branch 'main' into feature/gitlab-16.11.1-ee.0
fhebel Jun 7, 2024
d3cc140
Merge remote-tracking branch 'origin/main'
fhebel Jun 11, 2024
dcb9bc1
Merge branch 'main' into feature/variables-with-scope
fhebel Jun 11, 2024
591b25e
Merge branch 'ubisoft:main' into feature/variables-with-scope
fhebel Jun 11, 2024
4606b24
Reset to lowest supported version as suggested by @Toa741 ...
fhebel Jun 10, 2024
e83d0a1
Changed implementation to support backwards compatibility ...
fhebel Jun 11, 2024
527c94b
Substituted Obsolete by EditorBrowsableNone ...
fhebel Jun 11, 2024
ee581b7
Substituted some more Obsolete annotations by EditorBrowsableNever ...
fhebel Jun 11, 2024
6322e73
Removed another obsolete annotation ...
fhebel Jun 11, 2024
2e1cf3d
Added version and license information ...
fhebel Jun 11, 2024
61ec877
Added new test cases ...
fhebel Jun 11, 2024
2fc1d33
Clarified that create and update of group variables need a license ...
fhebel Jun 11, 2024
fa00ae1
Some formatting in order to comply with NGitLab formatting policies ...
fhebel Jun 11, 2024
834c3e8
Added extension class for scoped variables ...
fhebel Jun 11, 2024
7543c17
Added test case for matching scoped variables ...
fhebel Jun 11, 2024
22029a0
Some hygiene ..
fhebel Jun 11, 2024
e8646db
Remove obsolete return value
fhebel Jun 12, 2024
d91506b
Reformatted IDE information ...
fhebel Jun 12, 2024
bf58a1c
Renamed as suggested by @Toa741 ...
fhebel Jun 12, 2024
c168edc
Added additional test as showcase ...
fhebel Jun 13, 2024
1c9c37b
Removed filter feature for specific environment, because it was not a…
fhebel Jun 17, 2024
f97422e
Removed obsolete environment scope and extracted filter to method ...
fhebel Jun 18, 2024
ec9586f
Extended get in order to be able to specify environment scope ...
fhebel Jun 18, 2024
4425224
Extended and improved variable test cases ...
fhebel Jun 18, 2024
b9d6ac3
Keeping VariableCreate and VariableUpdate as suggested by @louis-z an…
fhebel Jun 18, 2024
aa31497
Merge branch 'ubisoft:main' into feature/variables-with-scope
fhebel Jun 18, 2024
40aecba
Some hygiene ...
fhebel Jun 18, 2024
af42069
It seems the key of an existing variable can't be changed ...
fhebel Jun 18, 2024
824f780
Applied @Toa741's suggestion
fhebel Jun 20, 2024
116eece
Applied @Toa741's suggestion
fhebel Jun 20, 2024
143c88e
Applied @Toa741's suggestion regarding old property Scope
fhebel Jun 20, 2024
e83ced4
Fixed implementation after change of interface ...
fhebel Jun 20, 2024
47df36d
Merge remote-tracking branch 'origin/feature/variables-with-scope' in…
fhebel Jun 20, 2024
3f18000
VariableCreate is once again used ...
fhebel Jun 20, 2024
b153ebe
Converted properties back to fields as suggested by @Toa741 ...
fhebel Jun 20, 2024
bbdc15a
Fixed some more implementations of the interface ...
fhebel Jun 20, 2024
5d0c0a7
Fixed another occurence of property instead of field ...
fhebel Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions NGitLab.Mock/Clients/GroupVariableClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using NGitLab.Models;

namespace NGitLab.Mock.Clients;
Expand All @@ -18,17 +19,37 @@ public GroupVariableClient(ClientContext context, GroupId groupId)

public IEnumerable<Variable> All => throw new NotImplementedException();

[EditorBrowsable(EditorBrowsableState.Never)]
public Variable Create(VariableCreate model)
{
throw new NotImplementedException();
}

public void Delete(string key)
public Variable Create(Variable model)
{
throw new NotImplementedException();
}

public Variable Update(string key, VariableUpdate model)
[EditorBrowsable(EditorBrowsableState.Never)]
public void Delete(string key) => Delete(key, null);

public void Delete(string key, string environmentScope)
{
throw new NotImplementedException();
}

[EditorBrowsable(EditorBrowsableState.Never)]
public Variable Update(string key, VariableUpdate model) => Update(key, model, null);

[EditorBrowsable(EditorBrowsableState.Never)]
public Variable Update(string key, VariableUpdate model, string environmentScope)
{
throw new NotImplementedException();
}

public Variable Update(string key, Variable model) => Update(key, model, null);

public Variable Update(string key, Variable model, string environmentScope)
{
throw new NotImplementedException();
}
Expand Down
25 changes: 23 additions & 2 deletions NGitLab.Mock/Clients/ProjectVariableClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using NGitLab.Models;

namespace NGitLab.Mock.Clients;
Expand All @@ -18,17 +19,37 @@ public ProjectVariableClient(ClientContext context, ProjectId projectId)

public IEnumerable<Variable> All => throw new NotImplementedException();

[EditorBrowsable(EditorBrowsableState.Never)]
public Variable Create(VariableCreate model)
{
throw new NotImplementedException();
}

public void Delete(string key)
public Variable Create(Variable model)
{
throw new NotImplementedException();
}

public Variable Update(string key, VariableUpdate model)
[EditorBrowsable(EditorBrowsableState.Never)]
public void Delete(string key) => Delete(key, null);

public void Delete(string key, string environmentScope)
{
throw new NotImplementedException();
}

[EditorBrowsable(EditorBrowsableState.Never)]
public Variable Update(string key, VariableUpdate model) => Update(key, model, null);

[EditorBrowsable(EditorBrowsableState.Never)]
public Variable Update(string key, VariableUpdate model, string environmentScope)
{
throw new NotImplementedException();
}

public Variable Update(string key, Variable model) => Update(key, model, null);

public Variable Update(string key, Variable model, string environmentScope)
{
throw new NotImplementedException();
}
Expand Down
62 changes: 62 additions & 0 deletions NGitLab.Tests/GroupVariableClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using NGitLab.Models;
using NGitLab.Tests.Docker;
using NuGet.Versioning;
using NUnit.Framework;

namespace NGitLab.Tests;
Expand Down Expand Up @@ -52,4 +54,64 @@ public async Task Test_group_variables()
variables = groupVariableClient.All.ToList();
Assert.That(variables, Has.Count.EqualTo(3));
}

[Test]
[NGitLabRetry]
public async Task Test_group_variables_with_complete_members()
{
using var context = await GitLabTestContext.CreateAsync();
var group = context.CreateGroup();
var groupVariableClient = context.Client.GetGroupVariableClient(group.Id);

// Create
var variable = groupVariableClient.Create(new Variable
{
Key = "My_Key",
Value = "My value",
Description = "Some important variable",
Protected = true,
Type = VariableType.Variable,
Masked = false,
Raw = false,
});

Assert.That(variable.Key, Is.EqualTo("My_Key"));
Assert.That(variable.Value, Is.EqualTo("My value"));

if (context.IsGitLabVersionInRange(VersionRange.Parse("[16.2,)"), out _))
{
Assert.That(variable.Description, Is.EqualTo("Some important variable"));
}

Assert.That(variable.Protected, Is.EqualTo(true));
Assert.That(variable.Type, Is.EqualTo(VariableType.Variable));
Assert.That(variable.Masked, Is.EqualTo(false));
Assert.That(variable.Raw, Is.EqualTo(false));

// Update
var newScope = "integration/*";
variable = groupVariableClient.Update(variable.Key,
new Variable { Value = "My value edited", Protected = false, EnvironmentScope = newScope},
variable.EnvironmentScope);

Assert.That(variable.Key, Is.EqualTo("My_Key"));
Assert.That(variable.Value, Is.EqualTo("My value edited"));
Assert.That(variable.Protected, Is.EqualTo(false));

// Delete
var ex = Assert.Throws<GitLabException>(() => groupVariableClient.Delete(variable.Key, "wrongScope"));
Assert.That(ex!.StatusCode == HttpStatusCode.NotFound);

groupVariableClient.Delete(variable.Key);

var variables = groupVariableClient.All.ToList();
Assert.That(variables, Is.Empty);

// All
groupVariableClient.Create(new Variable { Key = "Variable1", Value = "test", EnvironmentScope = "test/*" });
groupVariableClient.Create(new Variable { Key = "Variable2", Value = "test", EnvironmentScope = "integration" });
groupVariableClient.Create(new Variable { Key = "Variable3", Value = "test", EnvironmentScope = "*" });
variables = groupVariableClient.All.ToList();
Assert.That(variables, Has.Count.EqualTo(3));
}
}
138 changes: 138 additions & 0 deletions NGitLab.Tests/ProjectVariableClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using NGitLab.Extensions;
using NGitLab.Models;
using NGitLab.Tests.Docker;
using NuGet.Versioning;
using NUnit.Framework;

namespace NGitLab.Tests;
Expand Down Expand Up @@ -52,4 +55,139 @@ public async Task Test_project_variables()
variables = projectVariableClient.All.ToList();
Assert.That(variables, Has.Count.EqualTo(3));
}

[Test]
[NGitLabRetry]
public async Task Test_project_variables_with_scope()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var projectVariableClient = context.Client.GetProjectVariableClient(project.Id);

// Create
var variable = projectVariableClient.Create(new Variable
{
Key = "My_Key",
Value = "My value",
Description = "Some important variable",
Protected = true,
Type = VariableType.Variable,
Masked = false,
Raw = false,
EnvironmentScope = "test/*",
});

Assert.That(variable.Key, Is.EqualTo("My_Key"));
Assert.That(variable.Value, Is.EqualTo("My value"));

if (context.IsGitLabVersionInRange(VersionRange.Parse("[16.2,)"), out _))
{
Assert.That(variable.Description, Is.EqualTo("Some important variable"));
}

Assert.That(variable.Protected, Is.EqualTo(true));
Assert.That(variable.Type, Is.EqualTo(VariableType.Variable));
Assert.That(variable.Masked, Is.EqualTo(false));
Assert.That(variable.Raw, Is.EqualTo(false));
Assert.That(variable.EnvironmentScope, Is.EqualTo("test/*"));

// Update
var newScope = "integration/*";
variable = projectVariableClient.Update(variable.Key, new Variable
{
Value = "My value edited",
Protected = false,
EnvironmentScope = newScope,
},
variable.EnvironmentScope);

Assert.That(variable.Key, Is.EqualTo("My_Key"));
Assert.That(variable.Value, Is.EqualTo("My value edited"));
Assert.That(variable.Protected, Is.EqualTo(false));
Assert.That(variable.EnvironmentScope, Is.EqualTo(newScope));

// Delete
var ex = Assert.Throws<GitLabException>(() => projectVariableClient.Delete(variable.Key, "wrongScope"));
Assert.That(ex!.StatusCode == HttpStatusCode.NotFound);

projectVariableClient.Delete(variable.Key, newScope);

var variables = projectVariableClient.All.ToList();
Assert.That(variables, Is.Empty);

// All
projectVariableClient.Create(new Variable { Key = "Variable1", Value = "test", EnvironmentScope = "test/*" });
projectVariableClient.Create(new Variable { Key = "Variable2", Value = "test", EnvironmentScope = "integration" });
projectVariableClient.Create(new Variable { Key = "Variable3", Value = "test", EnvironmentScope = "*" });
variables = projectVariableClient.All.ToList();
Assert.That(variables, Has.Count.EqualTo(3));
}

[Test]
[NGitLabRetry]
public async Task Test_matching_scoped_variables()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var projectVariableClient = context.Client.GetProjectVariableClient(project.Id);

// Create variable for test environment
projectVariableClient.Create(new Variable
{
Key = "My_Key",
Value = "My value",
Description = "Variable for test environments",
Protected = true,
Type = VariableType.Variable,
Masked = false,
Raw = false,
EnvironmentScope = "test/*",
});

// Create variable for test environment
var variableForIntegration = projectVariableClient.Create(new Variable
{
Key = "My_Key",
Value = "My value",
Description = "Variable for integration environments",
Protected = true,
Type = VariableType.Variable,
Masked = false,
Raw = false,
EnvironmentScope = "integration/*",
});

var specificEnvironment = "integration/datacenter-105-left";
var variablesForSpecificEnvironment = projectVariableClient.All.Where(v => v.IsMatchForEnvironment(specificEnvironment));

Assert.That(variablesForSpecificEnvironment.Count(), Is.EqualTo(1));
}

[Test]
[NGitLabRetry]
public void Test_standalone_matching_scoped_variables()
{
var variable = new Variable
{
Key = "My_Key",
Value = "My value",
Description = "Some important variable",
Protected = true,
Type = VariableType.Variable,
Masked = false,
Raw = false,
EnvironmentScope = "test/*",
};

Assert.That(variable.IsMatchForEnvironment("test/"), Is.True);
fhebel marked this conversation as resolved.
Show resolved Hide resolved
Assert.That(variable.IsMatchForEnvironment("test/123"), Is.True);
Assert.That(variable.IsMatchForEnvironment("integration"), Is.False);

// Change to simple scope
variable.EnvironmentScope = "test";

Assert.That(variable.IsMatchForEnvironment("test"), Is.True);
Assert.That(variable.IsMatchForEnvironment("integration"), Is.False);
Assert.That(variable.IsMatchForEnvironment("integration/abc"), Is.False);
}
}
21 changes: 21 additions & 0 deletions NGitLab/Extensions/VariableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.RegularExpressions;
using NGitLab.Models;

#pragma warning disable MA0009 // Add regex evaluation timeout

namespace NGitLab.Extensions;

public static class VariableExtensions
{
public static bool IsMatchForEnvironment(this Variable variable, string environment)
fhebel marked this conversation as resolved.
Show resolved Hide resolved
{
var sqlStyleLike = variable.EnvironmentScope.Replace('*', '%').Replace('?', '_');

if (string.IsNullOrEmpty(environment))
return false; // or throw exception if source == null
if (string.IsNullOrEmpty(sqlStyleLike))
return false; // or throw exception if like == null

return Regex.IsMatch(environment, "^" + Regex.Escape(sqlStyleLike).Replace("_", ".").Replace("%", ".*") + "$");
}
}
22 changes: 22 additions & 0 deletions NGitLab/IProjectBadgeClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using NGitLab.Models;

namespace NGitLab;
Expand Down Expand Up @@ -28,11 +29,22 @@ public interface IProjectVariableClient

Variable this[string key] { get; }

[EditorBrowsable(EditorBrowsableState.Never)]
Variable Create(VariableCreate model);

Variable Create(Variable model);

[EditorBrowsable(EditorBrowsableState.Never)]
Variable Update(string key, VariableUpdate model);

Variable Update(string key, Variable model);

Variable Update(string key, Variable model, string environmentScope);

[EditorBrowsable(EditorBrowsableState.Never)]
void Delete(string key);

void Delete(string key, string environmentScope);
}

public interface IGroupVariableClient
Expand All @@ -43,7 +55,17 @@ public interface IGroupVariableClient

Variable Create(VariableCreate model);

Variable Create(Variable model);

[EditorBrowsable(EditorBrowsableState.Never)]
Variable Update(string key, VariableUpdate model);

Variable Update(string key, Variable model);

Variable Update(string key, Variable model, string environmentScope);

[EditorBrowsable(EditorBrowsableState.Never)]
void Delete(string key);

void Delete(string key, string environmentScope);
}
Loading