Skip to content

Commit

Permalink
Add page query option for releases (#432)
Browse files Browse the repository at this point in the history
- To be able to skip pages if desired (load first results, then next pages if triggered/required)
- Implement some parts of the query usage in mock
  • Loading branch information
Toa741 authored Apr 27, 2023
1 parent 504ea73 commit e70854e
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 12 deletions.
3 changes: 1 addition & 2 deletions NGitLab.Mock.Tests/CommitsMockTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using NGitLab.Mock.Config;
using NUnit.Framework;

Expand Down
73 changes: 72 additions & 1 deletion NGitLab.Mock.Tests/ReleasesMockTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using NGitLab.Mock.Config;
using NUnit.Framework;

Expand All @@ -25,5 +26,75 @@ public void Test_release()
Assert.AreEqual("1.2.3", singleRelease.TagName);
Assert.AreEqual($"{project.WebUrl}/-/releases/1.2.3", singleRelease.Links.Self);
}

[Test]
public void Test_release_page()
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithProject("Test", configure: project => project
.WithCommit("Changes with tag", tags: new[] { "1.2.3", "1.2.4" })
.WithRelease("user1", "1.2.3", createdAt: DateTime.UtcNow.AddHours(-2), releasedAt: DateTime.UtcNow.AddHours(-2))
.WithRelease("user1", "1.2.4", createdAt: DateTime.UtcNow.AddHours(-1), releasedAt: DateTime.UtcNow.AddHours(-1)))
.BuildServer();

var client = server.CreateClient("user1");
var project = client.Projects.Visible.First();
var releaseClient = client.GetReleases(project.Id);
var firstRelease = releaseClient.GetAsync(new Models.ReleaseQuery
{
PerPage = 1,
Page = 2,
}).SingleOrDefault();

Assert.IsNotNull(firstRelease);
Assert.AreEqual("1.2.3", firstRelease.TagName);
}

[Test]
public void Test_release_sort()
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithProject("Test", configure: project => project
.WithCommit("Changes with tag", tags: new[] { "1.2.3", "1.2.4" })
.WithRelease("user1", "1.2.3", createdAt: DateTime.UtcNow.AddHours(-2), releasedAt: DateTime.UtcNow.AddHours(-2))
.WithRelease("user1", "1.2.4", createdAt: DateTime.UtcNow.AddHours(-1), releasedAt: DateTime.UtcNow.AddHours(-1)))
.BuildServer();

var client = server.CreateClient("user1");
var project = client.Projects.Visible.First();
var releaseClient = client.GetReleases(project.Id);
var firstRelease = releaseClient.GetAsync(new Models.ReleaseQuery
{
Sort = "asc",
}).First();

Assert.IsNotNull(firstRelease);
Assert.AreEqual("1.2.3", firstRelease.TagName);
}

[Test]
public void Test_release_orderBy()
{
using var server = new GitLabConfig()
.WithUser("user1", isDefault: true)
.WithProject("Test", configure: project => project
.WithCommit("Changes with tag", tags: new[] { "1.2.3", "1.2.4" })
.WithRelease("user1", "1.2.3", createdAt: DateTime.UtcNow.AddHours(-1), releasedAt: DateTime.UtcNow.AddHours(-1))
.WithRelease("user1", "1.2.4", createdAt: DateTime.UtcNow.AddHours(-2), releasedAt: DateTime.UtcNow.AddHours(-2)))
.BuildServer();

var client = server.CreateClient("user1");
var project = client.Projects.Visible.First();
var releaseClient = client.GetReleases(project.Id);
var firstRelease = releaseClient.GetAsync(new Models.ReleaseQuery
{
OrderBy = "created_at",
}).First();

Assert.IsNotNull(firstRelease);
Assert.AreEqual("1.2.3", firstRelease.TagName);
}
}
}
20 changes: 14 additions & 6 deletions NGitLab.Mock/Clients/ReleaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,22 @@ public IReleaseLinkClient ReleaseLinks(string tagName)
using (Context.BeginOperationScope())
{
var project = GetProject(_projectId, ProjectPermission.View);
var result = project.Releases;
var result = project.Releases.AsEnumerable();
if (query != null)
{
if (!string.IsNullOrEmpty(query.Sort))
throw new NotImplementedException();

if (!string.IsNullOrEmpty(query.OrderBy))
throw new NotImplementedException();
var orderBy = !string.IsNullOrEmpty(query.OrderBy) && string.Equals(query.OrderBy, "created_at", StringComparison.Ordinal)
? new Func<ReleaseInfo, DateTime>(r => r.CreatedAt)
: new Func<ReleaseInfo, DateTime>(r => r.ReleasedAt);

var sortAsc = !string.IsNullOrEmpty(query.Sort) && string.Equals(query.Sort, "asc", StringComparison.Ordinal);
result = sortAsc ? result.OrderBy(orderBy) : result.OrderByDescending(orderBy);

if (query.Page.HasValue)
{
var perPage = query.PerPage ?? 20;
var page = Math.Max(0, query.Page.Value - 1);
result = result.Skip(perPage * page);
}

if (query.IncludeHtmlDescription == true)
throw new NotImplementedException();
Expand Down
7 changes: 6 additions & 1 deletion NGitLab.Mock/Config/GitLabHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,17 @@ public static GitLabProject WithIssue(this GitLabProject project, string? title
});
}

public static GitLabProject WithRelease(this GitLabProject project, string author, string tagName)
public static GitLabProject WithRelease(this GitLabProject project, string author, string tagName, DateTime? createdAt = null, DateTime? releasedAt = null)
{
return Configure(project, _ =>
{
var date = DateTime.UtcNow;
var release = new GitLabReleaseInfo
{
Author = author,
TagName = tagName,
CreatedAt = createdAt ?? date,
ReleasedAt = releasedAt ?? date,
};

project.Releases.Add(release);
Expand Down Expand Up @@ -1194,6 +1197,8 @@ private static void CreateRelease(GitLabServer server, Project project, GitLabRe
{
Author = new UserRef(user),
TagName = gitLabRelease.TagName,
CreatedAt = gitLabRelease.CreatedAt,
ReleasedAt = gitLabRelease.ReleasedAt,
};

project.Releases.Add(release);
Expand Down
8 changes: 7 additions & 1 deletion NGitLab.Mock/Config/GitLabReleaseInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NGitLab.Mock.Config
using System;

namespace NGitLab.Mock.Config
{
/// <summary>
/// Describes a release in a GitLab project
Expand All @@ -12,6 +14,10 @@ public GitLabReleaseInfo()
public string Author { get; set; }

public string TagName { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime ReleasedAt { get; set; }
}

public class GitLabReleaseInfoCollection : GitLabCollection<GitLabReleaseInfo, GitLabProject>
Expand Down
6 changes: 5 additions & 1 deletion NGitLab.Mock/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ NGitLab.Mock.Config.GitLabProject.Visibility.get -> NGitLab.Models.VisibilityLev
NGitLab.Mock.Config.GitLabReleaseInfo
NGitLab.Mock.Config.GitLabReleaseInfo.Author.get -> string
NGitLab.Mock.Config.GitLabReleaseInfo.Author.set -> void
NGitLab.Mock.Config.GitLabReleaseInfo.CreatedAt.get -> System.DateTime
NGitLab.Mock.Config.GitLabReleaseInfo.CreatedAt.set -> void
NGitLab.Mock.Config.GitLabReleaseInfo.GitLabReleaseInfo() -> void
NGitLab.Mock.Config.GitLabReleaseInfo.ReleasedAt.get -> System.DateTime
NGitLab.Mock.Config.GitLabReleaseInfo.ReleasedAt.set -> void
NGitLab.Mock.Config.GitLabReleaseInfo.TagName.get -> string
NGitLab.Mock.Config.GitLabReleaseInfo.TagName.set -> void
NGitLab.Mock.Config.GitLabReleaseInfoCollection
Expand Down Expand Up @@ -1209,7 +1213,7 @@ static NGitLab.Mock.Config.GitLabHelpers.WithMilestone(this NGitLab.Mock.Config.
static NGitLab.Mock.Config.GitLabHelpers.WithMilestone(this NGitLab.Mock.Config.GitLabProject project, string title, System.Action<NGitLab.Mock.Config.GitLabMilestone> configure) -> NGitLab.Mock.Config.GitLabProject
static NGitLab.Mock.Config.GitLabHelpers.WithPipeline(this NGitLab.Mock.Config.GitLabProject project, string ref, System.Action<NGitLab.Mock.Config.GitLabPipeline> configure) -> NGitLab.Mock.Config.GitLabProject
static NGitLab.Mock.Config.GitLabHelpers.WithProject(this NGitLab.Mock.Config.GitLabConfig config, string name = null, int id = 0, string namespace = null, string description = null, string defaultBranch = null, NGitLab.Models.VisibilityLevel visibility = NGitLab.Models.VisibilityLevel.Internal, bool initialCommit = false, bool addDefaultUserAsMaintainer = false, string clonePath = null, string cloneParameters = null, System.Action<NGitLab.Mock.Config.GitLabProject> configure = null) -> NGitLab.Mock.Config.GitLabConfig
static NGitLab.Mock.Config.GitLabHelpers.WithRelease(this NGitLab.Mock.Config.GitLabProject project, string author, string tagName) -> NGitLab.Mock.Config.GitLabProject
static NGitLab.Mock.Config.GitLabHelpers.WithRelease(this NGitLab.Mock.Config.GitLabProject project, string author, string tagName, System.DateTime? createdAt = null, System.DateTime? releasedAt = null) -> NGitLab.Mock.Config.GitLabProject
static NGitLab.Mock.Config.GitLabHelpers.WithSystemComment(this NGitLab.Mock.Config.GitLabIssue issue, string message = null, string innerHtml = null, int id = 0, string author = null, System.DateTime? createdAt = null, System.DateTime? updatedAt = null) -> NGitLab.Mock.Config.GitLabIssue
static NGitLab.Mock.Config.GitLabHelpers.WithSystemComment(this NGitLab.Mock.Config.GitLabMergeRequest mergeRequest, string message = null, string innerHtml = null, int id = 0, string author = null, System.DateTime? createdAt = null, System.DateTime? updatedAt = null) -> NGitLab.Mock.Config.GitLabMergeRequest
static NGitLab.Mock.Config.GitLabHelpers.WithUser(this NGitLab.Mock.Config.GitLabConfig config, string username, string name = null, string email = null, string avatarUrl = null, bool isAdmin = false, bool isDefault = false, System.Action<NGitLab.Mock.Config.GitLabUser> configure = null) -> NGitLab.Mock.Config.GitLabConfig
Expand Down
1 change: 1 addition & 0 deletions NGitLab/Impl/ReleaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public GitLabCollectionResponse<ReleaseInfo> GetAsync(ReleaseQuery query = null)
url = Utils.AddParameter(url, "sort", query.Sort);
url = Utils.AddParameter(url, "include_html_description", query.IncludeHtmlDescription);
url = Utils.AddParameter(url, "per_page", query.PerPage);
url = Utils.AddParameter(url, "page", query.Page);
}

return _api.Get().GetAllAsync<ReleaseInfo>(url);
Expand Down
17 changes: 17 additions & 0 deletions NGitLab/Models/ReleaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@
{
public class ReleaseQuery
{
/// <summary>
/// The field to use as order. Either released_at (default) or created_at.
/// </summary>
public string OrderBy { get; set; }

/// <summary>
/// The direction of the order. Either desc (default) for descending order or asc for ascending order.
/// </summary>
public string Sort { get; set; }

/// <summary>
/// If true, a response includes HTML rendered Markdown of the release description.
/// </summary>
public bool? IncludeHtmlDescription { get; set; }

/// <summary>
/// Specifies how many records per page (GitLab supports a maximum of 100 items per page and defaults to 20).
/// </summary>
public int? PerPage { get; set; }

/// <summary>
/// Specifies the start page.
/// </summary>
public int? Page { get; set; }
}
}
2 changes: 2 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,8 @@ NGitLab.Models.ReleaseQuery.IncludeHtmlDescription.get -> bool?
NGitLab.Models.ReleaseQuery.IncludeHtmlDescription.set -> void
NGitLab.Models.ReleaseQuery.OrderBy.get -> string
NGitLab.Models.ReleaseQuery.OrderBy.set -> void
NGitLab.Models.ReleaseQuery.Page.get -> int?
NGitLab.Models.ReleaseQuery.Page.set -> void
NGitLab.Models.ReleaseQuery.PerPage.get -> int?
NGitLab.Models.ReleaseQuery.PerPage.set -> void
NGitLab.Models.ReleaseQuery.ReleaseQuery() -> void
Expand Down

0 comments on commit e70854e

Please sign in to comment.