Skip to content

Commit

Permalink
Implements since and until parameters when retrieving commit list (#516)
Browse files Browse the repository at this point in the history
* Implements since and until parameters when retrieving commit list

* Replaces WebUtility.UrlEncode with Uri.EscapeDataString

---------

Co-authored-by: Gérald Barré <[email protected]>
  • Loading branch information
steve85 and meziantou authored Sep 6, 2023
1 parent fde8c62 commit a6333c1
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
96 changes: 96 additions & 0 deletions NGitLab.Tests/RepositoryClient/RepositoryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion NGitLab/GetCommitsRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NGitLab
using System;

namespace NGitLab
{
public class GetCommitsRequest
{
Expand All @@ -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; }
}
}
11 changes: 11 additions & 0 deletions NGitLab/Impl/RepositoryClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using NGitLab.Extensions;
Expand Down Expand Up @@ -85,6 +86,16 @@ public IEnumerable<Commit> 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()}");

Expand Down
4 changes: 4 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a6333c1

Please sign in to comment.