Skip to content

Commit

Permalink
Add runner registration/delete mock (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
BallyB authored Nov 16, 2023
1 parent d7c4ec1 commit 97a0852
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
33 changes: 25 additions & 8 deletions NGitLab.Mock/Clients/RunnerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,26 @@ public Models.Runner this[int id]
}
}

public void Delete(Models.Runner runner)
{
throw new NotImplementedException();
}
public void Delete(Models.Runner runner) => Delete(runner.Id);

public void Delete(int runnerId)
{
throw new NotImplementedException();
using (Context.BeginOperationScope())
{
var projects = Server.AllProjects.Where(p => p.EnabledRunners.Any(r => r.Id == runnerId));
if (!projects.Any())
{
throw new GitLabBadRequestException("Runner is not found in any project");
}

if (projects.Skip(1).Any())
{
throw new GitLabBadRequestException("Runner is enabled in multiple projects");
}

var project = GetProject(projects.Single().Id, ProjectPermission.Edit);
project.RemoveRunner(runnerId);
}
}

public Models.Runner Update(int runnerId, RunnerUpdate runnerUpdate)
Expand Down Expand Up @@ -130,7 +142,7 @@ public Models.Runner EnableRunner(int projectId, RunnerId runnerId)

if (project.EnabledRunners.Contains(runnerReference))
{
throw new GitLabException("Bad Request. Runner has already been taken");
throw new GitLabBadRequestException("Runner has already been taken");
}

project.EnabledRunners.Add(runnerReference);
Expand Down Expand Up @@ -174,9 +186,14 @@ private Runner GetServerRunner(int id)
return GetOwnedRunners().FirstOrDefault(runner => runner.Id == id) ?? throw new GitLabNotFoundException();
}

Models.Runner IRunnerClient.Register(RunnerRegister request)
public Models.Runner Register(RunnerRegister request)
{
throw new NotImplementedException();
using (Context.BeginOperationScope())
{
var project = Server.AllProjects.SingleOrDefault(p => string.Equals(p.RunnersToken, request.Token, StringComparison.Ordinal));
var runner = project.AddRunner(null, request.Description, request.Active ?? false, request.Locked ?? true, false, request.RunUntagged ?? false);
return runner.ToClientRunner(Context.User);
}
}
}
}
17 changes: 17 additions & 0 deletions NGitLab.Mock/GitLabServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public sealed class GitLabServer : GitLabObject, IDisposable
private int _lastResourceLabelEventId = 10000;
private int _lastResourceMilestoneEventId = 10000;
private int _lastResourceStateEventId = 10000;
private int _lastTokenId = 10000;
private int _lastRegistrationTokenId = 10000;

public event EventHandler ClientOperation;

Expand Down Expand Up @@ -130,11 +132,26 @@ public void Dispose()

internal int 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);
Expand Down
12 changes: 12 additions & 0 deletions NGitLab.Mock/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public string[] Tags

public ProtectedBranchCollection ProtectedBranches { get; }

public string RunnersToken { get; internal set; }

public void Remove()
{
Group.Projects.Remove(this);
Expand Down Expand Up @@ -345,6 +347,11 @@ public MergeRequest CreateMergeRequest(User user, string title, string descripti
return mr;
}

public bool RemoveRunner(int runnerId)
{
return RegisteredRunners.Remove(runnerId);
}

public Runner AddRunner(string name, string description, bool active, bool locked, bool isShared, bool runUntagged, int id)
{
var runner = new Runner
Expand Down Expand Up @@ -374,6 +381,11 @@ public Runner AddRunner(string name, string description, bool active, bool locke
return AddRunner(name, description, active, locked, isShared, runUntagged: false, default);
}

public Runner AddRunner(string name, string description, bool active, bool locked, bool isShared, bool runUntagged)
{
return AddRunner(name, description, active, locked, isShared, runUntagged, default);
}

public Project Fork(User user)
{
return Fork(user.Namespace, user, Name);
Expand Down
2 changes: 2 additions & 0 deletions NGitLab.Mock/ProjectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public override void Add(Project project)
project.Id = Server.GetNewProjectId();
}

project.RunnersToken ??= Server.GetNewRegistrationToken();

base.Add(project);
}
}
Expand Down
4 changes: 4 additions & 0 deletions NGitLab.Mock/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ NGitLab.Mock.Project
NGitLab.Mock.Project.AccessibleMergeRequests.get -> bool
NGitLab.Mock.Project.AccessibleMergeRequests.set -> void
NGitLab.Mock.Project.AddRunner(string name, string description, bool active, bool locked, bool isShared) -> NGitLab.Mock.Runner
NGitLab.Mock.Project.AddRunner(string name, string description, bool active, bool locked, bool isShared, bool runUntagged) -> NGitLab.Mock.Runner
NGitLab.Mock.Project.AddRunner(string name, string description, bool active, bool locked, bool isShared, bool runUntagged, int id) -> NGitLab.Mock.Runner
NGitLab.Mock.Project.AllThreadsMustBeResolvedToMerge.get -> bool
NGitLab.Mock.Project.AllThreadsMustBeResolvedToMerge.set -> void
Expand Down Expand Up @@ -858,9 +859,11 @@ NGitLab.Mock.Project.ProtectedBranches.get -> NGitLab.Mock.ProtectedBranchCollec
NGitLab.Mock.Project.RegisteredRunners.get -> NGitLab.Mock.RunnerCollection
NGitLab.Mock.Project.Releases.get -> NGitLab.Mock.ReleaseCollection
NGitLab.Mock.Project.Remove() -> void
NGitLab.Mock.Project.RemoveRunner(int runnerId) -> bool
NGitLab.Mock.Project.Repository.get -> NGitLab.Mock.Repository
NGitLab.Mock.Project.RepositoryAccessLevel.get -> NGitLab.Models.RepositoryAccessLevel
NGitLab.Mock.Project.RepositoryAccessLevel.set -> void
NGitLab.Mock.Project.RunnersToken.get -> string
NGitLab.Mock.Project.SshUrl.get -> string
NGitLab.Mock.Project.Statistics.get -> NGitLab.Models.ProjectStatistics
NGitLab.Mock.Project.Statistics.set -> void
Expand Down Expand Up @@ -1085,6 +1088,7 @@ NGitLab.Mock.Runner.Token.set -> void
NGitLab.Mock.Runner.Version.get -> string
NGitLab.Mock.Runner.Version.set -> void
NGitLab.Mock.RunnerCollection
NGitLab.Mock.RunnerCollection.Remove(int id) -> bool
NGitLab.Mock.RunnerCollection.RunnerCollection(NGitLab.Mock.Project parent) -> void
NGitLab.Mock.RunnerRef
NGitLab.Mock.RunnerRef.Id.get -> int
Expand Down
9 changes: 9 additions & 0 deletions NGitLab.Mock/RunnerCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace NGitLab.Mock
{
Expand All @@ -22,7 +23,15 @@ public override void Add(Runner item)
item.Id = Server.GetNewRunnerId();
}

item.Token ??= Server.GetNewRunnerToken();

base.Add(item);
}

public bool Remove(int id)
{
var r = this.SingleOrDefault(r => r.Id == id);
return Remove(r);
}
}
}

0 comments on commit 97a0852

Please sign in to comment.