From 6c11c1edff539f4538f7aeea3edc22305aea3f79 Mon Sep 17 00:00:00 2001 From: minoura-a <57167143+minoura-a@users.noreply.github.com> Date: Thu, 24 Nov 2022 02:13:29 +0900 Subject: [PATCH] [bug]: Fixed a bug that content cannot be retrieved if "#" is included in a file or folder name. --- .../Helpers/StringExtensionsTests.cs | 25 +++++++++++++++++++ Octokit/Helpers/StringExtensions.cs | 8 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Octokit.Tests/Helpers/StringExtensionsTests.cs b/Octokit.Tests/Helpers/StringExtensionsTests.cs index f342199ef9..e4687019db 100644 --- a/Octokit.Tests/Helpers/StringExtensionsTests.cs +++ b/Octokit.Tests/Helpers/StringExtensionsTests.cs @@ -89,5 +89,30 @@ public void EscapeDoubleQuotesEscapesAllDoubleQuotes() Assert.Equal("\\\"test milestone\\\"", input.EscapeDoubleQuotes()); } } + + public class EncodeSharpMethod + { + [Fact] + public void EncodeSharpReturnsNullForNullInput() + { + Assert.Equal(string.Empty, (null as string).EncodeSharp()); + } + + [Fact] + public void EncodeSharpReturnsInputWithoutDoubleQuotes() + { + string input = "some test input without double quotes in it"; + + Assert.Equal(input, input.EncodeSharp()); + } + + [Fact] + public void EncodeAllSharp() + { + string input = "#some test input with # in it#"; + + Assert.Equal("%23some test input with %23 in it%23", input.EncodeSharp()); + } + } } } diff --git a/Octokit/Helpers/StringExtensions.cs b/Octokit/Helpers/StringExtensions.cs index 42ed83361f..e3d288acaf 100644 --- a/Octokit/Helpers/StringExtensions.cs +++ b/Octokit/Helpers/StringExtensions.cs @@ -23,8 +23,9 @@ public static bool IsNotBlank(this string value) public static Uri FormatUri(this string pattern, params object[] args) { Ensure.ArgumentNotNullOrEmptyString(pattern, nameof(pattern)); + var uriString = string.Format(CultureInfo.InvariantCulture, pattern, args).EncodeSharp(); - return new Uri(string.Format(CultureInfo.InvariantCulture, pattern, args), UriKind.Relative); + return new Uri(uriString, UriKind.Relative); } public static string UriEncode(this string input) @@ -106,6 +107,11 @@ internal static string EscapeDoubleQuotes(this string value) return value; } + internal static string EncodeSharp(this string value) + { + return !string.IsNullOrEmpty(value) ? value?.Replace("#", "%23") : string.Empty; + } + static IEnumerable SplitUpperCase(this string source) { Ensure.ArgumentNotNullOrEmptyString(source, nameof(source));