Skip to content

Commit 0ee547d

Browse files
authored
fix: Skip release if there are no releasable units (#2010)
Fixes: #1971 --------- Signed-off-by: Lawrence Qiu <[email protected]>
1 parent b91721a commit 0ee547d

File tree

2 files changed

+590
-295
lines changed

2 files changed

+590
-295
lines changed

internal/librarian/release_init.go

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ func (r *initRunner) run(ctx context.Context) error {
7979
return err
8080
}
8181

82+
// No need to update the librarian state if there are no libraries
83+
// that need to be released
84+
if !hasLibrariesToRelease(r.state.Libraries) {
85+
slog.Info("No release created; skipping the commit/PR")
86+
return nil
87+
}
88+
8289
if err := saveLibrarianState(r.repo.GetDir(), r.state); err != nil {
8390
return err
8491
}
@@ -106,39 +113,54 @@ func (r *initRunner) run(ctx context.Context) error {
106113
return nil
107114
}
108115

116+
// hasLibrariesToRelease searches through the state of each library and checks
117+
// that there is a single library configured to be triggered.
118+
func hasLibrariesToRelease(libraryStates []*config.LibraryState) bool {
119+
for _, library := range libraryStates {
120+
if library.ReleaseTriggered {
121+
return true
122+
}
123+
}
124+
return false
125+
}
126+
109127
func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error {
110128
dst := r.partialRepo
111129
if err := os.MkdirAll(dst, 0755); err != nil {
112130
return fmt.Errorf("failed to make directory: %w", err)
113131
}
114132

115133
src := r.repo.GetDir()
116-
for _, library := range r.state.Libraries {
117-
if r.library != "" {
118-
if r.library != library.ID {
119-
continue
120-
}
121-
122-
// Only update one library with the given library ID.
123-
if err := r.updateLibrary(library); err != nil {
124-
return err
125-
}
126-
if err := copyLibraryFiles(r.state, dst, library.ID, src); err != nil {
127-
return err
128-
}
129-
130-
break
134+
librariesToRelease := r.state.Libraries
135+
// If a library has been specified, only process the release for it
136+
if r.library != "" {
137+
library := findLibraryByID(r.state, r.library)
138+
if library == nil {
139+
slog.Error("Unable to find the specified library. Cannot proceed with the release.", "library", r.library)
140+
return fmt.Errorf("unable to find library for release: %s", r.library)
131141
}
132-
133-
// Update all libraries.
142+
librariesToRelease = []*config.LibraryState{library}
143+
}
144+
// Mark if there are any library that needs to be released
145+
foundReleasableLibrary := false
146+
for _, library := range librariesToRelease {
134147
if err := r.updateLibrary(library); err != nil {
135148
return err
136149
}
137-
if err := copyLibraryFiles(r.state, dst, library.ID, src); err != nil {
138-
return err
150+
// Copy the library files over if a release is needed
151+
if library.ReleaseTriggered {
152+
foundReleasableLibrary = true
153+
if err := copyLibraryFiles(r.state, dst, library.ID, src); err != nil {
154+
return err
155+
}
139156
}
140157
}
141158

159+
if !foundReleasableLibrary {
160+
slog.Info("No libraries need to be released")
161+
return nil
162+
}
163+
142164
if err := copyLibrarianDir(dst, src); err != nil {
143165
return fmt.Errorf("failed to copy librarian dir from %s to %s: %w", src, dst, err)
144166
}
@@ -169,22 +191,12 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
169191
return err
170192
}
171193

172-
for _, library := range r.state.Libraries {
173-
if r.library != "" {
174-
if r.library != library.ID {
175-
continue
176-
}
177-
// Only copy one library to repository.
178-
if err := copyLibraryFiles(r.state, r.repo.GetDir(), r.library, outputDir); err != nil {
194+
for _, library := range librariesToRelease {
195+
// Copy the library files back if a release is needed
196+
if library.ReleaseTriggered {
197+
if err := copyLibraryFiles(r.state, r.repo.GetDir(), library.ID, outputDir); err != nil {
179198
return err
180199
}
181-
182-
break
183-
}
184-
185-
// Copy all libraries to repository.
186-
if err := copyLibraryFiles(r.state, r.repo.GetDir(), library.ID, outputDir); err != nil {
187-
return err
188200
}
189201
}
190202

@@ -201,16 +213,13 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
201213
//
202214
// 4. Set the library's release trigger to true.
203215
func (r *initRunner) updateLibrary(library *config.LibraryState) error {
204-
// Update the previous version, we need this value when creating release note.
205-
library.PreviousVersion = library.Version
206216
commits, err := GetConventionalCommitsSinceLastRelease(r.repo, library)
207217
if err != nil {
208218
return fmt.Errorf("failed to fetch conventional commits for library, %s: %w", library.ID, err)
209219
}
210220

211-
library.Changes = commits
212-
if len(library.Changes) == 0 {
213-
slog.Info("Skip releasing library since no eligible change is found", "library", library.ID)
221+
if len(commits) == 0 {
222+
slog.Info("Skip releasing library as there are no commits", "library", library.ID)
214223
return nil
215224
}
216225

@@ -219,8 +228,14 @@ func (r *initRunner) updateLibrary(library *config.LibraryState) error {
219228
return err
220229
}
221230

222-
library.Version = nextVersion
223-
library.ReleaseTriggered = true
231+
// Update the previous version, we need this value when creating release note.
232+
library.PreviousVersion = library.Version
233+
library.Changes = commits
234+
// Only update the library state if there are releasable units
235+
if library.Version != nextVersion {
236+
library.Version = nextVersion
237+
library.ReleaseTriggered = true
238+
}
224239

225240
return nil
226241
}

0 commit comments

Comments
 (0)