Back to ReleasesCLient.

This commit is contained in:
Matt Burke
2013-10-02 16:35:30 -04:00
committed by Haacked
parent 2efd9aa23b
commit ede0267a96
13 changed files with 206 additions and 152 deletions
@@ -49,6 +49,7 @@
<Compile Include="AutoCompleteClientTests.cs" />
<Compile Include="AutomationSettings.cs" />
<Compile Include="IntegrationTestAttribute.cs" />
<Compile Include="ReleasesClientTests.cs" />
<Compile Include="RepositoriesClientTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UsersClientTests.cs" />
@@ -0,0 +1,26 @@
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Octokit.Tests.Integration
{
public class ReleasesClientTests
{
public class TheGetReleasesMethod
{
[IntegrationTest]
public async Task ReturnsReleases()
{
var github = new GitHubClient
{
Credentials = AutomationSettings.Current.GitHubCredentials
};
var releases = await github.Releases.GetAll("git-tfs", "git-tfs");
Assert.True(releases.Count > 5);
Assert.True(releases.Any(release => release.TagName == "v0.18.0"));
}
}
}
}
@@ -90,22 +90,5 @@ namespace Octokit.Tests.Integration
Assert.Contains("<p><strong>WARNING: This is some haacky code.", readMeHtml);
}
}
public class TheGetReleasesMethod
{
[IntegrationTest]
public async Task ReturnsReleases()
{
var github = new GitHubClient
{
Credentials = AutomationSettings.Current.GitHubCredentials
};
var releases = await github.Repository.GetReleases("git-tfs", "git-tfs");
Assert.True(releases.Count > 5);
Assert.True(releases.Any(release => release.TagName == "v0.18.0"));
}
}
}
}
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Clients;
using Octokit.Http;
using Octokit.Tests.Helpers;
using Xunit;
namespace Octokit.Tests.Clients
{
public class ReleasesClientTests
{
public class TheGetReleasesMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var client = Substitute.For<IApiConnection<Release>>();
var releasesClient = new ReleasesClient(client);
releasesClient.GetAll("fake", "repo");
client.Received().GetAll<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"), null);
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var releasesClient = new ReleasesClient(Substitute.For<IApiConnection<Release>>());
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.GetAll(null, "name"));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.GetAll("owner", null));
}
}
public class TheCreateReleaseMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var client = Substitute.For<IApiConnection<Release>>();
var releasesClient = new ReleasesClient(client);
var data = new ReleaseUpdate("fake-tag");
releasesClient.CreateRelease("fake", "repo", data);
client.Received().Create<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"), data);
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var releasesClient = new ReleasesClient(Substitute.For<IApiConnection<Release>>());
var data = new ReleaseUpdate("fake-tag");
Assert.Throws<ArgumentNullException>(() => new ReleaseUpdate(null));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.CreateRelease(null, "name", data));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.CreateRelease("owner", null, data));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.CreateRelease("owner", "name", null));
}
}
public class TheUploadReleaseAssetMethod
{
[Fact]
public void UploadsToCorrectUrl()
{
var client = Substitute.For<IApiConnection<Release>>();
var releasesClient = new ReleasesClient(client);
var release = new Release { UploadUrl = "https://uploads.test.dev/does/not/matter/releases/1/assets{?name}" };
var rawData = Substitute.For<Stream>();
var upload = new ReleaseAssetUpload { FileName = "example.zip", ContentType = "application/zip", RawData = rawData };
releasesClient.UploadAsset(release, upload);
client.Received().Upload<ReleaseAsset>(Arg.Is<Uri>(u => u.ToString() == "https://uploads.test.dev/does/not/matter/releases/1/assets?name=example.zip"),
rawData,
Arg.Is<Dictionary<string, string>>(headers => headers["Content-Type"] == "application/zip"));
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var releasesClient = new ReleasesClient(Substitute.For<IApiConnection<Release>>());
var release = new Release { UploadUrl = "https://uploads.github.com/anything" };
var uploadData = new ReleaseAssetUpload { FileName = "good", ContentType = "good/good", RawData = Stream.Null };
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.UploadAsset(null, uploadData));
await AssertEx.Throws<ArgumentNullException>(async () => await releasesClient.UploadAsset(release, null));
}
}
}
}
@@ -141,85 +141,5 @@ namespace Octokit.Tests.Clients
client.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme"), null);
}
}
public class TheGetReleasesMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var client = Substitute.For<IApiConnection<Repository>>();
var repositoriesClient = new RepositoriesClient(client);
repositoriesClient.GetReleases("fake", "repo");
client.Received().GetAll<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"), null);
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var repositoriesClient = new RepositoriesClient(Substitute.For<IApiConnection<Repository>>());
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.GetReleases(null, "name"));
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.GetReleases("owner", null));
}
}
public class TheCreateReleaseMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var client = Substitute.For<IApiConnection<Repository>>();
var repositoriesClient = new RepositoriesClient(client);
var data = new ReleaseUpdate("fake-tag");
repositoriesClient.CreateRelease("fake", "repo", data);
client.Received().Create<Release>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/releases"), data);
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var repositoriesClient = new RepositoriesClient(Substitute.For<IApiConnection<Repository>>());
var data = new ReleaseUpdate("fake-tag");
Assert.Throws<ArgumentNullException>(() => new ReleaseUpdate(null));
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.CreateRelease(null, "name", data));
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.CreateRelease("owner", null, data));
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.CreateRelease("owner", "name", null));
}
}
public class TheUploadReleaseAssetMethod
{
[Fact]
public void UploadsToCorrectUrl()
{
var client = Substitute.For<IApiConnection<Repository>>();
var repositoriesClient = new RepositoriesClient(client);
var release = new Release { UploadUrl = "https://uploads.test.dev/does/not/matter/releases/1/assets{?name}" };
var rawData = Substitute.For<Stream>();
var upload = new ReleaseAssetUpload { FileName = "example.zip", ContentType = "application/zip", RawData = rawData };
repositoriesClient.UploadAsset(release, upload);
client.Received().Upload<ReleaseAsset>(Arg.Is<Uri>(u => u.ToString() == "https://uploads.test.dev/does/not/matter/releases/1/assets?name=example.zip"),
rawData,
Arg.Is<Dictionary<string, string>>(headers => headers["Content-Type"] == "application/zip"));
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var repositoriesClient = new RepositoriesClient(Substitute.For<IApiConnection<Repository>>());
var release = new Release { UploadUrl = "https://uploads.github.com/anything" };
var uploadData = new ReleaseAssetUpload { FileName = "good", ContentType = "good/good", RawData = Stream.Null };
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.UploadAsset(null, uploadData));
await AssertEx.Throws<ArgumentNullException>(async () => await repositoriesClient.UploadAsset(release, null));
}
}
}
}
+1
View File
@@ -51,6 +51,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\CredentialsTests.cs" />
<Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Exceptions\ApiValidationExceptionTests.cs" />
<Compile Include="Helpers\UriExtensionsTests.cs" />
+43
View File
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Octokit.Http;
namespace Octokit.Clients
{
public class ReleasesClient : ApiClient<Release>, IReleasesClient
{
public ReleasesClient(IApiConnection<Release> client) : base(client)
{
}
public async Task<IReadOnlyCollection<Release>> GetAll(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "repository");
var endpoint = "/repos/{0}/{1}/releases".FormatUri(owner, name);
return await Client.GetAll(endpoint);
}
public async Task<Release> CreateRelease(string owner, string name, ReleaseUpdate data)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "repository");
Ensure.ArgumentNotNull(data, "data");
var endpoint = "/repos/{0}/{1}/releases".FormatUri(owner, name);
return await Client.Create(endpoint, data);
}
public async Task<ReleaseAsset> UploadAsset(Release release, ReleaseAssetUpload data)
{
Ensure.ArgumentNotNull(release, "release");
Ensure.ArgumentNotNull(data, "data");
var endpoint = release.UploadUrl.ExpandUriTemplate(new { name = data.FileName });
return await Client.Upload<ReleaseAsset>(endpoint, data.RawData, new Dictionary<string, string> { { "Content-Type", data.ContentType }, { "Accept", "application/vnd.github.manifold-preview" } });
}
}
}
-30
View File
@@ -53,35 +53,5 @@ namespace Octokit.Clients
var readmeInfo = await Client.GetItem<ReadmeResponse>(endpoint, null);
return new Readme(readmeInfo, Client);
}
public async Task<IReadOnlyCollection<Release>> GetReleases(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "repository");
var endpoint = "/repos/{0}/{1}/releases".FormatUri(owner, name);
return await Client.GetAll<Release, Repository>(endpoint);
}
public async Task<Release> CreateRelease(string owner, string name, ReleaseUpdate data)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "repository");
Ensure.ArgumentNotNull(data, "data");
var endpoint = "/repos/{0}/{1}/releases".FormatUri(owner, name);
return await Client.Create<Release>(endpoint, data);
}
public async Task<ReleaseAsset> UploadAsset(Release release, ReleaseAssetUpload data)
{
Ensure.ArgumentNotNull(release, "release");
Ensure.ArgumentNotNull(data, "data");
var endpoint = release.UploadUrl.ExpandUriTemplate(new { name = data.FileName });
return await Client.Upload<ReleaseAsset>(endpoint, data.RawData, new Dictionary<string, string> { { "Content-Type", data.ContentType }, { "Accept", "application/vnd.github.manifold-preview" } });
}
}
}
+2
View File
@@ -39,6 +39,7 @@ namespace Octokit
AutoComplete = new AutoCompleteClient(connection);
Organization = new OrganizationsClient(new ApiConnection<Organization>(connection));
Repository = new RepositoriesClient(new ApiConnection<Repository>(connection));
Releases = new ReleasesClient(new ApiConnection<Release>(connection));
User = new UsersClient(new ApiConnection<User>(connection));
SshKey = new SshKeysClient(new ApiConnection<SshKey>(connection));
}
@@ -79,6 +80,7 @@ namespace Octokit
public IAutoCompleteClient AutoComplete { get; private set; }
public IOrganizationsClient Organization { get; private set; }
public IRepositoriesClient Repository { get; private set; }
public IReleasesClient Releases { get; private set; }
public ISshKeysClient SshKey { get; private set; }
public IUsersClient User { get; private set; }
}
+33
View File
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
public interface IReleasesClient
{
/// <summary>
/// Retrieves every <see cref="Release"/> for the specified repository.
/// </summary>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <returns>A <see cref="IReadonlyPagedCollection{Release}"/> of <see cref="Release"/>.</returns>
Task<IReadOnlyCollection<Release>> GetAll(string owner, string name);
/// <summary>
/// Create a <see cref="Release"/> for the specified repository.
/// </summary>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="data">The data for the release.</param>
/// <returns>A new <see cref="Release"/>.</returns>
Task<Release> CreateRelease(string owner, string name, ReleaseUpdate data);
/// <summary>
/// Upload a <see cref="ReleaseAsset"/> for the specified release.
/// </summary>
/// <param name="release">The <see cref="Release"/> to attach the asset to.</param>
/// <param name="data">The asset information.</param>
/// <returns>A new <see cref="ReleaseAsset"/>.</returns>
Task<ReleaseAsset> UploadAsset(Release release, ReleaseAssetUpload data);
}
}
-25
View File
@@ -57,30 +57,5 @@ namespace Octokit
/// <param name="name">The name of the repository.</param>
/// <returns></returns>
Task<Readme> GetReadme(string owner, string name);
/// <summary>
/// Retrieves every <see cref="Release"/> for the specified repository.
/// </summary>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <returns>A <see cref="IReadonlyPagedCollection{Release}"/> of <see cref="Release"/>.</returns>
Task<IReadOnlyCollection<Release>> GetReleases(string owner, string name);
/// <summary>
/// Create a <see cref="Release"/> for the specified repository.
/// </summary>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="data">The data for the release.</param>
/// <returns>A new <see cref="Release"/>.</returns>
Task<Release> CreateRelease(string owner, string name, ReleaseUpdate data);
/// <summary>
/// Upload a <see cref="ReleaseAsset"/> for the specified release.
/// </summary>
/// <param name="release">The <see cref="Release"/> to attach the asset to.</param>
/// <param name="data">The asset information.</param>
/// <returns>A new <see cref="ReleaseAsset"/>.</returns>
Task<ReleaseAsset> UploadAsset(Release release, ReleaseAssetUpload data);
}
}
+2
View File
@@ -86,6 +86,7 @@
<Compile Include="Clients\OrganizationsClient.cs" />
<Compile Include="Authentication\AnonymousAuthenticator.cs" />
<Compile Include="Authentication\Authenticator.cs" />
<Compile Include="Clients\ReleasesClient.cs" />
<Compile Include="Clients\SshKeysClient.cs" />
<Compile Include="Exceptions\ApiValidationException.cs" />
<Compile Include="Helpers\CollectionExtensions.cs" />
@@ -106,6 +107,7 @@
<Compile Include="IAuthorizationsClient.cs" />
<Compile Include="IAutoCompleteClient.cs" />
<Compile Include="IGitHubClient.cs" />
<Compile Include="IReleasesClient.cs" />
<Compile Include="ISshKeysClient.cs" />
<Compile Include="IOrganizationsClient.cs" />
<Compile Include="IReadOnlyPagedCollection.cs" />
+2
View File
@@ -110,6 +110,7 @@
<Compile Include="Clients\AuthorizationsClient.cs" />
<Compile Include="Clients\AutoCompleteClient.cs" />
<Compile Include="Clients\OrganizationsClient.cs" />
<Compile Include="Clients\ReleasesClient.cs" />
<Compile Include="Clients\RepositoriesClient.cs" />
<Compile Include="Clients\SshKeysClient.cs" />
<Compile Include="Clients\UsersClient.cs" />
@@ -153,6 +154,7 @@
<Compile Include="Http\IRequest.cs" />
<Compile Include="Http\IResponse.cs" />
<Compile Include="Http\Request.cs" />
<Compile Include="IReleasesClient.cs" />
<Compile Include="IRepositoriesClient.cs" />
<Compile Include="ISshKeysClient.cs" />
<Compile Include="IUsersClient.cs" />