-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload-spec.go
86 lines (72 loc) · 1.71 KB
/
upload-spec.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
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"context"
"encoding/json"
"github.com/rs/zerolog/log"
uspec "git.krd.sh/krd/woodpecker-sonatype-nexus/nexus/upload_spec"
)
func (p *Plugin) getUploadSpecs(ctx context.Context) error {
res, err := p.NexusRequest(ctx, "v1/formats/upload-specs")
if err == nil {
defer res.Body.Close()
err = GenericResponseHandler(res)
}
for {
if err != nil {
p.UploadSpecFallback = true
log.Error().Msg("unable to retrieve upload-specs")
break
}
var rawspecs []uspec.UploadSpec
dec := json.NewDecoder(res.Body)
err = dec.Decode(&rawspecs)
if err != nil {
p.UploadSpecFallback = true
log.Error().Msg("unable to decode information for upload-specs")
break
}
if len(rawspecs) == 0 {
p.UploadSpecFallback = true
log.Error().Msg("empty upload-specs")
break
}
p.UploadSpecs = make(map[string]uspec.UploadSpec)
for _, s := range rawspecs {
p.UploadSpecs[s.Format] = s
}
//lint:ignore SA4004 this is correct
break
}
if p.UploadSpecFallback {
log.Warn().Msg("using fallback upload-specs")
p.UploadSpecs = uspec.GetFallbackSpecs()
}
keys := make([]string, 0, len(p.UploadSpecs))
for k := range p.UploadSpecs {
keys = append(keys, k)
}
// refill UploadSpecs
for _, k := range keys {
s := p.UploadSpecs[k]
s.AllFieldNames = make(map[string]bool)
var seen bool
for _, f := range s.ComponentFields {
_, seen = s.AllFieldNames[f.Name]
if seen {
continue
}
s.AllFieldNames[f.Name] = true
}
for _, f := range s.AssetFields {
_, seen = s.AllFieldNames[f.Name]
if seen {
continue
}
s.AllFieldNames[f.Name] = true
}
p.UploadSpecs[k] = s
}
return nil
}