-
-
Notifications
You must be signed in to change notification settings - Fork 13
Added git url validation for Azure DevOps repositories #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
a2d18ee
7c95272
b1a8d8e
a2418c2
ef3e71a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,42 @@ namespace Dutchskull.Aspire.PolyRepo; | |
|
|
||
| internal static partial class GitUrlUtilities | ||
| { | ||
| internal static string GetProjectNameFromGitUrl(string gitUrl) => | ||
| gitUrl.RemovePostfix(".git").Split('/')[^1]; | ||
| internal static string GetProjectNameFromGitUrl(string gitUrl) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it maybe an idea to create a Uri object and get the segment you want by index? The reason I did some string math before is because it was a simple Uri. Now that it is more complex, a more simple solution that would scale would be nice.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you looking for "simpler", or more performant? I just pushed some changes with some optimizations for parsing and validation the URI.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like it simpler. Because I can image gitea and gitlab also having different Uri's and then it is interesting to just have a index to change of which segment of the uri you want to have. I am moving, so it could be that I will react after Monday. |
||
| { | ||
| ReadOnlySpan<char> gitUrlSpan = gitUrl.AsSpan().TrimEnd('/'); | ||
| int projectNameStart = gitUrlSpan.LastIndexOf('/'); | ||
|
|
||
| if (projectNameStart < 0 || projectNameStart == gitUrlSpan.Length - 1) | ||
| { | ||
| throw new ArgumentException("Git URL could not be parsed.", nameof(gitUrl)); | ||
| } | ||
|
|
||
| ReadOnlySpan<char> encodedProjectName = gitUrlSpan[(projectNameStart + 1)..]; | ||
|
|
||
| if (encodedProjectName.EndsWith(".git", StringComparison.Ordinal)) | ||
| { | ||
| encodedProjectName = encodedProjectName[..^4]; | ||
| } | ||
|
|
||
| string projectName = encodedProjectName.Contains('%') | ||
| ? Uri.UnescapeDataString(encodedProjectName.ToString()) | ||
| : encodedProjectName.ToString(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(projectName) || | ||
| projectName is "." or ".." || | ||
| projectName.Contains('/') || | ||
| projectName.Contains('\\') || | ||
| projectName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) | ||
| { | ||
| throw new ArgumentException("Git URL resolved to an unsafe repository name.", nameof(gitUrl)); | ||
| } | ||
|
|
||
| return projectName; | ||
| } | ||
|
|
||
| internal static bool IsValidGitUrl(string url) => | ||
| !string.IsNullOrEmpty(url) && GitUrlRegex().IsMatch(url); | ||
|
|
||
| [GeneratedRegex(@"^(?:git|https?|git@[\w\.]+):\/\/[\w\.@\:\/\-~]+\.git(?:\/)?$")] | ||
| [GeneratedRegex(@"^(?:(?:git|https?):\/\/[^\s]+\.git(?:\/)?|https:\/\/(?:[\w.-]+@)?dev\.azure\.com\/[^\/\s]+\/[^\/\s]+\/_git\/[^\/\s]+(?:\/)?|git@ssh\.dev\.azure\.com:v3\/[^\/\s]+\/[^\/\s]+\/[^\/\s]+(?:\/)?)$")] | ||
|
||
| private static partial Regex GitUrlRegex(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.