mrutils

package
v1.62.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GetMRForBranch = func(apiClient *gitlab.Client, mrOpts MrOptions) (*gitlab.BasicMergeRequest, error) {
	owner, currentBranch := resolveOwnerAndBranch(mrOpts.Branch)

	opts := gitlab.ListProjectMergeRequestsOptions{
		SourceBranch: gitlab.Ptr(currentBranch),
	}

	userAskedForSpecificState := mrOpts.State != "" && mrOpts.State != "any"
	if userAskedForSpecificState {
		opts.State = gitlab.Ptr(mrOpts.State)
	}

	mrs, err := api.ListMRs(apiClient, mrOpts.BaseRepo.FullName(), &opts)
	if err != nil {
		return nil, fmt.Errorf("failed to get open merge request for %q: %w", currentBranch, err)
	}

	if len(mrs) == 0 {
		return nil, fmt.Errorf("no open merge request available for %q", currentBranch)
	}

	userAskedForSpecificOwner := owner != ""
	if userAskedForSpecificOwner {
		for i := range mrs {
			mr := mrs[i]
			matchFound := mr.Author.Username == owner
			if matchFound {
				return mr, nil
			}
		}
		return nil, fmt.Errorf("no open merge request available for %q owned by @%s", currentBranch, owner)
	}

	if len(mrs) == 1 {
		return mrs[0], nil
	}

	mrMap := map[string]*gitlab.BasicMergeRequest{}
	var mrNames []string
	for i := range mrs {
		t := fmt.Sprintf("!%d (%s) by @%s", mrs[i].IID, currentBranch, mrs[i].Author.Username)
		mrMap[t] = mrs[i]
		mrNames = append(mrNames, t)
	}
	pickedMR := mrNames[0]

	if !mrOpts.PromptEnabled {

		err = fmt.Errorf("merge request ID number required. Possible matches:\n\n%s", strings.Join(mrNames, "\n"))
	} else {
		err = prompt.Select(&pickedMR,
			"mr",
			"Multiple merge requests exist for this branch. Select one:",
			mrNames,
		)
	}
	if err != nil {
		return nil, fmt.Errorf("you must select a merge request: %w", err)
	}

	return mrMap[pickedMR], nil
}

Functions

func DisplayAllMRs

func DisplayAllMRs(streams *iostreams.IOStreams, mrs []*gitlab.BasicMergeRequest) string

func DisplayMR

func DisplayMR(c *iostreams.ColorPalette, mr *gitlab.BasicMergeRequest, isTTY bool) string

func MRCheckErrors

func MRCheckErrors(mr *gitlab.MergeRequest, err MRCheckErrOptions) error

MRCheckErrors checks and return merge request errors specified in MRCheckErrOptions{}

func MRFromArgs

func MRFromArgs(f cmdutils.Factory, args []string, state string) (*gitlab.MergeRequest, glrepo.Interface, error)

MRFromArgs is wrapper around MRFromArgsWithOpts without any custom options

func MRFromArgsWithOpts

func MRFromArgsWithOpts(
	f cmdutils.Factory,
	args []string,
	opts *gitlab.GetMergeRequestsOptions,
	state string,
) (*gitlab.MergeRequest, glrepo.Interface, error)

MRFromArgsWithOpts gets MR with custom request options passed down to it

func MRsFromArgs

func MRsFromArgs(f cmdutils.Factory, args []string, state string) ([]*gitlab.MergeRequest, glrepo.Interface, error)

func PrintMRApprovalState

func PrintMRApprovalState(ios *iostreams.IOStreams, mrApprovals *gitlab.MergeRequestApprovalState)

PrintMRApprovalState renders an output to summarize the approval state of a merge request

func RebaseMR

func RebaseMR(ios *iostreams.IOStreams, apiClient *gitlab.Client, repo glrepo.Interface, mr *gitlab.MergeRequest, rebaseOpts *gitlab.RebaseMergeRequestOptions) error

Types

type MRCheckErrOptions

type MRCheckErrOptions struct {
	// Draft: check and return err if merge request is a DRAFT
	Draft bool
	// Closed: check and return err if merge request is closed
	Closed bool
	// Merged: check and return err if merge request is already merged
	Merged bool
	// Opened: check and return err if merge request is already opened
	Opened bool
	// Conflict: check and return err if there are merge conflicts
	Conflict bool
	// PipelineStatus: check and return err pipeline did not succeed and it is required before merging
	PipelineStatus bool
	// MergePermitted: check and return err if user is not authorized to merge
	MergePermitted bool
	// Subscribed: check and return err if user is already subscribed to MR
	Subscribed bool
	// Unsubscribed: check and return err if user is already unsubscribed to MR
	Unsubscribed bool
	// MergePrivilege: check and return err if user is not authorized to merge
	MergePrivilege bool
}

type MrOptions

type MrOptions struct {
	BaseRepo      glrepo.Interface
	Branch        string
	State         string
	PromptEnabled bool
}

Jump to

Keyboard shortcuts

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