Skip to content

Commit d7c5af0

Browse files
authored
refactor(cli): always use branches for docs (#178)
1 parent f559866 commit d7c5af0

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

cli/pkg/release/providers/docs.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r *DocsReleaser) Release() error {
8989
}
9090

9191
r.logger.Info("Cleaning existing docs from S3", "bucket", docsConfig.Bucket, "path", s3Path)
92-
if err := r.s3.DeleteDirectory(docsConfig.Bucket, s3Path, []string{".*/b/.*"}); err != nil {
92+
if err := r.s3.DeleteDirectory(docsConfig.Bucket, s3Path, nil); err != nil {
9393
return fmt.Errorf("failed to clean existing docs from S3: %w", err)
9494
}
9595

@@ -100,6 +100,7 @@ func (r *DocsReleaser) Release() error {
100100
}
101101

102102
if github.InCI() {
103+
r.logger.Info("Posting comment", "url", docsConfig.Url, "project", projectName)
103104
url := r.project.Blueprint.Global.Ci.Release.Docs.Url
104105
if err := r.postComment(url, projectName); err != nil {
105106
return fmt.Errorf("failed to post comment: %w", err)
@@ -111,11 +112,11 @@ func (r *DocsReleaser) Release() error {
111112
}
112113

113114
if isDefault {
114-
if err := r.cleanupBranches(docsConfig.Bucket, filepath.Join(s3Path, "b")); err != nil {
115+
r.logger.Info("Cleaning up branches from S3", "bucket", docsConfig.Bucket, "path", filepath.Dir(s3Path))
116+
if err := r.cleanupBranches(docsConfig.Bucket, filepath.Dir(s3Path)); err != nil {
115117
return fmt.Errorf("failed to cleanup branches: %w", err)
116118
}
117119
}
118-
119120
}
120121

121122
r.logger.Info("Docs release complete")
@@ -133,11 +134,13 @@ func (r *DocsReleaser) cleanupBranches(bucket, path string) error {
133134
for _, branch := range branches {
134135
branchNames = append(branchNames, branch.Name)
135136
}
137+
r.logger.Info("Repo branches", "branches", branchNames)
136138

137139
children, err := r.s3.ListImmediateChildren(bucket, path)
138140
if err != nil {
139141
return fmt.Errorf("failed to list immediate children: %w", err)
140142
}
143+
r.logger.Info("Docs branches", "branches", children)
141144

142145
for _, child := range children {
143146
if !slices.Contains(branchNames, child) {
@@ -154,25 +157,19 @@ func (r *DocsReleaser) cleanupBranches(bucket, path string) error {
154157
// generatePath generates the S3 path for the docs.
155158
func (r *DocsReleaser) generatePath(projectName string) (string, error) {
156159
docsConfig := r.project.Blueprint.Global.Ci.Release.Docs
157-
if docsConfig.Bucket == "" {
158-
return "", fmt.Errorf("no S3 bucket specified in global docs configuration")
159-
}
160-
161-
s3Path := projectName
162-
if docsConfig.Path != "" {
163-
s3Path = filepath.Join(docsConfig.Path, projectName)
164-
}
165-
166160
branch, err := git.GetBranch(r.ghClient, r.project.Repo)
167161
if err != nil {
168162
return "", fmt.Errorf("failed to get branch: %w", err)
169163
}
170164

171-
if branch == r.project.Blueprint.Global.Repo.DefaultBranch {
172-
return s3Path, nil
165+
var s3Path string
166+
if docsConfig.Path != "" {
167+
s3Path = filepath.Join(docsConfig.Path, projectName, branch)
168+
} else {
169+
s3Path = filepath.Join(projectName, branch)
173170
}
174171

175-
return filepath.Join(s3Path, "b", branch), nil
172+
return s3Path, nil
176173
}
177174

178175
// isDefaultBranch returns true if the current branch is the default branch.
@@ -211,12 +208,7 @@ func (r *DocsReleaser) postComment(baseURL, name string) error {
211208
return fmt.Errorf("failed to get branch: %w", err)
212209
}
213210

214-
var docURL string
215-
if branch == r.project.Blueprint.Global.Repo.DefaultBranch {
216-
docURL, err = url.JoinPath(baseURL, name)
217-
} else {
218-
docURL, err = url.JoinPath(baseURL, name, "b", branch)
219-
}
211+
docURL, err := url.JoinPath(baseURL, name, branch)
220212
if err != nil {
221213
return fmt.Errorf("failed to join URL path: %w", err)
222214
}

cli/pkg/release/providers/docs_test.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,29 @@ func TestDocsReleaserRelease(t *testing.T) {
121121
},
122122
},
123123
prComments: []gh.PullRequestComment{},
124-
branches: []gh.Branch{},
125-
inCI: true,
126-
isPR: false,
124+
branches: []gh.Branch{
125+
{
126+
Name: "master",
127+
},
128+
},
129+
inCI: true,
130+
isPR: false,
127131
validate: func(t *testing.T, result testResult) {
128132
assert.NoError(t, result.err)
129133

130-
exists, err := result.s3Fs.Exists("/bucket/prefix/test/index.html")
134+
exists, err := result.s3Fs.Exists("/bucket/prefix/test/master/index.html")
131135
require.NoError(t, err)
132136
assert.True(t, exists)
133137

134-
exists, err = result.s3Fs.Exists("/bucket/prefix/test/test.html")
138+
exists, err = result.s3Fs.Exists("/bucket/prefix/test/master/test.html")
135139
require.NoError(t, err)
136140
assert.False(t, exists)
137141

138-
exists, err = result.s3Fs.Exists("/bucket/prefix/test/b/mybranch/index.html")
142+
exists, err = result.s3Fs.Exists("/bucket/prefix/test/mybranch/index.html")
139143
require.NoError(t, err)
140144
assert.False(t, exists)
141145

142-
content, err := result.s3Fs.ReadFile("/bucket/prefix/test/index.html")
146+
content, err := result.s3Fs.ReadFile("/bucket/prefix/test/master/index.html")
143147
require.NoError(t, err)
144148
assert.Equal(t, "test docs", string(content))
145149

@@ -169,15 +173,15 @@ func TestDocsReleaserRelease(t *testing.T) {
169173
validate: func(t *testing.T, result testResult) {
170174
assert.NoError(t, result.err)
171175

172-
exists, err := result.s3Fs.Exists("/bucket/prefix/test/b/mybranch/index.html")
176+
exists, err := result.s3Fs.Exists("/bucket/prefix/test/mybranch/index.html")
173177
require.NoError(t, err)
174178
assert.True(t, exists)
175179

176-
exists, err = result.s3Fs.Exists("/bucket/prefix/test/b/mybranch/test.html")
180+
exists, err = result.s3Fs.Exists("/bucket/prefix/test/mybranch/test.html")
177181
require.NoError(t, err)
178182
assert.False(t, exists)
179183

180-
content, err := result.s3Fs.ReadFile("/bucket/prefix/test/b/mybranch/index.html")
184+
content, err := result.s3Fs.ReadFile("/bucket/prefix/test/mybranch/index.html")
181185
require.NoError(t, err)
182186
assert.Equal(t, "test docs", string(content))
183187

@@ -187,7 +191,7 @@ func TestDocsReleaserRelease(t *testing.T) {
187191
188192
The docs for this PR can be previewed at the following URL:
189193
190-
https://docs.example.com/test/b/mybranch
194+
https://docs.example.com/test/mybranch
191195
`
192196

193197
assert.Equal(t, expectedBody, result.prPost.body)
@@ -241,6 +245,7 @@ https://docs.example.com/test/b/mybranch
241245
prj.Blueprint.Global.Ci.Release.Docs.Bucket,
242246
prj.Blueprint.Global.Ci.Release.Docs.Path,
243247
tt.releaseName,
248+
tt.curBranch,
244249
name,
245250
)
246251
require.NoError(t, s3Fs.WriteFile(p, []byte(content), 0o644))
@@ -251,7 +256,6 @@ https://docs.example.com/test/b/mybranch
251256
prj.Blueprint.Global.Ci.Release.Docs.Bucket,
252257
prj.Blueprint.Global.Ci.Release.Docs.Path,
253258
tt.releaseName,
254-
"b",
255259
branchFile.branch,
256260
branchFile.name,
257261
)
@@ -294,10 +298,6 @@ https://docs.example.com/test/b/mybranch
294298
},
295299
ListObjectsV2Func: func(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error) {
296300
bucket := *params.Bucket
297-
prefix := ""
298-
if params.Prefix != nil {
299-
prefix = *params.Prefix
300-
}
301301
bucketDir := "/" + bucket
302302

303303
if params.Delimiter != nil {
@@ -308,13 +308,14 @@ https://docs.example.com/test/b/mybranch
308308
return err
309309
}
310310

311-
if info.IsDir() {
311+
if !info.IsDir() {
312312
return nil
313313
}
314314

315315
p1 := strings.TrimPrefix(path, "/"+tt.bucket+"/")
316316
if strings.HasPrefix(p1, *params.Prefix) {
317-
prefixes = append(prefixes, s3types.CommonPrefix{Prefix: &p1})
317+
prefix := strings.TrimPrefix(p1, *params.Prefix)
318+
prefixes = append(prefixes, s3types.CommonPrefix{Prefix: &prefix})
318319
}
319320
return nil
320321
})
@@ -324,14 +325,14 @@ https://docs.example.com/test/b/mybranch
324325
}
325326

326327
var contents []s3types.Object
327-
_ = s3Fs.Walk(bucketDir, func(path string, info os.FileInfo, err error) error {
328+
_ = s3Fs.Walk(filepath.Join(bucketDir, *params.Prefix), func(path string, info os.FileInfo, err error) error {
328329
if err != nil {
329330
return nil
330331
}
331332

332-
relPath, _ := filepath.Rel(bucketDir, path)
333-
if !info.IsDir() && strings.HasPrefix(relPath, prefix) {
334-
contents = append(contents, s3types.Object{Key: &relPath})
333+
if !info.IsDir() {
334+
p := strings.TrimPrefix(path, bucketDir+"/")
335+
contents = append(contents, s3types.Object{Key: &p})
335336
}
336337

337338
return nil

0 commit comments

Comments
 (0)