Merge pull request #1103 from M-Zuber/CreateBranchHelperMethod

Create branch helper method
This commit is contained in:
Phil Haack
2016-02-18 09:55:28 -08:00
9 changed files with 97 additions and 1 deletions
@@ -0,0 +1,32 @@
using Octokit.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Octokit.Tests.Integration.Helpers
{
public class ReferenceExtensionsTests
{
[IntegrationTest]
public async Task CreateABranch()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Git.Reference;
using (var context = await client.CreateRepositoryContext("public-repo"))
{
var branchFromMaster = await fixture.CreateBranch(context.RepositoryOwner, context.RepositoryName, "patch-1");
var branchFromPath = await fixture.CreateBranch(context.RepositoryOwner, context.RepositoryName, "patch-2", branchFromMaster);
var allBranchNames = (await client.Repository.GetAllBranches(context.RepositoryOwner, context.RepositoryName)).Select(b => b.Name);
Assert.Contains("patch-1", allBranchNames);
Assert.Contains("patch-2", allBranchNames);
}
}
}
}
@@ -117,6 +117,7 @@
<Compile Include="Helpers\GitHubEnterpriseTestAttribute.cs" />
<Compile Include="Helpers\PersonalAccessTokenTestAttribute.cs" />
<Compile Include="Helpers\PaidAccountTestAttribute.cs" />
<Compile Include="Helpers\ReferenceExtensionsTests.cs" />
<Compile Include="Helpers\RepositoryContext.cs" />
<Compile Include="Helpers\RepositorySetupHelper.cs" />
<Compile Include="HttpClientAdapterTests.cs" />
+57
View File
@@ -0,0 +1,57 @@
using System;
using System.Globalization;
using System.Threading.Tasks;
namespace Octokit.Helpers
{
/// <summary>
/// Represents operations to simplify working with references
/// </summary>
public static class ReferenceExtensions
{
/// <summary>
/// Creates a branch, based off the branch specified.
/// </summary>
/// <param name="referencesClient">The <see cref="IReferencesClient" /> this method extends</param>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="branchName">The new branch name</param>
/// <param name="baseReference">The <see cref="Reference" /> to base the branch from</param>
public static async Task<Reference> 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");
if (branchName.StartsWith("refs/heads"))
{
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The specified branch name '{0}' appears to be a ref name and not a branch name because it starts with the string 'refs/heads'. Either specify just the branch name or use the Create method if you need to specify the full ref name", branchName), "branchName");
}
return await referencesClient.Create(owner, name, new NewReference("refs/heads/" + branchName, baseReference.Object.Sha));
}
/// <summary>
/// Creates a branch, based off the master branch.
/// </summary>
/// <param name="referencesClient">The <see cref="IReferencesClient" /> this method extends</param>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
/// <param name="branchName">The new branch name</param>
public static async Task<Reference> CreateBranch(this IReferencesClient referencesClient, string owner, string name, string branchName)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName");
if (branchName.StartsWith("refs/heads"))
{
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The specified branch name '{0}' appears to be a ref name and not a branch name because it starts with the string 'refs/heads'. Either specify just the branch name or use the Create method if you need to specify the full ref name", 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));
}
}
}
+1
View File
@@ -437,6 +437,7 @@
<Compile Include="Models\Response\Enterprise\AdminStatsPulls.cs" />
<Compile Include="Models\Response\Enterprise\AdminStatsRepos.cs" />
<Compile Include="Models\Response\Enterprise\AdminStatsUsers.cs" />
<Compile Include="Helpers\ReferenceExtensions.cs" />
<Compile Include="Clients\Enterprise\EnterpriseLicenseClient.cs" />
<Compile Include="Clients\Enterprise\IEnterpriseLicenseClient.cs" />
<Compile Include="Models\Response\Enterprise\LicenseInfo.cs" />
+1
View File
@@ -450,6 +450,7 @@
<Compile Include="Models\Request\NewOrganization.cs" />
<Compile Include="Clients\IUserAdministrationClient.cs" />
<Compile Include="Clients\UserAdministrationClient.cs" />
<Compile Include="Helpers\ReferenceExtensions.cs" />
<Compile Include="Clients\Enterprise\EnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\IEnterpriseSearchIndexingClient.cs" />
<Compile Include="Models\Request\Enterprise\NewOrganization.cs" />
+1
View File
@@ -446,6 +446,7 @@
<Compile Include="Models\Request\NewOrganization.cs" />
<Compile Include="Clients\IUserAdministrationClient.cs" />
<Compile Include="Clients\UserAdministrationClient.cs" />
<Compile Include="Helpers\ReferenceExtensions.cs" />
<Compile Include="Clients\Enterprise\EnterpriseSearchIndexingClient.cs" />
<Compile Include="Clients\Enterprise\IEnterpriseSearchIndexingClient.cs" />
<Compile Include="Models\Request\Enterprise\NewOrganization.cs" />
+1
View File
@@ -434,6 +434,7 @@
<Compile Include="Models\Response\Enterprise\AdminStatsPulls.cs" />
<Compile Include="Models\Response\Enterprise\AdminStatsRepos.cs" />
<Compile Include="Models\Response\Enterprise\AdminStatsUsers.cs" />
<Compile Include="Helpers\ReferenceExtensions.cs" />
<Compile Include="Clients\Enterprise\EnterpriseLicenseClient.cs" />
<Compile Include="Clients\Enterprise\IEnterpriseLicenseClient.cs" />
<Compile Include="Models\Response\Enterprise\LicenseInfo.cs" />
+1
View File
@@ -441,6 +441,7 @@
<Compile Include="Models\Response\Enterprise\AdminStatsPulls.cs" />
<Compile Include="Models\Response\Enterprise\AdminStatsRepos.cs" />
<Compile Include="Models\Response\Enterprise\AdminStatsUsers.cs" />
<Compile Include="Helpers\ReferenceExtensions.cs" />
<Compile Include="Clients\Enterprise\EnterpriseLicenseClient.cs" />
<Compile Include="Clients\Enterprise\IEnterpriseLicenseClient.cs" />
<Compile Include="Models\Response\Enterprise\LicenseInfo.cs" />
+2 -1
View File
@@ -103,6 +103,7 @@
<Compile Include="Helpers\ConcurrentCache.cs" />
<Compile Include="Helpers\HttpClientExtensions.cs" />
<Compile Include="Helpers\PropertyOrField.cs" />
<Compile Include="Helpers\ReferenceExtensions.cs" />
<Compile Include="Helpers\SerializeNullAttribute.cs" />
<Compile Include="Helpers\WebHookConfigComparer.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
@@ -483,4 +484,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>