Skip to content

Commit 8023558

Browse files
authored
fix(internal/semver): add Rust pre-GA change downgrade option (#3217)
1 parent 4bcdca8 commit 8023558

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

internal/librarian/internal/rust/release.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ func release(cfg *config.Config, name string) (*config.Config, error) {
8383
}
8484

8585
found = true
86-
// Only ever take a minor version bump.
87-
// TODO(https://github.com/googleapis/librarian/issues/3182): Implement desired pre-1.0.0 semantics.
88-
newVersion, err := semver.DeriveNextOptions{BumpVersionCore: true}.DeriveNext(semver.Minor, manifest.Package.Version)
86+
newVersion, err := semver.DeriveNextOptions{
87+
BumpVersionCore: true,
88+
DowngradePreGAChanges: true,
89+
}.DeriveNext(semver.Minor, manifest.Package.Version)
8990
if err != nil {
9091
return err
9192
}

internal/semver/semver.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ type DeriveNextOptions struct {
189189
// Default behavior is to prefer bumping the prerelease number or adding one
190190
// when the version is a prerelease without a number.
191191
BumpVersionCore bool
192+
193+
// DowngradePreGAChanges specifically forces [Minor] changes to be treated
194+
// as [Patch] changes when the current version is pre-1.0.0. [Major] changes
195+
// are always downgraded to [Minor] changes when the current version is
196+
// pre-1.0.0 regardless of if this is enabled. This is primarily for Rust.
197+
//
198+
// This has no effect on prerelease versions unless BumpVersionCore is also
199+
// enabled.
200+
DowngradePreGAChanges bool
192201
}
193202

194203
// DeriveNext determines the appropriate SemVer version bump based on the
@@ -223,9 +232,15 @@ func (o DeriveNextOptions) DeriveNext(highestChange ChangeLevel, currentVersion
223232
*v.PrereleaseNumber = 1
224233
}
225234

226-
// Breaking changes result in a minor bump for pre-1.0.0 versions.
227-
if highestChange == Major && v.Major == 0 {
228-
highestChange = Minor
235+
// Breaking changes result in a minor bump for pre-1.0.0 versions across
236+
// all languages. Some languages, however, prefer to downgrade all pre-1.0.0
237+
// changes e.g. Rust.
238+
if v.Major == 0 {
239+
if highestChange == Major {
240+
highestChange = Minor
241+
} else if highestChange == Minor && o.DowngradePreGAChanges {
242+
highestChange = Patch
243+
}
229244
}
230245

231246
// Bump the version core.

internal/semver/semver_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,18 @@ func TestDeriveNextOptions_DeriveNext(t *testing.T) {
282282
expectedVersion: "1.2.4",
283283
},
284284
{
285-
name: "pre-1.0.0 feat is patch bump",
286-
highestChange: Minor, // feat is minor
285+
name: "pre-1.0.0 feat is minor bump",
286+
highestChange: Minor,
287287
currentVersion: "0.2.3",
288288
expectedVersion: "0.3.0",
289289
},
290+
{
291+
name: "pre-1.0.0 feat with downgrade is patch bump",
292+
highestChange: Minor,
293+
currentVersion: "0.2.3",
294+
expectedVersion: "0.2.4",
295+
opts: DeriveNextOptions{DowngradePreGAChanges: true},
296+
},
290297
{
291298
name: "pre-1.0.0 fix is patch bump",
292299
highestChange: Patch,
@@ -337,6 +344,27 @@ func TestDeriveNextOptions_DeriveNext(t *testing.T) {
337344
expectedVersion: "1.3.0-alpha.1",
338345
opts: DeriveNextOptions{BumpVersionCore: true},
339346
},
347+
{
348+
name: "pre-1.0.0 prerelease with numeric trailer and bump core option",
349+
highestChange: Minor,
350+
currentVersion: "0.2.3-alpha.2",
351+
expectedVersion: "0.3.0-alpha.1",
352+
opts: DeriveNextOptions{BumpVersionCore: true},
353+
},
354+
{
355+
name: "pre-1.0.0 prerelease with numeric trailer, bump core and downgrade options",
356+
highestChange: Minor,
357+
currentVersion: "0.2.3-alpha.2",
358+
expectedVersion: "0.2.4-alpha.1",
359+
opts: DeriveNextOptions{BumpVersionCore: true, DowngradePreGAChanges: true},
360+
},
361+
{
362+
name: "pre-1.0.0 prerelease feat with downgrade, no bump core, is still prerelease bump",
363+
highestChange: Minor,
364+
currentVersion: "0.2.3-alpha.2",
365+
expectedVersion: "0.2.3-alpha.3",
366+
opts: DeriveNextOptions{DowngradePreGAChanges: true},
367+
},
340368
} {
341369
t.Run(test.name, func(t *testing.T) {
342370
nextVersion, err := test.opts.DeriveNext(test.highestChange, test.currentVersion)

internal/sidekick/rust_release/update_manifest.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ func updateManifest(config *config.Release, lastTag, manifest string) ([]string,
6060
if !info.Package.Publish {
6161
return nil, nil
6262
}
63-
// Only ever take a minor version bump.
64-
// TODO(https://github.com/googleapis/librarian/issues/3182): Implement desired pre-1.0.0 semantics.
65-
newVersion, err := semver.DeriveNextOptions{BumpVersionCore: true}.DeriveNext(semver.Minor, info.Package.Version)
63+
newVersion, err := semver.DeriveNextOptions{
64+
BumpVersionCore: true,
65+
DowngradePreGAChanges: true,
66+
}.DeriveNext(semver.Minor, info.Package.Version)
6667
if err != nil {
6768
return nil, err
6869
}

0 commit comments

Comments
 (0)