diff --git a/Octokit/Helpers/ReferenceExtensions.cs b/Octokit/Helpers/ReferenceExtensions.cs new file mode 100644 index 00000000..87ffddc6 --- /dev/null +++ b/Octokit/Helpers/ReferenceExtensions.cs @@ -0,0 +1,45 @@ +using System.Threading.Tasks; + +namespace Octokit.Helpers +{ + /// + /// Represents operations to simplify workink with references + /// + public static class ReferenceExtensions + { + /// + /// Creates a branch, based off the branch specified. + /// + /// The this method extends + /// The owner of the repository. + /// The name of the repository. + /// The new branch name + /// The to base the branch from + public static async Task CreateBranch(this IReferencesClient referencesClient, string owner, string name, string branchName, Reference baseReference) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); + Ensure.ArgumentNotNull(baseReference, "baseReference"); + + return await referencesClient.Create(owner, name, new NewReference("refs/heads/" + branchName, baseReference.Object.Sha)); + } + + /// + /// Creates a branch, based off the master branch. + /// + /// The this method extends + /// The owner of the repository. + /// The name of the repository. + /// The new branch name + public static async Task CreateBranch(this IReferencesClient referencesClient, string owner, string name, string branchName) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); + + var baseBranch = await referencesClient.Get(owner, name, "heads/master"); + return await referencesClient.Create(owner, name, new NewReference("refs/heads/" + branchName, baseBranch.Object.Sha)); + } + } +}