-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support to Group hooks - Closes #654 * Unit test the group hooks client * Mock group hooks client * Refactors based on review comments * Set nullable properties in GroupHookUpsert Review refactor --------- Co-authored-by: Prashanthi Ramesh <prashanthi.ramesh@ubisoft.com>
- Loading branch information
1 parent
0403aa7
commit b911b96
Showing
15 changed files
with
583 additions
and
1 deletion.
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
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,109 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Mock.Clients; | ||
|
||
internal sealed class GroupHooksClient : ClientBase, IGroupHooksClient | ||
{ | ||
public int _groupId { get; } | ||
|
||
public GroupHooksClient(ClientContext context, GroupId groupId) | ||
: base(context) | ||
{ | ||
_groupId = Server.AllGroups.FindGroup(groupId.ValueAsUriParameter()).Id; | ||
} | ||
|
||
public IEnumerable<Models.GroupHook> All | ||
{ | ||
get | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var hooks = GetGroup(_groupId, GroupPermission.Edit).Hooks; | ||
return ToClientGroupHooks(hooks).ToList(); | ||
} | ||
} | ||
} | ||
|
||
public Models.GroupHook this[int hookId] | ||
{ | ||
get | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var hook = All.FirstOrDefault(h => h.Id == hookId) ?? throw new GitLabNotFoundException(); | ||
return hook; | ||
} | ||
} | ||
} | ||
|
||
public Models.GroupHook Create(GroupHookUpsert hook) | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var groupHook = UpsertToHook(hook); | ||
|
||
GetGroup(_groupId, GroupPermission.Edit).Hooks.Add(groupHook); | ||
return groupHook.ToClientGroupHook(); | ||
} | ||
} | ||
|
||
public Models.GroupHook Update(int hookId, GroupHookUpsert hook) | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var currentHook = GetGroup(_groupId, GroupPermission.Edit).Hooks.FirstOrDefault(h => h.Id == hookId) ?? throw new GitLabNotFoundException(); | ||
|
||
currentHook.Url = hook.Url; | ||
currentHook.PushEvents = hook.PushEvents ?? false; | ||
currentHook.MergeRequestsEvents = hook.MergeRequestsEvents ?? false; | ||
currentHook.IssuesEvents = hook.IssuesEvents ?? false; | ||
currentHook.TagPushEvents = hook.TagPushEvents ?? false; | ||
currentHook.NoteEvents = hook.NoteEvents ?? false; | ||
currentHook.JobEvents = hook.JobEvents ?? false; | ||
currentHook.PipelineEvents = hook.PipelineEvents ?? false; | ||
currentHook.WikiPagesEvents = hook.WikiPagesEvents ?? false; | ||
currentHook.EnableSslVerification = hook.EnableSslVerification ?? false; | ||
currentHook.Token = currentHook.Token; | ||
|
||
return currentHook.ToClientGroupHook(); | ||
} | ||
} | ||
|
||
public void Delete(int hookId) | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var groupHooks = GetGroup(_groupId, GroupPermission.Edit).Hooks; | ||
var hook = groupHooks.FirstOrDefault(h => h.Id == hookId) ?? throw new GitLabNotFoundException(); | ||
|
||
groupHooks.Remove(hook); | ||
} | ||
} | ||
|
||
private static IEnumerable<Models.GroupHook> ToClientGroupHooks(IEnumerable<GroupHook> hooks) | ||
{ | ||
return hooks.Select(hook => hook.ToClientGroupHook()); | ||
} | ||
|
||
private static GroupHook UpsertToHook(GroupHookUpsert hook) | ||
{ | ||
var hookFromUpsert = new GroupHook | ||
{ | ||
Url = hook.Url, | ||
PushEvents = hook.PushEvents ?? false, | ||
MergeRequestsEvents = hook.MergeRequestsEvents ?? false, | ||
IssuesEvents = hook.IssuesEvents ?? false, | ||
TagPushEvents = hook.TagPushEvents ?? false, | ||
NoteEvents = hook.NoteEvents ?? false, | ||
JobEvents = hook.JobEvents ?? false, | ||
PipelineEvents = hook.PipelineEvents ?? false, | ||
WikiPagesEvents = hook.WikiPagesEvents ?? false, | ||
EnableSslVerification = hook.EnableSslVerification ?? false, | ||
Token = hook.Token, | ||
}; | ||
|
||
return hookFromUpsert; | ||
} | ||
} |
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,55 @@ | ||
using System; | ||
|
||
namespace NGitLab.Mock; | ||
|
||
public sealed class GroupHook : GitLabObject | ||
{ | ||
public new Group Parent => (Group)base.Parent; | ||
|
||
public int Id { get; internal set; } | ||
|
||
public Uri Url { get; set; } | ||
|
||
public DateTime CreatedAt { get; set; } | ||
|
||
public bool PushEvents { get; set; } | ||
|
||
public bool MergeRequestsEvents { get; set; } | ||
|
||
public bool IssuesEvents { get; set; } | ||
|
||
public bool TagPushEvents { get; set; } | ||
|
||
public bool NoteEvents { get; set; } | ||
|
||
public bool JobEvents { get; set; } | ||
|
||
public bool PipelineEvents { get; set; } | ||
|
||
public bool WikiPagesEvents { get; set; } | ||
|
||
public bool EnableSslVerification { get; set; } | ||
|
||
public string Token { get; set; } | ||
|
||
public Models.GroupHook ToClientGroupHook() | ||
{ | ||
return new Models.GroupHook | ||
{ | ||
Id = Id, | ||
Url = Url, | ||
GroupId = Parent.Id, | ||
CreatedAt = CreatedAt, | ||
PushEvents = PushEvents, | ||
MergeRequestsEvents = MergeRequestsEvents, | ||
IssuesEvents = IssuesEvents, | ||
TagPushEvents = TagPushEvents, | ||
NoteEvents = NoteEvents, | ||
JobEvents = JobEvents, | ||
PipelineEvents = PipelineEvents, | ||
WikiPagesEvents = WikiPagesEvents, | ||
EnableSslVerification = EnableSslVerification, | ||
Token = Token, | ||
}; | ||
} | ||
} |
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,30 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace NGitLab.Mock; | ||
|
||
public class GroupHookCollection : Collection<GroupHook> | ||
{ | ||
public GroupHookCollection(GitLabObject container) | ||
: base(container) | ||
{ | ||
} | ||
|
||
public override void Add(GroupHook item) | ||
{ | ||
if (item is null) | ||
throw new ArgumentNullException(nameof(item)); | ||
|
||
if (item.Id == default) | ||
{ | ||
item.Id = GetNewId(); | ||
} | ||
|
||
base.Add(item); | ||
} | ||
|
||
private int GetNewId() | ||
{ | ||
return this.Select(hook => hook.Id).DefaultIfEmpty().Max() + 1; | ||
} | ||
} |
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
Oops, something went wrong.