-
Notifications
You must be signed in to change notification settings - Fork 31
/
module.go
146 lines (132 loc) · 3.34 KB
/
module.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// pkg is responsible for listing and downloading
// themes and plugins from the internet.
// Themes and plugins are stored in the same registry, but
// get installed to different places? Maybe linked
// to themes dir if active?
package pkg
import (
"encoding/json"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/octavore/naga/service"
"github.com/octavore/nagax/util/errors"
"github.com/ketchuphq/ketchup/proto/ketchup/packages"
"github.com/ketchuphq/ketchup/server/config"
)
// Module pkg manages downloading pkg
type Module struct {
Config *config.Module
httpGet func(url string) (*http.Response, error)
RegistryURL string
}
type endpoint struct {
pattern string
baseAPIURL string
parseResponse func([]byte) ([]RemoteTag, error)
}
type RemoteTag struct {
Name string
SHA string
URL string
}
type githubTag struct {
Name string `json:"name"`
Commit struct {
SHA string `json:"sha"`
URL string `json:"url"`
} `json:"commit"`
}
type bitbucketResponse struct {
Values []struct {
Name string `json:"name"`
Links struct {
HTML struct {
Href string `json:"href"`
} `json:"html"`
} `json:"links"`
Target struct {
Hash string `json:"hash"`
} `json:"target"`
} `json:"values"`
}
var github = endpoint{
pattern: `https://github.com/(?P<user>[^/]+)/(?P<repo>[^/]+)`,
baseAPIURL: "https://api.github.com/repos/{user}/{repo}/tags",
parseResponse: func(data []byte) ([]RemoteTag, error) {
response := []githubTag{}
err := json.Unmarshal(data, &response)
if err != nil {
return nil, errors.Wrap(err)
}
out := []RemoteTag{}
for _, el := range response {
out = append(out, RemoteTag{
Name: el.Name,
SHA: el.Commit.SHA,
URL: el.Commit.URL,
})
}
return out, nil
},
}
var bitbucket = endpoint{
pattern: `https://bitbucket.org/(?P<user>[^/]+)/(?P<repo>[^/]+)`,
baseAPIURL: "https://api.bitbucket.org/2.0/repositories/{user}/{repo}/refs/tags?sort=-name",
parseResponse: func(data []byte) ([]RemoteTag, error) {
response := &bitbucketResponse{}
err := json.Unmarshal(data, response)
if err != nil {
return nil, errors.Wrap(err)
}
out := []RemoteTag{}
for _, e := range response.Values {
out = append(out, RemoteTag{
Name: e.Name,
SHA: e.Target.Hash,
URL: e.Links.HTML.Href,
})
}
return out, nil
},
}
func (m *Module) Init(c *service.Config) {
// todo: keep track of all registries
c.Setup = func() error {
m.httpGet = http.Get
return nil
}
}
func (e endpoint) fetch(get func(url string) (*http.Response, error), p *packages.Package) ([]RemoteTag, error) {
re := regexp.MustCompile(e.pattern)
parts := re.FindStringSubmatch(p.GetVcsUrl())
if len(parts) == 0 {
return nil, nil
}
url := strings.Replace(e.baseAPIURL, "{user}", parts[1], 1)
url = strings.Replace(url, "{repo}", parts[2], 1)
resp, err := get(url)
if err != nil {
return nil, errors.Wrap(err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err)
}
return e.parseResponse(data)
}
// FetchTags fetches tag information about the given package
// from upstream vcs repo
func (m *Module) FetchTags(p *packages.Package) ([]RemoteTag, error) {
for _, e := range []endpoint{github, bitbucket} {
tags, err := e.fetch(m.httpGet, p)
if err != nil {
return nil, errors.Wrap(err)
}
if tags != nil {
return tags, nil
}
}
return nil, nil
}