Skip to content

Commit

Permalink
repository.DateOfLastUpdate = date of latest build's update (or nil)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Oct 16, 2024
1 parent 696dd83 commit 88b7f3f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
3 changes: 2 additions & 1 deletion backend/internal/data/repo_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package data
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
"log/slog"

"github.com/jmoiron/sqlx"
)

type Repo struct {
Expand Down
58 changes: 56 additions & 2 deletions backend/internal/server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}

Expand Down
21 changes: 3 additions & 18 deletions backend/internal/server/api/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package api

import (
"strconv"
"time"

"github.com/bee-ci/bee-ci-system/internal/data"
)

type getMyRepositoriesParams struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 88b7f3f

Please sign in to comment.