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
34 changes: 13 additions & 21 deletions cli/pkg/release/providers/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
45 changes: 23 additions & 22 deletions cli/pkg/release/providers/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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,
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
})
Expand All @@ -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
Expand Down
Loading