From f9bf9b2347f4d9c68b966b93e8171556c364e793 Mon Sep 17 00:00:00 2001 From: MikhailTymchukDX Date: Wed, 25 Apr 2018 13:51:22 +0300 Subject: [PATCH] Add proper accept header to all team calls (Fixes #1794) (#1795) * add proper accept header to all teams calls * use hardcoded headers in unit tests * dont remove support for preview branch protection, on earlier github enterprise versions --- .../Clients/ObservableRepositoriesClient.cs | 4 +- .../Clients/RepositoriesClientTests.cs | 4 ++ .../Clients/RepositoryBranchesClientTests.cs | 50 +++++++++++++------ Octokit/Clients/RepositoriesClient.cs | 2 +- Octokit/Clients/RepositoryBranchesClient.cs | 16 +++--- 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs index 394be43594..e12e92dce1 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs @@ -549,7 +549,7 @@ public IObservable GetAllTeams(string owner, string name, ApiOptions optio Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(options, nameof(options)); - return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryTeams(owner, name), options); + return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryTeams(owner, name), null, AcceptHeaders.NestedTeamsPreview, options); } /// @@ -565,7 +565,7 @@ public IObservable GetAllTeams(long repositoryId, ApiOptions options) { Ensure.ArgumentNotNull(options, nameof(options)); - return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryTeams(repositoryId), options); + return _connection.GetAndFlattenAllPages(ApiUrls.RepositoryTeams(repositoryId), null, AcceptHeaders.NestedTeamsPreview, options); } /// diff --git a/Octokit.Tests/Clients/RepositoriesClientTests.cs b/Octokit.Tests/Clients/RepositoriesClientTests.cs index 20a1de3254..a59156c395 100644 --- a/Octokit.Tests/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoriesClientTests.cs @@ -722,6 +722,8 @@ public async Task RequestsTheCorrectUrl() connection.Received() .GetAll( Arg.Is(u => u.ToString() == "repos/owner/name/teams"), + null, + "application/vnd.github.hellcat-preview+json", Args.ApiOptions); } @@ -757,6 +759,8 @@ public async Task RequestsTheCorrectUrlWithApiOptions() connection.Received() .GetAll( Arg.Is(u => u.ToString() == "repos/owner/name/teams"), + null, + "application/vnd.github.hellcat-preview+json", options); } diff --git a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs index 839cc0cb49..3243d69465 100644 --- a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs @@ -1037,12 +1037,14 @@ public void RequestsTheCorrectUrl() { var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.GetAllProtectedBranchTeamRestrictions("owner", "repo", "branch"); connection.Received() - .Get>(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), null, previewAcceptsHeader); + .Get>( + Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), + null, + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1050,12 +1052,14 @@ public void RequestsTheCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.GetAllProtectedBranchTeamRestrictions(1, "branch"); connection.Received() - .Get>(Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), null, previewAcceptsHeader); + .Get>( + Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), + null, + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1085,12 +1089,15 @@ public void RequestsTheCorrectUrl() var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); var newTeams = new BranchProtectionTeamCollection() { "test" }; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.UpdateProtectedBranchTeamRestrictions("owner", "repo", "branch", newTeams); connection.Received() - .Put>(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), Arg.Any>(), null, previewAcceptsHeader); + .Put>( + Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), + Arg.Any>(), + null, + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1099,12 +1106,15 @@ public void RequestsTheCorrectUrlWithRepositoryId() var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); var newTeams = new BranchProtectionTeamCollection() { "test" }; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.UpdateProtectedBranchTeamRestrictions(1, "branch", newTeams); connection.Received() - .Put>(Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), Arg.Any>(), null, previewAcceptsHeader); + .Put>( + Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), + Arg.Any>(), + null, + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1137,12 +1147,14 @@ public void RequestsTheCorrectUrl() var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); var newTeams = new BranchProtectionTeamCollection() { "test" }; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.AddProtectedBranchTeamRestrictions("owner", "repo", "branch", newTeams); connection.Received() - .Post>(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), Arg.Any>(), previewAcceptsHeader); + .Post>( + Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), + Arg.Any>(), + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1151,12 +1163,14 @@ public void RequestsTheCorrectUrlWithRepositoryId() var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); var newTeams = new BranchProtectionTeamCollection() { "test" }; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.AddProtectedBranchTeamRestrictions(1, "branch", newTeams); connection.Received() - .Post>(Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), Arg.Any>(), previewAcceptsHeader); + .Post>( + Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), + Arg.Any>(), + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1189,12 +1203,14 @@ public void RequestsTheCorrectUrl() var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); var teamsToRemove = new BranchProtectionTeamCollection() { "test" }; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.DeleteProtectedBranchTeamRestrictions("owner", "repo", "branch", teamsToRemove); connection.Received() - .Delete>(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), Arg.Any>(), previewAcceptsHeader); + .Delete>( + Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), + Arg.Any(), + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] @@ -1203,12 +1219,14 @@ public void RequestsTheCorrectUrlWithRepositoryId() var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); var teamsToRemove = new BranchProtectionTeamCollection() { "test" }; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.DeleteProtectedBranchTeamRestrictions(1, "branch", teamsToRemove); connection.Received() - .Delete>(Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), Arg.Any>(), previewAcceptsHeader); + .Delete>( + Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), + Arg.Any>(), + "application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json"); } [Fact] diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index 0fb6ed1f97..68565559e1 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -724,7 +724,7 @@ public Task> GetAllTeams(string owner, string name, ApiOptio Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(options, nameof(options)); - return ApiConnection.GetAll(ApiUrls.RepositoryTeams(owner, name), options); + return ApiConnection.GetAll(ApiUrls.RepositoryTeams(owner, name), null, AcceptHeaders.NestedTeamsPreview, options); } /// diff --git a/Octokit/Clients/RepositoryBranchesClient.cs b/Octokit/Clients/RepositoryBranchesClient.cs index ddd7197e2b..b0e50d8246 100644 --- a/Octokit/Clients/RepositoryBranchesClient.cs +++ b/Octokit/Clients/RepositoryBranchesClient.cs @@ -848,7 +848,7 @@ public Task> GetAllProtectedBranchTeamRestrictions(string ow Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); - return ApiConnection.Get>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), null, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Get>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -863,7 +863,7 @@ public Task> GetAllProtectedBranchTeamRestrictions(long repo { Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); - return ApiConnection.Get>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), null, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Get>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -883,7 +883,7 @@ public Task> UpdateProtectedBranchTeamRestrictions(string ow Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); Ensure.ArgumentNotNull(teams, nameof(teams)); - return ApiConnection.Put>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, null, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Put>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -900,7 +900,7 @@ public Task> UpdateProtectedBranchTeamRestrictions(long repo Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); Ensure.ArgumentNotNull(teams, nameof(teams)); - return ApiConnection.Put>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, null, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Put>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -920,7 +920,7 @@ public Task> AddProtectedBranchTeamRestrictions(string owner Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); Ensure.ArgumentNotNull(teams, nameof(teams)); - return ApiConnection.Post>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Post>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -937,7 +937,7 @@ public Task> AddProtectedBranchTeamRestrictions(long reposit Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); Ensure.ArgumentNotNull(teams, nameof(teams)); - return ApiConnection.Post>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Post>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -957,7 +957,7 @@ public Task> DeleteProtectedBranchTeamRestrictions(string ow Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); Ensure.ArgumentNotNull(teams, nameof(teams)); - return ApiConnection.Delete>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Delete>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } /// @@ -974,7 +974,7 @@ public Task> DeleteProtectedBranchTeamRestrictions(long repo Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); Ensure.ArgumentNotNull(teams, nameof(teams)); - return ApiConnection.Delete>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview); + return ApiConnection.Delete>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview)); } ///