-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathGitLabServer.cs
162 lines (119 loc) · 5.46 KB
/
GitLabServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NGitLab.Models;
namespace NGitLab.Mock;
/// <summary>
/// Starting point of your mock, instantiate GitLab client
/// and add fake content to it.
/// </summary>
public sealed class GitLabServer : GitLabObject, IDisposable
{
// Setting to a 'magic' high value to avoid having equalities between IDs and IiDs
private long _lastProjectId = 10000;
private long _lastGroupId = 10000;
private long _lastMergeRequestId = 10000;
private long _lastRunnerId = 10000;
private long _lastIssueId = 10000;
private long _lastMilestoneId = 10000;
private long _lastPipelineId = 10000;
private long _lastPipelineScheduleId = 10000;
private long _lastJobId = 10000;
private long _lastBadgeId = 10000;
private long _lastLabelId = 10000;
private long _lastProtectedBranchId = 10000;
private long _lastResourceLabelEventId = 10000;
private long _lastResourceMilestoneEventId = 10000;
private long _lastResourceStateEventId = 10000;
private long _lastTokenId = 10000;
private long _lastRegistrationTokenId = 10000;
public event EventHandler ClientOperation;
public GitLabServer()
{
Groups = new GroupCollection(this);
Users = new UserCollection(this);
SystemHooks = new SystemHookCollection(this);
Events = new EventCollection(this);
ResourceLabelEvents = new ResourceLabelEventCollection(this);
ResourceMilestoneEvents = new ResourceMilestoneEventCollection(this);
ResourceStateEvents = new ResourceStateEventCollection(this);
}
public string DefaultBranchName { get; set; } = "main";
public Uri Url { get; set; } = new Uri("https://gitlab.example.com/", UriKind.Absolute);
public GitLabVersion Version { get; set; } = new GitLabVersion { Version = "1.0.0", Revision = "rev1" };
public GroupCollection Groups { get; }
public UserCollection Users { get; }
public SystemHookCollection SystemHooks { get; }
public EventCollection Events { get; }
public ResourceStateEventCollection ResourceStateEvents { get; }
public ResourceLabelEventCollection ResourceLabelEvents { get; }
public ResourceMilestoneEventCollection ResourceMilestoneEvents { get; }
public VisibilityLevel DefaultForkVisibilityLevel { get; set; } = VisibilityLevel.Private;
public IGraphQLClient DefaultGraphQLClient { get; set; }
public IGitLabClient CreateClient(User user)
{
if (!Users.Contains(user))
{
Users.Add(user);
}
return new Clients.GitLabClient(new Clients.ClientContext(this, user));
}
public IEnumerable<Group> AllGroups
{
get
{
foreach (var group in Groups)
{
yield return group;
foreach (var subGroup in group.DescendantGroups)
{
yield return subGroup;
}
}
}
}
public IEnumerable<Runner> AllRunners => AllGroups.SelectMany(g => g.RegisteredRunners).Concat(AllProjects.SelectMany(p => p.RegisteredRunners));
public IEnumerable<Project> AllProjects => AllGroups.SelectMany(group => group.Projects);
public void Dispose()
{
foreach (var project in AllProjects)
{
project.Repository.Dispose();
}
}
internal long GetNewGroupId() => Interlocked.Increment(ref _lastGroupId);
internal long GetNewProjectId() => Interlocked.Increment(ref _lastProjectId);
internal long GetNewMergeRequestId() => Interlocked.Increment(ref _lastMergeRequestId);
internal long GetNewIssueId() => Interlocked.Increment(ref _lastIssueId);
internal long GetNewMilestoneId() => Interlocked.Increment(ref _lastMilestoneId);
internal long GetNewRunnerId() => Interlocked.Increment(ref _lastRunnerId);
internal long GetNewPipelineId() => Interlocked.Increment(ref _lastPipelineId);
internal long GetNewPipelineScheduleId() => Interlocked.Increment(ref _lastPipelineScheduleId);
internal long GetNewJobId() => Interlocked.Increment(ref _lastJobId);
internal long GetNewBadgeId() => Interlocked.Increment(ref _lastBadgeId);
internal long GetNewLabelId() => Interlocked.Increment(ref _lastLabelId);
internal long GetNewProtectedBranchId() => Interlocked.Increment(ref _lastProtectedBranchId);
internal long GetNewResourceLabelEventId() => Interlocked.Increment(ref _lastResourceLabelEventId);
internal long GetNewResourceMilestoneEventId() => Interlocked.Increment(ref _lastResourceMilestoneEventId);
internal long GetNewResourceStateEventId() => Interlocked.Increment(ref _lastResourceStateEventId);
internal string GetNewRunnerToken() => MakeToken(Convert.ToString(Interlocked.Increment(ref _lastTokenId)));
internal string GetNewRegistrationToken() => MakeRegistrationToken(Convert.ToString(Interlocked.Increment(ref _lastRegistrationTokenId)));
internal string MakeUrl(string relativeUrl)
{
return new Uri(Url, relativeUrl).AbsoluteUri;
}
internal static string MakeToken(string id, string prefix = "")
{
return prefix + id.PadLeft(20, '0');
}
internal static string MakeRegistrationToken(string id)
{
// Prefix is hardcoded: https://gitlab.com/gitlab-org/gitlab/-/issues/388379
return MakeToken(id, "GR1348941");
}
internal void RaiseOnClientOperation()
{
ClientOperation?.Invoke(this, EventArgs.Empty);
}
}