From 87c7eed2ebef89483d3ccd53d52a4fbefcf8a616 Mon Sep 17 00:00:00 2001 From: Kristian Hald Date: Tue, 21 Apr 2015 00:27:10 +0200 Subject: [PATCH] Changed 'dynamic' to Dictionary for configuration of hooks. --- .../Clients/RepositoryHooksClientTests.cs | 49 ++++++++++++++++--- .../fixtures/RepositoriesHooksFixture.cs | 6 ++- .../Clients/RepositoryHooksClientTest.cs | 12 +++-- Octokit/Models/Request/EditRepositoryHook.cs | 14 ++++-- Octokit/Models/Request/NewRepositoryHook.cs | 7 +-- Octokit/Models/Response/RepositoryHook.cs | 4 +- 6 files changed, 67 insertions(+), 25 deletions(-) diff --git a/Octokit.Tests.Integration/Clients/RepositoryHooksClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryHooksClientTests.cs index 07db26bd..137bd10d 100644 --- a/Octokit.Tests.Integration/Clients/RepositoryHooksClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoryHooksClientTests.cs @@ -1,5 +1,7 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; +using Octokit.Tests.Helpers; using Octokit.Tests.Integration.fixtures; using Xunit; @@ -62,21 +64,32 @@ namespace Octokit.Tests.Integration.Clients var repoName = Helper.MakeNameWithTimestamp("create-hooks-test"); var repository = await github.Repository.Create(new NewRepository(repoName) { AutoInit = true }); - var parameters = new NewRepositoryHook("tenxer", new { content_type = "json", url = "http://test.com/example" }) + var config = new Dictionary { - Events = new[] { "delete" }, + { "content_type", "json" }, + { "url", "http://test.com/example" }, + { "hostname", "http://hostname.url" }, + { "username", "username" }, + { "password", "password" } + }; + var parameters = new NewRepositoryHook("windowsazure", config) + { + Events = new[] { "push" }, Active = false }; + var hook = await github.Repository.Hooks.Create(Helper.Credentials.Login, repository.Name, parameters); var baseHookUrl = CreateExpectedBaseHookUrl(repository.Url, hook.Id); - Assert.Equal("tenxer", hook.Name); - Assert.Equal(new[] { "delete" }.ToList(), hook.Events.ToList()); + Assert.Equal("windowsazure", hook.Name); + Assert.Equal(new[] { "push" }.ToList(), hook.Events.ToList()); Assert.Equal(baseHookUrl, hook.Url); Assert.Equal(baseHookUrl + "/test", hook.TestUrl); Assert.Equal(baseHookUrl + "/pings", hook.PingUrl); Assert.NotNull(hook.CreatedAt); Assert.NotNull(hook.UpdatedAt); + Assert.Equal(config.Keys, hook.Config.Keys); + Assert.Equal(config.Values, hook.Config.Values); Assert.Equal(false, hook.Active); } @@ -97,7 +110,7 @@ namespace Octokit.Tests.Integration.Clients } [IntegrationTest] - public async Task EditHookWithNewInformation() + public async Task EditHookWithNoNewConfigRetainsTheOldConfig() { var github = Helper.GetAuthenticatedClient(); @@ -105,9 +118,31 @@ namespace Octokit.Tests.Integration.Clients { AddEvents = new[] { "pull_request" } }; + var actualHook = await github.Repository.Hooks.Edit(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHook.Id, editRepositoryHook); - Assert.Equal(new[] { "delete", "pull_request" }.ToList(), actualHook.Events.ToList()); + var expectedConfig = new Dictionary { { "content_type", "json" }, { "url", "http://test.com/example" } }; + Assert.Equal(new[] { "commit_comment", "pull_request" }.ToList(), actualHook.Events.ToList()); + Assert.Equal(expectedConfig.Keys, actualHook.Config.Keys); + Assert.Equal(expectedConfig.Values, actualHook.Config.Values); + } + + [IntegrationTest] + public async Task EditHookWithNewInformation() + { + var github = Helper.GetAuthenticatedClient(); + + var editRepositoryHook = new EditRepositoryHook(new Dictionary { { "project", "GEZDGORQFY2TCNZRGY2TSMBVGUYDK" } }) + { + AddEvents = new[] { "pull_request" } + }; + + var actualHook = await github.Repository.Hooks.Edit(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHook.Id, editRepositoryHook); + + var expectedConfig = new Dictionary { { "project", "GEZDGORQFY2TCNZRGY2TSMBVGUYDK" } }; + Assert.Equal(new[] { "commit_comment", "pull_request" }.ToList(), actualHook.Events.ToList()); + Assert.Equal(expectedConfig.Keys, actualHook.Config.Keys); + Assert.Equal(expectedConfig.Values, actualHook.Config.Values); } } diff --git a/Octokit.Tests.Integration/fixtures/RepositoriesHooksFixture.cs b/Octokit.Tests.Integration/fixtures/RepositoriesHooksFixture.cs index 671674f6..2c948a51 100644 --- a/Octokit.Tests.Integration/fixtures/RepositoriesHooksFixture.cs +++ b/Octokit.Tests.Integration/fixtures/RepositoriesHooksFixture.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace Octokit.Tests.Integration.fixtures { @@ -36,9 +37,10 @@ namespace Octokit.Tests.Integration.fixtures static RepositoryHook CreateHook(IGitHubClient github, Repository repository) { - var parameters = new NewRepositoryHook("tenxer", new { content_type = "json", url = "http://test.com/example" }) + var config = new Dictionary { { "content_type", "json" }, { "url", "http://test.com/example" } }; + var parameters = new NewRepositoryHook("apropos", config) { - Events = new[] { "delete" }, + Events = new[] { "commit_comment" }, Active = false }; var createdHook = github.Repository.Hooks.Create(Helper.Credentials.Login, repository.Name, parameters); diff --git a/Octokit.Tests/Clients/RepositoryHooksClientTest.cs b/Octokit.Tests/Clients/RepositoryHooksClientTest.cs index 8b157d1c..19308b10 100644 --- a/Octokit.Tests/Clients/RepositoryHooksClientTest.cs +++ b/Octokit.Tests/Clients/RepositoryHooksClientTest.cs @@ -1,4 +1,5 @@ -using NSubstitute; +using System.Collections.Generic; +using NSubstitute; using System; using System.Threading.Tasks; using Xunit; @@ -60,7 +61,7 @@ namespace Octokit.Tests.Clients { var connection = Substitute.For(); var client = new RepositoriesClient(connection); - var hook = new NewRepositoryHook("name", new { config = "" }); + var hook = new NewRepositoryHook("name", new Dictionary { {"config", "" }}); client.Hooks.Create("fake", "repo", hook); @@ -72,8 +73,9 @@ namespace Octokit.Tests.Clients { var client = new RepositoriesClient(Substitute.For()); - await Assert.ThrowsAsync(async () => await client.Hooks.Create(null, "name", new NewRepositoryHook("name", new { config = "" }))); - await Assert.ThrowsAsync(async () => await client.Hooks.Create("owner", null, new NewRepositoryHook("name", new { config = "" }))); + var config = new Dictionary { { "config", "" } }; + await Assert.ThrowsAsync(async () => await client.Hooks.Create(null, "name", new NewRepositoryHook("name", config))); + await Assert.ThrowsAsync(async () => await client.Hooks.Create("owner", null, new NewRepositoryHook("name", config))); await Assert.ThrowsAsync(async () => await client.Hooks.Create("owner", "name", null)); } @@ -82,7 +84,7 @@ namespace Octokit.Tests.Clients { var connection = Substitute.For(); var client = new RepositoriesClient(connection); - var newRepositoryHook = new NewRepositoryHook("name", new { config = "" }); + var newRepositoryHook = new NewRepositoryHook("name", new Dictionary { { "config", "" } }); client.Hooks.Create("owner", "repo", newRepositoryHook); diff --git a/Octokit/Models/Request/EditRepositoryHook.cs b/Octokit/Models/Request/EditRepositoryHook.cs index 5814c355..5d2ce258 100644 --- a/Octokit/Models/Request/EditRepositoryHook.cs +++ b/Octokit/Models/Request/EditRepositoryHook.cs @@ -9,10 +9,17 @@ namespace Octokit [DebuggerDisplay("{DebuggerDisplay,nq}")] public class EditRepositoryHook { - [Parameter(Key = "config")] - public dynamic Config { get; set; } + public EditRepositoryHook() + : this(null) + { } + + public EditRepositoryHook(IDictionary config) + { + Config = config; + } + + public IDictionary Config { get; private set; } - [Parameter(Key = "events")] public IEnumerable Events { get; set; } [Parameter(Key = "add_events")] @@ -21,7 +28,6 @@ namespace Octokit [Parameter(Key = "remove_events")] public IEnumerable RemoveEvents { get; set; } - [Parameter(Key = "active")] public bool? Active { get; set; } internal string DebuggerDisplay diff --git a/Octokit/Models/Request/NewRepositoryHook.cs b/Octokit/Models/Request/NewRepositoryHook.cs index 384ae9f4..508d8022 100644 --- a/Octokit/Models/Request/NewRepositoryHook.cs +++ b/Octokit/Models/Request/NewRepositoryHook.cs @@ -9,7 +9,7 @@ namespace Octokit [DebuggerDisplay("{DebuggerDisplay,nq}")] public class NewRepositoryHook { - public NewRepositoryHook(string name, dynamic config) + public NewRepositoryHook(string name, Dictionary config) { Name = name; Config = config; @@ -21,13 +21,10 @@ namespace Octokit /// /// Is a key value structure determined by the web hook being created /// - [Parameter(Key = "config")] - public dynamic Config { get; set; } + public IReadOnlyDictionary Config { get; private set; } - [Parameter(Key = "events")] public IEnumerable Events { get; set; } - [Parameter(Key = "active")] public bool Active { get; set; } internal string DebuggerDisplay diff --git a/Octokit/Models/Response/RepositoryHook.cs b/Octokit/Models/Response/RepositoryHook.cs index 6b6b482e..9da236fb 100644 --- a/Octokit/Models/Response/RepositoryHook.cs +++ b/Octokit/Models/Response/RepositoryHook.cs @@ -11,7 +11,7 @@ namespace Octokit { public RepositoryHook() { } - public RepositoryHook(int id, string url, string testUrl, string pingUrl, DateTimeOffset createdAt, DateTimeOffset updatedAt, string name, IReadOnlyList events, bool active, dynamic config) + public RepositoryHook(int id, string url, string testUrl, string pingUrl, DateTimeOffset createdAt, DateTimeOffset updatedAt, string name, IReadOnlyList events, bool active, IReadOnlyDictionary config) { Url = url; TestUrl = testUrl; @@ -45,7 +45,7 @@ namespace Octokit public bool Active { get; private set; } - public dynamic Config { get; private set; } + public IReadOnlyDictionary Config { get; private set; } internal string DebuggerDisplay {