Skip to content

Commit

Permalink
ProjectHasChanged Check (#347)
Browse files Browse the repository at this point in the history
* ProjectHasChanged Check

* Formatting Markdown

* Invert condition

* Remove delay

* Tweaks

* Formatting Markdown

* Pack all projects if on main

* Revert

* Debug

* Better throwing

* Remove stacktrace hidden

* ReleaseNotes.md

* Formatting Markdown

---------

Co-authored-by: Tom Longhurst <[email protected]>
  • Loading branch information
thomhurst and thomhurst authored Dec 26, 2023
1 parent 5011d4b commit 6cb6216
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dotnet-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
GitHub__PullRequest__Number: ${{ github.event.number }}
GitHub__PullRequest__Branch: ${{ github.event.pull_request.head.ref }}
GitHub__PullRequest__Author: ${{ github.event.pull_request.user.login }}
GitHub__StandardToken: ${{ secrets.DOTNET_FORMAT_PUSH_TOKEN }}
Publish__ShouldPublish: false
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
- name: Upload Code Coverage Results
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/dotnet-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
GitHub__PullRequest__Number: ${{ github.event.number }}
GitHub__PullRequest__Branch: ${{ github.event.pull_request.head.ref }}
GitHub__PullRequest__Author: ${{ github.event.pull_request.user.login }}
GitHub__StandardToken: ${{ secrets.DOTNET_FORMAT_PUSH_TOKEN }}
Publish__ShouldPublish: false
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
- name: Upload Code Coverage Results
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Options;
using ModularPipelines.Attributes;
using ModularPipelines.Build.Attributes;
using ModularPipelines.Build.Settings;
using ModularPipelines.Context;
Expand All @@ -13,6 +14,7 @@ namespace ModularPipelines.Build.Modules;

[SkipIfDependabot]
[SkipOnMainBranch]
[RunOnLinuxOnly]
public class CodeFormattedNicelyModule : Module<CommandResult>
{
public override ModuleRunType ModuleRunType => ModuleRunType.AlwaysRun;
Expand Down
1 change: 1 addition & 0 deletions src/ModularPipelines.Build/Modules/FormatMarkdownModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace ModularPipelines.Build.Modules;

[SkipIfDependabot]
[RunOnLinuxOnly]
[DependsOn<GenerateReadMeModule>]
public class FormatMarkdownModule : Module<CommandResult>
{
Expand Down
34 changes: 34 additions & 0 deletions src/ModularPipelines.Build/Modules/PackProjectsModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using EnumerableAsyncProcessor.Extensions;
using Microsoft.Extensions.Logging;
using ModularPipelines.Attributes;
using ModularPipelines.Context;
using ModularPipelines.DotNet.Extensions;
using ModularPipelines.DotNet.Options;
using ModularPipelines.Git.Extensions;
using ModularPipelines.Models;
using ModularPipelines.Modules;
using File = ModularPipelines.FileSystem.File;
Expand All @@ -13,6 +15,8 @@ namespace ModularPipelines.Build.Modules;
[DependsOn<PackageFilesRemovalModule>]
[DependsOn<CodeFormattedNicelyModule>]
[DependsOn<FindProjectDependenciesModule>]

[DependsOn<GetChangedFilesInPullRequest>]
[RunOnLinuxOnly]
public class PackProjectsModule : Module<CommandResult[]>
{
Expand All @@ -23,19 +27,49 @@ public class PackProjectsModule : Module<CommandResult[]>

var projectFiles = await GetModule<FindProjectDependenciesModule>();

var changesFiles = await GetModule<GetChangedFilesInPullRequest>();

var dependencies = await projectFiles.Value!.Dependencies
.ToAsyncProcessorBuilder()
.SelectAsync(async projectFile => await Pack(context, cancellationToken, projectFile, packageVersion))
.ProcessOneAtATime();

var gitVersioningInformation = await context.Git().Versioning.GetGitVersioningInformation();

var others = await projectFiles.Value!.Others
.Where(x =>
{
if (gitVersioningInformation.BranchName == "main")
{
return true;
}

return ProjectHasChanged(x,
changesFiles.Value?.Select(x => new File(x.FileName)).ToList() ?? new List<File>(), context);
})
.ToAsyncProcessorBuilder()
.SelectAsync(async projectFile => await Pack(context, cancellationToken, projectFile, packageVersion))
.ProcessInParallel();

return dependencies.Concat(others).ToArray();
}

private bool ProjectHasChanged(File projectFile, IEnumerable<File> changedFiles,
IPipelineContext context)
{
var projectDirectory = projectFile.Folder!;

if (!changedFiles.Any(x => x.Path.Contains(projectDirectory.Path)))
{
context.Logger.LogInformation("{Project} has not changed so not packing it", projectFile.Name);
return false;
}

context.Logger.LogInformation("{Project} has changed so packing it", projectFile.Name);

return true;
}

private static async Task<CommandResult> Pack(IPipelineContext context, CancellationToken cancellationToken, File projectFile, ModuleResult<string> packageVersion)
{
return await context.DotNet().Pack(new DotNetPackOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace ModularPipelines.Build.Modules;
[RunOnLinuxOnly]
[SkipIfDependabot]
[RunOnlyOnBranch("main")]
[RunOnLinuxOnly]
public class UploadPackagesToNugetModule : Module<CommandResult[]>
{
private readonly IOptions<NuGetSettings> _nugetSettings;
Expand Down
2 changes: 2 additions & 0 deletions src/ModularPipelines.Build/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ModularPipelines.Build;
using ModularPipelines.Build.Modules;
Expand Down Expand Up @@ -66,4 +67,5 @@ await PipelineHostBuilder.Create()
}
})
.ConfigurePipelineOptions((context, options) => options.DefaultRetryCount = 3)
.SetLogLevel(LogLevel.Debug)
.ExecutePipelineAsync();
3 changes: 2 additions & 1 deletion src/ModularPipelines.Build/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Rework execution engine to not use nested delegates
- Fix a bug where skipped modules could still start
* Fix a bug where skipped modules could still start
*
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private async Task SaveFailedResult(Exception exception)
await Module.HistoryHandler.SaveResult(moduleResult);
}

[StackTraceHidden]
private void CancelPipelineAndThrow(Exception exception)
{
Context.Logger.LogDebug("Module failed. Cancelling the pipeline");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public async Task OnBeforeExecute(IPipelineContext context)
catch (Exception exception)
{
Logger.LogError(exception, "Error in OnBeforeExecute");
throw;
}
}

Expand All @@ -31,6 +32,7 @@ public async Task OnAfterExecute(IPipelineContext context)
catch (Exception exception)
{
Logger.LogError(exception, "Error in OnAfterExecute");
throw;
}
}
}
15 changes: 12 additions & 3 deletions src/ModularPipelines/Engine/Executors/PipelineExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public async Task<PipelineSummary> ExecuteAsync(List<ModuleBase> runnableModules
var start = DateTimeOffset.UtcNow;
var stopWatch = Stopwatch.StartNew();

Exception? exception;
PipelineSummary pipelineSummary;
try
{
Expand All @@ -45,7 +46,7 @@ public async Task<PipelineSummary> ExecuteAsync(List<ModuleBase> runnableModules
}
finally
{
await WaitForAlwaysRunModules(runnableModules);
exception = await WaitForAlwaysRunModules(runnableModules);

var end = DateTimeOffset.UtcNow;

Expand All @@ -54,18 +55,26 @@ public async Task<PipelineSummary> ExecuteAsync(List<ModuleBase> runnableModules
await _pipelineSetupExecutor.OnEndAsync(pipelineSummary);
}

if (exception != null)
{
throw exception;
}

return pipelineSummary;
}

private async Task WaitForAlwaysRunModules(IEnumerable<ModuleBase> runnableModules)
private async Task<Exception?> WaitForAlwaysRunModules(IEnumerable<ModuleBase> runnableModules)
{
try
{
await Task.WhenAll(runnableModules.Where(m => m.ModuleRunType == ModuleRunType.AlwaysRun).Select(m => m.ExecutionTask));
}
catch (Exception e)
catch (Exception? e)
{
_logger.LogWarning(e, "Error while waiting for Always Run modules");
return e;
}

return null;
}
}

0 comments on commit 6cb6216

Please sign in to comment.