Skip to content

Commit 9e9fa13

Browse files
authored
[CI Visibility] - Defer await for git upload task (#3825)
* [CI Visibility] - Defer await for git upload * Changes * changes from review.
1 parent 23d7d09 commit 9e9fa13

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

tracer/src/Datadog.Trace.Tools.Runner/RunCiCommand.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
8282
}
8383
}
8484

85+
var uploadRepositoryChangesTask = Task.CompletedTask;
86+
8587
// Set Agentless configuration from the command line options
8688
ciVisibilitySettings.SetAgentlessConfiguration(agentless, apiKey, applicationKey, ciVisibilitySettings.AgentlessUrl);
8789

@@ -103,7 +105,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
103105
var lazyItrClient = new Lazy<IntelligentTestRunnerClient>(() => new(CIEnvironmentValues.Instance.WorkspacePath, ciVisibilitySettings));
104106
if (ciVisibilitySettings.GitUploadEnabled != false || ciVisibilitySettings.IntelligentTestRunnerEnabled)
105107
{
106-
await lazyItrClient.Value.UploadRepositoryChangesAsync().ConfigureAwait(false);
108+
// If we are in git upload only then we can defer the await until the child command exits.
109+
uploadRepositoryChangesTask = Task.Run(() => lazyItrClient.Value.UploadRepositoryChangesAsync());
107110

108111
// Once the repository has been uploaded we switch off the git upload in children processes
109112
profilerEnvironmentVariables[Configuration.ConfigurationKeys.CIVisibility.GitUploadEnabled] = "0";
@@ -241,9 +244,16 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
241244
{
242245
AnsiConsole.WriteLine("Running: " + command);
243246

244-
if (Program.CallbackForTests != null)
247+
if (ciVisibilitySettings.IntelligentTestRunnerEnabled || Program.CallbackForTests is not null)
248+
{
249+
// Awaiting git repository task before running the command if ITR is enabled.
250+
Log.Debug("RunCiCommand: Awaiting for the Git repository upload.");
251+
await uploadRepositoryChangesTask.ConfigureAwait(false);
252+
}
253+
254+
if (Program.CallbackForTests is { } callbackForTests)
245255
{
246-
Program.CallbackForTests(program, arguments, profilerEnvironmentVariables);
256+
callbackForTests(program, arguments, profilerEnvironmentVariables);
247257
return 0;
248258
}
249259

@@ -257,6 +267,14 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCiSettin
257267
exitCode = Utils.RunProcess(processInfo, _applicationContext.TokenSource.Token);
258268
session?.SetTag(TestTags.CommandExitCode, exitCode);
259269
Log.Debug<int>("RunCiCommand: Finished with exit code: {Value}", exitCode);
270+
271+
if (!ciVisibilitySettings.IntelligentTestRunnerEnabled)
272+
{
273+
// Awaiting git repository task after running the command if ITR is disabled.
274+
Log.Debug("RunCiCommand: Awaiting for the Git repository upload.");
275+
await uploadRepositoryChangesTask.ConfigureAwait(false);
276+
}
277+
260278
return exitCode;
261279
}
262280
catch (Exception ex)

tracer/src/Datadog.Trace/Ci/CIVisibility.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,11 @@ private static async Task GetIntelligentTestRunnerSkippableTestsAsync()
493493
{
494494
var lazyItrClient = new Lazy<IntelligentTestRunnerClient>(() => new(CIEnvironmentValues.Instance.WorkspacePath, _settings));
495495

496+
Task? uploadRepositoryChangesTask = null;
496497
if (_settings.GitUploadEnabled != false)
497498
{
498499
// Upload the git metadata
499-
await lazyItrClient.Value.UploadRepositoryChangesAsync().ConfigureAwait(false);
500+
uploadRepositoryChangesTask = Task.Run(() => lazyItrClient.Value.UploadRepositoryChangesAsync());
500501
}
501502

502503
if (!_settings.Agentless || !string.IsNullOrEmpty(_settings.ApplicationKey))
@@ -524,6 +525,13 @@ private static async Task GetIntelligentTestRunnerSkippableTestsAsync()
524525
// Log code coverage status
525526
Log.Information("{V}", _settings.CodeCoverageEnabled == true ? "ITR: Tests code coverage is enabled." : "ITR: Tests code coverage is disabled.");
526527

528+
// For ITR we need the git metadata upload before consulting the skippable tests.
529+
// If ITR is disable we just need to make sure the git upload task has completed before leaving this method.
530+
if (uploadRepositoryChangesTask is not null)
531+
{
532+
await uploadRepositoryChangesTask.ConfigureAwait(false);
533+
}
534+
527535
// If the tests skipping feature is enabled we query the api for the tests we have to skip
528536
if (_settings.TestsSkippingEnabled == true)
529537
{

0 commit comments

Comments
 (0)