NGitLab .NET Client Library

repository·main·Indexed 41 days ago

https://github.com/ubisoft/ngitlab

A .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.

Tokens
1.4K
Snippets
6
Records
7
Agent score
1%

What's inside NGitLab

  1. Mock GitLab using GitLabConfig (Recommended)

    main

    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.

    1. Instantiate and configure a GitLabConfig object using fluent methods like .WithUser() and .WithProject().
    2. Call .BuildServer() on the config to create a GitLabServer.
    3. Use server.CreateClient(username) to obtain an IGitLabClient.
    4. Important: Call 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();
  2. Configure GitLab mock via YAML file

    main

    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.

    YAML Schema Example

    gitlab:
      users:
        - username: test
      projects:
        - name: user-project
          namespace: test
          commits:
            - message: init
              user: test
              tags:
                - v1.0.0
        - name: SampleProject
          namespace: testgroup

    Loading in C#

    var content = File.ReadAllText(filePath);
    var config = GitLabConfig.Deserialize(content);
    var server = config.BuildServer();
    var content = File.ReadAllText(filePath);
    var config = GitLabConfig.Deserialize(content);
  3. Initialize a GitLabClient

    main

    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");
  4. Mock GitLab using GitLabServer models

    main

    For more granular control, you can manually manipulate the GitLabServer model.

    1. Instantiate a GitLabServer.
    2. Manually add User, Project, or Group objects to the server's collections.
    3. Use project.Repository.Commit(...) to create actual git repositories and history.
    4. Create the client using 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);
  5. Use variables in GitLab YAML configuration

    main

    The YAML deserialization for GitLabConfig supports environment variables or custom variables.

    • You can define a variables section in your YAML.
    • Use the syntax {variable_name} to reference them within the configuration.
    • A predefined variable 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