feat: Add summary card for repos and releases

This commit is contained in:
JakobDev 2024-12-14 19:36:09 +01:00
parent f5cfdd80a7
commit 7685a1e98e
No known key found for this signature in database
GPG key ID: 39DEF62C3ED6DC4C
11 changed files with 673 additions and 247 deletions

View file

@ -97,13 +97,11 @@ func init() {
// LoadAttributes load repo and publisher attributes for a release
func (r *Release) LoadAttributes(ctx context.Context) error {
var err error
if r.Repo == nil {
r.Repo, err = GetRepositoryByID(ctx, r.RepoID)
if err != nil {
return err
}
err := r.LoadRepo(ctx)
if err != nil {
return err
}
if r.Publisher == nil {
r.Publisher, err = user_model.GetUserByID(ctx, r.PublisherID)
if err != nil {
@ -123,6 +121,18 @@ func (r *Release) LoadAttributes(ctx context.Context) error {
return GetReleaseAttachments(ctx, r)
}
// LoadRepo load repo attribute for release
func (r *Release) LoadRepo(ctx context.Context) error {
if r.Repo != nil {
return nil
}
var err error
r.Repo, err = GetRepositoryByID(ctx, r.RepoID)
return err
}
// LoadArchiveDownloadCount loads the download count for the source archives
func (r *Release) LoadArchiveDownloadCount(ctx context.Context) error {
var err error
@ -130,6 +140,25 @@ func (r *Release) LoadArchiveDownloadCount(ctx context.Context) error {
return err
}
// GetTotalDownloadCount returns the summary of all dowlaod count of files attached to the release
func (r *Release) GetTotalDownloadCount(ctx context.Context) (int64, error) {
var archiveCount int64
if !r.HideArchiveLinks {
_, err := db.GetEngine(ctx).SQL("SELECT SUM(count) FROM repo_archive_download_count WHERE release_id = ?", r.ID).Get(&archiveCount)
if err != nil {
return 0, err
}
}
var attachmentCount int64
_, err := db.GetEngine(ctx).SQL("SELECT SUM(download_count) FROM attachment WHERE release_id = ?", r.ID).Get(&attachmentCount)
if err != nil {
return 0, err
}
return archiveCount + attachmentCount, nil
}
// APIURL the api url for a release. release must have attributes loaded
func (r *Release) APIURL() string {
return r.Repo.APIURL() + "/releases/" + strconv.FormatInt(r.ID, 10)
@ -160,6 +189,20 @@ func (r *Release) Link() string {
return r.Repo.Link() + "/releases/tag/" + util.PathEscapeSegments(r.TagName)
}
// SummaryCardURL returns the absolute URL to an image providing a summary of the release
func (r *Release) SummaryCardURL() string {
return fmt.Sprintf("%s/releases/summary-card/%d", r.Repo.HTMLURL(), r.ID)
}
// DisplayName retruns the name of the release
func (r *Release) DisplayName() string {
if r.IsTag && r.Title == "" {
return r.TagName
}
return r.Title
}
// IsReleaseExist returns true if release with given tag name already exists.
func IsReleaseExist(ctx context.Context, repoID int64, tagName string) (bool, error) {
if len(tagName) == 0 {

View file

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -25,3 +26,26 @@ func TestMigrate_InsertReleases(t *testing.T) {
err := InsertReleases(db.DefaultContext, r)
require.NoError(t, err)
}
func TestReleaseLoadRepo(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
release := unittest.AssertExistsAndLoadBean(t, &Release{ID: 1})
assert.Nil(t, release.Repo)
require.NoError(t, release.LoadRepo(db.DefaultContext))
assert.Equal(t, int64(1), release.Repo.ID)
}
func TestReleaseDisplayName(t *testing.T) {
release := Release{TagName: "TagName"}
assert.Empty(t, release.DisplayName())
release.IsTag = true
assert.Equal(t, "TagName", release.DisplayName())
release.Title = "Title"
assert.Equal(t, "Title", release.DisplayName())
}

View file

@ -327,6 +327,11 @@ func (repo *Repository) HTMLURL() string {
return setting.AppURL + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
}
// SummaryCardURL returns the absolute URL to an image providing a summary of the repo
func (repo *Repository) SummaryCardURL() string {
return fmt.Sprintf("%s/-/summary-card", repo.HTMLURL())
}
// CommitLink make link to by commit full ID
// note: won't check whether it's an right id
func (repo *Repository) CommitLink(commitID string) (result string) {