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 pagination support to repository invitation. #1692

Merged
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
20 changes: 20 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoryInvitationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public interface IObservableRepositoryInvitationsClient
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<RepositoryInvitation> GetAllForCurrent();

/// <summary>
/// Gets all invitations for the current user.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/invitations/#list-a-users-repository-invitations">API documentation</a> for more information.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<RepositoryInvitation> GetAllForCurrent(ApiOptions options);

/// <summary>
/// Gets all the invitations on a repository.
/// </summary>
Expand All @@ -52,6 +62,16 @@ public interface IObservableRepositoryInvitationsClient
/// <param name="repositoryId">The id of the repository</param>
IObservable<RepositoryInvitation> GetAllForRepository(long repositoryId);

/// <summary>
/// Gets all the invitations on a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/invitations/#list-invitations-for-a-repository">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The id of the repository</param>
/// /// <param name="options">Options for changing the API response</param>
IObservable<RepositoryInvitation> GetAllForRepository(long repositoryId, ApiOptions options);

/// <summary>
/// Updates a repository invitation.
/// </summary>
Expand Down
31 changes: 29 additions & 2 deletions Octokit.Reactive/Clients/ObservableRepositoryInvitationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public IObservable<RepositoryInvitation> Edit(long repositoryId, int invitationI
/// </remarks>
public IObservable<RepositoryInvitation> GetAllForCurrent()
{
return _connection.GetAndFlattenAllPages<RepositoryInvitation>(ApiUrls.UserInvitations(), null, AcceptHeaders.InvitationsApiPreview, ApiOptions.None);
return GetAllForCurrent(ApiOptions.None);
}

/// <summary>
/// Gets all invitations for the current user.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/invitations/#list-a-users-repository-invitations">API documentation</a> for more information.
/// </remarks>
/// <param name="options">Options for changing the API response</param>
public IObservable<RepositoryInvitation> GetAllForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages<RepositoryInvitation>(ApiUrls.UserInvitations(), null, AcceptHeaders.InvitationsApiPreview, options);
}

/// <summary>
Expand All @@ -92,7 +105,21 @@ public IObservable<RepositoryInvitation> GetAllForCurrent()
/// <param name="repositoryId">The id of the repository</param>
public IObservable<RepositoryInvitation> GetAllForRepository(long repositoryId)
{
return _connection.GetAndFlattenAllPages<RepositoryInvitation>(ApiUrls.RepositoryInvitations(repositoryId), null, AcceptHeaders.InvitationsApiPreview, ApiOptions.None);
return GetAllForRepository(repositoryId, ApiOptions.None);
}

/// <summary>
/// Gets all the invitations on a repository.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/invitations/#list-invitations-for-a-repository">API documentation</a> for more information.
/// </remarks>
/// <param name="repositoryId">The id of the repository</param>
/// <param name="options">Options for changing the API response</param>
public IObservable<RepositoryInvitation> GetAllForRepository(long repositoryId, ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");
return _connection.GetAndFlattenAllPages<RepositoryInvitation>(ApiUrls.RepositoryInvitations(repositoryId), null, AcceptHeaders.InvitationsApiPreview, options);
}
}
}
275 changes: 271 additions & 4 deletions Octokit.Tests.Integration/Clients/RepositoryInvitationsClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Octokit;
using System.Collections.Generic;
using Octokit;
using Octokit.Tests.Integration;
using Octokit.Tests.Integration.Helpers;
using System.Linq;
Expand All @@ -12,7 +13,6 @@ public class TheGetAllForRepositoryMethod
[IntegrationTest]
public async Task CanGetAllInvitations()
{
var collaborator = "octocat";
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");

Expand All @@ -22,9 +22,9 @@ public async Task CanGetAllInvitations()
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator
var response = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, collaborator, permission);
var response = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner, permission);

Assert.Equal(collaborator, response.Invitee.Login);
Assert.Equal(context.RepositoryOwner, response.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response.Permissions);

var invitations = await github.Repository.Invitation.GetAllForRepository(context.Repository.Id);
Expand All @@ -38,6 +38,115 @@ public async Task CanGetAllInvitations()
Assert.Equal(invitations[0].Repository.Id, response.Repository.Id);
}
}

[DualAccountTest]
public async Task ReturnsCorrectCountOfInvitationsWithStart()
{
var collaborator1 = Helper.CredentialsSecondUser.Login;
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");

using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator
var response1 = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, collaborator1, permission);

Assert.Equal(collaborator1, response1.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response1.Permissions);

var response2 = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner, permission);

Assert.Equal(context.RepositoryOwner, response2.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response2.Permissions);

var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};

var invitations = await github.Repository.Invitation.GetAllForRepository(context.Repository.Id, options);

Assert.Equal(1, invitations.Count);
}
}

[DualAccountTest]
public async Task ReturnsCorrectCountOfInvitationsWithoutStart()
{
var collaborator = Helper.CredentialsSecondUser.Login;
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");

using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator
var response = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, collaborator, permission);

Assert.Equal(collaborator, response.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response.Permissions);

var options = new ApiOptions
{
PageSize = 1,
PageCount = 1,
};

var invitations = await github.Repository.Invitation.GetAllForRepository(context.Repository.Id, options);

Assert.Equal(1, invitations.Count);
}
}

[IntegrationTest]
public async Task ReturnsDistinctInvitationsBasedOnStart()
{
var collaborator1 = Helper.CredentialsSecondUser.Login;
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("public-repo");

using (var context = await github.CreateRepositoryContext(new NewRepository(repoName)))
{
var fixture = github.Repository.Collaborator;
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator
var response1 = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, collaborator1, permission);

Assert.Equal(collaborator1, response1.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response1.Permissions);

var response2 = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner, permission);

Assert.Equal(context.RepositoryOwner, response2.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response2.Permissions);

var startOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1
};

var skipStartOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};

var firstInvitations = await github.Repository.Invitation.GetAllForRepository(context.Repository.Id, startOptions);
var secondInvitations = await github.Repository.Invitation.GetAllForRepository(context.Repository.Id, skipStartOptions);

Assert.NotEqual(firstInvitations[0].Invitee.Login, secondInvitations[0].Invitee.Login);
}
}
}

public class TheGetAllForCurrentMethod
Expand Down Expand Up @@ -70,6 +179,164 @@ public async Task CanGetAllInvitations()
Assert.NotNull(invitations.FirstOrDefault(i => i.Repository.Id == response.Repository.Id));
}
}

[IntegrationTest]
public async Task ReturnsCorrectCountOfInvitationsWithStart()
{
var github = Helper.GetAuthenticatedClient();
var repoNames = Enumerable.Range(0, 2).Select(i => Helper.MakeNameWithTimestamp($"public-repo{i}")).ToList();

var contexts = new List<RepositoryContext>();
try
{
foreach (var repoName in repoNames)
{
contexts.Add(await github.CreateRepositoryContext(new NewRepository(repoName)));
}
var fixture = github.Repository.Collaborator;
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator to all repos
foreach (var context in contexts)
{
var response = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner, permission);

Assert.Equal(context.RepositoryOwner, response.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response.Permissions);
}

var startOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};


var invitations = await github.Repository.Invitation.GetAllForCurrent(startOptions);
Assert.Equal(1, invitations.Count);
}
finally
{
if (contexts != null)
{
foreach (var context in contexts)
{
context?.Dispose();
}
}
}
}

[IntegrationTest]
public async Task ReturnsCorrectCountOfInvitationsWithoutStart()
{
var github = Helper.GetAuthenticatedClient();
var repoNames = Enumerable.Range(0, 2).Select(i => Helper.MakeNameWithTimestamp($"public-repo{i}")).ToList();

var contexts = new List<RepositoryContext>();
try
{
foreach (var repoName in repoNames)
{
contexts.Add(await github.CreateRepositoryContext(new NewRepository(repoName)));
}
var fixture = github.Repository.Collaborator;
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator to all repos
foreach (var context in contexts)
{
var response = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner, permission);

Assert.Equal(context.RepositoryOwner, response.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response.Permissions);
}

var startOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1
};


var invitations = await github.Repository.Invitation.GetAllForCurrent(startOptions);
Assert.Equal(1, invitations.Count);
}
finally
{
if (contexts != null)
{
foreach (var context in contexts)
{
context?.Dispose();
}
}
}
}

[IntegrationTest]
public async Task ReturnsDistinctInvitationsBasedOnStart()
{
var github = Helper.GetAuthenticatedClient();
var repoNames = Enumerable.Range(0, 2).Select(i => Helper.MakeNameWithTimestamp($"public-repo{i}")).ToList();

var contexts = new List<RepositoryContext>();
try
{
foreach (var repoName in repoNames)
{
contexts.Add(await github.CreateRepositoryContext(new NewRepository(repoName)));
}
var fixture = github.Repository.Collaborator;
var permission = new CollaboratorRequest(Permission.Push);

// invite a collaborator to all repos
foreach (var context in contexts)
{
var response = await fixture.Invite(context.RepositoryOwner, context.RepositoryName, context.RepositoryOwner, permission);

Assert.Equal(context.RepositoryOwner, response.Invitee.Login);
Assert.Equal(InvitationPermissionType.Write, response.Permissions);
}

var startOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1
};

var skipStartOptions = new ApiOptions
{
PageSize = 1,
PageCount = 1,
StartPage = 2
};

var firstInvitations = await github.Repository.Invitation.GetAllForCurrent(startOptions);
var secondInvitations = await github.Repository.Invitation.GetAllForCurrent(skipStartOptions);

var invitations = firstInvitations.Concat(secondInvitations).ToArray();
var invitationsLength = invitations.Length;
for (int i = 0; i < invitationsLength; i++)
{
for (int j = i+1; j < invitationsLength; j++)
{
Assert.NotEqual(invitations[i].Repository.FullName, invitations[j].Repository.FullName);
}
}
}
finally
{
if(contexts != null)
{
foreach (var context in contexts)
{
context?.Dispose();
}
}
}
}
}

public class TheAcceptMethod
Expand Down
Loading