Skip to content

Commit 22ad3e4

Browse files
Copilotlbussell
andauthored
Add failure label to AutoBuilder - Failed notifications for recent build failures
The failure label was only added when an exception was thrown, not when the category was 'Failed' due to recent build failures. This caused these actionable issues to be immediately closed by NotificationService. Change the condition from `exception is not null` to `category == "Failed"` so both failure paths (exception and recent failed builds) get the failure label and remain open. Agent-Logs-Url: https://github.com/dotnet/docker-tools/sessions/89cbb121-4c87-422e-83ec-09a65bd377c6 Co-authored-by: lbussell <36081148+lbussell@users.noreply.github.com>
1 parent 4a814d7 commit 22ad3e4

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/ImageBuilder.Tests/QueueBuildCommandTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,51 @@ public async Task QueueBuildCommand_RecentFailedBuilds_MaxFailed()
116116
context.Verify(notificationPostCallCount: 1, isQueuedBuildExpected: false);
117117
}
118118

119+
/// <summary>
120+
/// Verifies that the failure label is included in the notification when there are too many recent failed builds.
121+
/// </summary>
122+
[Fact]
123+
public async Task QueueBuildCommand_RecentFailedBuilds_IncludesFailureLabel()
124+
{
125+
const string path1 = "path1";
126+
127+
Subscription[] subscriptions = new Subscription[]
128+
{
129+
CreateSubscription("repo1")
130+
};
131+
132+
List<List<SubscriptionImagePaths>> allSubscriptionImagePaths = new()
133+
{
134+
new List<SubscriptionImagePaths>
135+
{
136+
new SubscriptionImagePaths
137+
{
138+
SubscriptionId = subscriptions[0].Id,
139+
ImagePaths = new string[]
140+
{
141+
path1
142+
}
143+
}
144+
}
145+
};
146+
147+
PagedList<WebApi.Build> allBuilds = new();
148+
149+
for (int i = 0; i < QueueBuildCommand.BuildFailureLimit; i++)
150+
{
151+
WebApi.Build failedBuild = CreateBuild($"https://failedbuild-{i}");
152+
failedBuild.Tags.Add(AzdoTags.AutoBuilder);
153+
failedBuild.Status = WebApi.BuildStatus.Completed;
154+
failedBuild.Result = WebApi.BuildResult.Failed;
155+
allBuilds.Add(failedBuild);
156+
}
157+
158+
using TestContext context = new(subscriptions, allSubscriptionImagePaths, new PagedList<WebApi.Build>(), allBuilds);
159+
await context.ExecuteCommandAsync();
160+
161+
context.VerifyNotificationLabelsContain(NotificationLabels.Failure);
162+
}
163+
119164
/// <summary>
120165
/// Verifies that a build is queued even if there are recent failed builds but not enough to meet the threshold.
121166
/// </summary>
@@ -588,6 +633,20 @@ public void Verify(int notificationPostCallCount, bool isQueuedBuildExpected, ID
588633
}
589634
}
590635

636+
public void VerifyNotificationLabelsContain(string expectedLabel)
637+
{
638+
_notificationServiceMock
639+
.Verify(o => o.PostAsync(
640+
It.IsAny<string>(),
641+
It.IsAny<string>(),
642+
It.Is<IEnumerable<string>>(labels => labels.Contains(expectedLabel)),
643+
It.IsAny<string>(),
644+
It.IsAny<string>(),
645+
It.IsAny<GitHubAuthOptions>(),
646+
It.IsAny<bool>(),
647+
It.IsAny<IEnumerable<string>>()));
648+
}
649+
591650
private string SerializeJsonObjectToTempFile(object jsonObject)
592651
{
593652
string path = Path.GetTempFileName();

src/ImageBuilder/Commands/QueueBuildCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ await _notificationService.PostAsync(
270270
{
271271
NotificationLabels.AutoBuilder,
272272
NotificationLabels.GetRepoLocationLabel(subscription.Manifest.Repo, subscription.Manifest.Branch)
273-
}.AppendIf(NotificationLabels.Failure, () => exception is not null),
273+
}.AppendIf(NotificationLabels.Failure, () => category == "Failed"),
274274
Options.GitOptions.Owner,
275275
Options.GitOptions.Repo,
276276
Options.GitOptions.GitHubAuthOptions,

0 commit comments

Comments
 (0)