-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
69 lines (60 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
60
61
62
63
64
65
66
67
68
69
// Package `gitlab` wraps a subset of `github.com/xanzy/go-gitlab`; only what
// other Nog packages use.
package gitlab
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
gitlab "github.com/xanzy/go-gitlab"
)
type Branch = gitlab.Branch
type Client = gitlab.Client
type Commit = gitlab.Commit
type CreateBranchOptions = gitlab.CreateBranchOptions
type CreateFileOptions = gitlab.CreateFileOptions
type CreateProjectOptions = gitlab.CreateProjectOptions
type GetFileOptions = gitlab.GetFileOptions
type ListOptions = gitlab.ListOptions
type ListProjectsOptions = gitlab.ListProjectsOptions
type ListTreeOptions = gitlab.ListTreeOptions
type Project = gitlab.Project
type TreeNode = gitlab.TreeNode
type UpdateFileOptions = gitlab.UpdateFileOptions
func String(s string) *string { return gitlab.String(s) }
func Int(i int) *int { return gitlab.Int(i) }
func Bool(b bool) *bool { return gitlab.Bool(b) }
func NewClient(addr, tokenPath string) (*Client, error) {
buf, err := ioutil.ReadFile(tokenPath)
if err != nil {
return nil, err
}
token := string(buf)
token = strings.TrimSpace(token)
c := gitlab.NewClient(nil, token)
addr = strings.TrimRight(addr, "/")
addr = fmt.Sprintf("%s/api/v4/", addr)
c.SetBaseURL(addr)
_, _, err = c.Version.GetVersion()
if err != nil {
err := fmt.Errorf("failed ping GitLab: %v", err)
return nil, err
}
return c, nil
}
func WithListOptions(opts ListOptions) gitlab.OptionFunc {
return func(req *http.Request) error {
addq := func(q string, val int) {
if val == 0 {
return
}
if req.URL.RawQuery != "" {
req.URL.RawQuery += "&"
}
req.URL.RawQuery += fmt.Sprintf("%s=%d", q, val)
}
addq("page", opts.Page)
addq("per_page", opts.PerPage)
return nil
}
}