[v10.0/forgejo] feat: Add summary card for repos and releases

This commit is contained in:
JakobDev 2025-01-01 22:00:26 +01:00
parent 9e6c3f226c
commit 13496203bc
No known key found for this signature in database
GPG key ID: 39DEF62C3ED6DC4C
13 changed files with 735 additions and 282 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 dowload 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/%s", r.Repo.HTMLURL(), util.PathEscapeSegments(r.TagName))
}
// 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 {