diff --git a/backend/internal/data/repo_repo.go b/backend/internal/data/repo_repo.go index d2cc355..c35fad6 100644 --- a/backend/internal/data/repo_repo.go +++ b/backend/internal/data/repo_repo.go @@ -3,8 +3,9 @@ package data import ( "context" "fmt" - "github.com/jmoiron/sqlx" "log/slog" + + "github.com/jmoiron/sqlx" ) type Repo struct { diff --git a/backend/internal/server/api/api.go b/backend/internal/server/api/api.go index 0724d3b..82d48ad 100644 --- a/backend/internal/server/api/api.go +++ b/backend/internal/server/api/api.go @@ -134,8 +134,35 @@ func (a *App) getMyRepositories(w http.ResponseWriter, r *http.Request) { repos = append(repos, allRepos[i]) } + repositories := make([]repository, 0) + for _, repo := range repos { + wasFound := true + latestBuild, err := a.BuildRepo.GetLatestByRepoID(r.Context(), userID, repo.ID) + if err != nil { + if errors.Is(err, data.ErrNotFound) { + wasFound = false + } else { + msg := "failed to get latest build for repo" + logger.Error(msg, slog.Any("error", err)) + http.Error(w, msg, http.StatusInternalServerError) + return + } + } + + var dateOfLastUpdate *time.Time = nil + if wasFound { + dateOfLastUpdate = &latestBuild.UpdatedAt + } + + repositories = append(repositories, repository{ + ID: strconv.FormatInt(repo.ID, 10), + Name: repo.Name, + DateOfLastUpdate: dateOfLastUpdate, + }) + } + response := getMyRepositoriesDTO{ - Repositories: toRepositories(repos), + Repositories: repositories, TotalRepositories: len(allRepos), TotalPages: int(totalPages), CurrentPage: params.CurrentPage, @@ -282,9 +309,36 @@ func (a *App) getDashboard(w http.ResponseWriter, r *http.Request) { pipelines = append(pipelines, pipeline) } + repositories := make([]repository, 0) + for _, repo := range repos { + wasFound := true + latestBuild, err := a.BuildRepo.GetLatestByRepoID(r.Context(), userID, repo.ID) + if err != nil { + if errors.Is(err, data.ErrNotFound) { + wasFound = false + } else { + msg := "failed to get latest build for repo" + logger.Error(msg, slog.Any("error", err)) + http.Error(w, msg, http.StatusInternalServerError) + return + } + } + + var dateOfLastUpdate *time.Time = nil + if wasFound { + dateOfLastUpdate = &latestBuild.UpdatedAt + } + + repositories = append(repositories, repository{ + ID: strconv.FormatInt(repo.ID, 10), + Name: repo.Name, + DateOfLastUpdate: dateOfLastUpdate, + }) + } + response := getDashboardDataDTO{ Stats: stats, - Repositories: toRepositories(repos), + Repositories: repositories, Pipelines: pipelines, } diff --git a/backend/internal/server/api/types.go b/backend/internal/server/api/types.go index e010f34..f92650d 100644 --- a/backend/internal/server/api/types.go +++ b/backend/internal/server/api/types.go @@ -1,10 +1,7 @@ package api import ( - "strconv" "time" - - "github.com/bee-ci/bee-ci-system/internal/data" ) type getMyRepositoriesParams struct { @@ -26,9 +23,9 @@ type getMyRepositoriesDTO struct { } type repository struct { - ID string `json:"id"` - Name string `json:"name"` - DateOfLastUpdate time.Time `json:"dateOfLastUpdate"` + ID string `json:"id"` + Name string `json:"name"` + DateOfLastUpdate *time.Time `json:"dateOfLastUpdate"` } type getDashboardDataDTO struct { @@ -68,15 +65,3 @@ type pipeline struct { StartDate time.Time `json:"startDate"` EndDate *time.Time `json:"endDate"` } - -func toRepositories(dbRepos []data.Repo) []repository { - repos := make([]repository, 0) - for _, repo := range dbRepos { - repos = append(repos, repository{ - ID: strconv.FormatInt(repo.ID, 10), - Name: repo.Name, - DateOfLastUpdate: repo.LatestCommitPushedAt, - }) - } - return repos -}