Skip to content

Commit

Permalink
[CI Visibility] Catch some gitinfo exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo committed Nov 18, 2024
1 parent 43ad231 commit 98a70ad
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions tracer/src/Datadog.Trace/Ci/CiEnvironment/GitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security;
using Datadog.Trace.Logging;

namespace Datadog.Trace.Ci.CiEnvironment;
Expand Down Expand Up @@ -156,13 +157,35 @@ public static IGitInfo GetCurrent()
return null;
}

var dirInfo = new DirectoryInfo(innerFolder);
DirectoryInfo? dirInfo;

try
{
dirInfo = new DirectoryInfo(innerFolder);
}
catch (Exception ex)
{
Log.Error(ex, "Error getting directory info");
return null;
}

while (dirInfo != null)
{
DirectoryInfo[] gitDirectories;
try
{
gitDirectories = dirInfo.GetDirectories(".git");
var gitDirectories = dirInfo.GetDirectories(".git");
if (gitDirectories.Length > 0)
{
foreach (var gitDir in gitDirectories)
{
if (gitDir.Name == ".git")
{
return gitDir;
}
}
}

dirInfo = dirInfo.Parent;
}
catch (DirectoryNotFoundException ex)
{
Expand All @@ -174,19 +197,11 @@ public static IGitInfo GetCurrent()
Log.Error(ex, "Get directories failed with UnauthorizedAccessException");
return null;
}

if (gitDirectories.Length > 0)
catch (SecurityException ex)
{
foreach (var gitDir in gitDirectories)
{
if (gitDir.Name == ".git")
{
return gitDir;
}
}
Log.Error(ex, "Get directories or parent directory failed with SecurityException");
return null;
}

dirInfo = dirInfo.Parent;
}

return null;
Expand Down

0 comments on commit 98a70ad

Please sign in to comment.