Skip to content

Commit

Permalink
mr create: Allow email addresses
Browse files Browse the repository at this point in the history
On large new projects users often know each other by their email
addresses and not their GitLab usernames.  GitLab provides functionality
to lookup a UserID from an email address so it is easy to incorporate
this functionality into lab.

Allow email addresses to be used in the --reviewer and --assignee fields
of 'mr create'.

This also allows email addresses to be used in the --assign and --review
fields of 'mr edit'.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Mar 24, 2021
1 parent d118285 commit f3a38a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f3a38a2

Please sign in to comment.