using System;
using System.Linq;
using System.Threading.Tasks;
namespace Octokit
{
///
/// A client for GitHub's Git Trees API.
///
///
/// See the Git Trees API documentation for more information.
///
public class TreesClient : ApiClient, ITreesClient
{
///
/// Instantiates a new GitHub Git Trees API client.
///
/// An API connection
public TreesClient(IApiConnection apiConnection)
: base(apiConnection)
{
}
///
/// Gets a Tree Response for a given SHA.
///
///
/// http://developer.github.com/v3/git/trees/#get-a-tree
///
/// The owner of the repository
/// The name of the repository
/// The SHA that references the tree
[ManualRoute("GET", "/repos/{owner}/{repo}/git/trees/{tree_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.Tree(owner, name, reference));
}
///
/// Gets a Tree Response for a given SHA.
///
///
/// http://developer.github.com/v3/git/trees/#get-a-tree
///
/// The Id of the repository
/// The SHA that references the tree
[ManualRoute("GET", "/repositories/{id}/git/trees/{tree_sha}")]
public Task Get(long repositoryId, string reference)
{
Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference));
return ApiConnection.Get(ApiUrls.Tree(repositoryId, reference));
}
///
/// Gets a Tree Response for a given SHA.
///
///
/// https://developer.github.com/v3/git/trees/#get-a-tree-recursively
///
/// The owner of the repository
/// The name of the repository
/// The SHA that references the tree
[ManualRoute("GET", "/repos/{owner}/{repo}/git/trees/{tree_sha}?recursive=1")]
public Task GetRecursive(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.TreeRecursive(owner, name, reference));
}
///
/// Gets a Tree Response for a given SHA.
///
///
/// https://developer.github.com/v3/git/trees/#get-a-tree-recursively
///
/// The Id of the repository
/// The SHA that references the tree
[ManualRoute("GET", "/repositories/{id}/git/trees/{tree_sha}?recursive=1")]
public Task GetRecursive(long repositoryId, string reference)
{
Ensure.ArgumentNotNullOrEmptyString(reference, nameof(reference));
return ApiConnection.Get(ApiUrls.TreeRecursive(repositoryId, reference));
}
///
/// Creates a new Tree in the specified repo
///
///
/// http://developer.github.com/v3/git/trees/#create-a-tree
///
/// The owner of the repository
/// The name of the repository
/// The value of the new tree
[ManualRoute("POST", "/repos/{owner}/{repo}/git/trees")]
public Task Create(string owner, string name, NewTree newTree)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(newTree, nameof(newTree));
if (newTree.Tree.Any(t => string.IsNullOrWhiteSpace(t.Mode)))
{
throw new ArgumentException("You have specified items in the tree which do not have a Mode value set.");
}
return ApiConnection.Post(ApiUrls.Tree(owner, name), newTree);
}
///
/// Creates a new Tree in the specified repo
///
///
/// http://developer.github.com/v3/git/trees/#create-a-tree
///
/// The Id of the repository
/// The value of the new tree
[ManualRoute("POST", "/repos/{id}/git/trees")]
public Task Create(long repositoryId, NewTree newTree)
{
Ensure.ArgumentNotNull(newTree, nameof(newTree));
if (newTree.Tree.Any(t => string.IsNullOrWhiteSpace(t.Mode)))
{
throw new ArgumentException("You have specified items in the tree which do not have a Mode value set.");
}
return ApiConnection.Post(ApiUrls.Tree(repositoryId), newTree);
}
}
}