/
TranslateRepositoryUrlGitTask.cs
132 lines (111 loc) · 4.95 KB
/
TranslateRepositoryUrlGitTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See
// License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.Build.Tasks.SourceControl
{
public class TranslateRepositoryUrlsGitTask : Task
{
private const string SourceControlName = "git";
public string? RepositoryUrl { get; set; }
public ITaskItem[]? SourceRoots { get; set; }
public ITaskItem[]? Hosts { get; set; }
public bool IsSingleProvider { get; set; }
[Output]
public string? TranslatedRepositoryUrl { get; set; }
[Output]
public ITaskItem[]? TranslatedSourceRoots { get; set; }
protected virtual string? TranslateSshUrl(Uri uri)
=> "https://" + uri.GetHost() + uri.GetPathAndQuery();
protected virtual string? TranslateGitUrl(Uri uri)
=> "https://" + uri.GetHost() + uri.GetPathAndQuery();
protected virtual string? TranslateHttpUrl(Uri uri)
=> uri.GetScheme() + "://" + uri.GetAuthority() + uri.GetPathAndQuery();
public override bool Execute()
{
ExecuteImpl();
return !Log.HasLoggedErrors;
}
private void ExecuteImpl()
{
var hostUris = GetHostUris().ToArray();
if (hostUris.Length == 0)
{
return;
}
static bool isMatchingHostUri(Uri hostUri, Uri uri)
=> uri.GetHost().Equals(hostUri.GetHost(), StringComparison.OrdinalIgnoreCase) ||
uri.GetHost().EndsWith("." + hostUri.GetHost(), StringComparison.OrdinalIgnoreCase);
// only need to translate valid ssh URLs that match one of our hosts:
string? translate(string? url)
{
if (Uri.TryCreate(url, UriKind.Absolute, out var uri) &&
hostUris.Any(h => isMatchingHostUri(h, uri)))
{
return (uri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase) ? TranslateHttpUrl(uri) :
uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase) ? TranslateHttpUrl(uri) :
uri.Scheme.Equals("ssh", StringComparison.OrdinalIgnoreCase) ? TranslateSshUrl(uri) :
uri.Scheme.Equals("git", StringComparison.OrdinalIgnoreCase) ? TranslateGitUrl(uri) : null) ?? url;
}
return url;
}
try
{
TranslatedRepositoryUrl = translate(RepositoryUrl);
}
catch (NotSupportedException e)
{
Log.LogError(e.Message);
return;
}
TranslatedSourceRoots = SourceRoots;
if (TranslatedSourceRoots != null)
{
foreach (var sourceRoot in TranslatedSourceRoots)
{
if (!string.Equals(sourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
string? translatedUrl;
try
{
translatedUrl = translate(sourceRoot.GetMetadata(Names.SourceRoot.ScmRepositoryUrl));
}
catch (NotSupportedException e)
{
Log.LogError(e.Message);
continue;
}
// Item metadata are stored msbuild-escaped. GetMetadata unescapes, SetMetadata
// stores the value as specified. When initializing the URL metadata from git
// information we msbuild-escaped the URL to preserve any URL escapes in it.
// Here, GetMetadata unescapes the msbuild escapes, then we translate the URL
// and finally msbuild-escape the resulting URL to preserve any URL escapes.
sourceRoot.SetMetadata(Names.SourceRoot.ScmRepositoryUrl, Evaluation.ProjectCollection.Escape(translatedUrl));
}
}
}
private IEnumerable<Uri> GetHostUris()
{
if (Hosts != null)
{
foreach (var item in Hosts)
{
if (UriUtilities.TryParseAuthority(item.ItemSpec, out var hostUri))
{
yield return hostUri;
}
}
}
// Add implicit host last, so that matching prefers explicitly listed hosts over the implicit one.
if (IsSingleProvider && Uri.TryCreate(RepositoryUrl, UriKind.Absolute, out var repositoryUri))
{
yield return repositoryUri;
}
}
}
}