-
Notifications
You must be signed in to change notification settings - Fork 787
/
update_center.go
217 lines (199 loc) · 6.79 KB
/
update_center.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package jenkins
import (
"encoding/json"
"fmt"
"io/ioutil"
"sort"
"strings"
"unicode/utf8"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
const (
// DefaultUpdateCenterURL the LTS update center metadata URL
DefaultUpdateCenterURL = "https://updates.jenkins.io/current/update-center.json"
)
// CoreVersion represents the version of Core
type CoreVersion struct {
BuildDate string `json:"buildDate"`
Name string `json:"name"`
Sha1 string `json:"sha1"`
Sha256 string `json:"sha256"`
URL string `json:"url"`
Version string `json:"version"`
}
// Dependency a dependency of a plugin
type Dependency struct {
Name string `json:"name"`
Optional bool `json:"optional"`
Version string `json:"version"`
}
// Developer a developer on a plugin
type Developer struct {
DeveloperID string `json:"developerId"`
Email string `json:"email"`
Name string `json:"name"`
}
// PluginRelease represets the data for a plugin
type PluginRelease struct {
BuildDate string `json:"buildDate"`
Dependencies []Dependency `json:"dependencies"`
Developers []Developer `json:"developers"`
Excerpt string `json:"excerpt"`
Gav string `json:"gav"`
Labels []string `json:"labels"`
Name string `json:"name"`
PreviousTimestamp string `json:"previousTimestamp"`
PreviousVersion string `json:"previousVersion"`
ReleaseTimestamp string `json:"releaseTimestamp"`
RequiredCore string `json:"requiredCore"`
Scm string `json:"scm"`
Sha1 string `json:"sha1"`
Sha256 string `json:"sha256"`
Title string `json:"title"`
URL string `json:"url"`
Version string `json:"version"`
Wiki string `json:"wiki"`
}
// Signature signature metadata
type Signature struct {
Certificates []string `json:"certificates"`
CorrectDigest string `json:"correct_digest"`
CorrectDigest512 string `json:"correct_digest512"`
CorrectSignature string `json:"correct_signature"`
CorrectSignature512 string `json:"correct_signature512"`
Digest string `json:"digest"`
Digest512 string `json:"digest512"`
Signature string `json:"signature"`
Signature512 string `json:"signature512"`
}
// Warning a warning message
type Warning struct {
ID string `json:"id"`
Message string `json:"message"`
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Versions []WarningVersion `json:"versions"`
}
// WarningVersion warning versions
type WarningVersion struct {
FirstVersion string `json:"firstVersion"`
LastVersion string `json:"lastVersion"`
Pattern string `json:"pattern"`
}
// UpdateCenter represents the Update Center metadata returned from URLs
// like https://updates.jenkins.io/current/update-center.json
type UpdateCenter struct {
ConnectionCheckURL string `json:"connectionCheckUrl"`
Core CoreVersion `json:"core"`
ID string `json:"id"`
Plugins map[string]PluginRelease `json:"plugins"`
Signature Signature `json:"signature"`
UpdateCenterVersion string `json:"updateCenterVersion"`
Warnings []Warning `json:"warnings"`
}
// LoadUpdateCenterFile loads the given UpdateCenter JSON file
func LoadUpdateCenterFile(fileName string) (*UpdateCenter, error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, errors.Wrapf(err, "failed to load UpdateCenter file %s", fileName)
}
answer, err := LoadUpdateCenterData(data)
if err != nil {
return answer, errors.Wrapf(err, "failed to unmarshal JSON from UpdateCenter file %s", fileName)
}
return answer, nil
}
// LoadUpdateCenterURL loads the given UpdateCenter URL
func LoadUpdateCenterURL(u string) (*UpdateCenter, error) {
httpClient := util.GetClient()
resp, err := httpClient.Get(u)
if err != nil {
return nil, errors.Wrapf(err, "failed to invoke GET on %s", u)
}
stream := resp.Body
defer stream.Close()
data, err := ioutil.ReadAll(stream)
if err != nil {
return nil, errors.Wrapf(err, "failed to GET data from %s", u)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("status %s when performing GET on %s", resp.Status, u)
}
answer, err := LoadUpdateCenterData(data)
if err != nil {
return answer, errors.Wrapf(err, "failed to unmarshal JSON from UpdateCenter URL %s", u)
}
return answer, nil
}
// LoadUpdateCenterData loads the given UpdateCenter data
func LoadUpdateCenterData(data []byte) (*UpdateCenter, error) {
answer := &UpdateCenter{}
// lets check for JSONP and strip it
text := strings.TrimSpace(string(data))
if !strings.HasPrefix(text, "{") && !strings.HasPrefix(text, "[") {
// we have JSONP of the form 'foo.bar(....)' so lets grab the contents
i := strings.Index(text, "(")
if i < 0 {
lines := strings.SplitN(text, "\n", 2)
return answer, fmt.Errorf("invalid JSON and JSONP document. Was expecting '(' but got: %s", lines[0])
}
text = text[i+1:]
text = strings.TrimSuffix(text, ";")
text = strings.TrimSpace(text)
text = strings.TrimSuffix(text, ")")
text = strings.TrimSpace(text)
data = []byte(text)
}
err := json.Unmarshal(data, answer)
return answer, err
}
// PickPlugins provides the user with a list of plugins that can be added to a Jenkins App
func (u *UpdateCenter) PickPlugins(currentValues []string, handles util.IOFileHandles) ([]string, error) {
names := []string{}
pluginMap := map[string]string{}
maxLen := 1
for _, plugin := range u.Plugins {
l := utf8.RuneCountInString(plugin.Name)
if l > maxLen {
maxLen = l
}
}
defaults := []string{}
for _, plugin := range u.Plugins {
name := util.PadRight(plugin.Name, " ", maxLen) + " " + plugin.Title
namePrefix := plugin.Name + ":"
current := false
for _, cv := range currentValues {
if strings.HasPrefix(cv, namePrefix) {
current = true
break
}
}
if current {
defaults = append(defaults, name)
}
value := namePrefix + plugin.Version
names = append(names, name)
pluginMap[name] = value
}
sort.Strings(names)
help := "select the Jenkins plugins you wish to include inside your Jenkins App"
message := "pick Jenkins plugins to include in your Jenkins App: "
selection, err := util.PickNamesWithDefaults(names, defaults, message, help, handles)
if err != nil {
return nil, err
}
answer := []string{}
for _, sel := range selection {
value := pluginMap[sel]
if value == "" {
log.Logger().Warnf("Could not find value for %s in map!", value)
} else {
answer = append(answer, value)
}
}
return answer, nil
}