diff --git a/cmd/util.go b/cmd/util.go index 10667fcb..8f9287b8 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -605,19 +605,31 @@ func same(a, b []string) bool { // getUser returns the userID for use with other GitLab API calls. func getUserID(user string) *int { + var ( + err error + userID int + ) + if user == "" { return nil } + if user[0] == '@' { user = user[1:] } - userID, err := lab.UserIDFromUsername(user) + + if strings.Contains(user, "@") { + userID, err = lab.UserIDFromEmail(user) + } else { + userID, err = lab.UserIDFromUsername(user) + } if err != nil { return nil } if userID == -1 { return nil } + return gitlab.Int(userID) } diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 0ce3ba08..5dd1d8e6 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -1499,6 +1499,18 @@ func UserIDFromUsername(username string) (int, error) { return us[0].ID, nil } +// UserIDFromEmail returns the associated Users ID in GitLab. This is useful +// for API calls that allow you to reference a user, but only by ID. +func UserIDFromEmail(email string) (int, error) { + us, _, err := lab.Users.ListUsers(&gitlab.ListUsersOptions{ + Search: gitlab.String(email), + }) + if err != nil || len(us) == 0 { + return -1, err + } + return us[0].ID, nil +} + // AddMRDiscussionNote adds a note to an existing MR discussion on GitLab func AddMRDiscussionNote(project string, mrNum int, discussionID string, body string) (string, error) { p, err := FindProject(project)