Skip to content

Commit b32b1e5

Browse files
committed
update latest codes
2 parents 7dc7c49 + 53e168a commit b32b1e5

File tree

3 files changed

+169
-47
lines changed

3 files changed

+169
-47
lines changed

cmd/git/update_org_status.go

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,20 @@ func updateLocalFile(orgName, sectionContent string) error {
142142
outputPath = ".github/README.md"
143143
}
144144

145-
// Ensure directory exists
146-
dir := filepath.Dir(outputPath)
147-
if err := os.MkdirAll(dir, 0755); err != nil {
148-
return fmt.Errorf("failed to create directory %s: %w", dir, err)
145+
// Update the specified output file
146+
if err := writeReadmeFile(outputPath, orgName, sectionContent); err != nil {
147+
return err
149148
}
150149

151-
// Read existing content or create new
152-
var newContent string
153-
if _, err := os.Stat(outputPath); err == nil {
154-
// File exists, update section
155-
existingContent, err := os.ReadFile(outputPath)
156-
if err != nil {
157-
return fmt.Errorf("failed to read existing file: %w", err)
150+
// Also update profile/README.md if output is under a .github directory
151+
outputDir := filepath.Dir(outputPath)
152+
if filepath.Base(outputDir) == ".github" {
153+
profilePath := filepath.Join(outputDir, "profile", "README.md")
154+
if err := writeReadmeFile(profilePath, orgName, sectionContent); err != nil {
155+
return err
158156
}
159-
newContent = updateSectionInContent(string(existingContent), sectionContent, updateOrgStatusSection)
160-
} else {
161-
// File doesn't exist, create full README
162-
newContent = generateFullREADME(orgName, sectionContent)
163157
}
164158

165-
// Write file
166-
if err := os.WriteFile(outputPath, []byte(newContent), 0644); err != nil {
167-
return fmt.Errorf("failed to write README: %w", err)
168-
}
169-
170-
fmt.Printf("Updated: %s\n", outputPath)
171-
172159
// Git operations (unless skipped)
173160
if !updateOrgStatusSkipPush {
174161
if err := gitCommitAndPush(outputPath, orgName); err != nil {
@@ -198,36 +185,18 @@ func updateDotGitHubRepo(orgName, sectionContent string) error {
198185
return fmt.Errorf("failed to clone .github repository: %w\nNote: Make sure the .github repository exists and is accessible", err)
199186
}
200187

201-
// Determine README path (profile/README.md or README.md)
202-
readmePath := filepath.Join(repoDir, "profile", "README.md")
203-
if _, err := os.Stat(readmePath); os.IsNotExist(err) {
204-
readmePath = filepath.Join(repoDir, "README.md")
188+
// Update both README.md and profile/README.md
189+
paths := []string{
190+
filepath.Join(repoDir, "README.md"),
191+
filepath.Join(repoDir, "profile", "README.md"),
205192
}
206193

207-
// Read existing content or create new
208-
var newContent string
209-
if _, err := os.Stat(readmePath); err == nil {
210-
existingContent, err := os.ReadFile(readmePath)
211-
if err != nil {
212-
return fmt.Errorf("failed to read existing README: %w", err)
194+
for _, readmePath := range paths {
195+
if err := writeReadmeFile(readmePath, orgName, sectionContent); err != nil {
196+
return err
213197
}
214-
newContent = updateSectionInContent(string(existingContent), sectionContent, updateOrgStatusSection)
215-
} else {
216-
newContent = generateFullREADME(orgName, sectionContent)
217-
}
218-
219-
// Ensure directory exists
220-
if err := os.MkdirAll(filepath.Dir(readmePath), 0755); err != nil {
221-
return fmt.Errorf("failed to create directory: %w", err)
222198
}
223199

224-
// Write file
225-
if err := os.WriteFile(readmePath, []byte(newContent), 0644); err != nil {
226-
return fmt.Errorf("failed to write README: %w", err)
227-
}
228-
229-
fmt.Printf("Updated: %s\n", readmePath)
230-
231200
// Git operations in .github repo
232201
if updateOrgStatusSkipPush {
233202
fmt.Println("Skipping git push (--skip-push used)")
@@ -273,6 +242,31 @@ func updateDotGitHubRepo(orgName, sectionContent string) error {
273242
return nil
274243
}
275244

245+
func writeReadmeFile(readmePath, orgName, sectionContent string) error {
246+
// Ensure directory exists
247+
if err := os.MkdirAll(filepath.Dir(readmePath), 0755); err != nil {
248+
return fmt.Errorf("failed to create directory: %w", err)
249+
}
250+
251+
var newContent string
252+
if _, err := os.Stat(readmePath); err == nil {
253+
existingContent, err := os.ReadFile(readmePath)
254+
if err != nil {
255+
return fmt.Errorf("failed to read existing README: %w", err)
256+
}
257+
newContent = updateSectionInContent(string(existingContent), sectionContent, updateOrgStatusSection)
258+
} else {
259+
newContent = generateFullREADME(orgName, sectionContent)
260+
}
261+
262+
if err := os.WriteFile(readmePath, []byte(newContent), 0644); err != nil {
263+
return fmt.Errorf("failed to write README: %w", err)
264+
}
265+
266+
fmt.Printf("Updated: %s\n", readmePath)
267+
return nil
268+
}
269+
276270
func updateSectionInContent(content, newSection, sectionName string) string {
277271
sectionHeader := "## " + sectionName
278272

cmd/git/update_org_status_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package git
22

33
import (
4+
"os"
5+
"path/filepath"
46
"strings"
57
"testing"
68

@@ -258,3 +260,119 @@ func TestSectionNameCustom(t *testing.T) {
258260
// Reset to default
259261
updateOrgStatusSection = "Project List"
260262
}
263+
264+
func TestWriteReadmeFile(t *testing.T) {
265+
updateOrgStatusSection = "Project List"
266+
sectionContent := "## Project List\n\nNew project data.\n"
267+
268+
t.Run("creates new file when not exists", func(t *testing.T) {
269+
tmpDir := t.TempDir()
270+
readmePath := filepath.Join(tmpDir, "README.md")
271+
272+
err := writeReadmeFile(readmePath, "testorg", sectionContent)
273+
if err != nil {
274+
t.Fatalf("writeReadmeFile failed: %v", err)
275+
}
276+
277+
content, err := os.ReadFile(readmePath)
278+
if err != nil {
279+
t.Fatalf("failed to read created file: %v", err)
280+
}
281+
282+
if !strings.Contains(string(content), "# Testorg") {
283+
t.Error("Should contain generated title for new file")
284+
}
285+
if !strings.Contains(string(content), "New project data.") {
286+
t.Error("Should contain section content")
287+
}
288+
})
289+
290+
t.Run("updates existing file section", func(t *testing.T) {
291+
tmpDir := t.TempDir()
292+
readmePath := filepath.Join(tmpDir, "README.md")
293+
294+
existing := "# Test Org\n\n## Project List\nOld data.\n\n## Other\nOther section.\n"
295+
os.WriteFile(readmePath, []byte(existing), 0644)
296+
297+
err := writeReadmeFile(readmePath, "testorg", sectionContent)
298+
if err != nil {
299+
t.Fatalf("writeReadmeFile failed: %v", err)
300+
}
301+
302+
content, err := os.ReadFile(readmePath)
303+
if err != nil {
304+
t.Fatalf("failed to read updated file: %v", err)
305+
}
306+
307+
result := string(content)
308+
if strings.Contains(result, "Old data.") {
309+
t.Error("Should replace old section content")
310+
}
311+
if !strings.Contains(result, "New project data.") {
312+
t.Error("Should contain new section content")
313+
}
314+
if !strings.Contains(result, "## Other") {
315+
t.Error("Should preserve other sections")
316+
}
317+
})
318+
319+
t.Run("creates directory if not exists", func(t *testing.T) {
320+
tmpDir := t.TempDir()
321+
readmePath := filepath.Join(tmpDir, "profile", "README.md")
322+
323+
err := writeReadmeFile(readmePath, "testorg", sectionContent)
324+
if err != nil {
325+
t.Fatalf("writeReadmeFile failed: %v", err)
326+
}
327+
328+
if _, err := os.Stat(readmePath); os.IsNotExist(err) {
329+
t.Error("profile/README.md should be created")
330+
}
331+
})
332+
}
333+
334+
func TestUpdateLocalFileUpdatesBothPaths(t *testing.T) {
335+
updateOrgStatusSection = "Project List"
336+
updateOrgStatusSkipPush = true
337+
338+
tmpDir := t.TempDir()
339+
340+
// Create .github directory structure
341+
githubDir := filepath.Join(tmpDir, ".github")
342+
profileDir := filepath.Join(githubDir, "profile")
343+
os.MkdirAll(profileDir, 0755)
344+
345+
// Write existing files
346+
readmeContent := "# Test\n\n## Project List\nOld.\n"
347+
os.WriteFile(filepath.Join(githubDir, "README.md"), []byte(readmeContent), 0644)
348+
os.WriteFile(filepath.Join(profileDir, "README.md"), []byte(readmeContent), 0644)
349+
350+
// Override output to use tmpDir
351+
oldOutput := updateOrgStatusOutput
352+
updateOrgStatusOutput = filepath.Join(githubDir, "README.md")
353+
defer func() { updateOrgStatusOutput = oldOutput }()
354+
355+
sectionContent := "## Project List\n\nUpdated data.\n"
356+
err := updateLocalFile("testorg", sectionContent)
357+
if err != nil {
358+
t.Fatalf("updateLocalFile failed: %v", err)
359+
}
360+
361+
// Check README.md is updated
362+
mainReadme, err := os.ReadFile(filepath.Join(githubDir, "README.md"))
363+
if err != nil {
364+
t.Fatalf("failed to read README.md: %v", err)
365+
}
366+
if !strings.Contains(string(mainReadme), "Updated data.") {
367+
t.Error("README.md should be updated with new section content")
368+
}
369+
370+
// Check profile/README.md is also updated
371+
profileReadme, err := os.ReadFile(filepath.Join(profileDir, "README.md"))
372+
if err != nil {
373+
t.Fatalf("failed to read profile/README.md: %v", err)
374+
}
375+
if !strings.Contains(string(profileReadme), "Updated data.") {
376+
t.Error("profile/README.md should also be updated with new section content")
377+
}
378+
}

tasks/features/git/git-org.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Git Org Command
2+
3+
## Task 1: Update Org Status Profiles
4+
5+
1. 目前的问题是:如何更新组织的状态配置文件更新README.md文件
6+
2. 需要同时更新README.md文件和profile目录下面的README.md 文件
7+
8+
verification:
9+
1. 检查README.md文件是否更新
10+
2. 检查profile目录下面的README.md 文件是否更新

0 commit comments

Comments
 (0)