forked from jwallet/gitlab-slack-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
94 lines (75 loc) · 2.45 KB
/
gitlab.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type GitLabWebhookEvent struct {
EventType string `json:"event_type"`
ObjectType string `json:"object_type"`
User GitLabUser `json:"user"`
Project GitLabName `json:"project"`
Note GitLabNote `json:"object_attributes"`
Repository GitLabName `json:"repository"`
MergeRequest GitLabMergeRequest `json:"merge_request"`
}
type GitLabUser struct {
Id int64 `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
State bool `json:"state,omitempty"`
Email string `json:"email,omitempty"`
}
type GitLabName struct {
Name string `json:"name"`
}
type GitLabNote struct {
Id int64 `json:"id"`
Note string `json:"note"`
Description string `json:"description"`
Url string `json:"url"`
}
type GitLabMergeRequest struct {
Description string `json:"description"`
Title string `json:"title"`
}
const defaultQueryParams = "active=true&blocked=false&without_project_bots=true"
func fetchGitLabUser(username string) (*GitLabUser, error) {
return nil, fmt.Errorf(`Not implemented!
To retrive a user private email you need to be an admin, so a GitLab staff member for the cloud service.
You can retrieve the user public email though by using GET:User/:id.
If you have GitLab self-hosted you can fetch any user private email.
Set your personal or project access token when using GET:Users
https://docs.gitlab.com/ee/api/users.html#for-administrators`)
}
func fetchBasicGitLabUser(username string) (*GitLabUser, error) {
client := getClient()
endpoint := fmt.Sprintf("https://gitlab.com/api/v4/users?%s&username=%s", defaultQueryParams, username)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
fmt.Printf("Bot is fetching GitLab user profile: %s\n", endpoint)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("GET user Failed %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
client.CloseIdleConnections()
var users []GitLabUser
json.Unmarshal(body, &users)
if len(users) < 1 {
return nil, fmt.Errorf("No user found on GitLab with that username tag.")
}
user := &users[0]
fmt.Printf("GitLab user fullname: %v\n", user.Name)
return user, nil
}