gitbeaker

repository·main·Indexed 23 days ago

https://github.com/jdalrymple/gitbeaker

A comprehensive, typed GitLab SDK supporting Node.js, Deno, Bun, and browsers, providing complete coverage of GitLab APIs (up to version 16.5). The project includes @gitbeaker/rest for general use, @gitbeaker/core for API definitions, @gitbeaker/cli for terminal interaction, and @gitbeaker/requester-utils for creating custom SDK wrappers.

Tokens
15.4K
Snippets
27
Records
128
Agent score
79%

What's inside gitbeaker

  1. Overview of Gitbeaker

    main
    Gitbeaker is a typed GitLab SDK designed for use in modern environments including Browsers, Node.js, Deno, and Bun. It also provides a CLI for command-line interaction with GitLab. The SDK aims to provide complete coverage of GitLab's exposed APIs (up to version 16.5) with extensive TypeScript declarations and high test coverage.
  2. Understand the Gitbeaker package structure

    main

    Gitbeaker is distributed as several specialized packages. Depending on your needs, you should choose the appropriate package:

    • @gitbeaker/rest: The primary library for most consumers. It is a wrapper around the core API that works in Node.js, Deno, Bun, and modern browsers using native fetch.
    • @gitbeaker/core: Contains the core API definitions and details all GitLab resource support.
    • @gitbeaker/cli: A CLI wrapper around the @gitbeaker/rest distribution for terminal usage.
    • @gitbeaker/requester-utils: Low-level utilities for the underlying HTTP request functionality.
  3. Install @gitbeaker/requester-utils

    main

    Depending on your runtime environment, you can install or load @gitbeaker/requester-utils using the following methods:

    Node.js (18+)

    Install via your preferred package manager:

    npm install @gitbeaker/requester-utils
    yarn add @gitbeaker/requester-utils
    pnpm add @gitbeaker/requester-utils

    Then import the utilities:

    import { RequesterUtils, BaseResource } from '@gitbeaker/requester-utils';

    Deno

    Load directly from esm.sh using the ?dts query parameter to include type definitions:

    import { RequesterUtils, BaseResource } from 'https://esm.sh/@gitbeaker/requester-utils?dts';

    Browsers

    Load directly from esm.sh as an ES module:

    <script type="module">
      import { RequesterUtils, BaseResource } from 'https://esm.sh/@gitbeaker/requester-utils';
    </script>
    import { RequesterUtils, BaseResource } from '@gitbeaker/requester-utils';
  4. Run integration tests against a local GitLab instance

    main

    If you want to run integration tests against a manually managed GitLab instance (instead of using the full test:full suite), follow these steps:

    1. Start GitLab: Navigate to the .docker directory and start the GitLab service.
      cd .docker && docker-compose up gitlab
    2. Configure Environment: Set the GITLAB_URL and GITLAB_PERSONAL_ACCESS_TOKEN environment variables.
    3. Execute Tests: Run the integration test command.

    Default GitLab Credentials (for the containerized instance):

    • URL: http://localhost:8080
    • Username: root
    • Password: gitbeaker
    • Personal Access Token: superstrongpassword123
    # Step 1: Start GitLab
    cd .docker && docker-compose up gitlab
    
    # Step 2 & 3: Set env vars and run tests
    export GITLAB_URL="http://localhost:8080"
    export GITLAB_PERSONAL_ACCESS_TOKEN="superstrongpassword123"
    pnpm test:integration
  5. Install and use @gitbeaker/core

    main

    The @gitbeaker/core package is the core SDK for the GitLab API. Note that it is not intended for direct use; developers should instead use @gitbeaker/rest or @gitbeaker/cli.

    Depending on your runtime, you can load the package as follows:

    Node.js (18+)

    Install via npm, yarn, or pnpm:

    npm install @gitbeaker/core
    yarn add @gitbeaker/core
    pnpm add @gitbeaker/core

    Then import the Gitlab class:

    import { Gitlab } from '@gitbeaker/core';

    Deno

    Load directly from esm.sh:

    import { Gitlab } from 'https://esm.sh/@gitbeaker/core?dts';

    Browsers

    Load directly from esm.sh using a module script:

    <script type="module">
      import { Gitlab } from 'https://esm.sh/@gitbeaker/core';
    </script>
    import { Gitlab } from '@gitbeaker/core';
  6. Handle HTTPS certificates for GitLab servers

    main

    If your GitLab server uses HTTPS with custom certificates, the recommended approach is to use the NODE_EXTRA_CA_CERTS environment variable to point to your certificate file.

    Warning: You can bypass certificate validation by setting process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0', but this is insecure and should be avoided.

    "scripts": {
        "start": "NODE_EXTRA_CA_CERTS=./secrets/3ShapeCA.pem node bot.js"
    },
  7. Enable support for Node.js v16.18+

    main

    Gitbeaker relies on fetch support. Since native fetch is standard in Node 18+, users on Node v16.18+ can enable compatibility by installing node-fetch and polyfilling the global fetch object in their application's entry point.

    const semver = require('semver');
    
    if (semver.lt(process.version, '20.0.0')) {
      global.fetch = require('node-fetch');
    }
  8. Use the gitbeaker CLI

    main

    The CLI follows a specific command structure to interact with the GitLab API services. You can use either the full gitbeaker command or the shorthand gb.

    Command Syntax

    gitbeaker [service name] [method name] --config_args pos_arg1 pos_argN --opts_arg1 --opts_argN

    Argument Definitions

    • service name: Any supported API name from the @gitbeaker/rest SDK.
    • method name: Any supported command on that API service (e.g., all, show, remove, update).
    • --config_args: General configuration arguments (like personal tokens). These must include a gb or gl prefix (e.g., --gb-token).
    • pos_arg1 ... pos_argN: Positional arguments required by the method. These must be provided in the correct order. They can also be provided as flags (e.g., --pos_arg1), but order must be preserved.
    • --opts_arg1 ... --opts_argN: Optional arguments as defined by the GitLab API documentation.
    # To get all the projects
    gitbeaker projects all --gb-token="personaltoken"
    
    # To get all the projects id=2 and optional parameter "search" = "cool"
    gitbeaker projects all --gb-token="personaltoken" 2 --search="cool"
  9. Run the full test suite using Docker

    main

    The recommended way to run the complete test suite (types, unit, integration, and e2e) is using the containerized approach. This starts a GitLab CE instance via Docker Compose, waits for it to be healthy, sets environment variables, and runs all tests sequentially.

    Prerequisites:

    • Docker and Docker Compose installed.
    • At least 4GB of available RAM.
    • Docker Desktop configured with sufficient memory allocation.
    pnpm test:full
  10. Run individual test types

    main

    You can run specific subsets of the test suite using the following commands:

    • Type tests: pnpm test:types
    • Unit tests: pnpm test:unit
    • Integration tests: pnpm test:integration (requires a running GitLab instance)
    • End-to-end tests: pnpm test:e2e
    # Type tests only
    pnpm test:types
    
    # Unit tests only
    pnpm test:unit
    
    # Integration tests only (requires GitLab running)
    pnpm test:integration
    
    # End-to-end tests only
    pnpm test:e2e