Skip to content

Commit

Permalink
Add proper accept header to all team calls (Fixes #1794) (#1795)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MikhailTymchukDX authored and ryangribble committed Apr 25, 2018
1 parent 82b1b21 commit f9bf9b2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public IObservable<Team> GetAllTeams(string owner, string name, ApiOptions optio
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(options, nameof(options));

return _connection.GetAndFlattenAllPages<Team>(ApiUrls.RepositoryTeams(owner, name), options);
return _connection.GetAndFlattenAllPages<Team>(ApiUrls.RepositoryTeams(owner, name), null, AcceptHeaders.NestedTeamsPreview, options);
}

/// <summary>
Expand All @@ -565,7 +565,7 @@ public IObservable<Team> GetAllTeams(long repositoryId, ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));

return _connection.GetAndFlattenAllPages<Team>(ApiUrls.RepositoryTeams(repositoryId), options);
return _connection.GetAndFlattenAllPages<Team>(ApiUrls.RepositoryTeams(repositoryId), null, AcceptHeaders.NestedTeamsPreview, options);
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ public async Task RequestsTheCorrectUrl()
connection.Received()
.GetAll<Team>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/teams"),
null,
"application/vnd.github.hellcat-preview+json",
Args.ApiOptions);
}

Expand Down Expand Up @@ -757,6 +759,8 @@ public async Task RequestsTheCorrectUrlWithApiOptions()
connection.Received()
.GetAll<Team>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/teams"),
null,
"application/vnd.github.hellcat-preview+json",
options);
}

Expand Down
50 changes: 34 additions & 16 deletions Octokit.Tests/Clients/RepositoryBranchesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,25 +1037,29 @@ public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryBranchesClient(connection);
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";

client.GetAllProtectedBranchTeamRestrictions("owner", "repo", "branch");

connection.Received()
.Get<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), null, previewAcceptsHeader);
.Get<IReadOnlyList<Team>>(
Arg.Is<Uri>(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]
public void RequestsTheCorrectUrlWithRepositoryId()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryBranchesClient(connection);
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";

client.GetAllProtectedBranchTeamRestrictions(1, "branch");

connection.Received()
.Get<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), null, previewAcceptsHeader);
.Get<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"),
null,
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand Down Expand Up @@ -1085,12 +1089,15 @@ public void RequestsTheCorrectUrl()
var connection = Substitute.For<IApiConnection>();
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<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), Arg.Any<IReadOnlyList<string>>(), null, previewAcceptsHeader);
.Put<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"),
Arg.Any<IReadOnlyList<string>>(),
null,
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand All @@ -1099,12 +1106,15 @@ public void RequestsTheCorrectUrlWithRepositoryId()
var connection = Substitute.For<IApiConnection>();
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<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), Arg.Any<IReadOnlyList<string>>(), null, previewAcceptsHeader);
.Put<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"),
Arg.Any<IReadOnlyList<string>>(),
null,
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand Down Expand Up @@ -1137,12 +1147,14 @@ public void RequestsTheCorrectUrl()
var connection = Substitute.For<IApiConnection>();
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<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), Arg.Any<IReadOnlyList<string>>(), previewAcceptsHeader);
.Post<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"),
Arg.Any<IReadOnlyList<string>>(),
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand All @@ -1151,12 +1163,14 @@ public void RequestsTheCorrectUrlWithRepositoryId()
var connection = Substitute.For<IApiConnection>();
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<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), Arg.Any<IReadOnlyList<string>>(), previewAcceptsHeader);
.Post<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"),
Arg.Any<IReadOnlyList<string>>(),
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand Down Expand Up @@ -1189,12 +1203,14 @@ public void RequestsTheCorrectUrl()
var connection = Substitute.For<IApiConnection>();
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<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"), Arg.Any<IReadOnlyList<string>>(), previewAcceptsHeader);
.Delete<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/protection/restrictions/teams"),
Arg.Any<BranchProtectionTeamCollection>(),
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand All @@ -1203,12 +1219,14 @@ public void RequestsTheCorrectUrlWithRepositoryId()
var connection = Substitute.For<IApiConnection>();
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<IReadOnlyList<Team>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"), Arg.Any<IReadOnlyList<string>>(), previewAcceptsHeader);
.Delete<IReadOnlyList<Team>>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection/restrictions/teams"),
Arg.Any<IReadOnlyList<string>>(),
"application/vnd.github.loki-preview+json,application/vnd.github.hellcat-preview+json");
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ public Task<IReadOnlyList<Team>> GetAllTeams(string owner, string name, ApiOptio
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(options, nameof(options));

return ApiConnection.GetAll<Team>(ApiUrls.RepositoryTeams(owner, name), options);
return ApiConnection.GetAll<Team>(ApiUrls.RepositoryTeams(owner, name), null, AcceptHeaders.NestedTeamsPreview, options);
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions Octokit/Clients/RepositoryBranchesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ public Task<IReadOnlyList<Team>> GetAllProtectedBranchTeamRestrictions(string ow
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));

return ApiConnection.Get<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), null, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Get<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -863,7 +863,7 @@ public Task<IReadOnlyList<Team>> GetAllProtectedBranchTeamRestrictions(long repo
{
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));

return ApiConnection.Get<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), null, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Get<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -883,7 +883,7 @@ public Task<IReadOnlyList<Team>> UpdateProtectedBranchTeamRestrictions(string ow
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNull(teams, nameof(teams));

return ApiConnection.Put<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, null, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Put<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -900,7 +900,7 @@ public Task<IReadOnlyList<Team>> UpdateProtectedBranchTeamRestrictions(long repo
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNull(teams, nameof(teams));

return ApiConnection.Put<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, null, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Put<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, null, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -920,7 +920,7 @@ public Task<IReadOnlyList<Team>> AddProtectedBranchTeamRestrictions(string owner
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNull(teams, nameof(teams));

return ApiConnection.Post<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Post<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -937,7 +937,7 @@ public Task<IReadOnlyList<Team>> AddProtectedBranchTeamRestrictions(long reposit
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNull(teams, nameof(teams));

return ApiConnection.Post<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Post<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -957,7 +957,7 @@ public Task<IReadOnlyList<Team>> DeleteProtectedBranchTeamRestrictions(string ow
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNull(teams, nameof(teams));

return ApiConnection.Delete<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Delete<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(owner, name, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand All @@ -974,7 +974,7 @@ public Task<IReadOnlyList<Team>> DeleteProtectedBranchTeamRestrictions(long repo
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNull(teams, nameof(teams));

return ApiConnection.Delete<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.ProtectedBranchesApiPreview);
return ApiConnection.Delete<IReadOnlyList<Team>>(ApiUrls.RepoRestrictionsTeams(repositoryId, branch), teams, AcceptHeaders.Concat(AcceptHeaders.ProtectedBranchesApiPreview, AcceptHeaders.NestedTeamsPreview));
}

/// <summary>
Expand Down

0 comments on commit f9bf9b2

Please sign in to comment.