api

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	NoToken authType = iota
	OAuthToken
	PrivateToken
)
View Source
const (
	NamespaceKindUser  = "user"
	NamespaceKindGroup = "group"
)

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

View Source
const UserAgent = "GLab - GitLab CLI"

Variables

View Source
var ApproveMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.ApproveMergeRequestOptions) (*gitlab.MergeRequestApprovals, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	mr, _, err := client.MergeRequestApprovals.ApproveMergeRequest(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}
View Source
var CancelPipelineJob = func(client *gitlab.Client, repo string, jobID int) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipe, _, err := client.Jobs.CancelJob(repo, jobID)
	if err != nil {
		return nil, err
	}
	return pipe, nil
}
View Source
var CreateBranch = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateBranchOptions) (*gitlab.Branch, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	branch, _, err := client.Branches.CreateBranch(projectID, opts)
	if err != nil {
		return nil, err
	}

	return branch, nil
}
View Source
var CreateGroupVariable = func(client *gitlab.Client, groupID interface{}, opts *gitlab.CreateGroupVariableOptions) (*gitlab.GroupVariable, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	vars, _, err := client.GroupVariables.CreateVariable(groupID, opts)
	if err != nil {
		return nil, err
	}

	return vars, nil
}
View Source
var CreateIssue = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateIssueOptions) (*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	issue, _, err := client.Issues.CreateIssue(projectID, opts)
	if err != nil {
		return nil, err
	}

	return issue, nil
}
View Source
var CreateIssueBoard = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateIssueBoardOptions) (*gitlab.IssueBoard, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	board, _, err := client.Boards.CreateIssueBoard(projectID, opts)
	if err != nil {
		return nil, err
	}

	return board, nil
}
View Source
var CreateIssueNote = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.CreateIssueNoteOptions) (*gitlab.Note, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	note, _, err := client.Notes.CreateIssueNote(projectID, mrID, opts)
	if err != nil {
		return note, err
	}

	return note, nil
}
View Source
var CreateLabel = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateLabelOptions) (*gitlab.Label, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	label, _, err := client.Labels.CreateLabel(projectID, opts)
	if err != nil {
		return nil, err
	}
	return label, nil
}
View Source
var CreateMR = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateMergeRequestOptions) (*gitlab.MergeRequest, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	mr, _, err := client.MergeRequests.CreateMergeRequest(projectID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}
View Source
var CreateMRNote = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.CreateMergeRequestNoteOptions) (*gitlab.Note, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	note, _, err := client.Notes.CreateMergeRequestNote(projectID, mrID, opts)
	if err != nil {
		return note, err
	}

	return note, nil
}
View Source
var CreatePipeline = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreatePipelineOptions) (*gitlab.Pipeline, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipe, _, err := client.Pipelines.CreatePipeline(projectID, opts)
	return pipe, err
}
View Source
var CreateProject = func(client *gitlab.Client, opts *gitlab.CreateProjectOptions) (*gitlab.Project, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	project, _, err := client.Projects.CreateProject(opts)
	if err != nil {
		return nil, err
	}
	return project, nil
}
View Source
var CreateProjectVariable = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateProjectVariableOptions) (*gitlab.ProjectVariable, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	vars, _, err := client.ProjectVariables.CreateVariable(projectID, opts)
	if err != nil {
		return nil, err
	}

	return vars, nil
}
View Source
var CreatePullMirror = func(
	client *gitlab.Client,
	projectID interface{},
	opts *CreatePullMirrorOptions,
) error {
	if client == nil {
		client = apiClient.Lab()
	}
	opt := &gitlab.EditProjectOptions{
		ImportURL:                   &opts.Url,
		Mirror:                      &opts.Enabled,
		OnlyMirrorProtectedBranches: &opts.OnlyProtectedBranches,
	}
	_, _, err := client.Projects.EditProject(projectID, opt)
	return err
}
View Source
var CreatePushMirror = func(
	client *gitlab.Client,
	projectID interface{},
	opts *CreatePushMirrorOptions,
) (*gitlab.ProjectMirror, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	opt := &gitlab.AddProjectMirrorOptions{
		URL:                   &opts.Url,
		Enabled:               &opts.Enabled,
		OnlyProtectedBranches: &opts.OnlyProtectedBranches,
		KeepDivergentRefs:     &opts.KeepDivergentRefs,
	}
	pm, _, err := client.ProjectMirrors.AddProjectMirror(projectID, opt)
	return pm, err
}
View Source
var CreateRelease = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateReleaseOptions) (*gitlab.Release, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	release, _, err := client.Releases.CreateRelease(projectID, opts)
	if err != nil {
		return nil, err
	}

	return release, nil
}
View Source
var CurrentUser = func(client *gitlab.Client) (*gitlab.User, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	u, _, err := client.Users.CurrentUser()
	if err != nil {
		return nil, err
	}
	return u, nil
}
View Source
var CurrentUserEvents = func(client *gitlab.Client) ([]*gitlab.ContributionEvent, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	events, _, err := client.Events.ListCurrentUserContributionEvents(&gitlab.ListContributionEventsOptions{})
	if err != nil {
		return nil, err
	}
	return events, nil
}
View Source
var DefaultListLimit = 30
View Source
var DeleteGroupVariable = func(client *gitlab.Client, groupID interface{}, key string) error {
	if client == nil {
		client = apiClient.Lab()
	}

	_, err := client.GroupVariables.RemoveVariable(groupID, key)

	if err != nil {
		return err
	}

	return nil
}
View Source
var DeleteIssue = func(client *gitlab.Client, projectID interface{}, issueID int) error {
	if client == nil {
		client = apiClient.Lab()
	}

	_, err := client.Issues.DeleteIssue(projectID, issueID)
	if err != nil {
		return err
	}

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

	return nil
}
View Source
var DeletePipeline = func(client *gitlab.Client, projectID interface{}, pipeID int) error {
	if client == nil {
		client = apiClient.Lab()
	}
	_, err := client.Pipelines.DeletePipeline(projectID, pipeID)
	if err != nil {
		return err
	}
	return nil
}
View Source
var DeleteProject = func(client *gitlab.Client, projectID interface{}) (*gitlab.Response, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	project, err := client.Projects.DeleteProject(projectID)
	if err != nil {
		return nil, err
	}
	return project, nil
}
View Source
var DeleteProjectVariable = func(client *gitlab.Client, projectID interface{}, key string, scope string) error {
	if client == nil {
		client = apiClient.Lab()
	}

	var filter = func(request *retryablehttp.Request) error {
		q := request.URL.Query()
		q.Add("filter[environment_scope]", scope)

		request.URL.RawQuery = q.Encode()

		return nil
	}

	_, err := client.ProjectVariables.RemoveVariable(projectID, key, filter)

	if err != nil {
		return err
	}

	return nil
}
View Source
var ErasePipelineJob = func(client *gitlab.Client, pid int, repo string) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipe, _, err := client.Jobs.EraseJob(repo, pid)
	if err != nil {
		return nil, err
	}
	return pipe, nil
}
View Source
var ForkProject = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ForkProjectOptions) (*gitlab.Project, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	project, _, err := client.Projects.ForkProject(projectID, opts)
	if err != nil {
		return nil, err
	}
	return project, nil
}
View Source
var GetCommit = func(client *gitlab.Client, repo string, ref string) (*gitlab.Commit, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	c, _, err := client.Commits.GetCommit(repo, ref)
	if err != nil {
		return nil, err
	}
	return c, nil
}
View Source
var GetCommitStatuses = func(client *gitlab.Client, pid interface{}, sha string) ([]*gitlab.CommitStatus, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	opt := &gitlab.GetCommitStatusesOptions{
		All: gitlab.Bool(true),
	}

	statuses, _, err := client.Commits.GetCommitStatuses(pid, sha, opt, nil)
	if err != nil {
		return nil, err
	}
	return statuses, nil
}
View Source
var GetFile = func(client *gitlab.Client, projectID interface{}, path string, ref string) (*gitlab.File, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	fileOpts := &gitlab.GetFileOptions{
		Ref: &ref,
	}
	file, _, err := client.RepositoryFiles.GetFile(projectID, path, fileOpts)

	if err != nil {
		return nil, err
	}

	return file, nil
}

GetFile retrieves a file from repository. Note that file content is Base64 encoded.

View Source
var GetGroup = func(client *gitlab.Client, groupID interface{}) (*gitlab.Group, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	group, _, err := client.Groups.GetGroup(groupID, &gitlab.GetGroupOptions{})
	if err != nil {
		return nil, err
	}
	return group, nil
}
View Source
var GetIssue = func(client *gitlab.Client, projectID interface{}, issueID int) (*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	issue, _, err := client.Issues.GetIssue(projectID, issueID)
	if err != nil {
		return nil, err
	}

	return issue, nil
}
View Source
var GetIssueBoardLists = func(client *gitlab.Client, projectID interface{}, boardID int, opts *gitlab.GetIssueBoardListsOptions) ([]*gitlab.BoardList, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	boardLists, _, err := client.Boards.GetIssueBoardLists(projectID, boardID, opts)
	if err != nil {
		return nil, err
	}

	return boardLists, nil
}
View Source
var GetJobs = func(client *gitlab.Client, repo string, opts *gitlab.ListJobsOptions) ([]*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	if opts == nil {
		opts = &gitlab.ListJobsOptions{}
	}
	jobs, _, err := client.Jobs.ListProjectJobs(repo, opts)
	if err != nil {
		return nil, err
	}
	return jobs, nil
}
View Source
var GetLastPipeline = func(client *gitlab.Client, repo string, ref string) (*gitlab.PipelineInfo, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	c, _, err := client.Commits.GetCommit(repo, ref)
	if err != nil {
		return nil, err
	}
	if c.LastPipeline != nil {
		return c.LastPipeline, nil
	}

	l := &gitlab.ListProjectPipelinesOptions{
		Ref:  gitlab.String(ref),
		Sort: gitlab.String("desc"),
	}

	l.Page = 1
	l.PerPage = 1

	pipes, err := GetPipelines(client, l, repo)
	if err != nil {
		return nil, err
	}

	if len(pipes) == 0 {
		return nil, errors.New("No pipeline running or available for ref " + ref)
	}

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

	return mr, nil
}
View Source
var GetMRApprovalState = func(client *gitlab.Client, projectID interface{}, mrID int, opts ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovalState, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	mrApprovals, _, err := client.MergeRequestApprovals.GetApprovalState(projectID, mrID, opts...)
	if err != nil {
		return nil, err
	}

	return mrApprovals, nil
}
View Source
var GetMRLinkedIssues = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.GetIssuesClosedOnMergeOptions) ([]*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	mrIssues, _, err := client.MergeRequests.GetIssuesClosedOnMerge(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mrIssues, nil
}
View Source
var GetPipelineFromBranch = func(client *gitlab.Client, ref, repo string) ([]*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	var err error
	if ref == "" {
		ref, err = git.CurrentBranch()
		if err != nil {
			return nil, err
		}
	}
	l := &gitlab.ListProjectPipelinesOptions{
		Ref:  gitlab.String(ref),
		Sort: gitlab.String("desc"),
	}
	l.Page = 1
	l.PerPage = 1
	pipeline, err := GetLastPipeline(client, repo, ref)
	if err != nil {
		return nil, err
	}
	jobs, err := GetPipelineJobs(client, pipeline.ID, repo)
	if err != nil {
		return nil, err
	}
	return jobs, nil
}
View Source
var GetPipelineJob = func(client *gitlab.Client, jid int, repo string) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	job, _, err := client.Jobs.GetJob(repo, jid)
	return job, err
}
View Source
var GetPipelineJobLog = func(client *gitlab.Client, jobID int, repo string) (io.Reader, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipeJoblog, _, err := client.Jobs.GetTraceFile(repo, jobID)
	if err != nil {
		return nil, err
	}
	return pipeJoblog, nil
}
View Source
var GetPipelineJobs = func(client *gitlab.Client, pid int, repo string) ([]*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipeJobs := make([]*gitlab.Job, 0, 10)
	listOptions := &gitlab.ListJobsOptions{
		ListOptions: gitlab.ListOptions{
			PerPage: 100,
		},
	}
	for {
		pageJobs, resp, err := client.Jobs.ListPipelineJobs(repo, pid, listOptions)
		if err != nil {
			return nil, err
		}
		pipeJobs = append(pipeJobs, pageJobs...)
		if resp.CurrentPage == resp.TotalPages {
			break
		}
		listOptions.Page = resp.NextPage
		if resp.CurrentPage >= resp.TotalPages {
			break
		}
	}
	return pipeJobs, nil
}
View Source
var GetPipelines = func(client *gitlab.Client, l *gitlab.ListProjectPipelinesOptions, repo interface{}) ([]*gitlab.PipelineInfo, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if l.PerPage == 0 {
		l.PerPage = DefaultListLimit
	}

	pipes, _, err := client.Pipelines.ListProjectPipelines(repo, l)
	if err != nil {
		return nil, err
	}
	return pipes, nil
}
View Source
var GetProject = func(client *gitlab.Client, projectID interface{}) (*gitlab.Project, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	opts := &gitlab.GetProjectOptions{
		Statistics:           gitlab.Bool(true),
		License:              gitlab.Bool(true),
		WithCustomAttributes: gitlab.Bool(true),
	}
	project, _, err := client.Projects.GetProject(projectID, opts)
	if err != nil {
		return nil, err
	}
	return project, nil
}
View Source
var GetRelease = func(client *gitlab.Client, projectID interface{}, tag string) (*gitlab.Release, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	release, _, err := client.Releases.GetRelease(projectID, tag)
	if err != nil {
		return nil, err
	}

	return release, nil
}
View Source
var GetSinglePipeline = func(client *gitlab.Client, pid int, repo string) (*gitlab.Pipeline, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipes, _, err := client.Pipelines.GetPipeline(repo, pid)
	if err != nil {
		return nil, err
	}
	return pipes, nil
}
View Source
var LinkIssues = func(client *gitlab.Client, projectID interface{}, issueIDD int, opts *gitlab.CreateIssueLinkOptions) (*gitlab.Issue, *gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	issueLink, _, err := client.IssueLinks.CreateIssueLink(projectID, issueIDD, opts)
	if err != nil {
		return nil, nil, err
	}

	return issueLink.SourceIssue, issueLink.TargetIssue, nil
}
View Source
var ListAllMilestones = func(client *gitlab.Client, projectID interface{}, 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 ListGroupIssues = func(client *gitlab.Client, groupID interface{}, opts *gitlab.ListGroupIssuesOptions) ([]*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}
	issues, _, err := client.Issues.ListGroupIssues(groupID, opts)
	if err != nil {
		return nil, err
	}

	return issues, nil
}
View Source
var ListGroupMRs = func(client *gitlab.Client, groupID interface{}, opts *gitlab.ListGroupMergeRequestsOptions) ([]*gitlab.MergeRequest, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	mrs, _, err := client.MergeRequests.ListGroupMergeRequests(groupID, opts)
	if err != nil {
		return nil, err
	}

	return mrs, nil
}
View Source
var ListGroupMilestones = func(client *gitlab.Client, groupID interface{}, opts *gitlab.ListGroupMilestonesOptions) ([]*gitlab.GroupMilestone, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	milestone, _, err := client.GroupMilestones.ListGroupMilestones(groupID, opts)
	if err != nil {
		return nil, err
	}
	return milestone, nil
}
View Source
var ListGroupProjects = func(client *gitlab.Client, groupID interface{}, opts *gitlab.ListGroupProjectsOptions) ([]*gitlab.Project, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	project, _, err := client.Groups.ListGroupProjects(groupID, opts)
	if err != nil {
		return nil, err
	}
	return project, nil
}
View Source
var ListGroupVariables = func(client *gitlab.Client, groupID interface{}, opts *gitlab.ListGroupVariablesOptions) ([]*gitlab.GroupVariable, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	vars, _, err := client.GroupVariables.ListVariables(groupID, opts)
	if err != nil {
		return nil, err
	}

	return vars, nil
}
View Source
var ListIssueBoards = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListIssueBoardsOptions) ([]*gitlab.IssueBoard, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	boards, _, err := client.Boards.ListIssueBoards(projectID, opts)
	if err != nil {
		return nil, err
	}

	return boards, nil
}
View Source
var ListIssueNotes = func(client *gitlab.Client, projectID interface{}, issueID int, opts *gitlab.ListIssueNotesOptions) ([]*gitlab.Note, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}
	notes, _, err := client.Notes.ListIssueNotes(projectID, issueID, opts)
	if err != nil {
		return nil, err
	}
	return notes, nil
}
View Source
var ListIssues = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectIssuesOptions) ([]*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}
	issues, _, err := client.Issues.ListProjectIssues(projectID, opts)
	if err != nil {
		return nil, err
	}

	return issues, nil
}
View Source
var ListLabels = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	label, _, err := client.Labels.ListLabels(projectID, opts)
	if err != nil {
		return nil, err
	}
	return label, nil
}
View Source
var ListMRNotes = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.ListMergeRequestNotesOptions) ([]*gitlab.Note, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	notes, _, err := client.Notes.ListMergeRequestNotes(projectID, mrID, opts)
	if err != nil {
		return notes, err
	}

	return notes, nil
}
View Source
var ListMRs = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.MergeRequest, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	mrs, _, err := client.MergeRequests.ListProjectMergeRequests(projectID, opts)
	if err != nil {
		return nil, err
	}

	return mrs, nil
}
View Source
var ListMRsWithAssigneesOrReviewers = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions, assigneeIds []int, reviewerIds []int) ([]*gitlab.MergeRequest, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	mrs := make([]*gitlab.MergeRequest, 0)
	for _, id := range assigneeIds {
		opts.AssigneeID = gitlab.Int(id)
		assingeMrs, err := ListMRs(client, projectID, opts)
		if err != nil {
			return nil, err
		}
		mrs = append(mrs, assingeMrs...)
	}
	opts.AssigneeID = nil
	for _, id := range reviewerIds {
		opts.ReviewerID = gitlab.Int(id)
		reviewerMrs, err := ListMRs(client, projectID, opts)
		if err != nil {
			return nil, err
		}
		mrs = append(mrs, reviewerMrs...)
	}
	return mrs, nil
}
View Source
var ListProjectMembers = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMembersOptions) ([]*gitlab.ProjectMember, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	members, _, err := client.ProjectMembers.ListAllProjectMembers(projectID, opts)
	if err != nil {
		return nil, err
	}
	return members, nil
}
View Source
var ListProjectMilestones = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListMilestonesOptions) ([]*gitlab.Milestone, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	milestone, _, err := client.Milestones.ListMilestones(projectID, opts)
	if err != nil {
		return nil, err
	}
	return milestone, nil
}
View Source
var ListProjectPipelines = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectPipelinesOptions) ([]*gitlab.PipelineInfo, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipes, _, err := client.Pipelines.ListProjectPipelines(projectID, opts)
	if err != nil {
		return pipes, err
	}
	return pipes, nil
}
View Source
var ListProjectVariables = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectVariablesOptions) ([]*gitlab.ProjectVariable, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	vars, _, err := client.ProjectVariables.ListVariables(projectID, opts)
	if err != nil {
		return nil, err
	}

	return vars, nil
}
View Source
var ListReleases = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListReleasesOptions) ([]*gitlab.Release, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	releases, _, err := client.Releases.ListReleases(projectID, opts)
	if err != nil {
		return nil, err
	}

	return releases, nil
}
View Source
var MRTodo = func(client *gitlab.Client, projectID interface{}, mrID int, opts gitlab.RequestOptionFunc) (*gitlab.Todo, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	mr, _, err := client.MergeRequests.CreateTodo(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}
View Source
var MergeMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.AcceptMergeRequestOptions) (*gitlab.MergeRequest, *gitlab.Response, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	mrs, resp, err := client.MergeRequests.AcceptMergeRequest(projectID, mrID, opts)
	if err != nil {
		return nil, resp, err
	}

	return mrs, resp, nil
}
View Source
var PipelineCILint = func(client *gitlab.Client, content string) (*gitlab.LintResult, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	c, _, err := client.Validate.Lint(content)
	if err != nil {
		return nil, err
	}
	return c, nil
}
View Source
var PipelineJobWithSha = func(client *gitlab.Client, pid interface{}, sha, name string) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	jobs, err := PipelineJobsWithSha(client, pid, sha)
	if len(jobs) == 0 || err != nil {
		return nil, err
	}
	var (
		job          *gitlab.Job
		lastRunning  *gitlab.Job
		firstPending *gitlab.Job
	)

	for _, j := range jobs {
		if j.Status == "running" {
			lastRunning = j
		}
		if j.Status == "pending" && firstPending == nil {
			firstPending = j
		}
		if j.Name == name {
			job = j

		}
	}
	if job == nil {
		job = lastRunning
	}
	if job == nil {
		job = firstPending
	}
	if job == nil {
		job = jobs[len(jobs)-1]
	}
	return job, err
}
View Source
var PipelineJobsWithSha = func(client *gitlab.Client, pid interface{}, sha string) ([]*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipelines, err := GetPipelines(client, &gitlab.ListProjectPipelinesOptions{
		SHA: gitlab.String(sha),
	}, pid)
	if len(pipelines) == 0 || err != nil {
		return nil, err
	}
	target := pipelines[0].ID
	opts := &gitlab.ListJobsOptions{
		ListOptions: gitlab.ListOptions{
			PerPage: 500,
		},
	}
	jobsList := make([]*gitlab.Job, 0)
	for {
		jobs, resp, err := client.Jobs.ListPipelineJobs(pid, target, opts)
		if err != nil {
			return nil, err
		}
		opts.Page = resp.NextPage
		jobsList = append(jobsList, jobs...)
		if resp.CurrentPage == resp.TotalPages {
			break
		}
	}

	sort.Sort(JobSort{Jobs: jobsList})
	return jobsList, nil
}

PipelineJobsWithSha returns a list of jobs in a pipeline for a given commit sha. The jobs are returned in the order in which they were created

View Source
var PlayOrRetryJobs = func(client *gitlab.Client, repo string, jobID int, status string) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	switch status {
	case "pending", "running":
		return nil, nil
	case "manual":
		j, err := PlayPipelineJob(client, jobID, repo)
		if err != nil {
			return nil, err
		}
		return j, nil
	default:

		j, err := RetryPipelineJob(client, jobID, repo)
		if err != nil {
			return nil, err
		}

		return j, nil
	}
}
View Source
var PlayPipelineJob = func(client *gitlab.Client, pid int, repo string) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipe, _, err := client.Jobs.PlayJob(repo, pid)
	if err != nil {
		return nil, err
	}
	return pipe, nil
}
View Source
var ProjectListIssueOptionsToGroup = func(l *gitlab.ListProjectIssuesOptions) *gitlab.ListGroupIssuesOptions {
	return &gitlab.ListGroupIssuesOptions{
		ListOptions:        l.ListOptions,
		State:              l.State,
		Labels:             l.Labels,
		NotLabels:          l.NotLabels,
		WithLabelDetails:   l.WithLabelDetails,
		IIDs:               l.IIDs,
		Milestone:          l.Milestone,
		Scope:              l.Scope,
		AuthorID:           l.AuthorID,
		NotAuthorID:        l.NotAuthorID,
		AssigneeID:         l.AssigneeID,
		NotAssigneeID:      l.NotAssigneeID,
		AssigneeUsername:   l.AssigneeUsername,
		MyReactionEmoji:    l.MyReactionEmoji,
		NotMyReactionEmoji: l.NotMyReactionEmoji,
		OrderBy:            l.OrderBy,
		Sort:               l.Sort,
		Search:             l.Search,
		In:                 l.In,
		CreatedAfter:       l.CreatedAfter,
		CreatedBefore:      l.CreatedBefore,
		UpdatedAfter:       l.UpdatedAfter,
		UpdatedBefore:      l.UpdatedBefore,
		IssueType:          l.IssueType,
	}
}
View Source
var ProjectListMROptionsToGroup = func(l *gitlab.ListProjectMergeRequestsOptions) *gitlab.ListGroupMergeRequestsOptions {
	return &gitlab.ListGroupMergeRequestsOptions{
		ListOptions:            l.ListOptions,
		State:                  l.State,
		OrderBy:                l.OrderBy,
		Sort:                   l.Sort,
		Milestone:              l.Milestone,
		View:                   l.View,
		Labels:                 l.Labels,
		NotLabels:              l.NotLabels,
		WithLabelsDetails:      l.WithLabelsDetails,
		WithMergeStatusRecheck: l.WithMergeStatusRecheck,
		CreatedAfter:           l.CreatedAfter,
		CreatedBefore:          l.CreatedBefore,
		UpdatedAfter:           l.UpdatedAfter,
		UpdatedBefore:          l.UpdatedBefore,
		Scope:                  l.Scope,
		AuthorID:               l.AuthorID,
		AssigneeID:             l.AssigneeID,
		ReviewerID:             l.ReviewerID,
		ReviewerUsername:       l.ReviewerUsername,
		MyReactionEmoji:        l.MyReactionEmoji,
		SourceBranch:           l.SourceBranch,
		TargetBranch:           l.TargetBranch,
		Search:                 l.Search,
		WIP:                    l.WIP,
	}
}
View Source
var ProjectMilestoneByTitle = func(client *gitlab.Client, projectID interface{}, name string) (*gitlab.Milestone, error) {
	opts := &gitlab.ListMilestonesOptions{Title: gitlab.String(name)}

	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	milestones, _, err := client.Milestones.ListMilestones(projectID, opts)
	if err != nil {
		return nil, err
	}

	if len(milestones) != 1 {
		return nil, fmt.Errorf("failed to find milestone by title: %s", name)
	}

	return milestones[0], nil
}
View Source
var RebaseMR = func(client *gitlab.Client, projectID interface{}, mrID int) error {
	if client == nil {
		client = apiClient.Lab()
	}

	_, err := client.MergeRequests.RebaseMergeRequest(projectID, mrID)
	if err != nil {
		return err
	}

	return nil
}
View Source
var RetryPipeline = func(client *gitlab.Client, pid int, repo string) (*gitlab.Pipeline, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipe, _, err := client.Pipelines.RetryPipelineBuild(repo, pid)
	if err != nil {
		return nil, err
	}
	return pipe, nil
}
View Source
var RetryPipelineJob = func(client *gitlab.Client, pid int, repo string) (*gitlab.Job, error) {
	if client == nil {
		client = apiClient.Lab()
	}
	pipe, _, err := client.Jobs.RetryJob(repo, pid)
	if err != nil {
		return nil, err
	}
	return pipe, nil
}
View Source
var SubscribeToIssue = func(client *gitlab.Client, projectID interface{}, issueID int, opts gitlab.RequestOptionFunc) (*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	issue, resp, err := client.Issues.SubscribeToIssue(projectID, issueID, opts)
	if err != nil {
		if resp != nil {

			if resp.StatusCode == 304 {
				return nil, errors.New("you are already subscribed to this issue")
			}
		}
		return issue, err
	}

	return issue, nil
}
View Source
var SubscribeToMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts gitlab.RequestOptionFunc) (*gitlab.MergeRequest, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	mr, _, err := client.MergeRequests.SubscribeToMergeRequest(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}
View Source
var UnapproveMR = func(client *gitlab.Client, projectID interface{}, mrID int) error {
	if client == nil {
		client = apiClient.Lab()
	}

	_, err := client.MergeRequestApprovals.UnapproveMergeRequest(projectID, mrID)
	if err != nil {
		return err
	}

	return nil
}
View Source
var UnsubscribeFromIssue = func(client *gitlab.Client, projectID interface{}, issueID int, opts gitlab.RequestOptionFunc) (*gitlab.Issue, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	issue, resp, err := client.Issues.UnsubscribeFromIssue(projectID, issueID, opts)
	if err != nil {
		if resp != nil {

			if resp.StatusCode == 304 {
				return nil, errors.New("you are not subscribed to this issue")
			}
		}
		return issue, err
	}

	return issue, nil
}
View Source
var UnsubscribeFromMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts gitlab.RequestOptionFunc) (*gitlab.MergeRequest, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	mr, _, err := client.MergeRequests.UnsubscribeFromMergeRequest(projectID, mrID, opts)
	if err != nil {
		return nil, err
	}

	return mr, nil
}
View Source
var UpdateGroupVariable = func(client *gitlab.Client, groupID interface{}, key string, opts *gitlab.UpdateGroupVariableOptions) (*gitlab.GroupVariable, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	vars, _, err := client.GroupVariables.UpdateVariable(groupID, key, opts)
	if err != nil {
		return nil, err
	}

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

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

	return mr, nil
}
View Source
var UpdateProjectVariable = func(client *gitlab.Client, projectID interface{}, key string, opts *gitlab.UpdateProjectVariableOptions) (*gitlab.ProjectVariable, error) {
	if client == nil {
		client = apiClient.Lab()
	}

	var filter = func(request *retryablehttp.Request) error {
		q := request.URL.Query()
		q.Add("filter[environment_scope]", *opts.EnvironmentScope)

		request.URL.RawQuery = q.Encode()

		return nil
	}

	vars, _, err := client.ProjectVariables.UpdateVariable(projectID, key, opts, filter)
	if err != nil {
		return nil, err
	}

	return vars, nil
}
View Source
var UserByName = func(client *gitlab.Client, name string) (*gitlab.User, error) {
	opts := &gitlab.ListUsersOptions{Username: gitlab.String(name)}

	if client == nil {
		client = apiClient.Lab()
	}

	if opts.PerPage == 0 {
		opts.PerPage = DefaultListLimit
	}

	if name == "@me" {
		return CurrentUser(client)
	}

	users, _, err := client.Users.ListUsers(opts)
	if err != nil {
		return nil, err
	}

	if len(users) != 1 {
		return nil, fmt.Errorf("failed to find user by name : %s", name)
	}

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

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

Functions

func HTTPClient

func HTTPClient() *http.Client

HTTPClient returns the httpClient instance used to initialise the gitlab api client

func IsValidToken

func IsValidToken(token string) bool

IsValidToken checks if a token provided is valid. The token string must be 20 characters in length to be recognized as a valid personal access token.

TODO: check if token has minimum scopes required by glab

func NewHTTPRequest added in v1.18.0

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

func OverrideHTTPClient

func OverrideHTTPClient(client *http.Client)

OverrideHTTPClient overrides the default http client

func RefreshClient

func RefreshClient()

RefreshClient re-initializes the api client

func SetProtocol

func SetProtocol(protocol string)

func Token

func Token() string

Token returns the authentication token

Types

type Client

type Client struct {
	// LabClient represents GitLab API client.
	// Note: this is exported for tests. Do not access it directly. Use Lab() method
	LabClient *gitlab.Client

	// Token type used to make authenticated API calls.
	AuthType authType

	// Protocol: host url protocol to make requests. Default is https
	Protocol string
	// contains filtered or unexported fields
}

Client represents an argument to NewClient

func GetClient

func GetClient() *Client

GetClient returns the global Client instance.

func NewClient

func NewClient(host, token string, allowInsecure bool, isGraphQL bool) (*Client, error)

NewClient initializes a api client for use throughout glab.

func NewClientWithCfg

func NewClientWithCfg(repoHost string, cfg config.Config, isGraphQL bool) (client *Client, err error)

NewClientWithCfg initializes the global api with the config data

func NewClientWithCustomCA

func NewClientWithCustomCA(host, token, caFile string, isGraphQL bool) (*Client, error)

NewClientWithCustomCA initializes the global api client with a self-signed certificate

func NewClientWithCustomCAClientCert added in v1.20.0

func NewClientWithCustomCAClientCert(host, token, caFile string, certFile string, keyFile string, isGraphQL bool) (*Client, error)

NewClientWithCustomCAClientCert initializes the global api client with a self-signed certificate and client certificates

func TestClient

func TestClient(httpClient *http.Client, token, host string, isGraphQL bool) (*Client, error)

func (*Client) BaseURL

func (c *Client) BaseURL() *url.URL

BaseURL returns a copy of the BaseURL

func (*Client) HTTPClient

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

func (*Client) Lab

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

Lab returns the initialized GitLab client. Initializes a new GitLab Client if not initialized but error is ignored

func (*Client) NewLab

func (c *Client) NewLab() error

NewLab initializes the GitLab Client

func (*Client) OverrideHTTPClient

func (c *Client) OverrideHTTPClient(client *http.Client)

func (*Client) SetProtocol

func (c *Client) SetProtocol(protocol string)

func (*Client) Token

func (c *Client) Token() string

type CreatePullMirrorOptions added in v1.22.0

type CreatePullMirrorOptions struct {
	Url                   string
	Enabled               bool
	OnlyProtectedBranches bool
}

type CreatePushMirrorOptions added in v1.22.0

type CreatePushMirrorOptions struct {
	Url                   string
	Enabled               bool
	OnlyProtectedBranches bool
	KeepDivergentRefs     bool
}

type JobSort

type JobSort struct {
	Jobs []*gitlab.Job
}

func (JobSort) Len

func (s JobSort) Len() int

func (JobSort) Less

func (s JobSort) Less(i, j int) bool

func (JobSort) Swap

func (s JobSort) Swap(i, j int)

type ListMilestonesOptions added in v1.21.0

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

func (*ListMilestonesOptions) ListGroupMilestonesOptions added in v1.21.0

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

func (*ListMilestonesOptions) ListProjectMilestonesOptions added in v1.21.0

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

type Milestone added in v1.21.0

type Milestone struct {
	ID    int
	Title string
}

func NewGroupMilestone added in v1.21.0

func NewGroupMilestone(m *gitlab.GroupMilestone) *Milestone

func NewProjectMilestone added in v1.21.0

func NewProjectMilestone(m *gitlab.Milestone) *Milestone

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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