From aa6a2d0d4813ddf0556130756e7e9d1d89b0a8ab Mon Sep 17 00:00:00 2001 From: Marcin Torba Date: Thu, 5 Sep 2024 18:55:23 +0200 Subject: [PATCH] Remove redundant options and fix switch to default branch when requires origin checkout. --- src/DependencyUpdated.Core/Config/Project.cs | 19 ++++- .../Config/UpdaterConfig.cs | 8 +++ .../AzureDevOps.cs | 71 +++++++++++-------- src/DependencyUpdated/Options.cs | 3 - src/DependencyUpdated/Program.cs | 7 +- 5 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/DependencyUpdated.Core/Config/Project.cs b/src/DependencyUpdated.Core/Config/Project.cs index be31cb3..c4d675e 100644 --- a/src/DependencyUpdated.Core/Config/Project.cs +++ b/src/DependencyUpdated.Core/Config/Project.cs @@ -10,11 +10,11 @@ public sealed class Project : IValidatableObject public string Name { get; set; } = default!; - public string[] DependencyConfigurations { get; set; } = ["https://api.nuget.org/v3/index.json"]; + public string[] DependencyConfigurations { get; set; } = []; - public string[] Directories { get; set; } = ArraySegment.Empty.ToArray(); + public string[] Directories { get; set; } = []; - public string[] Groups { get; set; } = ["*"]; + public string[] Groups { get; set; } = []; public IEnumerable Validate(ValidationContext validationContext) { @@ -38,4 +38,17 @@ public IEnumerable Validate(ValidationContext validationContex yield return new ValidationResult($"Missing ${nameof(Groups)}."); } } + + public void ApplyDefaultValue() + { + if (DependencyConfigurations.Length == 0 && Type == ProjectType.DotNet) + { + DependencyConfigurations = ["https://api.nuget.org/v3/index.json"]; + } + + if (Groups.Length == 0) + { + Groups = ["*"]; + } + } } \ No newline at end of file diff --git a/src/DependencyUpdated.Core/Config/UpdaterConfig.cs b/src/DependencyUpdated.Core/Config/UpdaterConfig.cs index 5e0eee8..22b1f0c 100644 --- a/src/DependencyUpdated.Core/Config/UpdaterConfig.cs +++ b/src/DependencyUpdated.Core/Config/UpdaterConfig.cs @@ -46,4 +46,12 @@ public IEnumerable Validate(ValidationContext validationContex } } } + + public void ApplyDefaultValues() + { + foreach (var project in Projects) + { + project.ApplyDefaultValue(); + } + } } \ No newline at end of file diff --git a/src/DependencyUpdated.Repositories.AzureDevOps/AzureDevOps.cs b/src/DependencyUpdated.Repositories.AzureDevOps/AzureDevOps.cs index 5a7820a..e79510f 100644 --- a/src/DependencyUpdated.Repositories.AzureDevOps/AzureDevOps.cs +++ b/src/DependencyUpdated.Repositories.AzureDevOps/AzureDevOps.cs @@ -2,6 +2,7 @@ using DependencyUpdated.Core.Config; using DependencyUpdated.Repositories.AzureDevOps.Dto; using LibGit2Sharp; +using LibGit2Sharp.Handlers; using Microsoft.Extensions.Options; using Serilog; using System.Net; @@ -15,14 +16,22 @@ internal sealed class AzureDevOps(TimeProvider timeProvider, IOptions.Empty, options, string.Empty); + var branch = repo.Branches[branchName] ?? repo.Branches[$"{RemoteName}/{branchName}"]; + + if (branch == null) + { + throw new InvalidOperationException($"Branch {branchName} doesn't exist"); + } Commands.Checkout(repo, branch); } @@ -53,27 +62,14 @@ public void CommitChanges(string repositoryPath, string projectName, string grou { var gitBranchName = CreateGitBranchName(projectName, config.Value.AzureDevOps.BranchName, group); logger.Information("Commiting {Repository} to branch {Branch}", repositoryPath, gitBranchName); - using (var repo = new Repository(repositoryPath)) - { - Commands.Stage(repo, "*"); - var author = new Signature(config.Value.AzureDevOps.Username, config.Value.AzureDevOps.Email, - timeProvider.GetUtcNow()); - repo.Commit(GitCommitMessage, author, author); - var options = new PushOptions(); - - if (!string.IsNullOrEmpty(config.Value.AzureDevOps.Username) && - !string.IsNullOrEmpty(config.Value.AzureDevOps.PAT)) - { - options.CredentialsProvider = (_, _, _) => - new UsernamePasswordCredentials() - { - Username = config.Value.AzureDevOps.Username, - Password = config.Value.AzureDevOps.PAT - }; - } - - repo.Network.Push(repo.Branches[gitBranchName], options); - } + using var repo = new Repository(repositoryPath); + Commands.Stage(repo, "*"); + var author = new Signature(config.Value.AzureDevOps.Username, config.Value.AzureDevOps.Email, + timeProvider.GetUtcNow()); + repo.Commit(GitCommitMessage, author, author); + var options = new PushOptions(); + options.CredentialsProvider = CreateGitCredentialsProvider(); + repo.Network.Push(repo.Branches[gitBranchName], options); } public async Task SubmitPullRequest(IReadOnlyCollection updates, string projectName, string group) @@ -106,11 +102,8 @@ public async Task SubmitPullRequest(IReadOnlyCollection updates, s } var response = await result.Content.ReadAsStringAsync(); - var options = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true, - }; - var responseObject = JsonSerializer.Deserialize(response, options) ?? + var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }; + var responseObject = JsonSerializer.Deserialize(response, options) ?? throw new InvalidOperationException("Missing response from API"); logger.Information("New PR created {Id}", responseObject.PullRequestId); @@ -154,10 +147,10 @@ public async Task SubmitPullRequest(IReadOnlyCollection updates, s result.EnsureSuccessStatusCode(); } } - + private static string CreateGitBranchName(string projectName, string branchName, string group) { - return $"{projectName.ToLower()}/{branchName.ToLower()}/{group.ToLower().Replace("*", "-asterix-")}"; + return $"{projectName.ToLower()}/{branchName.ToLower()}/{group.ToLower().Replace("*", "asterix")}"; } private string CreatePrDescription(IReadOnlyCollection updates) @@ -173,4 +166,22 @@ private string CreatePrDescription(IReadOnlyCollection updates) return stringBuilder.ToString(); } + + private CredentialsHandler? CreateGitCredentialsProvider() + { + if (string.IsNullOrEmpty(config.Value.AzureDevOps.Username)) + { + return null; + } + + if (string.IsNullOrEmpty(config.Value.AzureDevOps.PAT)) + { + return null; + } + + return (_, _, _) => new UsernamePasswordCredentials() + { + Username = config.Value.AzureDevOps.Username, Password = config.Value.AzureDevOps.PAT + }; + } } \ No newline at end of file diff --git a/src/DependencyUpdated/Options.cs b/src/DependencyUpdated/Options.cs index 2152023..15d5bbd 100644 --- a/src/DependencyUpdated/Options.cs +++ b/src/DependencyUpdated/Options.cs @@ -6,7 +6,4 @@ public class Options { [Option('c', "configPath", Required = false, HelpText = "Path for the configuration file.")] public string? ConfigPath { get; set; } - - [Option('r', "repoPath", Required = false, HelpText = "Path for the repository folder.")] - public string? RepositoryPath { get; set; } } \ No newline at end of file diff --git a/src/DependencyUpdated/Program.cs b/src/DependencyUpdated/Program.cs index 29ea647..0dc953e 100644 --- a/src/DependencyUpdated/Program.cs +++ b/src/DependencyUpdated/Program.cs @@ -30,6 +30,7 @@ private static async Task RunApplication(Options options) ConfigureServices(); var config = _serviceProvider.GetRequiredService>(); + config.Value.ApplyDefaultValues(); var validationResult = config.Value.Validate(new ValidationContext(config.Value)).ToList(); if (validationResult.Count != 0) { @@ -39,11 +40,7 @@ private static async Task RunApplication(Options options) var repositoryProvider = _serviceProvider.GetRequiredKeyedService(config.Value.RepositoryType); - var repositoryPath = string.IsNullOrEmpty(options.RepositoryPath) - ? Environment.CurrentDirectory - : options.RepositoryPath; - - Directory.SetCurrentDirectory(repositoryPath); + var repositoryPath = Environment.CurrentDirectory; foreach (var configEntry in config.Value.Projects) {