-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Count downloads for tag archives #25606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JakobDev
wants to merge
26
commits into
go-gitea:main
Choose a base branch
from
JakobDev:archivecount
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
88127fb
Count downloads for tag archives
JakobDev 29facb9
Delete Download Count when Tag is deleted
JakobDev b6cb538
Merge branch 'main' into archivecount
JakobDev 3e53b68
Fix typo
JakobDev ad05135
Add Count to Tags API and add Tests
JakobDev 4582d88
Delete download count when repo is deleted
JakobDev 6fdb31b
Merge branch 'main' into archivecount
JakobDev e1916fd
Merge branch 'main' into archivecount
JakobDev 7aabb71
Merge branch 'main' into archivecount
techknowlogick 1530b15
make fmt
techknowlogick ccb0d3f
fix lint
techknowlogick 36a3d8e
Merge branch 'main' into archivecount
JakobDev fd039c0
Merge branch 'main' into archivecount
JakobDev 8ab3a9b
Run make fmt
JakobDev 443a41c
Merge branch 'main' into archivecount
JakobDev c4d27f1
Merge branch 'main' into archivecount
JakobDev 7317815
Fix Template
JakobDev 1477b6e
Fix compiling
JakobDev 37c99e4
Run make fmt
JakobDev 1e27863
Update
JakobDev 585d1de
Some fixes
JakobDev 2d807ca
Add missing header
JakobDev a6b6dfa
Return not exists error
JakobDev a9195b9
Fix building
JakobDev 300605a
Merge branch 'main' into archivecount
JakobDev 476696c
Merge branch 'main' into archivecount
JakobDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_22 //nolint | ||
|
||
import ( | ||
"xorm.io/xorm" | ||
) | ||
|
||
func AddRepoArchiveDownloadCount(x *xorm.Engine) error { | ||
type RepoArchiveDownloadCount struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"index unique(s)"` | ||
ReleaseID int64 `xorm:"index unique(s)"` | ||
Type int `xorm:"unique(s)"` | ||
Count int64 | ||
} | ||
|
||
return x.Sync(&RepoArchiveDownloadCount{}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 databse, 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 { | ||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
"code.gitea.io/gitea/modules/git" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRepoArchiveDownloadCount(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
release, err := repo_model.GetReleaseByID(db.DefaultContext, 1) | ||
require.NoError(t, err) | ||
|
||
// We have no count, so it should return 0 | ||
downloadCount, err := repo_model.GetArchiveDownloadCount(db.DefaultContext, release.RepoID, release.ID) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(0), downloadCount.Zip) | ||
assert.Equal(t, int64(0), downloadCount.TarGz) | ||
|
||
// Set the TarGz counter to 1 | ||
err = repo_model.CountArchiveDownload(db.DefaultContext, release.RepoID, release.ID, git.TARGZ) | ||
require.NoError(t, err) | ||
|
||
downloadCount, err = repo_model.GetArchiveDownloadCountForTagName(db.DefaultContext, release.RepoID, release.TagName) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(0), downloadCount.Zip) | ||
assert.Equal(t, int64(1), downloadCount.TarGz) | ||
|
||
// Set the TarGz counter to 2 | ||
err = repo_model.CountArchiveDownload(db.DefaultContext, release.RepoID, release.ID, git.TARGZ) | ||
require.NoError(t, err) | ||
|
||
downloadCount, err = repo_model.GetArchiveDownloadCountForTagName(db.DefaultContext, release.RepoID, release.TagName) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(0), downloadCount.Zip) | ||
assert.Equal(t, int64(2), downloadCount.TarGz) | ||
|
||
// Set the Zip counter to 1 | ||
err = repo_model.CountArchiveDownload(db.DefaultContext, release.RepoID, release.ID, git.ZIP) | ||
require.NoError(t, err) | ||
|
||
downloadCount, err = repo_model.GetArchiveDownloadCountForTagName(db.DefaultContext, release.RepoID, release.TagName) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(1), downloadCount.Zip) | ||
assert.Equal(t, int64(2), downloadCount.TarGz) | ||
|
||
// Delete the count | ||
err = repo_model.DeleteArchiveDownloadCountForRelease(db.DefaultContext, release.ID) | ||
require.NoError(t, err) | ||
|
||
downloadCount, err = repo_model.GetArchiveDownloadCountForTagName(db.DefaultContext, release.RepoID, release.TagName) | ||
require.NoError(t, err) | ||
assert.Equal(t, int64(0), downloadCount.Zip) | ||
assert.Equal(t, int64(0), downloadCount.TarGz) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.