-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1103 from M-Zuber/CreateBranchHelperMethod
Create branch helper method
- Loading branch information
Showing
9 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Octokit.Tests.Integration/Helpers/ReferenceExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters