Skip to content

Commit 89a5807

Browse files
authored
Add EventClient Mock (#37)
1 parent e921e22 commit 89a5807

File tree

6 files changed

+182
-12
lines changed

6 files changed

+182
-12
lines changed

NGitLab.Mock/Clients/EventClient.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NGitLab.Models;
4+
5+
namespace NGitLab.Mock.Clients
6+
{
7+
internal sealed class EventClient : ClientBase, IEventClient
8+
{
9+
private readonly int? _userId;
10+
private readonly int? _projectId;
11+
12+
public EventClient(ClientContext context)
13+
: base(context)
14+
{
15+
}
16+
17+
public EventClient(ClientContext context, int? userId = null, int? projectId = null)
18+
: base(context)
19+
{
20+
_userId = userId;
21+
_projectId = projectId;
22+
}
23+
24+
IEnumerable<Models.Event> IEventClient.Get(EventQuery query)
25+
{
26+
using (Context.BeginOperationScope())
27+
{
28+
return Server.Events.Get(query, _userId, _projectId).Select(e => e.ToClientEvent());
29+
}
30+
}
31+
}
32+
}

NGitLab.Mock/Clients/GitLabClient.cs

+3-12
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,11 @@ public GitLabClient(ClientContext context)
3737

3838
public IMergeRequestClient MergeRequests => new MergeRequestClient(Context);
3939

40-
public IEventClient GetEvents()
41-
{
42-
throw new System.NotImplementedException();
43-
}
40+
public IEventClient GetEvents() => new EventClient(Context);
4441

45-
public IEventClient GetUserEvents(int userId)
46-
{
47-
throw new System.NotImplementedException();
48-
}
42+
public IEventClient GetUserEvents(int userId) => new EventClient(Context, userId: userId);
4943

50-
public IEventClient GetProjectEvents(int projectId)
51-
{
52-
throw new System.NotImplementedException();
53-
}
44+
public IEventClient GetProjectEvents(int projectId) => new EventClient(Context, projectId: projectId);
5445

5546
public ICommitStatusClient GetCommitStatus(int projectId) => new CommitStatusClient(Context, projectId);
5647

NGitLab.Mock/Event.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using NGitLab.Models;
3+
4+
namespace NGitLab.Mock
5+
{
6+
public sealed class Event : GitLabObject
7+
{
8+
public int Id { get; set; }
9+
10+
public string Title { get; set; }
11+
12+
public int ProjectId { get; set; }
13+
14+
public DynamicEnum<EventAction> Action { get; set; }
15+
16+
public long? TargetId { get; set; }
17+
18+
public long? TargetIId { get; set; }
19+
20+
public DynamicEnum<EventTargetType> TargetType { get; set; }
21+
22+
public string TargetTitle { get; set; }
23+
24+
public int AuthorId { get; set; }
25+
26+
public string AuthorUserName { get; set; }
27+
28+
public DateTime CreatedAt { get; set; }
29+
30+
public Note Note { get; set; }
31+
32+
public PushData PushData { get; set; }
33+
34+
public Models.Event ToClientEvent()
35+
{
36+
return new Models.Event
37+
{
38+
Id = Id,
39+
Title = Title,
40+
ProjectId = ProjectId,
41+
Action = Action,
42+
TargetId = TargetId,
43+
TargetIId = TargetIId,
44+
TargetType = TargetType,
45+
TargetTitle = TargetTitle,
46+
AuthorId = AuthorId,
47+
AuthorUserName = AuthorUserName,
48+
CreatedAt = CreatedAt,
49+
Note = Note?.ToClientEvent(),
50+
PushData = PushData,
51+
};
52+
}
53+
}
54+
}

NGitLab.Mock/EventCollection.cs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace NGitLab.Mock
6+
{
7+
public sealed class EventCollection : Collection<Event>
8+
{
9+
public EventCollection(GitLabObject container)
10+
: base(container)
11+
{
12+
}
13+
14+
public override void Add(Event item)
15+
{
16+
if (item is null)
17+
throw new ArgumentNullException(nameof(item));
18+
19+
if (item.Id == default)
20+
{
21+
item.Id = GetNewId();
22+
}
23+
24+
base.Add(item);
25+
}
26+
27+
internal IEnumerable<Event> Get(Models.EventQuery query, int? userId, int? projectId)
28+
{
29+
var events = this.AsQueryable();
30+
31+
if (userId.HasValue)
32+
{
33+
events = events.Where(e => e.AuthorId == userId);
34+
}
35+
36+
if (projectId.HasValue)
37+
{
38+
events = events.Where(e => e.ProjectId == projectId);
39+
}
40+
41+
if (query.Action.HasValue)
42+
{
43+
events = events.Where(u => u.Action == query.Action.Value);
44+
}
45+
46+
if (query.After.HasValue)
47+
{
48+
events = events.Where(u => u.CreatedAt > query.After);
49+
}
50+
51+
if (query.Before.HasValue)
52+
{
53+
events = events.Where(u => u.CreatedAt < query.Before);
54+
}
55+
56+
if (query.Type.HasValue)
57+
{
58+
events = events.Where(u => u.TargetType == query.Type.Value);
59+
}
60+
61+
if (!string.IsNullOrEmpty(query.Sort))
62+
{
63+
var sortAsc = !string.IsNullOrEmpty(query.Sort) && string.Equals(query.Sort, "asc", StringComparison.Ordinal);
64+
events = sortAsc ? events.OrderBy(e => e.CreatedAt) : events.OrderByDescending(e => e.CreatedAt);
65+
}
66+
67+
return events;
68+
}
69+
70+
private int GetNewId()
71+
{
72+
return this.Select(evt => evt.Id).DefaultIfEmpty().Max() + 1;
73+
}
74+
}
75+
}

NGitLab.Mock/GitLabServer.cs

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public GitLabServer()
2727
Groups = new GroupCollection(this);
2828
Users = new UserCollection(this);
2929
SystemHooks = new SystemHookCollection(this);
30+
Events = new EventCollection(this);
3031
}
3132

3233
public string DefaultBranchName { get; set; } = "main";
@@ -41,6 +42,8 @@ public GitLabServer()
4142

4243
public SystemHookCollection SystemHooks { get; }
4344

45+
public EventCollection Events { get; }
46+
4447
public VisibilityLevel DefaultForkVisibilityLevel { get; set; } = VisibilityLevel.Private;
4548

4649
public IGitLabClient CreateClient(User user)

NGitLab.Mock/Note.cs

+15
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,20 @@ protected Note()
3333
public bool Resolvable { get; set; }
3434

3535
public bool Resolved { get; set; }
36+
37+
public Models.Note ToClientEvent()
38+
{
39+
return new Models.Note
40+
{
41+
Id = Id,
42+
Body = Body,
43+
CreatedAt = CreatedAt.UtcDateTime,
44+
Author = Author?.ToUserClient(),
45+
Resolvable = Resolvable,
46+
System = System,
47+
Resolved = Resolved,
48+
UpdatedAt = UpdatedAt.UtcDateTime,
49+
};
50+
}
3651
}
3752
}

0 commit comments

Comments
 (0)