-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
67 lines (56 loc) · 1.48 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
package gitlab
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"sort"
"time"
)
type GitlabTags struct {
Name string `json:"name"`
}
func GetTags(gitlabEndpoint, group, namespace, collection string) (out []GitlabTags, status int, err error) {
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
endpoint := fmt.Sprintf("%s/api/v4/projects/%s/repository/tags", gitlabEndpoint, url.QueryEscape(fmt.Sprintf("%s/%s.%s", group, namespace, collection)))
req, _ := http.NewRequest("GET", endpoint, nil)
response, err := client.Do(req)
if err != nil || response.StatusCode != http.StatusOK {
if response.StatusCode >= 500 {
return nil, 500, fmt.Errorf("Failed to load")
}
return nil, response.StatusCode, fmt.Errorf("Failed to load")
}
decoder := json.NewDecoder(response.Body)
tags := []GitlabTags{}
err = decoder.Decode(&tags)
if err != nil || response.StatusCode != http.StatusOK {
return nil, 500, fmt.Errorf("Failed to load")
}
if len(tags) == 0 {
return nil, 404, fmt.Errorf("Not found")
}
sort.Slice(tags, func(i, j int) bool {
return tags[i].Name > tags[j].Name
})
out = []GitlabTags{}
for _, v := range tags {
r, _ := regexp.Compile("^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)$")
if r.MatchString(v.Name) {
out = append(out, v)
}
}
if len(out) == 0 {
return nil, 404, fmt.Errorf("Not found")
}
return
}