Skip to content

Commit

Permalink
gitlab: remove duplicate function for getting used ID
Browse files Browse the repository at this point in the history
There were two functions to get the userID from API, UserIDFromUsername and
UserIDByUserName, both were doing basically the same operation. For
simplicity, I removed the second one so now we have UserIDFromUsername and
UserIDFromEmail.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Mar 26, 2021
1 parent b7be9f9 commit 258693d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
14 changes: 6 additions & 8 deletions cmd/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,17 @@ func issueList(args []string) ([]*gitlab.Issue, error) {
// because of that we need to get user's ID for both assignee and
// author.
if issueAuthor != "" {
authorID, err := lab.UserIDByUserName(issueAuthor)
if err != nil {
log.Fatal(err)
issueAuthorID := getUserID(issueAuthor)
if issueAuthorID == nil {
log.Fatal(fmt.Errorf("%s user not found\n", issueAuthor))
}
issueAuthorID = &authorID
}

if issueAssignee != "" {
assigneeID, err := lab.UserIDByUserName(issueAssignee)
if err != nil {
log.Fatal(err)
issueAssigneeID := getUserID(issueAssignee)
if issueAssigneeID == nil {
log.Fatal(fmt.Errorf("%s user not found\n", issueAssignee))
}
issueAssigneeID = &assigneeID
}

opts := gitlab.ListProjectIssuesOptions{
Expand Down
14 changes: 6 additions & 8 deletions cmd/mr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ func mrList(args []string) ([]*gitlab.MergeRequest, error) {
// gitlab lib still doesn't have search by assignee and author username
// for merge requests, because of that we need to get the ID for both.
if mrAssignee != "" {
assigneeID, err := lab.UserIDByUserName(mrAssignee)
if err != nil {
log.Fatal(err)
mrAssigneeID := getUserID(mrAssignee)
if mrAssigneeID == nil {
log.Fatal(fmt.Errorf("%s user not found\n", mrAssignee))
}
mrAssigneeID = &assigneeID
} else if mrMine {
assigneeID, err := lab.UserID()
if err != nil {
Expand All @@ -94,11 +93,10 @@ func mrList(args []string) ([]*gitlab.MergeRequest, error) {
}

if mrAuthor != "" {
authorID, err := lab.UserIDByUserName(mrAuthor)
if err != nil {
log.Fatal(err)
mrAuthorID := getUserID(mrAuthor)
if mrAuthorID == nil {
log.Fatal(fmt.Errorf("%s user not found\n", mrAuthor))
}
mrAuthorID = &authorID
}

if mrMilestone != "" {
Expand Down
18 changes: 0 additions & 18 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,6 @@ func UserID() (int, error) {
return u.ID, nil
}

func UserIDByUserName(username string) (int, error) {
opts := gitlab.ListUsersOptions{
ListOptions: gitlab.ListOptions{
PerPage: 1,
},
Username: &username,
}
users, _, err := lab.Users.ListUsers(&opts)
if err != nil {
return 0, err
}
for _, user := range users {
return user.ID, nil
}

return 0, errors.New("No user found with username " + username)
}

// Init initializes a gitlab client for use throughout lab.
func Init(_host, _user, _token string, allowInsecure bool) {
if len(_host) > 0 && _host[len(_host)-1:][0] == '/' {
Expand Down

0 comments on commit 258693d

Please sign in to comment.