2017-10-27 09:10:54 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
2019-12-15 12:51:28 +03:00
import (
"fmt"
"xorm.io/builder"
)
2017-10-27 09:10:54 +03:00
// RepoIndexerStatus status of a repo's entry in the repo indexer
// For now, implicitly refers to default branch
type RepoIndexerStatus struct {
ID int64 ` xorm:"pk autoincr" `
RepoID int64 ` xorm:"INDEX" `
CommitSha string ` xorm:"VARCHAR(40)" `
}
2019-12-15 12:51:28 +03:00
// GetUnindexedRepos returns repos which do not have an indexer status
func GetUnindexedRepos ( maxRepoID int64 , page , pageSize int ) ( [ ] int64 , error ) {
ids := make ( [ ] int64 , 0 , 50 )
cond := builder . Cond ( builder . IsNull {
"repo_indexer_status.id" ,
} )
sess := x . Table ( "repository" ) . Join ( "LEFT OUTER" , "repo_indexer_status" , "repository.id = repo_indexer_status.repo_id" )
if maxRepoID > 0 {
cond = builder . And ( cond , builder . Lte {
"repository.id" : maxRepoID ,
} )
}
if page >= 0 && pageSize > 0 {
start := 0
if page > 0 {
start = ( page - 1 ) * pageSize
}
sess . Limit ( pageSize , start )
}
sess . Where ( cond ) . Cols ( "repository.id" ) . Desc ( "repository.id" )
err := sess . Find ( & ids )
return ids , err
}
2019-12-08 22:15:35 +03:00
// GetIndexerStatus loads repo codes indxer status
func ( repo * Repository ) GetIndexerStatus ( ) error {
2017-10-27 09:10:54 +03:00
if repo . IndexerStatus != nil {
return nil
}
status := & RepoIndexerStatus { RepoID : repo . ID }
has , err := x . Get ( status )
if err != nil {
return err
} else if ! has {
status . CommitSha = ""
}
repo . IndexerStatus = status
return nil
}
2019-12-08 22:15:35 +03:00
// UpdateIndexerStatus updates indexer status
func ( repo * Repository ) UpdateIndexerStatus ( sha string ) error {
if err := repo . GetIndexerStatus ( ) ; err != nil {
2019-12-15 12:51:28 +03:00
return fmt . Errorf ( "UpdateIndexerStatus: Unable to getIndexerStatus for repo: %s/%s Error: %v" , repo . MustOwnerName ( ) , repo . Name , err )
2017-10-27 09:10:54 +03:00
}
if len ( repo . IndexerStatus . CommitSha ) == 0 {
repo . IndexerStatus . CommitSha = sha
_ , err := x . Insert ( repo . IndexerStatus )
2019-12-15 12:51:28 +03:00
if err != nil {
return fmt . Errorf ( "UpdateIndexerStatus: Unable to insert repoIndexerStatus for repo: %s/%s Sha: %s Error: %v" , repo . MustOwnerName ( ) , repo . Name , sha , err )
}
return nil
2017-10-27 09:10:54 +03:00
}
repo . IndexerStatus . CommitSha = sha
_ , err := x . ID ( repo . IndexerStatus . ID ) . Cols ( "commit_sha" ) .
Update ( repo . IndexerStatus )
2019-12-15 12:51:28 +03:00
if err != nil {
return fmt . Errorf ( "UpdateIndexerStatus: Unable to update repoIndexerStatus for repo: %s/%s Sha: %s Error: %v" , repo . MustOwnerName ( ) , repo . Name , sha , err )
}
return nil
2017-10-27 09:10:54 +03:00
}