Skip to content

Commit

Permalink
Fixes #1586 - Repository license API (#1630)
Browse files Browse the repository at this point in the history
* Implement GetLicenseContents() method for getting repository's license info

* Request License Preview API for calls that return Repository object.

* Add missing accept headers to observable methods for ObservableRepositoriesClients

* fix impacted unit tests
  • Loading branch information
jozefizso authored and ryangribble committed Jan 19, 2018
1 parent a2f6650 commit 270356b
Show file tree
Hide file tree
Showing 16 changed files with 443 additions and 57 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
46 changes: 38 additions & 8 deletions Octokit.Reactive/Clients/ObservableRepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public IObservable<Repository> Get(long repositoryId)
/// <returns>A <see cref="IReadOnlyPagedCollection{Repository}"/> of <see cref="Repository"/>.</returns>
public IObservable<Repository> GetAllPublic()
{
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.AllPublicRepositories());
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.AllPublicRepositories(), null, AcceptHeaders.LicensesApiPreview);
}

/// <summary>
Expand All @@ -146,7 +146,7 @@ public IObservable<Repository> GetAllPublic(PublicRepositoryRequest request)

var url = ApiUrls.AllPublicRepositories(request.Since);

return _connection.GetAndFlattenAllPages<Repository>(url);
return _connection.GetAndFlattenAllPages<Repository>(url, null, AcceptHeaders.LicensesApiPreview);
}

/// <summary>
Expand All @@ -172,7 +172,7 @@ public IObservable<Repository> GetAllForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.Repositories(), options);
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.Repositories(), null, AcceptHeaders.LicensesApiPreview, options);
}

/// <summary>
Expand Down Expand Up @@ -203,7 +203,7 @@ public IObservable<Repository> GetAllForCurrent(RepositoryRequest request, ApiOp
Ensure.ArgumentNotNull(request, "request");
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.Repositories(), request.ToParametersDictionary());
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.Repositories(), request.ToParametersDictionary(), AcceptHeaders.LicensesApiPreview);
}

/// <summary>
Expand All @@ -229,7 +229,7 @@ public IObservable<Repository> GetAllForUser(string login, ApiOptions options)
Ensure.ArgumentNotNullOrEmptyString(login, "login");
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.Repositories(login), options);
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.Repositories(login), null, AcceptHeaders.LicensesApiPreview, options);
}

/// <summary>
Expand Down Expand Up @@ -257,15 +257,15 @@ public IObservable<Repository> GetAllForOrg(string organization, ApiOptions opti
Ensure.ArgumentNotNullOrEmptyString(organization, "organization");
Ensure.ArgumentNotNull(options, "options");

return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.OrganizationRepositories(organization), options);
return _connection.GetAndFlattenAllPages<Repository>(ApiUrls.OrganizationRepositories(organization), null, AcceptHeaders.LicensesApiPreview, options);
}

/// <summary>
/// A client for GitHub's Commit Status API.
/// </summary>
/// <remarks>
/// See the <a href="http://developer.github.com/v3/repos/statuses/">Commit Status API documentation</a> for more
/// details. Also check out the <a href="https://github.com/blog/1227-commit-status-api">blog post</a>
/// details. Also check out the <a href="https://github.com/blog/1227-commit-status-api">blog post</a>
/// that announced this feature.
/// </remarks>
public IObservableCommitStatusClient Status { get; private set; }
Expand Down Expand Up @@ -303,7 +303,7 @@ public IObservable<Repository> GetAllForOrg(string organization, ApiOptions opti
/// <summary>
/// A client for GitHub's Repository Forks API.
/// </summary>
/// <remarks>See <a href="http://developer.github.com/v3/repos/forks/">Forks API documentation</a> for more information.</remarks>
/// <remarks>See <a href="http://developer.github.com/v3/repos/forks/">Forks API documentation</a> for more information.</remarks>
public IObservableRepositoryForksClient Forks { get; private set; }

/// <summary>
Expand Down 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, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(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
69 changes: 69 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task CreatesANewPublicRepository()
Assert.True(repository.HasWiki);
Assert.Null(repository.Homepage);
Assert.NotNull(repository.DefaultBranch);
Assert.Null(repository.License);
}
}

Expand Down Expand Up @@ -189,6 +190,35 @@ public async Task CreatesARepositoryWithAGitignoreTemplate()
}
}

[IntegrationTest]
public async Task CreatesARepositoryWithALicenseTemplate()
{
var github = Helper.GetAuthenticatedClient();
var repoName = Helper.MakeNameWithTimestamp("repo-with-license");

var newRepository = new NewRepository(repoName)
{
AutoInit = true,
LicenseTemplate = "mit"
};

using (var context = await github.CreateRepositoryContext(newRepository))
{
var createdRepository = context.Repository;

// NOTE: the License attribute is empty for newly created repositories
Assert.Null(createdRepository.License);

// license information is not immediatelly available after the repository is created
await Task.Delay(TimeSpan.FromSeconds(1));

// check for actual license by reloading repository info
var repository = await github.Repository.Get(Helper.UserName, repoName);
Assert.NotNull(repository.License);
Assert.Equal("mit", repository.License.Key);
}
}


[IntegrationTest]
public async Task ThrowsInvalidGitIgnoreExceptionForInvalidTemplateNames()
Expand Down Expand Up @@ -746,6 +776,18 @@ public async Task ReturnsRepositoryMergeOptionsWithRepositoryId()
Assert.NotNull(repository.AllowMergeCommit);
}
}

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

var repository = await github.Repository.Get("github", "choosealicense.com");

Assert.NotNull(repository.License);
Assert.Equal("mit", repository.License.Key);
Assert.Equal("MIT License", repository.License.Name);
}
}

public class TheGetAllPublicMethod
Expand Down Expand Up @@ -1612,4 +1654,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);
}
}
}
Loading

0 comments on commit 270356b

Please sign in to comment.