From 15c8ff9362719e392302bb5b402471696674ee14 Mon Sep 17 00:00:00 2001 From: Mordechai Zuber Date: Sun, 17 Jan 2016 22:33:02 +0200 Subject: [PATCH] Add implementation and unit tests --- .../Clients/RepositoryPagesClientTests.cs | 85 +++++++++++++++++++ Octokit.Tests/Octokit.Tests.csproj | 1 + Octokit/Clients/IRepositoriesClient.cs | 10 ++- Octokit/Clients/IRepositoryPagesClient.cs | 22 +++-- Octokit/Clients/RepositoriesClient.cs | 9 ++ Octokit/Clients/RepositoryPagesClient.cs | 77 +++++++++++++++++ Octokit/Helpers/ApiUrls.cs | 15 ++++ Octokit/Models/Response/Page.cs | 2 +- Octokit/Models/Response/PagesBuild.cs | 6 +- Octokit/Octokit.csproj | 1 + 10 files changed, 218 insertions(+), 10 deletions(-) create mode 100644 Octokit.Tests/Clients/RepositoryPagesClientTests.cs create mode 100644 Octokit/Clients/RepositoryPagesClient.cs diff --git a/Octokit.Tests/Clients/RepositoryPagesClientTests.cs b/Octokit.Tests/Clients/RepositoryPagesClientTests.cs new file mode 100644 index 00000000..6088c9d4 --- /dev/null +++ b/Octokit.Tests/Clients/RepositoryPagesClientTests.cs @@ -0,0 +1,85 @@ +using NSubstitute; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Octokit.Tests.Clients +{ + public class RepositoryPagesClientTests + { + public class TheGetMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryPagesClient(connection); + + client.Get("fake", "repo"); + + connection.Received().Get(Arg.Is(u => u.ToString() == "repos/fake/repo/pages"), null); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryPagesClient(connection); + + await Assert.ThrowsAsync(() => client.Get(null, "name")); + await Assert.ThrowsAsync(() => client.Get("owner", null)); + } + } + + public class TheGetBuildsMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryPagesClient(connection); + + client.GetBuilds("fake", "repo"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/pages/builds")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryPagesClient(connection); + + await Assert.ThrowsAsync(() => client.Get(null, "name")); + await Assert.ThrowsAsync(() => client.Get("owner", null)); + } + } + + public class TheGetLatestBuildMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryPagesClient(connection); + + client.GetLatestBuild("fake", "repo"); + + connection.Received().Get(Arg.Is(u => u.ToString() == "repos/fake/repo/pages/builds/latest"), null); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryPagesClient(connection); + + await Assert.ThrowsAsync(() => client.Get(null, "name")); + await Assert.ThrowsAsync(() => client.Get("owner", null)); + } + } + } +} diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 5dc32016..57055097 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -93,6 +93,7 @@ + diff --git a/Octokit/Clients/IRepositoriesClient.cs b/Octokit/Clients/IRepositoriesClient.cs index 654a4231..d88480a6 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -269,7 +269,7 @@ namespace Octokit /// See the Commits API documentation for more details /// IRepositoryCommitsClient Commit { get; } - + /// /// Access GitHub's Releases API. /// @@ -384,5 +384,13 @@ namespace Octokit /// New values to update the branch with /// The updated Task EditBranch(string owner, string name, string branch, BranchUpdate update); + + /// + /// A client for GitHub's Repository Pages API. + /// + /// + /// See the Repository Pages API documentation for more information. + /// + IRepositoryPagesClient Page { get; } } } diff --git a/Octokit/Clients/IRepositoryPagesClient.cs b/Octokit/Clients/IRepositoryPagesClient.cs index 71709f9f..eb3a0555 100644 --- a/Octokit/Clients/IRepositoryPagesClient.cs +++ b/Octokit/Clients/IRepositoryPagesClient.cs @@ -1,32 +1,39 @@ -using Octokit.Models.Response; -using System; +using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Octokit.Clients +namespace Octokit { /// /// A client for GitHub's Repository Pages API. /// /// - /// See the Repository Pages API documentation for more information. + /// See the Repository Pages API documentation for more information. /// - interface IRepositoryPagesClient + public interface IRepositoryPagesClient { /// /// Gets the page metadata for a given repository /// /// The owner of the repository /// The name of the repository + /// + /// See the API documentation for more information. + /// /// - Task> Get(string owner, string repositoryName); + [SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get")] + Task Get(string owner, string repositoryName); /// /// Gets all build metadata for a given repository /// /// The owner of the repository /// The name of the repository + /// + /// See the API documentation for more information. + /// /// Task> GetBuilds(string owner, string repositoryName); /// @@ -34,6 +41,9 @@ namespace Octokit.Clients /// /// The owner of the repository /// The name of the repository + /// + /// See the API documentation for more information. + /// /// Task GetLatestBuild(string owner, string repositoryName); } diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index 8360a970..0c3c9379 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -44,6 +44,7 @@ namespace Octokit DeployKeys = new RepositoryDeployKeysClient(apiConnection); Merging = new MergingClient(apiConnection); Content = new RepositoryContentsClient(apiConnection); + Page = new RepositoryPagesClient(apiConnection); } /// @@ -577,5 +578,13 @@ namespace Octokit return ApiConnection.Get(ApiUrls.RepoBranch(owner, repositoryName, branchName), null, AcceptHeaders.ProtectedBranchesApiPreview); } + + /// + /// A client for GitHub's Repository Pages API. + /// + /// + /// See the Repository Pages API documentation for more information. + /// + public IRepositoryPagesClient Page { get; private set; } } } diff --git a/Octokit/Clients/RepositoryPagesClient.cs b/Octokit/Clients/RepositoryPagesClient.cs new file mode 100644 index 00000000..a8492d9f --- /dev/null +++ b/Octokit/Clients/RepositoryPagesClient.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit +{ + /// + /// A client for GitHub's Repository Pages API. + /// + /// + /// See the Repository Pages API documentation for more information. + /// + public class RepositoryPagesClient : ApiClient, IRepositoryPagesClient + { + /// + /// Initializes a new GitHub Repository Pages API client. + /// + /// An API connection. + public RepositoryPagesClient(IApiConnection apiConnection) : base(apiConnection) + { + + } + + /// + /// Gets the page metadata for a given repository + /// + /// The owner of the repository + /// The name of the repository + /// + /// See the API documentation for more information. + /// + /// + public Task Get(string owner, string repositoryName) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + return ApiConnection.Get(ApiUrls.RepositoryPage(owner, repositoryName)); + } + + /// + /// Gets all build metadata for a given repository + /// + /// The owner of the repository + /// The name of the repository + /// + /// See the API documentation for more information. + /// + /// + public Task> GetBuilds(string owner, string repositoryName) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + return ApiConnection.GetAll(ApiUrls.RepositoryBuilds(owner, repositoryName)); + } + + /// + /// Gets the build metadata for the last build for a given repository + /// + /// The owner of the repository + /// The name of the repository + /// + /// See the API documentation for more information. + /// + /// + public Task GetLatestBuild(string owner, string repositoryName) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + return ApiConnection.Get(ApiUrls.RepositoryBuildsLatest(owner, repositoryName)); + } + } +} diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index f8f79d68..ee3b0d22 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -1554,5 +1554,20 @@ namespace Octokit { return "repos/{0}/{1}/contents/{2}?ref={3}".FormatUri(owner, name, path, reference); } + + public static Uri RepositoryPage(string owner, string name) + { + return "repos/{0}/{1}/pages".FormatUri(owner, name); + } + + public static Uri RepositoryBuilds(string owner, string name) + { + return "repos/{0}/{1}/pages/builds".FormatUri(owner, name); + } + + public static Uri RepositoryBuildsLatest(string owner, string name) + { + return "repos/{0}/{1}/pages/builds/latest".FormatUri(owner, name); + } } } diff --git a/Octokit/Models/Response/Page.cs b/Octokit/Models/Response/Page.cs index ddfff06c..454f2565 100644 --- a/Octokit/Models/Response/Page.cs +++ b/Octokit/Models/Response/Page.cs @@ -7,7 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Octokit.Models.Response +namespace Octokit { public enum PagesBuildStatus { diff --git a/Octokit/Models/Response/PagesBuild.cs b/Octokit/Models/Response/PagesBuild.cs index cc11a47d..b40199a8 100644 --- a/Octokit/Models/Response/PagesBuild.cs +++ b/Octokit/Models/Response/PagesBuild.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Octokit.Models.Response +namespace Octokit { /// /// Metadata of a Github Pages build. @@ -16,10 +16,11 @@ namespace Octokit.Models.Response { public PagesBuild() { } - public PagesBuild(string url, PagesBuildStatus status, User pusher, Commit commit, TimeSpan duration, DateTime createdAt, DateTime updatedAt) + public PagesBuild(string url, PagesBuildStatus status, ApiError error, User pusher, Commit commit, TimeSpan duration, DateTime createdAt, DateTime updatedAt) { Url = url; Status = status; + Error = error; Pusher = pusher; Commit = commit; Duration = duration; @@ -35,6 +36,7 @@ namespace Octokit.Models.Response /// The status of the build. /// public PagesBuildStatus Status { get; protected set; } + public ApiError Error { get; set; } /// /// The user whose commit intiated the build. /// diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index 7238ed97..73a2f144 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -73,6 +73,7 @@ +