Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-12-01 06:16:09 +01:00
forgejo/models/repo/archive_download_count.go
Yaroslav Halchenko 2b2fd2728c Add codespell support and fix a good number of typos with its help (#3270)
More about codespell: https://github.com/codespell-project/codespell .

I personally introduced it to dozens if not hundreds of projects already and so far only positive feedback.

```
❯ grep lint-spell Makefile
	@echo " - lint-spell                       lint spelling"
	@echo " - lint-spell-fix                   lint spelling and fix issues"
lint: lint-frontend lint-backend lint-spell
lint-fix: lint-frontend-fix lint-backend-fix lint-spell-fix
.PHONY: lint-spell
lint-spell: lint-codespell
.PHONY: lint-spell-fix
lint-spell-fix: lint-codespell-fix
❯ git grep lint- -- .forgejo/
.forgejo/workflows/testing.yml:      - run: make --always-make -j$(nproc) lint-backend checks-backend # ensure the "go-licenses" make target runs
.forgejo/workflows/testing.yml:      - run: make lint-frontend
```
so how would you like me to invoke `lint-codespell` on CI? (without that would be IMHO very suboptimal and let typos sneak in)

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3270
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Yaroslav Halchenko <debian@onerussian.com>
Co-committed-by: Yaroslav Halchenko <debian@onerussian.com>
2024-05-09 13:49:37 +00:00

91 lines
2.7 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"context"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
)
// RepoArchiveDownloadCount counts all archive downloads for a tag
type RepoArchiveDownloadCount struct { //nolint:revive
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"index unique(s)"`
ReleaseID int64 `xorm:"index unique(s)"`
Type git.ArchiveType `xorm:"unique(s)"`
Count int64
}
func init() {
db.RegisterModel(new(RepoArchiveDownloadCount))
}
// CountArchiveDownload adds one download the the given archive
func CountArchiveDownload(ctx context.Context, repoID, releaseID int64, tp git.ArchiveType) error {
updateCount, err := db.GetEngine(ctx).Where("repo_id = ?", repoID).And("release_id = ?", releaseID).And("`type` = ?", tp).Incr("count").Update(new(RepoArchiveDownloadCount))
if err != nil {
return err
}
if updateCount != 0 {
// The count was updated, so we can exit
return nil
}
// The archive does not esxists in the database, so let's add it
newCounter := &RepoArchiveDownloadCount{
RepoID: repoID,
ReleaseID: releaseID,
Type: tp,
Count: 1,
}
_, err = db.GetEngine(ctx).Insert(newCounter)
return err
}
// GetArchiveDownloadCount returns the download count of a tag
func GetArchiveDownloadCount(ctx context.Context, repoID, releaseID int64) (*api.TagArchiveDownloadCount, error) {
downloadCountList := make([]RepoArchiveDownloadCount, 0)
err := db.GetEngine(ctx).Where("repo_id = ?", repoID).And("release_id = ?", releaseID).Find(&downloadCountList)
if err != nil {
return nil, err
}
tagCounter := new(api.TagArchiveDownloadCount)
for _, singleCount := range downloadCountList {
switch singleCount.Type {
case git.ZIP:
tagCounter.Zip = singleCount.Count
case git.TARGZ:
tagCounter.TarGz = singleCount.Count
}
}
return tagCounter, nil
}
// GetDownloadCountForTagName returns the download count of a tag with the given name
func GetArchiveDownloadCountForTagName(ctx context.Context, repoID int64, tagName string) (*api.TagArchiveDownloadCount, error) {
release, err := GetRelease(ctx, repoID, tagName)
if err != nil {
if IsErrReleaseNotExist(err) {
return new(api.TagArchiveDownloadCount), nil
}
return nil, err
}
return GetArchiveDownloadCount(ctx, repoID, release.ID)
}
// DeleteArchiveDownloadCountForRelease deletes the release from the repo_archive_download_count table
func DeleteArchiveDownloadCountForRelease(ctx context.Context, releaseID int64) error {
_, err := db.GetEngine(ctx).Delete(&RepoArchiveDownloadCount{ReleaseID: releaseID})
return err
}