api: replace generated Created/Updated timestamps with commit properties

This commit is contained in:
mappu 2020-11-19 11:04:48 +13:00
parent 818a93de1b
commit b21cd5585d
1 changed files with 44 additions and 8 deletions

52
api.go
View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
@ -189,21 +190,56 @@ func (this *Application) repos(ctx context.Context) ([]Repo, error) {
}
if len(page) == 0 && len(ret) > 0 {
return ret, nil // Found enough already
break // Found enough already
}
ret = append(ret, page...)
nextPage += 1
}
// The Created/Modified times aren't very good
// Replace them with the earliest/latest commit dates we can find
for i, rr := range ret {
// The most recent commit will be the head of one of the branches (easy to find)
brs, err := this.branches(ctx, rr.Name)
if err != nil {
log.Printf("loading branches for '%s': %s", rr.Name, err)
continue
}
newestCommit := time.Unix(0, 0) // sentinel
for _, br := range brs {
if br.Commit.Timestamp.After(newestCommit) {
newestCommit = br.Commit.Timestamp
}
}
if !newestCommit.Equal(time.Unix(0, 0)) {
ret[i].UpdatedAt = newestCommit // replace it
}
}
// Separate loop for oldest-commits, in case we needed to continue/break out
// of the earliest-commit loop
for i, rr := range ret {
// The oldest commit needs us to page through the commit history to find it
oldestCommit, err := this.oldestCommit(ctx, rr.Name, "")
if err != nil {
log.Printf("finding oldest commit for '%s': %s", rr.Name, err)
continue
}
ret[i].CreatedAt = oldestCommit.Commit.Author.Date
}
return ret, nil
}
}
}
// repoFile gets a single file from the default branch of the git repository
// Usually the default branch is `master`.
func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) {