/
autodiscovery.go
247 lines (208 loc) · 7.42 KB
/
autodiscovery.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package engine
import (
"crypto/sha256"
"fmt"
"io"
"os"
"sync"
"github.com/sirupsen/logrus"
"github.com/updatecli/updatecli/pkg/core/config"
"github.com/updatecli/updatecli/pkg/core/pipeline"
"github.com/updatecli/updatecli/pkg/core/pipeline/action"
"github.com/updatecli/updatecli/pkg/core/pipeline/autodiscovery"
"github.com/updatecli/updatecli/pkg/core/pipeline/scm"
"github.com/updatecli/updatecli/pkg/core/result"
"github.com/updatecli/updatecli/pkg/core/version"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
)
// LoadAutoDiscovery tries to guess available pipelines based on specific directory
//
//nolint:funlen
func (e *Engine) LoadAutoDiscovery(defaultEnabled bool) error {
// Default Autodiscovery pipeline
if defaultEnabled {
logrus.Debugf("Default Autodiscovery crawlers enabled")
var defaultPipeline pipeline.Pipeline
err := defaultPipeline.Init(
&config.Config{
Spec: config.Spec{
Name: "Local AutoDiscovery",
AutoDiscovery: autodiscovery.DefaultCrawlerSpecs,
},
},
pipeline.Options{},
)
if err != nil {
logrus.Errorln(err)
} else {
e.Pipelines = append(e.Pipelines, defaultPipeline)
}
}
PrintTitle("Auto Discovery")
for id, p := range e.Pipelines {
if p.Config.Spec.AutoDiscovery.Crawlers == nil {
continue
}
PrintTitle(p.Name)
var actionConfig *action.Config
var autodiscoveryScm scm.Scm
var autodiscoveryAction action.Action
var found bool
workDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed getting current working directory due to %v", err)
}
// Retrieve scm spec if it exists
if len(p.Config.Spec.AutoDiscovery.ScmId) > 0 {
autodiscoveryScm, found = p.SCMs[p.Config.Spec.AutoDiscovery.ScmId]
if found {
workDir = autodiscoveryScm.Handler.GetDirectory()
}
}
/** Check for deprecated items **/
if p.Config.Spec.AutoDiscovery.PullrequestId != "" {
if p.Config.Spec.AutoDiscovery.ActionId != "" {
return fmt.Errorf("the `autodiscovery.pullrequestid` and `autodiscovery.actionid` keywords are mutually exclusive. Please use only `autodiscovery.actionid` as `autodiscovery.pullrequestid` is deprecated")
}
logrus.Warningf("The `autodiscovery.pullrequestid` keyword is deprecated in favor of `autodiscovery.actionid`, please update this manifest. Updatecli will continue the execution while trying to translate `autodiscovery.pullrequestid` to `autodiscovery.actionid`.")
p.Config.Spec.AutoDiscovery.ActionId = p.Config.Spec.AutoDiscovery.PullrequestId
p.Config.Spec.AutoDiscovery.PullrequestId = ""
}
// Retrieve action spec if it exists
if len(p.Config.Spec.AutoDiscovery.ActionId) > 0 {
autodiscoveryAction, found = p.Actions[p.Config.Spec.AutoDiscovery.ActionId]
if found {
actionConfig = &autodiscoveryAction.Config
}
}
c, err := autodiscovery.New(
p.Config.Spec.AutoDiscovery, workDir)
if err != nil {
e.Pipelines[id].Report.Result = result.FAILURE
logrus.Errorln(err)
return err
}
errs := []error{}
bytesManifests, err := c.Run()
if err != nil {
e.Pipelines[id].Report.Result = result.FAILURE
logrus.Errorln(err)
return err
}
if len(bytesManifests) == 0 {
logrus.Infof("nothing detected")
}
for i := range bytesManifests {
manifest := config.Spec{}
// We expected manifest generated by the autodiscovery to use the yaml syntax
err = yaml.Unmarshal(bytesManifests[i], &manifest)
if err != nil {
return err
}
switch p.Config.Spec.AutoDiscovery.GroupBy {
/*
By default if "group by" is not se then we fallback to all
which means that we generate a single pipeline for all discovered manifests
The goal is to have a "safe" default behavior and to avoid to accidentally generate
dozens pullrequests for a single updatecli run
*/
case autodiscovery.GROUPBYALL, "":
manifest.PipelineID = p.Config.Spec.PipelineID
case autodiscovery.GROUPBYINDIVIDUAL:
hash := sha256.New()
/*
We need to generate an uniq ID per individual pipeline
but we shouldn't use the manifest of a pipeline
because it may change over pipeline execution
such as different source version filter
Starting the id with the autodiscovery pipelineid looks enough
to avoid collision
*/
_, err := io.WriteString(hash, p.Config.Spec.PipelineID+"/"+manifest.Name)
if err != nil {
logrus.Errorln(err)
}
manifest.PipelineID = fmt.Sprintf("%x", hash.Sum(nil))
}
// Only add the scm if it is not already defined
if len(manifest.SCMs) == 0 {
manifest.SCMs = make(map[string]scm.Config)
}
autodiscoveryGeneratedSCMs := maps.Keys(manifest.SCMs)
for scmId, sc := range p.SCMs {
manifest.SCMs[scmId] = *sc.Config
}
if actionConfig != nil {
// Only initialize the action if it is not already defined
if len(manifest.Actions) == 0 {
manifest.Actions = make(map[string]action.Config)
}
if (p.Config.Spec.AutoDiscovery.GroupBy == autodiscovery.GROUPBYALL ||
p.Config.Spec.AutoDiscovery.GroupBy == "") &&
actionConfig.Title == "" {
/*
Normally the config spec name should never be empty as it is a required field according the jsonschema spec
*/
actionConfig.Title = p.Config.Spec.Name
if actionConfig.Title == "" {
/*
If Autodiscovery option "groupby" is set to "all" and if associated action title are set to "empty"
then we want to be sure that the action title is not empty by using a generic title.
Otherwise each pipeline generated by the autodiscovery will have a different title which will constantly update the pullrequest title.
*/
defaultActionTitle := "deps: bumping various version"
logrus.Warningf("action title %q used by autodiscovery is empty, fallback to generic:\n\t=> %s",
p.Config.Spec.AutoDiscovery.ActionId,
defaultActionTitle)
actionConfig.Title = defaultActionTitle
}
}
manifest.Actions[p.Config.Spec.AutoDiscovery.ScmId] = *actionConfig
}
if manifest.Version != "" {
manifest.Version = version.Version
}
newConfig := config.Config{
Spec: manifest,
}
newPipeline := pipeline.Pipeline{}
err = newPipeline.Init(&newConfig, e.Options.Pipeline)
// We must clone any scm generated by autodiscovery
if len(autodiscoveryGeneratedSCMs) >= 0 {
hashes := []uint64{}
wg := sync.WaitGroup{}
channel := make(chan int, 20)
defer wg.Wait()
for _, id := range autodiscoveryGeneratedSCMs {
s := newPipeline.SCMs[id]
if s.Handler != nil {
logrus.Debugf("scm %s generated by autodiscovery must be cloned in %s", id, s.Handler.GetDirectory())
err = Clone(&s.Handler, channel, &hashes, &wg)
if err != nil {
logrus.Debugf("Error while cloning autodiscovery %s - %s", s.Handler.GetDirectory(), err)
}
}
}
}
if err == nil {
e.Pipelines = append(e.Pipelines, newPipeline)
e.configurations = append(e.configurations, newConfig)
} else {
e.Pipelines[id].Report.Result = result.FAILURE
// don't initially fail as init. of the pipeline still fails even with a successful validation
err := fmt.Errorf("%q - %s", manifest.Name, err)
errs = append(errs, err)
}
if len(errs) > 0 {
e.Pipelines[id].Report.Result = result.FAILURE
logrus.Errorf("Error(s) happened while generating Updatecli pipeline manifest")
for i := range errs {
logrus.Errorf("%v", errs[i])
}
}
}
e.Pipelines[id].Report.Result = result.SUCCESS
}
return nil
}