|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package sources |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "regexp" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components" |
| 14 | + "github.com/microsoft/azure-linux-dev-tools/internal/global/opctx" |
| 15 | + "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" |
| 16 | + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/spec" |
| 17 | +) |
| 18 | + |
| 19 | +// autoreleasePattern matches the %autorelease macro invocation in a Release tag value. |
| 20 | +// This covers both the bare form (%autorelease) and the braced form (%{autorelease}). |
| 21 | +var autoreleasePattern = regexp.MustCompile(`%(\{autorelease\}|autorelease($|\s))`) |
| 22 | + |
| 23 | +// staticReleasePattern matches a leading integer in a static Release tag value, |
| 24 | +// followed by an optional suffix (e.g. "%{?dist}"). |
| 25 | +var staticReleasePattern = regexp.MustCompile(`^(\d+)(.*)$`) |
| 26 | + |
| 27 | +// GetReleaseTagValue reads the Release tag value from the spec file at specPath. |
| 28 | +// It returns the raw value string as written in the spec (e.g. "1%{?dist}" or "%autorelease"). |
| 29 | +// Returns [spec.ErrNoSuchTag] if no Release tag is found. |
| 30 | +func GetReleaseTagValue(fs opctx.FS, specPath string) (string, error) { |
| 31 | + specFile, err := fs.Open(specPath) |
| 32 | + if err != nil { |
| 33 | + return "", fmt.Errorf("failed to open spec %#q:\n%w", specPath, err) |
| 34 | + } |
| 35 | + defer specFile.Close() |
| 36 | + |
| 37 | + openedSpec, err := spec.OpenSpec(specFile) |
| 38 | + if err != nil { |
| 39 | + return "", fmt.Errorf("failed to parse spec %#q:\n%w", specPath, err) |
| 40 | + } |
| 41 | + |
| 42 | + var releaseValue string |
| 43 | + |
| 44 | + err = openedSpec.VisitTagsPackage("", func(tagLine *spec.TagLine, _ *spec.Context) error { |
| 45 | + if strings.EqualFold(tagLine.Tag, "Release") { |
| 46 | + releaseValue = tagLine.Value |
| 47 | + } |
| 48 | + |
| 49 | + return nil |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + return "", fmt.Errorf("failed to visit tags in spec %#q:\n%w", specPath, err) |
| 53 | + } |
| 54 | + |
| 55 | + if releaseValue == "" { |
| 56 | + return "", fmt.Errorf("release tag not found in spec %#q:\n%w", specPath, spec.ErrNoSuchTag) |
| 57 | + } |
| 58 | + |
| 59 | + return releaseValue, nil |
| 60 | +} |
| 61 | + |
| 62 | +// ReleaseUsesAutorelease reports whether the given Release tag value uses the |
| 63 | +// %autorelease macro (either bare or braced form). |
| 64 | +func ReleaseUsesAutorelease(releaseValue string) bool { |
| 65 | + return autoreleasePattern.MatchString(releaseValue) |
| 66 | +} |
| 67 | + |
| 68 | +// BumpStaticRelease increments the leading integer in a static Release tag value |
| 69 | +// by the given commit count. |
| 70 | +func BumpStaticRelease(releaseValue string, commitCount int) (string, error) { |
| 71 | + matches := staticReleasePattern.FindStringSubmatch(releaseValue) |
| 72 | + if matches == nil { |
| 73 | + return "", fmt.Errorf("release value %#q does not start with an integer", releaseValue) |
| 74 | + } |
| 75 | + |
| 76 | + currentRelease, err := strconv.Atoi(matches[1]) |
| 77 | + if err != nil { |
| 78 | + return "", fmt.Errorf("failed to parse release number from %#q:\n%w", releaseValue, err) |
| 79 | + } |
| 80 | + |
| 81 | + newRelease := currentRelease + commitCount |
| 82 | + suffix := matches[2] |
| 83 | + |
| 84 | + return fmt.Sprintf("%d%s", newRelease, suffix), nil |
| 85 | +} |
| 86 | + |
| 87 | +// HasUserReleaseOverlay reports whether the given overlay list contains an overlay |
| 88 | +// that explicitly sets or updates the Release tag. This is used to determine whether |
| 89 | +// a user has configured the component to handle a non-standard Release value |
| 90 | +// (e.g. one using a custom macro like %{pkg_release}). |
| 91 | +func HasUserReleaseOverlay(overlays []projectconfig.ComponentOverlay) bool { |
| 92 | + for _, overlay := range overlays { |
| 93 | + if !strings.EqualFold(overlay.Tag, "Release") { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if overlay.Type == projectconfig.ComponentOverlaySetSpecTag || |
| 98 | + overlay.Type == projectconfig.ComponentOverlayUpdateSpecTag { |
| 99 | + return true |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +// tryBumpStaticRelease checks whether the component's spec uses %autorelease. |
| 107 | +// If not, it bumps the static Release tag by commitCount and applies the change |
| 108 | +// as an overlay to the spec file in-place. This ensures that components with static |
| 109 | +// release numbers get deterministic version bumps matching the number of synthetic |
| 110 | +// commits applied from the project repository. |
| 111 | +// |
| 112 | +// When the spec uses %autorelease, this function is a no-op because rpmautospec |
| 113 | +// already resolves the release number from git history. |
| 114 | +// |
| 115 | +// When the Release tag uses a non-standard value (not %autorelease and not a leading |
| 116 | +// integer, e.g. %{pkg_release}), the component must define an explicit overlay that |
| 117 | +// sets the Release tag. If no such overlay exists, an error is returned. |
| 118 | +func (p *sourcePreparerImpl) tryBumpStaticRelease( |
| 119 | + component components.Component, |
| 120 | + sourcesDirPath string, |
| 121 | + commitCount int, |
| 122 | +) error { |
| 123 | + specPath, err := p.resolveSpecPath(component, sourcesDirPath) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + releaseValue, err := GetReleaseTagValue(p.fs, specPath) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed to read Release tag for component %#q:\n%w", |
| 131 | + component.GetName(), err) |
| 132 | + } |
| 133 | + |
| 134 | + if ReleaseUsesAutorelease(releaseValue) { |
| 135 | + slog.Debug("Spec uses %%autorelease; skipping static release bump", |
| 136 | + "component", component.GetName()) |
| 137 | + |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + // Skip static release bump if the user has defined an explicit overlay for the Release tag. |
| 142 | + if HasUserReleaseOverlay(component.GetConfig().Overlays) { |
| 143 | + slog.Debug("Component has an explicit Release overlay; skipping static release bump", |
| 144 | + "component", component.GetName()) |
| 145 | + |
| 146 | + return nil |
| 147 | + } |
| 148 | + |
| 149 | + newRelease, err := BumpStaticRelease(releaseValue, commitCount) |
| 150 | + if err != nil { |
| 151 | + // The Release tag does not start with an integer (e.g. %{pkg_release}) |
| 152 | + // and the user did not provide an explicit overlay to set it. |
| 153 | + return fmt.Errorf( |
| 154 | + "component %#q has a non-standard Release tag value %#q that cannot be auto-bumped; "+ |
| 155 | + "add a \"spec-set-tag\" overlay for the Release tag in the component configuration:\n%w", |
| 156 | + component.GetName(), releaseValue, err) |
| 157 | + } |
| 158 | + |
| 159 | + slog.Info("Bumping static release", |
| 160 | + "component", component.GetName(), |
| 161 | + "oldRelease", releaseValue, |
| 162 | + "newRelease", newRelease, |
| 163 | + "commitCount", commitCount) |
| 164 | + |
| 165 | + overlay := projectconfig.ComponentOverlay{ |
| 166 | + Type: projectconfig.ComponentOverlayUpdateSpecTag, |
| 167 | + Tag: "Release", |
| 168 | + Value: newRelease, |
| 169 | + } |
| 170 | + |
| 171 | + if err := ApplySpecOverlayToFileInPlace(p.fs, overlay, specPath); err != nil { |
| 172 | + return fmt.Errorf("failed to apply release bump overlay for component %#q:\n%w", |
| 173 | + component.GetName(), err) |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
0 commit comments