From a6333c1a66f6b05accec3e40ae5d4ce688e4256f Mon Sep 17 00:00:00 2001 From: Steve Ryan Date: Thu, 7 Sep 2023 00:52:25 +1000 Subject: [PATCH] Implements since and until parameters when retrieving commit list (#516) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Implements since and until parameters when retrieving commit list * Replaces WebUtility.UrlEncode with Uri.EscapeDataString --------- Co-authored-by: Gérald Barré --- .../RepositoryClient/RepositoryClientTests.cs | 96 +++++++++++++++++++ NGitLab/GetCommitsRequest.cs | 8 +- NGitLab/Impl/RepositoryClient.cs | 11 +++ NGitLab/PublicAPI.Unshipped.txt | 4 + 4 files changed, 118 insertions(+), 1 deletion(-) diff --git a/NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs b/NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs index 125adc57..0f756ccb 100644 --- a/NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs +++ b/NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs @@ -130,6 +130,102 @@ public async Task GetCommitBySha1Range() Assert.AreEqual(allCommits[3].Id, commits[1].Id); } + [Test] + [NGitLabRetry] + public async Task GetCommitsSince() + { + // Arrange + using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 5); + + var defaultBranch = context.Project.DefaultBranch; + var since = DateTime.UtcNow; + var expectedSinceValue = Uri.EscapeDataString(since.ToString("s", CultureInfo.InvariantCulture)); + var commitRequest = new GetCommitsRequest + { + RefName = defaultBranch, + Since = since, + }; + + // Act + var commits = context.RepositoryClient.GetCommits(commitRequest).ToArray(); + + // Assert + var lastRequestQueryString = context.Context.LastRequest.RequestUri.Query; + + Assert.True(lastRequestQueryString.Contains($"since={expectedSinceValue}")); + } + + [Test] + [NGitLabRetry] + public async Task GetCommitsDoesntIncludeSinceWhenNotSpecified() + { + // Arrange + using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 5); + + var defaultBranch = context.Project.DefaultBranch; + var commitRequest = new GetCommitsRequest + { + RefName = defaultBranch, + Since = null, + }; + + // Act + var commits = context.RepositoryClient.GetCommits(commitRequest).ToArray(); + + // Assert + var lastRequestQueryString = context.Context.LastRequest.RequestUri.Query; + + Assert.False(lastRequestQueryString.Contains("since=")); + } + + [Test] + [NGitLabRetry] + public async Task GetCommitsUntil() + { + // Arrange + using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 5); + + var defaultBranch = context.Project.DefaultBranch; + var until = DateTime.UtcNow; + var expectedUntilValue = Uri.EscapeDataString(until.ToString("s", CultureInfo.InvariantCulture)); + var commitRequest = new GetCommitsRequest + { + RefName = defaultBranch, + Until = until, + }; + + // Act + var commits = context.RepositoryClient.GetCommits(commitRequest).ToArray(); + + // Assert + var lastRequestQueryString = context.Context.LastRequest.RequestUri.Query; + + Assert.True(lastRequestQueryString.Contains($"until={expectedUntilValue}")); + } + + [Test] + [NGitLabRetry] + public async Task GetCommitsDoesntIncludeUntilWhenNotSpecified() + { + // Arrange + using var context = await RepositoryClientTestsContext.CreateAsync(commitCount: 5); + + var defaultBranch = context.Project.DefaultBranch; + var commitRequest = new GetCommitsRequest + { + RefName = defaultBranch, + Until = null, + }; + + // Act + var commits = context.RepositoryClient.GetCommits(commitRequest).ToArray(); + + // Assert + var lastRequestQueryString = context.Context.LastRequest.RequestUri.Query; + + Assert.False(lastRequestQueryString.Contains("until=")); + } + [Test] [NGitLabRetry] public async Task GetCommitDiff() diff --git a/NGitLab/GetCommitsRequest.cs b/NGitLab/GetCommitsRequest.cs index 3f3a9a75..ec24a860 100644 --- a/NGitLab/GetCommitsRequest.cs +++ b/NGitLab/GetCommitsRequest.cs @@ -1,4 +1,6 @@ -namespace NGitLab +using System; + +namespace NGitLab { public class GetCommitsRequest { @@ -13,5 +15,9 @@ public class GetCommitsRequest public int MaxResults { get; set; } public uint PerPage { get; set; } = DefaultPerPage; + + public DateTime? Since { get; set; } + + public DateTime? Until { get; set; } } } diff --git a/NGitLab/Impl/RepositoryClient.cs b/NGitLab/Impl/RepositoryClient.cs index e66d513c..56a0c0ea 100644 --- a/NGitLab/Impl/RepositoryClient.cs +++ b/NGitLab/Impl/RepositoryClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using NGitLab.Extensions; @@ -85,6 +86,16 @@ public IEnumerable GetCommits(GetCommitsRequest request) lst.Add($"first_parent={Uri.EscapeDataString(request.FirstParent.ToString())}"); } + if (request.Since.HasValue) + { + lst.Add($"since={Uri.EscapeDataString(request.Since.Value.ToString("s", CultureInfo.InvariantCulture))}"); + } + + if (request.Until.HasValue) + { + lst.Add($"until={Uri.EscapeDataString(request.Until.Value.ToString("s", CultureInfo.InvariantCulture))}"); + } + var perPage = request.MaxResults > 0 ? Math.Min(request.MaxResults, request.PerPage) : request.PerPage; lst.Add($"per_page={perPage.ToStringInvariant()}"); diff --git a/NGitLab/PublicAPI.Unshipped.txt b/NGitLab/PublicAPI.Unshipped.txt index d28176f9..75705c78 100644 --- a/NGitLab/PublicAPI.Unshipped.txt +++ b/NGitLab/PublicAPI.Unshipped.txt @@ -35,6 +35,10 @@ NGitLab.GetCommitsRequest.PerPage.get -> uint NGitLab.GetCommitsRequest.PerPage.set -> void NGitLab.GetCommitsRequest.RefName.get -> string NGitLab.GetCommitsRequest.RefName.set -> void +NGitLab.GetCommitsRequest.Since.get -> System.DateTime? +NGitLab.GetCommitsRequest.Since.set -> void +NGitLab.GetCommitsRequest.Until.get -> System.DateTime? +NGitLab.GetCommitsRequest.Until.set -> void NGitLab.GitLabClient NGitLab.GitLabClient.AdvancedSearch.get -> NGitLab.ISearchClient NGitLab.GitLabClient.Deployments.get -> NGitLab.IDeploymentClient