Skip to content

Commit

Permalink
Fix #6146, add support for the "new" dev.azure.com style URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerRaviv committed Oct 16, 2024
1 parent aa5cedc commit 02e5c58
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ internal class AzureDevOpsSourceLinkUrlParser : SourceLinkUrlParser
/// 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&versionType=commit&version=0e4d29442102e6cef1c271025d513c8b2187bcd6&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 Down Expand Up @@ -52,9 +58,9 @@ internal override bool TryParseSourceLinkUrl(Uri uri, [NotNullWhen(true)] out st
return false;
}

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

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

private static string? BuildRepositoryUrl(Uri uri)
{
var segments = uri.AbsolutePath.Split(['/'], 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}";
}
else 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}";
}
else
{
throw new NotSupportedException($"Unsupported Azure DevOps host: {uri.Host}");
}
}

private static NameValueCollection ParseQueryString(string queryString)
{
// We can't use HttpUtility.ParseQueryString because it would mean taking a dependency on System.Web.
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

0 comments on commit 02e5c58

Please sign in to comment.