Skip to content

Commit 56d59af

Browse files
authored
fix: read release init response (#2075)
Fixes #2057
1 parent 7702829 commit 56d59af

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

internal/config/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ const (
5151
LibrarianDir = ".librarian"
5252
// ReleaseInitRequest is a JSON file that describes which library to release.
5353
ReleaseInitRequest = "release-init-request.json"
54-
55-
pipelineStateFile = "state.yaml"
56-
versionCmdName = "version"
54+
// ReleaseInitResponse is a JSON file that describes which library to change
55+
// after release.
56+
ReleaseInitResponse = "release-init-response.json"
57+
pipelineStateFile = "state.yaml"
58+
versionCmdName = "version"
5759
)
5860

5961
// are variables so it can be replaced during testing.

internal/librarian/mocks_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ type mockContainerClient struct {
125125
noBuildResponse bool
126126
noConfigureResponse bool
127127
noGenerateResponse bool
128+
noReleaseResponse bool
128129
noInitVersion bool
129130
wantErrorMsg bool
130131
// Set this value if you want library files
@@ -264,6 +265,25 @@ func (m *mockContainerClient) Generate(ctx context.Context, request *docker.Gene
264265

265266
func (m *mockContainerClient) ReleaseInit(ctx context.Context, request *docker.ReleaseInitRequest) error {
266267
m.initCalls++
268+
if m.noReleaseResponse {
269+
return m.initErr
270+
}
271+
// Write a release-init-response.json unless we're configured not to.
272+
if err := os.MkdirAll(filepath.Join(request.PartialRepoDir, ".librarian"), 0755); err != nil {
273+
return err
274+
}
275+
276+
library := &config.LibraryState{}
277+
if m.wantErrorMsg {
278+
library.ErrorMessage = "simulated error message"
279+
}
280+
b, err := json.MarshalIndent(library, "", " ")
281+
if err != nil {
282+
return err
283+
}
284+
if err := os.WriteFile(filepath.Join(request.PartialRepoDir, ".librarian", config.ReleaseInitResponse), b, 0755); err != nil {
285+
return err
286+
}
267287
return m.initErr
268288
}
269289

internal/librarian/release_init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ func (r *initRunner) runInitCommand(ctx context.Context, outputDir string) error
204204
return err
205205
}
206206

207+
// Read the response file.
208+
if _, err := readLibraryState(
209+
filepath.Join(initRequest.PartialRepoDir, config.LibrarianDir, config.ReleaseInitResponse)); err != nil {
210+
return err
211+
}
212+
207213
for _, library := range r.state.Libraries {
208214
if r.cfg.Library != "" {
209215
if r.cfg.Library != library.ID {

internal/librarian/release_init_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ func TestInitRun(t *testing.T) {
338338
},
339339
},
340340
},
341-
342341
{
343342
name: "docker command returns error",
344343
runner: &initRunner{
@@ -357,6 +356,24 @@ func TestInitRun(t *testing.T) {
357356
wantErr: true,
358357
wantErrMsg: "simulated init error",
359358
},
359+
{
360+
name: "release response contains error message",
361+
runner: &initRunner{
362+
workRoot: t.TempDir(),
363+
containerClient: &mockContainerClient{
364+
wantErrorMsg: true,
365+
},
366+
cfg: &config.Config{},
367+
state: &config.LibrarianState{},
368+
repo: &MockRepository{
369+
Dir: t.TempDir(),
370+
},
371+
partialRepo: t.TempDir(),
372+
librarianConfig: &config.LibrarianConfig{},
373+
},
374+
wantErr: true,
375+
wantErrMsg: "failed with error message: simulated error message",
376+
},
360377
{
361378
name: "invalid work root",
362379
runner: &initRunner{

0 commit comments

Comments
 (0)