@ -23,6 +23,8 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
)
// ErrUserDoesNotHaveAccessToRepo represets an error where the user doesn't has access to a given repo.
@ -319,13 +321,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
// UnitEnabled if this repository has the given unit enabled
func ( repo * Repository ) UnitEnabled ( tp unit . Type ) ( result bool ) {
if err := db . WithContext ( func ( ctx * db . Context ) error {
result = repo . UnitEnabledCtx ( ctx , tp )
return nil
} ) ; err != nil {
log . Error ( "repo.UnitEnabled: %v" , err )
}
return
return repo . UnitEnabledCtx ( db . DefaultContext , tp )
}
// UnitEnabled if this repository has the given unit enabled
@ -760,33 +756,28 @@ func CountRepositories(ctx context.Context, opts CountRepositoryOptions) (int64,
return count , nil
}
// StatsCorrectNumClosed update repository's issue related numbers
func StatsCorrectNumClosed ( ctx context . Context , id int64 , isPull bool , field string ) error {
_ , err := db . Exec ( ctx , "UPDATE `repository` SET " + field + "=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_closed=? AND is_pull=?) WHERE id=?" , id , true , isPull , id )
return err
}
// UpdateRepoIssueNumbers update repository issue numbers
// UpdateRepoIssueNumbers updates one of a repositories amount of (open|closed) (issues|PRs) with the current count
func UpdateRepoIssueNumbers ( ctx context . Context , repoID int64 , isPull , isClosed bool ) error {
e := db . GetEngine ( ctx )
if isPull {
if _ , err := e . ID ( repoID ) . Decr ( "num_pulls" ) . Update ( new ( Repository ) ) ; err != nil {
return err
}
field := "num_"
if isClosed {
if _ , err := e . ID ( repoID ) . Decr ( "num_closed_pulls" ) . Update ( new ( Repository ) ) ; err != nil {
return err
}
field += "closed_"
}
if isPull {
field += "pulls"
} else {
if _ , err := e . ID ( repoID ) . Decr ( "num_issues" ) . Update ( new ( Repository ) ) ; err != nil {
return err
field += "issues"
}
if isClosed {
if _ , err := e . ID ( repoID ) . Decr ( "num_closed_issues" ) . Update ( new ( Repository ) ) ; err != nil {
subQuery := builder . Select ( "count(*)" ) .
From ( "issue" ) . Where ( builder . Eq {
"repo_id" : repoID ,
"is_pull" : isPull ,
} . And ( builder . If ( isClosed , builder . Eq { "is_closed" : isClosed } ) ) )
// builder.Update(cond) will generate SQL like UPDATE ... SET cond
query := builder . Update ( builder . Eq { field : subQuery } ) .
From ( "repository" ) .
Where ( builder . Eq { "id" : repoID } )
_ , err := db . Exec ( ctx , query )
return err
}
}
}
return nil
}