api

package
v1.66.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

This is a silly wrapper for gitlab client-go but helps maintain consistency

Index

Constants

View Source
const (
	NamespaceKindUser  = "user"
	NamespaceKindGroup = "group"
)

Describe namespace kinds which is either group or user See docs: https://docs.gitlab.com/api/namespaces/

View Source
const MaxPerPage = 100

MaxPerPage is the maximum number of items per page supported by the GitLab API. https://docs.gitlab.com/api/rest/#offset-based-pagination

Variables

View Source
var DefaultListLimit = 30
View Source
var DeleteMR = func(client *gitlab.Client, projectID any, mrID int) error {
	_, err := client.MergeRequests.DeleteMergeRequest(projectID, mrID)
	if err != nil {
		return err
	}

	return nil
}

UpdateMR updates an MR Attention: this is a global variable and may be overridden in tests.

View Source
var GetIssue = func(client *gitlab.Client, projectID any, issueID int) (*gitlab.Issue, error) {
	issue, _, err := client.Issues.GetIssue(projectID, issueID)
	if err != nil {
		return nil, err
	}

	return issue, nil
}

GetIssue returns an issue Attention: this is a global variable and may be overridden in tests.

View Source
var GetMR = func(client *gitlab.Client, projectID any, mrID int, opts *gitlab.GetMergeRequestsOptions) (*gitlab.MergeRequest, error) {
	mr, _, err := client.MergeRequests.GetMergeRequest(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}

GetMR returns an MR Attention: this is a global variable and may be overridden in tests.

View Source
var GetProject = func(client *gitlab.Client, projectID any) (*gitlab.Project, error) {
	opts := &gitlab.GetProjectOptions{
		License:              gitlab.Ptr(true),
		WithCustomAttributes: gitlab.Ptr(true),
	}
	project, _, err := client.Projects.GetProject(projectID, opts)
	if err != nil {
		return nil, err
	}
	return project, nil
}

GetProject returns a single project Attention: this is a global variable and may be overridden in tests.

View Source
var ListAllMilestones = func(client *gitlab.Client, projectID any, opts *ListMilestonesOptions) ([]*Milestone, error) {
	project, err := GetProject(client, projectID)
	if err != nil {
		return nil, err
	}

	errGroup := &errgroup.Group{}
	projectMilestones := []*gitlab.Milestone{}
	groupMilestones := []*gitlab.GroupMilestone{}

	errGroup.Go(func() error {
		var err error
		projectMilestones, err = listProjectMilestones(client, projectID, opts.ListProjectMilestonesOptions())
		return err
	})

	if project.Namespace.Kind == NamespaceKindGroup {
		errGroup.Go(func() error {
			var err error
			groupMilestones, err = listGroupMilestones(client, project.Namespace.ID, opts.ListGroupMilestonesOptions())
			return err
		})
	}

	if err := errGroup.Wait(); err != nil {
		return nil, fmt.Errorf("failed to get all project related milestones. %w", err)
	}

	milestones := make([]*Milestone, 0, len(projectMilestones)+len(groupMilestones))
	for _, v := range projectMilestones {
		milestones = append(milestones, NewProjectMilestone(v))
	}

	for _, v := range groupMilestones {
		milestones = append(milestones, NewGroupMilestone(v))
	}

	return milestones, nil
}
View Source
var ListMRs = func(client *gitlab.Client, projectID any, opts *gitlab.ListProjectMergeRequestsOptions, listOpts ...CliListMROption) ([]*gitlab.BasicMergeRequest, error) {
	composedListOpts := composeCliListMROptions(listOpts...)
	assigneeIds, reviewerIds := composedListOpts.assigneeIds, composedListOpts.reviewerIds

	if len(assigneeIds) > 0 || len(reviewerIds) > 0 {
		return listMRsWithAssigneesOrReviewers(client, projectID, opts, assigneeIds, reviewerIds)
	} else {
		return listMRsBase(client, projectID, opts)
	}
}

ListMRs retrieves merge requests for a given project with optional filtering by assignees or reviewers.

Parameters:

  • client: A GitLab client instance.
  • projectID: The ID or name of the project.
  • opts: GitLab-specific options for listing merge requests.
  • listOpts: Optional list of arguments to filter by assignees or reviewers. May be any combination of api.WithMRAssignees and api.WithMRReviewers.

Returns:

  • A slice of GitLab merge request objects and an error, if any.

Example usage:

mrs, err := api.ListMRs(client, "my-group", &gitlab.ListProjectMergeRequestsOptions{},
	api.WithMRAssignees([]int{123, 456}),
	api.WithMRReviewers([]int{789}))

Attention: this is a global variable and may be overridden in tests.

View Source
var UpdateIssue = func(client *gitlab.Client, projectID any, issueID int, opts *gitlab.UpdateIssueOptions) (*gitlab.Issue, error) {
	issue, _, err := client.Issues.UpdateIssue(projectID, issueID, opts)
	if err != nil {
		return nil, err
	}

	return issue, nil
}

UpdateIssue updates an issue Attention: this is a global variable and may be overridden in tests.

View Source
var UpdateMR = func(client *gitlab.Client, projectID any, mrID int, opts *gitlab.UpdateMergeRequestOptions) (*gitlab.MergeRequest, error) {
	mr, _, err := client.MergeRequests.UpdateMergeRequest(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}

UpdateMR updates an MR Attention: this is a global variable and may be overridden in tests.

View Source
var UsersByNames = func(client *gitlab.Client, names []string) ([]*gitlab.User, error) {
	users := make([]*gitlab.User, 0, len(names))
	for _, name := range names {
		user, err := UserByName(client, name)
		if err != nil {
			return nil, err
		}

		users = append(users, user)
	}
	return users, nil
}

Functions

func Is404

func Is404(err error) bool

Is404 checks if the error represents a 404 response

func IsTokenConfigured added in v1.66.0

func IsTokenConfigured(token string) bool

IsTokenConfigured checks if a token is configured (non-empty after trimming whitespace)

func ListGroupMRs

func ListGroupMRs(client *gitlab.Client, projectID any, opts *gitlab.ListGroupMergeRequestsOptions, listOpts ...CliListMROption) ([]*gitlab.BasicMergeRequest, error)

ListGroupMRs retrieves merge requests for a given group with optional filtering by assignees or reviewers.

Parameters:

  • client: A GitLab client instance.
  • groupID: The ID or name of the group.
  • opts: GitLab-specific options for listing group merge requests.
  • listOpts: Optional list of arguments to filter by assignees or reviewers. May be any combination of api.WithMRAssignees and api.WithMRReviewers.

Returns:

  • A slice of GitLab merge request objects and an error, if any.

Example usage:

groupMRs, err := api.ListGroupMRs(client, "my-group", &gitlab.ListGroupMergeRequestsOptions{},
	api.WithMRAssignees([]int{123}),
	api.WithMRReviewers([]int{456, 789}))

func NewHTTPRequest

func NewHTTPRequest(ctx context.Context, c *Client, method string, baseURL *url.URL, body io.Reader, headers []string, bodyIsJSON bool) (*http.Request, error)

func PipelineJobWithSha

func PipelineJobWithSha(client *gitlab.Client, pid any, sha, name string) (*gitlab.Job, error)

func PipelineJobsWithID

func PipelineJobsWithID(client *gitlab.Client, pid any, ppid int) ([]*gitlab.Job, []*gitlab.Bridge, error)

PipelineJobsWithID returns a list of jobs in a pipeline for a id. The jobs are returned in the order in which they were created

func PlayOrRetryJobs

func PlayOrRetryJobs(client *gitlab.Client, repo string, jobID int, status string) (*gitlab.Job, error)

func UserByName

func UserByName(client *gitlab.Client, name string) (*gitlab.User, error)

Types

type BuildInfo

type BuildInfo struct {
	Version, Commit, Platform, Architecture string
}

func (BuildInfo) UserAgent

func (i BuildInfo) UserAgent() string

type CliListMROption

type CliListMROption func(*cliListMROptions)

func WithMRAssignees

func WithMRAssignees(assigneeIds []int) CliListMROption

func WithMRReviewers

func WithMRReviewers(reviewerIds []int) CliListMROption

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents an argument to NewClient

func NewClient

func NewClient(newAuthSource newAuthSource, options ...ClientOption) (*Client, error)

NewClient initializes a api client for use throughout glab.

func NewClientFromConfig

func NewClientFromConfig(repoHost string, cfg config.Config, isGraphQL bool, userAgent string) (*Client, error)

NewClientFromConfig initializes the global api with the config data

func (*Client) AuthSource

func (c *Client) AuthSource() gitlab.AuthSource

AuthSource returns the auth source TODO: clarify use cases for this.

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

func (*Client) Lab

func (c *Client) Lab() *gitlab.Client

Lab returns the initialized GitLab client.

type ClientOption

type ClientOption func(*Client) error

ClientOption represents a function that configures a Client

func WithBaseURL

func WithBaseURL(baseURL string) ClientOption

WithBaseURL configures the base URL for the GitLab instance

func WithClientCertificate

func WithClientCertificate(certFile, keyFile string) ClientOption

WithClientCertificate configures the client to use client certificates for mTLS

func WithCustomCA

func WithCustomCA(caFile string) ClientOption

WithCustomCA configures the client to use a custom CA certificate

func WithGitLabClient

func WithGitLabClient(client *gitlab.Client) ClientOption

WithGitLabClient configures the GitLab client

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) ClientOption

WithHTTPClient configures the HTTP client

func WithInsecureSkipVerify

func WithInsecureSkipVerify(skip bool) ClientOption

WithInsecureSkipVerify configures the client to skip TLS verification

func WithUserAgent

func WithUserAgent(userAgent string) ClientOption

WithUserAgent configures the user agent to use

type ListMilestonesOptions

type ListMilestonesOptions struct {
	IIDs                    []int
	State                   *string
	Title                   *string
	Search                  *string
	IncludeParentMilestones *bool
	PerPage                 int
	Page                    int
}

func (*ListMilestonesOptions) ListGroupMilestonesOptions

func (opts *ListMilestonesOptions) ListGroupMilestonesOptions() *gitlab.ListGroupMilestonesOptions

func (*ListMilestonesOptions) ListProjectMilestonesOptions

func (opts *ListMilestonesOptions) ListProjectMilestonesOptions() *gitlab.ListMilestonesOptions

type Milestone

type Milestone struct {
	ID    int
	Title string
}

func NewGroupMilestone

func NewGroupMilestone(m *gitlab.GroupMilestone) *Milestone

func NewProjectMilestone

func NewProjectMilestone(m *gitlab.Milestone) *Milestone

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL