Skip to content

Commit f0cd7c3

Browse files
committed
Add the PostBranchAsync, unit tests over PostBranch and PostBranchAsync, then complete the Branch POCO based on unit tests data.
1 parent 92bf9da commit f0cd7c3

9 files changed

Lines changed: 154 additions & 22 deletions

File tree

SharpBucket/V2/EndPoints/BranchResource.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using SharpBucket.Utility;
2-
using SharpBucket.V2.Pocos;
1+
using SharpBucket.V2.Pocos;
32
using System;
43
using System.Collections.Generic;
54
using System.Threading;
@@ -42,16 +41,6 @@ public List<Branch> ListBranches(ListParameters parameters)
4241
return GetPaginatedValues<Branch>(_baseUrl, parameters.Max, parameters.ToDictionary());
4342
}
4443

45-
/// <summary>
46-
/// Creates a new branch in the specified repository.
47-
/// </summary>
48-
/// <param name="branch">The branch to create.</param>
49-
/// <returns>The created branch.</returns>
50-
public Branch PostBranch(Branch branch)
51-
{
52-
return _sharpBucketV2.Post(branch, _baseUrl);
53-
}
54-
5544
/// <summary>
5645
/// Enumerate branches associated with a specific repository.
5746
/// </summary>
@@ -90,6 +79,27 @@ public IAsyncEnumerable<Branch> EnumerateBranchesAsync(EnumerateParameters param
9079
}
9180
#endif
9281

82+
/// <summary>
83+
/// Creates a new branch in the specified repository.
84+
/// </summary>
85+
/// <param name="branch">The branch to create.</param>
86+
/// <returns>The created branch.</returns>
87+
public Branch PostBranch(Branch branch)
88+
{
89+
return _sharpBucketV2.Post(branch, _baseUrl);
90+
}
91+
92+
/// <summary>
93+
/// Creates a new branch in the specified repository.
94+
/// </summary>
95+
/// <param name="branch">The branch to create.</param>
96+
/// <param name="token">The cancellation token</param>
97+
/// <returns>The created branch.</returns>
98+
public Task<Branch> PostBranchAsync(Branch branch, CancellationToken token = default)
99+
{
100+
return _sharpBucketV2.PostAsync(branch, _baseUrl, token);
101+
}
102+
93103
/// <summary>
94104
/// Removes a branch.
95105
/// </summary>

SharpBucket/V2/Pocos/Branch.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public class Branch
44
{
55
public string name { get; set; }
6-
public BranchTarget target { get; set; }
6+
public Commit target { get; set; }
7+
public MergeStrategy default_merge_strategy { get; set; }
8+
public BranchLinks links { get; set; }
79
}
810
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SharpBucket.V2.Pocos
2+
{
3+
public class BranchLinks
4+
{
5+
public Link commits { get; set; }
6+
public Link html { get; set; }
7+
public Link self { get; set; }
8+
}
9+
}

SharpBucket/V2/Pocos/BranchTarget.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SharpBucket.V2.Pocos
2+
{
3+
public enum MergeStrategy
4+
{
5+
merge_commit,
6+
squash,
7+
fast_forward
8+
}
9+
}

SharpBucketTests/V2/EndPoints/BranchResourceTests.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using NUnit.Framework;
33
using Shouldly;
44
using System.Linq;
5+
using SharpBucket.V2.Pocos;
6+
using SharpBucketTests.V2.Pocos;
57

68
namespace SharpBucketTests.V2.EndPoints
79
{
@@ -53,7 +55,55 @@ public async Task EnumerateBranchesAsync_NotEmptyRepository_ReturnAtLeastMainBra
5355
}
5456

5557
[Test]
56-
public void DeleteBranch_ExistingBranch_BrancCouldNotBeListedAnymore()
58+
public void PostBranch_NewBranch_BranchIsCreated()
59+
{
60+
var branchResource = SampleRepositories.TestRepository.RepositoryResource.BranchesResource;
61+
var initialBranches = branchResource.ListBranches();
62+
var newBranch = new Branch
63+
{
64+
name = "newPostedBranch",
65+
target = new Commit
66+
{
67+
hash = SampleRepositories.TestRepository.RepositoryInfo.FirstCommit
68+
}
69+
};
70+
71+
var createdBranch = branchResource.PostBranch(newBranch);
72+
73+
createdBranch.ShouldBeFilled();
74+
createdBranch.name.ShouldBe(newBranch.name);
75+
createdBranch.target.hash.ShouldBe(newBranch.target.hash);
76+
77+
var currentBranches = branchResource.ListBranches();
78+
currentBranches.Count.ShouldBe(initialBranches.Count + 1);
79+
}
80+
81+
[Test]
82+
public void PostBranchAsync_NewBranch_BranchIsCreated()
83+
{
84+
var branchResource = SampleRepositories.TestRepository.RepositoryResource.BranchesResource;
85+
var initialBranches = branchResource.ListBranches();
86+
var newBranch = new Branch
87+
{
88+
name = "newPostedBranchAsync",
89+
target = new Commit
90+
{
91+
hash = SampleRepositories.TestRepository.RepositoryInfo.FirstCommit
92+
}
93+
};
94+
95+
var createdBranch = branchResource.PostBranch(newBranch);
96+
97+
createdBranch.ShouldBeFilled();
98+
createdBranch.name.ShouldBe(newBranch.name);
99+
createdBranch.target.hash.ShouldBe(newBranch.target.hash);
100+
101+
var currentBranches = branchResource.ListBranches();
102+
currentBranches.Count.ShouldBe(initialBranches.Count + 1);
103+
}
104+
105+
[Test]
106+
public void DeleteBranch_ExistingBranch_BranchCouldNotBeListedAnymore()
57107
{
58108
var branchResource = SampleRepositories.TestRepository.RepositoryResource.BranchesResource;
59109
var initialBranches = branchResource.ListBranches();
@@ -65,7 +115,7 @@ public void DeleteBranch_ExistingBranch_BrancCouldNotBeListedAnymore()
65115
}
66116

67117
[Test]
68-
public async Task DeleteBranchAsync_ExistingBranch_BrancCouldNotBeListedAnymore()
118+
public async Task DeleteBranchAsync_ExistingBranch_BranchCouldNotBeListedAnymore()
69119
{
70120
var branchResource = SampleRepositories.TestRepository.RepositoryResource.BranchesResource;
71121
var initialBranches = branchResource.ListBranches();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SharpBucket.V2.Pocos;
2+
using Shouldly;
3+
4+
namespace SharpBucketTests.V2.Pocos
5+
{
6+
public static class AuthorAssertions
7+
{
8+
public static Author ShouldBeFilled(this Author author)
9+
{
10+
author.ShouldNotBeNull();
11+
author.raw.ShouldNotBeNullOrEmpty();
12+
////author.user is null if raw do not match a known user of bitbucket
13+
14+
return author;
15+
}
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using SharpBucket.V2.Pocos;
2+
using Shouldly;
3+
4+
namespace SharpBucketTests.V2.Pocos
5+
{
6+
public static class BranchAssertions
7+
{
8+
public static Branch ShouldBeFilled(this Branch branch)
9+
{
10+
branch.ShouldNotBeNull();
11+
branch.name.ShouldNotBeNullOrEmpty();
12+
branch.target.ShouldBeFilled();
13+
branch.links.ShouldNotBeNull();
14+
branch.links.commits.ShouldBeFilled();
15+
branch.links.html.ShouldBeFilled();
16+
branch.links.self.ShouldBeFilled();
17+
18+
return branch;
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using SharpBucket.V2.Pocos;
2+
using Shouldly;
3+
4+
namespace SharpBucketTests.V2.Pocos
5+
{
6+
public static class CommitAssertions
7+
{
8+
public static Commit ShouldBeFilled(this Commit commit)
9+
{
10+
commit.ShouldNotBeNull();
11+
commit.author.ShouldBeFilled();
12+
commit.date.ShouldNotBeNullOrEmpty();
13+
commit.hash.ShouldNotBeNull();
14+
commit.message.ShouldNotBeNullOrEmpty();
15+
////commit.parents could be null for initial commit
16+
////commit.repository.ShouldBeFilled(); at least in Branch POCO it seems that this should be just a RepositoryInfo. TODO must verify in other contexts before changing type.
17+
18+
return commit;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)