Skip to content

Commit

Permalink
Set pull request state for issue providers for which no issues are re…
Browse files Browse the repository at this point in the history
…ported (#288)
  • Loading branch information
pascalberger authored Aug 31, 2022
1 parent d88c04d commit 3bcb4f5
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,29 @@ public interface IIssuesState
IEnumerable<IIssue> Issues { get; }

/// <summary>
/// Adds an issue to the data class.
/// Gets the list of issue providers and runs for which issues are read.
/// </summary>
IList<(IIssueProvider, string)> IssueProvidersAndRuns { get; }

/// <summary>
/// Adds an issue to <see cref="Issues"/>.
/// To read issues from an issue provider use <see cref="AddIssues(IIssueProvider, IReadIssuesSettings)"/>.
/// </summary>
/// <param name="issue">Issue which should be added.</param>
void AddIssue(IIssue issue);

/// <summary>
/// Adds a list of issues to the data class.
/// Adds a list of issues to <see cref="Issues"/>.
/// To read issues from an issue provider use <see cref="AddIssues(IIssueProvider, IReadIssuesSettings)"/>.
/// </summary>
/// <param name="issues">Issues which should be added.</param>
void AddIssues(IEnumerable<IIssue> issues);

/// <summary>
/// Reads issues from an issue provider and adds the issues to <see cref="Issues"/>.
/// </summary>
/// <param name="issueProvider">Issue provider used to read the issues.</param>
/// <param name="settings">Settings for reading the issues. <c>Null</c> for default values.</param>
void AddIssues(IIssueProvider issueProvider, IReadIssuesSettings settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Issues;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Cake.Frosting.Issues.Recipe
{
Expand All @@ -14,8 +15,12 @@ namespace Cake.Frosting.Issues.Recipe
/// </summary>
public class IssuesState : IIssuesState
{
private readonly IIssuesContext context;

private readonly List<IIssue> issues = new List<IIssue>();

private readonly List<(IIssueProvider, string)> issueProvidersAndRuns = new List<(IIssueProvider, string)>();

/// <inheritdoc />
public DirectoryPath RepositoryRootDirectory { get; }

Expand Down Expand Up @@ -49,6 +54,9 @@ public class IssuesState : IIssuesState
/// <inheritdoc />
public IEnumerable<IIssue> Issues => this.issues.AsReadOnly();

/// <inheritdoc />
public IList<(IIssueProvider, string)> IssueProvidersAndRuns => this.issueProvidersAndRuns.AsReadOnly();

/// <summary>
/// Creates a new instance of the <see cref="IssuesState"/> class.
/// </summary>
Expand All @@ -63,6 +71,8 @@ public IssuesState(
throw new ArgumentNullException(nameof(context));
}

this.context = context;

this.BuildRootDirectory = context.MakeAbsolute(context.Directory("./"));
context.Information("Build script root directory: {0}", this.BuildRootDirectory);

Expand Down Expand Up @@ -114,6 +124,28 @@ public void AddIssues(IEnumerable<IIssue> issues)
this.issues.AddRange(issues);
}

/// <inheritdoc />
public void AddIssues(IIssueProvider issueProvider, IReadIssuesSettings settings)
{
issueProvider.NotNull(nameof(issueProvider));

this.issueProvidersAndRuns.Add((issueProvider, settings?.Run));

// Define default settings.
var defaultSettings = new ReadIssuesSettings(this.ProjectRootDirectory);

if (this.PullRequestSystem != null)
{
defaultSettings.FileLinkSettings =
this.PullRequestSystem.GetFileLinkSettings(context);
}

AddIssues(
context.ReadIssues(
issueProvider,
GetSettings(settings, defaultSettings)));
}

/// <summary>
/// Determines the repository info provider to use.
/// </summary>
Expand Down Expand Up @@ -212,5 +244,20 @@ private static IIssuesPullRequestSystem DeterminePullRequestSystem(IIssuesContex

return null;
}

private static IReadIssuesSettings GetSettings(IReadIssuesSettings configuredSettings, IReadIssuesSettings defaultSettings)
{
if (configuredSettings == null)
{
return defaultSettings;
}

if (configuredSettings.FileLinkSettings == null)
{
configuredSettings.FileLinkSettings = defaultSettings.FileLinkSettings;
}

return configuredSettings;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,43 @@ public override void SetPullRequestIssuesState(IIssuesContext context)
// Set status for individual issue providers
if (context.Parameters.PullRequestSystem.ShouldSetSeparatePullRequestStatusForEachIssueProviderAndRun)
{
foreach (var providerGroup in context.State.Issues.GroupBy(x => x.ProviderType))
// Determine issue providers and runs from the list of issues providers from which issues were read
// and issue providers and runs of the reported issues.
var issueProvidersAndRuns =
context.State.IssueProvidersAndRuns
.Select(x =>
new
{
x.Item1.ProviderName,
Run = x.Item2
})
.Union(
context.State.Issues
.GroupBy(x =>
new
{
x.ProviderName,
x.Run
})
.Select(x =>
new
{
x.Key.ProviderName,
x.Key.Run
}));

foreach (var item in issueProvidersAndRuns)
{
var issueProvider = providerGroup.Key;
foreach (var runGroup in providerGroup.GroupBy(x => x.Run))
var issueProvider = item.ProviderName;
if (!string.IsNullOrEmpty(item.Run))
{
if (!string.IsNullOrEmpty(runGroup.Key))
{
issueProvider += $"-{runGroup.Key}";
}

SetPullRequestStatus(
context,
runGroup,
issueProvider);
issueProvider += $" ({item.Run})";
}

SetPullRequestStatus(
context,
context.State.Issues.Where(x => x.ProviderName == item.ProviderName && x.Run == item.Run),
issueProvider);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,117 +20,83 @@ public override void Run(IIssuesContext context)
{
context.NotNull(nameof(context));

// Define default settings.
var defaultSettings = new ReadIssuesSettings(context.State.ProjectRootDirectory);

if (context.State.PullRequestSystem != null)
{
defaultSettings.FileLinkSettings =
context.State.PullRequestSystem.GetFileLinkSettings(context);
}

// Read MSBuild log files created by XmlFileLogger.
foreach (var logFile in context.Parameters.InputFiles.MsBuildXmlFileLoggerLogFilePaths)
{
context.NotNull(nameof(context));

context.State.AddIssues(
context.ReadIssues(
context.MsBuildIssuesFromFilePath(
logFile.Key,
context.MsBuildXmlFileLoggerFormat()),
GetSettings(logFile.Value, defaultSettings)));
context.MsBuildIssuesFromFilePath(
logFile.Key,
context.MsBuildXmlFileLoggerFormat()),
logFile.Value);
}

// Read MSBuild binary log files.
foreach (var logFile in context.Parameters.InputFiles.MsBuildBinaryLogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.MsBuildIssuesFromFilePath(
logFile.Key,
context.MsBuildBinaryLogFileFormat()),
GetSettings(logFile.Value, defaultSettings)));
context.MsBuildIssuesFromFilePath(
logFile.Key,
context.MsBuildBinaryLogFileFormat()),
logFile.Value);
}

// Read InspectCode log files.
foreach (var logFile in context.Parameters.InputFiles.InspectCodeLogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.InspectCodeIssuesFromFilePath(logFile.Key),
GetSettings(logFile.Value, defaultSettings)));
context.InspectCodeIssuesFromFilePath(logFile.Key),
logFile.Value);
}

// Read dupFinder log files.
foreach (var logFile in context.Parameters.InputFiles.DupFinderLogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.DupFinderIssuesFromFilePath(logFile.Key),
GetSettings(logFile.Value, defaultSettings)));
context.DupFinderIssuesFromFilePath(logFile.Key),
logFile.Value);
}

// Read markdownlint-cli log files.
foreach (var logFile in context.Parameters.InputFiles.MarkdownlintCliLogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.MarkdownlintIssuesFromFilePath(
logFile.Key,
context.MarkdownlintCliLogFileFormat()),
GetSettings(logFile.Value, defaultSettings)));
context.MarkdownlintIssuesFromFilePath(
logFile.Key,
context.MarkdownlintCliLogFileFormat()),
logFile.Value);
}

// Read markdownlint-cli log files created with --json.
foreach (var logFile in context.Parameters.InputFiles.MarkdownlintCliJsonLogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.MarkdownlintIssuesFromFilePath(
logFile.Key,
context.MarkdownlintCliJsonLogFileFormat()),
GetSettings(logFile.Value, defaultSettings)));
context.MarkdownlintIssuesFromFilePath(
logFile.Key,
context.MarkdownlintCliJsonLogFileFormat()),
logFile.Value);
}

// Read markdownlint log files in version 1.
foreach (var logFile in context.Parameters.InputFiles.MarkdownlintV1LogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.MarkdownlintIssuesFromFilePath(
logFile.Key,
context.MarkdownlintV1LogFileFormat()),
GetSettings(logFile.Value, defaultSettings)));
context.MarkdownlintIssuesFromFilePath(
logFile.Key,
context.MarkdownlintV1LogFileFormat()),
logFile.Value);
}

// Read ESLint log files in JSON format.
foreach (var logFile in context.Parameters.InputFiles.EsLintJsonLogFilePaths)
{
context.State.AddIssues(
context.ReadIssues(
context.EsLintIssuesFromFilePath(
logFile.Key,
context.EsLintJsonFormat()),
GetSettings(logFile.Value, defaultSettings)));
context.EsLintIssuesFromFilePath(
logFile.Key,
context.EsLintJsonFormat()),
logFile.Value);
}

context.Information("{0} issues are found.", context.State.Issues.Count());
}

private static IReadIssuesSettings GetSettings(IReadIssuesSettings configuredSettings, IReadIssuesSettings defaultSettings)
{
if (configuredSettings == null)
{
return defaultSettings;
}

if (configuredSettings.FileLinkSettings == null)
{
configuredSettings.FileLinkSettings = defaultSettings.FileLinkSettings;
}

return configuredSettings;
}
}
}
Loading

0 comments on commit 3bcb4f5

Please sign in to comment.