mrutils

package
v1.90.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

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

	userAskedForSpecificState := mrOpts.State != "" && mrOpts.State != "any"
	if userAskedForSpecificState {
		opts.State = new(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 = ios.Select(ctx, &pickedMR, multipleMRSelectQuestion, mrNames)
	}
	if err != nil {
		return nil, fmt.Errorf("you must select a merge request: %w", err)
	}

	return mrMap[pickedMR], nil
}
View Source
var ListAllDiscussions = func(ctx context.Context, client *gitlab.Client, projectID any, mrIID int64, opts *gitlab.ListMergeRequestDiscussionsOptions) ([]*gitlab.Discussion, error) {
	if opts == nil {
		opts = &gitlab.ListMergeRequestDiscussionsOptions{}
	}
	if opts.PerPage == 0 {
		opts.PerPage = api.DefaultListLimit
	}

	var allDiscussions []*gitlab.Discussion
	page := opts.Page
	if page == 0 {
		page = 1
	}

	for {
		opts.Page = page
		discussions, resp, err := client.Discussions.ListMergeRequestDiscussions(projectID, mrIID, opts, gitlab.WithContext(ctx))
		if err != nil {
			return nil, err
		}

		allDiscussions = append(allDiscussions, discussions...)

		if resp == nil || resp.NextPage == 0 {
			break
		}
		page = resp.NextPage
	}

	return allDiscussions, nil
}

ListAllDiscussions fetches all discussions for a merge request, paginating automatically.

Functions

func AutofillMRFromCommits added in v1.66.0

func AutofillMRFromCommits(targetBranch, sourceBranch string, fillCommitBody bool) (string, string, error)

AutofillMRFromCommits generates title and body from commit information between two branches

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 FilterDiscussions added in v1.90.0

func FilterDiscussions(discussions []*gitlab.Discussion, opts FilterOpts) []*gitlab.Discussion

FilterDiscussions filters discussions based on the provided options.

func FindDiscussionByNoteID added in v1.90.0

func FindDiscussionByNoteID(discussions []*gitlab.Discussion, noteID int64) (string, error)

FindDiscussionByNoteID finds the discussion containing a specific note ID. Returns the discussion ID, or an error if the note is not found.

func GenerateMRCommitListBody added in v1.66.0

func GenerateMRCommitListBody(commits []*git.Commit, fillCommitBody bool) (string, error)

GenerateMRCommitListBody creates a markdown list of commits with optional commit bodies

func GenerateMRTitleAndBody added in v1.66.0

func GenerateMRTitleAndBody(commits []*git.Commit, sourceBranch string, fillCommitBody bool) (string, string, error)

GenerateMRTitleAndBody creates title and body from commits

func MRCheckErrors

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

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

func MRFromArgs

func MRFromArgs(ctx context.Context, 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(
	ctx context.Context,
	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(ctx context.Context, f cmdutils.Factory, args []string, state string) ([]*gitlab.MergeRequest, glrepo.Interface, error)

func PrintCommentFileContext added in v1.90.0

func PrintCommentFileContext(out io.Writer, c *iostreams.ColorPalette, pos *gitlab.NotePosition)

PrintCommentFileContext prints file and line context for a note position.

func PrintDiscussionsRaw added in v1.90.0

func PrintDiscussionsRaw(out io.Writer, discussions []*gitlab.Discussion, showSystemLogs bool)

PrintDiscussionsRaw renders discussions as flat, chronologically sorted notes in raw format.

func PrintDiscussionsTTY added in v1.90.0

func PrintDiscussionsTTY(out io.Writer, ios *iostreams.IOStreams, discussions []*gitlab.Discussion, showSystemLogs bool)

PrintDiscussionsTTY renders discussions in TTY format to the given writer.

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

func ResolveDiscussionID added in v1.90.0

func ResolveDiscussionID(ctx context.Context, client *gitlab.Client, projectID any, mrIID int64, prefix string) (string, error)

ResolveDiscussionID resolves a prefix (8+ chars) to a full discussion ID. Returns an error if the prefix is ambiguous or not found.

func TruncateDiscussionID added in v1.90.0

func TruncateDiscussionID(id string) string

TruncateDiscussionID truncates a discussion ID to 8 characters with an ellipsis. If the ID is 8 characters or shorter, it is returned unchanged.

Types

type FilterOpts added in v1.90.0

type FilterOpts struct {
	// State filters by resolution status: "resolved", "unresolved", "resolvable", or "" for all.
	State string
	// Type filters by discussion type: "general", "diff", "system", or "" for all.
	Type string
	// FilePath filters to discussions on a specific file path.
	FilePath string
}

FilterOpts specifies how to filter discussions.

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