-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
59 lines (49 loc) · 1.72 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
package gitlab
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// FetchProject will try to fetch requested project by impersonating gitlab user using the access token
func FetchProject(token string, gitlabDomain string, requestedProject string) (*ProjectInfo, error) {
if requestedProject == "" {
return nil, fmt.Errorf("error requested project can't be blank")
}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v4/projects/%s", gitlabDomain, url.QueryEscape(requestedProject)), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error when getting project `%v`, status code : %v", requestedProject, resp.StatusCode)
}
// Decode json to gitlab project struct
var project ProjectInfo
err = json.NewDecoder(resp.Body).Decode(&project)
if err != nil {
return nil, fmt.Errorf("error when decoding project `%v`, response body : %v", requestedProject, err)
}
return &project, err
}
// ProjectInfo struct contains all the required data about a Gitlab Project
type ProjectInfo struct {
Name string `json:"name"`
ID int `json:"id"`
Archived bool `json:"archived"`
PathWithNamespace string `json:"path_with_namespace"`
Permissions projectPermission `json:"permissions"`
}
type permissionAccess struct {
AccessLevel int `json:"access_level"`
}
type projectPermission struct {
ProjectAccess *permissionAccess `json:"project_access"`
GroupAccess *permissionAccess `json:"group_access"`
}