From 16f6eec1c809bff5067ce452fb92b0507ca94cf8 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Tue, 18 Jul 2017 14:30:17 +0200 Subject: [PATCH] Implement GetLicenseContents() method for getting repository's license info --- .../Clients/IObservableRepositoriesClient.cs | 21 +++++++++ .../Clients/ObservableRepositoriesClient.cs | 30 +++++++++++++ Octokit/Clients/IRepositoriesClient.cs | 21 +++++++++ Octokit/Clients/RepositoriesClient.cs | 31 +++++++++++++ Octokit/Helpers/ApiUrls.cs | 21 +++++++++ Octokit/Models/Response/RepositoryLicense.cs | 44 +++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 Octokit/Models/Response/RepositoryLicense.cs diff --git a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs index dae1ec2690..d0669870cd 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoriesClient.cs @@ -422,6 +422,27 @@ public interface IObservableRepositoriesClient /// All of the repositories tags. IObservable GetAllTags(long repositoryId, ApiOptions options); + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// Returns the contents of the repository's license file, if one is detected. + IObservable GetLicenseContents(string owner, string name); + + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// Returns the contents of the repository's license file, if one is detected. + IObservable GetLicenseContents(long repositoryId); + /// /// Updates the specified repository with the values given in /// diff --git a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs index aea66d3b8a..04f570f697 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoriesClient.cs @@ -649,6 +649,36 @@ public IObservable Edit(string owner, string name, RepositoryUpdate return _client.Edit(owner, name, update).ToObservable(); } + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// Returns the contents of the repository's license file, if one is detected. + public IObservable GetLicenseContents(string owner, string name) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + + return _client.GetLicenseContents(owner, name).ToObservable(); + } + + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// Returns the contents of the repository's license file, if one is detected. + public IObservable GetLicenseContents(long repositoryId) + { + return _client.GetLicenseContents(repositoryId).ToObservable(); + } + /// /// Updates the specified repository with the values given in /// diff --git a/Octokit/Clients/IRepositoriesClient.cs b/Octokit/Clients/IRepositoriesClient.cs index bab7c37ef9..0ce28e6c4f 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -527,6 +527,27 @@ public interface IRepositoriesClient /// All of the repositories tags. Task> GetAllTags(long repositoryId, ApiOptions options); + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// Returns the contents of the repository's license file, if one is detected. + Task GetLicenseContents(string owner, string name); + + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// Returns the contents of the repository's license file, if one is detected. + Task GetLicenseContents(long repositoryId); + /// /// Updates the specified repository with the values given in /// diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index e7a9b044f4..a27999cc35 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -808,6 +808,37 @@ public Task> GetAllTags(long repositoryId, ApiOptio return ApiConnection.GetAll(ApiUrls.RepositoryTags(repositoryId), options); } + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// Returns the contents of the repository's license file, if one is detected. + public Task GetLicenseContents(string owner, string name) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + + return ApiConnection.Get(ApiUrls.RepositoryLicense(owner, name), null, AcceptHeaders.LicensesApiPreview); + + } + + /// + /// Get the contents of a repository's license + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// Returns the contents of the repository's license file, if one is detected. + public Task GetLicenseContents(long repositoryId) + { + return ApiConnection.Get(ApiUrls.RepositoryLicense(repositoryId), null, AcceptHeaders.LicensesApiPreview); + } + /// /// A client for GitHub's Repository Pages API. /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 24120b3165..62cc4b065d 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -3699,5 +3699,26 @@ public static Uri ProjectCardMove(int id) { return "projects/columns/cards/{0}/moves".FormatUri(id); } + + /// + /// Returns the for repository's license requests. + /// + /// The owner of repo + /// The name of repo + /// The for repository's license requests. + public static Uri RepositoryLicense(string owner, string repo) + { + return "repos/{0}/{1}/license".FormatUri(owner, repo); + } + + /// + /// Returns the for repository's license requests. + /// + /// The id of the repository + /// The for repository's license requests. + public static Uri RepositoryLicense(long repositoryId) + { + return "repositories/{0}/license".FormatUri(repositoryId); + } } } diff --git a/Octokit/Models/Response/RepositoryLicense.cs b/Octokit/Models/Response/RepositoryLicense.cs new file mode 100644 index 0000000000..8eb99519db --- /dev/null +++ b/Octokit/Models/Response/RepositoryLicense.cs @@ -0,0 +1,44 @@ +using System.Diagnostics; +using System.Globalization; + +namespace Octokit +{ + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class RepositoryLicense : LicenseMetadata + { + public RepositoryLicense( + string key, + string name, + string spdxId, + string url, + bool featured) : base(key, name, url) + { + Ensure.ArgumentNotNull(spdxId, "spdxId"); + + SpdxId = spdxId; + Featured = featured; + } + + public RepositoryLicense() + { + } + + /// + /// SPDX license identifier. + /// + public string SpdxId { get; protected set; } + + /// + /// Whether the license is one of the licenses featured on https://choosealicense.com + /// + public bool Featured { get; protected set; } + + internal override string DebuggerDisplay + { + get + { + return string.Format(CultureInfo.InvariantCulture, "{0} Featured: {1}", base.DebuggerDisplay, Featured); + } + } + } +}