diff --git a/src/Credfeto.DotNet.Repo.Tools.Packages/Services/BulkPackageUpdater.cs b/src/Credfeto.DotNet.Repo.Tools.Packages/Services/BulkPackageUpdater.cs index ec14bc36..5daf78f7 100644 --- a/src/Credfeto.DotNet.Repo.Tools.Packages/Services/BulkPackageUpdater.cs +++ b/src/Credfeto.DotNet.Repo.Tools.Packages/Services/BulkPackageUpdater.cs @@ -81,8 +81,7 @@ public async ValueTask BulkUpdateAsync(string templateRepository, IReadOnlyList packages = await this._bulkPackageConfigLoader.LoadAsync(path: packagesFileName, cancellationToken: cancellationToken); - using (IGitRepository templateRepo = - await this._gitRepositoryFactory.OpenOrCloneAsync(workDir: workFolder, repoUrl: templateRepository, cancellationToken: cancellationToken)) + using (IGitRepository templateRepo = await this._gitRepositoryFactory.OpenOrCloneAsync(workDir: workFolder, repoUrl: templateRepository, cancellationToken: cancellationToken)) { PackageUpdateContext updateContext = await this.BuildUpdateContextAsync(cacheFileName: cacheFileName, templateRepo: templateRepo, @@ -106,10 +105,7 @@ await this._gitRepositoryFactory.OpenOrCloneAsync(workDir: workFolder, repoUrl: } } - public async ValueTask UpdateRepositoriesAsync(PackageUpdateContext updateContext, - IReadOnlyList repositories, - IReadOnlyList packages, - CancellationToken cancellationToken) + public async ValueTask UpdateRepositoriesAsync(PackageUpdateContext updateContext, IReadOnlyList repositories, IReadOnlyList packages, CancellationToken cancellationToken) { try { @@ -148,10 +144,7 @@ public async ValueTask UpdateRepositoriesAsync(PackageUpdateContext updateContex } } - private async ValueTask UpdateCachedPackagesAsync(string workFolder, - IReadOnlyList packages, - PackageUpdateContext updateContext, - CancellationToken cancellationToken) + private async ValueTask UpdateCachedPackagesAsync(string workFolder, IReadOnlyList packages, PackageUpdateContext updateContext, CancellationToken cancellationToken) { IReadOnlyList allPackages = this._packageCache.GetAll(); @@ -223,8 +216,7 @@ private async ValueTask UpdateRepositoryAsync(PackageUpdateContext updateContext { this._logger.LogProcessingRepo(repo); - using (IGitRepository repository = - await this._gitRepositoryFactory.OpenOrCloneAsync(workDir: updateContext.WorkFolder, repoUrl: repo, cancellationToken: cancellationToken)) + using (IGitRepository repository = await this._gitRepositoryFactory.OpenOrCloneAsync(workDir: updateContext.WorkFolder, repoUrl: repo, cancellationToken: cancellationToken)) { if (!ChangeLogDetector.TryFindChangeLog(repository: repository.Active, out string? changeLogFileName)) { @@ -243,20 +235,14 @@ await this._trackingCache.UpdateTrackingAsync(new(Repository: repository, Change } } - private async ValueTask ProcessRepoUpdatesAsync(PackageUpdateContext updateContext, - RepoContext repoContext, - IReadOnlyList packages, - CancellationToken cancellationToken) + private async ValueTask ProcessRepoUpdatesAsync(PackageUpdateContext updateContext, RepoContext repoContext, IReadOnlyList packages, CancellationToken cancellationToken) { string? lastKnownGoodBuild = this._trackingCache.Get(repoContext.ClonePath); if (!repoContext.HasDotNetFiles(out string? sourceDirectory, out IReadOnlyList? solutions, out IReadOnlyList? projects)) { this._logger.LogNoDotNetFilesFound(); - await this._trackingCache.UpdateTrackingAsync(repoContext: repoContext, - updateContext: updateContext, - value: repoContext.Repository.HeadRev, - cancellationToken: cancellationToken); + await this._trackingCache.UpdateTrackingAsync(repoContext: repoContext, updateContext: updateContext, value: repoContext.Repository.HeadRev, cancellationToken: cancellationToken); return; } @@ -317,8 +303,7 @@ await this._releaseGeneration.TryCreateNextPatchAsync(repoContext: repoContext, await this._trackingCache.UpdateTrackingAsync(repoContext: repoContext, updateContext: updateContext, value: lastKnownGoodBuild, cancellationToken: cancellationToken); } - IReadOnlyList updatesMade = - await this.UpdatePackagesAsync(updateContext: updateContext, repoContext: repoContext, package: package, cancellationToken: cancellationToken); + IReadOnlyList updatesMade = await this.UpdatePackagesAsync(updateContext: updateContext, repoContext: repoContext, package: package, cancellationToken: cancellationToken); if (updatesMade.Count == 0) { @@ -385,9 +370,6 @@ private static string BuildInvalidUpdateBranch(string branchPrefix) return headRev; } - this._logger.LogResettingToDefault(repoContext); - await repoContext.Repository.ResetToMasterAsync(upstream: GitConstants.Upstream, cancellationToken: cancellationToken); - return null; } @@ -400,42 +382,75 @@ private static NuGetVersion GetUpdateVersion(IReadOnlyList updat private async ValueTask CommitToRepositoryAsync(RepoContext repoContext, PackageUpdate package, string version, bool builtOk, CancellationToken cancellationToken) { - string branchPrefix = GetBranchPrefixForPackage(package); + try + { + string branchPrefix = GetBranchPrefixForPackage(package); - if (builtOk) + if (builtOk) + { + string invalidUpdateBranch = BuildInvalidUpdateBranch(branchPrefix); + + await this.CommitDefaultBranchToRepositoryAsync(repoContext: repoContext, + package: package, + version: version, + cancellationToken: cancellationToken, + invalidUpdateBranch: invalidUpdateBranch, + branchPrefix: branchPrefix); + } + else + { + string branchForUpdate = BuildBranchForVersion(branchPrefix: branchPrefix, version: version); + + await this.CommitToNamedBranchAsync(repoContext: repoContext, + package: package, + version: version, + cancellationToken: cancellationToken, + branchForUpdate: branchForUpdate, + branchPrefix: branchPrefix); + } + } + finally { - string invalidUpdateBranch = BuildInvalidUpdateBranch(branchPrefix); - - this._logger.LogCommittingToDefault(repoContext: repoContext, packageId: package.PackageId, version: version); - await CommitChangeWithChangelogAsync(repoContext: repoContext, package: package, version: version, cancellationToken: cancellationToken); - await repoContext.Repository.PushAsync(cancellationToken: cancellationToken); - await repoContext.Repository.RemoveBranchesForPrefixAsync(branchForUpdate: invalidUpdateBranch, - branchPrefix: branchPrefix, - upstream: GitConstants.Upstream, - cancellationToken: cancellationToken); + this._logger.LogResettingToDefault(repoContext); + await repoContext.Repository.ResetToMasterAsync(upstream: GitConstants.Upstream, cancellationToken: cancellationToken); } - else + } + + private async ValueTask CommitToNamedBranchAsync(RepoContext repoContext, PackageUpdate package, string version, string branchForUpdate, string branchPrefix, CancellationToken cancellationToken) + { + if (repoContext.Repository.DoesBranchExist(branchName: branchForUpdate)) { - string branchForUpdate = BuildBranchForVersion(branchPrefix: branchPrefix, version: version); + // nothing to do - may already be a PR that's being worked on + this._logger.LogSkippingPackageCommit(repoContext: repoContext, branch: branchForUpdate, packageId: package.PackageId, version: version); - if (repoContext.Repository.DoesBranchExist(branchName: branchForUpdate)) - { - // nothing to do - may already be a PR that's being worked on - this._logger.LogSkippingPackageCommit(repoContext: repoContext, branch: branchForUpdate, packageId: package.PackageId, version: version); + return; + } - return; - } + this._logger.LogCommittingToNamedBranch(repoContext: repoContext, branch: branchForUpdate, packageId: package.PackageId, version: version); + await repoContext.Repository.CreateBranchAsync(branchName: branchForUpdate, cancellationToken: cancellationToken); - this._logger.LogCommittingToNamedBranch(repoContext: repoContext, branch: branchForUpdate, packageId: package.PackageId, version: version); - await repoContext.Repository.CreateBranchAsync(branchName: branchForUpdate, cancellationToken: cancellationToken); + await CommitChangeWithChangelogAsync(repoContext: repoContext, package: package, version: version, cancellationToken: cancellationToken); + await repoContext.Repository.PushOriginAsync(branchName: branchForUpdate, upstream: GitConstants.Upstream, cancellationToken: cancellationToken); + await repoContext.Repository.RemoveBranchesForPrefixAsync(branchForUpdate: branchForUpdate, branchPrefix: branchPrefix, upstream: GitConstants.Upstream, cancellationToken: cancellationToken); + } - await CommitChangeWithChangelogAsync(repoContext: repoContext, package: package, version: version, cancellationToken: cancellationToken); - await repoContext.Repository.PushOriginAsync(branchName: branchForUpdate, upstream: GitConstants.Upstream, cancellationToken: cancellationToken); - await repoContext.Repository.RemoveBranchesForPrefixAsync(branchForUpdate: branchForUpdate, - branchPrefix: branchPrefix, - upstream: GitConstants.Upstream, - cancellationToken: cancellationToken); - } + private async ValueTask CommitDefaultBranchToRepositoryAsync(RepoContext repoContext, + PackageUpdate package, + string version, + string invalidUpdateBranch, + string branchPrefix, + CancellationToken cancellationToken) + { + this._logger.LogCommittingToDefault(repoContext: repoContext, packageId: package.PackageId, version: version); + await CommitChangeWithChangelogAsync(repoContext: repoContext, package: package, version: version, cancellationToken: cancellationToken); + await repoContext.Repository.PushAsync(cancellationToken: cancellationToken); + await repoContext.Repository.RemoveBranchesForPrefixAsync(branchForUpdate: invalidUpdateBranch, + branchPrefix: branchPrefix, + upstream: GitConstants.Upstream, + cancellationToken: cancellationToken); + + this._logger.LogResettingToDefault(repoContext); + await repoContext.Repository.ResetToMasterAsync(upstream: GitConstants.Upstream, cancellationToken: cancellationToken); } private static string BuildBranchForVersion(string branchPrefix, string version) @@ -495,10 +510,7 @@ private ValueTask> UpdatePackagesAsync(in PackageU this._logger.LogUpdatingPackageId(package.PackageId); PackageUpdateConfiguration config = this.BuildConfiguration(package); - return this._packageUpdater.UpdateAsync(basePath: repoContext.WorkingDirectory, - configuration: config, - packageSources: updateContext.AdditionalSources, - cancellationToken: cancellationToken); + return this._packageUpdater.UpdateAsync(basePath: repoContext.WorkingDirectory, configuration: config, packageSources: updateContext.AdditionalSources, cancellationToken: cancellationToken); } private PackageUpdateConfiguration BuildConfiguration(PackageUpdate package)