Skip to content

Commit

Permalink
Always reset to head whether or not build & push succeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
credfeto committed Mar 8, 2024
1 parent a17176a commit 4c3b485
Showing 1 changed file with 69 additions and 57 deletions.
126 changes: 69 additions & 57 deletions src/Credfeto.DotNet.Repo.Tools.Packages/Services/BulkPackageUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public async ValueTask BulkUpdateAsync(string templateRepository,

IReadOnlyList<PackageUpdate> 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,
Expand All @@ -106,10 +105,7 @@ await this._gitRepositoryFactory.OpenOrCloneAsync(workDir: workFolder, repoUrl:
}
}

public async ValueTask UpdateRepositoriesAsync(PackageUpdateContext updateContext,
IReadOnlyList<string> repositories,
IReadOnlyList<PackageUpdate> packages,
CancellationToken cancellationToken)
public async ValueTask UpdateRepositoriesAsync(PackageUpdateContext updateContext, IReadOnlyList<string> repositories, IReadOnlyList<PackageUpdate> packages, CancellationToken cancellationToken)
{
try
{
Expand Down Expand Up @@ -148,10 +144,7 @@ public async ValueTask UpdateRepositoriesAsync(PackageUpdateContext updateContex
}
}

private async ValueTask UpdateCachedPackagesAsync(string workFolder,
IReadOnlyList<PackageUpdate> packages,
PackageUpdateContext updateContext,
CancellationToken cancellationToken)
private async ValueTask UpdateCachedPackagesAsync(string workFolder, IReadOnlyList<PackageUpdate> packages, PackageUpdateContext updateContext, CancellationToken cancellationToken)
{
IReadOnlyList<PackageVersion> allPackages = this._packageCache.GetAll();

Expand Down Expand Up @@ -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))
{
Expand All @@ -243,20 +235,14 @@ await this._trackingCache.UpdateTrackingAsync(new(Repository: repository, Change
}
}

private async ValueTask ProcessRepoUpdatesAsync(PackageUpdateContext updateContext,
RepoContext repoContext,
IReadOnlyList<PackageUpdate> packages,
CancellationToken cancellationToken)
private async ValueTask ProcessRepoUpdatesAsync(PackageUpdateContext updateContext, RepoContext repoContext, IReadOnlyList<PackageUpdate> packages, CancellationToken cancellationToken)
{
string? lastKnownGoodBuild = this._trackingCache.Get(repoContext.ClonePath);

if (!repoContext.HasDotNetFiles(out string? sourceDirectory, out IReadOnlyList<string>? solutions, out IReadOnlyList<string>? 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;
}
Expand Down Expand Up @@ -317,8 +303,7 @@ await this._releaseGeneration.TryCreateNextPatchAsync(repoContext: repoContext,
await this._trackingCache.UpdateTrackingAsync(repoContext: repoContext, updateContext: updateContext, value: lastKnownGoodBuild, cancellationToken: cancellationToken);
}

IReadOnlyList<PackageVersion> updatesMade =
await this.UpdatePackagesAsync(updateContext: updateContext, repoContext: repoContext, package: package, cancellationToken: cancellationToken);
IReadOnlyList<PackageVersion> updatesMade = await this.UpdatePackagesAsync(updateContext: updateContext, repoContext: repoContext, package: package, cancellationToken: cancellationToken);

if (updatesMade.Count == 0)
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -400,42 +382,75 @@ private static NuGetVersion GetUpdateVersion(IReadOnlyList<PackageVersion> 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)
Expand Down Expand Up @@ -495,10 +510,7 @@ private ValueTask<IReadOnlyList<PackageVersion>> 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)
Expand Down

0 comments on commit 4c3b485

Please sign in to comment.