Skip to content

Commit bf8dacb

Browse files
article-creator: use file modification time for commit timestamps
1 parent 2a89bcc commit bf8dacb

File tree

1 file changed

+20
-7
lines changed
  • custom/services/article-creator

1 file changed

+20
-7
lines changed

custom/services/article-creator/main.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ func (c *giteaClient) processFile(filePath, username string, public bool) bool {
250250

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

253+
// Get file info for modification time
254+
fileInfo, err := os.Stat(filePath)
255+
if err != nil {
256+
fmt.Printf(" ✗ Failed to stat file: %v\n", err)
257+
c.stats.failed++
258+
return false
259+
}
260+
253261
// Read file content
254262
content, err := os.ReadFile(filePath)
255263
if err != nil {
@@ -273,6 +281,10 @@ func (c *giteaClient) processFile(filePath, username string, public bool) bool {
273281
fmt.Printf(" No YAML title found, using filename as description\n")
274282
}
275283

284+
// Use file modification time as the commit timestamp.
285+
// This reflects when the article was fetched/written to disk.
286+
fileModTime := fileInfo.ModTime()
287+
276288
// Create repository slug
277289
repoName := createSlug(filepath.Base(filePath))
278290
fmt.Printf(" Repository name: %s\n", repoName)
@@ -292,8 +304,8 @@ func (c *giteaClient) processFile(filePath, username string, public bool) bool {
292304
return false
293305
}
294306

295-
// Create README.md file
296-
if err := c.createReadmeFile(username, repoName, string(content)); err != nil {
307+
// Create README.md file with file modification time as commit timestamp
308+
if err := c.createReadmeFile(username, repoName, string(content), fileModTime); err != nil {
297309
fmt.Printf(" ✗ Failed to create README.md: %v\n", err)
298310
c.stats.failed++
299311
return false
@@ -368,19 +380,20 @@ func (c *giteaClient) createRepository(repoName, description, subject string, pu
368380
return repo.HTMLURL, nil
369381
}
370382

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

374-
// Set commit timestamp to current time in RFC3339 format
375-
now := time.Now().Format(time.RFC3339)
388+
commitTimeStr := commitTime.Format(time.RFC3339)
376389

377390
reqData := createFileRequest{
378391
Message: "Import article from Wikipedia",
379392
Content: contentB64,
380393
Branch: "main",
381394
Dates: commitDateOptions{
382-
Author: now,
383-
Committer: now,
395+
Author: commitTimeStr,
396+
Committer: commitTimeStr,
384397
},
385398
}
386399

0 commit comments

Comments
 (0)