GitLab PHP API Client

repository·12.1·Indexed 21 days ago

https://github.com/gitlabphp/client

A modern PHP client for the GitLab API v4, designed to be decoupled from HTTP implementations using PSR standards. Supports PHP 8.1-8.5 and provides functionality for authenticating with GitLab instances, handling paginated responses via ResultPager, and managing groups, group members, projects, issues, labels, CI/CD variables, merge requests, iterations, packages, deploy tokens, and epics.

Tokens
18.1K
Snippets
60
Records
73
Agent score
75%

What's inside gitlabphp-client

  1. Connect to a self-hosted GitLab instance

    12.1

    If you are not using gitlab.com, use the setUrl method to point the client to your specific GitLab domain.

    $client = new Gitlab\Client();
    $client->setUrl('https://git.yourdomain.com');
    $client->authenticate('your_access_token', Gitlab\Client::AUTH_HTTP_TOKEN);
  2. Integrate GitLab Client with Laravel or Symfony

    12.1

    For framework-specific integrations, use the following packages:

    Laravel: Use graham-campbell/gitlab.

    Symfony: Use zeichen32/gitlabapibundle.

    # Laravel
    $ composer require "graham-campbell/gitlab:^8.1"
    
    # Symfony
    $ composer require "zeichen32/gitlabapibundle:^7.0"
  3. Install the GitLab PHP API Client

    12.1

    To install the GitLab PHP API Client, use Composer. This version supports PHP 8.1-8.5. You must also ensure you have packages installed that provide psr/http-client-implementation and psr/http-factory-implementation (such as Guzzle).

    $ composer require "m4tthumphrey/php-gitlab-api:^12.1" "guzzlehttp/guzzle:^7.9.2"
  4. Initialize the GitLab Client

    12.1

    The Gitlab\Client is the primary entry point for interacting with the GitLab API. You can initialize it in two ways:

    1. Default Initialization: Using the constructor, which sets up a default HTTP client builder configured for https://gitlab.com with standard plugins (ExceptionThrower, History, Redirect, and User-Agent).
    2. Custom HTTP Client: Using createWithHttpClient() if you want to provide your own PSR-18 compliant ClientInterface implementation.

    To target a self-hosted GitLab instance, use the setUrl() method after instantiation.

    use Gitlab\Client;
    
    // Option 1: Default (gitlab.com)
    $client = new Client();
    
    // Option 2: Custom HTTP Client
    $client = Client::createWithHttpClient($myPsr18Client);
    
    // For self-hosted instances
    $client->setUrl('https://gitlab.example.com');
  5. Fetch all results using the ResultPager

    12.1

    To handle paginated API responses and retrieve all items in a collection, use the Gitlab\ResultPager. The fetchAll method takes the API resource, a pagination type (e.g., 'all'), and an optional array of filters.

    $pager = new Gitlab\ResultPager($client);
    $issues = $pager->fetchAll($client->issues(), 'all', [null, ['state' => 'closed']]);
  6. Authenticate with the GitLab API

    12.1

    Initialize a Gitlab\Client and use the authenticate method to provide credentials. The client supports multiple authentication methods via constants:

    • Gitlab\Client::AUTH_HTTP_TOKEN: For Personal Access Tokens.
    • Gitlab\Client::AUTH_OAUTH_TOKEN: For OAuth2 tokens.
    $client = new Gitlab\Client();
    
    // Personal access token authentication
    $client->authenticate('your_access_token', Gitlab\Client::AUTH_HTTP_TOKEN);
    
    // OAuth2 authentication
    // $client->authenticate('your_oauth_token', Gitlab\Client::AUTH_OAUTH_TOKEN);
  7. Customize the HTTP Client with a Builder

    12.1

    You can customize the underlying HTTP behavior (like adding custom headers or User-Agents) by passing a Gitlab\HttpClient\Builder to the Gitlab\Client constructor. This allows you to use HTTPlug plugins to modify requests.

    $plugin = new Http\Client\Common\Plugin\HeaderSetPlugin([
        'User-Agent' => 'Foobar',
    ]);
    
    $builder = new Gitlab\HttpClient\Builder();
    $builder->addPlugin($plugin);
    
    $client = new Gitlab\Client($builder);
  8. List Projects within a Group

    12.1

    Use the projects(int|string $id, array $parameters) method to retrieve projects belonging to a specific group.

    Available Parameters:

    • search: Search for projects matching criteria.
    • visibility: public, internal, or private.
    • order_by: id, name, path, created_at, updated_at, or last_activity_at (default created_at).
    • sort: asc or desc (default desc).
    • include_subgroups: Include projects in subgroups (default false).
    • simple: If true, returns only id, url, name, and path.
    • archived: Filter by archived status.
    • last_activity_after / last_activity_before: Filter by activity time using \DateTimeInterface objects.
    // Get all public projects in a group, including subgroups
    $projects = $groups->projects($groupId, [
        'visibility' => 'public',
        'include_subgroups' => true
    ]);
  9. List all merge requests

    12.1

    Use the all() method to retrieve a list of merge requests. You can optionally provide a project_id to scope the request to a specific project. The method accepts a parameters array for filtering and sorting.

    Available State Constants:

    • MergeRequests::STATE_ALL ('all')
    • MergeRequests::STATE_MERGED ('merged')
    • MergeRequests::STATE_OPENED ('opened')
    • MergeRequests::STATE_CLOSED ('closed')
    • MergeRequests::STATE_LOCKED ('locked')
    // List all merge requests for a specific project with filters
    $mergeRequests = $api->mergeRequests()->all($project_id, [
        'state' => 'opened',
        'assignee_id' => 123,
        'labels' => 'feature,bug',
        'order_by' => 'created_at',
        'sort' => 'desc'
    ]);
  10. Manage issue notes and discussions

    12.1

    GitLab issues support notes (simple comments) and discussions (threaded conversations). Use the following methods to manage them:

    Notes

    • showNotes(int|string $project_id, int $issue_iid): List all notes for an issue.
    • showNote(int|string $project_id, int $issue_iid, int $note_id): Get a specific note.
    • addNote(int|string $project_id, int $issue_iid, string $body, array $params = []): Add a new note.
    • updateNote(int|string $project_id, int $issue_iid, int $note_id, string $body, array $params = []): Update a note's body.
    • removeNote(int|string $project_id, int $issue_iid, int $note_id): Delete a note.

    Discussions

    • showDiscussions(int|string $project_id, int $issue_iid): List all discussions.
    • showDiscussion(int|string $project_id, int $issue_iid, string $discussion_id): Get a specific discussion.
    • addDiscussion(int|string $project_id, int $issue_iid, string $body): Start a new discussion.
    • addDiscussionNote(int|string $project_id, int $issue_iid, string $discussion_id, string $body): Add a note to an existing discussion.
    • updateDiscussionNote(int|string $project_id, int $issue_iid, string $discussion_id, int $note_id, string $body): Update a note within a discussion.
    • removeDiscussionNote(int|string $project_id, int $issue_iid, string $discussion_id, int $note_id): Delete a note within a discussion.
    // Add a note to an issue
    $issuesApi->addNote($project_id, $issue_iid, 'This is a comment.');
    
    // Start a discussion
    $issuesApi->addDiscussion($project_id, $issue_iid, 'Let us discuss this approach.');
  11. Search within a Group

    12.1

    The search method allows you to perform scoped searches within a specific group (e.g., searching for issues, merge requests, or projects).

    Required Parameters:

    • scope (string): The category to search in. Allowed values are:
      • issues
      • merge_requests
      • milestones
      • projects
      • users
      • blobs
      • commits
      • notes
      • wiki_blobs
    • search (string): The search query string.

    Optional Parameters:

    • order_by (string): Sort by created_at.
    • sort (string): asc or desc (default is desc).
    • state (string): Filter by opened or closed (supported for issues and merge requests).
    • confidential (bool): Filter by confidentiality (supported for issues scope).
    // Search for open issues in a group
    $results = $client->groups()->search($group_id, [
        'scope' => 'issues',
        'search' => 'bug report',
        'state' => 'opened',
        'sort' => 'desc'
    ]);
  12. Manage Merge Request award emojis

    12.1

    You can manage emoji reactions on GitLab merge requests using the following methods. Note that $project_id can be an integer or a string (e.g., a URL-encoded path), and $mr_iid is the internal ID of the merge request.

    // Add an emoji
    $client->mergeRequests()->addAwardEmoji($project_id, $mr_iid, 'thumbsup');
    
    // Show details of a specific award
    $emoji = $client->mergeRequests()->showAwardEmoji($project_id, $mr_iid, $award_id);
    
    // Remove an emoji
    $client->mergeRequests()->removeAwardEmoji($project_id, $mr_iid, $award_id);