Skip to content
Merged
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
35 changes: 26 additions & 9 deletions custom/services/article-creator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,23 @@ func (c *giteaClient) processFile(filePath, username string, public bool) bool {

fmt.Printf("\nProcessing: %s\n", filepath.Base(filePath))

// Read file content
content, err := os.ReadFile(filePath)
// Open file once to get both content and modification time atomically.
f, err := os.Open(filePath)
if err != nil {
fmt.Printf(" ✗ Failed to open file: %v\n", err)
c.stats.failed++
return false
}
defer f.Close()

fileInfo, err := f.Stat()
if err != nil {
fmt.Printf(" ✗ Failed to stat file: %v\n", err)
c.stats.failed++
return false
}

content, err := io.ReadAll(f)
if err != nil {
fmt.Printf(" ✗ Failed to read file: %v\n", err)
c.stats.failed++
Expand Down Expand Up @@ -292,8 +307,9 @@ func (c *giteaClient) processFile(filePath, username string, public bool) bool {
return false
}

// Create README.md file
if err := c.createReadmeFile(username, repoName, string(content)); err != nil {
// Create README.md file with file modification time as commit timestamp.
// This reflects when the article was fetched/written to disk.
if err := c.createReadmeFile(username, repoName, string(content), fileInfo.ModTime()); err != nil {
fmt.Printf(" ✗ Failed to create README.md: %v\n", err)
c.stats.failed++
return false
Expand Down Expand Up @@ -368,19 +384,20 @@ func (c *giteaClient) createRepository(repoName, description, subject string, pu
return repo.HTMLURL, nil
}

func (c *giteaClient) createReadmeFile(username, repoName, content string) error {
// createReadmeFile creates the README.md file in the repository.
// commitTime is the timestamp to use for the commit (typically the file's modification time).
func (c *giteaClient) createReadmeFile(username, repoName, content string, commitTime time.Time) error {
contentB64 := base64.StdEncoding.EncodeToString([]byte(content))

// Set commit timestamp to current time in RFC3339 format
now := time.Now().Format(time.RFC3339)
commitTimeStr := commitTime.Format(time.RFC3339)

reqData := createFileRequest{
Message: "Import article from Wikipedia",
Content: contentB64,
Branch: "main",
Dates: commitDateOptions{
Author: now,
Committer: now,
Author: commitTimeStr,
Committer: commitTimeStr,
},
}

Expand Down
Loading