-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGitHubRepositoryInfo.cs
More file actions
104 lines (84 loc) · 3.27 KB
/
GitHubRepositoryInfo.cs
File metadata and controls
104 lines (84 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using Initialization.Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ModularPipelines.Git;
using ModularPipelines.Git.Options;
using ModularPipelines.Options;
// ReSharper disable ConvertToPrimaryConstructor
namespace ModularPipelines.GitHub;
[ExcludeFromCodeCoverage]
internal record GitHubRepositoryInfo : IGitHubRepositoryInfo, IInitializer
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<GitHubRepositoryInfo> _logger;
public bool IsInitialized { get; private set; }
public string? Url { get; private set; }
public string? Endpoint { get; private set; }
public string? Owner { get; private set; }
public string? RepositoryName { get; private set; }
public GitHubRepositoryInfo(IServiceProvider serviceProvider, ILogger<GitHubRepositoryInfo> logger)
{
_serviceProvider = serviceProvider;
_logger = logger;
}
public async Task InitializeAsync()
{
if (IsInitialized)
{
return;
}
try
{
await using var scope = _serviceProvider.CreateAsyncScope();
var git = scope.ServiceProvider.GetRequiredService<IGit>();
var options = new GitRemoteOptions
{
Arguments = ["get-url", "origin"],
};
var executionOptions = new CommandExecutionOptions
{
ThrowOnNonZeroExitCode = false,
LogSettings = scope.ServiceProvider
.GetRequiredService<IOptions<LoggerFilterOptions>>()
.Value
.MinLevel == LogLevel.Debug
? CommandLoggingOptions.Diagnostic
: CommandLoggingOptions.Silent,
};
var remote = await git.Commands.Remote(options, executionOptions);
var remoteUrl = remote.StandardOutput;
if (string.IsNullOrEmpty(remoteUrl))
{
_logger.LogWarning("Error when detecting GitHub git repository: {Error}", remote.StandardError);
// Will not initialize as git repo is not setup
return;
}
// Parse owner and repository name from the remote URL
var endpoint = "github";
var sshPattern = $@"git@{endpoint}\.com:(?<owner>.+)/(?<name>.+)\.git";
var httpsPattern = $@"https://(.*@)?{endpoint}\.com/(?<owner>.+)/(?<name>.+)(\.git)?";
var match = Regex.Match(remoteUrl, sshPattern);
if (!match.Success)
{
match = Regex.Match(remoteUrl, httpsPattern);
}
if (!match.Success)
{
// Will not initialize as git repo is not setup
return;
}
Url = remoteUrl;
Endpoint = endpoint;
Owner = match.Groups["owner"].Value;
RepositoryName = match.Groups["name"].Value;
IsInitialized = true;
}
catch (Exception e) when (e is not (OutOfMemoryException or StackOverflowException))
{
System.Console.WriteLine(e);
}
}
}