From 13ba84b060ea1e025a7dcfa6c843289002cdab67 Mon Sep 17 00:00:00 2001 From: Chris Simpson Date: Fri, 15 Jul 2022 20:50:35 +0100 Subject: [PATCH] fix: Trims trailing slashes from user passed paths that appear before query string parameters (#2486) --- .../Clients/RepositoryContentsClientTests.cs | 16 +++++++++++++++- Octokit/Helpers/ApiUrls.cs | 8 ++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs index d2283388b0..099209d1b5 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs @@ -216,7 +216,21 @@ public async Task GetsDirectoryContent() var contents = await github .Repository .Content - .GetAllContentsByRef("octokit", "octokit.net", "Octokit", "master"); + .GetAllContentsByRef("octokit", "octokit.net", "Octokit", "main"); + + Assert.True(contents.Count > 2); + Assert.Equal(ContentType.Dir, contents.First().Type); + } + + [IntegrationTest] + public async Task GetsDirectoryContentWithTrailingSlash() + { + var github = Helper.GetAuthenticatedClient(); + + var contents = await github + .Repository + .Content + .GetAllContentsByRef("octokit", "octokit.net", "Octokit/", "main"); Assert.True(contents.Count > 2); Assert.Equal(ContentType.Dir, contents.First().Type); diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 5544d18a59..32955feb11 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -1698,7 +1698,7 @@ public static Uri Tree(string owner, string name, string reference) /// public static Uri TreeRecursive(string owner, string name, string reference) { - return "repos/{0}/{1}/git/trees/{2}?recursive=1".FormatUri(owner, name, reference); + return "repos/{0}/{1}/git/trees/{2}?recursive=1".FormatUri(owner, name, reference.TrimEnd('/')); } /// @@ -2445,7 +2445,7 @@ public static Uri RepositoryArchiveLink(string owner, string name, ArchiveFormat /// The for getting the contents of the specified repository and path public static Uri RepositoryContent(string owner, string name, string path, string reference) { - return "repos/{0}/{1}/contents/{2}?ref={3}".FormatUri(owner, name, path == "/" ? "" : path, reference); + return "repos/{0}/{1}/contents/{2}?ref={3}".FormatUri(owner, name, path == "/" ? "" : path.TrimEnd('/'), reference); } /// @@ -3451,7 +3451,7 @@ public static Uri RepositoryContent(long repositoryId, string path) /// The for getting the contents of the specified repository and path public static Uri RepositoryContent(long repositoryId, string path, string reference) { - return "repositories/{0}/contents/{1}?ref={2}".FormatUri(repositoryId, path, reference); + return "repositories/{0}/contents/{1}?ref={2}".FormatUri(repositoryId, path.TrimEnd('/'), reference); } /// @@ -3708,7 +3708,7 @@ public static Uri Tree(long repositoryId, string reference) /// The for the specified tree. public static Uri TreeRecursive(long repositoryId, string reference) { - return "repositories/{0}/git/trees/{1}?recursive=1".FormatUri(repositoryId, reference); + return "repositories/{0}/git/trees/{1}?recursive=1".FormatUri(repositoryId, reference.TrimEnd('/')); } ///