Documentation
¶
Index ¶
- Variables
- func AutofillMRFromCommits(targetBranch, sourceBranch string, fillCommitBody bool) (string, string, error)
- func BuildDiffPosition(version *gitlab.MergeRequestDiffVersion, fileDiff *gitlab.Diff, ...) (*gitlab.PositionOptions, error)
- func DisplayAllMRs(streams *iostreams.IOStreams, mrs []*gitlab.BasicMergeRequest) string
- func DisplayMR(c *iostreams.ColorPalette, mr *gitlab.BasicMergeRequest, isTTY bool) string
- func FilterDiscussions(discussions []*gitlab.Discussion, opts FilterOpts) []*gitlab.Discussion
- func FindFileDiff(version *gitlab.MergeRequestDiffVersion, filePath string) (*gitlab.Diff, error)
- func FindNoteInDiscussions(discussions []*gitlab.Discussion, noteID int64) (string, *gitlab.Note, error)
- func GenerateMRCommitListBody(commits []*git.Commit, fillCommitBody bool) (string, error)
- func GenerateMRTitleAndBody(commits []*git.Commit, sourceBranch string, fillCommitBody bool) (string, string, error)
- func MRCheckErrors(mr *gitlab.MergeRequest, err MRCheckErrOptions) error
- func MRFromArgs(ctx context.Context, f cmdutils.Factory, args []string, state string) (*gitlab.MergeRequest, glrepo.Interface, error)
- func MRFromArgsWithOpts(ctx context.Context, f cmdutils.Factory, args []string, ...) (*gitlab.MergeRequest, glrepo.Interface, error)
- func MRState(c *iostreams.ColorPalette, m *gitlab.BasicMergeRequest) string
- func MRWebURLFromArgs(ctx context.Context, f cmdutils.Factory, args []string, state string) (string, glrepo.Interface, error)
- func MRsFromArgs(ctx context.Context, f cmdutils.Factory, args []string, state string) ([]*gitlab.MergeRequest, glrepo.Interface, error)
- func ParseLine(s string) (int, int, error)
- func PrintCommentFileContext(out io.Writer, c *iostreams.ColorPalette, pos *gitlab.NotePosition)
- func PrintDiscussions(out io.Writer, ios *iostreams.IOStreams, discussions []*gitlab.Discussion, ...)
- func PrintMRApprovalState(ios *iostreams.IOStreams, mrApprovals *gitlab.MergeRequestApprovalState)
- func RebaseMR(ios *iostreams.IOStreams, apiClient *gitlab.Client, repo glrepo.Interface, ...) error
- func ResolveDiscussionID(ctx context.Context, client *gitlab.Client, projectID any, mrIID int64, ...) (string, error)
- func TruncateDiscussionID(id string) string
- type FilterOpts
- type MRCheckErrOptions
- type MrOptions
- type PrintDiscussionsOptions
Constants ¶
This section is empty.
Variables ¶
var GetLatestDiffVersion = func(client *gitlab.Client, project string, mrIID int64) (*gitlab.MergeRequestDiffVersion, error) { versions, _, err := client.MergeRequests.GetMergeRequestDiffVersions(project, mrIID, nil) if err != nil { return nil, fmt.Errorf("failed to list MR diff versions: %w", err) } if len(versions) == 0 { return nil, fmt.Errorf("no diff versions found for MR !%d", mrIID) } latest := versions[0] full, _, err := client.MergeRequests.GetSingleMergeRequestDiffVersion(project, mrIID, latest.ID, nil) if err != nil { return nil, fmt.Errorf("failed to fetch diff version %d: %w", latest.ID, err) } return full, nil }
GetLatestDiffVersion fetches MR diff versions and returns the latest one (with diffs included).
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 }
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 BuildDiffPosition ¶ added in v1.94.0
func BuildDiffPosition(version *gitlab.MergeRequestDiffVersion, fileDiff *gitlab.Diff, lineStart, lineEnd, oldLine int) (*gitlab.PositionOptions, error)
BuildDiffPosition builds a PositionOptions for a diff comment. lineStart/lineEnd refer to new-side lines (lineEnd > lineStart for multiline). oldLine refers to an old-side (removed) line. For file-level comments, pass lineStart=0 and oldLine=0.
When targeting an "unchanged" (context) line, both OldLine and NewLine must be set in the position — GitLab's API requires both sides for context lines. For added lines only NewLine is set; for removed lines only OldLine. Reference implementation: GitLab VS Code Extension, see https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/blob/main/src/common/services/mr/create_comment.ts
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 FindFileDiff ¶ added in v1.94.0
FindFileDiff finds a file's diff in the given version by matching NewPath or OldPath.
func FindNoteInDiscussions ¶ added in v1.98.0
func FindNoteInDiscussions(discussions []*gitlab.Discussion, noteID int64) (string, *gitlab.Note, error)
FindNoteInDiscussions finds the discussion and note object for a specific note ID. Returns the discussion ID and the Note, or an error if not found.
func GenerateMRCommitListBody ¶ added in v1.66.0
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 MRState ¶
func MRState(c *iostreams.ColorPalette, m *gitlab.BasicMergeRequest) string
func MRWebURLFromArgs ¶ added in v1.111.0
func MRWebURLFromArgs(ctx context.Context, f cmdutils.Factory, args []string, state string) (string, glrepo.Interface, error)
MRWebURLFromArgs resolves the web URL of a merge request using as few API calls as possible. When the merge request is identified by branch, the URL is taken from the list response instead of fetching the merge request again.
func MRsFromArgs ¶
func ParseLine ¶ added in v1.94.0
ParseLine parses a line flag value like "42" or "10:15" into start and end line numbers. For a single line, start == end.
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 PrintDiscussions ¶ added in v1.108.0
func PrintDiscussions(out io.Writer, ios *iostreams.IOStreams, discussions []*gitlab.Discussion, opts PrintDiscussionsOptions)
PrintDiscussions renders discussions to out.
func PrintMRApprovalState ¶
func PrintMRApprovalState(ios *iostreams.IOStreams, mrApprovals *gitlab.MergeRequestApprovalState)
PrintMRApprovalState renders an output to summarize the approval state of a merge request
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
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
}