-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrelease.go
More file actions
183 lines (151 loc) · 6.45 KB
/
Copy pathrelease.go
File metadata and controls
183 lines (151 loc) · 6.45 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package sources
import (
"fmt"
"log/slog"
"regexp"
"strconv"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components"
"github.com/microsoft/azure-linux-dev-tools/internal/global/opctx"
"github.com/microsoft/azure-linux-dev-tools/internal/projectconfig"
"github.com/microsoft/azure-linux-dev-tools/internal/rpm/spec"
)
// autoreleasePattern matches the %autorelease macro invocation in a Release tag value.
// This covers:
// - bare form: %autorelease
// - braced form: %{autorelease}
// - braced form with arguments: %{autorelease -e asan}
// - conditional form (no fallback): %{?autorelease}
var autoreleasePattern = regexp.MustCompile(`%(\{[?]?autorelease($|[}\s])|autorelease($|\s))`)
// staticReleasePattern matches only the two Release tag forms we can safely
// auto-bump: a bare integer (e.g. "1") or an integer followed by a
// dist macro (e.g. "5%{?dist}" or "5%{dist}"). Any other suffix — dotted
// segments, unknown macros, etc. — is rejected so the component must use
// 'release.calculation = "manual"'.
var staticReleasePattern = regexp.MustCompile(`^(\d+)(%\{\??dist\})?$`)
// GetReleaseTagValue reads the Release tag value from the spec file at specPath.
// It returns the raw value string as written in the spec (e.g. "1%{?dist}" or "%autorelease").
// Returns [spec.ErrNoSuchTag] if no Release tag is found.
func GetReleaseTagValue(fs opctx.FS, specPath string, options ...spec.OpenOption) (string, error) {
specFile, err := fs.Open(specPath)
if err != nil {
return "", fmt.Errorf("failed to open spec %#q:\n%w", specPath, err)
}
defer specFile.Close()
openedSpec, err := spec.OpenSpec(specFile, options...)
if err != nil {
return "", fmt.Errorf("failed to parse spec %#q:\n%w", specPath, err)
}
releaseValue, err := openedSpec.GetLastTag("", "Release")
if err != nil {
return "", fmt.Errorf("failed to get Release tag from spec %#q:\n%w", specPath, err)
}
return releaseValue, nil
}
// ReleaseUsesAutorelease reports whether the given Release tag value uses the
// %autorelease macro (either bare or braced form).
func ReleaseUsesAutorelease(releaseValue string) bool {
return autoreleasePattern.MatchString(releaseValue)
}
// BumpStaticRelease increments the leading integer in a static Release tag value
// by the given commit count.
func BumpStaticRelease(releaseValue string, commitCount int) (string, error) {
matches := staticReleasePattern.FindStringSubmatch(releaseValue)
if matches == nil {
return "", fmt.Errorf("release value %#q does not start with an integer", releaseValue)
}
currentRelease, err := strconv.Atoi(matches[1])
if err != nil {
return "", fmt.Errorf("failed to parse release number from %#q:\n%w", releaseValue, err)
}
newRelease := currentRelease + commitCount
suffix := matches[2]
return fmt.Sprintf("%d%s", newRelease, suffix), nil
}
// tryBumpStaticRelease manages the Release tag based on the component's release
// calculation mode. It may bump, skip, or auto-detect depending on configuration:
//
// - "manual": no-op — component manages its own release numbering.
// - "autorelease": no-op — rpmautospec resolves the release from git history.
// - "static": always bumps the static integer release by commitCount.
// - "auto": auto-detects from the spec's Release tag value; skips if
// %autorelease is found, otherwise bumps the static integer.
func (p *sourcePreparerImpl) tryBumpStaticRelease(
component components.Component,
sourcesDirPath string,
commitCount int,
) error {
calc := component.GetConfig().Release.Calculation
switch calc {
case projectconfig.ReleaseCalculationManual:
slog.Debug("Component uses manual release calculation; skipping static release bump",
"component", component.GetName())
return nil
case projectconfig.ReleaseCalculationAutorelease:
slog.Debug("Component uses autorelease calculation; skipping static release bump",
"component", component.GetName())
return nil
case projectconfig.ReleaseCalculationStatic:
return p.readAndBumpRelease(component, sourcesDirPath, commitCount, true)
case projectconfig.ReleaseCalculationAuto:
return p.readAndBumpRelease(component, sourcesDirPath, commitCount, false)
default:
return fmt.Errorf("component %#q has unknown release calculation mode %#q",
component.GetName(), calc)
}
}
// readAndBumpRelease reads the Release tag from the spec and bumps its static integer.
// When requireStaticRelease is true (explicit static mode), encountering %autorelease
// produces an error telling the user to switch to 'release.calculation = "autorelease"'.
// When false (auto mode), specs using %autorelease are silently skipped.
func (p *sourcePreparerImpl) readAndBumpRelease(
component components.Component,
sourcesDirPath string,
commitCount int,
requireStaticRelease bool,
) error {
specPath, err := p.resolveSpecPath(component, sourcesDirPath)
if err != nil {
return err
}
releaseValue, err := GetReleaseTagValue(p.fs, specPath, spec.WithEditor(p.specEditor))
if err != nil {
return fmt.Errorf("failed to read Release tag for component %#q:\n%w",
component.GetName(), err)
}
if ReleaseUsesAutorelease(releaseValue) {
if requireStaticRelease {
return fmt.Errorf(
"component %#q has 'release.calculation = \"static\"' but its Release tag "+
"uses %%autorelease; set 'release.calculation = \"autorelease\"' instead",
component.GetName())
}
slog.Debug("Spec uses %%autorelease; skipping static release bump",
"component", component.GetName())
return nil
}
newRelease, err := BumpStaticRelease(releaseValue, commitCount)
if err != nil {
return fmt.Errorf(
"component %#q has a non-standard Release tag value %#q that cannot be auto-bumped; "+
"set 'release.calculation = \"manual\"' in the component configuration "+
"and add a \"spec-set-tag\" overlay for the Release tag if needed:\n%w",
component.GetName(), releaseValue, err)
}
slog.Info("Bumping static release",
"component", component.GetName(),
"oldRelease", releaseValue,
"newRelease", newRelease,
"commitCount", commitCount)
overlay := projectconfig.ComponentOverlay{
Type: projectconfig.ComponentOverlayUpdateSpecTag,
Tag: "Release",
Value: newRelease,
}
if err := ApplySpecOverlayToFileInPlace(p.fs, overlay, specPath, spec.WithEditor(p.specEditor)); err != nil {
return fmt.Errorf("failed to apply release bump overlay for component %#q:\n%w",
component.GetName(), err)
}
return nil
}