Skip to content

Commit e6a5ef9

Browse files
authored
Add --git-token and --git-username parameters (#174)
1 parent 5f4ecc0 commit e6a5ef9

38 files changed

Lines changed: 434 additions & 154 deletions

src/Confix.Tool/src/Confix.Library/Entities/Component/Providers/Git/GitComponentProvider.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public async Task ExecuteAsync(IComponentProviderContext context)
5252
{
5353
var components = context.ComponentReferences.Where(x => x.Provider == _name && x.IsEnabled);
5454

55-
var refs = await FetchRefsAsync(context.CancellationToken);
55+
var refs = await FetchRefsAsync(context);
5656

5757
var tasks = components
58-
.Select(x => ProcessComponentAsync(x, refs, context.Logger, context.CancellationToken))
58+
.Select(x => ProcessComponentAsync(x, refs, context))
5959
.ToArray();
6060

6161
List<string>? errors = null;
@@ -82,18 +82,20 @@ public async Task ExecuteAsync(IComponentProviderContext context)
8282
}
8383

8484
private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
85-
CancellationToken cancellationToken)
85+
IComponentProviderContext context)
8686
{
8787
var directory =
8888
new DirectoryInfo(Path.Combine(_cloneDirectory.FullName, "__refs"));
8989
var refs = new Dictionary<string, string>();
90+
91+
string gitUrl = GitUrl.Create(_repositoryUrl, context.Parameter);
9092

9193
var sparseConfig =
92-
new GitSparseCheckoutConfiguration(_repositoryUrl, directory.FullName, _arguments);
93-
await _git.SparseCheckoutAsync(sparseConfig, cancellationToken);
94+
new GitSparseCheckoutConfiguration(gitUrl, directory.FullName, _arguments);
95+
await _git.SparseCheckoutAsync(sparseConfig, context.CancellationToken);
9496

9597
var showRefConfig = new GitShowRefsConfiguration(directory.FullName, _arguments);
96-
var refsOutput = await _git.ShowRefsAsync(showRefConfig, cancellationToken);
98+
var refsOutput = await _git.ShowRefsAsync(showRefConfig, context.CancellationToken);
9799

98100
foreach (var line in refsOutput.Split('\n'))
99101
{
@@ -115,8 +117,7 @@ private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
115117
private async Task<ComponentOrError?> ProcessComponentAsync(
116118
ComponentReferenceDefinition definition,
117119
IReadOnlyDictionary<string, string> refs,
118-
IConsoleLogger logger,
119-
CancellationToken cancellationToken)
120+
IComponentProviderContext context)
120121
{
121122
var version = definition.Version ?? "latest";
122123
var componentName = definition.ComponentName;
@@ -129,13 +130,15 @@ private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
129130
directory.EnsureFolder();
130131

131132
var cloneArgument = _arguments.ToList();
133+
134+
string gitUrl = GitUrl.Create(_repositoryUrl, context.Parameter);
132135

133136
var cloneConfiguration = new GitCloneConfiguration(
134-
_repositoryUrl,
137+
gitUrl,
135138
directory.FullName,
136139
cloneArgument.ToArray());
137140

138-
await _git.CloneAsync(cloneConfiguration, cancellationToken);
141+
await _git.CloneAsync(cloneConfiguration, context.CancellationToken);
139142

140143
if (version is not "latest")
141144
{
@@ -147,7 +150,7 @@ private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
147150

148151
await _git.CheckoutAsync(
149152
new GitCheckoutConfiguration(directory.FullName, hash, cloneArgument.ToArray()),
150-
cancellationToken);
153+
context.CancellationToken);
151154
}
152155

153156
var pathToComponent = Path
@@ -159,7 +162,7 @@ await _git.CheckoutAsync(
159162
$"Could not find component {componentName} ({version}) in git repository");
160163
}
161164

162-
logger.FoundComponent(componentName, version);
165+
context.Logger.FoundComponent(componentName, version);
163166

164167
var json = JsonSchema.FromFile(pathToComponent);
165168
var component =
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Confix.Tool.Common.Pipelines;
2+
3+
namespace Confix.Tool.Entities.Components.Git;
4+
5+
public static class GitUrl
6+
{
7+
public static string Create(string repositoryUrl, IParameterCollection parameters)
8+
{
9+
parameters.TryGet(GitUsernameOptions.Instance, out string? username);
10+
parameters.TryGet(GitTokenOptions.Instance, out string? token);
11+
12+
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(token))
13+
{
14+
if (repositoryUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
15+
{
16+
var uri = new Uri(repositoryUrl);
17+
var builder = new UriBuilder(uri)
18+
{
19+
UserName = username,
20+
Password = token
21+
};
22+
return builder.Uri.ToString();
23+
}
24+
}
25+
26+
return repositoryUrl;
27+
}
28+
}

src/Confix.Tool/src/Confix.Library/Middlewares/Project/BuildProjectMiddleware.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public sealed class BuildProjectMiddleware : IMiddleware
99
/// <inheritdoc />
1010
public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate next)
1111
{
12+
var variableContext = new VariableProviderContext(
13+
context.Parameter,
14+
context.CancellationToken);
15+
1216
context.SetStatus("Building the project files");
1317

1418
var cancellationToken = context.CancellationToken;
@@ -32,7 +36,7 @@ public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate nex
3236
context.Logger.ReplaceVariablesOfConfigurationFile(file);
3337

3438
file.Content = await variableReplacer
35-
.RewriteOrThrowAsync(content, context.CancellationToken);
39+
.RewriteOrThrowAsync(content, variableContext);
3640
}
3741

3842
await next(context);

src/Confix.Tool/src/Confix.Library/Middlewares/Project/RestoreProjectMiddleware.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Confix.Tool.Entities.Components.DotNet;
66
using Confix.Tool.Middlewares.JsonSchemas;
77
using Confix.Tool.Schema;
8+
using Confix.Variables;
89

910
namespace Confix.Tool.Middlewares.Project;
1011

@@ -30,6 +31,10 @@ public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate nex
3031
var jsonSchemas = context.Features.Get<JsonSchemaFeature>();
3132
var configuration = context.Features.Get<ConfigurationFeature>();
3233
var files = context.Features.Get<ConfigurationFileFeature>().Files;
34+
35+
var variableContext = new VariableProviderContext(
36+
context.Parameter,
37+
context.CancellationToken);
3338

3439
configuration.EnsureProjectScope();
3540

@@ -42,7 +47,7 @@ public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate nex
4247

4348
context.SetStatus("Loading variables...");
4449
var variableResolver = context.Features.Get<VariablesFeature>().Resolver;
45-
var variables = await variableResolver.ListVariables(cancellationToken);
50+
var variables = await variableResolver.ListVariables(variableContext);
4651

4752
context.SetStatus("Composing the schema...");
4853
var jsonSchema = _projectComposer.Compose(components, variables);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.CommandLine;
2+
3+
namespace Confix.Tool;
4+
5+
internal sealed class GitTokenOptions : Option<string>
6+
{
7+
public static GitTokenOptions Instance { get; } = new();
8+
9+
private GitTokenOptions()
10+
: base("--git-token")
11+
{
12+
Arity = ArgumentArity.ZeroOrOne;
13+
Description = "The token used for git authentication.";
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.CommandLine;
2+
3+
namespace Confix.Tool;
4+
5+
internal sealed class GitUsernameOptions : Option<string>
6+
{
7+
public static GitUsernameOptions Instance { get; } = new();
8+
9+
private GitUsernameOptions()
10+
: base("--git-username")
11+
{
12+
Arity = ArgumentArity.ZeroOrOne;
13+
Description = "The username used for git authentication.";
14+
}
15+
}

src/Confix.Tool/src/Confix.Library/Pipelines/BuildCommandPipeline.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ protected override void Configure(IPipelineDescriptor builder)
1717
.AddOption(ActiveEnvironmentOption.Instance)
1818
.AddOption(OutputFileOption.Instance)
1919
.AddOption(EncryptionOption.Instance)
20+
.AddOption(GitUsernameOptions.Instance)
21+
.AddOption(GitTokenOptions.Instance)
2022
.UseHandler(InvokeAsync);
2123
}
2224

src/Confix.Tool/src/Confix.Library/Pipelines/Project/ProjectBuildPipeline.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ protected override void Configure(IPipelineDescriptor builder)
1212
{
1313
builder
1414
.AddOption(NoRestoreOptions.Instance)
15+
.AddOption(GitUsernameOptions.Instance)
16+
.AddOption(GitTokenOptions.Instance)
1517
.Use<LoadConfigurationMiddleware>()
1618
.UseReadConfigurationFiles()
1719
.UseEnvironment()

src/Confix.Tool/src/Confix.Library/Pipelines/Reporting/ProjectReportMiddleware.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Confix.Tool.Middlewares.Reporting;
1010
using Confix.Tool.Schema;
1111
using Confix.Utilities;
12+
using Confix.Variables;
1213
using Microsoft.Extensions.DependencyInjection;
1314

1415
namespace Confix.Tool.Reporting;
@@ -215,11 +216,18 @@ private static async Task<List<VariableReport>> GetVariablesAsync(
215216
ConfigurationFile file,
216217
CancellationToken ct)
217218
{
219+
var variableContext = new VariableProviderContext(
220+
context.Parameter,
221+
context.CancellationToken);
222+
218223
var variablesFeature = context.Features.Get<VariablesFeature>();
219224
var variables = new List<VariableReport>();
220225

221226
var content = await file.TryLoadContentAsync(ct);
222-
foreach (var variable in await variablesFeature.Extractor.ExtractAsync(content, ct))
227+
var variableInfos = await variablesFeature.Extractor
228+
.ExtractAsync(content, variableContext);
229+
230+
foreach (var variable in variableInfos)
223231
{
224232
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(variable.VariableValue));
225233

src/Confix.Tool/src/Confix.Library/Pipelines/RestoreCommandPipeline.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ protected override void Configure(IPipelineDescriptor builder)
1313
{
1414
builder
1515
.AddOption(DotnetConfigurationOptions.Instance)
16+
.AddOption(GitUsernameOptions.Instance)
17+
.AddOption(GitTokenOptions.Instance)
1618
.Use<LoadConfigurationMiddleware>()
1719
.UseEnvironment()
1820
.UseHandler(InvokeAsync);

0 commit comments

Comments
 (0)