1
1
mirror of https://github.com/go-gitea/gitea synced 2025-10-31 19:38:23 +00:00

Merge two functions with the same content

This commit is contained in:
Lunny Xiao
2024-12-11 22:15:47 -08:00
parent dfd7594499
commit 82ca99db2d
6 changed files with 38 additions and 29 deletions

View File

@@ -21,15 +21,17 @@ func GetRepositoriesByForkID(ctx context.Context, forkID int64) ([]*Repository,
}
// GetForkedRepo checks if given user has already forked a repository with given ID.
func GetForkedRepo(ctx context.Context, ownerID, repoID int64) *Repository {
func GetForkedRepo(ctx context.Context, ownerID, repoID int64) (*Repository, error) {
repo := new(Repository)
has, _ := db.GetEngine(ctx).
has, err := db.GetEngine(ctx).
Where("owner_id=? AND fork_id=?", ownerID, repoID).
Get(repo)
if has {
return repo
if err != nil {
return nil, err
} else if has {
return nil, ErrRepoNotExist{ID: repoID}
}
return nil
return repo, nil
}
// HasForkedRepo checks if given user has already forked a repository with given ID.
@@ -41,19 +43,6 @@ func HasForkedRepo(ctx context.Context, ownerID, repoID int64) bool {
return has
}
// GetUserFork return user forked repository from this repository, if not forked return nil
func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) {
var forkedRepo Repository
has, err := db.GetEngine(ctx).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return &forkedRepo, nil
}
// IncrementRepoForkNum increment repository fork number
func IncrementRepoForkNum(ctx context.Context, repoID int64) error {
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", repoID)
@@ -87,8 +76,8 @@ func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *Rep
if user == nil {
return repoList, nil
}
forkedRepo, err := GetUserFork(ctx, repo.ID, user.ID)
if err != nil {
forkedRepo, err := GetForkedRepo(ctx, repo.ID, user.ID)
if err != nil && !IsErrRepoNotExist(err) {
return repoList, err
}
if forkedRepo != nil {