Merge pull request #795 from octokit/org-teams

adding missing organization teams APIs
This commit is contained in:
Phil Haack
2015-06-11 19:28:24 -07:00
14 changed files with 528 additions and 138 deletions
@@ -31,6 +31,14 @@ namespace Octokit.Reactive
/// <returns>A list of the orgs's teams <see cref="Team"/>s.</returns>
IObservable<Team> GetAll(string org);
/// <summary>
/// Returns all <see cref="Team" />s for the current user.
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of the user's <see cref="Team"/>s.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<Team> GetAllForCurrent();
/// <summary>
/// Returns all members of the given team.
/// </summary>
@@ -63,6 +71,29 @@ namespace Octokit.Reactive
/// <returns></returns>
IObservable<Unit> Delete(int id);
/// <summary>
/// Adds a <see cref="User"/> to a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#add-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to add to the team.</param>
/// <exception cref="ApiValidationException">Thrown if you attempt to add an organization to a team.</exception>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
IObservable<TeamMembership> AddMembership(int id, string login);
/// <summary>
/// Removes a <see cref="User"/> from a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#remove-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to remove from the team.</param>
/// <returns><see langword="true"/> if the user was removed from the team; <see langword="false"/> otherwise.</returns>
IObservable<bool> RemoveMembership(int id, string login);
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
@@ -70,8 +101,18 @@ namespace Octokit.Reactive
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns><see langword="true"/> if the user is a member of the team; <see langword="false"/> otherwise.</returns>
[Obsolete("Use GetMembership(id, login) to detect pending memberships")]
IObservable<bool> IsMember(int id, string login);
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
/// </summary>
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
IObservable<TeamMembership> GetMembership(int id, string login);
/// <summary>
/// Returns all team's repositories.
/// </summary>
@@ -79,32 +120,36 @@ namespace Octokit.Reactive
/// <returns>The team's repositories</returns>
IObservable<Repository> GetAllRepositories(int id);
/// <summary>
/// Add a member to the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
IObservable<Unit> AddMember(int id, string login);
/// <summary>
/// Remove a member from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
IObservable<Unit> RemoveMember(int id, string login);
/// <summary>
/// Add a repository to the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
IObservable<Unit> AddRepository(int id, string organization, string repoName);
/// <summary>
/// Remove a repository from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
IObservable<Unit> RemoveRepository(int id, string organization, string repoName);
IObservable<bool> RemoveRepository(int id, string organization, string repoName);
/// <summary>
/// Adds a <see cref="Repository"/> to a <see cref="Team"/>.
/// </summary>
/// <param name="id">The team identifier.</param>
/// <param name="organization">Org to associate the repo with.</param>
/// <param name="repoName">Name of the repo.</param>
/// <exception cref="ApiValidationException">Thrown if you attempt to add a repository to a team that is not owned by the organization.</exception>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#add-team-repo">API documentation</a> for more information.
/// </remarks>
/// <returns><see langword="true"/> if the repository was added to the team; <see langword="false"/> otherwise.</returns>
IObservable<bool> AddRepository(int id, string organization, string repoName);
/// <summary>
/// Gets whether or not the given repository is managed by the given team.
/// </summary>
/// <param name="id">The team identifier</param>
/// <param name="owner">Owner of the org the team is associated with.</param>
/// <param name="repo">Name of the repo.</param>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#get-team-repo">API documentation</a> for more information.
/// </remarks>
/// <returns><see langword="true"/> if the repository is managed by the given team; <see langword="false"/> otherwise.</returns>
IObservable<bool> IsRepositoryManagedByTeam(int id, string owner, string repo);
}
}
@@ -51,6 +51,16 @@ namespace Octokit.Reactive
return _connection.GetAndFlattenAllPages<Team>(ApiUrls.OrganizationTeams(org));
}
/// <summary>
/// Returns all <see cref="Team" />s for the current user.
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of the user's <see cref="Team"/>s.</returns>
public IObservable<Team> GetAllForCurrent()
{
return _connection.GetAndFlattenAllPages<Team>(ApiUrls.UserTeams());
}
/// <summary>
/// Returns all members of the given team.
/// </summary>
@@ -95,6 +105,35 @@ namespace Octokit.Reactive
return _client.Delete(id).ToObservable();
}
/// <summary>
/// Adds a <see cref="User"/> to a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#add-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to add to the team.</param>
/// <exception cref="ApiValidationException">Thrown if you attempt to add an organization to a team.</exception>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
public IObservable<TeamMembership> AddMembership(int id, string login)
{
return _client.AddMembership(id, login).ToObservable();
}
/// <summary>
/// Removes a <see cref="User"/> from a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#remove-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to remove from the team.</param>
/// <returns><see langword="true"/> if the user was removed from the team; <see langword="false"/> otherwise.</returns>
public IObservable<bool> RemoveMembership(int id, string login)
{
return _client.RemoveMembership(id, login).ToObservable();
}
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
@@ -102,11 +141,24 @@ namespace Octokit.Reactive
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns><see langword="true"/> if the user is a member of the team; <see langword="false"/> otherwise.</returns>
[Obsolete("Use GetMembership(id, login) to detect pending memberships")]
public IObservable<bool> IsMember(int id, string login)
{
return _client.IsMember(id, login).ToObservable();
}
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
/// </summary>
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
public IObservable<TeamMembership> GetMembership(int id, string login)
{
return _client.GetMembership(id, login).ToObservable();
}
/// <summary>
/// Returns all team's repositories.
/// </summary>
@@ -118,43 +170,45 @@ namespace Octokit.Reactive
}
/// <summary>
/// Add a member to the team
/// Adds a <see cref="Repository"/> to a <see cref="Team"/>.
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public IObservable<Unit> AddMember(int id, string login)
{
return _client.AddMember(id, login).ToObservable();
}
/// <summary>
/// Remove a member from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public IObservable<Unit> RemoveMember(int id, string login)
{
return _client.RemoveMember(id, login).ToObservable();
}
/// <summary>
/// Add a repository to the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public IObservable<Unit> AddRepository(int id, string organization, string repoName)
/// <param name="id">The team identifier.</param>
/// <param name="organization">Org to associate the repo with.</param>
/// <param name="repoName">Name of the repo.</param>
/// <exception cref="ApiValidationException">Thrown if you attempt to add a repository to a team that is not owned by the organization.</exception>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#add-team-repo">API documentation</a> for more information.
/// </remarks>
/// <returns><see langword="true"/> if the repository was added to the team; <see langword="false"/> otherwise.</returns>
public IObservable<bool> AddRepository(int id, string organization, string repoName)
{
return _client.AddRepository(id, organization, repoName).ToObservable();
}
/// <summary>
/// Remove a repository from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public IObservable<Unit> RemoveRepository(int id, string organization, string repoName)
public IObservable<bool> RemoveRepository(int id, string organization, string repoName)
{
return _client.RemoveRepository(id, organization, repoName).ToObservable();
}
/// <summary>
/// Gets whether or not the given repository is managed by the given team.
/// </summary>
/// <param name="id">The team identifier</param>
/// <param name="owner">Owner of the org the team is associated with.</param>
/// <param name="repo">Name of the repo.</param>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#get-team-repo">API documentation</a> for more information.
/// </remarks>
/// <returns><see langword="true"/> if the repository is managed by the given team; <see langword="false"/> otherwise.</returns>
public IObservable<bool> IsRepositoryManagedByTeam(int id, string owner, string repo)
{
return _client.IsRepositoryManagedByTeam(id, owner, repo).ToObservable();
}
}
}
@@ -3,7 +3,6 @@ using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Helpers;
using Octokit.Tests.Integration;
using Xunit;
@@ -46,28 +45,29 @@ public class TeamsClientTests
}
}
public class TheIsMemberMethod
public class TheGetAllForCurrentMethod
{
[IntegrationTest]
public async Task GetsIsMemberWhenAuthenticated()
{
var github = Helper.GetAuthenticatedClient();
var teams = await github.Organization.Team.GetAllForCurrent();
Assert.NotEmpty(teams);
}
}
public class TheGetMembershipMethod
{
readonly Team team;
public TheIsMemberMethod()
public TheGetMembershipMethod()
{
var github = Helper.GetAuthenticatedClient();
team = github.Organization.Team.GetAll(Helper.Organization).Result.First();
}
[OrganizationTest(Skip="actually returning the membership information! Maybe because it's a public organization?")]
public async Task FailsWhenNotAuthenticated()
{
var github = Helper.GetAnonymousClient();
var e = await Assert.ThrowsAsync<AuthorizationException>(() => github.Organization.Team.IsMember(team.Id, Helper.UserName));
Assert.Equal(HttpStatusCode.Unauthorized, e.StatusCode);
}
[OrganizationTest(Skip = "see https://github.com/octokit/octokit.net/issues/533 for the resolution to this failing test")]
[OrganizationTest]
public async Task FailsWhenAuthenticatedWithBadCredentials()
{
var github = Helper.GetBadCredentialsClient();
@@ -81,9 +81,9 @@ public class TeamsClientTests
{
var github = Helper.GetAuthenticatedClient();
var isMember = await github.Organization.Team.IsMember(team.Id, Helper.UserName);
var membership = await github.Organization.Team.GetMembership(team.Id, Helper.UserName);
Assert.True(isMember);
Assert.Equal(TeamMembership.Active, membership);
}
[OrganizationTest]
@@ -91,9 +91,9 @@ public class TeamsClientTests
{
var github = Helper.GetAuthenticatedClient();
var isMember = await github.Organization.Team.IsMember(team.Id, "foo");
var membership = await github.Organization.Team.GetMembership(team.Id, "foo");
Assert.False(isMember);
Assert.Equal(TeamMembership.NotFound, membership);
}
}
+106 -23
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Tests.Helpers;
@@ -21,6 +22,20 @@ namespace Octokit.Tests.Clients
}
}
public class TheGetMethod
{
[Fact]
public void RequestsTheCorrectlUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
client.Get(1);
connection.Received().Get<Team>(Arg.Is<Uri>(u => u.ToString() == "teams/1"), null);
}
}
public class TheGetAllMethod
{
[Fact]
@@ -57,7 +72,7 @@ namespace Octokit.Tests.Clients
}
}
public class TheCreateTeamMethod
public class TheCreateMethod
{
[Fact]
public void RequestsTheCorrectUrl()
@@ -121,7 +136,48 @@ namespace Octokit.Tests.Clients
}
}
public class TheIsMemberMethod
public class TheAddMembershipMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
await client.AddMembership(1, "user");
connection.Received().Put<Dictionary<string, string>>(
Arg.Is<Uri>(u => u.ToString() == "teams/1/memberships/user"),
Args.Object);
}
[Fact]
public async Task EnsuresNonNullOrEmptyLogin()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.AddMembership(1, null));
await Assert.ThrowsAsync<ArgumentException>(() => client.AddMembership(1, ""));
}
}
public class TheGetAllForCurrentMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
client.GetAllForCurrent();
connection.Received().GetAll<Team>(Arg.Is<Uri>(u => u.ToString() == "user/teams"));
}
}
public class TheGetMembershipMethod
{
[Fact]
public void EnsuresNonNullLogin()
@@ -129,7 +185,7 @@ namespace Octokit.Tests.Clients
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
Assert.ThrowsAsync<ArgumentNullException>(() => client.IsMember(1, null));
Assert.ThrowsAsync<ArgumentNullException>(() => client.GetMembership(1, null));
}
[Fact]
@@ -138,44 +194,45 @@ namespace Octokit.Tests.Clients
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
Assert.ThrowsAsync<ArgumentException>(() => client.IsMember(1, ""));
Assert.ThrowsAsync<ArgumentException>(() => client.GetMembership(1, ""));
}
}
public class TheAddMemberMethod
public class TheRRemoveMembershipMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
client.RemoveMembership(1, "user");
client.AddMember(1, "user");
connection.Received().Put(Arg.Is<Uri>(u => u.ToString() == "teams/1/memberships/user"));
connection.Connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "teams/1/memberships/user"));
}
[Fact]
public async Task EnsuresNonNullOrEmptyLogin()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
await Assert.ThrowsAsync<ArgumentNullException>(() => client.RemoveMembership(1, null));
await Assert.ThrowsAsync<ArgumentException>(() => client.RemoveMembership(1, ""));
}
}
public class TheRemoveMemberMethod
public class TheGetAllRepositoriesMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
client.RemoveMember(1, "user");
client.GetAllRepositories(1);
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "teams/1/memberships/user"));
}
}
connection.Received().GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "teams/1/repos"));
public class TheGetRepositoriesMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
client.GetAllRepositories(1);
connection.Received().GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "teams/1/repos"));
@@ -191,12 +248,28 @@ namespace Octokit.Tests.Clients
var client = new TeamsClient(connection);
client.RemoveRepository(1, "org", "repo");
connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "teams/1/repos/org/repo"));
connection.Connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "teams/1/repos/org/repo"));
}
}
public class TheAddRepositoryMethod
{
[Fact]
public async Task EnsuresNonNullOrEmptyArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
// Check owner arguments.
await Assert.ThrowsAsync<ArgumentNullException>(() => client.RemoveRepository(1, null, "repoName"));
await Assert.ThrowsAsync<ArgumentException>(() => client.RemoveRepository(1, "", "repoName"));
// Check repo arguments.
await Assert.ThrowsAsync<ArgumentNullException>(() => client.RemoveRepository(1, "ownerName", null));
await Assert.ThrowsAsync<ArgumentException>(() => client.RemoveRepository(1, "ownerName", ""));
}
[Fact]
public void RequestsTheCorrectUrl()
{
@@ -204,7 +277,7 @@ namespace Octokit.Tests.Clients
var client = new TeamsClient(connection);
client.AddRepository(1, "org", "repo");
connection.Received().Put(Arg.Is<Uri>(u => u.ToString() == "teams/1/repos/org/repo"));
connection.Connection.Received().Put(Arg.Is<Uri>(u => u.ToString() == "teams/1/repos/org/repo"));
}
[Fact]
@@ -215,15 +288,25 @@ namespace Octokit.Tests.Clients
Assert.ThrowsAsync<ArgumentException>(() => client.AddRepository(1, null, "Repo Name"));
}
}
public class TheIsRepositoryManagedByTeamMethod
{
[Fact]
public void EnsureNonNullRepo()
public void EnsuresNonNullOrEmptyArguments()
{
var connection = Substitute.For<IApiConnection>();
var client = new TeamsClient(connection);
Assert.ThrowsAsync<ArgumentException>(() => client.AddRepository(1, "org name", null));
// Check owner arguments.
Assert.ThrowsAsync<ArgumentNullException>(() => client.IsRepositoryManagedByTeam(1, null, "repoName"));
Assert.ThrowsAsync<ArgumentException>(() => client.IsRepositoryManagedByTeam(1, "", "repoName"));
// Check repo arguments.
Assert.ThrowsAsync<ArgumentNullException>(() => client.IsRepositoryManagedByTeam(1, "ownerName", null));
Assert.ThrowsAsync<ArgumentException>(() => client.IsRepositoryManagedByTeam(1, "ownerName", ""));
}
}
}
+54 -14
View File
@@ -1,4 +1,5 @@
#if NET_45
using System;
#if NET_45
using System.Collections.Generic;
#endif
using System.Threading.Tasks;
@@ -33,6 +34,14 @@ namespace Octokit
/// <returns>A list of the orgs's teams <see cref="Team"/>s.</returns>
Task<IReadOnlyList<Team>> GetAll(string org);
/// <summary>
/// Returns all <see cref="Team" />s for the current user.
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of the user's <see cref="Team"/>s.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyList<Team>> GetAllForCurrent();
/// <summary>
/// Returns all members of the given team.
/// </summary>
@@ -64,6 +73,29 @@ namespace Octokit
/// <returns></returns>
Task Delete(int id);
/// <summary>
/// Adds a <see cref="User"/> to a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#add-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to add to the team.</param>
/// <exception cref="ApiValidationException">Thrown if you attempt to add an organization to a team.</exception>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
Task<TeamMembership> AddMembership(int id, string login);
/// <summary>
/// Removes a <see cref="User"/> from a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#remove-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to remove from the team.</param>
/// <returns><see langword="true"/> if the user was removed from the team; <see langword="false"/> otherwise.</returns>
Task<bool> RemoveMembership(int id, string login);
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
@@ -71,21 +103,17 @@ namespace Octokit
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns><see langword="true"/> if the user is a member of the team; <see langword="false"/> otherwise.</returns>
[Obsolete("Use GetMembership(id, login) as this will report on pending requests")]
Task<bool> IsMember(int id, string login);
/// <summary>
/// Add a member to the team
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
Task AddMember(int id, string login);
/// <summary>
/// Remove a member from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
Task RemoveMember(int id, string login);
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
Task<TeamMembership> GetMembership(int id, string login);
/// <summary>
/// Returns all team's repositories.
@@ -99,13 +127,25 @@ namespace Octokit
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
Task AddRepository(int id, string organization, string repoName);
Task<bool> AddRepository(int id, string organization, string repoName);
/// <summary>
/// Remove a repository from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
Task RemoveRepository(int id, string organization, string repoName);
Task<bool> RemoveRepository(int id, string organization, string repoName);
/// <summary>
/// Gets whether or not the given repository is managed by the given team.
/// </summary>
/// <param name="id">The team identifier</param>
/// <param name="owner">Owner of the org the team is associated with.</param>
/// <param name="repo">Name of the repo.</param>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#get-team-repo">API documentation</a> for more information.
/// </remarks>
/// <returns><see langword="true"/> if the repository is managed by the given team; <see langword="false"/> otherwise.</returns>
Task<bool> IsRepositoryManagedByTeam(int id, string owner, string repo);
}
}
+175 -33
View File
@@ -1,12 +1,12 @@
#if NET_45
using System;
using System.Net;
#if NET_45
using System.Collections.Generic;
#endif
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
namespace Octokit
{
/// <summary>
/// A client for GitHub's Organization Teams API.
/// </summary>
@@ -52,6 +52,17 @@ namespace Octokit
return ApiConnection.GetAll<Team>(endpoint);
}
/// <summary>
/// Returns all <see cref="Team" />s for the current user.
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns>A list of the user's <see cref="Team"/>s.</returns>
public Task<IReadOnlyList<Team>> GetAllForCurrent()
{
var endpoint = ApiUrls.UserTeams();
return ApiConnection.GetAll<Team>(endpoint);
}
/// <summary>
/// Returns all members of the given team.
/// </summary>
@@ -67,6 +78,33 @@ namespace Octokit
return ApiConnection.GetAll<User>(endpoint);
}
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
/// </summary>
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
public async Task<TeamMembership> GetMembership(int id, string login)
{
var endpoint = ApiUrls.TeamMember(id, login);
Dictionary<string, string> response;
try
{
response = await ApiConnection.Get<Dictionary<string, string>>(endpoint);
}
catch (NotFoundException)
{
return TeamMembership.NotFound;
}
return response["state"] == "active"
? TeamMembership.Active
: TeamMembership.Pending;
}
/// <summary>
/// Returns newly created <see cref="Team" /> for the current org.
/// </summary>
@@ -102,9 +140,74 @@ namespace Octokit
public Task Delete(int id)
{
var endpoint = ApiUrls.Teams(id);
return ApiConnection.Delete(endpoint);
}
/// <summary>
/// Adds a <see cref="User"/> to a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#add-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to add to the team.</param>
/// <exception cref="ApiValidationException">Thrown if you attempt to add an organization to a team.</exception>
/// <returns>A <see cref="TeamMembership"/> result indicating the membership status</returns>
public async Task<TeamMembership> AddMembership(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
var endpoint = ApiUrls.TeamMember(id, login);
Dictionary<string, string> response;
try
{
response = await ApiConnection.Put<Dictionary<string, string>>(endpoint, null);
}
catch (NotFoundException)
{
return TeamMembership.NotFound;
}
if (response == null || !response.ContainsKey("state"))
{
return TeamMembership.NotFound;
}
return response["state"] == "active"
? TeamMembership.Active
: TeamMembership.Pending;
}
/// <summary>
/// Removes a <see cref="User"/> from a <see cref="Team"/>.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#remove-team-member">API documentation</a> for more information.
/// </remarks>
/// <param name="id">The team identifier.</param>
/// <param name="login">The user to remove from the team.</param>
/// <returns><see langword="true"/> if the user was removed from the team; <see langword="false"/> otherwise.</returns>
public async Task<bool> RemoveMembership(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
var endpoint = ApiUrls.TeamMember(id, login);
try
{
var httpStatusCode = await ApiConnection.Connection.Delete(endpoint);
return httpStatusCode == HttpStatusCode.NoContent;
}
catch (NotFoundException)
{
return false;
}
}
/// <summary>
/// Gets whether the user with the given <paramref name="login"/>
/// is a member of the team with the given <paramref name="id"/>.
@@ -112,6 +215,7 @@ namespace Octokit
/// <param name="id">The team to check.</param>
/// <param name="login">The user to check.</param>
/// <returns><see langword="true"/> if the user is a member of the team; <see langword="false"/> otherwise.</returns>
[Obsolete("Use GetMembership(id, login) as this will report on pending requests")]
public async Task<bool> IsMember(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
@@ -121,7 +225,7 @@ namespace Octokit
try
{
var response = await ApiConnection.Connection.GetResponse<string>(endpoint);
return response.HttpResponse.StatusCode == System.Net.HttpStatusCode.NoContent;
return response.HttpResponse.StatusCode == HttpStatusCode.NoContent;
}
catch (NotFoundException)
{
@@ -129,30 +233,6 @@ namespace Octokit
}
}
/// <summary>
/// Add a member to the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public Task AddMember(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
var endpoint = ApiUrls.TeamMember(id, login);
return ApiConnection.Put(endpoint);
}
/// <summary>
/// Remove a member from the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public Task RemoveMember(int id, string login)
{
Ensure.ArgumentNotNullOrEmptyString(login, "login");
return ApiConnection.Delete(ApiUrls.TeamMember(id, login));
}
/// <summary>
/// Returns all team's repositories.
/// </summary>
@@ -165,18 +245,42 @@ namespace Octokit
return ApiConnection.GetAll<Repository>(endpoint);
}
/// <summary>
/// Returns all <see cref="Repository"/>(ies) associated with the given team.
/// </summary>
/// <param name="id">The team identifier</param>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#list-team-repos">API documentation</a> for more information.
/// </remarks>
/// <returns>A list of the team's <see cref="Repository"/>(ies).</returns>
public Task<IReadOnlyList<Repository>> GetRepositories(int id)
{
var endpoint = ApiUrls.TeamRepositories(id);
return ApiConnection.GetAll<Repository>(endpoint);
}
/// <summary>
/// Add a repository to the team
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public Task AddRepository(int id, string organization, string repoName)
public async Task<bool> AddRepository(int id, string organization, string repoName)
{
Ensure.ArgumentNotNullOrEmptyString(organization, "organization");
Ensure.ArgumentNotNullOrEmptyString(repoName, "repoName");
var endpoint = ApiUrls.TeamRepository(id, organization, repoName);
return ApiConnection.Put(endpoint);
try
{
var httpStatusCode = await ApiConnection.Connection.Put(endpoint);
return httpStatusCode == HttpStatusCode.NoContent;
}
catch (NotFoundException)
{
return false;
}
}
/// <summary>
@@ -184,13 +288,51 @@ namespace Octokit
/// </summary>
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
/// <returns></returns>
public Task RemoveRepository(int id, string organization, string repoName)
public async Task<bool> RemoveRepository(int id, string organization, string repoName)
{
Ensure.ArgumentNotNullOrEmptyString(organization, "organization");
Ensure.ArgumentNotNullOrEmptyString(organization, "owner");
Ensure.ArgumentNotNullOrEmptyString(repoName, "repoName");
var endpoint = ApiUrls.TeamRepository(id, organization, repoName);
return ApiConnection.Delete(endpoint);
try
{
var httpStatusCode = await ApiConnection.Connection.Delete(endpoint);
return httpStatusCode == HttpStatusCode.NoContent;
}
catch (NotFoundException)
{
return false;
}
}
/// <summary>
/// Gets whether or not the given repository is managed by the given team.
/// </summary>
/// <param name="id">The team identifier</param>
/// <param name="owner">Owner of the org the team is associated with.</param>
/// <param name="repo">Name of the repo.</param>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/orgs/teams/#get-team-repo">API documentation</a> for more information.
/// </remarks>
/// <returns><see langword="true"/> if the repository is managed by the given team; <see langword="false"/> otherwise.</returns>
public async Task<bool> IsRepositoryManagedByTeam(int id, string owner, string repo)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(repo, "repo");
var endpoint = ApiUrls.TeamRepository(id, owner, repo);
try
{
var response = await ApiConnection.Connection.GetResponse<string>(endpoint);
return response.HttpResponse.StatusCode == HttpStatusCode.NoContent;
}
catch (NotFoundException)
{
return false;
}
}
}
}
+13 -2
View File
@@ -1149,9 +1149,20 @@ namespace Octokit
}
/// <summary>
/// returns the <see cref="Uri"/> for teams
/// Returns the <see cref="Uri"/> to discover teams
/// for the current user
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static Uri UserTeams()
{
return "user/teams".FormatUri();
}
/// <summary>
/// Returns the <see cref="Uri"/> for teams
/// use for getting, updating, or deleting a <see cref="Team"/>.
/// </summary>
/// <param name="id">The id of the <see cref="Team"/>.</param>
/// <returns></returns>
public static Uri Teams(int id)
{
@@ -0,0 +1,9 @@
namespace Octokit
{
public enum TeamMembership
{
NotFound = 0,
Pending = 1,
Active = 2
}
}
+1
View File
@@ -393,6 +393,7 @@
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+1
View File
@@ -409,6 +409,7 @@
<Compile Include="Models\Request\RepositoryHooksPingRequest.cs" />
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Models\Request\RepositoryHookTestRequest.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
+1
View File
@@ -402,6 +402,7 @@
<Compile Include="Models\Request\RepositoryHooksPingRequest.cs" />
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Models\Request\RepositoryHookTestRequest.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+1
View File
@@ -391,6 +391,7 @@
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
+2 -1
View File
@@ -395,6 +395,7 @@
<Compile Include="Models\Response\PullRequestFile.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
<Compile Include="Exceptions\TwoFactorAuthorizationException.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
@@ -410,4 +411,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
+1
View File
@@ -222,6 +222,7 @@
<Compile Include="Models\Response\SearchResult.cs" />
<Compile Include="Models\Response\SearchUsersResult.cs" />
<Compile Include="Models\Response\Subscription.cs" />
<Compile Include="Models\Response\TeamMembership.cs" />
<Compile Include="Models\Response\ThreadSubscription.cs" />
<Compile Include="Models\Response\TreeItem.cs" />
<Compile Include="Models\Response\TreeResponse.cs" />