Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Page property to control pagination. Add asynchronous support for GetCommits, GetCommit, and GetCommitDiff #754

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NGitLab.Mock/Clients/CommitClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using NGitLab.Mock.Internals;
using NGitLab.Models;

Expand Down Expand Up @@ -37,6 +39,12 @@ public Commit GetCommit(string @ref)
}
}

public async Task<Commit> GetCommitAsync(string @ref, CancellationToken cancellationToken = default)
{
await Task.Yield();
return GetCommit(@ref);
}

public Commit CherryPick(CommitCherryPick cherryPick)
{
using (Context.BeginOperationScope())
Expand Down
27 changes: 27 additions & 0 deletions NGitLab.Mock/Clients/RepositoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using NGitLab.Mock.Internals;
using NGitLab.Models;

Expand Down Expand Up @@ -107,16 +110,40 @@ public IEnumerable<Commit> GetCommits(GetCommitsRequest request)
}
}

public async IAsyncEnumerable<Commit> GetCommitsAsync(GetCommitsRequest request, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await Task.Yield();

using (Context.BeginOperationScope())
{
var project = GetProject(_projectId, ProjectPermission.View);
foreach (var commit in project.Repository.GetCommits(request))
{
yield return ConvertToNGitLabCommit(commit, project);
}
}
}

public Commit GetCommit(Sha1 sha)
{
throw new NotImplementedException();
}

public Task<Commit> GetCommitAsync(Sha1 sha, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public IEnumerable<Diff> GetCommitDiff(Sha1 sha)
{
throw new NotImplementedException();
}

public IAsyncEnumerable<Diff> GetCommitDiffAsync(Sha1 sha, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public IEnumerable<Ref> GetCommitRefs(Sha1 sha, CommitRefType type = CommitRefType.All)
{
throw new NotImplementedException();
Expand Down
40 changes: 40 additions & 0 deletions NGitLab.Tests/CommitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public async Task Test_can_get_commit()
Assert.That(commit.ShortId, Is.Not.Null);
}

[Test]
[NGitLabRetry]
public async Task Test_can_get_commit_async()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject(initializeWithCommits: true);

var commit = await context.Client.GetCommits(project.Id).GetCommitAsync(project.DefaultBranch);
Assert.That(commit.Message, Is.Not.Null);
Assert.That(commit.ShortId, Is.Not.Null);
}

[Test]
[NGitLabRetry]
public async Task Test_can_get_stats_in_commit()
Expand Down Expand Up @@ -51,6 +63,34 @@ public async Task Test_can_get_stats_in_commit()
Assert.That(commit.Stats.Total, Is.EqualTo(5));
}

[Test]
[NGitLabRetry]
public async Task Test_can_get_stats_in_commit_async()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject(initializeWithCommits: true);
context.Client.GetRepository(project.Id).Files.Create(new FileUpsert
{
Branch = project.DefaultBranch,
CommitMessage = "file to be updated",
Path = "CommitStats.txt",
RawContent = "I'm defective and i need to be fixeddddddddddddddd",
});

context.Client.GetRepository(project.Id).Files.Update(new FileUpsert
{
Branch = project.DefaultBranch,
CommitMessage = "fixing the file",
Path = "CommitStats.txt",
RawContent = "I'm no longer defective and i have been fixed\n\n\r\n\r\rEnjoy",
});

var commit = await context.Client.GetCommits(project.Id).GetCommitAsync(project.DefaultBranch);
Assert.That(commit.Stats.Additions, Is.EqualTo(4));
Assert.That(commit.Stats.Deletions, Is.EqualTo(1));
Assert.That(commit.Stats.Total, Is.EqualTo(5));
}

[Test]
[NGitLabRetry]
public async Task Test_can_get_merge_request_associated_to_commit()
Expand Down
Loading