Skip to content

Commit

Permalink
Add support to Group hooks (#661)
Browse files Browse the repository at this point in the history
* 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
PrashanthiRamesh and Prashanthi Ramesh authored Apr 24, 2024
1 parent 0403aa7 commit b911b96
Show file tree
Hide file tree
Showing 15 changed files with 583 additions and 1 deletion.
69 changes: 68 additions & 1 deletion NGitLab.Mock.Tests/GroupsMockTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using NGitLab.Mock.Config;
using NGitLab.Models;
using NUnit.Framework;

namespace NGitLab.Mock.Tests;
Expand Down Expand Up @@ -339,4 +341,69 @@ public async Task Test_page_projects_in_subgroup_with_descendants()
Assert.That(total, Is.EqualTo(3));
});
}

[Test]
public void Test_create_update_delete_group_hooks()
{
// Arrange
var groupId = 1;

using var server = new GitLabConfig()
.WithUser("user1", isAdmin: true)
.WithGroup("group1", groupId)
.BuildServer();

var client = server.CreateClient("user1");
var groupHooksClient = client.GetGroupHooksClient(groupId);

var toCreateGroupHook = new GroupHookUpsert
{
Url = new Uri("https://test-create-group-hook.com"),
EnableSslVerification = true,
PushEvents = true,
};

// Act
var createdGroupHook = groupHooksClient.Create(toCreateGroupHook);

// Assert
Assert.That(groupHooksClient.All.ToArray(), Has.Length.EqualTo(1));

Assert.That(createdGroupHook.Url, Is.EqualTo(toCreateGroupHook.Url));
Assert.That(createdGroupHook.EnableSslVerification, Is.EqualTo(toCreateGroupHook.EnableSslVerification));
Assert.That(createdGroupHook.PushEvents, Is.EqualTo(toCreateGroupHook.PushEvents));

var groupHookById = groupHooksClient[createdGroupHook.Id];
Assert.That(groupHookById.Url, Is.EqualTo(toCreateGroupHook.Url));
Assert.That(groupHookById.EnableSslVerification, Is.EqualTo(toCreateGroupHook.EnableSslVerification));
Assert.That(groupHookById.PushEvents, Is.EqualTo(toCreateGroupHook.PushEvents));

// Arrange
var toUpdateGroupHook = new GroupHookUpsert
{
Url = new Uri("https://test-update-group-hook.com"),
PushEvents = false,
};

// Act
var updatedGroupHook = groupHooksClient.Update(createdGroupHook.Id, toUpdateGroupHook);

// Assert
Assert.That(groupHooksClient.All.ToArray(), Has.Length.EqualTo(1));

Assert.That(updatedGroupHook.Url, Is.EqualTo(toUpdateGroupHook.Url));
Assert.That(updatedGroupHook.PushEvents, Is.EqualTo(toUpdateGroupHook.PushEvents));
Assert.That(updatedGroupHook.EnableSslVerification, Is.False);

groupHookById = groupHooksClient[updatedGroupHook.Id];
Assert.That(groupHookById.Url, Is.EqualTo(toUpdateGroupHook.Url));
Assert.That(groupHookById.PushEvents, Is.EqualTo(toUpdateGroupHook.PushEvents));
Assert.That(groupHookById.EnableSslVerification, Is.False);

// Act
groupHooksClient.Delete(updatedGroupHook.Id);

// Assert
Assert.That(groupHooksClient.All.ToArray(), Is.Empty);
}
}
2 changes: 2 additions & 0 deletions NGitLab.Mock/Clients/GitLabClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,6 @@ public IProtectedTagClient GetProtectedTagClient(ProjectId projectId)
public ISearchClient GetProjectSearchClient(int projectId) => GetProjectSearchClient((long)projectId);

public ISearchClient GetProjectSearchClient(ProjectId projectId) => new ProjectSearchClient(Context, projectId);

public IGroupHooksClient GetGroupHooksClient(GroupId groupId) => new GroupHooksClient(Context, groupId);
}
109 changes: 109 additions & 0 deletions NGitLab.Mock/Clients/GroupHooksClient.cs
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;
}
}
3 changes: 3 additions & 0 deletions NGitLab.Mock/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Group(string name)
Badges = new BadgeCollection(this);
Labels = new LabelsCollection(this);
Milestones = new MilestoneCollection(this);
Hooks = new GroupHookCollection(this);
Name = name;
}

Expand Down Expand Up @@ -76,6 +77,8 @@ public string Name

public MilestoneCollection Milestones { get; }

public GroupHookCollection Hooks { get; }

public IEnumerable<MergeRequest> MergeRequests => AllProjects.SelectMany(project => project.MergeRequests);

public string Path
Expand Down
55 changes: 55 additions & 0 deletions NGitLab.Mock/GroupHook.cs
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,
};
}
}
30 changes: 30 additions & 0 deletions NGitLab.Mock/GroupHookCollection.cs
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;
}
}
33 changes: 33 additions & 0 deletions NGitLab.Mock/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ NGitLab.Mock.Group.Group() -> void
NGitLab.Mock.Group.Group(NGitLab.Mock.User user) -> void
NGitLab.Mock.Group.Group(string name) -> void
NGitLab.Mock.Group.Groups.get -> NGitLab.Mock.GroupCollection
NGitLab.Mock.Group.Hooks.get -> NGitLab.Mock.GroupHookCollection
NGitLab.Mock.Group.Id.get -> int
NGitLab.Mock.Group.Id.set -> void
NGitLab.Mock.Group.IsUserNamespace.get -> bool
Expand Down Expand Up @@ -476,6 +477,37 @@ NGitLab.Mock.Group.Visibility.set -> void
NGitLab.Mock.GroupCollection
NGitLab.Mock.GroupCollection.GroupCollection(NGitLab.Mock.GitLabObject container) -> void
NGitLab.Mock.GroupExtensions
NGitLab.Mock.GroupHook
NGitLab.Mock.GroupHook.CreatedAt.get -> System.DateTime
NGitLab.Mock.GroupHook.CreatedAt.set -> void
NGitLab.Mock.GroupHook.EnableSslVerification.get -> bool
NGitLab.Mock.GroupHook.EnableSslVerification.set -> void
NGitLab.Mock.GroupHook.GroupHook() -> void
NGitLab.Mock.GroupHook.Id.get -> int
NGitLab.Mock.GroupHook.IssuesEvents.get -> bool
NGitLab.Mock.GroupHook.IssuesEvents.set -> void
NGitLab.Mock.GroupHook.JobEvents.get -> bool
NGitLab.Mock.GroupHook.JobEvents.set -> void
NGitLab.Mock.GroupHook.MergeRequestsEvents.get -> bool
NGitLab.Mock.GroupHook.MergeRequestsEvents.set -> void
NGitLab.Mock.GroupHook.NoteEvents.get -> bool
NGitLab.Mock.GroupHook.NoteEvents.set -> void
NGitLab.Mock.GroupHook.Parent.get -> NGitLab.Mock.Group
NGitLab.Mock.GroupHook.PipelineEvents.get -> bool
NGitLab.Mock.GroupHook.PipelineEvents.set -> void
NGitLab.Mock.GroupHook.PushEvents.get -> bool
NGitLab.Mock.GroupHook.PushEvents.set -> void
NGitLab.Mock.GroupHook.TagPushEvents.get -> bool
NGitLab.Mock.GroupHook.TagPushEvents.set -> void
NGitLab.Mock.GroupHook.ToClientGroupHook() -> NGitLab.Models.GroupHook
NGitLab.Mock.GroupHook.Token.get -> string
NGitLab.Mock.GroupHook.Token.set -> void
NGitLab.Mock.GroupHook.Url.get -> System.Uri
NGitLab.Mock.GroupHook.Url.set -> void
NGitLab.Mock.GroupHook.WikiPagesEvents.get -> bool
NGitLab.Mock.GroupHook.WikiPagesEvents.set -> void
NGitLab.Mock.GroupHookCollection
NGitLab.Mock.GroupHookCollection.GroupHookCollection(NGitLab.Mock.GitLabObject container) -> void
NGitLab.Mock.Issue
NGitLab.Mock.Issue.Assignee.get -> NGitLab.Mock.UserRef
NGitLab.Mock.Issue.Assignee.set -> void
Expand Down Expand Up @@ -1193,6 +1225,7 @@ override NGitLab.Mock.EventCollection.Add(NGitLab.Mock.Event item) -> void
override NGitLab.Mock.Config.GitLabCollection<TItem>.InsertItem(int index, TItem item) -> void
override NGitLab.Mock.Config.GitLabCollection<TItem>.SetItem(int index, TItem item) -> void
override NGitLab.Mock.GroupCollection.Add(NGitLab.Mock.Group group) -> void
override NGitLab.Mock.GroupHookCollection.Add(NGitLab.Mock.GroupHook item) -> void
override NGitLab.Mock.IssueCollection.Add(NGitLab.Mock.Issue item) -> void
override NGitLab.Mock.JobCollection.Add(NGitLab.Mock.Job job) -> void
override NGitLab.Mock.LabelsCollection.Add(NGitLab.Mock.Label label) -> void
Expand Down
Loading

0 comments on commit b911b96

Please sign in to comment.