Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runner registration/delete mock #562

Merged
merged 9 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 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 GitLabException("Bad Request. Runner is not found in any project");
BallyB marked this conversation as resolved.
Show resolved Hide resolved
}

if (projects.Take(2).Count() > 1)
{
throw new GitLabException("Bad Request. Runner is enabled in multiple projects");
BallyB marked this conversation as resolved.
Show resolved Hide resolved
}

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

public Models.Runner Update(int runnerId, RunnerUpdate runnerUpdate)
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);
}
}
}
}
16 changes: 16 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,25 @@ 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 string MakeToken(string id, string prefix = "")
{
return prefix + id.PadLeft(20, '0');
}

internal string MakeRegistrationToken(string id)
{
return MakeToken(id, "GR1348941");
BallyB marked this conversation as resolved.
Show resolved Hide resolved
}

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; set; }
BallyB marked this conversation as resolved.
Show resolved Hide resolved

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
5 changes: 5 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,12 @@ 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.RunnersToken.set -> void
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 +1089,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);
}
}
}