Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

feat(commands/api): add a reviewer filter to the mr list command #681

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion api/merge_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var ListMRs = func(client *gitlab.Client, projectID interface{}, opts *gitlab.Li
return mrs, nil
}

var ListMRsWithAssignees = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions, assigneeIds []int) ([]*gitlab.MergeRequest, error) {
var ListMRsWithAssigneesOrReviewers = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions, assigneeIds []int, reviewerIds []int) ([]*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
Expand All @@ -73,6 +73,15 @@ var ListMRsWithAssignees = func(client *gitlab.Client, projectID interface{}, op
}
mrs = append(mrs, assingeMrs...)
}
opts.AssigneeID = nil // reset because it's Assignee OR Reviewer
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
}

Expand Down
18 changes: 16 additions & 2 deletions commands/mr/list/mr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type ListOptions struct {
// metadata
Assignee []string
Reviewer []string
Author string
Labels []string
NotLabels []string
Expand Down Expand Up @@ -64,6 +65,7 @@ func NewCmdList(f *cmdutils.Factory, runE func(opts *ListOptions) error) *cobra.
$ glab mr list --all
$ glab mr ls -a
$ glab mr list --assignee=@me
$ glab mr list --reviewer=@me
$ glab mr list --source-branch=new-feature
$ glab mr list --target-branch=trunk
$ glab mr list --search "this adds feature X"
Expand Down Expand Up @@ -124,6 +126,7 @@ func NewCmdList(f *cmdutils.Factory, runE func(opts *ListOptions) error) *cobra.
mrListCmd.Flags().IntVarP(&opts.Page, "page", "p", 1, "Page number")
mrListCmd.Flags().IntVarP(&opts.PerPage, "per-page", "P", 30, "Number of items to list per page")
mrListCmd.Flags().StringSliceVarP(&opts.Assignee, "assignee", "a", []string{}, "Get only merge requests assigned to users")
mrListCmd.Flags().StringSliceVarP(&opts.Reviewer, "reviewer", "r", []string{}, "Get only merge requests with users as reviewer")

mrListCmd.Flags().BoolP("opened", "o", false, "Get only open merge requests")
_ = mrListCmd.Flags().MarkHidden("opened")
Expand Down Expand Up @@ -210,8 +213,19 @@ func listRun(opts *ListOptions) error {
}
}

if len(assigneeIds) > 0 {
mergeRequests, err = api.ListMRsWithAssignees(apiClient, repo.FullName(), l, assigneeIds)
reviewerIds := make([]int, 0)
if len(opts.Reviewer) > 0 {
users, err := api.UsersByNames(apiClient, opts.Reviewer)
if err != nil {
return err
}
for _, user := range users {
reviewerIds = append(reviewerIds, user.ID)
}
}

if len(assigneeIds) > 0 || len(reviewerIds) > 0 {
mergeRequests, err = api.ListMRsWithAssigneesOrReviewers(apiClient, repo.FullName(), l, assigneeIds, reviewerIds)

} else {
mergeRequests, err = api.ListMRs(apiClient, repo.FullName(), l)
Expand Down