-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Gérald Barré <[email protected]> Co-authored-by: Louis Zanella <[email protected]>
- Loading branch information
1 parent
c08a5d0
commit b52d08d
Showing
12 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
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,66 @@ | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
using NGitLab.Tests.Docker; | ||
using NUnit.Framework; | ||
|
||
namespace NGitLab.Tests | ||
{ | ||
public class ProtectedBranchTests | ||
{ | ||
[Test] | ||
public async Task ProtectBranch_Test() | ||
{ | ||
using var context = await GitLabTestContext.CreateAsync(); | ||
var project = context.CreateProject(initializeWithCommits: true); | ||
var branchClient = context.Client.GetRepository(project.Id).Branches; | ||
var branch = branchClient.Create(new BranchCreate() { Name = "protectedBranch", Ref = project.DefaultBranch }); | ||
var protectedBranchClient = context.Client.GetProtectedBranchClient(project.Id); | ||
var branchProtect = new BranchProtect(branch.Name) | ||
{ | ||
PushAccessLevel = AccessLevel.Maintainer, | ||
MergeAccessLevel = AccessLevel.NoAccess, | ||
AllowForcePush = true, | ||
AllowedToPush = new AccessLevelInfo[] | ||
{ | ||
new AccessLevelInfo() | ||
{ | ||
AccessLevel = AccessLevel.Admin, | ||
Description = "Admin", | ||
}, | ||
}, | ||
AllowedToUnprotect = new AccessLevelInfo[] | ||
{ | ||
new AccessLevelInfo() | ||
{ | ||
AccessLevel = AccessLevel.NoAccess, | ||
Description = "Example", | ||
}, | ||
}, | ||
}; | ||
|
||
// Protect branch | ||
ProtectedBranchAndBranchProtectAreEquals(branchProtect, protectedBranchClient.ProtectBranch(branchProtect)); | ||
|
||
// Get branch | ||
ProtectedBranchAndBranchProtectAreEquals(branchProtect, protectedBranchClient.GetProtectedBranch(branch.Name)); | ||
|
||
// Get branches | ||
Assert.IsNotEmpty(protectedBranchClient.GetProtectedBranches()); | ||
var protectedBranches = protectedBranchClient.GetProtectedBranches(branch.Name); | ||
Assert.IsNotEmpty(protectedBranches); | ||
ProtectedBranchAndBranchProtectAreEquals(branchProtect, protectedBranches[0]); | ||
|
||
// Unprotect branch | ||
protectedBranchClient.UnprotectBranch(branch.Name); | ||
Assert.IsEmpty(protectedBranchClient.GetProtectedBranches(branch.Name)); | ||
} | ||
|
||
private void ProtectedBranchAndBranchProtectAreEquals(BranchProtect branchProtect, ProtectedBranch protectedBranch) | ||
{ | ||
Assert.AreEqual(branchProtect.BranchName, protectedBranch.Name); | ||
Assert.AreEqual(branchProtect.PushAccessLevel, protectedBranch.PushAccessLevels[0].AccessLevel); | ||
Assert.AreEqual(branchProtect.MergeAccessLevel, protectedBranch.MergeAccessLevels[0].AccessLevel); | ||
Assert.AreEqual(branchProtect.AllowForcePush, protectedBranch.AllowForcePush); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -17,4 +17,4 @@ public interface IBranchClient | |
|
||
void Delete(string name); | ||
} | ||
} | ||
} |
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,15 @@ | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab | ||
{ | ||
public interface IProtectedBranchClient | ||
{ | ||
ProtectedBranch ProtectBranch(BranchProtect branchProtect); | ||
|
||
void UnprotectBranch(string branchName); | ||
|
||
ProtectedBranch GetProtectedBranch(string branchName); | ||
|
||
ProtectedBranch[] GetProtectedBranches(string search = null); | ||
} | ||
} |
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,31 @@ | ||
using System; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Impl | ||
{ | ||
internal sealed class ProtectedBranchClient : IProtectedBranchClient | ||
{ | ||
private readonly API _api; | ||
private readonly int _projectId; | ||
private readonly string _protectedBranchesUrl; | ||
|
||
public ProtectedBranchClient(API api, int projectId) | ||
{ | ||
_api = api; | ||
_projectId = projectId; | ||
_protectedBranchesUrl = $"{Project.Url}/{_projectId}/protected_branches"; | ||
} | ||
|
||
public ProtectedBranch ProtectBranch(BranchProtect branchProtect) | ||
=> _api.Post().With(branchProtect).To<ProtectedBranch>(_protectedBranchesUrl); | ||
|
||
public void UnprotectBranch(string branchName) | ||
=> _api.Delete().Execute($"{_protectedBranchesUrl}/{Uri.EscapeDataString(branchName)}"); | ||
|
||
public ProtectedBranch GetProtectedBranch(string branchName) | ||
=> _api.Get().To<ProtectedBranch>($"{_protectedBranchesUrl}/{Uri.EscapeDataString(branchName)}"); | ||
|
||
public ProtectedBranch[] GetProtectedBranches(string search = null) | ||
=> _api.Get().To<ProtectedBranch[]>(Utils.AddParameter(_protectedBranchesUrl, "search", search)); | ||
} | ||
} |
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,14 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace NGitLab.Models | ||
{ | ||
[DataContract] | ||
public class AccessLevelInfo | ||
{ | ||
[DataMember(Name = "access_level")] | ||
public AccessLevel AccessLevel { get; set; } | ||
|
||
[DataMember(Name = "access_level_description")] | ||
public string Description { get; set; } | ||
} | ||
} |
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,40 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace NGitLab.Models | ||
{ | ||
[DataContract] | ||
public class BranchProtect | ||
{ | ||
public BranchProtect(string branchName) | ||
{ | ||
BranchName = branchName; | ||
} | ||
|
||
[DataMember(Name = "name")] | ||
public string BranchName { get; set; } | ||
|
||
[DataMember(Name = "push_access_level")] | ||
public AccessLevel? PushAccessLevel { get; set; } = null; | ||
|
||
[DataMember(Name = "merge_access_level")] | ||
public AccessLevel? MergeAccessLevel { get; set; } = null; | ||
|
||
[DataMember(Name = "unprotect_access_level")] | ||
public AccessLevel? UnprotectAccessLevel { get; set; } = null; | ||
|
||
[DataMember(Name = "allow_force_push")] | ||
public bool AllowForcePush { get; set; } = false; | ||
|
||
[DataMember(Name = "allowed_to_merge")] | ||
public AccessLevelInfo[] AllowedToMerge { get; set; } | ||
|
||
[DataMember(Name = "allowed_to_push")] | ||
public AccessLevelInfo[] AllowedToPush { get; set; } | ||
|
||
[DataMember(Name = "allowed_to_unprotect")] | ||
public AccessLevelInfo[] AllowedToUnprotect { get; set; } | ||
|
||
[DataMember(Name = "code_owner_approval_required")] | ||
public bool CodeOwnerApprovalRequired { get; set; } = false; | ||
} | ||
} |
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,26 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace NGitLab.Models | ||
{ | ||
[DataContract] | ||
public class ProtectedBranch | ||
{ | ||
[DataMember(Name = "id")] | ||
public long Id { get; set; } | ||
|
||
[DataMember(Name = "name")] | ||
public string Name { get; set; } | ||
|
||
[DataMember(Name = "push_access_levels")] | ||
public AccessLevelInfo[] PushAccessLevels { get; set; } | ||
|
||
[DataMember(Name = "merge_access_levels")] | ||
public AccessLevelInfo[] MergeAccessLevels { get; set; } | ||
|
||
[DataMember(Name = "allow_force_push")] | ||
public bool AllowForcePush { get; set; } | ||
|
||
[DataMember(Name = "code_owner_approval_required")] | ||
public bool CodeOwnerApprovalRequired { get; set; } | ||
} | ||
} |