Skip to content

Commit aa507b1

Browse files
authored
fix: ignore version override if library flag is not specified (#1822)
Fixes #1823
1 parent c0c043b commit aa507b1

File tree

2 files changed

+48
-133
lines changed

2 files changed

+48
-133
lines changed

internal/librarian/release_init.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
125125
continue
126126
}
127127
// Only update one library with the given library ID.
128-
if err := updateLibrary(r, r.state, i); err != nil {
128+
updatedLibrary, err := updateLibrary(r.repo, library, r.cfg.LibraryVersion)
129+
if err != nil {
129130
return err
130131
}
132+
r.state.Libraries[i] = updatedLibrary
131133
if err := copyLibrary(dst, src, library); err != nil {
132134
return err
133135
}
@@ -136,9 +138,11 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
136138
}
137139

138140
// Update all libraries.
139-
if err := updateLibrary(r, r.state, i); err != nil {
141+
updatedLibrary, err := updateLibrary(r.repo, library, r.cfg.LibraryVersion)
142+
if err != nil {
140143
return err
141144
}
145+
r.state.Libraries[i] = updatedLibrary
142146
if err := copyLibrary(dst, src, library); err != nil {
143147
return err
144148
}
@@ -188,28 +192,25 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
188192
return cleanAndCopyGlobalAllowlist(r.librarianConfig, r.repo.GetDir(), outputDir)
189193
}
190194

191-
// updateLibrary updates the library which is the index-th library in the given
192-
// [config.LibrarianState].
193-
func updateLibrary(r *initRunner, state *config.LibrarianState, index int) error {
194-
library := state.Libraries[index]
195-
updatedLibrary, err := getChangesOf(r.repo, library)
195+
// updateLibrary updates the given library in the following way:
196+
//
197+
// 1. Get the library's commit history in the given git repository.
198+
//
199+
// 2. Override the library version if libraryVersion is not empty.
200+
//
201+
// 3. Set the library's release trigger to true.
202+
func updateLibrary(repo gitrepo.Repository, library *config.LibraryState, libraryVersion string) (*config.LibraryState, error) {
203+
updatedLibrary, err := getChangesOf(repo, library)
196204
if err != nil {
197-
return fmt.Errorf("failed to update library, %s: %w", library.ID, err)
205+
return nil, fmt.Errorf("failed to update library, %s: %w", library.ID, err)
198206
}
199207

200-
setReleaseTrigger(updatedLibrary, r.cfg.LibraryVersion, true)
201-
state.Libraries[index] = updatedLibrary
202-
203-
return nil
204-
}
205-
206-
// setReleaseTrigger sets the release trigger for the given library and
207-
// overrides the version, if provided.
208-
func setReleaseTrigger(library *config.LibraryState, libraryVersion string, trigger bool) {
209208
if libraryVersion != "" {
210-
library.Version = libraryVersion
209+
updatedLibrary.Version = libraryVersion
211210
}
212-
library.ReleaseTriggered = trigger
211+
updatedLibrary.ReleaseTriggered = true
212+
213+
return updatedLibrary, nil
213214
}
214215

215216
// getChangesOf gets commit history of the given library.

internal/librarian/release_init_test.go

Lines changed: 28 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -385,61 +385,14 @@ func TestInitRun(t *testing.T) {
385385
}
386386
}
387387

388-
func TestSetReleaseTrigger(t *testing.T) {
389-
t.Parallel()
390-
for _, test := range []struct {
391-
name string
392-
library *config.LibraryState
393-
libraryID string
394-
libraryVersion string
395-
trigger bool
396-
want *config.LibraryState
397-
}{
398-
{
399-
name: "set trigger for a library",
400-
library: &config.LibraryState{
401-
ID: "one-example-id",
402-
Version: "1.0.0",
403-
},
404-
trigger: true,
405-
want: &config.LibraryState{
406-
ID: "one-example-id",
407-
Version: "1.0.0",
408-
ReleaseTriggered: true,
409-
},
410-
},
411-
{
412-
name: "set trigger for one library and override version",
413-
library: &config.LibraryState{
414-
ID: "one-example-id",
415-
Version: "1.0.0",
416-
},
417-
trigger: true,
418-
libraryVersion: "2.0.0",
419-
want: &config.LibraryState{
420-
ID: "one-example-id",
421-
Version: "2.0.0",
422-
ReleaseTriggered: true,
423-
},
424-
},
425-
} {
426-
t.Run(test.name, func(t *testing.T) {
427-
setReleaseTrigger(test.library, test.libraryVersion, test.trigger)
428-
if diff := cmp.Diff(test.want, test.library); diff != "" {
429-
t.Errorf("state mismatch (-want +got):\n%s", diff)
430-
}
431-
})
432-
}
433-
}
434-
435388
func TestUpdateLibrary(t *testing.T) {
436389
t.Parallel()
437390
for _, test := range []struct {
438391
name string
439392
pathAndMessages []pathAndMessage
440393
tags []string
441-
runner *initRunner
442-
state *config.LibrarianState
394+
libraryVersion string
395+
library *config.LibraryState
443396
repo gitrepo.Repository
444397
want *config.LibraryState
445398
wantErr bool
@@ -468,28 +421,13 @@ func TestUpdateLibrary(t *testing.T) {
468421
tags: []string{
469422
"one-id-1.2.3",
470423
},
471-
runner: &initRunner{
472-
cfg: &config.Config{
473-
LibraryVersion: "2.0.0",
474-
},
475-
},
476-
state: &config.LibrarianState{
477-
Libraries: []*config.LibraryState{
478-
{
479-
ID: "another-id",
480-
Version: "2.3.4",
481-
SourceRoots: []string{
482-
"another/path",
483-
},
484-
},
485-
{
486-
ID: "one-id",
487-
Version: "1.2.3",
488-
SourceRoots: []string{
489-
"one/path",
490-
"two/path",
491-
},
492-
},
424+
libraryVersion: "2.0.0",
425+
library: &config.LibraryState{
426+
ID: "one-id",
427+
Version: "1.2.3",
428+
SourceRoots: []string{
429+
"one/path",
430+
"two/path",
493431
},
494432
},
495433
want: &config.LibraryState{
@@ -532,26 +470,13 @@ func TestUpdateLibrary(t *testing.T) {
532470
},
533471
tags: []string{
534472
"one-id-1.2.3",
535-
"another-id-2.3.4",
536473
},
537-
runner: &initRunner{cfg: &config.Config{}},
538-
state: &config.LibrarianState{
539-
Libraries: []*config.LibraryState{
540-
{
541-
ID: "another-id",
542-
Version: "2.3.4",
543-
SourceRoots: []string{
544-
"another/path",
545-
},
546-
},
547-
{
548-
ID: "one-id",
549-
Version: "1.2.3",
550-
SourceRoots: []string{
551-
"one/path",
552-
"two/path",
553-
},
554-
},
474+
library: &config.LibraryState{
475+
ID: "one-id",
476+
Version: "1.2.3",
477+
SourceRoots: []string{
478+
"one/path",
479+
"two/path",
555480
},
556481
},
557482
want: &config.LibraryState{
@@ -576,25 +501,13 @@ func TestUpdateLibrary(t *testing.T) {
576501
},
577502
},
578503
{
579-
name: "failed to get commit history of one library",
580-
runner: &initRunner{cfg: &config.Config{}},
581-
state: &config.LibrarianState{
582-
Libraries: []*config.LibraryState{
583-
{
584-
ID: "another-id",
585-
Version: "2.3.4",
586-
SourceRoots: []string{
587-
"another/path",
588-
},
589-
},
590-
{
591-
ID: "one-id",
592-
Version: "1.2.3",
593-
SourceRoots: []string{
594-
"one/path",
595-
"two/path",
596-
},
597-
},
504+
name: "failed to get commit history of one library",
505+
library: &config.LibraryState{
506+
ID: "one-id",
507+
Version: "1.2.3",
508+
SourceRoots: []string{
509+
"one/path",
510+
"two/path",
598511
},
599512
},
600513
repo: &MockRepository{
@@ -606,12 +519,12 @@ func TestUpdateLibrary(t *testing.T) {
606519
} {
607520
t.Run(test.name, func(t *testing.T) {
608521
var err error
522+
var updated *config.LibraryState
609523
if test.repo != nil {
610-
test.runner.repo = test.repo
611-
err = updateLibrary(test.runner, test.state, 1)
524+
updated, err = updateLibrary(test.repo, test.library, test.libraryVersion)
612525
} else {
613-
test.runner.repo = setupRepoForGetCommits(t, test.pathAndMessages, test.tags)
614-
err = updateLibrary(test.runner, test.state, 1)
526+
repo := setupRepoForGetCommits(t, test.pathAndMessages, test.tags)
527+
updated, err = updateLibrary(repo, test.library, test.libraryVersion)
615528
}
616529

617530
if test.wantErr {
@@ -625,10 +538,11 @@ func TestUpdateLibrary(t *testing.T) {
625538

626539
return
627540
}
541+
628542
if err != nil {
629543
t.Errorf("failed to run getChangesOf(): %q", err.Error())
630544
}
631-
if diff := cmp.Diff(test.want, test.state.Libraries[1], cmpopts.IgnoreFields(config.Change{}, "CommitHash")); diff != "" {
545+
if diff := cmp.Diff(test.want, updated, cmpopts.IgnoreFields(config.Change{}, "CommitHash")); diff != "" {
632546
t.Errorf("state mismatch (-want +got):\n%s", diff)
633547
}
634548
})

0 commit comments

Comments
 (0)