Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9</LangVersion>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<Authors>Denis Ivanov</Authors>
<FileVersion>1.0.0</FileVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<AssemblyVersion>2.0.0</AssemblyVersion>
<Description>Provides GitLab variables configuration provider</Description>
<PackageTags>GitLab;configuration; ASP.NET Core</PackageTags>
<RepositoryUrl>https://github.com/denis-ivanov/Microsoft.Extensions.Configuration.GitLab</RepositoryUrl>
Expand All @@ -16,8 +15,21 @@

<ItemGroup>
<PackageReference Include="GitLabApiClient" Version="1.8.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.*" />
</ItemGroup>

<ItemGroup>
Expand Down
108 changes: 37 additions & 71 deletions Extensions.Configuration.GitLab/GitLabConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,48 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Extensions.Configuration.GitLab
namespace Extensions.Configuration.GitLab;

public static class GitLabConfigurationExtensions
{
public static class GitLabConfigurationExtensions
public static IConfigurationBuilder AddGitLab(
[NotNull] this IConfigurationBuilder builder,
[NotNull] string hostUrl,
[NotNull] string projectId,
[NotNull] string authenticationToken,
[NotNull] string environmentName)
{
public static IConfigurationBuilder AddGitLab(
[NotNull] this IConfigurationBuilder builder,
[NotNull] string hostUrl,
[NotNull] string projectId,
[NotNull] string authenticationToken,
[NotNull] string environmentName)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (hostUrl == null)
{
throw new ArgumentNullException(nameof(hostUrl));
}

if (projectId == null)
{
throw new ArgumentNullException(nameof(projectId));
}

if (authenticationToken == null)
{
throw new ArgumentNullException(nameof(authenticationToken));
}

if (environmentName == null)
{
throw new ArgumentNullException(nameof(environmentName));
}

var options = new GitLabConfigurationOptions(hostUrl, projectId, authenticationToken, environmentName);
return builder.AddGitLab(options);
}

public static IConfigurationBuilder AddGitLab(
[NotNull] this IConfigurationBuilder builder,
[NotNull] Action<GitLabConfigurationOptions> configure)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(hostUrl);
ArgumentNullException.ThrowIfNull(projectId);
ArgumentNullException.ThrowIfNull(authenticationToken);
ArgumentNullException.ThrowIfNull(environmentName);

var options = new GitLabConfigurationOptions(hostUrl, projectId, authenticationToken, environmentName);
return builder.AddGitLab(options);
}

var options = new GitLabConfigurationOptions();
configure(options);
return builder.AddGitLab(options);
}
public static IConfigurationBuilder AddGitLab(
[NotNull] this IConfigurationBuilder builder,
[NotNull] Action<GitLabConfigurationOptions> configure)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configure);

public static IConfigurationBuilder AddGitLab(
[NotNull] this IConfigurationBuilder builder,
[NotNull] GitLabConfigurationOptions options)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
var options = new GitLabConfigurationOptions();
configure(options);
return builder.AddGitLab(options);
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
public static IConfigurationBuilder AddGitLab(
[NotNull] this IConfigurationBuilder builder,
[NotNull] GitLabConfigurationOptions options)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(options);

var gitlabClient = new GitLabClient(options.HostUrl, options.AuthenticationToken);
var source = new GitLabConfigurationSource(gitlabClient, options);
return builder.Add(source);
}
var gitlabClient = new GitLabClient(options.HostUrl, options.AuthenticationToken);
var source = new GitLabConfigurationSource(gitlabClient, options);
return builder.Add(source);
}
}
127 changes: 63 additions & 64 deletions Extensions.Configuration.GitLab/GitLabConfigurationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,87 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Extensions.Configuration.GitLab
namespace Extensions.Configuration.GitLab;

public class GitLabConfigurationOptions
{
public class GitLabConfigurationOptions
public GitLabConfigurationOptions()
{
public GitLabConfigurationOptions()
{
}
}

public GitLabConfigurationOptions(
[NotNull] string hostUrl,
[NotNull] string projectId,
[NotNull] string authenticationToken,
[NotNull] string environmentName)
{
HostUrl = hostUrl ?? throw new ArgumentNullException(nameof(hostUrl));
ProjectId = projectId ?? throw new ArgumentNullException(nameof(projectId));
AuthenticationToken = authenticationToken ?? throw new ArgumentNullException(nameof(authenticationToken));
EnvironmentName = environmentName ?? throw new ArgumentNullException(nameof(environmentName));
}
public GitLabConfigurationOptions(
[NotNull] string hostUrl,
[NotNull] string projectId,
[NotNull] string authenticationToken,
[NotNull] string environmentName)
{
HostUrl = hostUrl ?? throw new ArgumentNullException(nameof(hostUrl));
ProjectId = projectId ?? throw new ArgumentNullException(nameof(projectId));
AuthenticationToken = authenticationToken ?? throw new ArgumentNullException(nameof(authenticationToken));
EnvironmentName = environmentName ?? throw new ArgumentNullException(nameof(environmentName));
}

public TimeSpan ReloadInterval { get; set; } = TimeSpan.FromSeconds(3);
public TimeSpan ReloadInterval { get; set; } = TimeSpan.FromSeconds(3);

public string HostUrl { get; set; }
public string HostUrl { get; set; }

public string AuthenticationToken { get; set; }
public string AuthenticationToken { get; set; }

public string ProjectId { get; set; }
public string ProjectId { get; set; }

public string EnvironmentName { get; set; } = "*";
public string EnvironmentName { get; set; } = "*";

public Func<string, string> KeyNormalizer { get; set; } = NormalizeKey;
public Func<string, string> KeyNormalizer { get; set; } = NormalizeKey;

public GitLabConfigurationOptions WithReloadInterval(TimeSpan reloadInterval)
{
ReloadInterval = reloadInterval;
return this;
}
public GitLabConfigurationOptions WithReloadInterval(TimeSpan reloadInterval)
{
ReloadInterval = reloadInterval;
return this;
}

public GitLabConfigurationOptions WithHostUrl([NotNull] string hostUrl)
{
HostUrl = hostUrl ?? throw new ArgumentNullException(nameof(hostUrl));
return this;
}
public GitLabConfigurationOptions WithHostUrl([NotNull] string hostUrl)
{
HostUrl = hostUrl ?? throw new ArgumentNullException(nameof(hostUrl));
return this;
}

public GitLabConfigurationOptions WithAuthenticationToken([NotNull] string authenticationToken)
{
AuthenticationToken = authenticationToken ?? throw new ArgumentNullException(nameof(authenticationToken));
return this;
}
public GitLabConfigurationOptions WithAuthenticationToken([NotNull] string authenticationToken)
{
AuthenticationToken = authenticationToken ?? throw new ArgumentNullException(nameof(authenticationToken));
return this;
}

public GitLabConfigurationOptions WithProjectId([NotNull] string projectId)
{
ProjectId = projectId ?? throw new ArgumentNullException(nameof(projectId));
return this;
}
public GitLabConfigurationOptions WithProjectId([NotNull] string projectId)
{
ProjectId = projectId ?? throw new ArgumentNullException(nameof(projectId));
return this;
}

public GitLabConfigurationOptions WithEnvironmentName([NotNull] string environmentName)
{
EnvironmentName = environmentName ?? throw new ArgumentNullException(nameof(environmentName));
return this;
}
public GitLabConfigurationOptions WithEnvironmentName([NotNull] string environmentName)
{
EnvironmentName = environmentName ?? throw new ArgumentNullException(nameof(environmentName));
return this;
}

public GitLabConfigurationOptions WithKeyNormalizer([NotNull] Func<string, string> keyNormalizer)
{
KeyNormalizer = keyNormalizer ?? throw new ArgumentNullException(nameof(keyNormalizer));
return this;
}
public GitLabConfigurationOptions WithKeyNormalizer([NotNull] Func<string, string> keyNormalizer)
{
KeyNormalizer = keyNormalizer ?? throw new ArgumentNullException(nameof(keyNormalizer));
return this;
}

private static string NormalizeKey(string key)
private static string NormalizeKey(string key)
{
if (string.IsNullOrEmpty(key))
{
if (string.IsNullOrEmpty(key))
{
return key;
}
return key;
}

var segments = Array.ConvertAll(key.Split('_'), e =>
{
e = e.ToLower();
return e.Length <= 1 ? e : char.ToUpper(e[0]) + e.Substring(1);
});
var segments = Array.ConvertAll(key.Split('_'), e =>
{
e = e.ToLower();
return e.Length <= 1 ? e : char.ToUpper(e[0]) + e.Substring(1);
});

return string.Join(ConfigurationPath.KeyDelimiter, segments);
}
return string.Join(ConfigurationPath.KeyDelimiter, segments);
}
}
Loading