[FEAT] Allow users to fetch all commits for two commits comparision (#2690)

This commit is contained in:
Roman
2023-04-14 22:41:24 +04:00
committed by GitHub
parent aed70e335a
commit 7b1fa04b38
6 changed files with 188 additions and 2 deletions
@@ -62,6 +62,27 @@ namespace Octokit.Reactive
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "base")]
IObservable<CompareResult> Compare(long repositoryId, string @base, string head);
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "base")]
IObservable<CompareResult> Compare(string owner, string name, string @base, string head, ApiOptions options);
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "base")]
IObservable<CompareResult> Compare(long repositoryId, string @base, string head, ApiOptions options);
/// <summary>
/// Gets all commits for a given repository
/// </summary>
@@ -110,6 +110,41 @@ namespace Octokit.Reactive
return _commit.Compare(repositoryId, @base, head).ToObservable();
}
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
public IObservable<CompareResult> Compare(string owner, string name, string @base, string head, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(@base, "base");
Ensure.ArgumentNotNullOrEmptyString(head, nameof(head));
Ensure.ArgumentNotNull(options, nameof(options));
return _commit.Compare(owner, name, @base, head, options).ToObservable();
}
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
public IObservable<CompareResult> Compare(long repositoryId, string @base, string head, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(@base, "base");
Ensure.ArgumentNotNullOrEmptyString(head, nameof(head));
Ensure.ArgumentNotNull(options, nameof(options));
return _commit.Compare(repositoryId, @base, head, options).ToObservable();
}
/// <summary>
/// Gets all commits for a given repository
/// </summary>
@@ -297,6 +297,28 @@ public class RepositoryCommitsClientTests
Assert.NotNull(sha1);
}
[IntegrationTest]
public async Task CanFetchAllCommitsInComparision()
{
const string @base = "8dcb1db0da7c86596bf1d63631bd335363c64b8c";
const string head = "7349ecd6685c370ba84eb13f4c39f75f33";
var compareResultWithoutOptions = await _fixture.Compare(octokitNetRepositoryId, @base, head);
Assert.Equal(250, compareResultWithoutOptions.Commits.Count);
var compareResult = await _fixture.Compare(octokitNetRepositoryId, @base, head, new ApiOptions { PageSize = 100 });
Assert.Equal(534, compareResult.Commits.Count);
}
[IntegrationTest]
public async Task CanCompareTheSameCommitWithApiOptions()
{
const string head = "7349ecd6685c370ba84eb13f4c39f75f33";
var compareResult = await _fixture.Compare(octokitNetRepositoryId, head, head, new ApiOptions { PageSize = 100 });
Assert.Equal(0, compareResult.Commits.Count);
}
}
public class TestsWithNewRepository : IDisposable
@@ -503,4 +525,4 @@ public class RepositoryCommitsClientTests
_context.Dispose();
}
}
}
}
@@ -1186,6 +1186,23 @@ namespace Octokit.Tests.Clients
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", "base", null));
await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "repo", "base", ""));
var options = new ApiOptions();
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare(null, "repo", "base", "head", options));
await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("", "repo", "base", "head", options));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", null, "base", "head", options));
await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "", "base", "head", options));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", null, "head", options));
await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "repo", "", "head", options));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", "base", null, options));
await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "repo", "base", "", options));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", "base", "head", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", "base", "head", null));
}
[Fact]
@@ -63,6 +63,27 @@ namespace Octokit
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "base")]
Task<CompareResult> Compare(long repositoryId, string @base, string head);
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "base")]
Task<CompareResult> Compare(string owner, string name, string @base, string head, ApiOptions options);
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "base")]
Task<CompareResult> Compare(long repositoryId, string @base, string head, ApiOptions options);
/// <summary>
/// Gets a single commit for a given repository
/// </summary>
+71 -1
View File
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Octokit
@@ -105,6 +107,74 @@ namespace Octokit
return ApiConnection.Get<CompareResult>(ApiUrls.RepoCompare(repositoryId, @base, head));
}
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/repos/{owner}/{repo}/compare/{base}...{head}")]
public Task<CompareResult> Compare(string owner, string name, string @base, string head, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(@base, nameof(@base));
Ensure.ArgumentNotNullOrEmptyString(head, nameof(head));
Ensure.ArgumentNotNull(options, nameof(options));
return Compare(ApiUrls.RepoCompare(owner, name, @base, head), options);
}
/// <summary>
/// Compare two references in a repository
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="base">The reference to use as the base commit</param>
/// <param name="head">The reference to use as the head commit</param>
/// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/repositories/{id}/compare/{base}...{head}")]
public Task<CompareResult> Compare(long repositoryId, string @base, string head, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(@base, nameof(@base));
Ensure.ArgumentNotNullOrEmptyString(head, nameof(head));
Ensure.ArgumentNotNull(options, nameof(options));
return Compare(ApiUrls.RepoCompare(repositoryId, @base, head), options);
}
private async Task<CompareResult> Compare(Uri uri, ApiOptions options)
{
var results = await ApiConnection.GetAll<CompareResult>(uri, options);
if (results.Count == 1) return results[0];
var firstCompareResult = results[0];
var commits = firstCompareResult.Commits.ToList();
var files = firstCompareResult.Files.ToList();
foreach (var compareResult in results.Skip(1))
{
commits.AddRange(compareResult.Commits ?? Array.Empty<GitHubCommit>());
files.AddRange(compareResult.Files ?? Array.Empty<GitHubCommitFile>());
}
return new CompareResult(
firstCompareResult.Url,
firstCompareResult.HtmlUrl,
firstCompareResult.PermalinkUrl,
firstCompareResult.DiffUrl,
firstCompareResult.PatchUrl,
firstCompareResult.BaseCommit,
firstCompareResult.MergeBaseCommit,
firstCompareResult.Status,
firstCompareResult.AheadBy,
firstCompareResult.BehindBy,
firstCompareResult.TotalCommits,
commits,
files);
}
/// <summary>
/// Gets a single commit for a given repository
/// </summary>