-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.go
86 lines (74 loc) · 2.08 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
package main
import (
"fmt"
)
type GitLabAssetsLink struct {
Name string `json:"name"`
DirectAssetURL string `json:"direct_asset_url"`
}
type GitLabRelease struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
UpcomingRelease bool `json:"upcoming_release"`
Assets struct {
Links []GitLabAssetsLink `json:"links"`
} `json:"assets"`
}
type GitLab struct {
url string
apiURL string
token string
repo string
projectID string
authHeaders map[string]string
}
func NewGitLab(gitlabURL, token, repo string) *GitLab {
var projectID string
// Encode project_id if it is not an integer and not encoded
if !isNumeric(repo) && !isEncoded(repo) {
projectID = urlEncode(repo)
} else {
projectID = repo
}
authHeaders := make(map[string]string)
if token != "" {
authHeaders["PRIVATE-TOKEN"] = token
}
return &GitLab{
url: gitlabURL,
apiURL: gitlabURL + "/api/v4",
token: token,
repo: repo,
projectID: projectID,
authHeaders: authHeaders,
}
}
func (g *GitLab) GetLatestRelease() (Release, error) {
// https://docs.gitlab.com/ee/api/releases/#get-the-latest-release
url := fmt.Sprintf("%s/projects/%s/releases/permalink/latest", g.apiURL, g.projectID)
return g.getRelease(url)
}
func (g *GitLab) GetTaggedRelease(tag string) (Release, error) {
// https://docs.gitlab.com/ee/api/releases/#get-a-release-by-a-tag-name
url := fmt.Sprintf("%s/projects/%s/releases/%s", g.apiURL, g.projectID, tag)
return g.getRelease(url)
}
func (g *GitLab) getRelease(url string) (Release, error) {
// https://docs.gitlab.com/ee/api/releases/#get-the-latest-release
var gr GitLabRelease
if err := GetRelease(url, g.authHeaders, &gr); err != nil {
return Release{}, err
}
return g.convertRelease(gr), nil
}
func (g *GitLab) convertRelease(gr GitLabRelease) Release {
r := Release{
Name: gr.Name,
TagName: gr.TagName,
AuthHeaders: g.authHeaders,
}
for _, link := range gr.Assets.Links {
r.Assets = append(r.Assets, *NewAsset(link.Name, link.DirectAssetURL))
}
return r
}