diff --git a/cli/pkg/release/providers/docs.go b/cli/pkg/release/providers/docs.go index 2e435c29..2a30d07e 100644 --- a/cli/pkg/release/providers/docs.go +++ b/cli/pkg/release/providers/docs.go @@ -89,7 +89,7 @@ func (r *DocsReleaser) Release() error { } r.logger.Info("Cleaning existing docs from S3", "bucket", docsConfig.Bucket, "path", s3Path) - if err := r.s3.DeleteDirectory(docsConfig.Bucket, s3Path, []string{".*/b/.*"}); err != nil { + if err := r.s3.DeleteDirectory(docsConfig.Bucket, s3Path, nil); err != nil { return fmt.Errorf("failed to clean existing docs from S3: %w", err) } @@ -100,6 +100,7 @@ func (r *DocsReleaser) Release() error { } if github.InCI() { + r.logger.Info("Posting comment", "url", docsConfig.Url, "project", projectName) url := r.project.Blueprint.Global.Ci.Release.Docs.Url if err := r.postComment(url, projectName); err != nil { return fmt.Errorf("failed to post comment: %w", err) @@ -111,11 +112,11 @@ func (r *DocsReleaser) Release() error { } if isDefault { - if err := r.cleanupBranches(docsConfig.Bucket, filepath.Join(s3Path, "b")); err != nil { + r.logger.Info("Cleaning up branches from S3", "bucket", docsConfig.Bucket, "path", filepath.Dir(s3Path)) + if err := r.cleanupBranches(docsConfig.Bucket, filepath.Dir(s3Path)); err != nil { return fmt.Errorf("failed to cleanup branches: %w", err) } } - } r.logger.Info("Docs release complete") @@ -133,11 +134,13 @@ func (r *DocsReleaser) cleanupBranches(bucket, path string) error { for _, branch := range branches { branchNames = append(branchNames, branch.Name) } + r.logger.Info("Repo branches", "branches", branchNames) children, err := r.s3.ListImmediateChildren(bucket, path) if err != nil { return fmt.Errorf("failed to list immediate children: %w", err) } + r.logger.Info("Docs branches", "branches", children) for _, child := range children { if !slices.Contains(branchNames, child) { @@ -154,25 +157,19 @@ func (r *DocsReleaser) cleanupBranches(bucket, path string) error { // generatePath generates the S3 path for the docs. func (r *DocsReleaser) generatePath(projectName string) (string, error) { docsConfig := r.project.Blueprint.Global.Ci.Release.Docs - if docsConfig.Bucket == "" { - return "", fmt.Errorf("no S3 bucket specified in global docs configuration") - } - - s3Path := projectName - if docsConfig.Path != "" { - s3Path = filepath.Join(docsConfig.Path, projectName) - } - branch, err := git.GetBranch(r.ghClient, r.project.Repo) if err != nil { return "", fmt.Errorf("failed to get branch: %w", err) } - if branch == r.project.Blueprint.Global.Repo.DefaultBranch { - return s3Path, nil + var s3Path string + if docsConfig.Path != "" { + s3Path = filepath.Join(docsConfig.Path, projectName, branch) + } else { + s3Path = filepath.Join(projectName, branch) } - return filepath.Join(s3Path, "b", branch), nil + return s3Path, nil } // isDefaultBranch returns true if the current branch is the default branch. @@ -211,12 +208,7 @@ func (r *DocsReleaser) postComment(baseURL, name string) error { return fmt.Errorf("failed to get branch: %w", err) } - var docURL string - if branch == r.project.Blueprint.Global.Repo.DefaultBranch { - docURL, err = url.JoinPath(baseURL, name) - } else { - docURL, err = url.JoinPath(baseURL, name, "b", branch) - } + docURL, err := url.JoinPath(baseURL, name, branch) if err != nil { return fmt.Errorf("failed to join URL path: %w", err) } diff --git a/cli/pkg/release/providers/docs_test.go b/cli/pkg/release/providers/docs_test.go index 26820456..844ea8ca 100644 --- a/cli/pkg/release/providers/docs_test.go +++ b/cli/pkg/release/providers/docs_test.go @@ -121,25 +121,29 @@ func TestDocsReleaserRelease(t *testing.T) { }, }, prComments: []gh.PullRequestComment{}, - branches: []gh.Branch{}, - inCI: true, - isPR: false, + branches: []gh.Branch{ + { + Name: "master", + }, + }, + inCI: true, + isPR: false, validate: func(t *testing.T, result testResult) { assert.NoError(t, result.err) - exists, err := result.s3Fs.Exists("/bucket/prefix/test/index.html") + exists, err := result.s3Fs.Exists("/bucket/prefix/test/master/index.html") require.NoError(t, err) assert.True(t, exists) - exists, err = result.s3Fs.Exists("/bucket/prefix/test/test.html") + exists, err = result.s3Fs.Exists("/bucket/prefix/test/master/test.html") require.NoError(t, err) assert.False(t, exists) - exists, err = result.s3Fs.Exists("/bucket/prefix/test/b/mybranch/index.html") + exists, err = result.s3Fs.Exists("/bucket/prefix/test/mybranch/index.html") require.NoError(t, err) assert.False(t, exists) - content, err := result.s3Fs.ReadFile("/bucket/prefix/test/index.html") + content, err := result.s3Fs.ReadFile("/bucket/prefix/test/master/index.html") require.NoError(t, err) assert.Equal(t, "test docs", string(content)) @@ -169,15 +173,15 @@ func TestDocsReleaserRelease(t *testing.T) { validate: func(t *testing.T, result testResult) { assert.NoError(t, result.err) - exists, err := result.s3Fs.Exists("/bucket/prefix/test/b/mybranch/index.html") + exists, err := result.s3Fs.Exists("/bucket/prefix/test/mybranch/index.html") require.NoError(t, err) assert.True(t, exists) - exists, err = result.s3Fs.Exists("/bucket/prefix/test/b/mybranch/test.html") + exists, err = result.s3Fs.Exists("/bucket/prefix/test/mybranch/test.html") require.NoError(t, err) assert.False(t, exists) - content, err := result.s3Fs.ReadFile("/bucket/prefix/test/b/mybranch/index.html") + content, err := result.s3Fs.ReadFile("/bucket/prefix/test/mybranch/index.html") require.NoError(t, err) assert.Equal(t, "test docs", string(content)) @@ -187,7 +191,7 @@ func TestDocsReleaserRelease(t *testing.T) { The docs for this PR can be previewed at the following URL: -https://docs.example.com/test/b/mybranch +https://docs.example.com/test/mybranch ` assert.Equal(t, expectedBody, result.prPost.body) @@ -241,6 +245,7 @@ https://docs.example.com/test/b/mybranch prj.Blueprint.Global.Ci.Release.Docs.Bucket, prj.Blueprint.Global.Ci.Release.Docs.Path, tt.releaseName, + tt.curBranch, name, ) require.NoError(t, s3Fs.WriteFile(p, []byte(content), 0o644)) @@ -251,7 +256,6 @@ https://docs.example.com/test/b/mybranch prj.Blueprint.Global.Ci.Release.Docs.Bucket, prj.Blueprint.Global.Ci.Release.Docs.Path, tt.releaseName, - "b", branchFile.branch, branchFile.name, ) @@ -294,10 +298,6 @@ https://docs.example.com/test/b/mybranch }, ListObjectsV2Func: func(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error) { bucket := *params.Bucket - prefix := "" - if params.Prefix != nil { - prefix = *params.Prefix - } bucketDir := "/" + bucket if params.Delimiter != nil { @@ -308,13 +308,14 @@ https://docs.example.com/test/b/mybranch return err } - if info.IsDir() { + if !info.IsDir() { return nil } p1 := strings.TrimPrefix(path, "/"+tt.bucket+"/") if strings.HasPrefix(p1, *params.Prefix) { - prefixes = append(prefixes, s3types.CommonPrefix{Prefix: &p1}) + prefix := strings.TrimPrefix(p1, *params.Prefix) + prefixes = append(prefixes, s3types.CommonPrefix{Prefix: &prefix}) } return nil }) @@ -324,14 +325,14 @@ https://docs.example.com/test/b/mybranch } var contents []s3types.Object - _ = s3Fs.Walk(bucketDir, func(path string, info os.FileInfo, err error) error { + _ = s3Fs.Walk(filepath.Join(bucketDir, *params.Prefix), func(path string, info os.FileInfo, err error) error { if err != nil { return nil } - relPath, _ := filepath.Rel(bucketDir, path) - if !info.IsDir() && strings.HasPrefix(relPath, prefix) { - contents = append(contents, s3types.Object{Key: &relPath}) + if !info.IsDir() { + p := strings.TrimPrefix(path, bucketDir+"/") + contents = append(contents, s3types.Object{Key: &p}) } return nil