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

Fixes #2143 - Support listing branch/pr per commit #2315

Merged
merged 7 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
64 changes: 64 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoryCommitsClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ namespace Octokit.Reactive
/// </remarks>
public interface IObservableRepositoryCommitsClient
{
/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
IObservable<Branch> BranchesWhereHead(long repositoryId, string sha1);

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
/// /// <param name="options">Options for changing the API response</param>
IObservable<Branch> BranchesWhereHead(long repositoryId, string sha1, ApiOptions options);

/// <summary>
/// List pull requests associated with a commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
IObservable<Branch> BranchesWhereHead(string owner, string name, string sha1);

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
/// /// <param name="options">Options for changing the API response</param>
IObservable<Branch> BranchesWhereHead(string owner, string name, string sha1, ApiOptions options);

/// <summary>
/// Compare two references in a repository
/// </summary>
Expand Down Expand Up @@ -123,5 +155,37 @@ public interface IObservableRepositoryCommitsClient
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="reference">The repository reference</param>
IObservable<string> GetSha1(long repositoryId, string reference);

/// <summary>
/// List pull requests associated with a commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
IObservable<CommitPullRequest> PullRequests(string owner, string name, string sha1);

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
IObservable<CommitPullRequest> PullRequests(long repositoryId, string sha1);

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
/// /// <param name="options">Options for changing the API response</param>
IObservable<CommitPullRequest> PullRequests(long repositoryId, string sha1, ApiOptions options);

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
/// /// <param name="options">Options for changing the API response</param>
IObservable<CommitPullRequest> PullRequests(string owner, string name, string sha1, ApiOptions options);
}
}
112 changes: 112 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoryCommitsClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,62 @@ public ObservableRepositoryCommitsClient(IGitHubClient client)
_commit = client.Repository.Commit;
}

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
[ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/branches-where-head")]
public IObservable<Branch> BranchesWhereHead(long repositoryId, string sha1)
{
return BranchesWhereHead(repositoryId, sha1, ApiOptions.None);
}

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
/// /// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/branches-where-head")]
public IObservable<Branch> BranchesWhereHead(long repositoryId, string sha1, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1));
Ensure.ArgumentNotNull(options, nameof(options));

return _connection.GetAndFlattenAllPages<Branch>(ApiUrls.RepositoryCommitsBranchesWhereHead(repositoryId, sha1), null, AcceptHeaders.ListBranchOrPullForCommitPreview, options);
}

/// <summary>
/// List pull requests associated with a commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
[ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head")]
public IObservable<Branch> BranchesWhereHead(string owner, string name, string sha1)
{
return BranchesWhereHead(owner, name, sha1, ApiOptions.None);
}

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch</param>
/// /// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head")]
public IObservable<Branch> BranchesWhereHead(string owner, string name, string sha1, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1));
Ensure.ArgumentNotNull(options, nameof(options));

return _connection.GetAndFlattenAllPages<Branch>(ApiUrls.RepositoryCommitsBranchesWhereHead(owner, name, sha1), null, AcceptHeaders.ListBranchOrPullForCommitPreview, options);
}

/// <summary>
/// Compare two references in a repository
/// </summary>
Expand Down Expand Up @@ -214,5 +270,61 @@ public IObservable<string> GetSha1(long repositoryId, string reference)

return _commit.GetSha1(repositoryId, reference).ToObservable();
}

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
[ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/pulls")]
public IObservable<CommitPullRequest> PullRequests(long repositoryId, string sha1)
{
return PullRequests(repositoryId, sha1, ApiOptions.None);
}

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
/// /// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/pulls")]
public IObservable<CommitPullRequest> PullRequests(long repositoryId, string sha1, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1));
Ensure.ArgumentNotNull(options, nameof(options));

return _connection.GetAndFlattenAllPages<CommitPullRequest>(ApiUrls.RepositoryCommitsPull(repositoryId, sha1), null, AcceptHeaders.ListBranchOrPullForCommitPreview, options);
}

/// <summary>
/// List pull requests associated with a commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
[ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/pulls")]
public IObservable<CommitPullRequest> PullRequests(string owner, string name, string sha1)
{
return PullRequests(owner, name, sha1, ApiOptions.None);
}

/// <summary>
/// Gets all pull requests for a given commit
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="sha1">Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history</param>
/// /// <param name="options">Options for changing the API response</param>
[ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/pulls")]
public IObservable<CommitPullRequest> PullRequests(string owner, string name, string sha1, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1));
Ensure.ArgumentNotNull(options, nameof(options));

return _connection.GetAndFlattenAllPages<CommitPullRequest>(ApiUrls.RepositoryCommitsPull(owner, name, sha1), null, AcceptHeaders.ListBranchOrPullForCommitPreview, options);
}
}
}
58 changes: 58 additions & 0 deletions Octokit.Tests/Clients/RespositoryCommitsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,63 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync<ArgumentException>(() => client.GetSha1(1, ""));
}
}

public class ThePullRequestsMethod
{
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryCommitsClient(connection);
var options = new ApiOptions
{
PageCount = 1,
StartPage = 1,
PageSize = 1
};

await client.PullRequests("fake", "repo", "ref", options);

connection.Received().GetAll<CommitPullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/commits/ref/pulls"),
null, "application/vnd.github.groot-preview+json", options);
}

[Fact]
public async Task RequestsCorrectUrlWithRepositoryId()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryCommitsClient(connection);
var options = new ApiOptions
{
PageCount = 1,
StartPage = 1,
PageSize = 1
};

await client.PullRequests(1, "ref", options);

connection.Received().GetAll<CommitPullRequest>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/commits/ref/pulls"),
null, "application/vnd.github.groot-preview+json", options);
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryCommitsClient(connection);

await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests(null, "name", "ref"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests("owner", null, "ref"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests("owner", "name", null));

await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests(1, null));

await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests("", "name", "ref"));
await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests("owner", "", "ref"));
await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests("owner", "name", ""));

await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests(1, ""));
}
}
}
}
68 changes: 68 additions & 0 deletions Octokit.Tests/Reactive/ObservableRepositoryCommitsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,73 @@ public async Task GetsCorrectUrl()
.GetAll(1);
}
}

public class ThePullRequestsMethod
{
[Fact]
public async Task EnsuresNonEmptyArguments()
{
var client = new ObservableRepositoryCommitsClient(Substitute.For<IGitHubClient>());

await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests("", "name", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests("owner", "", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentException>(() => client.PullRequests("owner", "name", "").ToTask());
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new ObservableRepositoryCommitsClient(Substitute.For<IGitHubClient>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests(null, "name", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests("owner", null, "reference").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.PullRequests("owner", "name", null).ToTask());
}

[Fact]
public void GetsCorrectUrl()
{
var githubClient = Substitute.For<IGitHubClient>();
var client = new ObservableRepositoryCommitsClient(githubClient);
var options = new ApiOptions();

client.PullRequests("fake", "repo", "reference", options);
githubClient.Received().Repository.Commit.PullRequests("fake", "repo", "reference", options);
}
}

public class TheBranchesWhereHeadMethod
{
[Fact]
public async Task EnsuresNonEmptyArguments()
{
var client = new ObservableRepositoryCommitsClient(Substitute.For<IGitHubClient>());

await Assert.ThrowsAsync<ArgumentException>(() => client.BranchesWhereHead("", "name", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentException>(() => client.BranchesWhereHead("owner", "", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentException>(() => client.BranchesWhereHead("owner", "name", "").ToTask());
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new ObservableRepositoryCommitsClient(Substitute.For<IGitHubClient>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.BranchesWhereHead(null, "name", "reference").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.BranchesWhereHead("owner", null, "reference").ToTask());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.BranchesWhereHead("owner", "name", null).ToTask());
}

[Fact]
public void GetsCorrectUrl()
{
var githubClient = Substitute.For<IGitHubClient>();
var client = new ObservableRepositoryCommitsClient(githubClient);
var options = new ApiOptions();

client.BranchesWhereHead("fake", "repo", "reference", options);
githubClient.Received().Repository.Commit.BranchesWhereHead("fake", "repo", "reference", options);
}
}
}
}
Loading