/
calls_users.go
44 lines (38 loc) · 1.09 KB
/
calls_users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gitlab
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
)
// GetUsers - Return users from gitlab
func (r *gitlabClient) GetUsers(search string) (string, error) {
nextPage := "1"
combinedResults := ""
uri := "/users?active=true"
for {
// TODO: detect if there are no options passed in, ? verus & for page option
fetchUri := fmt.Sprintf("https://%s%s%s&search=%s&page=%s", r.BaseUrl, r.ApiPath, uri, search, nextPage)
// logrus.Warn(fetchUri)
resp, resperr := r.Client.R().
SetHeader("PRIVATE-TOKEN", r.Token).
Get(fetchUri)
if resperr != nil {
logrus.WithError(resperr).Error("Oops")
return "", resperr
}
items := strings.TrimPrefix(string(resp.Body()[:]), "[")
items = strings.TrimSuffix(items, "]")
if combinedResults == "" {
combinedResults += items
} else {
combinedResults += fmt.Sprintf(", %s", items)
}
currentPage := resp.Header().Get("X-Page")
nextPage = resp.Header().Get("X-Next-Page")
totalPages := resp.Header().Get("X-Total-Pages")
if currentPage == totalPages {
break
}
}
return fmt.Sprintf("[%s]", combinedResults), nil
}