-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
128 lines (102 loc) · 2.68 KB
/
client.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
var httpClient http.Client = http.Client{}
func lookupUser(username string) GitLabUser {
apiUrl := url.URL{
Scheme: "https",
Host: host,
Path: "/api/v4/users",
}
qs := apiUrl.Query()
qs.Set("search", username)
qs.Set("active", "true")
qs.Set("locked", "false")
qs.Set("exclude_external", "true")
qs.Set("without_project_bots", "true")
apiUrl.RawQuery = qs.Encode()
req, _ := http.NewRequest(http.MethodGet, apiUrl.String(), nil)
req.Header.Add("PRIVATE-TOKEN", token)
resp, _ := httpClient.Do(req)
if resp.StatusCode != 200 {
fmt.Printf("WARN: Unable to lookup for user '%s'.\n", username)
return GitLabUser{}
}
body, _ := io.ReadAll(resp.Body)
var users []GitLabUser
json.Unmarshal(body, &users)
for _, user := range users {
if user.Username == strings.ToLower(username) {
return user
}
}
return GitLabUser{}
}
func getGroupId(groupName string) GitLabGroup {
apiUrl := url.URL{
Scheme: "https",
Host: host,
Path: "/api/v4/groups",
}
qs := apiUrl.Query()
qs.Set("search", groupName)
apiUrl.RawQuery = qs.Encode()
req, _ := http.NewRequest(http.MethodGet, apiUrl.String(), nil)
req.Header.Add("PRIVATE-TOKEN", token)
resp, _ := httpClient.Do(req)
if resp.StatusCode != 200 {
fmt.Printf("WARN: Unable to lookup for group '%s'.\n", groupName)
return GitLabGroup{}
}
body, _ := io.ReadAll(resp.Body)
var groups []GitLabGroup
json.Unmarshal(body, &groups)
// Compare with the fullpath
for _, group := range groups {
if group.FullPath == strings.ToLower(groupName) {
return group
}
}
// Fallback to the path if no group has been found
for _, group := range groups {
if group.Path == strings.ToLower(groupName) {
return group
}
}
return GitLabGroup{}
}
func lookupGroupUsers(groupName string) []GitLabUser {
groupName = groupName[1:] // Remove @ at the beginning of the group name
group := getGroupId(groupName)
if group == (GitLabGroup{}) {
return nil
}
membersUrl := url.URL{
Scheme: "https",
Host: host,
Path: fmt.Sprintf("/api/v4/groups/%d/members", group.Id),
}
req, _ := http.NewRequest(http.MethodGet, membersUrl.String(), nil)
req.Header.Add("PRIVATE-TOKEN", token)
resp, _ := httpClient.Do(req)
if resp.StatusCode != 200 {
fmt.Printf("WARN: Unable to lookup for group '%s'.\n", groupName)
return nil
}
body, _ := io.ReadAll(resp.Body)
var users []GitLabUser
json.Unmarshal(body, &users)
var filteredUsers []GitLabUser
for _, user := range users {
if !strings.Contains(user.Username, "_bot") && user.MembershipState == "active" {
filteredUsers = append(filteredUsers, user)
}
}
return filteredUsers
}