Skip to content

Commit

Permalink
Implement GetLicenseContents() method for getting repository's licens…
Browse files Browse the repository at this point in the history
…e info
  • Loading branch information
jozefizso committed Nov 15, 2017
1 parent b6e80e8 commit 6360fbb
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 21 deletions.
21 changes: 21 additions & 0 deletions Octokit.Reactive/Clients/IObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,27 @@ public interface IObservableRepositoriesClient
/// <returns>All of the repositories tags.</returns>
IObservable<RepositoryTag> GetAllTags(long repositoryId, ApiOptions options);

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
IObservable<RepositoryContentLicense> GetLicenseContents(string owner, string name);

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="repositoryId">The Id of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
IObservable<RepositoryContentLicense> GetLicenseContents(long repositoryId);

/// <summary>
/// Updates the specified repository with the values given in <paramref name="update"/>
/// </summary>
Expand Down
30 changes: 30 additions & 0 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,36 @@ public IObservable<Repository> Edit(string owner, string name, RepositoryUpdate
return _client.Edit(owner, name, update).ToObservable();
}

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
public IObservable<RepositoryContentLicense> GetLicenseContents(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _client.GetLicenseContents(owner, name).ToObservable();
}

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="repositoryId">The Id of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
public IObservable<RepositoryContentLicense> GetLicenseContents(long repositoryId)
{
return _client.GetLicenseContents(repositoryId).ToObservable();
}

/// <summary>
/// Updates the specified repository with the values given in <paramref name="update"/>
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,4 +1612,31 @@ public async Task GetsPagesOfBranchesWithRepositoryId()
Assert.NotEqual(firstPage[4].Name, secondPage[4].Name);
}
}

public class TheGetLicenseContentsMethod
{
[IntegrationTest]
public async Task ReturnsLicenseContent()
{
var github = Helper.GetAuthenticatedClient();

var license = await github.Repository.GetLicenseContents("octokit", "octokit.net");
Assert.Equal("LICENSE.txt", license.Name);
Assert.NotNull(license.License);
Assert.Equal("mit", license.License.Key);
Assert.Equal("MIT License", license.License.Name);
}

[IntegrationTest]
public async Task ReturnsLicenseContentWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();

var license = await github.Repository.GetLicenseContents(7528679);
Assert.Equal("LICENSE.txt", license.Name);
Assert.NotNull(license.License);
Assert.Equal("mit", license.License.Key);
Assert.Equal("MIT License", license.License.Name);
}
}
}
40 changes: 38 additions & 2 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public async Task RequestsCorrectUrl()
connection.Received().Get<Repository>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/name"),
null,
"application/vnd.github.polaris-preview+json");
"application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");
}

[Fact]
Expand All @@ -275,7 +275,7 @@ public async Task RequestsCorrectUrlWithRepositoryId()
connection.Received().Get<Repository>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1"),
null,
"application/vnd.github.polaris-preview+json");
"application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");
}

[Fact]
Expand Down Expand Up @@ -796,6 +796,42 @@ public async Task EnsuresNonNullArguments()
}
}

public class TheGetLicenseContentsMethod
{
[Fact]
public async Task RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);

await client.GetLicenseContents("owner", "name");

connection.Received()
.Get<RepositoryContentLicense>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/license"), null, "application/vnd.github.drax-preview+json");
}

[Fact]
public async Task RequestsTheCorrectUrlWithRepositoryId()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);

await client.GetLicenseContents(1);

connection.Received()
.Get<RepositoryContentLicense>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/license"), null, "application/vnd.github.drax-preview+json");
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new RepositoriesClient(Substitute.For<IApiConnection>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetLicenseContents(null, "repo"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetLicenseContents("owner", null));
}
}

public class TheGetAllTagsMethod
{
[Fact]
Expand Down
32 changes: 32 additions & 0 deletions Octokit.Tests/Models/LicenseMetadataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using Octokit.Internal;
using Xunit;

namespace Octokit.Tests.Models
{
public class LicenseMetadataTests
{
[Fact]
public void CanBeDeserializedFromLicenseJson()
{
const string json = @"{
""key"": ""mit"",
""name"": ""MIT License"",
""spdx_id"": ""MIT"",
""url"": ""https://api.github.com/licenses/mit"",
""featured"": true
}";
var serializer = new SimpleJsonSerializer();

var license = serializer.Deserialize<LicenseMetadata>(json);

Assert.Equal("mit", license.Key);
Assert.Equal("MIT License", license.Name);
Assert.Equal("MIT", license.SpdxId);
Assert.Equal("https://api.github.com/licenses/mit", license.Url);
Assert.True(license.Featured);
}
}
}

51 changes: 51 additions & 0 deletions Octokit.Tests/Models/RepositoryContentLicenseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using Octokit.Internal;
using Xunit;

namespace Octokit.Tests.Models
{
public class RepositoryContentLicenseTests
{
[Fact]
public void CanBeDeserializedFromRepositoryContentLicenseJson()
{
const string json = @"{
""name"": ""LICENSE"",
""path"": ""LICENSE"",
""sha"": ""401c59dcc4570b954dd6d345e76199e1f4e76266"",
""size"": 1077,
""url"": ""https://api.github.com/repos/benbalter/gman/contents/LICENSE?ref=master"",
""html_url"": ""https://github.com/benbalter/gman/blob/master/LICENSE"",
""git_url"": ""https://api.github.com/repos/benbalter/gman/git/blobs/401c59dcc4570b954dd6d345e76199e1f4e76266"",
""download_url"": ""https://raw.githubusercontent.com/benbalter/gman/master/LICENSE?lab=true"",
""type"": ""file"",
""content"": ""VGhlIE1JVCBMaWNlbnNlIChNSVQpCgpDb3B5cmlnaHQgKGMpIDIwMTMgQmVu\nIEJhbHRlcgoKUGVybWlzc2lvbiBpcyBoZXJlYnkgZ3JhbnRlZCwgZnJlZSBv\nZiBjaGFyZ2UsIHRvIGFueSBwZXJzb24gb2J0YWluaW5nIGEgY29weSBvZgp0\naGlzIHNvZnR3YXJlIGFuZCBhc3NvY2lhdGVkIGRvY3VtZW50YXRpb24gZmls\nZXMgKHRoZSAiU29mdHdhcmUiKSwgdG8gZGVhbCBpbgp0aGUgU29mdHdhcmUg\nd2l0aG91dCByZXN0cmljdGlvbiwgaW5jbHVkaW5nIHdpdGhvdXQgbGltaXRh\ndGlvbiB0aGUgcmlnaHRzIHRvCnVzZ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IElOIFRIRSBTT0ZUV0FSRS4K\n"",
""encoding"": ""base64"",
""_links"": {
""self"": ""https://api.github.com/repos/benbalter/gman/contents/LICENSE?ref=master"",
""git"": ""https://api.github.com/repos/benbalter/gman/git/blobs/401c59dcc4570b954dd6d345e76199e1f4e76266"",
""html"": ""https://github.com/benbalter/gman/blob/master/LICENSE""
},
""license"": {
""key"": ""mit"",
""name"": ""MIT License"",
""spdx_id"": ""MIT"",
""url"": ""https://api.github.com/licenses/mit"",
""featured"": true
}
}";
var serializer = new SimpleJsonSerializer();

var license = serializer.Deserialize<RepositoryContentLicense>(json);
var licenseMetadata = license.License;

Assert.Equal("LICENSE", license.Name);
Assert.Equal("LICENSE", license.Path);
Assert.Equal("401c59dcc4570b954dd6d345e76199e1f4e76266", license.Sha);
Assert.NotNull(license.License);
Assert.Equal("mit", licenseMetadata.Key);
}
}
}

16 changes: 8 additions & 8 deletions Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ public async Task IsALukeWarmObservable()
var response = Task.Factory.StartNew<IApiResponse<Repository>>(() =>
new ApiResponse<Repository>(new Response(), repository));
var connection = Substitute.For<IConnection>();
connection.Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json").Returns(response);
connection.Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json").Returns(response);
var gitHubClient = new GitHubClient(connection);
var client = new ObservableRepositoriesClient(gitHubClient);
var observable = client.Get("stark", "ned");

connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");

var result = await observable;
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");
var result2 = await observable;
// TODO: If we change this to a warm observable, we'll need to change this to Received(2)
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");

Assert.Same(repository, result);
Assert.Same(repository, result2);
Expand All @@ -117,18 +117,18 @@ public async Task IsALukeWarmObservableWithRepositoryId()
var response = Task.Factory.StartNew<IApiResponse<Repository>>(() =>
new ApiResponse<Repository>(new Response(), repository));
var connection = Substitute.For<IConnection>();
connection.Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json").Returns(response);
connection.Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json").Returns(response);
var gitHubClient = new GitHubClient(connection);
var client = new ObservableRepositoriesClient(gitHubClient);
var observable = client.Get(1);

connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");

var result = await observable;
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");
var result2 = await observable;
// TODO: If we change this to a warm observable, we'll need to change this to Received(2)
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json,application/vnd.github.drax-preview+json");

Assert.Same(repository, result);
Assert.Same(repository, result2);
Expand Down
21 changes: 21 additions & 0 deletions Octokit/Clients/IRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,27 @@ public interface IRepositoriesClient
/// <returns>All of the repositories tags.</returns>
Task<IReadOnlyList<RepositoryTag>> GetAllTags(long repositoryId, ApiOptions options);

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
Task<RepositoryContentLicense> GetLicenseContents(string owner, string name);

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="repositoryId">The Id of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
Task<RepositoryContentLicense> GetLicenseContents(long repositoryId);

/// <summary>
/// Updates the specified repository with the values given in <paramref name="update"/>
/// </summary>
Expand Down
35 changes: 33 additions & 2 deletions Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public Task<Repository> Get(string owner, string name)
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return ApiConnection.Get<Repository>(ApiUrls.Repository(owner, name), null, AcceptHeaders.SquashCommitPreview);
return ApiConnection.Get<Repository>(ApiUrls.Repository(owner, name), null, AcceptHeaders.Concat(AcceptHeaders.SquashCommitPreview, AcceptHeaders.LicensesApiPreview));
}

/// <summary>
Expand All @@ -222,7 +222,7 @@ public Task<Repository> Get(string owner, string name)
/// <returns>A <see cref="Repository"/></returns>
public Task<Repository> Get(long repositoryId)
{
return ApiConnection.Get<Repository>(ApiUrls.Repository(repositoryId), null, AcceptHeaders.SquashCommitPreview);
return ApiConnection.Get<Repository>(ApiUrls.Repository(repositoryId), null, AcceptHeaders.Concat(AcceptHeaders.SquashCommitPreview, AcceptHeaders.LicensesApiPreview));
}

/// <summary>
Expand Down Expand Up @@ -808,6 +808,37 @@ public Task<IReadOnlyList<RepositoryTag>> GetAllTags(long repositoryId, ApiOptio
return ApiConnection.GetAll<RepositoryTag>(ApiUrls.RepositoryTags(repositoryId), options);
}

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
public Task<RepositoryContentLicense> GetLicenseContents(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));

return ApiConnection.Get<RepositoryContentLicense>(ApiUrls.RepositoryLicense(owner, name), null, AcceptHeaders.LicensesApiPreview);

}

/// <summary>
/// Get the contents of a repository's license
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/licenses/#get-the-contents-of-a-repositorys-license">API documentation</a> for more details
/// </remarks>
/// <param name="repositoryId">The Id of the repository</param>
/// <returns>Returns the contents of the repository's license file, if one is detected.</returns>
public Task<RepositoryContentLicense> GetLicenseContents(long repositoryId)
{
return ApiConnection.Get<RepositoryContentLicense>(ApiUrls.RepositoryLicense(repositoryId), null, AcceptHeaders.LicensesApiPreview);
}

/// <summary>
/// A client for GitHub's Repository Pages API.
/// </summary>
Expand Down
Loading

0 comments on commit 6360fbb

Please sign in to comment.