Skip to content

Commit 3a47462

Browse files
dnaglnmklotas
authored andcommitted
Feature/protected branches (#40)
* Added Branch response * Added Request for creating a branch * Renamed CreateBranch class to CreateBranchRequest * Added Guard to constructor * Added DeleteBranchRequest class * Added DeleteMergedBranchesRequest * Made DeleteMergedBranchesRequest public sealed * Added BranchQueryOptions class * Added BranchQueryBuilder * Updated classname for CreateBranchRequest * Changed CreateBranchRequest Property 'Id' to 'ProjectId' * Changed property names for DeleteBranchRequest class * Changed property names for DeleteMergedBranchesRequest class * Added BranchClient * Fixed tests * Fixed XML cref warning * Added models for protected branches * Added ProtectBranchRequest * Extended BranchClient class to protect/unprotect branches * Fixed tests - syntax errors
1 parent 96426b6 commit 3a47462

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

src/GitLabApiClient/BranchClient.cs

+11
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,16 @@ public async Task DeleteBranch(DeleteBranchRequest request) =>
4343
public async Task DeleteMergedBranches(DeleteMergedBranchesRequest request) =>
4444
await _httpFacade.Delete($"projects/{request.ProjectId}/repository/merged_branches");
4545

46+
public async Task<ProtectedBranch> GetProtectedBranchesAsync(string projectId, string branchName) =>
47+
await _httpFacade.Get<ProtectedBranch>($"projects/{projectId}/protected_branches/{branchName}");
48+
49+
public async Task<IList<ProtectedBranch>> GetProtectedBranchesAsync(string projectId) =>
50+
await _httpFacade.GetPagedList<ProtectedBranch>($"projects/{projectId}/protected_branches");
51+
52+
public async Task<ProtectedBranch> ProtectBranchAsync(ProtectBranchRequest request) =>
53+
await _httpFacade.Post<ProtectedBranch>($"projects/{request.ProjectId}/protected_branches", request);
54+
55+
public async Task UnprotectBranchAsync(string projectId, string branchName) =>
56+
await _httpFacade.Delete($"projects/{projectId}/protected_branches/{branchName}");
4657
}
4758
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using GitLabApiClient.Internal.Utilities;
5+
using GitLabApiClient.Models.Branches.Responses;
6+
using Newtonsoft.Json;
7+
8+
namespace GitLabApiClient.Models.Branches.Requests
9+
{
10+
/// <summary>
11+
/// Protects a branch.
12+
/// </summary>
13+
public sealed class ProtectBranchRequest
14+
{
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="ProtectBranchRequest"/> class.
18+
/// </summary>
19+
/// <param name="projectId">ID or URL-Encoded path of the project.</param>
20+
/// <param name="name">The name of the branch or wildcard.</param>
21+
/// <param name="pushAccessLevel">Access levels allowed to push.</param>
22+
/// <param name="mergeAccessLevel">Access levels allowed to merge.</param>
23+
/// <param name="unprotectAccessLevel">Access levels allowed to unprotect.</param>
24+
public ProtectBranchRequest(
25+
string projectId,
26+
string name,
27+
ProtectedRefAccessLevels? pushAccessLevel = ProtectedRefAccessLevels.MAINTAINER_ACCESS,
28+
ProtectedRefAccessLevels? mergeAccessLevel = ProtectedRefAccessLevels.MAINTAINER_ACCESS,
29+
ProtectedRefAccessLevels? unprotectAccessLevel = ProtectedRefAccessLevels.MAINTAINER_ACCESS)
30+
{
31+
Guard.NotEmpty(projectId, nameof(projectId));
32+
Guard.NotEmpty(name, nameof(name));
33+
34+
PushAccessLevel = pushAccessLevel.ToString();
35+
MergeAccessLevel = mergeAccessLevel.ToString();
36+
UnprotectAccessLevel = unprotectAccessLevel.ToString();
37+
}
38+
39+
/// <summary>
40+
/// Id or URL-Encoded path of the project.
41+
/// </summary>
42+
[JsonProperty("id")]
43+
public string ProjectId { get; set; }
44+
45+
/// <summary>
46+
/// The name of the branch or wildcard.
47+
/// </summary>
48+
[JsonProperty("name")]
49+
public string Name { get; set; }
50+
51+
/// <summary>
52+
/// Access levels allowed to push (defaults: 40, maintainer access level).
53+
/// </summary>
54+
[JsonProperty("push_access_level")]
55+
public string PushAccessLevel { get; set; }
56+
57+
/// <summary>
58+
/// Access levels allowed to merge (defaults: 40, maintainer access level).
59+
/// </summary>
60+
[JsonProperty("merge_access_level")]
61+
public string MergeAccessLevel { get; set; }
62+
63+
/// <summary>
64+
/// Access levels allowed to unprotect (defaults: 40, maintainer access level).
65+
/// </summary>
66+
[JsonProperty("unprotect_access_level")]
67+
public string UnprotectAccessLevel { get; set; }
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Newtonsoft.Json;
5+
6+
namespace GitLabApiClient.Models.Branches.Responses
7+
{
8+
public sealed class ProtectedBranch
9+
{
10+
[JsonProperty("name")]
11+
public string Name { get; set; }
12+
13+
[JsonProperty("push_access_levels")]
14+
public PushAccessLevel[] PushAccessLevels { get; set; }
15+
16+
[JsonProperty("merge_access_levels")]
17+
public PushAccessLevel[] MergeAccessLevels { get; set; }
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace GitLabApiClient.Models.Branches.Responses
6+
{
7+
public enum ProtectedRefAccessLevels
8+
{
9+
NO_ACCESS = 0,
10+
DEVELOPER_ACCESS = 30,
11+
MAINTAINER_ACCESS = 40,
12+
ADMIN_ACCESS = 60
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Newtonsoft.Json;
5+
6+
namespace GitLabApiClient.Models.Branches.Responses
7+
{
8+
public sealed class PushAccessLevel
9+
{
10+
[JsonProperty("access_level")]
11+
public ProtectedRefAccessLevels AccessLevel { get; set; }
12+
13+
[JsonProperty("access_level_description")]
14+
public string AccessLevelDescription { get; set; }
15+
}
16+
}

0 commit comments

Comments
 (0)