go-gitlab

repository·main·Indexed 25 days ago

https://github.com/xanzy/go-gitlab

A GitLab API client enabling Go programs to interact with GitLab in a simple and uniform way. This repository is archived and has moved to the official GitLab organization; users are advised to migrate to gitlab.com/gitlab-org/api/client-go. The library provides services for managing users, projects, access requests, instance appearance, OAuth applications, and audit events.

Tokens
62.5K
Snippets
122
Records
479
Agent score
82%

What's inside go-gitlab

  1. Migrate to the official GitLab API client

    main

    This repository (github.com/xanzy/go-gitlab) is archived and has moved to the official GitLab organization. To stay up to date, migrate your codebase by replacing the import path.

    Migration Steps:

    1. Replace github.com/xanzy/go-gitlab with gitlab.com/gitlab-org/api/client-go in your code.

    The code is fully backwards-compatible, and no breaking changes are expected during this transition.

  2. Manage award emojis on issues, merge requests, and snippets

    main

    The AwardEmojiService provides methods to manage emoji reactions (award emojis) on GitLab resources including Issues, Merge Requests, and Snippets. You can list, retrieve, create, and delete emojis directly on these resources.

    Available resource types:

    • Issues
    • Merge Requests
    • Snippets
  3. Manage project CI/CD job token access settings

    main
    The JobTokenScopeService allows you to manage the CI/CD job token access settings (job token scope) for a project. This includes viewing and patching the inbound/outbound enablement settings, as well as managing the inbound allowlists for specific projects or groups.
  4. Handle GitLab API Pagination

    main

    The Response type wraps the standard http.Response and provides helper fields to handle both offset-based and keyset-based pagination automatically by parsing GitLab's response headers.

    Offset-based Pagination

    Use these fields to navigate pages using Page and PerPage in your ListOptions:

    • TotalItems: Total number of items available.
    • TotalPages: Total number of pages.
    • ItemsPerPage: Number of items per page.
    • CurrentPage: The current page number.
    • NextPage: The next page number.
    • PreviousPage: The previous page number.

    Keyset-based Pagination

    Use these fields for cursor-based navigation (often used with PageToken in ListOptions):

    • NextLink: URL for the next page.
    • PreviousLink: URL for the previous page.
    • FirstLink: URL for the first page.
    • LastLink: URL for the last page.
  5. Merge Request Data Structures

    main

    The following types represent core GitLab Merge Request entities:

    • MergeRequest: The primary object representing a merge request, containing metadata like Title, State, Author, Assignee, Labels, and WebURL.
    • MergeRequestDiff: Represents a single file change within a merge request, including OldPath, NewPath, and the Diff content.
    • MergeRequestDiffVersion: Represents a specific version of a merge request's diffs.
    • MergeRequestReviewer: Represents a user assigned as a reviewer and their current State.
  6. Manage access requests via AccessRequestsService

    main

    The AccessRequestsService provides methods to list, request, approve, and deny access requests for GitLab projects and groups.

    Key Capabilities:

    • Listing: Retrieve access requests viewable by the authenticated user for a specific project or group.
    • Requesting: Request access for the authenticated user to a project or group.
    • Approving: Approve an existing access request for a specific user, optionally specifying an AccessLevel.
    • Denying: Deny an existing access request for a specific user.
  7. Authentication Types in GitLab

    main

    The AuthType type defines the supported authentication mechanisms for the GitLab API:

    • BasicAuth: Username and password.
    • JobToken: GitLab CI/CD job tokens.
    • OAuthToken: OAuth2 access tokens.
    • PrivateToken: Personal or Project access tokens.
  8. Initialize a GitLab Client

    main

    The go-gitlab library provides several ways to initialize a Client depending on your authentication method.

    Note: This package is deprecated. It is recommended to migrate to gitlab.com/gitlab-org/api/client-go.

    Available authentication methods:

    • Private Token: Use NewClient(token) for personal or project access tokens.
    • Basic Auth: Use NewBasicAuthClient(username, password) for username/password authentication.
    • Job Token: Use NewJobClient(token) for CI/CD job tokens.
    • OAuth Token: Use NewOAuthClient(token) for OAuth2 tokens.

    All constructors accept optional ClientOptionFunc arguments to customize the client (e.g., setting a custom BaseURL).

  9. Create a project and a snippet

    main

    This example demonstrates how to create a new project and then add a snippet to that specific project using the ProjectSnippets service.

    package main
    
    import (
    	"log"
    
    	"github.com/xanzy/go-gitlab"
    )
    
    func main() {
    	git, err := gitlab.NewClient("yourtokengoeshere")
    	if err != nil {
    		log.Fatalf("Failed to create client: %v", err)
    	}
    
    	// Create new project
    	p := &gitlab.CreateProjectOptions{
    		Name:                     gitlab.Ptr("My Project"),
    		Description:              gitlab.Ptr("Just a test project to play with"),
    		MergeRequestsAccessLevel: gitlab.Ptr(gitlab.EnabledAccessControl),
    		SnippetsAccessLevel:      gitlab.Ptr(gitlab.EnabledAccessControl),
    		Visibility:               gitlab.Ptr(gitlab.PublicVisibility),
    	}
    	project, _, err := git.Projects.CreateProject(p)
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	// Add a new snippet
    	s := &gitlab.CreateProjectSnippetOptions{
    		Title:           gitlab.Ptr("Dummy Snippet"),
    		FileName:        gitlab.Ptr("snippet.go"),
    		Content:         gitlab.Ptr("package main...."),
    		Visibility:      gitlab.Ptr(gitlab.PublicVisibility),
    	}
    	_, _, err = git.ProjectSnippets.CreateSnippet(project.ID, s)
    	if err != nil {
    		log.Fatal(err)
    	}
    }
  10. List users and projects with options

    main

    The client provides various services (e.g., Users, Projects) to access different parts of the GitLab API. Most methods accept an options struct to filter or refine results. Use gitlab.Ptr() to pass pointers to literal values in these options.

    // List all users
    users, _, err := git.Users.ListUsers(&gitlab.ListUsersOptions{})
    
    // List projects with a search filter
    opt := &gitlab.ListProjectsOptions{Search: gitlab.Ptr("svanharmelen")}
    projects, _, err := git.Projects.ListProjects(opt)
  11. Initialize a GitLab client

    main
    To interact with the GitLab API, construct a new client using gitlab.NewClient. You must provide a personal access token. You can also use functional options like gitlab.WithBaseURL to customize the client (e.g., for self-managed GitLab instances).
  12. Create group deploy token options

    main

    Configuration for creating a new group-level deploy token.

    Fields:

    • Name (*string): The name of the token.
    • ExpiresAt (*time.Time): When the token expires.
    • Username (*string): The username for the token.
    • Scopes (*[]string): The list of scopes to assign.