Skip to content

Commit

Permalink
Merge pull request #1103 from M-Zuber/CreateBranchHelperMethod
Browse files Browse the repository at this point in the history
Create branch helper method
  • Loading branch information
haacked committed Feb 18, 2016
2 parents ee71652 + 8c12046 commit e41c32d
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 1 deletion.
32 changes: 32 additions & 0 deletions Octokit.Tests.Integration/Helpers/ReferenceExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
57 changes: 57 additions & 0 deletions Octokit/Helpers/ReferenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-netcore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
3 changes: 2 additions & 1 deletion Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -483,4 +484,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

0 comments on commit e41c32d

Please sign in to comment.