forked from dotnet/sourcelink
/
AzureDevOpsUrlParser.cs
275 lines (222 loc) · 8.96 KB
/
AzureDevOpsUrlParser.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.Build.Tasks.SourceControl;
namespace Microsoft.SourceLink
{
/// <summary>
/// URL parsing utilities for Azure DevOps source control providers (Services and Server).
/// See https://github.com/dotnet/sourcelink/blob/master/docs/Implementation/AzureDevOpsUrlParser.md
/// </summary>
internal static class AzureDevOpsUrlParser
{
public static bool IsVisualStudioHostedServer(string host)
=> host.EndsWith(".visualstudio.com", StringComparison.OrdinalIgnoreCase) ||
host.EndsWith(".vsts.me", StringComparison.OrdinalIgnoreCase);
public static bool TryParseHostedHttp(string host, string relativeUrl, [NotNullWhen(true)]out string? projectPath, [NotNullWhen(true)]out string? repositoryName)
{
projectPath = repositoryName = null;
if (!UriUtilities.TrySplitRelativeUrl(relativeUrl, out var parts) || parts.Length == 0)
{
return false;
}
int index = 0;
string? account;
bool isVisualStudioHost = IsVisualStudioHostedServer(host);
if (isVisualStudioHost)
{
// account is stored in the domain, not in the path:
account = null;
// Trim optional "DefaultCollection" from path:
if (StringComparer.OrdinalIgnoreCase.Equals(parts[index], "DefaultCollection"))
{
index++;
}
}
else
{
// Check this is not an 'enterprise' discovery page URL
if (StringComparer.OrdinalIgnoreCase.Equals(parts[0], "e"))
{
return false;
}
// Account is first part of path:
account = parts[index++];
}
if (index == parts.Length)
{
return false;
}
if (!TryParsePath(parts, index, "_git", out var projectName, out var teamName, out repositoryName))
{
return false;
}
projectPath = projectName ?? repositoryName;
if (!isVisualStudioHost)
{
if (teamName != null)
{
return false;
}
projectPath = account + "/" + projectPath;
}
return true;
}
public static bool TryParseOnPremHttp(string relativeUrl, string virtualDirectory, [NotNullWhen(true)]out string? projectPath, [NotNullWhen(true)]out string? repositoryName)
{
projectPath = repositoryName = null;
if (!UriUtilities.TrySplitRelativeUrl(relativeUrl, out var parts) || parts.Length == 0 ||
!UriUtilities.TrySplitRelativeUrl(virtualDirectory, out var virtualDirectoryParts))
{
return false;
}
// skip virtual directory:
if (!parts.Take(virtualDirectoryParts.Length).SequenceEqual(virtualDirectoryParts, StringComparer.OrdinalIgnoreCase))
{
return false;
}
// collection:
int i = virtualDirectoryParts.Length;
var collection = parts[i++];
if (!TryParsePath(parts, i, "_git", out var projectName, out _, out repositoryName))
{
return false;
}
projectPath = string.Join("/", parts, 0, virtualDirectoryParts.Length) + "/" + collection + "/" + (projectName ?? repositoryName);
return true;
}
public static bool TryParseHostedSsh(Uri uri, [NotNullWhen(true)]out string? account, [NotNullWhen(true)]out string? repositoryPath, [NotNullWhen(true)]out string? repositoryName)
{
NullableDebug.Assert(uri != null);
account = repositoryPath = repositoryName = null;
// {"DefaultCollection"|""}/{repositoryPath}/"_ssh"/{"_full"|"_optimized"}/{repositoryName}
if (!UriUtilities.TrySplitRelativeUrl(uri.GetPath(), out var parts) || parts.Length == 0)
{
return false;
}
// Check for v3 url format
if (parts[0] == "v3" &&
parts.Length >= 3 &&
TryParsePath(parts, 2, type: null, out repositoryPath, out repositoryName) &&
repositoryPath != "")
{
// ssh://{user}@{domain}:{port}/v3/{account}/{repositoryPath}/{'_full'|'_optimized'|''}/{repositoryName}
account = parts[1];
}
else
{
// ssh v1/v2 url formats
// ssh://{account}@vs-ssh.visualstudio.com/
account = uri.UserInfo;
int index = 0;
if (StringComparer.OrdinalIgnoreCase.Equals(parts[0], "DefaultCollection"))
{
index++;
}
if (!TryParsePath(parts, index, "_ssh", out repositoryPath, out repositoryName))
{
// Failed to parse path
return false;
}
}
if (account.Length == 0)
{
return false;
}
return true;
}
public static bool TryParseOnPremSsh(Uri uri, [NotNullWhen(true)]out string? repositoryPath, [NotNullWhen(true)]out string? repositoryName)
{
repositoryPath = repositoryName = null;
if (!UriUtilities.TrySplitRelativeUrl(uri.GetPath(), out var parts))
{
return false;
}
if (!TryParseRepositoryName(parts, out int teamNameIndex, "_ssh", out repositoryName))
{
return false;
}
repositoryPath = string.Join("/", parts, 0, teamNameIndex + 1);
return true;
}
private static bool TryParsePath(string[] parts, int startIndex, string? type, [NotNullWhen(true)]out string? repositoryPath, [NotNullWhen(true)]out string? repositoryName)
{
if (TryParsePath(parts, startIndex, type, out var projectName, out var teamName, out repositoryName))
{
repositoryPath = (projectName != null && teamName != null) ? projectName + "/" + teamName : (projectName ?? teamName ?? "");
return true;
}
repositoryPath = null;
return false;
}
private static bool TryParsePath(string[] parts, int projectPartIndex, string? type, out string? projectName, out string? teamName, [NotNullWhen(true)]out string? repositoryName)
{
// {projectName?}/{teamName?}/{type}/{"_full"|"_optimized"|""}/{repositoryName}
projectName = teamName = null;
if (!TryParseRepositoryName(parts, out int teamNameIndex, type, out repositoryName))
{
return false;
}
switch (teamNameIndex - projectPartIndex + 1)
{
case 0:
return true;
case 1:
projectName = parts[projectPartIndex];
return true;
case 2:
projectName = parts[projectPartIndex];
teamName = parts[projectPartIndex + 1];
return true;
default:
return false;
}
}
private static bool TryParseRepositoryName(string[] parts, out int teamNameIndex, string? type, [NotNullWhen(true)]out string? repositoryName)
{
Debug.Assert(type == "_git" || type == "_ssh" || type == null);
// {type}/{"_full"|"_optimized"|""}/{repositoryName}
repositoryName = null;
teamNameIndex = -1;
int i = parts.Length - 1;
if (i < 0)
{
return false;
}
repositoryName = parts[i--];
if (i < 0)
{
return false;
}
if (parts[i] == "_full" || parts[i] == "_optimized")
{
i--;
if (i < 0)
{
return false;
}
}
// _git or _ssh
if (type != null)
{
if (parts[i--] != type)
{
return false;
}
}
else
{
if (parts[i] == "_ssh" || parts[i] == "_git")
{
return false;
}
}
teamNameIndex = i;
return true;
}
}
}