Skip to content
Merged
Changes from 3 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
52 changes: 52 additions & 0 deletions src/Microsoft.DotNet.Build.Tasks.Feed/src/BuildModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Microsoft.DotNet.Build.Tasks.Feed
Expand Down Expand Up @@ -71,6 +72,11 @@ public BuildModelFactory(

private const string AssetsVirtualDir = "assets/";

private static readonly string AzureDevOpsHostPattern = @"dev\.azure\.com\";

private readonly Regex LegacyRepositoryUriPattern = new Regex(
@"^https://(?<account>[a-zA-Z0-9]+)\.visualstudio\.com/");

/// <summary>
/// Create a build manifest for packages, blobs, and associated signing information
/// </summary>
Expand Down Expand Up @@ -204,6 +210,9 @@ private BuildModel CreateModel(
{
_log.LogError("Missing 'location' property from ManifestBuildData");
}

NormalizeUrisInBuildData(attributes);

BuildModel buildModel = new BuildModel(
new BuildIdentity
{
Expand Down Expand Up @@ -241,5 +250,48 @@ private bool ManifestBuildDataHasLocationInformation(IDictionary<string, string>
{
return attributes.ContainsKey("Location") || attributes.ContainsKey("InitialAssetsLocation");
}

private void NormalizeUrisInBuildData(IDictionary<string, string> attributes)
{
if (attributes.ContainsKey("AzureDevOpsRepository"))
{
attributes["AzureDevOpsRepository"] = NormalizeUrl(attributes["AzureDevOpsRepository"]);
}

if (attributes.ContainsKey("InitialAssetsLocation"))
{
attributes["InitialAssetsLocation"] = NormalizeUrl(attributes["InitialAssetsLocation"]);
}
}

/// <summary>
// If repoUri includes the user in the account we remove it from URIs like
// https://dnceng@dev.azure.com/dnceng/internal/_git/repo
// If the URL host is of the form "dnceng.visualstudio.com" like
// https://dnceng.visualstudio.com/internal/_git/repo we replace it to "dev.azure.com/dnceng"
// for consistency
/// </summary>
/// <param name="repoUri">The original url</param>
/// <returns>Transformed url</returns>
private string NormalizeUrl(string repoUri)
Comment thread
michellemcdaniel marked this conversation as resolved.
Outdated
{
if (Uri.TryCreate(repoUri, UriKind.Absolute, out Uri parsedUri))
{
if (!string.IsNullOrEmpty(parsedUri.UserInfo))
{
repoUri = repoUri.Replace($"{parsedUri.UserInfo}@", string.Empty);
}

Match m = LegacyRepositoryUriPattern.Match(repoUri);

if (m.Success)
{
string replacementUri = $"{Regex.Unescape(AzureDevOpsHostPattern)}/{m.Groups["account"].Value}";
repoUri = repoUri.Replace(parsedUri.Host, replacementUri);
}
}

return repoUri;
}
}
}