-
Notifications
You must be signed in to change notification settings - Fork 129
1180 fix url validation #1464
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
1180 fix url validation #1464
Changes from 6 commits
1fe142f
836364a
4387a57
f21b11c
786bbe5
11c53c4
75dcb0e
a061ec7
f0e9825
56a43b0
3561fe4
02efd86
2ef9d49
5a2f2ff
2ec88b9
c622573
998011b
cf12f4e
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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| - Added validation to detect and return clear error messages when a URL is provided instead of a name for organization, repository, or enterprise arguments (e.g., `--github-org`, `--github-target-org`, `--source-repo`, `--github-target-enterprise`) | ||
|
|
||
| - bbs2gh : Added validation for `--archive-path` and `--bbs-shared-home` options to fail fast with clear error messages if the provided paths do not exist or are not accessible. Archive path is now logged before upload operations to help with troubleshooting |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,5 +26,29 @@ public static class StringExtensions | |
| public static string EscapeDataString(this string value) => Uri.EscapeDataString(value); | ||
|
|
||
| public static byte[] ToBytes(this string s) => Encoding.UTF8.GetBytes(s); | ||
|
|
||
| public static bool IsUrl(this string s) | ||
|
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. Would prefer we use standard dotnet core library to validate something like below referencing https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=net-8.0 public static bool IsUrl(this string s)
{
if (s.IsNullOrWhiteSpace())
{
return false;
}
return Uri.TryCreate(s, UriKind.Absolute, out var uri)
&& (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
}
Contributor
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. Done! The implementation now uses Uri.TryCreate() for validation, and the tests have been updated accordingly |
||
| { | ||
| if (s.IsNullOrWhiteSpace()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if string starts with http:// or https:// | ||
| if (s.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || | ||
| s.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if string contains common URL patterns like domain.com/path or www. | ||
| if (s.Contains("://") || s.StartsWith("www.", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if it looks like a URL path (contains / and .) | ||
| return s.Contains('/') && s.Contains('.'); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using OctoshiftCLI.Commands.CreateTeam; | ||
| using OctoshiftCLI.Services; | ||
| using Xunit; | ||
|
|
||
| namespace OctoshiftCLI.Tests.Octoshift.Commands.CreateTeam; | ||
|
|
||
| public class CreateTeamCommandArgsTests | ||
| { | ||
| private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>(); | ||
|
|
||
| private const string GITHUB_ORG = "foo-org"; | ||
| private const string TEAM_NAME = "my-team"; | ||
|
|
||
| [Fact] | ||
| public void Validate_Throws_When_GithubOrg_Is_Url() | ||
| { | ||
| var args = new CreateTeamCommandArgs | ||
| { | ||
| GithubOrg = "http://github.com/my-org", | ||
| TeamName = TEAM_NAME | ||
| }; | ||
|
|
||
| FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object)) | ||
| .Should() | ||
| .ThrowExactly<OctoshiftCliException>() | ||
| .WithMessage("The --github-org option expects an organization name, not a URL. Please provide just the organization name (e.g., 'my-org' instead of 'https://github.com/my-org')."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_Succeeds_With_Valid_Name() | ||
| { | ||
| var args = new CreateTeamCommandArgs | ||
| { | ||
| GithubOrg = GITHUB_ORG, | ||
| TeamName = TEAM_NAME | ||
| }; | ||
|
|
||
| args.Validate(_mockOctoLogger.Object); | ||
|
|
||
| args.GithubOrg.Should().Be(GITHUB_ORG); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using OctoshiftCLI.Commands.DownloadLogs; | ||
| using OctoshiftCLI.Services; | ||
| using Xunit; | ||
|
|
||
| namespace OctoshiftCLI.Tests.Octoshift.Commands.DownloadLogs; | ||
|
|
||
| public class DownloadLogsCommandArgsTests | ||
| { | ||
| private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>(); | ||
|
|
||
| private const string GITHUB_ORG = "foo-org"; | ||
| private const string GITHUB_REPO = "foo-repo"; | ||
|
|
||
| [Fact] | ||
| public void Validate_Throws_When_GithubOrg_Is_Url() | ||
| { | ||
| var args = new DownloadLogsCommandArgs | ||
| { | ||
| GithubOrg = "https://github.com/my-org", | ||
| GithubRepo = GITHUB_REPO | ||
| }; | ||
|
|
||
| FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object)) | ||
| .Should() | ||
| .ThrowExactly<OctoshiftCliException>() | ||
| .WithMessage("The --github-org option expects an organization name, not a URL. Please provide just the organization name (e.g., 'my-org' instead of 'https://github.com/my-org')."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_Throws_When_GithubRepo_Is_Url() | ||
| { | ||
| var args = new DownloadLogsCommandArgs | ||
| { | ||
| GithubOrg = GITHUB_ORG, | ||
| GithubRepo = "github.com/org/repo" | ||
| }; | ||
|
|
||
| FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object)) | ||
| .Should() | ||
| .ThrowExactly<OctoshiftCliException>() | ||
| .WithMessage("The --github-repo option expects a repository name, not a URL. Please provide just the repository name (e.g., 'my-repo' instead of 'https://github.com/my-org/my-repo')."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_Succeeds_With_Valid_Names() | ||
| { | ||
| var args = new DownloadLogsCommandArgs | ||
| { | ||
| GithubOrg = GITHUB_ORG, | ||
| GithubRepo = GITHUB_REPO | ||
| }; | ||
|
|
||
| args.Validate(_mockOctoLogger.Object); | ||
|
|
||
| args.GithubOrg.Should().Be(GITHUB_ORG); | ||
| args.GithubRepo.Should().Be(GITHUB_REPO); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using OctoshiftCLI.Commands.GenerateMannequinCsv; | ||
| using OctoshiftCLI.Services; | ||
| using Xunit; | ||
|
|
||
| namespace OctoshiftCLI.Tests.Octoshift.Commands.GenerateMannequinCsv; | ||
|
|
||
| public class GenerateMannequinCsvCommandArgsTests | ||
| { | ||
| private readonly Mock<OctoLogger> _mockOctoLogger = TestHelpers.CreateMock<OctoLogger>(); | ||
|
|
||
| private const string GITHUB_ORG = "foo-org"; | ||
|
|
||
| [Fact] | ||
| public void Validate_Throws_When_GithubOrg_Is_Url() | ||
| { | ||
| var args = new GenerateMannequinCsvCommandArgs | ||
| { | ||
| GithubOrg = "www.github.com" | ||
| }; | ||
|
|
||
| FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object)) | ||
| .Should() | ||
| .ThrowExactly<OctoshiftCliException>() | ||
| .WithMessage("The --github-org option expects an organization name, not a URL. Please provide just the organization name (e.g., 'my-org' instead of 'https://github.com/my-org')."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_Succeeds_With_Valid_Name() | ||
| { | ||
| var args = new GenerateMannequinCsvCommandArgs | ||
| { | ||
| GithubOrg = GITHUB_ORG | ||
| }; | ||
|
|
||
| args.Validate(_mockOctoLogger.Object); | ||
|
|
||
| args.GithubOrg.Should().Be(GITHUB_ORG); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.