From d32879a32154bff1a7e613ba7c1e3229c01e0889 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 5 Apr 2023 16:57:19 -0400 Subject: [PATCH] fix(ui): show repo update time for new repos --- server/backend/repo.go | 5 +++++ server/backend/sqlite/repo.go | 21 +++++++++++++++++++++ ui/pages/selection/item.go | 8 ++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/server/backend/repo.go b/server/backend/repo.go index 88a25564b..7172d71ee 100644 --- a/server/backend/repo.go +++ b/server/backend/repo.go @@ -1,6 +1,8 @@ package backend import ( + "time" + "github.com/charmbracelet/soft-serve/git" ) @@ -78,6 +80,9 @@ type Repository interface { IsMirror() bool // IsHidden returns whether the repository is hidden. IsHidden() bool + // UpdatedAt returns the time the repository was last updated. + // If the repository has never been updated, it returns the time it was created. + UpdatedAt() time.Time // Open returns the underlying git.Repository. Open() (*git.Repository, error) } diff --git a/server/backend/sqlite/repo.go b/server/backend/sqlite/repo.go index 9259f8bea..6b90ca92c 100644 --- a/server/backend/sqlite/repo.go +++ b/server/backend/sqlite/repo.go @@ -2,6 +2,7 @@ package sqlite import ( "context" + "time" "github.com/charmbracelet/soft-serve/git" "github.com/charmbracelet/soft-serve/server/backend" @@ -100,3 +101,23 @@ func (r *Repo) IsHidden() bool { return hidden } + +// UpdatedAt returns the repository's last update time. +func (r *Repo) UpdatedAt() time.Time { + rr, err := git.Open(r.path) + if err == nil { + t, err := rr.LatestCommitTime() + if err == nil { + return t + } + } + + var updatedAt time.Time + if err := wrapTx(r.db, context.Background(), func(tx *sqlx.Tx) error { + return tx.Get(&updatedAt, "SELECT updated_at FROM repo WHERE name = ?", r.name) + }); err != nil { + return time.Time{} + } + + return updatedAt +} diff --git a/ui/pages/selection/item.go b/ui/pages/selection/item.go index 79c5b6346..837ce69d3 100644 --- a/ui/pages/selection/item.go +++ b/ui/pages/selection/item.go @@ -56,13 +56,9 @@ type Item struct { // New creates a new Item. func NewItem(repo backend.Repository, cfg *config.Config) (Item, error) { - r, err := repo.Open() - if err != nil { - return Item{}, err - } var lastUpdate *time.Time - lu, err := r.LatestCommitTime() - if err == nil { + lu := repo.UpdatedAt() + if !lu.IsZero() { lastUpdate = &lu } return Item{