Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the "new" dev.azure.com style URLs in SourceLink URL parsing logic #6159

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ namespace Datadog.Trace.Pdb.SourceLink;
internal class AzureDevOpsSourceLinkUrlParser : SourceLinkUrlParser
{
/// <summary>
/// Extract the git commit sha and repository url from a Azure DevOps SourceLink mapping string.
/// For example, for the following SourceLink mapping string:
/// https://test.visualstudio.com/test-org/_apis/git/repositories/my-repo/items?api-version=1.0&amp;versionType=commit&amp;version=dd35903c688a74b62d1c6a9e4f41371c65704db8&amp;path=/*
/// It will return:
/// Extract the git commit sha and repository url from a Azure DevOps SourceLink mapping string.
/// For example, for the following SourceLink mapping string:
/// https://test.visualstudio.com/test-org/_apis/git/repositories/my-repo/items?api-version=1.0&amp;versionType=commit
/// &amp;version=dd35903c688a74b62d1c6a9e4f41371c65704db8&amp;path=/*
/// It will return:
/// - commit sha: dd35903c688a74b62d1c6a9e4f41371c65704db8
/// - repository URL: https://test.visualstudio.com/test-org/_git/my-repo
/// Likewise, for the following SourceLink mapping string:
/// https://dev.azure.com/organisation/project/_apis/git/repositories/example.shopping.api/items?api-version=1.0&amp;
/// versionType=commit&amp;version=0e4d29442102e6cef1c271025d513c8b2187bcd6&amp;path=/*
/// It will return:
/// - commit sha: 0e4d29442102e6cef1c271025d513c8b2187bcd6
/// - repository URL: https://dev.azure.com/organisation/project/_git/example.shopping.api
/// </summary>
internal override bool TryParseSourceLinkUrl(Uri uri, [NotNullWhen(true)] out string? commitSha, [NotNullWhen(true)] out string? repositoryUrl)
{
Expand All @@ -46,15 +53,9 @@ internal override bool TryParseSourceLinkUrl(Uri uri, [NotNullWhen(true)] out st
return false;
}

var segments = uri.AbsolutePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length < 5)
{
return false;
}
repositoryUrl = BuildRepositoryUrl(uri);

repositoryUrl = $"https://{uri.Host}/{segments[0]}/_git/{segments[4]}";

return true;
return repositoryUrl != null;
}
catch (Exception ex)
{
Expand All @@ -64,6 +65,35 @@ internal override bool TryParseSourceLinkUrl(Uri uri, [NotNullWhen(true)] out st
return false;
}

private static string? BuildRepositoryUrl(Uri uri)
{
var segments = uri.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length < 5)
{
return null;
}

if (uri.Host.EndsWith("visualstudio.com", StringComparison.OrdinalIgnoreCase))
{
// Legacy format: https://{organization}.visualstudio.com
var project = segments[0];
var repoName = segments[4];
return $"https://{uri.Host}/{project}/_git/{repoName}";
}

if (uri.Host.EndsWith("dev.azure.com", StringComparison.OrdinalIgnoreCase))
{
// New format: https://dev.azure.com/{organization}
var organization = segments[0];
var project = segments[1];
var repoName = segments[5];
return $"https://{uri.Host}/{organization}/{project}/_git/{repoName}";
}

Log.Error("Unsupported Azure DevOps host: {Host}", uri.Host);
return null;
}

private static NameValueCollection ParseQueryString(string queryString)
{
// We can't use HttpUtility.ParseQueryString because it would mean taking a dependency on System.Web.
Expand All @@ -73,7 +103,7 @@ private static NameValueCollection ParseQueryString(string queryString)
var pairs = queryString.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var pair in pairs)
{
var parts = pair.Split(new[] { '=' }, 2);
var parts = pair.Split(new char[] { '=' }, 2);
if (parts.Length == 2)
{
query.Add(parts[0], parts[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static IEnumerable<object[]> ValidTestCases
yield return new object[] { "https://api.bitbucket.org/2.0/repositories/test-org/test-repo/src/dd35903c688a74b62d1c6a9e4f41371c65704db8/*", "dd35903c688a74b62d1c6a9e4f41371c65704db8", "https://bitbucket.org/test-org/test-repo", typeof(BitBucketSourceLinkUrlParser) };
yield return new object[] { "https://test.visualstudio.com/test-org/_apis/git/repositories/my-repo/items?api-version=1.0&versionType=commit&version=dd35903c688a74b62d1c6a9e4f41371c65704db8&path=/*", "dd35903c688a74b62d1c6a9e4f41371c65704db8", "https://test.visualstudio.com/test-org/_git/my-repo", typeof(AzureDevOpsSourceLinkUrlParser) };
yield return new object[] { "https://test-gitlab-domain.com/test-org/test-repo/raw/dd35903c688a74b62d1c6a9e4f41371c65704db8/*", "dd35903c688a74b62d1c6a9e4f41371c65704db8", "https://test-gitlab-domain.com/test-org/test-repo", typeof(GitLabSourceLinkUrlParser) };
yield return new object[] { "https://dev.azure.com/organisation/project/_apis/git/repositories/example.shopping.api/items?api-version=1.0&versionType=commit&version=0e4d29442102e6cef1c271025d513c8b2187bcd6&path=/*", "0e4d29442102e6cef1c271025d513c8b2187bcd6", "https://dev.azure.com/organisation/project/_git/example.shopping.api", typeof(AzureDevOpsSourceLinkUrlParser) };
}
}

Expand Down
Loading