Skip to content

Commit 8a1631b

Browse files
log messages as they're sent (#12117)
Co-authored-by: Abhishek <[email protected]>
1 parent 3e616e8 commit 8a1631b

File tree

7 files changed

+308
-2
lines changed

7 files changed

+308
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
using NuGetUpdater.Core.Run.ApiModel;
2+
3+
using Xunit;
4+
5+
namespace NuGetUpdater.Core.Test.Run;
6+
7+
public class MessageReportTests
8+
{
9+
[Fact]
10+
public void AllMessagesAreTested()
11+
{
12+
var untestedTypes = typeof(MessageBase).Assembly.GetTypes()
13+
.Where(t => t.IsSubclassOf(typeof(MessageBase)))
14+
.Where(t => t != typeof(JobErrorBase)) // this is an abstract class and can't be directly tested
15+
.ToHashSet();
16+
foreach (var data in MessageBaseTestData())
17+
{
18+
var testedMessageType = data[0]!.GetType();
19+
untestedTypes.Remove(testedMessageType);
20+
}
21+
22+
Assert.Empty(untestedTypes.Select(t => t.Name));
23+
}
24+
25+
[Theory]
26+
[MemberData(nameof(MessageBaseTestData))]
27+
public void MessageBase(MessageBase message, string expected)
28+
{
29+
var actual = message.GetReport().Replace("\r", "");
30+
Assert.Equal(expected.Replace("\r", ""), actual);
31+
}
32+
33+
public static IEnumerable<object[]> MessageBaseTestData()
34+
{
35+
yield return
36+
[
37+
// message
38+
new BadRequirement("unparseable"),
39+
// expected
40+
"""
41+
Error type: illformed_requirement
42+
- message: unparseable
43+
"""
44+
];
45+
46+
yield return
47+
[
48+
// message
49+
new ClosePullRequest()
50+
{
51+
DependencyNames = ["Dependency1", "Dependency2"]
52+
},
53+
// expected
54+
"""
55+
ClosePullRequest: up_to_date
56+
- Dependency1
57+
- Dependency2
58+
"""
59+
];
60+
61+
yield return
62+
[
63+
// message
64+
new CreatePullRequest()
65+
{
66+
Dependencies = [
67+
new()
68+
{
69+
Name = "Dependency1",
70+
Version = "1.2.3",
71+
Requirements = [], // unused
72+
},
73+
new()
74+
{
75+
Name = "Dependency2",
76+
Version = "4.5.6",
77+
Requirements = [], // unused
78+
}
79+
],
80+
UpdatedDependencyFiles = [], // unused
81+
BaseCommitSha = "unused",
82+
CommitMessage = "unused",
83+
PrTitle = "unused",
84+
PrBody = "unused",
85+
},
86+
// expected
87+
"""
88+
CreatePullRequest
89+
- Dependency1/1.2.3
90+
- Dependency2/4.5.6
91+
"""
92+
];
93+
94+
yield return
95+
[
96+
// message
97+
new DependencyFileNotFound("path/to/file.txt", "custom message"),
98+
// expected
99+
"""
100+
Error type: dependency_file_not_found
101+
- message: custom message
102+
- file-path: path/to/file.txt
103+
"""
104+
];
105+
106+
yield return
107+
[
108+
// message
109+
new DependencyFileNotParseable("path/to/file.txt", "custom message"),
110+
// expected
111+
"""
112+
Error type: dependency_file_not_parseable
113+
- message: custom message
114+
- file-path: path/to/file.txt
115+
"""
116+
];
117+
118+
yield return
119+
[
120+
// message
121+
new DependencyNotFound("Some.Dependency"),
122+
// expected
123+
"""
124+
Error type: dependency_not_found
125+
- source: Some.Dependency
126+
"""
127+
];
128+
129+
yield return
130+
[
131+
// message
132+
new JobRepoNotFound("custom message"),
133+
// expected
134+
"""
135+
Error type: job_repo_not_found
136+
- message: custom message
137+
"""
138+
];
139+
140+
yield return
141+
[
142+
// message
143+
new PrivateSourceAuthenticationFailure(["url1", "url2"]),
144+
// expected
145+
"""
146+
Error type: private_source_authentication_failure
147+
- source: (url1|url2)
148+
"""
149+
];
150+
151+
yield return
152+
[
153+
// message
154+
new PrivateSourceBadResponse(["url1", "url2"]),
155+
// expected
156+
"""
157+
Error type: private_source_bad_response
158+
- source: (url1|url2)
159+
"""
160+
];
161+
162+
yield return
163+
[
164+
// message
165+
new PullRequestExistsForLatestVersion("Some.Dependency", "1.2.3"),
166+
// expected
167+
"""
168+
Error type: pull_request_exists_for_latest_version
169+
- dependency-name: Some.Dependency
170+
- dependency-version: 1.2.3
171+
"""
172+
];
173+
174+
yield return
175+
[
176+
// message
177+
new SecurityUpdateNotNeeded("Some.Dependency"),
178+
// expected
179+
"""
180+
Error type: security_update_not_needed
181+
- dependency-name: Some.Dependency
182+
"""
183+
];
184+
185+
yield return
186+
[
187+
// message
188+
new UnknownError(new NotImplementedException("error message"), "TEST-JOB-ID"),
189+
// expected
190+
"""
191+
Error type: unknown_error
192+
- error-class: NotImplementedException
193+
- error-message: error message
194+
- error-backtrace: <unknown>
195+
- package-manager: nuget
196+
- job-id: TEST-JOB-ID
197+
"""
198+
];
199+
200+
yield return
201+
[
202+
// message
203+
new UpdateNotPossible(["Dependency1", "Dependency2"]),
204+
// expected
205+
"""
206+
Error type: update_not_possible
207+
- dependencies: Dependency1, Dependency2
208+
"""
209+
];
210+
211+
yield return
212+
[
213+
// message
214+
new UpdatePullRequest()
215+
{
216+
DependencyNames = ["Dependency1", "Dependency2"],
217+
UpdatedDependencyFiles = [], // unused
218+
BaseCommitSha = "unused",
219+
CommitMessage = "unused",
220+
PrTitle = "unused",
221+
PrBody = "unused",
222+
DependencyGroup = "unused",
223+
},
224+
// expected
225+
"""
226+
UpdatePullRequest
227+
- Dependency1
228+
- Dependency2
229+
"""
230+
];
231+
}
232+
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/ApiModel/ClosePullRequest.cs

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Immutable;
2+
using System.Text;
23
using System.Text.Json.Serialization;
34

45
namespace NuGetUpdater.Core.Run.ApiModel;
@@ -9,4 +10,16 @@ public sealed record ClosePullRequest : MessageBase
910
public required ImmutableArray<string> DependencyNames { get; init; }
1011

1112
public string Reason { get; init; } = "up_to_date";
13+
14+
public override string GetReport()
15+
{
16+
var report = new StringBuilder();
17+
report.AppendLine($"{nameof(ClosePullRequest)}: {Reason}");
18+
foreach (var dependencyName in DependencyNames)
19+
{
20+
report.AppendLine($" - {dependencyName}");
21+
}
22+
23+
return report.ToString().Trim();
24+
}
1225
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/ApiModel/CreatePullRequest.cs

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System.Text;
12
using System.Text.Json.Serialization;
23

4+
using NuGet.Versioning;
5+
36
namespace NuGetUpdater.Core.Run.ApiModel;
47

58
public sealed record CreatePullRequest : MessageBase
@@ -15,4 +18,21 @@ public sealed record CreatePullRequest : MessageBase
1518
public required string PrTitle { get; init; }
1619
[JsonPropertyName("pr-body")]
1720
public required string PrBody { get; init; }
21+
22+
public override string GetReport()
23+
{
24+
var dependencyNames = Dependencies
25+
.OrderBy(d => d.Name, StringComparer.OrdinalIgnoreCase)
26+
.ThenBy(d => NuGetVersion.Parse(d.Version!))
27+
.Select(d => $"{d.Name}/{d.Version}")
28+
.ToArray();
29+
var report = new StringBuilder();
30+
report.AppendLine(nameof(CreatePullRequest));
31+
foreach (var d in dependencyNames)
32+
{
33+
report.AppendLine($" - {d}");
34+
}
35+
36+
return report.ToString().Trim();
37+
}
1838
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/ApiModel/JobErrorBase.cs

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net;
2+
using System.Text;
23
using System.Text.Json.Serialization;
34

45
using Microsoft.Build.Exceptions;
@@ -20,6 +21,24 @@ public JobErrorBase(string type)
2021
[JsonPropertyName("error-details")]
2122
public Dictionary<string, object> Details { get; init; } = new();
2223

24+
public override string GetReport()
25+
{
26+
var report = new StringBuilder();
27+
report.AppendLine($"Error type: {Type}");
28+
foreach (var (key, value) in Details)
29+
{
30+
var valueString = value.ToString();
31+
if (value is IEnumerable<string> strings)
32+
{
33+
valueString = string.Join(", ", strings);
34+
}
35+
36+
report.AppendLine($" - {key}: {valueString}");
37+
}
38+
39+
return report.ToString().Trim();
40+
}
41+
2342
public static JobErrorBase ErrorFromException(Exception ex, string jobId, string currentDirectory)
2443
{
2544
return ex switch

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/ApiModel/MessageBase.cs

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ namespace NuGetUpdater.Core.Run.ApiModel;
22

33
public abstract record MessageBase
44
{
5+
public abstract string GetReport();
56
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/ApiModel/UpdatePullRequest.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Immutable;
2+
using System.Text;
23
using System.Text.Json.Serialization;
34

45
namespace NuGetUpdater.Core.Run.ApiModel;
@@ -25,4 +26,19 @@ public sealed record UpdatePullRequest : MessageBase
2526

2627
[JsonPropertyName("dependency-group")]
2728
public required string? DependencyGroup { get; init; }
29+
30+
public override string GetReport()
31+
{
32+
var dependencyNames = DependencyNames
33+
.Order(StringComparer.OrdinalIgnoreCase)
34+
.ToArray();
35+
var report = new StringBuilder();
36+
report.AppendLine(nameof(UpdatePullRequest));
37+
foreach (var d in dependencyNames)
38+
{
39+
report.AppendLine($" - {d}");
40+
}
41+
42+
return report.ToString().Trim();
43+
}
2844
}

nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Run/RunWorker.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,15 @@ async Task AddUpdatedFileIfDifferentAsync(string directory, string fileName)
365365

366366
private async Task SendApiMessage(MessageBase? message)
367367
{
368+
if (message is null)
369+
{
370+
return;
371+
}
372+
373+
var report = message.GetReport();
374+
_logger.Info(report);
368375
switch (message)
369376
{
370-
case null:
371-
break;
372377
case JobErrorBase error:
373378
await _apiHandler.RecordUpdateJobError(error);
374379
break;

0 commit comments

Comments
 (0)