What is NGitLab?
mainNGitLab is a .NET client implementation for the GitLab REST API.repository·main·Indexed 41 days ago
https://github.com/ubisoft/ngitlabA .NET client library for the GitLab REST API that provides a typed interface for automating GitLab tasks. Includes NGitLab.Mock for creating mocked GitLab environments via GitLabConfig, GitLabServer models, or YAML configuration.
NGitLab is a .NET client implementation for the GitLab REST API.The recommended way to set up a mocked GitLab environment is using GitLabConfig. This method uses extension methods for easy setup and supports YAML serialization.
GitLabConfig object using fluent methods like .WithUser() and .WithProject()..BuildServer() on the config to create a GitLabServer.server.CreateClient(username) to obtain an IGitLabClient.server.Dispose() at the end of your test to clean up resources like local git repositories.var config = new GitLabConfig()
// Add a user
.WithUser("test")
// Add a new project in the user namespace and add commits
.WithProject("user-project", @namespace: "test", configure: project => project
.WithCommit("init", user: "test", tags: new[] { "v1.0.0" }))
// Add a project in a group
.WithProject("SampleProject", @namespace: "testgroup");
var server = config.BuildServer();
var client = server.CreateClient("test");
// Clean up after tests
server.Dispose();You can define your entire GitLab mock setup in a YAML file and load it using GitLabConfig.Deserialize(content). This is useful for keeping test data separate from code.
gitlab:
users:
- username: test
projects:
- name: user-project
namespace: test
commits:
- message: init
user: test
tags:
- v1.0.0
- name: SampleProject
namespace: testgroupvar content = File.ReadAllText(filePath);
var config = GitLabConfig.Deserialize(content);
var server = config.BuildServer();var content = File.ReadAllText(filePath);
var config = GitLabConfig.Deserialize(content);To use the library, create an instance of GitLabClient. You must provide the base URL of your GitLab instance and a private token. Private tokens can be obtained from your GitLab account page. It is recommended to use a custom user for API interactions.
var client = new GitLabClient("https://mygitlab.example.com", "your_private_token");You can install the NGitLab package using the dotnet CLI to add it to your .NET project.
dotnet add package NGitLabFor more granular control, you can manually manipulate the GitLabServer model.
GitLabServer.User, Project, or Group objects to the server's collections.project.Repository.Commit(...) to create actual git repositories and history.server.CreateClient(user).// Instantiate server
var server = new GitLabServer();
// Add a user
var user = new User("test");
server.Users.Add(user);
// Create a new project in the user namespace and add commits
var project = user.Namespace.Projects.AddNew();
project.Repository.Commit(user, "init"); // It creates an actual git repository
project.Repository.CreateTag("v1.0.0");
// Create a group and add a project in it
var project2 = new Project("SampleProject");
var group = new Group();
group.Projects.Add(project);
server.Groups.Add(group);
// Create the client
IGitLabClient client = server.CreateClient(user);The YAML deserialization for GitLabConfig supports environment variables or custom variables.
variables section in your YAML.{variable_name} to reference them within the configuration.new_guid is available to generate a new GUID in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.variables:
repos_path: "%localappdata%\\ngitlab\\repos"
gitlab:
users:
- username: user
projects:
- name: SampleProject
clonePath: "{repos_path}\\SampleProject"
commits:
- message: Initial commit
user: user