Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions internal/legacylibrarian/legacylibrarian/release_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ package legacylibrarian

import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"

"github.com/googleapis/librarian/internal/config"
"github.com/googleapis/librarian/internal/legacylibrarian/legacyconfig"
"github.com/googleapis/librarian/internal/legacylibrarian/legacydocker"
"github.com/googleapis/librarian/internal/legacylibrarian/legacygitrepo"
"github.com/googleapis/librarian/internal/librarian"
"github.com/googleapis/librarian/internal/semver"
"github.com/googleapis/librarian/internal/yaml"
)

var errVersionRegression = errors.New("version is regression")

type stageRunner struct {
branch string
commit bool
Expand Down Expand Up @@ -87,6 +93,9 @@ func (r *stageRunner) run(ctx context.Context) error {
if err := saveLibrarianState(r.repo.GetDir(), r.state); err != nil {
return err
}
if err := r.updateLibrarianYAML(ctx); err != nil {
return err
}

prBodyBuilder := func() (string, error) {
gitHubRepo, err := GetGitHubRepositoryFromGitRepo(r.repo)
Expand Down Expand Up @@ -366,3 +375,36 @@ func toCommit(c []*legacygitrepo.ConventionalCommit, libraryID string) []*legacy
}
return commits
}

func (r *stageRunner) updateLibrarianYAML(ctx context.Context) error {
librarianYAMLPath := filepath.Join(r.repo.GetDir(), config.LibrarianYAML)
cfg, err := yaml.Read[config.Config](librarianYAMLPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
newCfg, err := syncVersion(r.state, cfg)
if err != nil {
return err
}
return librarian.RunTidyOnConfig(ctx, r.repo.GetDir(), newCfg)
}

func syncVersion(state *legacyconfig.LibrarianState, cfg *config.Config) (*config.Config, error) {
for _, lib := range cfg.Libraries {
legacyLibrary := state.LibraryByID(lib.Name)
if legacyLibrary == nil || legacyLibrary.Version == "" {
continue
}
maxVersion := semver.MaxVersion(lib.Version, legacyLibrary.Version)
if maxVersion == lib.Version && legacyLibrary.Version != lib.Version {
// lib.Version is greater than legacyLibrary.Version, something is
// wrong, fail in this case.
return nil, fmt.Errorf("library %s, version in state, %s, is smaller than version in librarian.yaml, %s: %w", lib.Name, legacyLibrary.Version, lib.Version, errVersionRegression)
}
lib.Version = legacyLibrary.Version
}
return cfg, nil
}
Loading
Loading