using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's Repository Commits API. /// /// /// See the Repository Commits API documentation for more information. /// public class RepositoryCommitsClient : ApiClient, IRepositoryCommitsClient { public RepositoryCommitsClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Gets all pull requests for a given commit /// /// The Id of the repository /// Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch [ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/branches-where-head")] public Task> BranchesWhereHead(long repositoryId, string sha1) { return BranchesWhereHead(repositoryId, sha1, ApiOptions.None); } /// /// Gets all pull requests for a given commit /// /// The Id of the repository /// Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch /// /// Options for changing the API response [ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/branches-where-head")] public Task> BranchesWhereHead(long repositoryId, string sha1, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryCommitsBranchesWhereHead(repositoryId, sha1), null, options); } /// /// List pull requests associated with a commit /// /// The owner of the repository /// The name of the repository /// Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head")] public Task> BranchesWhereHead(string owner, string name, string sha1) { return BranchesWhereHead(owner, name, sha1, ApiOptions.None); } /// /// Gets all pull requests for a given commit /// /// The owner of the repository /// The name of the repository /// Used to find all branches where the given commit SHA is the HEAD, or latest commit for the branch /// /// Options for changing the API response [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head")] public Task> BranchesWhereHead(string owner, string name, string sha1, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryCommitsBranchesWhereHead(owner, name, sha1), null, options); } /// /// Compare two references in a repository /// /// The owner of the repository /// The name of the repository /// The reference to use as the base commit /// The reference to use as the head commit [ManualRoute("GET", "/repos/{owner}/{repo}/compare/{base}...{head}")] public Task Compare(string owner, string name, string @base, string head) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(@base, nameof(@base)); Ensure.ArgumentNotNullOrEmptyString(head, nameof(head)); return ApiConnection.Get(ApiUrls.RepoCompare(owner, name, @base, head)); } /// /// Compare two references in a repository /// /// The Id of the repository /// The reference to use as the base commit /// The reference to use as the head commit [ManualRoute("GET", "/repositories/{id}/compare/{base}...{head}")] public Task Compare(long repositoryId, string @base, string head) { Ensure.ArgumentNotNullOrEmptyString(@base, nameof(@base)); Ensure.ArgumentNotNullOrEmptyString(head, nameof(head)); return ApiConnection.Get(ApiUrls.RepoCompare(repositoryId, @base, head)); } /// /// Compare two references in a repository /// /// The owner of the repository /// The name of the repository /// The reference to use as the base commit /// The reference to use as the head commit /// Options for changing the API response [ManualRoute("GET", "/repos/{owner}/{repo}/compare/{base}...{head}")] public Task Compare(string owner, string name, string @base, string head, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(@base, nameof(@base)); Ensure.ArgumentNotNullOrEmptyString(head, nameof(head)); Ensure.ArgumentNotNull(options, nameof(options)); return Compare(ApiUrls.RepoCompare(owner, name, @base, head), options); } /// /// Compare two references in a repository /// /// The Id of the repository /// The reference to use as the base commit /// The reference to use as the head commit /// Options for changing the API response [ManualRoute("GET", "/repositories/{id}/compare/{base}...{head}")] public Task Compare(long repositoryId, string @base, string head, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(@base, nameof(@base)); Ensure.ArgumentNotNullOrEmptyString(head, nameof(head)); Ensure.ArgumentNotNull(options, nameof(options)); return Compare(ApiUrls.RepoCompare(repositoryId, @base, head), options); } private async Task Compare(Uri uri, ApiOptions options) { var results = await ApiConnection.GetAll(uri, options); if (results.Count == 1) return results[0]; var firstCompareResult = results[0]; var commits = firstCompareResult.Commits.ToList(); var files = firstCompareResult.Files.ToList(); foreach (var compareResult in results.Skip(1)) { commits.AddRange(compareResult.Commits ?? Array.Empty()); files.AddRange(compareResult.Files ?? Array.Empty()); } return new CompareResult( firstCompareResult.Url, firstCompareResult.HtmlUrl, firstCompareResult.PermalinkUrl, firstCompareResult.DiffUrl, firstCompareResult.PatchUrl, firstCompareResult.BaseCommit, firstCompareResult.MergeBaseCommit, firstCompareResult.Status, firstCompareResult.AheadBy, firstCompareResult.BehindBy, firstCompareResult.TotalCommits, commits, files); } /// /// Gets a single commit for a given repository /// /// The owner of the repository /// The name of the repository /// The reference for the commit (SHA) [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}")] public Task Get(string owner, string name, string reference) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return ApiConnection.Get(ApiUrls.RepositoryCommit(owner, name, reference)); } /// /// Gets a single commit for a given repository /// /// The Id of the repository /// The reference for the commit (SHA) [ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}")] public Task Get(long repositoryId, string reference) { Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return ApiConnection.Get(ApiUrls.RepositoryCommit(repositoryId, reference)); } /// /// Gets all commits for a given repository /// /// The owner of the repository /// The name of the repository [ManualRoute("GET", "/repos/{owner}/{repo}/commits")] public Task> GetAll(string owner, string name) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return GetAll(owner, name, new CommitRequest(), ApiOptions.None); } /// /// Gets all commits for a given repository /// /// The Id of the repository [ManualRoute("GET", "/repositories/{id}/commits")] public Task> GetAll(long repositoryId) { return GetAll(repositoryId, new CommitRequest(), ApiOptions.None); } /// /// Gets all commits for a given repository /// /// The owner of the repository /// The name of the repository /// Options for changing the API response [ManualRoute("GET", "/repos/{owner}/{repo}/commits")] public Task> GetAll(string owner, string name, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return GetAll(owner, name, new CommitRequest(), options); } /// /// Gets all commits for a given repository /// /// The Id of the repository /// Options for changing the API response [ManualRoute("GET", "/repositories/{id}/commits")] public Task> GetAll(long repositoryId, ApiOptions options) { return GetAll(repositoryId, new CommitRequest(), options); } /// /// Gets all commits for a given repository /// /// The owner of the repository /// The name of the repository /// Used to filter list of commits returned [ManualRoute("GET", "/repos/{owner}/{repo}/commits")] public Task> GetAll(string owner, string name, CommitRequest request) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(request, nameof(request)); return GetAll(owner, name, request, ApiOptions.None); } /// /// Gets all commits for a given repository /// /// The Id of the repository /// Used to filter list of commits returned [ManualRoute("GET", "/repositories/{id}/commits")] public Task> GetAll(long repositoryId, CommitRequest request) { Ensure.ArgumentNotNull(request, nameof(request)); return GetAll(repositoryId, request, ApiOptions.None); } /// /// Gets all commits for a given repository /// /// The owner of the repository /// The name of the repository /// Used to filter list of commits returned /// Options for changing the API response [ManualRoute("GET", "/repos/{owner}/{repo}/commits")] public Task> GetAll(string owner, string name, CommitRequest request, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryCommits(owner, name), request.ToParametersDictionary(), options); } /// /// Gets all commits for a given repository /// /// The Id of the repository /// Used to filter list of commits returned /// Options for changing the API response [ManualRoute("GET", "/repositories/{id}/commits")] public Task> GetAll(long repositoryId, CommitRequest request, ApiOptions options) { Ensure.ArgumentNotNull(request, nameof(request)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryCommits(repositoryId), request.ToParametersDictionary(), options); } /// /// Get the SHA-1 of a commit reference /// /// The owner of the repository /// The name of the repository /// The repository reference [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}")] public Task GetSha1(string owner, string name, string reference) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return ApiConnection.Get(ApiUrls.RepositoryCommit(owner, name, reference), null); } /// /// Get the SHA-1 of a commit reference /// /// The Id of the repository /// The repository reference [ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}")] public Task GetSha1(long repositoryId, string reference) { Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference)); return ApiConnection.Get(ApiUrls.RepositoryCommit(repositoryId, reference), null); } /// /// Gets all pull requests for a given commit /// /// The Id of the repository /// Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history [ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/pulls")] public Task> PullRequests(long repositoryId, string sha1) { return PullRequests(repositoryId, sha1, ApiOptions.None); } /// /// Gets all pull requests for a given commit /// /// The Id of the repository /// Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history /// /// Options for changing the API response [ManualRoute("GET", "/repositories/{id}/commits/{commit_sha}/pulls")] public Task> PullRequests(long repositoryId, string sha1, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryCommitsPull(repositoryId, sha1), null, options); } /// /// List pull requests associated with a commit /// /// The owner of the repository /// The name of the repository /// Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/pulls")] public Task> PullRequests(string owner, string name, string sha1) { return PullRequests(owner, name, sha1, ApiOptions.None); } /// /// Gets all pull requests for a given commit /// /// The owner of the repository /// The name of the repository /// Used to find all pull requests containing the provided commit SHA, which can be from any point in the commit history /// /// Options for changing the API response [ManualRoute("GET", "/repos/{owner}/{repo}/commits/{commit_sha}/pulls")] public Task> PullRequests(string owner, string name, string sha1, ApiOptions options) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); Ensure.ArgumentNotNullOrEmptyString(sha1, nameof(sha1)); Ensure.ArgumentNotNull(options, nameof(options)); return ApiConnection.GetAll(ApiUrls.RepositoryCommitsPull(owner, name, sha1), null, options); } } }