2021-12-12 10:48:20 -05:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-12 10:48:20 -05:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 04:01:49 -04:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-06-09 07:15:08 -04:00
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
"xorm.io/builder"
|
2021-12-12 10:48:20 -05:00
|
|
|
)
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
// GetRepositoriesByForkID returns all repositories with given fork ID.
|
|
|
|
func GetRepositoriesByForkID(ctx context.Context, forkID int64) ([]*Repository, error) {
|
2021-12-12 10:48:20 -05:00
|
|
|
repos := make([]*Repository, 0, 10)
|
2022-05-20 10:08:52 -04:00
|
|
|
return repos, db.GetEngine(ctx).
|
2021-12-12 10:48:20 -05:00
|
|
|
Where("fork_id=?", forkID).
|
|
|
|
Find(&repos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetForkedRepo checks if given user has already forked a repository with given ID.
|
2023-09-14 13:09:32 -04:00
|
|
|
func GetForkedRepo(ctx context.Context, ownerID, repoID int64) *Repository {
|
2021-12-12 10:48:20 -05:00
|
|
|
repo := new(Repository)
|
2023-09-14 13:09:32 -04:00
|
|
|
has, _ := db.GetEngine(ctx).
|
2021-12-12 10:48:20 -05:00
|
|
|
Where("owner_id=? AND fork_id=?", ownerID, repoID).
|
|
|
|
Get(repo)
|
|
|
|
if has {
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasForkedRepo checks if given user has already forked a repository with given ID.
|
2023-09-14 13:09:32 -04:00
|
|
|
func HasForkedRepo(ctx context.Context, ownerID, repoID int64) bool {
|
|
|
|
has, _ := db.GetEngine(ctx).
|
2021-12-12 10:48:20 -05:00
|
|
|
Table("repository").
|
|
|
|
Where("owner_id=? AND fork_id=?", ownerID, repoID).
|
|
|
|
Exist()
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserFork return user forked repository from this repository, if not forked return nil
|
2022-03-29 15:13:41 -04:00
|
|
|
func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) {
|
2021-12-12 10:48:20 -05:00
|
|
|
var forkedRepo Repository
|
2022-03-29 15:13:41 -04:00
|
|
|
has, err := db.GetEngine(ctx).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo)
|
2021-12-12 10:48:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &forkedRepo, nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
// 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)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecrementRepoForkNum decrement repository fork number
|
|
|
|
func DecrementRepoForkNum(ctx context.Context, repoID int64) error {
|
|
|
|
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repoID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindUserOrgForks returns the forked repositories for one user from a repository
|
|
|
|
func FindUserOrgForks(ctx context.Context, repoID, userID int64) ([]*Repository, error) {
|
|
|
|
cond := builder.And(
|
|
|
|
builder.Eq{"fork_id": repoID},
|
|
|
|
builder.In("owner_id",
|
|
|
|
builder.Select("org_id").
|
|
|
|
From("org_user").
|
|
|
|
Where(builder.Eq{"uid": userID}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
var repos []*Repository
|
|
|
|
return repos, db.GetEngine(ctx).Table("repository").Where(cond).Find(&repos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetForksByUserAndOrgs return forked repos of the user and owned orgs
|
|
|
|
func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *Repository) ([]*Repository, error) {
|
|
|
|
var repoList []*Repository
|
|
|
|
if user == nil {
|
|
|
|
return repoList, nil
|
|
|
|
}
|
|
|
|
forkedRepo, err := GetUserFork(ctx, repo.ID, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return repoList, err
|
|
|
|
}
|
|
|
|
if forkedRepo != nil {
|
|
|
|
repoList = append(repoList, forkedRepo)
|
|
|
|
}
|
|
|
|
orgForks, err := FindUserOrgForks(ctx, repo.ID, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repoList = append(repoList, orgForks...)
|
|
|
|
return repoList, nil
|
|
|
|
}
|