File tree 6 files changed +182
-12
lines changed
6 files changed +182
-12
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -37,20 +37,11 @@ public GitLabClient(ClientContext context)
37
37
38
38
public IMergeRequestClient MergeRequests => new MergeRequestClient ( Context ) ;
39
39
40
- public IEventClient GetEvents ( )
41
- {
42
- throw new System . NotImplementedException ( ) ;
43
- }
40
+ public IEventClient GetEvents ( ) => new EventClient ( Context ) ;
44
41
45
- public IEventClient GetUserEvents ( int userId )
46
- {
47
- throw new System . NotImplementedException ( ) ;
48
- }
42
+ public IEventClient GetUserEvents ( int userId ) => new EventClient ( Context , userId : userId ) ;
49
43
50
- public IEventClient GetProjectEvents ( int projectId )
51
- {
52
- throw new System . NotImplementedException ( ) ;
53
- }
44
+ public IEventClient GetProjectEvents ( int projectId ) => new EventClient ( Context , projectId : projectId ) ;
54
45
55
46
public ICommitStatusClient GetCommitStatus ( int projectId ) => new CommitStatusClient ( Context , projectId ) ;
56
47
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ public GitLabServer()
27
27
Groups = new GroupCollection ( this ) ;
28
28
Users = new UserCollection ( this ) ;
29
29
SystemHooks = new SystemHookCollection ( this ) ;
30
+ Events = new EventCollection ( this ) ;
30
31
}
31
32
32
33
public string DefaultBranchName { get ; set ; } = "main" ;
@@ -41,6 +42,8 @@ public GitLabServer()
41
42
42
43
public SystemHookCollection SystemHooks { get ; }
43
44
45
+ public EventCollection Events { get ; }
46
+
44
47
public VisibilityLevel DefaultForkVisibilityLevel { get ; set ; } = VisibilityLevel . Private ;
45
48
46
49
public IGitLabClient CreateClient ( User user )
Original file line number Diff line number Diff line change @@ -33,5 +33,20 @@ protected Note()
33
33
public bool Resolvable { get ; set ; }
34
34
35
35
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
+ }
36
51
}
37
52
}
You can’t perform that action at this time.
0 commit comments