/
TranslateRepositoryUrlGitTask.cs
156 lines (131 loc) · 6.37 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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();
protected virtual bool IsProtocolSupportedByProvider(string protocol)
=> true;
public override bool Execute()
{
ExecuteImpl();
return !Log.HasLoggedErrors;
}
private void ExecuteImpl()
{
var hostUris = GetHostUris().ToArray();
if (hostUris.Length == 0)
{
return;
}
static bool getUri(string? url, out Uri uri) => Uri.TryCreate(url, UriKind.Absolute, out uri);
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(Uri uri)
{
if(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) ?? uri.OriginalString;
}
return uri.OriginalString;
}
var providerName = this.GetType().Namespace;
if(getUri(RepositoryUrl, out var repoUrlUri))
{
if(!IsProtocolSupportedByProvider(repoUrlUri.Scheme))
{
// TODO should this be an error or a warning?
Log.LogError(CommonResources.RepositoryUrlIsNotSupportedByProvider, providerName, repoUrlUri.Scheme);
return;
}
TranslatedRepositoryUrl = translate(repoUrlUri);
}
else
{
// preserve previous behavior of using the URL as is if URI cannot be parsed.
TranslatedRepositoryUrl = RepositoryUrl;
Log.LogWarning(CommonResources.RepositoryUrlIsNotComplete, RepositoryUrl);
}
TranslatedSourceRoots = SourceRoots;
if (TranslatedSourceRoots != null)
{
foreach (var sourceRoot in TranslatedSourceRoots)
{
if (!string.Equals(sourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
var scmRepoUrl = sourceRoot.GetMetadata(Names.SourceRoot.ScmRepositoryUrl);
if (getUri(scmRepoUrl, out var scmRepoUri))
{
if (!IsProtocolSupportedByProvider(scmRepoUri.Scheme))
{
// TODO should this be an error or a warning?
Log.LogError(CommonResources.RepositoryUrlIsNotSupportedByProvider, providerName, scmRepoUri.Scheme);
continue;
}
setScmRepoUrlMetaData(translate(scmRepoUri));
}
else
{
// preserve previous behavior of using the URL as is if URI cannot be parsed.
Log.LogWarning(CommonResources.RepositoryUrlIsNotComplete, scmRepoUrl);
setScmRepoUrlMetaData(scmRepoUrl);
}
void setScmRepoUrlMetaData(string url)
{
// 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(url));
}
}
}
}
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;
}
}
}
}