-
Notifications
You must be signed in to change notification settings - Fork 95
/
bump.go
157 lines (130 loc) · 3.93 KB
/
bump.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
// Copyright 2022 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bump
import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"log"
"os"
"strings"
"github.com/dprotaso/go-yit"
"gopkg.in/yaml.v3"
"chainguard.dev/melange/pkg/renovate"
"chainguard.dev/melange/pkg/util"
)
// BumpConfig contains the configuration data for a bump
// renovator.
type BumpConfig struct {
TargetVersion string
}
// Option sets a config option on a BumpConfig.
type Option func(cfg *BumpConfig) error
// WithTargetVersion sets the desired target version for the
// bump renovator.
func WithTargetVersion(targetVersion string) Option {
return func(cfg *BumpConfig) error {
cfg.TargetVersion = targetVersion
return nil
}
}
// New returns a renovator which performs a version bump.
func New(opts ...Option) renovate.Renovator {
bcfg := BumpConfig{}
for _, opt := range opts {
if err := opt(&bcfg); err != nil {
return func(rc *renovate.RenovationContext) error {
return fmt.Errorf("while constructing: %w", err)
}
}
}
return func(rc *renovate.RenovationContext) error {
log.Printf("attempting to bump version to %s", bcfg.TargetVersion)
// Find the package.version node first and change it.
packageNode, err := renovate.NodeFromMapping(rc.Root.Content[0], "package")
if err != nil {
return err
}
versionNode, err := renovate.NodeFromMapping(packageNode, "version")
if err != nil {
return err
}
versionNode.Value = bcfg.TargetVersion
versionNode.Style = yaml.FlowStyle
versionNode.Tag = "!!str"
epochNode, err := renovate.NodeFromMapping(packageNode, "epoch")
if err != nil {
return err
}
epochNode.Value = "0"
// Find our main pipeline YAML node.
pipelineNode, err := renovate.NodeFromMapping(rc.Root.Content[0], "pipeline")
if err != nil {
return err
}
// Look for fetch nodes.
it := yit.FromNode(pipelineNode).
RecurseNodes().
Filter(yit.WithMapValue("fetch"))
for fetchNode, ok := it(); ok; fetchNode, ok = it() {
if err := updateFetch(fetchNode, bcfg.TargetVersion); err != nil {
return err
}
}
return nil
}
}
// updateFetch takes a "fetch" pipeline node and updates the parameters of it.
func updateFetch(node *yaml.Node, targetVersion string) error {
withNode, err := renovate.NodeFromMapping(node, "with")
if err != nil {
return err
}
uriNode, err := renovate.NodeFromMapping(withNode, "uri")
if err != nil {
return err
}
log.Printf("processing fetch node:")
// Fetch the new sources.
evaluatedUri := strings.ReplaceAll(uriNode.Value, "${{package.version}}", targetVersion)
log.Printf(" uri: %s", uriNode.Value)
log.Printf(" evaluated: %s", evaluatedUri)
downloadedFile, err := util.DownloadFile(evaluatedUri)
if err != nil {
return err
}
defer os.Remove(downloadedFile)
log.Printf(" fetched-as: %s", downloadedFile)
// Calculate SHA2-256 and SHA2-512 hashes.
fileSHA256, err := util.HashFile(downloadedFile, sha256.New())
if err != nil {
return err
}
log.Printf(" expected-sha256: %s", fileSHA256)
fileSHA512, err := util.HashFile(downloadedFile, sha512.New())
if err != nil {
return err
}
log.Printf(" expected-sha512: %s", fileSHA512)
// Update expected hash nodes.
nodeSHA256, err := renovate.NodeFromMapping(withNode, "expected-sha256")
if err == nil {
nodeSHA256.Value = fileSHA256
}
nodeSHA512, err := renovate.NodeFromMapping(withNode, "expected-sha512")
if err == nil {
nodeSHA512.Value = fileSHA512
}
return nil
}