-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitlab.go
124 lines (104 loc) · 2.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
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
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
var (
ErrNotFound = errors.New("Not Found")
)
type GitLab struct {
Url string
Token string
Client *http.Client
}
type Arguments map[string]interface{}
type Project struct {
Id int `json:"id"`
Name string `json:"name"`
SSHURLToRepo string `json:"ssh_url_to_repo"`
LastActivity time.Time `json:"last_activity_at"`
PathWithNamespace string `json:"path_with_namespace"`
}
type Reference struct {
Name string `json:"name"`
Commit Commit `json:"commit"`
}
type Commit struct {
Id string `json:"id"`
Message string `json:"message"`
Authored time.Time `json:"authored_date"`
Committed time.Time `json:"committed_date"`
}
type File struct {
Name string `json:"file_name"`
Path string `json:"file_path"`
Encoding string `json:"encoding"`
Content string `json:"content"`
BlobId string `json:"blob_id"`
CommitId string `json:"commit_id"`
}
func (g *GitLab) Projects(page int, perPage int) (*[]Project, error) {
p := new([]Project)
if err := g.call("projects", p, &Arguments{"page": page, "per_page": perPage}); err != nil {
return nil, err
}
return p, nil
}
func (g *GitLab) Branches(id int) (*[]Reference, error) {
r := new([]Reference)
if err := g.callProject(id, "repository/branches", r, nil); err != nil {
return nil, err
}
return r, nil
}
func (g *GitLab) Tags(id int) (*[]Reference, error) {
r := new([]Reference)
if err := g.callProject(id, "repository/tags", r, nil); err != nil {
return nil, err
}
return r, nil
}
func (g *GitLab) File(id int, reference string, name string) (*File, error) {
f := &File{}
if err := g.callProject(id, "repository/files", f, &Arguments{"file_path": name, "ref": reference}); err != nil {
return nil, err
}
return f, nil
}
func (g *GitLab) callProject(id int, resource string, result interface{}, args *Arguments) error {
return g.call(fmt.Sprintf("projects/%d/%s", id, resource), result, args)
}
func (g *GitLab) call(resource string, result interface{}, args *Arguments) error {
p := url.Values{}
p.Set("private_token", g.Token)
if args != nil {
for k, v := range *args {
p.Set(k, fmt.Sprintf("%v", v))
}
}
url := g.Url + resource + "?" + p.Encode()
r, err := g.Client.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode == 404 {
return ErrNotFound
} else if r.StatusCode != 200 {
return fmt.Errorf("HTTP %d", r.StatusCode)
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(b, result)
}
func (f *File) DecodeContent() ([]byte, error) {
return base64.StdEncoding.DecodeString(f.Content)
}