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 all 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
12 changes: 9 additions & 3 deletions NGitLab.Mock/Clients/GroupVariableClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public GroupVariableClient(ClientContext context, GroupId groupId)
_groupId = Server.AllGroups.FindGroup(groupId.ValueAsString()).Id;
}

public Variable this[string key] => throw new NotImplementedException();
public Variable this[string key] => this[key, null];

public Variable this[string key, string environmentScope] => throw new NotImplementedException();

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

Expand All @@ -23,12 +25,16 @@ public Variable Create(VariableCreate model)
throw new NotImplementedException();
}

public void Delete(string key)
public void Delete(string key) => Delete(key, null);

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

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

public Variable Update(string key, string environmentScope, VariableUpdate model)
{
throw new NotImplementedException();
}
Expand Down
12 changes: 9 additions & 3 deletions NGitLab.Mock/Clients/ProjectVariableClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public ProjectVariableClient(ClientContext context, ProjectId projectId)
_projectId = Server.AllProjects.FindProject(projectId.ValueAsString()).Id;
}

public Variable this[string key] => throw new NotImplementedException();
public Variable this[string key] => this[key, null];

public Variable this[string key, string environmentScope] => throw new NotImplementedException();

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

Expand All @@ -23,12 +25,16 @@ public Variable Create(VariableCreate model)
throw new NotImplementedException();
}

public void Delete(string key)
public void Delete(string key) => Delete(key, null);

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

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

public Variable Update(string key, string environmentScope, VariableUpdate model)
{
throw new NotImplementedException();
}
Expand Down
65 changes: 65 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,67 @@ 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 VariableCreate
{
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, variable.EnvironmentScope, new VariableUpdate
{
Value = "My value edited",
Protected = false,
EnvironmentScope = newScope
});

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 VariableCreate { Key = "Variable1", Value = "test", EnvironmentScope = "test/*" });
groupVariableClient.Create(new VariableCreate { Key = "Variable2", Value = "test", EnvironmentScope = "integration" });
groupVariableClient.Create(new VariableCreate { Key = "Variable3", Value = "test", EnvironmentScope = "*" });
variables = groupVariableClient.All.ToList();
Assert.That(variables, Has.Count.EqualTo(3));
}
}
93 changes: 93 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,94 @@ 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 firstScope = "test/*";
var changingScopeVariable = projectVariableClient.Create(new VariableCreate
{
Key = "My_Key",
Value = "My value",
Description = "Some important variable",
Protected = true,
Type = VariableType.Variable,
Masked = false,
Raw = false,
EnvironmentScope = firstScope,
});

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

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

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

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

// Check single access with scoped variables
Assert.That(projectVariableClient[changingScopeVariable.Key, changingScopeVariable.EnvironmentScope], Is.Not.Null);

var exMultipleVariables = Assert.Throws<GitLabException>(() =>
{
var dummy = projectVariableClient[changingScopeVariable.Key];
});
Assert.That(exMultipleVariables?.ErrorMessage, Is.EqualTo("There are multiple variables with provided parameters. Please use 'filter[environment_scope]'"));

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

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

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

projectVariableClient.Delete(changingScopeVariable.Key, newScope);
projectVariableClient.Delete(integrationVariable.Key, integrationScope);

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

// All
projectVariableClient.Create(new VariableCreate { Key = "Variable1", Value = "test", EnvironmentScope = "test/*" });
projectVariableClient.Create(new VariableCreate { Key = "Variable2", Value = "test", EnvironmentScope = "integration" });
projectVariableClient.Create(new VariableCreate { Key = "Variable3", Value = "test", EnvironmentScope = "*" });
variables = projectVariableClient.All.ToList();
Assert.That(variables, Has.Count.EqualTo(3));
}
}
12 changes: 12 additions & 0 deletions NGitLab/IProjectBadgeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ public interface IProjectVariableClient

Variable this[string key] { get; }

Variable this[string key, string environmentScope] { get; }

Variable Create(VariableCreate model);

Variable Update(string key, VariableUpdate model);

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

void Delete(string key);

void Delete(string key, string environmentScope);
}

public interface IGroupVariableClient
Expand All @@ -41,9 +47,15 @@ public interface IGroupVariableClient

Variable this[string key] { get; }

Variable this[string key, string environmentScope] { get; }

Variable Create(VariableCreate model);

Variable Update(string key, VariableUpdate model);

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

void Delete(string key);

void Delete(string key, string environmentScope);
}
17 changes: 13 additions & 4 deletions NGitLab/Impl/VariableClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using NGitLab.Models;

namespace NGitLab.Impl;
Expand All @@ -8,6 +9,8 @@ internal abstract class VariableClient
private readonly string _urlPrefix;
private readonly API _api;

private string EnvironmentScopeFilter(string environmentScope = null) => !string.IsNullOrWhiteSpace(environmentScope) ? $"?filter[environment_scope]={Uri.EscapeDataString(environmentScope)}" : string.Empty;

protected VariableClient(API api, string urlPrefix)
{
_urlPrefix = urlPrefix;
Expand All @@ -16,11 +19,17 @@ protected VariableClient(API api, string urlPrefix)

public IEnumerable<Variable> All => _api.Get().GetAll<Variable>(_urlPrefix + "/variables");

public Variable this[string key] => _api.Get().To<Variable>(_urlPrefix + "/variables/" + key);
public Variable this[string key] => this[key, null];

public Variable this[string key, string environmentScope] => _api.Get().To<Variable>($"{_urlPrefix}/variables/{key}{EnvironmentScopeFilter(environmentScope)}");

public Variable Create(VariableCreate model) => _api.Post().With(model).To<Variable>(_urlPrefix + "/variables");

public Variable Update(string key, VariableUpdate model) => _api.Put().With(model).To<Variable>(_urlPrefix + "/variables/" + key);
public Variable Update(string key, VariableUpdate model) => Update(key, null, model);

public Variable Update(string key, string environmentScope, VariableUpdate model) => _api.Put().With(model).To<Variable>($"{_urlPrefix}/variables/{key}{EnvironmentScopeFilter(environmentScope)}");

public void Delete(string key) => Delete(key, null);

public void Delete(string key) => _api.Delete().Execute(_urlPrefix + "/variables/" + key);
public void Delete(string key, string environmentScope) => _api.Delete().Execute($"{_urlPrefix}/variables/{key}{EnvironmentScopeFilter(environmentScope)}");
}
30 changes: 28 additions & 2 deletions NGitLab/Models/Variable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace NGitLab.Models;

Expand All @@ -10,6 +11,14 @@ public class Variable
[JsonPropertyName("value")]
public string Value { get; set; }

/// <summary>
/// The description of a variable
/// </summary>
/// <returns>The description of a variable</returns>
/// <remarks>Introduced in GitLab 16.2</remarks>
[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("protected")]
public bool Protected { get; set; }

Expand All @@ -19,6 +28,23 @@ public class Variable
[JsonPropertyName("masked")]
public bool Masked { get; set; }

[JsonPropertyName("raw")]
public bool Raw { get; set; }

[EditorBrowsable(EditorBrowsableState.Never)]
public string Scope
{
get => EnvironmentScope;
set => EnvironmentScope = value;
}

/// <summary>
/// The environment scope of a variable
/// </summary>
/// <remarks>
/// Create and Update of project variable: All tiers (Free, Premium, Ultimate).<br/>
/// Create and Update of group variable: Premium and Ultimate only.
/// </remarks>
[JsonPropertyName("environment_scope")]
fhebel marked this conversation as resolved.
Show resolved Hide resolved
public string Scope { get; set; }
public string EnvironmentScope { get; set; }
}
Loading