Skip to content

Commit 8fb40aa

Browse files
[CI Visibility] Fix detached head situation (#6594)
## Summary of changes In a detached head situation, create a synthetic branch name like `auto:git-detached-head`. ## Reason for change An empty branch name (as when a detached head happens) leads to many of the TestVis features to be disabled. ## Implementation details ## Test coverage ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. -->
1 parent 819d718 commit 8fb40aa

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public IntelligentTestRunnerClient(string? workingDirectory, CIVisibilitySetting
8888
_customConfigurations = GetCustomTestsConfigurations(_settings.TracerSettings.GlobalTags);
8989

9090
_repositoryUrl = GetRepositoryUrl();
91-
_branchName = GetBranchName();
9291
_commitSha = GetCommitSha();
92+
_branchName = GetBranchName();
93+
9394
_apiRequestFactory = CIVisibility.GetRequestFactory(_settings.TracerSettings, TimeSpan.FromSeconds(45));
9495

9596
const string settingsUrlPath = "api/v2/libraries/tests/services/setting";
@@ -1269,7 +1270,15 @@ private string GetBranchName()
12691270
}
12701271

12711272
var gitOutput = GitCommandHelper.RunGitCommand(_workingDirectory, "branch --show-current", MetricTags.CIVisibilityCommands.GetBranch);
1272-
return gitOutput?.Output.Replace("\n", string.Empty) ?? string.Empty;
1273+
var res = gitOutput?.Output.Replace("\n", string.Empty) ?? string.Empty;
1274+
1275+
if (string.IsNullOrEmpty(res))
1276+
{
1277+
Log.Warning("ITR: empty branch indicates a detached head at commit {Commit}", _commitSha);
1278+
res = $"auto:git-detached-head";
1279+
}
1280+
1281+
return res;
12731282
}
12741283

12751284
private string GetCommitSha()

tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/CI/TestingFrameworkImpactedTests.cs

+25-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class TestingFrameworkImpactedTests : TestingFrameworkTest
2929
#pragma warning disable SA1401 // FieldsMustBePrivate
3030
protected const string ModifiedLine = "// Modified by TestingFrameworkImpactedTests.cs";
3131
protected const int ExpectedTestCount = 16;
32-
protected const string GitHubBaseSha = "a700b56e12ddcbad5d19a2fa8852a15518ab205b";
32+
protected string baseSha = string.Empty;
3333
protected string repositoryRoot = string.Empty;
3434
protected string repo = string.Empty;
3535
protected string branch = string.Empty;
@@ -76,7 +76,7 @@ protected string GetSettingsJson(bool enabled = false)
7676

7777
protected string GetDiffFilesJson(bool baseCommit = true)
7878
{
79-
var commitValue = baseCommit ? GitHubBaseSha : string.Empty;
79+
var commitValue = baseCommit ? baseSha : string.Empty;
8080
return $$"""
8181
{
8282
"data": {
@@ -167,7 +167,6 @@ protected override Dictionary<string, string> DefineCIEnvironmentValues(Dictiona
167167
{
168168
// Base sets Azure CI values. Take those we can reuse for Git Hub
169169
repo = values[CIEnvironmentValues.Constants.AzureBuildRepositoryUri];
170-
branch = values[CIEnvironmentValues.Constants.AzureBuildSourceBranch];
171170

172171
return values;
173172
}
@@ -198,13 +197,13 @@ protected void InjectGitHubActionsSession(bool setupPr = true, bool? enabled = t
198197
SetEnvironmentVariable(ConfigurationKeys.CIVisibility.ImpactedTestsDetectionEnabled, enabled.Value ? "True" : "False");
199198
}
200199

201-
static string GetEventJsonFile()
200+
string GetEventJsonFile()
202201
{
203202
string content = $$"""
204203
{
205204
"pull_request": {
206205
"base": {
207-
"sha": "{{GitHubBaseSha}}"
206+
"sha": "{{baseSha}}"
208207
}
209208
}
210209
}
@@ -243,6 +242,26 @@ private void InitGit()
243242
output = RunGitCommand("branch --show-current");
244243
}
245244

245+
if (output.ExitCode == 0)
246+
{
247+
// Retrieve branch name
248+
branch = output.Output.Trim();
249+
}
250+
251+
if (output.ExitCode == 0)
252+
{
253+
// Retrieve last commit
254+
output = RunGitCommand("rev-parse --verify HEAD");
255+
if (output.ExitCode == 0)
256+
{
257+
baseSha = output.Output.Trim();
258+
if (string.IsNullOrEmpty(branch))
259+
{
260+
branch = $"auto:git-detached-head";
261+
}
262+
}
263+
}
264+
246265
if (output.ExitCode == 0)
247266
{
248267
// Retrieve WS root directory
@@ -251,7 +270,7 @@ private void InitGit()
251270
{
252271
gitAvailable = true;
253272
repositoryRoot = output.Output.Trim();
254-
Output.WriteLine($"Git available. Repository: {repositoryRoot}");
273+
Output.WriteLine($"Git available. Repository: {repositoryRoot} Branch: {branch} Sha: {baseSha}");
255274
}
256275
}
257276

0 commit comments

Comments
 (0)